Benutzer:Jan/Antragsconverter
Zur Navigation springen
Zur Suche springen
Aktuelle Version BPT12.2
<?php $API = 'http://wiki.piratenpartei.de/wiki/api.php'; $WIKIDOMAIN = 'http://wiki.piratenpartei.de/'; $PARTEITAG = 'Bundesparteitag'; $JAHRESZAHL = '2012.2'; $ANTRAGSVIEWERNAME = "Anträge zum BPT 2012.2"; // MUST BE JSON-SAFE, WILL NOT BE ESCAPED! $PREFIX = "$PARTEITAG $JAHRESZAHL/Antragsportal/"; // must occur only once in titles (see below)! $NAMESPACENAME = "Antrag"; $NAMESPACENUMBER = "262"; // see http://wiki.piratenpartei.de/wiki/api.php?action=query&meta=siteinfo&siprop=namespaces $BAUSTEIN_REGEXP = "\\{\\{(Programmantrag|Satzungsänderungsantrag|Sonstiger Antrag) BPT"; $ERSATZBAUSTEIN = "{{:Benutzer:Jan/AntragsvorlageAntragsviewerBPT"; //$RENDERLIMIT = 1; // Limit rendering while debugging $continue = $PREFIX; $foundpages = array(); $wikifile = fopen("wiki.txt", "w"); @mkdir("data"); function wikiout($text) { global $wikifile; fputs($wikifile, $text); echo $text; } function putfile($name, $text) { $file = fopen($name, "w"); fputs($file, $text); fclose($file); } $rendered = 0; function render($text, $title) { global $WIKIDOMAIN; global $API; global $BAUSTEIN_REGEXP; global $ERSATZBAUSTEIN; global $RENDERLIMIT; global $rendered; if (!empty($RENDERLIMIT) && $rendered++ > $RENDERLIMIT) return $text; // For debugging - limit rendering echo "Rendering $title..."; $text = preg_replace("/^".$BAUSTEIN_REGEXP."\n/", "$ERSATZBAUSTEIN\n", $text); $postdata = http_build_query( array( 'action' => 'parse', 'title' => $title, 'prop' => 'text', 'text' => $text, 'format' => 'xml' ) ); $opts = array( 'http'=>array( 'method'=>"POST", 'header' => 'Content-type: application/x-www-form-urlencoded', 'content' => $postdata, ) ); $context = stream_context_create($opts); $rawresult = file_get_contents($API, false, $context) or die('Wiki wollte keinen Text liefern'); $result = simplexml_load_string($rawresult); $html = "".$result->parse->text; $html = preg_replace('!href="/!','href="'.$WIKIDOMAIN, $html); $html = preg_replace('!src="/!','src="'.$WIKIDOMAIN, $html); echo " done.\n"; return $html; } wikiout("<pre>\n"); while (true) { wikiout("Hole Seiten ab '$continue'...\n"); $url = $API.'?format=xml&action=query&prop=revisions&rvprop=content&generator=allpages&gaplimit=50&gapprefix='.urlencode($PREFIX).'&gapfrom='.urlencode($continue); if ($NAMESPACENUMBER) { $url .= '&gapnamespace='.$NAMESPACENUMBER; } $rawresult = file_get_contents($url) or die('Keine Antwort vom Wiki - URL: '. $url); $result = simplexml_load_string($rawresult); if ($result == '<?xml version="1.0"?><api />') { wikiout("Herunterladen abgeschlossen.\n\n"); break; } if ($result->{'warnings'}) { wikiout("WARNUNG: " . $result->{'warnings'}->asXML() . "\n"); } foreach ($result->{'query'}->{'pages'}->{'page'} as $page) { $title = $page->attributes()->{'title'}; $title = str_replace((empty($NAMESPACENAME)?'':"$NAMESPACENAME:").$PREFIX, "", $title); // WARNING: If prefix appears multiple times, it will be removed each time! $content = "".$page->{'revisions'}->{'rev'}[0]; $foundpages[] = array('title'=>$title, 'content'=>$content); } if ($result->{'query-continue'}) { $continue = "".$result->{'query-continue'}->{'allpages'}->attributes()->{'gapfrom'}; } else { wikiout("Herunterladen abgeschlossen.\n\n"); break; } } // Reihenfolge korrigieren: Erst Programmanträge (PA001), dann Positionspapiere (P001) function comparator($a, $b) { $astr = $a['title']; $bstr = $b['title']; // Programmanträge bekommen für das Sortieren eine "1" an den Anfang $astr = preg_replace('/^PA/', '1PA', $astr); $bstr = preg_replace('/^PA/', '1PA', $bstr); // Danach wird normal sortiert return strcmp($astr, $bstr); } usort($foundpages, 'comparator'); $output = array(); $jsonout = array(); $folderid = 0; $seen_ids = array(); $limesurvey =""; $limesurvey2 =""; for ($i = 0; $i < count($foundpages); $i++) { $text = $foundpages[$i]['content']; $title = $foundpages[$i]['title']; $lines = explode("\n", $text); if (!preg_match("/^".$BAUSTEIN_REGEXP."$/", array_shift($lines))) { wikiout("WARNUNG: Seite '$title' ist kein (sauberer) Antrag\n"); continue; } if (!preg_match('/^(PA|P|SÄA|X)([0-9]{3})$/', $title, $matches)) { wikiout("WARNUNG: Verstehe Titel '$title' nicht!\n"); continue; } if ($matches[1] == "PA") $type = "programm"; elseif ($matches[1] == "P") $type = "pospap"; elseif ($matches[1] == "SÄA") $type = "satzung"; elseif ($matches[1] == "X") $type = "sonstiges"; else die("Programmierfehler"); $id = $matches[2]; $properties = array(); foreach ($lines as $line) { if (preg_match('/^\\|\s*([a-zA-Z0-9äöüÄÖÜß]+)\s*=(.*)$/', $line, $matches)) { if ($matches[1] == "text" || $matches[1] == "begruendung") continue; $properties[$matches[1]] = $matches[2]; } } if (empty($properties['antragsgruppe'])) wikiout("Info: '$title' fehlt die Gruppe\n"); $group = empty($properties['antragsgruppe']) ? "_Fehlende Gruppe_" : $properties['antragsgruppe']; if (empty($properties['antragstyp'])) { wikiout("WARNUNG: '$title' hat keinen Antragstyp\n"); $properties['antragstyp'] = "ANTRAGSTYP FEHLT"; } $titlesuffix = ""; switch ($type) { case "programm": $idprefix = "PAPA"; $titlesuffix = ' ('.$properties['antragstyp'].')'; if ($properties['antragstyp'] != "Grundsatzprogramm" && $properties['antragstyp'] != "Wahlprogramm") { wikiout("WARNUNG: '$title' hat falschen Antragstyp ".$properties['antragstyp']."\n"); } break; case "pospap": $idprefix = "PAP"; $titlesuffix = ' (Positionspapier)'; if ($properties['antragstyp'] != "Positionspapier") wikiout("WARNUNG: '$title' hat falschen Antragstyp ".$properties['antragstyp']."\n"); $type = "programm"; // Positionspapiere zu Programmanträgen einsortieren break; case "satzung": $idprefix = "SAEASAEA"; if ($properties['antragstyp'] != "Satzungsänderungsantrag") wikiout("WARNUNG: '$title' hat falschen Antragstyp ".$properties['antragstyp']."\n"); break; case "sonstiges": $idprefix = "SOX"; if ($properties['antragstyp'] != "Sonstiger Antrag" && $properties['antragstyp'] != "Sonstiges") { wikiout("WARNUNG: '$title' hat falschen Antragstyp ".$properties['antragstyp']."\n"); } break; } if (!empty($properties['autor'])) { $autor = $properties['autor']; } if (!empty($properties['titel'])) { $nicetitle = $properties['titel']; } else { $nicetitle = "(ohne Name)"; wikiout("Info: '$title' hat keinen Titel\n"); } $fullid = $idprefix.$id; if (!empty($seen_ids[$fullid])) { echo "FEHLER: Mehrfach vergebene ID $fullid (sollte nie passieren)\n"; exit(1); } $seen_ids[$fullid] = 1; $displaytitle = "$title - $nicetitle$titlesuffix"; $output[$type][$group][] = array("title"=>$title, "name"=>$displaytitle, "id"=>$fullid, "suffix"=>$titlesuffix); if (empty($jsonout['folders'][$type][$group])) { $foldername = 'folder'.(++$folderid); $jsonout['folders'][$type][$group] = array('id' => $foldername, 'name' => $group, 'dataurl' => "data/$foldername.json"); } else { $foldername = $jsonout['folders'][$type][$group]['id']; } $html = render($text, (empty($NAMESPACENAME)?'':"$NAMESPACENAME:").$PREFIX.$title); $jsonout['data'][$foldername]['messages'][] = array('id'=>$fullid, 'subject' => $displaytitle, 'text' => $html, 'sender' => $autor); $jsonout['data'][$foldername]['rootMessages'][] = $fullid; $jsonout['index'][$fullid] = $foldername; } wikiout("< /pre>\n\n"); DAS LEERZEICHEN OBEN WEGMACHEN, MUSS SEIN UM ES INS WIKI PASTEN ZU KÖNNEN. function printWikiSection($header, $key) { global $output; global $NAMESPACENAME; global $PREFIX; wikiout("\n\n== $header ==\n"); if (!empty($output[$key])) { uksort($output[$key], 'strnatcmp'); foreach ($output[$key] as $gruppe => $inhalt) { wikiout("\n=== $gruppe ===\n"); foreach ($inhalt as $eintrag) { wikiout('* [['.(empty($NAMESPACENAME)?'':"$NAMESPACENAME:").$PREFIX.$eintrag['title']."|".$eintrag['name']."]]\n"); } } } } printWikiSection("Programmanträge", "programm"); printWikiSection("Satzungsänderungsanträge", "satzung"); printWikiSection("Sonstige Anträge", "sonstiges"); $jsonfolders = array(); function prepareJSON($key) { global $jsonfolders; global $jsonout; if (empty($jsonout['folders'][$key])) { $jsonfolders[$key] = '[]'; } else { uksort($jsonout['folders'][$key], 'strnatcmp'); $jsonfolders[$key] = json_encode(array_values($jsonout['folders'][$key])); } } prepareJSON("programm"); prepareJSON("satzung"); prepareJSON("sonstiges"); putfile("data/main.json", <<<EOT { "folders":[ {"id":"programm", "name":"Programmanträge", "dataurl":"data\/emptyfolder.json", "children":{$jsonfolders['programm']}}, {"id":"satzung", "name":"Satzungsanträge", "dataurl":"data\/emptyfolder.json", "children":{$jsonfolders['satzung']}}, {"id":"sonstiges", "name":"Sonstige Anträge", "dataurl":"data\/emptyfolder.json", "children":{$jsonfolders['sonstiges']}} ], "settings":{"title":"$ANTRAGSVIEWERNAME","autoMarkRead":true,"showSender":false,"showDate":false,"showScore":false,"allowReply":false,"allowVoting":true,"allowInterest":false,"readOnExpand":true}, "searchindex":"data\/msgindex.json" } EOT ); putfile("data/emptyfolder.json", <<<EOT {"messages":[{"id":"emptymsg","subject":"Wähle einen Unterordner, um Anträge zu sehen","text":"Wähle links im Menü einen Unterordner aus, um Anträge zu sehen","settings":{"allowVoting":false}}],"rootMessages":["emptymsg"], "settings":[]} EOT ); foreach($jsonout['data'] as $key => $value) { $value['settings'] = array(); putfile("data/$key.json", json_encode($value)); } putfile("data/msgindex.json", json_encode($jsonout['index'])); // Limesurvey-Output @mkdir("limesurvey"); function generateLimesurvesSection($key) { global $output; global $NAMESPACENAME; global $PREFIX; $limefile = fopen("limesurvey/limesurvey-$key.txt", "w"); if (!empty($output[$key])) { // already sorted by wiki-output: uksort($output[$key], 'strnatcmp'); foreach ($output[$key] as $gruppe => $inhalt) { foreach ($inhalt as $eintrag) { $varname = htmlspecialchars($eintrag['id']); $text = htmlspecialchars($eintrag['title'] . ' - ' . $gruppe . $eintrag['suffix']); $data = <<<EOT <subQuestion varName="$varname"> <text>$text</text> </subQuestion> EOT; fputs($limefile, $data); } } } fclose($limefile); } generateLimesurvesSection("programm"); generateLimesurvesSection("satzung"); generateLimesurvesSection("sonstiges"); /* {{Programmantrag BPT |bundesparteitag=2012.2 |autor=Jan |antragstyp=Wahlprogramm |antragsgruppe=Freiheit und Grundrechte |titel=Nicht medizinisch indizierte Eingriffe / Beschneidung |zusammenfassung=Die Piratenpartei lehnt ... |text=''Der Bundesparteitag möge beschließen, den folgenden Text an geeigneter Stelle in das Wahlprogramm aufzunehmen'': Die Piratenpartei lehnt ... |begruendung=''Dieser Antrag basiert auf einem in Hessen mit 2/3-Mehrheit positiv beantworteten [[HE:Meinungsbilder/Beschneidung|Meinungsbild]]'' Medizinisch notwendige ... |schlagworte=minderjährige, kinder, chirurgische eingriffe, beschneidung |prüficon=1 |abstimmung=1 |wikiBenutzer=Jan }} */
Alte Version
<?php $API = 'http://wiki.piratenpartei.de/wiki/api.php'; $WIKIDOMAIN = 'http://wiki.piratenpartei.de/'; $PARTEITAG = 'Bundesparteitag'; $JAHRESZAHL = '2012.1'; $PREFIX = "$PARTEITAG $JAHRESZAHL/Antragsportal/"; // must occur only once in titles (see below)! $continue = $PREFIX; $foundpages = array(); $wikifile = fopen("wiki.txt", "w"); function wikiout($text) { global $wikifile; fputs($wikifile, $text); echo $text; } function putfile($name, $text) { $file = fopen($name, "w"); fputs($file, $text); fclose($file); } $rendered = 0; function render($text, $title) { global $WIKIDOMAIN; global $rendered; //if ($rendered++ > 3) return $text; // For debugging - limit rendering $text = preg_replace("/^\\{\\{Antragsformular\n/", "{{:Benutzer:Jan/AntragsvorlageAntragsviewer\n", $text); $postdata = http_build_query( array( 'action' => 'parse', 'title' => $title, 'prop' => 'text', 'text' => $text, 'format' => 'xml' ) ); $opts = array( 'http'=>array( 'method'=>"POST", 'header' => 'Content-type: application/x-www-form-urlencoded', 'content' => $postdata, ) ); $context = stream_context_create($opts); $rawresult = file_get_contents('http://wiki.piratenpartei.de/wiki/api.php', false, $context) or die('Wiki wollte keinen Text liefern'); $result = simplexml_load_string($rawresult); $html = "".$result->parse->text; $html = preg_replace('!href="/!','href="'.$WIKIDOMAIN, $html); $html = preg_replace('!src="/!','src="'.$WIKIDOMAIN, $html); return $html; } wikiout("<pre>\n"); while (true) { wikiout("Hole Seiten ab '$continue'...\n"); $url = $API.'?format=xml&action=query&prop=revisions&rvprop=content&generator=allpages&gaplimit=500&gapprefix='.urlencode($PREFIX).'&gapfrom='.urlencode($continue); $rawresult = file_get_contents($url) or die('Keine Antwort vom Wiki - URL: '. $url); $result = simplexml_load_string($rawresult); if ($result == '<?xml version="1.0"?><api />') { wikiout("Herunterladen abgeschlossen.\n\n"); break; } if ($result->{'warnings'}) { wikiout("WARNUNG: " . $result->{'warnings'}->asXML() . "\n"); } foreach ($result->{'query'}->{'pages'}->{'page'} as $page) { $title = $page->attributes()->{'title'}; $title = str_replace($PREFIX, "", $title); // WARNING: If prefix appears multiple times, it will be removed each time! $content = "".$page->{'revisions'}->{'rev'}[0]; $foundpages[] = array('title'=>$title, 'content'=>$content); } if ($result->{'query-continue'}) { $continue = "".$result->{'query-continue'}->{'allpages'}->attributes()->{'gapfrom'}; } else { wikiout("Herunterladen abgeschlossen.\n\n"); break; } } $output = array(); $jsonout = array(); $folderid = 0; $seen_ids = array(); for ($i = 0; $i < count($foundpages); $i++) { $text = $foundpages[$i]['content']; $title = $foundpages[$i]['title']; $lines = explode("\n", $text); if (array_shift($lines) != '{{Antragsformular' || array_pop($lines) != "}}") { wikiout("WARNUNG: Seite '$title' ist kein (sauberer) Antrag\n"); continue; } if (!preg_match('/^(Programmantrag|Satzungsänderungsantrag|Sonstiger Antrag) - ([0-9]{3})$/', $title, $matches)) { wikiout("WARNUNG: Verstehe Titel '$title' nicht!\n"); continue; } if ($matches[1] == "Programmantrag") $type = "programm"; elseif ($matches[1] == "Satzungsänderungsantrag") $type = "satzung"; elseif ($matches[1] == "Sonstiger Antrag") $type = "sonstiges"; else die("Programmierfehler"); $id = $matches[2]; $properties = array(); foreach ($lines as $line) { if (preg_match('/^\\|\s*([a-zA-Z0-9äöüÄÖÜß]+)\s*=(.*)$/', $line, $matches)) { if ($matches[1] == "text" || $matches[1] == "begruendung") continue; $properties[$matches[1]] = $matches[2]; } } $titlesuffix = ""; if (!empty($properties['artprogrammantrag'])) { $titlesuffix = ' ('.$properties['artprogrammantrag'].')'; if ($type != "programm") { wikiout("WARNUNG: '$title' ist ein Programmantrag\n"); } } else { if ($type == "programm") wikiout("Hinweis: '$title' hat keine Art\n"); } switch ($type) { case "programm": if (!empty($properties['antragsgruppeSA'])) wikiout("Info: '$title' hat falsche Gruppe\n"); if (!empty($properties['antragsgruppeSoA'])) wikiout("Info: '$title' hat falsche Gruppe\n"); if (empty($properties['antragsgruppePA'])) wikiout("Info: '$title' fehlt die Gruppe\n"); $group = empty($properties['antragsgruppePA']) ? "_Fehlende Gruppe_" : $properties['antragsgruppePA']; $titleprefix = "P"; break; case "satzung": if (!empty($properties['antragsgruppePA'])) wikiout("Info: '$title' hat falsche Gruppe\n"); if (!empty($properties['antragsgruppeSoA'])) wikiout("Info: '$title' hat falsche Gruppe\n"); if (empty($properties['antragsgruppeSA'])) wikiout("Info: '$title' fehlt die Gruppe\n"); $group = empty($properties['antragsgruppeSA']) ? "_Fehlende Gruppe_" : $properties['antragsgruppeSA']; $titleprefix = "S"; break; case "sonstiges": if (!empty($properties['antragsgruppePA'])) wikiout("Info: '$title' hat falsche Gruppe\n"); if (!empty($properties['antragsgruppeSA'])) wikiout("Info: '$title' hat falsche Gruppe\n"); if (empty($properties['antragsgruppeSoA'])) wikiout("Info: '$title' fehlt die Gruppe\n"); $group = empty($properties['antragsgruppeSoA']) ? "_Fehlende Gruppe_" : $properties['antragsgruppeSoA']; $titleprefix = "X"; break; } if (!empty($properties['autor'])) { $autor = $properties['autor']; } if (!empty($properties['titel'])) { $nicetitle = $properties['titel']; } else { $nicetitle = $title; if (empty($properties['antragsgruppeSoA'])) wikiout("Info: '$title' hat keinen Titel\n"); } $fullid = $titleprefix.$id; if (!empty($seen_ids[$fullid])) { echo "FEHLER: Mehrfach vergebene ID $fullid (sollte nie passieren)\n"; exit(1); } $seen_ids[$fullid] = 1; $displaytitle = "$fullid $nicetitle$titlesuffix"; $output[$type][$group][] = array("title"=>$title, "name"=>$displaytitle); if (empty($jsonout['folders'][$type][$group])) { $foldername = 'folder'.(++$folderid); $jsonout['folders'][$type][$group] = array('id' => $foldername, 'name' => $group, 'dataurl' => "data/$foldername.json"); } else { $foldername = $jsonout['folders'][$type][$group]['id']; } $html = render($text, $PREFIX.$title); $jsonout['data'][$foldername]['messages'][] = array('id'=>$fullid, 'subject' => $displaytitle, 'text' => $html, 'sender' => $autor); $jsonout['data'][$foldername]['rootMessages'][] = $fullid; } wikiout("< /pre>\n\n"); DAS LEERZEICHEN OBEN WEGMACHEN, MUSS SEIN UM ES INS WIKI PASTEN ZU KÖNNEN. $folderid = 0; $folders = array("programm"=>array(), "satzung"=>array(), "sonstiges"=>array()); wikiout("\n\n== Programmanträge ==\n"); foreach ($output["programm"] as $gruppe => $inhalt) { wikiout("\n=== $gruppe ===\n"); foreach ($inhalt as $eintrag) { wikiout("* [[$PREFIX".$eintrag['title']."|".$eintrag['name']."]]\n"); } } wikiout("\n\n== Satzungsänderungsanträge ==\n"); foreach ($output["satzung"] as $gruppe => $inhalt) { wikiout("\n=== $gruppe ===\n"); foreach ($inhalt as $eintrag) { wikiout("* [[$PREFIX".$eintrag['title']."|".$eintrag['name']."]]\n"); } } wikiout("\n\n== Sonstige Anträge ==\n"); foreach ($output["sonstiges"] as $gruppe => $inhalt) { wikiout("\n=== $gruppe ===\n"); foreach ($inhalt as $eintrag) { wikiout("* [[$PREFIX".$eintrag['title']."|".$eintrag['name']."]]\n"); } } $jsonfolders_programm = json_encode(array_values($jsonout['folders']['programm'])); $jsonfolders_satzung = json_encode(array_values($jsonout['folders']['satzung'])); $jsonfolders_sonstiges = json_encode(array_values($jsonout['folders']['sonstiges'])); putfile("data/main.json", <<<EOT { "folders":[ {"id":"programm", "name":"Programmanträge", "dataurl":"data\/emptyfolder.json", "children":$jsonfolders_programm}, {"id":"satzung", "name":"Satzungsanträge", "dataurl":"data\/emptyfolder.json", "children":$jsonfolders_satzung}, {"id":"sonstiges", "name":"Sonstige Anträge", "dataurl":"data\/emptyfolder.json", "children":$jsonfolders_sonstiges} ], "settings":{"title":"Anträge zum BPT 2012.1","autoMarkRead":true,"showSender":false,"showDate":false,"showScore":false,"allowReply":false,"allowVoting":true,"allowInterest":false,"readOnExpand":true} } EOT ); putfile("data/emptyfolder.json", <<<EOT {"messages":[{"id":"emptymsg","subject":"Wähle einen Unterordner, um Anträge zu sehen","text":"Wähle links im Menü einen Unterordner aus, um Anträge zu sehen","settings":{"allowVoting":false}}],"rootMessages":["emptymsg"], "settings":[]} EOT ); foreach($jsonout['data'] as $key => $value) { $value['settings'] = array(); putfile("data/$key.json", json_encode($value)); } /* {{Antragsformular |parteitag=Bundesparteitag |jahreszahl=2012.1 |antragstyp=Programmantrag |artprogrammantrag=Positionspapier |titel=Grundschulen auf 6 Jahre verlängern |autor=Lessing |text=Der Bundesparteitag der Piratenpartei möge ... |begruendung=Das jetzige System... |prüficon=1 |urltype=Bundesparteitag 2012.1 }} */