Skip to content

Commit

Permalink
Improve page read, add ability to do any action
Browse files Browse the repository at this point in the history
  • Loading branch information
tosfos committed May 20, 2015
1 parent 422e3e3 commit fad262b
Showing 1 changed file with 36 additions and 7 deletions.
43 changes: 36 additions & 7 deletions MediaWiki_Api_functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ function listPageInNamespace( $namespace ) {
}

function listPageInCategory( $category ) {
$category = urlencode( $category );
$url = $this->siteUrl .
"/api.php?format=xml&action=query&cmtitle=$category&list=categorymembers&cmlimit=10000";
$data = httpRequest( $url, $params = '' );
Expand All @@ -108,13 +109,14 @@ function getFileUrl( $pageName ) {
return (string) $imageInfo[0]['url'];
}

/**
*
* @param string $pageName
* @return string Wikitext
*/
function readPage( $pageName ) {
$url = $this->siteUrl .
"/api.php?format=xml&action=query&titles=$pageName&prop=revisions&rvprop=content";
$data = httpRequest( $url, $params = '' );
$xml = simplexml_load_string( $data );
errorHandler( $xml );
return (string) $xml->query->pages->page->revisions->rev;
return file_get_contents( $this->siteUrl . '/index.php?title=' .
urlencode( $pageName ) . '&action=raw' );
}

function createPage( $pageName, $content ) {
Expand Down Expand Up @@ -166,6 +168,33 @@ function deleteById( $id ) {
$xml = simplexml_load_string( $data );
errorHandler( $xml, $url . $params );
}

/**
* Do an action and post the parameter string
* @todo The other action functions should use this somehow.
*
* @param string $action
* @param string|array $paramString Parameters without a token
* @param string $format
* @return mixed
*/
function doAction( $action, $paramString, $format = 'xml' ) {
if ( is_array( $paramString ) ) {
$paramString = http_build_query( $paramString );
}

if ( empty( $this->editToken ) ) {
$this->setEditToken();
}
$actionToken = $this->editToken;
$url = $this->siteUrl . "/api.php?action=$action&format=$format";
$params = "action=$action&token=$actionToken&" . $paramString;
$data = httpRequest( $url, $params );
$xml = simplexml_load_string( $data );
errorHandler( $xml, $url . $params );

return $data;
}
}

function httpRequest( $url, $post = "", $retry = false, $retryNumber = 0, $headers = array() ) {
Expand All @@ -176,7 +205,7 @@ function httpRequest( $url, $post = "", $retry = false, $retryNumber = 0, $heade
//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'] ) {
if ( isset( $settings['serverAuth'] ) ) {
curl_setopt( $ch, CURLOPT_USERPWD, $settings['AuthUsername'] . ":" . $settings['AuthPassword'] );
}
curl_setopt( $ch, CURLOPT_URL, ($url ) );
Expand Down

0 comments on commit fad262b

Please sign in to comment.