Skip to content

Commit

Permalink
Generalizes and streamlines the MediaWiki API client.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jim Safley committed Apr 15, 2011
1 parent 1781ac3 commit e874dad
Show file tree
Hide file tree
Showing 3 changed files with 365 additions and 378 deletions.
18 changes: 9 additions & 9 deletions lib/Scripto.php
Expand Up @@ -163,7 +163,7 @@ public function canProtect()
*/
public function setUserInfo()
{
$this->_userInfo = $this->_mediawiki->getUserInfo();
$this->_userInfo = $this->_mediawiki->getUserInfo('groups|rights');
}

/**
Expand Down Expand Up @@ -191,12 +191,12 @@ public function getUserDocumentPages($limit = 10)
$documentTitles = array();
$start = null;
do {
$userContribs = $this->_mediawiki->getUserContributions(
$response = $this->_mediawiki->getUserContributions(
$this->_userInfo['query']['userinfo']['name'],
array('ucstart' => $start,
'uclimit' => 100)
);
foreach ($userContribs['query']['usercontribs'] as $value) {
foreach ($response['query']['usercontribs'] as $value) {

// Filter out duplicate pages.
if (array_key_exists($value['pageid'], $userDocumentPages)) {
Expand Down Expand Up @@ -239,8 +239,8 @@ public function getUserDocumentPages($limit = 10)
}

// Set the query continue, if any.
if (isset($userContribs['query-continue'])) {
$start = $userContribs['query-continue']['usercontribs']['ucstart'];
if (isset($response['query-continue'])) {
$start = $response['query-continue']['usercontribs']['ucstart'];
} else {
$start = null;
}
Expand All @@ -265,13 +265,13 @@ public function getRecentChanges($limit = 10)
$documentTitles = array();
$start = null;
do {
$changes = $this->_mediawiki->getRecentChanges(
$response = $this->_mediawiki->getRecentChanges(
array('rctype' => 'edit|new',
'rcprop' => 'user|timestamp|title|ids',
'rcnamespace' => '0',
'rcstart' => $start)
);
foreach ($changes['query']['recentchanges'] as $value) {
foreach ($response['query']['recentchanges'] as $value) {

// Filter out changes that are not document pages.
if (Scripto_Document::BASE_TITLE_PREFIX != $value['title'][0]) {
Expand Down Expand Up @@ -310,8 +310,8 @@ public function getRecentChanges($limit = 10)
}

// Set the query continue, if any.
if (isset($changes['query-continue'])) {
$start = $changes['query-continue']['recentchanges']['rcstart'];
if (isset($response['query-continue'])) {
$start = $response['query-continue']['recentchanges']['rcstart'];
} else {
$start = null;
}
Expand Down
101 changes: 22 additions & 79 deletions lib/Scripto/Document.php
Expand Up @@ -189,57 +189,6 @@ public function getPageImageUrl()
return $this->_adapter->getDocumentPageImageUrl($this->_id, $this->_pageId);
}

/**
* Get the MediaWiki page wikitext for the specified title.
*
* @link http://www.mediawiki.org/wiki/API:Query#Exporting_pages
* @param string $title The title of the page.
* @return string The wikitext of the page.
*/
protected function _getPageWikitext($title)
{
$revision = $this->_mediawiki->getRevisions(
$title,
array('rvprop' => 'content',
'rvlimit' => '1')
);
$page = current($revision['query']['pages']);

// Return the wikitext only if the page already exists.
$pageWikitext = null;
if (isset($page['revisions'][0]['*'])) {
$pageWikitext = $page['revisions'][0]['*'];
}
return $pageWikitext;
}

/**
* Get the MediaWiki page HTML for the specified title.
*
* @link http://www.mediawiki.org/wiki/API:Parsing_wikitext#parse
* @link http://lists.wikimedia.org/pipermail/mediawiki-api/2010-April/001694.html
* @param string $title
* @return string|null
*/
protected function _getPageHtml($title)
{
$parse = $this->_mediawiki->getParse(
// To exclude [edit] links in the parsed wikitext, we must use the
// following hack.
array('text' => '__NOEDITSECTION__{{:' . $title . '}}')
);

// Return the text only if the page already exists. Otherwise, the
// returned HTML is a link to the document's MediaWiki edit page. The
// only indicator I found in the response XML is the "exists" attribute
// in the templates node; but this may not be adequate.
$pageHtml = null;
if (isset($parse['parse']['templates'][0]['exists'])) {
$pageHtml = $parse['parse']['text']['*'];
}
return $pageHtml;
}

