554ae7683975d600af2ccde36c812bf06bed4903
4 * Created on Sep 24, 2006
6 * API for MediaWiki 1.8+
8 * Copyright © 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 * http://www.gnu.org/copyleft/gpl.html
26 if ( !defined( 'MEDIAWIKI' ) ) {
27 // Eclipse helper - will be ignored in production
28 require_once( 'ApiQueryBase.php' );
32 * This class contains a list of pages that the client has requested.
33 * Initially, when the client passes in titles=, pageids=, or revisions=
34 * parameter, an instance of the ApiPageSet class will normalize titles,
35 * determine if the pages/revisions exist, and prefetch any additional page
38 * When a generator is used, the result of the generator will become the input
39 * for the second instance of this class, and all subsequent actions will use
40 * the second instance for all their work.
44 class ApiPageSet
extends ApiQueryBase
{
46 private $mAllPages; // [ns][dbkey] => page_id or negative when missing
47 private $mTitles, $mGoodTitles, $mMissingTitles, $mInvalidTitles;
48 private $mMissingPageIDs, $mRedirectTitles;
49 private $mNormalizedTitles, $mInterwikiTitles;
50 private $mResolveRedirects, $mPendingRedirectIDs;
51 private $mGoodRevIDs, $mMissingRevIDs;
54 private $mRequestedPageFields;
58 * @param $query ApiQuery
59 * @param $resolveRedirects bool Whether redirects should be resolved
61 public function __construct( $query, $resolveRedirects = false ) {
62 parent
::__construct( $query, 'query' );
64 $this->mAllPages
= array();
65 $this->mTitles
= array();
66 $this->mGoodTitles
= array();
67 $this->mMissingTitles
= array();
68 $this->mInvalidTitles
= array();
69 $this->mMissingPageIDs
= array();
70 $this->mRedirectTitles
= array();
71 $this->mNormalizedTitles
= array();
72 $this->mInterwikiTitles
= array();
73 $this->mGoodRevIDs
= array();
74 $this->mMissingRevIDs
= array();
76 $this->mRequestedPageFields
= array();
77 $this->mResolveRedirects
= $resolveRedirects;
78 if ( $resolveRedirects ) {
79 $this->mPendingRedirectIDs
= array();
82 $this->mFakePageId
= - 1;
86 * Check whether this PageSet is resolving redirects
89 public function isResolvingRedirects() {
90 return $this->mResolveRedirects
;
94 * Request an additional field from the page table. Must be called
96 * @param $fieldName string Field name
98 public function requestField( $fieldName ) {
99 $this->mRequestedPageFields
[$fieldName] = null;
103 * Get the value of a custom field previously requested through
105 * @param $fieldName string Field name
106 * @return mixed Field value
108 public function getCustomField( $fieldName ) {
109 return $this->mRequestedPageFields
[$fieldName];
113 * Get the fields that have to be queried from the page table:
114 * the ones requested through requestField() and a few basic ones
116 * @return array of field names
118 public function getPageTableFields() {
119 // Ensure we get minimum required fields
120 // DON'T change this order
122 'page_namespace' => null,
123 'page_title' => null,
127 if ( $this->mResolveRedirects
) {
128 $pageFlds['page_is_redirect'] = null;
131 // only store non-default fields
132 $this->mRequestedPageFields
= array_diff_key( $this->mRequestedPageFields
, $pageFlds );
134 $pageFlds = array_merge( $pageFlds, $this->mRequestedPageFields
);
135 return array_keys( $pageFlds );
139 * Returns an array [ns][dbkey] => page_id for all requested titles.
140 * page_id is a unique negative number in case title was not found.
141 * Invalid titles will also have negative page IDs and will be in namespace 0
144 public function getAllTitlesByNamespace() {
145 return $this->mAllPages
;
149 * All Title objects provided.
150 * @return array of Title objects
152 public function getTitles() {
153 return $this->mTitles
;
157 * Returns the number of unique pages (not revisions) in the set.
160 public function getTitleCount() {
161 return count( $this->mTitles
);
165 * Title objects that were found in the database.
166 * @return array page_id (int) => Title (obj)
168 public function getGoodTitles() {
169 return $this->mGoodTitles
;
173 * Returns the number of found unique pages (not revisions) in the set.
176 public function getGoodTitleCount() {
177 return count( $this->mGoodTitles
);
181 * Title objects that were NOT found in the database.
182 * The array's index will be negative for each item
183 * @return array of Title objects
185 public function getMissingTitles() {
186 return $this->mMissingTitles
;
190 * Titles that were deemed invalid by Title::newFromText()
191 * The array's index will be unique and negative for each item
192 * @return array of strings (not Title objects)
194 public function getInvalidTitles() {
195 return $this->mInvalidTitles
;
199 * Page IDs that were not found in the database
200 * @return array of page IDs
202 public function getMissingPageIDs() {
203 return $this->mMissingPageIDs
;
207 * Get a list of redirect resolutions - maps a title to its redirect
209 * @return array prefixed_title (string) => prefixed_title (string)
211 public function getRedirectTitles() {
212 return $this->mRedirectTitles
;
216 * Get a list of title normalizations - maps a title to its normalized
218 * @return array raw_prefixed_title (string) => prefixed_title (string)
220 public function getNormalizedTitles() {
221 return $this->mNormalizedTitles
;
225 * Get a list of interwiki titles - maps a title to its interwiki
227 * @return array raw_prefixed_title (string) => interwiki_prefix (string)
229 public function getInterwikiTitles() {
230 return $this->mInterwikiTitles
;
234 * Get the list of revision IDs (requested with the revids= parameter)
235 * @return array revID (int) => pageID (int)
237 public function getRevisionIDs() {
238 return $this->mGoodRevIDs
;
242 * Revision IDs that were not found in the database
243 * @return array of revision IDs
245 public function getMissingRevisionIDs() {
246 return $this->mMissingRevIDs
;
250 * Returns the number of revisions (requested with revids= parameter)\
253 public function getRevisionCount() {
254 return count( $this->getRevisionIDs() );
258 * Populate the PageSet from the request parameters.
260 public function execute() {
262 $params = $this->extractRequestParams();
264 // Only one of the titles/pageids/revids is allowed at the same time
266 if ( isset( $params['titles'] ) ) {
267 $dataSource = 'titles';
269 if ( isset( $params['pageids'] ) ) {
270 if ( isset( $dataSource ) ) {
271 $this->dieUsage( "Cannot use 'pageids' at the same time as '$dataSource'", 'multisource' );
273 $dataSource = 'pageids';
275 if ( isset( $params['revids'] ) ) {
276 if ( isset( $dataSource ) ) {
277 $this->dieUsage( "Cannot use 'revids' at the same time as '$dataSource'", 'multisource' );
279 $dataSource = 'revids';
282 switch ( $dataSource ) {
284 $this->initFromTitles( $params['titles'] );
287 $this->initFromPageIds( $params['pageids'] );
290 if ( $this->mResolveRedirects
) {
291 $this->setWarning( 'Redirect resolution cannot be used together with the revids= parameter. ' .
292 'Any redirects the revids= point to have not been resolved.' );
294 $this->mResolveRedirects
= false;
295 $this->initFromRevIDs( $params['revids'] );
298 // Do nothing - some queries do not need any of the data sources.
305 * Populate this PageSet from a list of Titles
306 * @param $titles array of Title objects
308 public function populateFromTitles( $titles ) {
310 $this->initFromTitles( $titles );
315 * Populate this PageSet from a list of page IDs
316 * @param $pageIDs array of page IDs
318 public function populateFromPageIDs( $pageIDs ) {
320 $this->initFromPageIds( $pageIDs );
325 * Populate this PageSet from a rowset returned from the database
326 * @param $db Database object
327 * @param $queryResult Query result object
329 public function populateFromQueryResult( $db, $queryResult ) {
331 $this->initFromQueryResult( $db, $queryResult );
336 * Populate this PageSet from a list of revision IDs
337 * @param $revIDs array of revision IDs
339 public function populateFromRevisionIDs( $revIDs ) {
341 $this->initFromRevIDs( $revIDs );
346 * Extract all requested fields from the row received from the database
347 * @param $row Result row
349 public function processDbRow( $row ) {
350 // Store Title object in various data structures
351 $title = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
353 $pageId = intval( $row->page_id
);
354 $this->mAllPages
[$row->page_namespace
][$row->page_title
] = $pageId;
355 $this->mTitles
[] = $title;
357 if ( $this->mResolveRedirects
&& $row->page_is_redirect
== '1' ) {
358 $this->mPendingRedirectIDs
[$pageId] = $title;
360 $this->mGoodTitles
[$pageId] = $title;
363 foreach ( $this->mRequestedPageFields
as $fieldName => &$fieldValues ) {
364 $fieldValues[$pageId] = $row-> $fieldName;
369 * Resolve redirects, if applicable
371 public function finishPageSetGeneration() {
373 $this->resolvePendingRedirects();
378 * This method populates internal variables with page information
379 * based on the given array of title strings.
382 * #1 For each title, get data from `page` table
383 * #2 If page was not found in the DB, store it as missing
385 * Additionally, when resolving redirects:
386 * #3 If no more redirects left, stop.
387 * #4 For each redirect, get its target from the `redirect` table.
388 * #5 Substitute the original LinkBatch object with the new list
389 * #6 Repeat from step #1
391 * @param $titles array of Title objects or strings
393 private function initFromTitles( $titles ) {
394 // Get validated and normalized title objects
395 $linkBatch = $this->processTitlesArray( $titles );
396 if ( $linkBatch->isEmpty() ) {
400 $db = $this->getDB();
401 $set = $linkBatch->constructSet( 'page', $db );
403 // Get pageIDs data from the `page` table
404 $this->profileDBIn();
405 $res = $db->select( 'page', $this->getPageTableFields(), $set,
407 $this->profileDBOut();
409 // Hack: get the ns:titles stored in array(ns => array(titles)) format
410 $this->initFromQueryResult( $db, $res, $linkBatch->data
, true ); // process Titles
412 // Resolve any found redirects
413 $this->resolvePendingRedirects();
417 * Does the same as initFromTitles(), but is based on page IDs instead
418 * @param $pageids array of page IDs
420 private function initFromPageIds( $pageids ) {
421 if ( !count( $pageids ) ) {
425 $pageids = array_map( 'intval', $pageids ); // paranoia
427 'page_id' => $pageids
429 $db = $this->getDB();
431 // Get pageIDs data from the `page` table
432 $this->profileDBIn();
433 $res = $db->select( 'page', $this->getPageTableFields(), $set,
435 $this->profileDBOut();
437 $remaining = array_flip( $pageids );
438 $this->initFromQueryResult( $db, $res, $remaining, false ); // process PageIDs
440 // Resolve any found redirects
441 $this->resolvePendingRedirects();
445 * Iterate through the result of the query on 'page' table,
446 * and for each row create and store title object and save any extra fields requested.
447 * @param $db Database
448 * @param $res DB Query result
449 * @param $remaining array of either pageID or ns/title elements (optional).
450 * If given, any missing items will go to $mMissingPageIDs and $mMissingTitles
451 * @param $processTitles bool Must be provided together with $remaining.
452 * If true, treat $remaining as an array of [ns][title]
453 * If false, treat it as an array of [pageIDs]
455 private function initFromQueryResult( $db, $res, &$remaining = null, $processTitles = null ) {
456 if ( !is_null( $remaining ) && is_null( $processTitles ) ) {
457 ApiBase
::dieDebug( __METHOD__
, 'Missing $processTitles parameter when $remaining is provided' );
460 while ( $row = $db->fetchObject( $res ) ) {
461 $pageId = intval( $row->page_id
);
463 // Remove found page from the list of remaining items
464 if ( isset( $remaining ) ) {
465 if ( $processTitles ) {
466 unset( $remaining[$row->page_namespace
][$row->page_title
] );
468 unset( $remaining[$pageId] );
472 // Store any extra fields requested by modules
473 $this->processDbRow( $row );
476 if ( isset( $remaining ) ) {
477 // Any items left in the $remaining list are added as missing
478 if ( $processTitles ) {
479 // The remaining titles in $remaining are non-existent pages
480 foreach ( $remaining as $ns => $dbkeys ) {
481 foreach ( $dbkeys as $dbkey => $unused ) {
482 $title = Title
::makeTitle( $ns, $dbkey );
483 $this->mAllPages
[$ns][$dbkey] = $this->mFakePageId
;
484 $this->mMissingTitles
[$this->mFakePageId
] = $title;
485 $this->mFakePageId
--;
486 $this->mTitles
[] = $title;
490 // The remaining pageids do not exist
491 if ( !$this->mMissingPageIDs
) {
492 $this->mMissingPageIDs
= array_keys( $remaining );
494 $this->mMissingPageIDs
= array_merge( $this->mMissingPageIDs
, array_keys( $remaining ) );
501 * Does the same as initFromTitles(), but is based on revision IDs
503 * @param $revids array of revision IDs
505 private function initFromRevIDs( $revids ) {
506 if ( !count( $revids ) ) {
510 $revids = array_map( 'intval', $revids ); // paranoia
511 $db = $this->getDB();
513 $remaining = array_flip( $revids );
515 $tables = array( 'revision', 'page' );
516 $fields = array( 'rev_id', 'rev_page' );
517 $where = array( 'rev_id' => $revids, 'rev_page = page_id' );
519 // Get pageIDs data from the `page` table
520 $this->profileDBIn();
521 $res = $db->select( $tables, $fields, $where, __METHOD__
);
522 while ( $row = $db->fetchObject( $res ) ) {
523 $revid = intval( $row->rev_id
);
524 $pageid = intval( $row->rev_page
);
525 $this->mGoodRevIDs
[$revid] = $pageid;
526 $pageids[$pageid] = '';
527 unset( $remaining[$revid] );
529 $this->profileDBOut();
531 $this->mMissingRevIDs
= array_keys( $remaining );
533 // Populate all the page information
534 $this->initFromPageIds( array_keys( $pageids ) );
538 * Resolve any redirects in the result if redirect resolution was
539 * requested. This function is called repeatedly until all redirects
540 * have been resolved.
542 private function resolvePendingRedirects() {
543 if ( $this->mResolveRedirects
) {
544 $db = $this->getDB();
545 $pageFlds = $this->getPageTableFields();
547 // Repeat until all redirects have been resolved
548 // The infinite loop is prevented by keeping all known pages in $this->mAllPages
549 while ( $this->mPendingRedirectIDs
) {
550 // Resolve redirects by querying the pagelinks table, and repeat the process
551 // Create a new linkBatch object for the next pass
552 $linkBatch = $this->getRedirectTargets();
554 if ( $linkBatch->isEmpty() ) {
558 $set = $linkBatch->constructSet( 'page', $db );
559 if ( $set === false ) {
563 // Get pageIDs data from the `page` table
564 $this->profileDBIn();
565 $res = $db->select( 'page', $pageFlds, $set, __METHOD__
);
566 $this->profileDBOut();
568 // Hack: get the ns:titles stored in array(ns => array(titles)) format
569 $this->initFromQueryResult( $db, $res, $linkBatch->data
, true );
575 * Get the targets of the pending redirects from the database
577 * Also creates entries in the redirect table for redirects that don't
581 private function getRedirectTargets() {
582 $lb = new LinkBatch();
583 $db = $this->getDB();
585 $this->profileDBIn();
592 ), array( 'rd_from' => array_keys( $this->mPendingRedirectIDs
) ),
595 $this->profileDBOut();
597 while ( $row = $db->fetchObject( $res ) ) {
598 $rdfrom = intval( $row->rd_from
);
599 $from = $this->mPendingRedirectIDs
[$rdfrom]->getPrefixedText();
600 $to = Title
::makeTitle( $row->rd_namespace
, $row->rd_title
)->getPrefixedText();
601 unset( $this->mPendingRedirectIDs
[$rdfrom] );
602 if ( !isset( $this->mAllPages
[$row->rd_namespace
][$row->rd_title
] ) ) {
603 $lb->add( $row->rd_namespace
, $row->rd_title
);
605 $this->mRedirectTitles
[$from] = $to;
608 if ( $this->mPendingRedirectIDs
) {
609 // We found pages that aren't in the redirect table
611 foreach ( $this->mPendingRedirectIDs
as $id => $title ) {
612 $article = new Article( $title );
613 $rt = $article->insertRedirect();
615 // What the hell. Let's just ignore this
619 $this->mRedirectTitles
[$title->getPrefixedText()] = $rt->getPrefixedText();
620 unset( $this->mPendingRedirectIDs
[$id] );
627 * Given an array of title strings, convert them into Title objects.
628 * Alternativelly, an array of Title objects may be given.
629 * This method validates access rights for the title,
630 * and appends normalization values to the output.
632 * @param $titles array of Title objects or strings
635 private function processTitlesArray( $titles ) {
636 $linkBatch = new LinkBatch();
638 foreach ( $titles as $title ) {
639 $titleObj = is_string( $title ) ? Title
::newFromText( $title ) : $title;
641 // Handle invalid titles gracefully
642 $this->mAllpages
[0][$title] = $this->mFakePageId
;
643 $this->mInvalidTitles
[$this->mFakePageId
] = $title;
644 $this->mFakePageId
--;
645 continue; // There's nothing else we can do
647 $iw = $titleObj->getInterwiki();
648 if ( strval( $iw ) !== '' ) {
649 // This title is an interwiki link.
650 $this->mInterwikiTitles
[$titleObj->getPrefixedText()] = $iw;
653 if ( $titleObj->getNamespace() < 0 ) {
654 $this->setWarning( 'No support for special pages has been implemented' );
656 $linkBatch->addObj( $titleObj );
660 // Make sure we remember the original title that was
661 // given to us. This way the caller can correlate new
662 // titles with the originally requested when e.g. the
663 // namespace is localized or the capitalization is
665 if ( is_string( $title ) && $title !== $titleObj->getPrefixedText() ) {
666 $this->mNormalizedTitles
[$title] = $titleObj->getPrefixedText();
673 protected function getAllowedParams() {
676 ApiBase
::PARAM_ISMULTI
=> true
679 ApiBase
::PARAM_TYPE
=> 'integer',
680 ApiBase
::PARAM_ISMULTI
=> true
683 ApiBase
::PARAM_TYPE
=> 'integer',
684 ApiBase
::PARAM_ISMULTI
=> true
689 protected function getParamDescription() {
691 'titles' => 'A list of titles to work on',
692 'pageids' => 'A list of page IDs to work on',
693 'revids' => 'A list of revision IDs to work on'
697 public function getPossibleErrors() {
698 return array_merge( parent
::getPossibleErrors(), array(
699 array( 'code' => 'multisource', 'info' => "Cannot use 'pageids' at the same time as 'dataSource'" ),
700 array( 'code' => 'multisource', 'info' => "Cannot use 'revids' at the same time as 'dataSource'" ),
704 public function getVersion() {
705 return __CLASS__
. ': $Id$';