96be5ef02e1a9e19b382e9c16fcaac9b5a1f9220
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 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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, $mSpecialTitles;
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();
75 $this->mSpecialTitles
= array();
77 $this->mRequestedPageFields
= array();
78 $this->mResolveRedirects
= $resolveRedirects;
79 if ( $resolveRedirects ) {
80 $this->mPendingRedirectIDs
= array();
83 $this->mFakePageId
= - 1;
87 * Check whether this PageSet is resolving redirects
90 public function isResolvingRedirects() {
91 return $this->mResolveRedirects
;
95 * Request an additional field from the page table. Must be called
97 * @param $fieldName string Field name
99 public function requestField( $fieldName ) {
100 $this->mRequestedPageFields
[$fieldName] = null;
104 * Get the value of a custom field previously requested through
106 * @param $fieldName string Field name
107 * @return mixed Field value
109 public function getCustomField( $fieldName ) {
110 return $this->mRequestedPageFields
[$fieldName];
114 * Get the fields that have to be queried from the page table:
115 * the ones requested through requestField() and a few basic ones
117 * @return array of field names
119 public function getPageTableFields() {
120 // Ensure we get minimum required fields
121 // DON'T change this order
123 'page_namespace' => null,
124 'page_title' => null,
128 if ( $this->mResolveRedirects
) {
129 $pageFlds['page_is_redirect'] = null;
132 // only store non-default fields
133 $this->mRequestedPageFields
= array_diff_key( $this->mRequestedPageFields
, $pageFlds );
135 $pageFlds = array_merge( $pageFlds, $this->mRequestedPageFields
);
136 return array_keys( $pageFlds );
140 * Returns an array [ns][dbkey] => page_id for all requested titles.
141 * page_id is a unique negative number in case title was not found.
142 * Invalid titles will also have negative page IDs and will be in namespace 0
145 public function getAllTitlesByNamespace() {
146 return $this->mAllPages
;
150 * All Title objects provided.
151 * @return array of Title objects
153 public function getTitles() {
154 return $this->mTitles
;
158 * Returns the number of unique pages (not revisions) in the set.
161 public function getTitleCount() {
162 return count( $this->mTitles
);
166 * Title objects that were found in the database.
167 * @return array page_id (int) => Title (obj)
169 public function getGoodTitles() {
170 return $this->mGoodTitles
;
174 * Returns the number of found unique pages (not revisions) in the set.
177 public function getGoodTitleCount() {
178 return count( $this->mGoodTitles
);
182 * Title objects that were NOT found in the database.
183 * The array's index will be negative for each item
184 * @return array of Title objects
186 public function getMissingTitles() {
187 return $this->mMissingTitles
;
191 * Titles that were deemed invalid by Title::newFromText()
192 * The array's index will be unique and negative for each item
193 * @return array of strings (not Title objects)
195 public function getInvalidTitles() {
196 return $this->mInvalidTitles
;
200 * Page IDs that were not found in the database
201 * @return array of page IDs
203 public function getMissingPageIDs() {
204 return $this->mMissingPageIDs
;
208 * Get a list of redirect resolutions - maps a title to its redirect
210 * @return array prefixed_title (string) => prefixed_title (string)
212 public function getRedirectTitles() {
213 return $this->mRedirectTitles
;
217 * Get a list of title normalizations - maps a title to its normalized
219 * @return array raw_prefixed_title (string) => prefixed_title (string)
221 public function getNormalizedTitles() {
222 return $this->mNormalizedTitles
;
226 * Get a list of interwiki titles - maps a title to its interwiki
228 * @return array raw_prefixed_title (string) => interwiki_prefix (string)
230 public function getInterwikiTitles() {
231 return $this->mInterwikiTitles
;
235 * Get the list of revision IDs (requested with the revids= parameter)
236 * @return array revID (int) => pageID (int)
238 public function getRevisionIDs() {
239 return $this->mGoodRevIDs
;
243 * Revision IDs that were not found in the database
244 * @return array of revision IDs
246 public function getMissingRevisionIDs() {
247 return $this->mMissingRevIDs
;
251 * Get the list of titles with negative namespace
252 * @return array Title
254 public function getSpecialTitles() {
255 return $this->mSpecialTitles
;
259 * Returns the number of revisions (requested with revids= parameter)\
262 public function getRevisionCount() {
263 return count( $this->getRevisionIDs() );
267 * Populate the PageSet from the request parameters.
269 public function execute() {
271 $params = $this->extractRequestParams();
273 // Only one of the titles/pageids/revids is allowed at the same time
275 if ( isset( $params['titles'] ) ) {
276 $dataSource = 'titles';
278 if ( isset( $params['pageids'] ) ) {
279 if ( isset( $dataSource ) ) {
280 $this->dieUsage( "Cannot use 'pageids' at the same time as '$dataSource'", 'multisource' );
282 $dataSource = 'pageids';
284 if ( isset( $params['revids'] ) ) {
285 if ( isset( $dataSource ) ) {
286 $this->dieUsage( "Cannot use 'revids' at the same time as '$dataSource'", 'multisource' );
288 $dataSource = 'revids';
291 switch ( $dataSource ) {
293 $this->initFromTitles( $params['titles'] );
296 $this->initFromPageIds( $params['pageids'] );
299 if ( $this->mResolveRedirects
) {
300 $this->setWarning( 'Redirect resolution cannot be used together with the revids= parameter. ' .
301 'Any redirects the revids= point to have not been resolved.' );
303 $this->mResolveRedirects
= false;
304 $this->initFromRevIDs( $params['revids'] );
307 // Do nothing - some queries do not need any of the data sources.
314 * Populate this PageSet from a list of Titles
315 * @param $titles array of Title objects
317 public function populateFromTitles( $titles ) {
319 $this->initFromTitles( $titles );
324 * Populate this PageSet from a list of page IDs
325 * @param $pageIDs array of page IDs
327 public function populateFromPageIDs( $pageIDs ) {
329 $this->initFromPageIds( $pageIDs );
334 * Populate this PageSet from a rowset returned from the database
335 * @param $db Database object
336 * @param $queryResult Query result object
338 public function populateFromQueryResult( $db, $queryResult ) {
340 $this->initFromQueryResult( $db, $queryResult );
345 * Populate this PageSet from a list of revision IDs
346 * @param $revIDs array of revision IDs
348 public function populateFromRevisionIDs( $revIDs ) {
350 $this->initFromRevIDs( $revIDs );
355 * Extract all requested fields from the row received from the database
356 * @param $row Result row
358 public function processDbRow( $row ) {
359 // Store Title object in various data structures
360 $title = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
362 $pageId = intval( $row->page_id
);
363 $this->mAllPages
[$row->page_namespace
][$row->page_title
] = $pageId;
364 $this->mTitles
[] = $title;
366 if ( $this->mResolveRedirects
&& $row->page_is_redirect
== '1' ) {
367 $this->mPendingRedirectIDs
[$pageId] = $title;
369 $this->mGoodTitles
[$pageId] = $title;
372 foreach ( $this->mRequestedPageFields
as $fieldName => &$fieldValues ) {
373 $fieldValues[$pageId] = $row-> $fieldName;
378 * Resolve redirects, if applicable
380 public function finishPageSetGeneration() {
382 $this->resolvePendingRedirects();
387 * This method populates internal variables with page information
388 * based on the given array of title strings.
391 * #1 For each title, get data from `page` table
392 * #2 If page was not found in the DB, store it as missing
394 * Additionally, when resolving redirects:
395 * #3 If no more redirects left, stop.
396 * #4 For each redirect, get its target from the `redirect` table.
397 * #5 Substitute the original LinkBatch object with the new list
398 * #6 Repeat from step #1
400 * @param $titles array of Title objects or strings
402 private function initFromTitles( $titles ) {
403 // Get validated and normalized title objects
404 $linkBatch = $this->processTitlesArray( $titles );
405 if ( $linkBatch->isEmpty() ) {
409 $db = $this->getDB();
410 $set = $linkBatch->constructSet( 'page', $db );
412 // Get pageIDs data from the `page` table
413 $this->profileDBIn();
414 $res = $db->select( 'page', $this->getPageTableFields(), $set,
416 $this->profileDBOut();
418 // Hack: get the ns:titles stored in array(ns => array(titles)) format
419 $this->initFromQueryResult( $db, $res, $linkBatch->data
, true ); // process Titles
421 // Resolve any found redirects
422 $this->resolvePendingRedirects();
426 * Does the same as initFromTitles(), but is based on page IDs instead
427 * @param $pageids array of page IDs
429 private function initFromPageIds( $pageids ) {
430 if ( !count( $pageids ) ) {
434 $pageids = array_map( 'intval', $pageids ); // paranoia
436 'page_id' => $pageids
438 $db = $this->getDB();
440 // Get pageIDs data from the `page` table
441 $this->profileDBIn();
442 $res = $db->select( 'page', $this->getPageTableFields(), $set,
444 $this->profileDBOut();
446 $remaining = array_flip( $pageids );
447 $this->initFromQueryResult( $db, $res, $remaining, false ); // process PageIDs
449 // Resolve any found redirects
450 $this->resolvePendingRedirects();
454 * Iterate through the result of the query on 'page' table,
455 * and for each row create and store title object and save any extra fields requested.
456 * @param $db Database
457 * @param $res DB Query result
458 * @param $remaining array of either pageID or ns/title elements (optional).
459 * If given, any missing items will go to $mMissingPageIDs and $mMissingTitles
460 * @param $processTitles bool Must be provided together with $remaining.
461 * If true, treat $remaining as an array of [ns][title]
462 * If false, treat it as an array of [pageIDs]
464 private function initFromQueryResult( $db, $res, &$remaining = null, $processTitles = null ) {
465 if ( !is_null( $remaining ) && is_null( $processTitles ) ) {
466 ApiBase
::dieDebug( __METHOD__
, 'Missing $processTitles parameter when $remaining is provided' );
469 foreach ( $res as $row ) {
470 $pageId = intval( $row->page_id
);
472 // Remove found page from the list of remaining items
473 if ( isset( $remaining ) ) {
474 if ( $processTitles ) {
475 unset( $remaining[$row->page_namespace
][$row->page_title
] );
477 unset( $remaining[$pageId] );
481 // Store any extra fields requested by modules
482 $this->processDbRow( $row );
485 if ( isset( $remaining ) ) {
486 // Any items left in the $remaining list are added as missing
487 if ( $processTitles ) {
488 // The remaining titles in $remaining are non-existent pages
489 foreach ( $remaining as $ns => $dbkeys ) {
490 foreach ( $dbkeys as $dbkey => $unused ) {
491 $title = Title
::makeTitle( $ns, $dbkey );
492 $this->mAllPages
[$ns][$dbkey] = $this->mFakePageId
;
493 $this->mMissingTitles
[$this->mFakePageId
] = $title;
494 $this->mFakePageId
--;
495 $this->mTitles
[] = $title;
499 // The remaining pageids do not exist
500 if ( !$this->mMissingPageIDs
) {
501 $this->mMissingPageIDs
= array_keys( $remaining );
503 $this->mMissingPageIDs
= array_merge( $this->mMissingPageIDs
, array_keys( $remaining ) );
510 * Does the same as initFromTitles(), but is based on revision IDs
512 * @param $revids array of revision IDs
514 private function initFromRevIDs( $revids ) {
515 if ( !count( $revids ) ) {
519 $revids = array_map( 'intval', $revids ); // paranoia
520 $db = $this->getDB();
522 $remaining = array_flip( $revids );
524 $tables = array( 'revision', 'page' );
525 $fields = array( 'rev_id', 'rev_page' );
526 $where = array( 'rev_id' => $revids, 'rev_page = page_id' );
528 // Get pageIDs data from the `page` table
529 $this->profileDBIn();
530 $res = $db->select( $tables, $fields, $where, __METHOD__
);
531 foreach ( $res as $row ) {
532 $revid = intval( $row->rev_id
);
533 $pageid = intval( $row->rev_page
);
534 $this->mGoodRevIDs
[$revid] = $pageid;
535 $pageids[$pageid] = '';
536 unset( $remaining[$revid] );
538 $this->profileDBOut();
540 $this->mMissingRevIDs
= array_keys( $remaining );
542 // Populate all the page information
543 $this->initFromPageIds( array_keys( $pageids ) );
547 * Resolve any redirects in the result if redirect resolution was
548 * requested. This function is called repeatedly until all redirects
549 * have been resolved.
551 private function resolvePendingRedirects() {
552 if ( $this->mResolveRedirects
) {
553 $db = $this->getDB();
554 $pageFlds = $this->getPageTableFields();
556 // Repeat until all redirects have been resolved
557 // The infinite loop is prevented by keeping all known pages in $this->mAllPages
558 while ( $this->mPendingRedirectIDs
) {
559 // Resolve redirects by querying the pagelinks table, and repeat the process
560 // Create a new linkBatch object for the next pass
561 $linkBatch = $this->getRedirectTargets();
563 if ( $linkBatch->isEmpty() ) {
567 $set = $linkBatch->constructSet( 'page', $db );
568 if ( $set === false ) {
572 // Get pageIDs data from the `page` table
573 $this->profileDBIn();
574 $res = $db->select( 'page', $pageFlds, $set, __METHOD__
);
575 $this->profileDBOut();
577 // Hack: get the ns:titles stored in array(ns => array(titles)) format
578 $this->initFromQueryResult( $db, $res, $linkBatch->data
, true );
584 * Get the targets of the pending redirects from the database
586 * Also creates entries in the redirect table for redirects that don't
590 private function getRedirectTargets() {
591 $lb = new LinkBatch();
592 $db = $this->getDB();
594 $this->profileDBIn();
601 ), array( 'rd_from' => array_keys( $this->mPendingRedirectIDs
) ),
604 $this->profileDBOut();
606 foreach ( $res as $row ) {
607 $rdfrom = intval( $row->rd_from
);
608 $from = $this->mPendingRedirectIDs
[$rdfrom]->getPrefixedText();
609 $to = Title
::makeTitle( $row->rd_namespace
, $row->rd_title
)->getPrefixedText();
610 unset( $this->mPendingRedirectIDs
[$rdfrom] );
611 if ( !isset( $this->mAllPages
[$row->rd_namespace
][$row->rd_title
] ) ) {
612 $lb->add( $row->rd_namespace
, $row->rd_title
);
614 $this->mRedirectTitles
[$from] = $to;
617 if ( $this->mPendingRedirectIDs
) {
618 // We found pages that aren't in the redirect table
620 foreach ( $this->mPendingRedirectIDs
as $id => $title ) {
621 $article = new Article( $title );
622 $rt = $article->insertRedirect();
624 // What the hell. Let's just ignore this
628 $this->mRedirectTitles
[$title->getPrefixedText()] = $rt->getPrefixedText();
629 unset( $this->mPendingRedirectIDs
[$id] );
636 * Given an array of title strings, convert them into Title objects.
637 * Alternativelly, an array of Title objects may be given.
638 * This method validates access rights for the title,
639 * and appends normalization values to the output.
641 * @param $titles array of Title objects or strings
644 private function processTitlesArray( $titles ) {
645 $linkBatch = new LinkBatch();
647 foreach ( $titles as $title ) {
648 $titleObj = is_string( $title ) ? Title
::newFromText( $title ) : $title;
650 // Handle invalid titles gracefully
651 $this->mAllpages
[0][$title] = $this->mFakePageId
;
652 $this->mInvalidTitles
[$this->mFakePageId
] = $title;
653 $this->mFakePageId
--;
654 continue; // There's nothing else we can do
656 $iw = $titleObj->getInterwiki();
657 if ( strval( $iw ) !== '' ) {
658 // This title is an interwiki link.
659 $this->mInterwikiTitles
[$titleObj->getPrefixedText()] = $iw;
661 if ( $titleObj->getNamespace() < 0 ) {
662 $titleObj = $titleObj->fixSpecialName();
663 $this->mSpecialTitles
[$this->mFakePageId
] = $titleObj;
664 $this->mFakePageId
--;
666 $linkBatch->addObj( $titleObj );
670 // Make sure we remember the original title that was
671 // given to us. This way the caller can correlate new
672 // titles with the originally requested when e.g. the
673 // namespace is localized or the capitalization is
675 if ( is_string( $title ) && $title !== $titleObj->getPrefixedText() ) {
676 $this->mNormalizedTitles
[$title] = $titleObj->getPrefixedText();
683 protected function getAllowedParams() {
686 ApiBase
::PARAM_ISMULTI
=> true
689 ApiBase
::PARAM_TYPE
=> 'integer',
690 ApiBase
::PARAM_ISMULTI
=> true
693 ApiBase
::PARAM_TYPE
=> 'integer',
694 ApiBase
::PARAM_ISMULTI
=> true
699 protected function getParamDescription() {
701 'titles' => 'A list of titles to work on',
702 'pageids' => 'A list of page IDs to work on',
703 'revids' => 'A list of revision IDs to work on'
707 public function getPossibleErrors() {
708 return array_merge( parent
::getPossibleErrors(), array(
709 array( 'code' => 'multisource', 'info' => "Cannot use 'pageids' at the same time as 'dataSource'" ),
710 array( 'code' => 'multisource', 'info' => "Cannot use 'revids' at the same time as 'dataSource'" ),
714 public function getVersion() {
715 return __CLASS__
. ': $Id$';