/**
* Get the MediaWiki transcription page wikitext for the current page.
*
Expand All @@ -250,7 +199,7 @@ public function getTranscriptionPageWikitext()
if (is_null($this->_pageId)) {
throw new Scripto_Exception('The document page must be set before getting the transcription page wikitext.');
}
return $this->_getPageWikitext($this->_baseTitle);
return $this->_mediawiki->getLatestRevisionWikitext($this->_baseTitle);
}

/**
Expand All @@ -263,7 +212,7 @@ public function getTalkPageWikitext()
if (is_null($this->_pageId)) {
throw new Scripto_Exception('The document page must be set before getting the talk page wikitext.');
}
return $this->_getPageWikitext('Talk:' . $this->_baseTitle);
return $this->_mediawiki->getLatestRevisionWikitext('Talk:' . $this->_baseTitle);
}

/**
Expand All @@ -276,7 +225,7 @@ public function getTranscriptionPageHtml()
if (is_null($this->_pageId)) {
throw new Scripto_Exception('The document page must be set before getting the transcription page HTML.');
}
return $this->_getPageHtml($this->_baseTitle);
return $this->_mediawiki->getLatestRevisionHtml($this->_baseTitle);
}

/**
Expand All @@ -289,7 +238,7 @@ public function getTalkPageHtml()
if (is_null($this->_pageId)) {
throw new Scripto_Exception('The document page must be set before getting the talk page HTML.');
}
return $this->_getPageHtml('Talk:' . $this->_baseTitle);
return $this->_mediawiki->getLatestRevisionHtml('Talk:' . $this->_baseTitle);
}

/**
Expand All @@ -302,7 +251,7 @@ public function getTranscriptionPagePlainText()
if (is_null($this->_pageId)) {
throw new Scripto_Exception('The document page must be set before getting the transcription page plain text.');
}
return html_entity_decode(strip_tags($this->_getPageHtml($this->_baseTitle)));
return html_entity_decode(strip_tags($this->_mediawiki->getLatestRevisionHtml($this->_baseTitle)));
}

/**
Expand All @@ -315,21 +264,7 @@ public function getTalkPagePlainText()
if (is_null($this->_pageId)) {
throw new Scripto_Exception('The document page must be set before getting the talk page plain text.');
}
return html_entity_decode(strip_tags($this->_getPageHtml('Talk:' . $this->_baseTitle)));
}

/**
* Get an HTML preview of the provided wikitext.
*
* @param string $wikitext The wikitext.
* @return string The wikitext parsed as HTML.
*/
public function getPreview($wikitext)
{
$parse = $this->_mediawiki->getParse(
array('text' => '__NOEDITSECTION__' . $wikitext)
);
return $parse['parse']['text']['*'];
return html_entity_decode(strip_tags($this->_mediawiki->getLatestRevisionHtml('Talk:' . $this->_baseTitle)));
}

/**
Expand All @@ -349,7 +284,7 @@ public function canEdit()
throw new Scripto_Exception('The document page must be set before determining whether the user can edit it.');
}

$userInfo = $this->_mediawiki->getUserInfo();
$userInfo = $this->_mediawiki->getUserInfo('rights');

// Users without edit rights cannot edit pages.
if (!in_array('edit', $userInfo['query']['userinfo']['rights'])) {
Expand Down Expand Up @@ -395,7 +330,7 @@ public function editTranscriptionPage($text)
if (is_null($this->_pageId)) {
throw new Scripto_Exception('The document page must be set before editing the transcription page.');
}
$this->_mediawiki->editPage($this->_baseTitle, $text);
$this->_mediawiki->edit($this->_baseTitle, $text);
}

/**
Expand All @@ -408,23 +343,31 @@ public function editTalkPage($text)
if (is_null($this->_pageId)) {
throw new Scripto_Exception('The document page must be set before editing the talk page.');
}
$this->_mediawiki->editPage('Talk:' . $this->_baseTitle, $text);
$this->_mediawiki->edit('Talk:' . $this->_baseTitle, $text);
}

/**
* Protect the current page.
*/
public function protectPage()
{
$this->_mediawiki->protectPage($this->_baseTitle, $protectToken);
if ($this->_mediawiki->pageCreated($this->_baseTitle)) {
$this->_mediawiki->protect($this->_baseTitle, 'edit=sysop');
} else {
$this->_mediawiki->protect($this->_baseTitle, 'create=sysop');
}
}

/**
* Unprotect the current page.
*/
public function unprotectPage()
{
$this->_mediawiki->unprotectPage($this->_baseTitle, $protectToken);
if ($this->_mediawiki->pageCreated($this->_baseTitle)) {
$this->_mediawiki->protect($this->_baseTitle, 'edit=all');
} else {
$this->_mediawiki->protect($this->_baseTitle, 'create=all');
}
}

/**
Expand Down Expand Up @@ -592,13 +535,13 @@ public function exportTranscription($type = 'plain_text',
$baseTitle = self::encodeBaseTitle($this->_id, $pageId);
switch ($type) {
case 'plain_text':
$text[] = html_entity_decode(strip_tags($this->_getPageHtml($baseTitle)));
$text[] = html_entity_decode(strip_tags($this->_mediawiki->getLatestRevisionHtml($baseTitle)));
break;
case 'html':
$text[] = $this->_getPageHtml($baseTitle);
$text[] = $this->_mediawiki->getLatestRevisionHtml($baseTitle);
break;
case 'wikitext':
$text[] = $this->_getPageWikitext($baseTitle);
$text[] = $this->_mediawiki->getLatestRevisionWikitext($baseTitle);
break;
default:
throw new Scripto_Exception('The provided import type is invalid.');
Expand Down

0 comments on commit e874dad

Please sign in to comment.