5 * Created on Sep 24, 2006
7 * API for MediaWiki 1.8+
9 * Copyright (C) 2006 Yuri Astrakhan <FirstnameLastname@gmail.com>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 * http://www.gnu.org/copyleft/gpl.html
27 if (!defined('MEDIAWIKI')) {
28 // Eclipse helper - will be ignored in production
29 require_once ('ApiQueryBase.php');
32 class ApiPageSet
extends ApiQueryBase
{
34 private $mAllPages; // [ns][dbkey] => page_id or 0 when missing
35 private $mGoodTitles, $mMissingTitles, $mRedirectTitles, $mNormalizedTitles;
37 private $mRequestedFields;
39 public function __construct($query) {
40 parent
:: __construct($query, __CLASS__
);
42 $this->mAllPages
= array ();
43 $this->mGoodTitles
= array ();
44 $this->mMissingTitles
= array ();
45 $this->mRedirectTitles
= array ();
46 $this->mNormalizedTitles
= array ();
48 $this->mRequestedFields
= array ();
51 public function requestField($fieldName) {
52 $this->mRequestedFields
[$fieldName] = null;
56 * Title objects that were found in the database.
57 * @return array page_id (int) => Title (obj)
59 public function getGoodTitles() {
60 return $this->mGoodTitles
;
64 * Title objects that were NOT found in the database.
65 * @return array of Title objects
67 public function getMissingTitles() {
68 return $this->mMissingTitles
;
72 * Get a list of redirects when doing redirect resolution
73 * @return array prefixed_title (string) => prefixed_title (string)
75 public function getRedirectTitles() {
76 return $this->mRedirectTitles
;
80 * Get a list of title normalizations - maps the title given
81 * with its normalized version.
82 * @return array raw_prefixed_title (string) => prefixed_title (string)
84 public function getNormalizedTitles() {
85 return $this->mNormalizedTitles
;
89 * Returns the number of unique pages (not revisions) in the set.
91 public function getGoodTitleCount() {
92 return count($this->getGoodTitles());
96 * Get the list of revision IDs (requested with revids= parameter)
98 public function getRevisionIDs() {
99 $this->dieUsage(__METHOD__
. ' is not implemented', 'notimplemented');
103 * Returns the number of revisions (requested with revids= parameter)
105 public function getRevisionCount() {
106 return 0; // TODO: implement
110 * This method populates internal variables with page information
111 * based on the given array of title strings.
114 * #1 For each title, get data from `page` table
115 * #2 If page was not found in the DB, store it as missing
117 * Additionally, when resolving redirects:
118 * #3 If no more redirects left, stop.
119 * #4 For each redirect, get its links from `pagelinks` table.
120 * #5 Substitute the original LinkBatch object with the new list
121 * #6 Repeat from step #1
123 private function populateTitles($titles, $redirects) {
125 // Ensure we get minimum required fields
128 'page_namespace' => null,
132 // only store non-default fields
133 $this->mRequestedFields
= array_diff_key($this->mRequestedFields
, $pageFlds);
136 $pageFlds['page_is_redirect'] = null;
138 $pageFlds = array_keys(array_merge($this->mRequestedFields
, $pageFlds));
140 // Get validated and normalized title objects
141 $linkBatch = $this->processTitlesStrArray($titles);
143 $db = $this->getDB();
146 // Repeat until all redirects have been resolved
148 while (false !== ($set = $linkBatch->constructSet('page', $db))) {
150 // Hack: get the ns:titles stored in array(ns => array(titles)) format
151 $remaining = $linkBatch->data
;
153 $redirectIds = array ();
156 // Get data about $linkBatch from `page` table
158 $this->profileDBIn();
159 $res = $db->select('page', $pageFlds, $set, __METHOD__
);
160 $this->profileDBOut();
161 while ($row = $db->fetchObject($res)) {
163 unset ($remaining[$row->page_namespace
][$row->page_title
]);
164 $title = Title
:: makeTitle($row->page_namespace
, $row->page_title
);
165 $this->mAllPages
[$row->page_namespace
][$row->page_title
] = $row->page_id
;
167 if ($redirects && $row->page_is_redirect
== '1') {
168 $redirectIds[$row->page_id
] = $title;
170 $this->mGoodTitles
[$row->page_id
] = $title;
173 $db->freeResult($res);
176 // The remaining titles in $remaining are non-existant pages
178 foreach ($remaining as $ns => $dbkeys) {
179 foreach ($dbkeys as $dbkey => $nothing) {
180 $this->mMissingTitles
[] = Title
:: makeTitle($ns, $dbkey);
181 $this->mAllPages
[$ns][$dbkey] = 0;
185 if (!$redirects ||
empty ($redirectIds))
189 // Resolve redirects by querying the pagelinks table, and repeat the process
192 // Create a new linkBatch object for the next pass
193 $linkBatch = new LinkBatch();
195 // find redirect targets for all redirect pages
196 $this->profileDBIn();
197 $res = $db->select('pagelinks', array (
202 'pl_from' => array_keys($redirectIds
204 $this->profileDBOut();
206 while ($row = $db->fetchObject($res)) {
208 // Bug 7304 workaround
209 // ( http://bugzilla.wikipedia.org/show_bug.cgi?id=7304 )
210 // A redirect page may have more than one link.
211 // This code will only use the first link returned.
212 if (isset ($redirectIds[$row->pl_from
])) { // remove line when 7304 is fixed
214 $titleStrFrom = $redirectIds[$row->pl_from
]->getPrefixedText();
215 $titleStrTo = Title
:: makeTitle($row->pl_namespace
, $row->pl_title
)->getPrefixedText();
216 $this->mRedirectTitles
[$titleStrFrom] = $titleStrTo;
218 unset ($redirectIds[$row->pl_from
]); // remove line when 7304 is fixed
220 // Avoid an infinite loop by checking if we have already processed this target
221 if (!isset ($this->mAllPages
[$row->pl_namespace
][$row->pl_title
])) {
222 $linkBatch->add($row->pl_namespace
, $row->pl_title
);
226 $db->freeResult($res);
231 * Given an array of title strings, convert them into Title objects.
232 * This method validates access rights for the title,
233 * and appends normalization values to the output.
235 * @return LinkBatch of title objects.
237 private function processTitlesStrArray($titles) {
239 $linkBatch = new LinkBatch();
241 foreach ($titles as $titleString) {
242 $titleObj = Title
:: newFromText($titleString);
246 $this->dieUsage("bad title $titleString", 'invalidtitle');
247 if ($titleObj->getNamespace() < 0)
248 $this->dieUsage("No support for special page $titleString has been implemented", 'unsupportednamespace');
249 if (!$titleObj->userCanRead())
250 $this->dieUsage("No read permission for $titleString", 'titleaccessdenied');
252 $linkBatch->addObj($titleObj);
254 // Make sure we remember the original title that was given to us
255 // This way the caller can correlate new titles with the originally requested,
256 // i.e. namespace is localized or capitalization is different
257 if ($titleString !== $titleObj->getPrefixedText()) {
258 $this->mNormalizedTitles
[$titleString] = $titleObj->getPrefixedText();
265 private function populatePageIDs($pageids) {
266 $this->dieUsage(__METHOD__
. ' is not implemented', 'notimplemented');
269 public function execute() {
270 $titles = $pageids = $revids = $redirects = null;
271 extract($this->extractRequestParams());
273 // Only one of the titles/pageids/revids is allowed at the same time
276 $dataSource = 'titles';
277 if (isset ($pageids)) {
278 if (isset ($dataSource))
279 $this->dieUsage("Cannot use 'pageids' at the same time as '$dataSource'", 'multisource');
280 $dataSource = 'pageids';
282 if (isset ($revids)) {
283 if (isset ($dataSource))
284 $this->dieUsage("Cannot use 'revids' at the same time as '$dataSource'", 'multisource');
285 $dataSource = 'revids';
288 switch ($dataSource) {
290 $this->populateTitles($titles, $redirects);
293 $this->populatePageIDs($pageids, $redirects);
296 $this->populateRevIDs($revids);
299 // Do nothing - some queries do not need any of the data sources.
304 protected function getAllowedParams() {
307 ApiBase
::PARAM_ISMULTI
=> true
310 ApiBase
::PARAM_TYPE
=> 'integer',
311 ApiBase
::PARAM_ISMULTI
=> true
314 ApiBase
::PARAM_TYPE
=> 'integer',
315 ApiBase
::PARAM_ISMULTI
=> true
321 protected function getParamDescription() {
323 'titles' => 'A list of titles to work on',
324 'pageids' => 'A list of page IDs to work on',
325 'revids' => 'A list of revision IDs to work on',
326 'redirects' => 'Automatically resolve redirects'