Skip to content

Commit

Permalink
Extracted out MediaWiki_Api as a library.
Browse files Browse the repository at this point in the history
Some work still left to do. Also the library is currently bundled in while it
is supposed to be maintained separately
  • Loading branch information
nischayn22 committed Apr 4, 2014
1 parent 97ed66a commit c84a417
Show file tree
Hide file tree
Showing 6 changed files with 888 additions and 153 deletions.
5 changes: 5 additions & 0 deletions .gitignore
@@ -0,0 +1,5 @@
*~
*/
*tmp
*txt
*Copy*
674 changes: 674 additions & 0 deletions MediaWiki_Api/LICENSE

Large diffs are not rendered by default.

191 changes: 191 additions & 0 deletions MediaWiki_Api/MediaWiki_Api_functions.php
@@ -0,0 +1,191 @@
<?php

class MediaWikiApi{
private $editToken;
private $siteUrl;
private $logggedIn = false;

function MediaWikiApi($siteUrl){
$this->siteUrl = $siteUrl;
}

function login($user, $pass){

try {
global $settings;
$token = this->_login($user,$pass);
$token = this->_login($user,$pass, $token);
return true;
} catch (Exception $e) {
die("FAILED: " . $e->getMessage() . "\n");
}


}


private function _login ( $user, $pass, $token='') {

assert(!empty($this->siteUrl));
assert(!empty($user));
assert(!empty($pass));
$url = $this->siteUrl . "/api.php?action=login&format=xml";

$params = "action=login&lgname=$user&lgpassword=$pass";
if (!empty($token)) {
$params .= "&lgtoken=$token";
}

$data = httpRequest($url, $params);

if (empty($data)) {
throw new Exception("No data received from server. Check that API is enabled.");
}

$xml = simplexml_load_string($data);
if (!empty($token)) {
//Check for successful login
$expr = "/api/login[@result='Success']";
$result = $xml->xpath($expr);

if(!count($result)) {
throw new Exception("Login failed");
}
} else {
$expr = "/api/login[@token]";
$result = $xml->xpath($expr);

if(!count($result)) {
throw new Exception("Login token not found in XML");
}
}

return $result[0]->attributes()->token;
}


function setEditToken( ){
$url = $this->siteUrl . "/api.php?format=xml&action=query&titles=Main_Page&prop=info|revisions&intoken=edit";
$data = httpRequest($url, $params = '');
$xml = simplexml_load_string($data);
this->editToken = urlencode( (string)$xml->query->pages->page['edittoken'] );
return $this->editToken;
}

function listPageInNamespace($namespace){
$url = $this->siteUrl . "/api.php?action=query&list=allpages&format=xml&apnamespace=$namespace&aplimit=10000"; // Hope this limit is enough large that we don't have the trouble to do this again and again using 'continue'
$data = httpRequest($url, $params = '');
$xml = simplexml_load_string($data);
$expr = "/api/query/allpages/p";
$result = $xml->xpath($expr);
return $result;
}
function exportPage( $pageName, $content){
if (empty($this->editToken))
$this->setEditToken();

$editToken = $this->editToken;
$site = $this->siteUrl;
$content = urlencode( $content );
$url = $site . "/api.php?format=xml&action=edit&title=$pageName";
$data = httpRequest($url, $params = "format=xml&action=edit&title=$pageName&text=$content&token=$editToken");

$xml = simplexml_load_string($data);
errorHandler( $xml );

if ($data == null)
return null;
else
return 1;
}


function deleteById( $id ){
$deleteToken = $this->editToken;
$url = $this->siteUrl . "/api.php?action=delete&format=xml";
$params = "action=delete&pageid=$pageid&token=$deleteToken&reason=Outdated";
httpRequest($url, $params);
// Nothing to do with response currently
// $data = httpRequest($url, $params);
// $xml = simplexml_load_string($data);
}


}


