„Benutzer:S2cchst/API“ – Versionsunterschied
Erscheinungsbild
Inhalt gelöscht Inhalt hinzugefügt
Update |
K edittoken wird zweimal urlencode |
||
Zeile 142: | Zeile 142: | ||
'format' => 'php' |
'format' => 'php' |
||
); |
); |
||
list($header, $content) = PostRequest("http://de.wikipedia.org/w/api.php", |
list($header, $content) = PostRequest("http://de.wikipedia.org/w/api.php",'http://'.$_SERVER['REMOTE_ADDR'],$data); |
||
$content = unserialize($content); |
$content = unserialize($content); |
||
mysql_query("TRUNCATE TABLE `cookies`;"); |
mysql_query("TRUNCATE TABLE `cookies`;"); |
||
Zeile 185: | Zeile 185: | ||
'prop' => 'info', |
'prop' => 'info', |
||
'intoken' => 'edit', |
'intoken' => 'edit', |
||
'titles' => |
'titles' => $atitle, |
||
//'token' => $wplgtoken, |
//'token' => $wplgtoken, |
||
'format' => 'php' |
'format' => 'php' |
||
Zeile 210: | Zeile 210: | ||
'text' => $text, |
'text' => $text, |
||
'starttimestamp' => $timestamp, |
'starttimestamp' => $timestamp, |
||
'token' => |
'token' => $edittoken, |
||
'format' => 'php' |
'format' => 'php' |
||
); |
); |
Version vom 12. Oktober 2010, 20:40 Uhr
Dies ist ein auf der MediaWiki API basierendes Framework mit Funktionen für Bots. Als Grundlage diente das alte Skript vom PmegBot, welches ich im prinzip übernommen und die WP-Zugriffe auf API-Funktionen umgestellt habe.
Damit das Framework funktioniert wird noch der Code von PHPPOSTRequest benötigt, der als PostRequest.php in mein Framework includiert wird und eine Funktion für den zum Zugriff auf die Wikipedia nötigen POST-Request bereitstellt.
Das Framework ist noch nicht komplett. Es fehlen noch die restlichen API-Schnittstellen und eine Internationalisierung, bis jetzt greift das Skript nur auf http://de.wikipedia.org/w/api.php zu.
<?php
include_once("PostRequest.inc.php");
mysql_connect("localhost","root","");
mysql_select_db("bot");
// Variable for status message reporting
$status = '';
/**
* void savecookie(string cookie_name, string cookie_value);
* Saves a cookie into MYSQL Database
*/
function savecookie($cookie_name, $cookie_value)
{
$result = mysql_query("SELECT * FROM `cookies` WHERE `cookie_name` = '".$cookie_name."';");
if(mysql_num_rows($result) != 0)
{
mysql_query("DELETE FROM `cookies` WHERE `cookie_name` = '".$cookie_name."';");
mysql_query("INSERT INTO `cookies` SET `cookie_name` = '".$cookie_name."', `cookie_value` = '".$cookie_value."';");
status(mysql_error());
}
else
{
mysql_query("INSERT INTO `cookies` SET `cookie_name` = '".$cookie_name."', `cookie_value` = '".$cookie_value."';");
}
}
/*
* void parsecookie(string http_string);
* Extract Cookie-Data and saves them with savecookie()
*/
function parsecookies($content)
{
preg_match_all("^Set-Cookie: (.*?)=(.*?);^", $content, $cookies);
for($x = 0; $x < count($cookies[1]); $x++)
{
savecookie($cookies[1][$x], $cookies[2][$x]);
}
status("All cookies updated.");
}
/*
* int getlastactivity(void);
* returns last activity from bot (phantom cookie = time)
*/
function getlastactivity()
{
$result = mysql_query("SELECT * FROM `cookies` WHERE `cookie_name` = 'time';");
while($data = mysql_fetch_object($result))
return $data->cookie_value;
}
/*
* string getcookies(void);
* Returns all saved cookie, seperated by ; - started with Cookie:
*/
function getcookies()
{
$return = 'Cookie: ';
$result = mysql_query("SELECT * FROM `cookies`;");
while($data = mysql_fetch_object($result)) $return .= $data->cookie_name."=".$data->cookie_value."; ";
return substr($return, 0, -2);
}
/**
* void status(string text);
* Add status message into status variable
*/
function status($text)
{
global $status;
$status .= "<font face=\"Courier New\" size=\"2\">[".date("H:i:s")."] $text</font><br>";
}
/**
* array login(string lgname, string lgpassword);
* Login with nickname and password
*/
function login($lgname, $lgpassword, $wikilang)
{
status("Login as $lgname");
$data = array(
'action' => 'login',
'lgname' => $lgname,
'lgpassword' => $lgpassword,
'format' => 'php'
);
list($header, $content) = PostRequest('http://'.$wikilang.'.wikipedia.org/w/api.php','http://'.$_SERVER['REMOTE_ADDR'], $data);
$content = unserialize($content);
$result = $content['login']['result'];
$token = $content['login']['token'];
$prefix = $content['login']['cookieprefix'];
$sessionid = $content['login']['sessionid'];
$lgaddheader = 'Cookie: '.$prefix.'_session='.$sessionid.'; path=/;domain=.wikipedia.org;HttpOnly';
status("Confirm Token");
$data2 = array(
'action' => 'login',
'lgname' => $lgname,
'lgpassword' => $lgpassword,
'lgtoken' => $token,
'format' => 'php'
);
list($header, $content) = PostRequest('http://'.$wikilang.'.wikipedia.org/w/api.php','http://'.$_SERVER['REMOTE_ADDR'], $data2, $lgaddheader);
$content = unserialize($content);
$result = $content['login']['result'];
$userid = $content['login']['lguserid'];
$username = $content['login']['lgusername'];
$token = $content['login']['lgtoken'];
$cookieprefix = $content['login']['cookieprefix'];
$sessionid = $content['login']['sessionid'];
if($result == 'Success')
{
status("<font style=\"color:#00FF00;\">Login successful</font>");
parsecookies($header);
}
else
{
status("<font style=\"color:#FF0000;\">Login failed</font>");
die;
}
}
/*
* void logout(void);
* logged out and delete all cookies
*/
function logout()
{
$data = array(
'action' => 'logout',
'format' => 'php'
);
list($header, $content) = PostRequest("http://de.wikipedia.org/w/api.php",'http://'.$_SERVER['REMOTE_ADDR'],$data);
$content = unserialize($content);
mysql_query("TRUNCATE TABLE `cookies`;");
status("<font style=\"color:#FF0000;\">logged out</font>");
}
/*
* array getcontent(var title);
* Get the content of article with name title and the saving attributes
* array ("text" => content of article with name title, "attr" => attributes)
*/
function getcontent($title, $wikilang)
{
status("Fetching the article ".$title);
$dbresult = mysql_query("SELECT * FROM `cookies` WHERE `cookie_name` = 'centralauth_Token';");
while($t = mysql_fetch_object($dbresult))
{
$token = $t->cookie_value;
}
$data = array(
'action' => 'query',
'prop' => 'info',
'rvprop' => 'content',
'titles' => $title,
'lgtoken' => $token,
'format' => 'php'
);
list($header, $content) = PostRequest("http://".$wikilang.".wikipedia.org/w/api.php","http://".$_SERVER['REMOTE_ADDR'], $data);
status("Fetching article $title succeded");
return unserialize($content);
}
/*
* void savecontent(string title, string text, string summary)
* Saves an edited article back to Wikipedia
*/
function savecontent($atitle, $text, $summary, $wikilang)
{
status("requesting for edittoken for article $title");
$scaddheader = getcookies();
$data = array(
'action' => 'query',
'prop' => 'info',
'intoken' => 'edit',
'titles' => $atitle,
//'token' => $wplgtoken,
'format' => 'php'
);
list($header, $content) = PostRequest("http://".$wikilang.".wikipedia.org/w/api.php","http://".$_SERVER['REMOTE_ADDR'], $data, $scaddheader);
$content = unserialize($content);
parsecookies($header);
print "<br><br>$header<br><br>";
var_dump($content);
print "<br><br>";
$scaddheader.=getcookies();
status("Save new Version of article $atitle");
$namespace = $content['query']['pages'][-1]['ns'];
$title = $content['query']['pages'][-1]['title'];
$missing = $content['query']['pages'][-1]['missing'];
$timestamp = $content['query']['pages'][-1]['starttimestamp'];
$edittoken = $content['query']['pages'][-1]['edittoken'];
$edittoken = $edittoken;
print "<br><br><b>Edittoken:</b> $edittoken<br><br>";
$data = array(
'action' => 'edit',
'title' => $atitle,
'summary' => $summary,
'text' => $text,
'starttimestamp' => $timestamp,
'token' => $edittoken,
'format' => 'php'
);
print '<b>Data:</b><br>';
var_dump($data);
print "<br><br>$scaddheader<br><br>";
list($header, $content) = PostRequest("http://".$wikilang.".wikipedia.org/w/api.php","http://".$_SERVER['REMOTE_ADDR'], $data, $scaddheader);
$content = unserialize($content);
print "<br><br>";
print $header."<br><br>";
var_dump($content);
print "<br><br>";
}
login("s2cchst","****", "de");
$article = getcontent("Wikipedia Diskussion:Hauptseite", "de");
//print "<br><br>$article<br><br>";
var_dump($article);
savecontent("Benutzer:S2cchst/Log","Test","Bot: Log erstellt", "de");
logout();
print $status;
?>