Dies ist ein ReadOnly-Mirror von wiki.piratenpartei.de!
Die Daten werden täglich aktualisiert.
Die Daten werden täglich aktualisiert.
Benutzer:Jan/Protectbot
Zur Navigation springen
Zur Suche springen
Dieser Bot schützt alle Seiten, die mit einem bestimmten Präfix (z. B. "Bundesparteitag 2012.1/Antragsportal/") beginnen. Eine Liste der zu schützenden Seiten wird vorher angezeigt; es muss ein Benutzerkonto mit writeapi- und Seitenschutzrecht eingegeben werden.
Der folgende Code muss in eine Datei eingefügt und dann mit php ausgeführt werden. Falls SSL (Passwort verschlüsselt übertragen) richtig verwendet werden soll, muss in der Datei "trustedcerts.pem" eine Liste vertrauenswürdiger Zertifikate (im PEM-Format) liegen. Wenn man keinen Schutz gegen MitM-Angriffe haben will, kann man stattdessen auch $SECURE_SSL auf false setzen.
<?php
$API = 'https://wiki.piratenpartei.de/wiki/api.php';
$PARTEITAG = 'Bundesparteitag';
$JAHRESZAHL = '2012.1';
$PREFIX = "$PARTEITAG $JAHRESZAHL/Antragsportal/";
$REASON = "Antragsseite";
$SECURE_SSL = true;
$DEBUG = false;
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, "ProtectBot by Jan Schejbal");
curl_setopt($ch, CURLOPT_POST, true);
//curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie.tmp.txt");
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie.tmp.txt");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_MAXREDIRS, 7);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $SECURE_SSL);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, $SECURE_SSL ? 2 : 0);
curl_setopt($ch, CURLOPT_SSLVERSION, 3);
curl_setopt($ch, CURLOPT_CAINFO, 'trustedcerts.pem');
curl_setopt($ch, CURLOPT_URL, $API);
// data: array von POST-vars
function fetch($data) {
global $ch;
global $DEBUG;
$data['format'] = 'json';
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data)); // use this for urlencode encoding
//curl_setopt($ch, CURLOPT_POSTFIELDS, $data); // use this for multipart/form-data encoding
if ($DEBUG) {
echo "\n======================================================================\nRequesting:\n";
var_dump($data);
}
$response = curl_exec($ch);
if (!$response) die("Could not get response from server");
if ($DEBUG) {
echo "\n----------------------------------------------------------------------\nResponse:\n";
var_dump($response);
}
$decoded = json_decode($response,true);
if ($DEBUG) {
var_dump($decoded);
}
if (!empty($decoded['error']['code'])) {
echo "API error: ".$decoded['error']['code']."\n";
if (!empty($decoded['error']['info'])) echo " ".$decoded['error']['info']."\n\n";
exit(1);
}
return $decoded;
}
$titles = array();
echo "Fetching list of titles...\n";
$start = "";
do {
$result = fetch(array('action' => 'query', 'list' => 'allpages', 'apprefix' => $PREFIX, 'aplimit' => 500, 'apfrom' => $start));
if (!isset($result['query']['allpages']) || !is_array($result['query']['allpages'])) die("Unexpected response - missing page array");
foreach ($result['query']['allpages'] as $pagearray) {
if (!isset($pagearray['title'])) die("Unexpected response - missing title");
$titles[] = $pagearray['title'];
echo " ".$pagearray['title']."\n";
}
$continue = isset($result['query-continue']['allpages']['apfrom']);
if ($continue) $start = $result['query-continue']['allpages']['apfrom'];
} while ($continue);
echo "\nThe ".count($titles)." page(s) listed above will be protected.\n";
echo "To continue, enter your wiki login. To abort, press CTRL-C.\n";
echo "(warning: your password will be shown on the screen)\n\n";
echo "username: ";
$username = trim(fgets(STDIN));
echo "password: ";
$password = trim(fgets(STDIN));
echo "\nLogging in as '$username'\n";
$result = fetch(array('action' => 'login', 'lgname' => $username, 'lgpassword'=> $password));
if (empty($result['login']['result'])) die("Unexpected response to first login step");
if ($result['login']['result'] != "Success") {
if ($result['login']['result'] != "NeedToken") die("Login failed: ".$result['login']['result']);
if (empty($result['login']['token'])) die("Login failed - could not get login token");
$result = fetch(array('action' => 'login', 'lgname' => $username, 'lgpassword'=> $password, 'lgtoken' => $result['login']['token']));
if (empty($result['login']['result']) || $result['login']['result'] != "Success") die("Login failed: ".$result['login']['result']);
}
echo "Login successful, fetching token.\n";
$result = fetch(array('action' => 'query', 'prop' => 'info', 'intoken' => 'protect', 'titles' => 'Hauptseite'));
if (empty($result['query']['pages']) || !is_array($result['query']['pages'])) die("Unexpected response when fetching token");
$pagearray = array_pop($result['query']['pages']);
if (empty($pagearray['protecttoken'])) die("Could not get token");
$token = $pagearray['protecttoken'];
foreach ($titles as $title) {
echo "Protecting $title\n";
$result = fetch(array('action' => 'protect', 'title' => $title, 'token' => $token, 'protections' => 'edit=sysop|move=sysop', 'reason' => "[via Bot] - $REASON"));
}
echo "\nFinished\n\n";
curl_close($ch);