function httpRequest($url, $post="", $retry = false, $retryNumber = 0, $headers) {
global $settings;

try {
$ch = curl_init();
//Change the user agent below suitably
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.9) Gecko/20071025 Firefox/2.0.0.9');
if( $settings['serverAuth'] ) {
curl_setopt($ch, CURLOPT_USERPWD, $settings['AuthUsername'] . ":" . $settings['AuthPassword']);
}
curl_setopt($ch, CURLOPT_URL, ($url));
curl_setopt( $ch, CURLOPT_ENCODING, "UTF-8" );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_COOKIEFILE, $settings['cookiefile']);
curl_setopt($ch, CURLOPT_COOKIEJAR, $settings['cookiefile']);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
if (!empty($post)) curl_setopt($ch,CURLOPT_POSTFIELDS,$post);
//UNCOMMENT TO DEBUG TO output.tmp
//curl_setopt($ch, CURLOPT_VERBOSE, true); // Display communication with server
//$fp = fopen("output.tmp", "w");
//curl_setopt($ch, CURLOPT_STDERR, $fp); // Display communication with server

$xml = curl_exec($ch);

if (!$xml) {
throw new Exception("Error getting data from server: " . curl_error($ch));
}

curl_close($ch);
} catch( Exception $e ) {
echo 'Caught exception: ', $e->getMessage(), "\n";
if( !$retry && $retryNumber <3 ) {
echo "Retrying \n";
httpRequest($url, $post, true, $retryNumber++ );
} else {
echo "Could not perform action after 3 attempts. Skipping now...\n";
return null;
}
}
return $xml;
}

function download($url, $file_target) {
global $settings;
$fp = fopen ( $file_target, 'w+');//This is the file where we save the information
$ch = curl_init(str_replace(" ","%20",$url));//Here is the file we are downloading, replace spaces with %20
if( $settings['serverAuth'] ) {
curl_setopt($ch, CURLOPT_USERPWD, $settings['AuthUsername'] . ":" . $settings['AuthPassword']);
}
curl_setopt($ch, CURLOPT_URL, ($url));
// curl_setopt( $ch, CURLOPT_ENCODING, "UTF-8" );
// curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_COOKIEFILE, $settings['cookiefile']);
curl_setopt($ch, CURLOPT_COOKIEJAR, $settings['cookiefile']);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_TIMEOUT, 50);
curl_setopt($ch, CURLOPT_FILE, $fp); // write curl response to file
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$xml = curl_exec($ch); // get curl response
curl_close($ch);
fclose($fp);
return $xml;
}


function errorHandler( $xml ){
if( property_exists( $xml, 'error' ) ) {
$errors = is_array( $xml->error )? $xml->error : array( $xml->error );
foreach( $errors as $error ) {
echo "Error code: " . $error['code'] . " " . $error['info'] . "\n";
}
}
}
4 changes: 4 additions & 0 deletions MediaWiki_Api/README.md
@@ -0,0 +1,4 @@
MediaWiki_Api
=============

A library around to use the MediaWiki Api (http://www.mediawiki.org/wiki/API)
126 changes: 2 additions & 124 deletions helperfunctions.php
Expand Up @@ -4,95 +4,9 @@
* @license GPL v2 or later
*/

function httpRequest($url, $post="", $retry = false, $retryNumber = 0) {
global $settings;

try {
$ch = curl_init();
//Change the user agent below suitably
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.9) Gecko/20071025 Firefox/2.0.0.9');
if( $settings['serverAuth'] ) {
curl_setopt($ch, CURLOPT_USERPWD, $settings['AuthUsername'] . ":" . $settings['AuthPassword']);
}
curl_setopt($ch, CURLOPT_URL, ($url));
curl_setopt( $ch, CURLOPT_ENCODING, "UTF-8" );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_COOKIEFILE, $settings['cookiefile']);
curl_setopt($ch, CURLOPT_COOKIEJAR, $settings['cookiefile']);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
if (!empty($post)) curl_setopt($ch,CURLOPT_POSTFIELDS,$post);
//UNCOMMENT TO DEBUG TO output.tmp
//curl_setopt($ch, CURLOPT_VERBOSE, true); // Display communication with server
//$fp = fopen("output.tmp", "w");
//curl_setopt($ch, CURLOPT_STDERR, $fp); // Display communication with server

$xml = curl_exec($ch);

if (!$xml) {
throw new Exception("Error getting data from server: " . curl_error($ch));
}

curl_close($ch);
} catch( Exception $e ) {
echo 'Caught exception: ', $e->getMessage(), "\n";
if( !$retry && $retryNumber <3 ) {
echo "Retrying \n";
httpRequest($url, $post, true, $retryNumber++ );
} else {
echo "Could not perform action after 3 attempts. Skipping now...\n";
return null;
}
}
return $xml;
}


