summaryrefslogtreecommitdiff
path: root/includes/api/ApiPageSet.php
diff options
context:
space:
mode:
Diffstat (limited to 'includes/api/ApiPageSet.php')
-rw-r--r--includes/api/ApiPageSet.php115
1 files changed, 76 insertions, 39 deletions
diff --git a/includes/api/ApiPageSet.php b/includes/api/ApiPageSet.php
index dea87b88..185c0c59 100644
--- a/includes/api/ApiPageSet.php
+++ b/includes/api/ApiPageSet.php
@@ -5,7 +5,7 @@
*
* API for MediaWiki 1.8+
*
- * Copyright (C) 2006 Yuri Astrakhan <FirstnameLastname@gmail.com>
+ * Copyright (C) 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@@ -29,14 +29,25 @@ if (!defined('MEDIAWIKI')) {
}
/**
+ * This class contains a list of pages that the client has requested.
+ * Initially, when the client passes in titles=, pageids=, or revisions= parameter,
+ * an instance of the ApiPageSet class will normalize titles,
+ * determine if the pages/revisions exist, and prefetch any additional data page data requested.
+ *
+ * When generator is used, the result of the generator will become the input for the
+ * second instance of this class, and all subsequent actions will go use the second instance
+ * for all their work.
+ *
* @addtogroup API
*/
class ApiPageSet extends ApiQueryBase {
private $mAllPages; // [ns][dbkey] => page_id or 0 when missing
- private $mTitles, $mGoodTitles, $mMissingTitles, $mMissingPageIDs, $mRedirectTitles, $mNormalizedTitles;
+ private $mTitles, $mGoodTitles, $mMissingTitles, $mMissingPageIDs, $mRedirectTitles;
+ private $mNormalizedTitles, $mInterwikiTitles;
private $mResolveRedirects, $mPendingRedirectIDs;
private $mGoodRevIDs, $mMissingRevIDs;
+ private $mFakePageId;
private $mRequestedPageFields;
@@ -50,6 +61,7 @@ class ApiPageSet extends ApiQueryBase {
$this->mMissingPageIDs = array ();
$this->mRedirectTitles = array ();
$this->mNormalizedTitles = array ();
+ $this->mInterwikiTitles = array ();
$this->mGoodRevIDs = array();
$this->mMissingRevIDs = array();
@@ -57,6 +69,8 @@ class ApiPageSet extends ApiQueryBase {
$this->mResolveRedirects = $resolveRedirects;
if($resolveRedirects)
$this->mPendingRedirectIDs = array();
+
+ $this->mFakePageId = -1;
}
public function isResolvingRedirects() {
@@ -88,7 +102,16 @@ class ApiPageSet extends ApiQueryBase {
if ($this->mResolveRedirects)
$pageFlds['page_is_redirect'] = null;
- return array_keys(array_merge($pageFlds, $this->mRequestedPageFields));
+ $pageFlds = array_merge($pageFlds, $this->mRequestedPageFields);
+ return array_keys($pageFlds);
+ }
+
+ /**
+ * Returns an array [ns][dbkey] => page_id for all requested titles
+ * page_id is a unique negative number in case title was not found
+ */
+ public function getAllTitlesByNamespace() {
+ return $this->mAllPages;
}
/**
@@ -123,6 +146,7 @@ class ApiPageSet extends ApiQueryBase {
/**
* Title objects that were NOT found in the database.
+ * The array's index will be negative for each item
* @return array of Title objects
*/
public function getMissingTitles() {
@@ -155,6 +179,15 @@ class ApiPageSet extends ApiQueryBase {
}
/**
+ * Get a list of interwiki titles - maps the title given
+ * with to the interwiki prefix.
+ * @return array raw_prefixed_title (string) => interwiki_prefix (string)
+ */
+ public function getInterwikiTitles() {
+ return $this->mInterwikiTitles;
+ }
+
+ /**
* Get the list of revision IDs (requested with revids= parameter)
* @return array revID (int) => pageID (int)
*/
@@ -233,7 +266,6 @@ class ApiPageSet extends ApiQueryBase {
*/
public function populateFromPageIDs($pageIDs) {
$this->profileIn();
- $pageIDs = array_map('intval', $pageIDs); // paranoia
$this->initFromPageIds($pageIDs);
$this->profileOut();
}
@@ -265,22 +297,18 @@ class ApiPageSet extends ApiQueryBase {
// Store Title object in various data structures
$title = Title :: makeTitle($row->page_namespace, $row->page_title);
- // skip any pages that user has no rights to read
- if ($title->userCanRead()) {
-
- $pageId = intval($row->page_id);
- $this->mAllPages[$row->page_namespace][$row->page_title] = $pageId;
- $this->mTitles[] = $title;
-
- if ($this->mResolveRedirects && $row->page_is_redirect == '1') {
- $this->mPendingRedirectIDs[$pageId] = $title;
- } else {
- $this->mGoodTitles[$pageId] = $title;
- }
-
- foreach ($this->mRequestedPageFields as $fieldName => & $fieldValues)
- $fieldValues[$pageId] = $row-> $fieldName;
+ $pageId = intval($row->page_id);
+ $this->mAllPages[$row->page_namespace][$row->page_title] = $pageId;
+ $this->mTitles[] = $title;
+
+ if ($this->mResolveRedirects && $row->page_is_redirect == '1') {
+ $this->mPendingRedirectIDs[$pageId] = $title;
+ } else {
+ $this->mGoodTitles[$pageId] = $title;
}
+
+ foreach ($this->mRequestedPageFields as $fieldName => & $fieldValues)
+ $fieldValues[$pageId] = $row-> $fieldName;
}
public function finishPageSetGeneration() {
@@ -306,7 +334,7 @@ class ApiPageSet extends ApiQueryBase {
private function initFromTitles($titles) {
// Get validated and normalized title objects
- $linkBatch = $this->processTitlesStrArray($titles);
+ $linkBatch = $this->processTitlesArray($titles);
if($linkBatch->isEmpty())
return;
@@ -328,7 +356,8 @@ class ApiPageSet extends ApiQueryBase {
private function initFromPageIds($pageids) {
if(empty($pageids))
return;
-
+
+ $pageids = array_map('intval', $pageids); // paranoia
$set = array (
'page_id' => $pageids
);
@@ -386,8 +415,9 @@ class ApiPageSet extends ApiQueryBase {
foreach ($remaining as $ns => $dbkeys) {
foreach ( $dbkeys as $dbkey => $unused ) {
$title = Title :: makeTitle($ns, $dbkey);
- $this->mMissingTitles[] = $title;
- $this->mAllPages[$ns][$dbkey] = 0;
+ $this->mAllPages[$ns][$dbkey] = $this->mFakePageId;
+ $this->mMissingTitles[$this->mFakePageId] = $title;
+ $this->mFakePageId--;
$this->mTitles[] = $title;
}
}
@@ -536,39 +566,46 @@ class ApiPageSet extends ApiQueryBase {
/**
* Given an array of title strings, convert them into Title objects.
+ * Alternativelly, an array of Title objects may be given.
* This method validates access rights for the title,
* and appends normalization values to the output.
*
* @return LinkBatch of title objects.
*/
- private function processTitlesStrArray($titles) {
+ private function processTitlesArray($titles) {
$linkBatch = new LinkBatch();
- foreach ($titles as $titleString) {
- $titleObj = Title :: newFromText($titleString);
-
- // Validation
+ foreach ($titles as $title) {
+
+ $titleObj = is_string($title) ? Title :: newFromText($title) : $title;
if (!$titleObj)
- $this->dieUsage("bad title $titleString", 'invalidtitle');
- if ($titleObj->getNamespace() < 0)
- $this->dieUsage("No support for special page $titleString has been implemented", 'unsupportednamespace');
- if (!$titleObj->userCanRead())
- $this->dieUsage("No read permission for $titleString", 'titleaccessdenied');
+ $this->dieUsage("bad title", 'invalidtitle');
- $linkBatch->addObj($titleObj);
+ $iw = $titleObj->getInterwiki();
+ if (!empty($iw)) {
+ // This title is an interwiki link.
+ $this->mInterwikiTitles[$titleObj->getPrefixedText()] = $iw;
+ } else {
+ // Validation
+ if ($titleObj->getNamespace() < 0)
+ $this->dieUsage("No support for special pages has been implemented", 'unsupportednamespace');
+
+ $linkBatch->addObj($titleObj);
+ }
+
// Make sure we remember the original title that was given to us
// This way the caller can correlate new titles with the originally requested,
// i.e. namespace is localized or capitalization is different
- if ($titleString !== $titleObj->getPrefixedText()) {
- $this->mNormalizedTitles[$titleString] = $titleObj->getPrefixedText();
+ if (is_string($title) && $title !== $titleObj->getPrefixedText()) {
+ $this->mNormalizedTitles[$title] = $titleObj->getPrefixedText();
}
}
return $linkBatch;
}
-
+
protected function getAllowedParams() {
return array (
'titles' => array (
@@ -594,7 +631,7 @@ class ApiPageSet extends ApiQueryBase {
}
public function getVersion() {
- return __CLASS__ . ': $Id: ApiPageSet.php 21402 2007-04-20 08:55:14Z nickj $';
+ return __CLASS__ . ': $Id: ApiPageSet.php 24935 2007-08-20 08:13:16Z nickj $';
}
}
-?>
+