Benutzer:S2cchst/API
Zur Navigation springen
Zur Suche springen
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.
In der hier als Quellcode vorliegenden Version unterstützt das Framework erstmals einen Zugriff auf alle Wikipedia Projektseiten, dazu muss in den Funktionen mit dem Wikilang Attribut das Sprachkürzel der jeweiligen Wikipedia übergeben werden.
<?php
ini_set('max_execution_time',3600);
// Einbinden der PostRequest-Funktion
include_once("PostRequest.inc.php");
// Initializing variable for status-messages
$status = '';
// Initializing variable for authentification cookie data
$mwcookies = array();
/**
* void savecookies(string cookie_name, string cookie_value)
* saves a ccokie into variable mwcookies
*/
function savecookie($cookie_name, $cookie_value)
{
global $mwcookies;
$mwcookies[$cookie_name] = $cookie_value;
}
/**
* void parsecookies(string content)
* parsing all http-cookies
*/
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.");
}
/*
* string getlastactivity (void)
* returns last activity of the bot (phantom cookie = time)
*/
function getlastactivity()
{
global $mwcookies;
return $mwcookies['time'];
}
/**
* string getcookies(void)
* returns all cookies from variable mwcookies (Beginning with Cookies: )
*/
function getcookies()
{
global $mwcookies;
$return = 'Cookie: ';
foreach($mwcookies as $k => $v)
{
$return .= "$k=$v;";
}
return $return;
}
/**
* void status(string text)
* Add status-message into status variable
*/
function status($text)
{
global $status;
$status .= "<font face=\"Courier New\" size=\"2\">[".date("Y-m-d H:i:s")."] $text</font><br>";
}
/**
* void login(string lgname, string lgpassword, string wikilang)
* Login with username 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)
* Logout fron your account
*/
function logout()
{
global $mwcookies;
unset($mwcookies);
$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);
status("<font style=\"color:#FF0000;\">logged out</font>");
}
/**
* array getcontent(string title, string wikilang)
* fetching content from an article
*/
function getcontent($title, $wikilang)
{
status("Fetching page $title");
$title = preg_replace("/ /","_",$title);
$data = array(
'action' => 'query',
'prop' => 'info|revisions',
'rvprop' => 'content',
'titles' => $title,
'format' => 'php'
);
list($header, $content) = PostRequest('http://'.$wikilang.'.wikipedia.org/w/api.php','http://'.$_SERVER['REMOTE_ADDR'],$data);
parsecookies($header);
$content = unserialize($content);
$info = $content['query']['pages'];
foreach($info as $k=>$v)
{
$pageid = $k;
}
$pagedata = array();
$pagedata['pageid'] = $pageid;
$pagedata['ns'] = $info[$pageid]['ns'];
$pagedata['title'] = $info[$pageid]['title'];
$pagedata['missing'] = (isset($info[$pageid]['missing'])) ? (int) 1 : (int) 0;
$pagedata['text'] = $info[$pageid]['revisions'][0]['*'];
return $pagedata;
}
/**
* void savecontent(array adata, string wikilang)
* Saves an edited article back to wikipedia
*/
function savecontent($adata, $wikilang)
{
$adata['title'] = preg_replace("/ /","_",$adata['title']);
status("requesting for edittoken for article ".$adata['title']);
$cookies = getcookies();
$data = array(
'action' => 'query',
'prop' => 'info|revisions',
'intoken' => 'edit',
'titles' => $adata['title'],
'format' => 'php'
);
list($header, $content) = PostRequest('http://'.$wikilang.'.wikipedia.org/w/api.php',$_SERVER['HTTP_REFERER'],$data, $cookies);
parsecookies($header);
$content = unserialize($content);
$pageid = $adata['pageid'];
$edittoken = $content['query']['pages'][$pageid]['edittoken'];
$cookies = getcookies();
status("Save new Version of article ".$adata['title']);
$data2 = array(
'action' => 'edit',
'title' => $adata['title'],
'summary' => $adata['summary'],
'text' => $adata['text'],
'token' => $edittoken,
'watch',
'format' => 'php'
);
list($header, $content) = PostRequest('http://'.$wikilang.'.wikipedia.org/w/api.php',$_SERVER['HTTP_REFERER'],$data2, $cookies);
$content = unserialize($content);
//var_dump($content);
if(is_array($content['error']))
{
status("<font style=\"color:#FF0000;\">ERROR: Saving article ".$adata['title']." failed. ".$content['error']['info']."</font>");
global $status;
BotLog($status);
print "<br>$status<br>";
exit;
}
else if(isset($content['result']) && $content['result'] == "Success")
{
status("<font style=\"color:#00FF00;\">Saving article ".$adata['title']." successful</font>");
}
}
/**
* void BotLog(string text)
* Saves the status messages back to wikipedia
*/
function BotLog($text)
{
$oldcontent = getcontent("Benutzer:s2cchst/Log","de");
$oldcontent['text'] .= $text;
$oldcontent['summary'] = "Bot: Logdatei aktualisiert.";
savecontent($oldcontent,"de");
}
/**
* array pagelist(string typ, integer limit, string wikilang)
* Returns an array of Pages from wikipedia
*/
function pagelist($typ, $from, $limit = 500, $ns, $wikilang)
{
$data = array(
'action' => 'query',
'list' => $typ,
'apfrom' => $from,
'aplimit' => $limit,
'apnamespace' => $ns,
'format' => 'php'
);
$pagelist = array();
$cookies = getcookies();
list($header, $content) = PostRequest('http://'.$wikilang.'.wikipedia.org/w/api.php',$_SERVER['HTTP_REFERER'],$data, $cookies);
$content = unserialize($content);
$pages = $content['query']['allpages'];
foreach($pages as $page)
{
$pagelist[] = $page['title'];
}
return $pagelist;
}
/**
* void linklist(string bltitle, int limit, int ns, string filter, string wikilang)
* gives a list of links for a defined pagetitle
*/
function linklist($bltitle, $limit = 10, $ns, $filter, $wikilang)
{
$data = array(
'action' => 'query',
'list' => 'backlinks',
'bltitle' => $bltitle,
//'blnamespace' => $ns,
'blfilterredir' => $filter,
'format' => 'php',
);
$cookies = getcookies();
list($header, $content) = PostRequest("http://".$wikilang.".wikipedia.org/w/api.php",$_SERVER['HTTP_REFERER'], $data, $cookies);
$content = unserialize($content);
//var_dump($content);
}
/**
* array embedlist(string etitle, int limit, string wikilang)
* returns an array of pages where a defined page is embedded
*/
function embedlist($eititle, $limit = 10,$wikilang)
{
$return = array();
$data = array(
'action' => 'query',
'list' => 'embeddedin',
'eititle' => $eititle,
'eilimit' => $limit,
'format' => 'php'
);
$cookies = getcookies();
list($header, $content) = PostRequest("http://".$wikilang.".wikipedia.org/w/api.php",$_SERVER['HTTP_REFERER'], $data, $cookies);
$content = unserialize($content);
$liste = $content['query']['embeddedin'];
for($i=0; $i<count($liste); $i++)
{
$return[] = $liste[$i]['title'];
}
return $return;
}
function watch($title, $wikilang, $opt='')
{
$data = array(
'action' => 'watch',
'title' => $title,
'format' => 'php'
);
if(isset($opt) && $opt !="")
{
array_push($data, $opt);
status("Removing $title from watchlist");
}
else
{
status("Adding $title to watchlist");
}
$cookies = getcookies();
list($header, $content) = PostRequest("http://".$wikilang.".wikipedia.org/w/api.php",$_SERVER['HTTP_REFERER'], $data,$cookies);
var_dump(unserialize($content));
}
?>