function login ( $site, $user, $pass, $token='') {

$url = $site . "/api.php?action=login&format=xml";

$params = "action=login&lgname=$user&lgpassword=$pass";
if (!empty($token)) {
$params .= "&lgtoken=$token";
}

$data = httpRequest($url, $params);

if (empty($data)) {
throw new Exception("No data received from server. Check that API is enabled.");
}

$xml = simplexml_load_string($data);
if (!empty($token)) {
//Check for successful login
$expr = "/api/login[@result='Success']";
$result = $xml->xpath($expr);

if(!count($result)) {
throw new Exception("Login failed");
}
} else {
$expr = "/api/login[@token]";
$result = $xml->xpath($expr);

if(!count($result)) {
throw new Exception("Login token not found in XML");
}
}

return $result[0]->attributes()->token;
}

function deletepage( $pageid, $deleteToken ) {
global $settings;

$url = $settings['publicWiki'] . "/api.php?action=delete&format=xml";
$params = "action=delete&pageid=$pageid&token=$deleteToken&reason=Outdated";
httpRequest($url, $params);
// Nothing to do with response currently
// $data = httpRequest($url, $params);
// $xml = simplexml_load_string($data);
deleteId($settings['publicWiki'], $pageid, $deleteToken);
}

function copypage( $pageName, $editToken, $recursivelyCalled = true ) {
Expand Down Expand Up @@ -138,18 +52,13 @@ function copypage( $pageName, $editToken, $recursivelyCalled = true ) {
}

// now copy normal page
$content = urlencode( $content );
$url = $settings['publicWiki'] . "/api.php?format=xml&action=edit&title=$pageName";
$data = httpRequest($url, $params = "format=xml&action=edit&title=$pageName&text=$content&token=$editToken");
$data = exportPage($settings['publicWiki'], $pageName, $content, $editToken);
if ( $data == null ) {
// write to file that copy failed
echo "logging page name in failed_pages.txt\n";
file_put_contents( 'failed_pages.txt' , $pageName . "\n", FILE_APPEND );
}

$xml = simplexml_load_string($data);
errorHandler( $xml );

// Now import images linked on the page
echo "Finding file links in $pageName ...\n";
$url = $settings['privateWiki'] . "/api.php?format=xml&action=query&prop=images&titles=$pageName&imlimit=1000";
Expand Down Expand Up @@ -186,34 +95,3 @@ function copypage( $pageName, $editToken, $recursivelyCalled = true ) {
}
}

function errorHandler( $xml ){
if( property_exists( $xml, 'error' ) ) {
$errors = is_array( $xml->error )? $xml->error : array( $xml->error );
foreach( $errors as $error ) {
echo "Error code: " . $error['code'] . " " . $error['info'] . "\n";
}
}
}

function download($url, $file_target) {
global $settings;
$fp = fopen ( $file_target, 'w+');//This is the file where we save the information
$ch = curl_init(str_replace(" ","%20",$url));//Here is the file we are downloading, replace spaces with %20
if( $settings['serverAuth'] ) {
curl_setopt($ch, CURLOPT_USERPWD, $settings['AuthUsername'] . ":" . $settings['AuthPassword']);
}
curl_setopt($ch, CURLOPT_URL, ($url));
// curl_setopt( $ch, CURLOPT_ENCODING, "UTF-8" );
// curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_COOKIEFILE, $settings['cookiefile']);
curl_setopt($ch, CURLOPT_COOKIEJAR, $settings['cookiefile']);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_TIMEOUT, 50);
curl_setopt($ch, CURLOPT_FILE, $fp); // write curl response to file
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$xml = curl_exec($ch); // get curl response
curl_close($ch);
fclose($fp);
return $xml;
}

0 comments on commit c84a417

Please sign in to comment.