d462862518045c18d35a97f7da442cec8bde2a5b
5 * Created on Sep 24, 2006
7 * Copyright © 2006, 2013 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * http://www.gnu.org/copyleft/gpl.html
28 * This class contains a list of pages that the client has requested.
29 * Initially, when the client passes in titles=, pageids=, or revisions=
30 * parameter, an instance of the ApiPageSet class will normalize titles,
31 * determine if the pages/revisions exist, and prefetch any additional page
34 * When a generator is used, the result of the generator will become the input
35 * for the second instance of this class, and all subsequent actions will use
36 * the second instance for all their work.
39 * @since 1.21 derives from ApiBase instead of ApiQueryBase
41 class ApiPageSet
extends ApiBase
{
43 * Constructor flag: The new instance of ApiPageSet will ignore the 'generator=' parameter
46 const DISABLE_GENERATORS
= 1;
50 private $mResolveRedirects;
51 private $mConvertTitles;
52 private $mAllowGenerator;
54 private $mAllPages = array(); // [ns][dbkey] => page_id or negative when missing
55 private $mTitles = array();
56 private $mGoodAndMissingPages = array(); // [ns][dbkey] => page_id or negative when missing
57 private $mGoodPages = array(); // [ns][dbkey] => page_id
58 private $mGoodTitles = array();
59 private $mMissingPages = array(); // [ns][dbkey] => fake page_id
60 private $mMissingTitles = array();
61 private $mInvalidTitles = array();
62 private $mMissingPageIDs = array();
63 private $mRedirectTitles = array();
64 private $mSpecialTitles = array();
65 private $mNormalizedTitles = array();
66 private $mInterwikiTitles = array();
68 private $mPendingRedirectIDs = array();
69 private $mConvertedTitles = array();
70 private $mGoodRevIDs = array();
71 private $mLiveRevIDs = array();
72 private $mDeletedRevIDs = array();
73 private $mMissingRevIDs = array();
74 private $mGeneratorData = array(); // [ns][dbkey] => data array
75 private $mFakePageId = -1;
76 private $mCacheMode = 'public';
77 private $mRequestedPageFields = array();
79 private $mDefaultNamespace = NS_MAIN
;
82 * Add all items from $values into the result
83 * @param array $result Output
84 * @param array $values Values to add
85 * @param string $flag The name of the boolean flag to mark this element
86 * @param string $name If given, name of the value
88 private static function addValues( array &$result, $values, $flag = null, $name = null ) {
89 foreach ( $values as $val ) {
90 if ( $val instanceof Title
) {
92 ApiQueryBase
::addTitleInfo( $v, $val );
93 } elseif ( $name !== null ) {
94 $v = array( $name => $val );
98 if ( $flag !== null ) {
106 * @param ApiBase $dbSource Module implementing getDB().
107 * Allows PageSet to reuse existing db connection from the shared state like ApiQuery.
108 * @param int $flags Zero or more flags like DISABLE_GENERATORS
109 * @param int $defaultNamespace The namespace to use if none is specified by a prefix.
110 * @since 1.21 accepts $flags instead of two boolean values
112 public function __construct( ApiBase
$dbSource, $flags = 0, $defaultNamespace = NS_MAIN
) {
113 parent
::__construct( $dbSource->getMain(), $dbSource->getModuleName() );
114 $this->mDbSource
= $dbSource;
115 $this->mAllowGenerator
= ( $flags & ApiPageSet
::DISABLE_GENERATORS
) == 0;
116 $this->mDefaultNamespace
= $defaultNamespace;
118 $this->mParams
= $this->extractRequestParams();
119 $this->mResolveRedirects
= $this->mParams
['redirects'];
120 $this->mConvertTitles
= $this->mParams
['converttitles'];
124 * In case execute() is not called, call this method to mark all relevant parameters as used
125 * This prevents unused parameters from being reported as warnings
127 public function executeDryRun() {
128 $this->executeInternal( true );
132 * Populate the PageSet from the request parameters.
134 public function execute() {
135 $this->executeInternal( false );
139 * Populate the PageSet from the request parameters.
140 * @param bool $isDryRun If true, instantiates generator, but only to mark
141 * relevant parameters as used
143 private function executeInternal( $isDryRun ) {
144 $generatorName = $this->mAllowGenerator ?
$this->mParams
['generator'] : null;
145 if ( isset( $generatorName ) ) {
146 $dbSource = $this->mDbSource
;
147 if ( !$dbSource instanceof ApiQuery
) {
148 // If the parent container of this pageset is not ApiQuery, we must create it to run generator
149 $dbSource = $this->getMain()->getModuleManager()->getModule( 'query' );
151 $generator = $dbSource->getModuleManager()->getModule( $generatorName, null, true );
152 if ( $generator === null ) {
153 $this->dieUsage( 'Unknown generator=' . $generatorName, 'badgenerator' );
155 if ( !$generator instanceof ApiQueryGeneratorBase
) {
156 $this->dieUsage( "Module $generatorName cannot be used as a generator", 'badgenerator' );
158 // Create a temporary pageset to store generator's output,
159 // add any additional fields generator may need, and execute pageset to populate titles/pageids
160 $tmpPageSet = new ApiPageSet( $dbSource, ApiPageSet
::DISABLE_GENERATORS
);
161 $generator->setGeneratorMode( $tmpPageSet );
162 $this->mCacheMode
= $generator->getCacheMode( $generator->extractRequestParams() );
165 $generator->requestExtraData( $tmpPageSet );
167 $tmpPageSet->executeInternal( $isDryRun );
169 // populate this pageset with the generator output
171 $generator->executeGenerator( $this );
172 Hooks
::run( 'APIQueryGeneratorAfterExecute', array( &$generator, &$this ) );
174 // Prevent warnings from being reported on these parameters
175 $main = $this->getMain();
176 foreach ( $generator->extractRequestParams() as $paramName => $param ) {
177 $main->getVal( $generator->encodeParamName( $paramName ) );
182 $this->resolvePendingRedirects();
185 // Only one of the titles/pageids/revids is allowed at the same time
187 if ( isset( $this->mParams
['titles'] ) ) {
188 $dataSource = 'titles';
190 if ( isset( $this->mParams
['pageids'] ) ) {
191 if ( isset( $dataSource ) ) {
192 $this->dieUsage( "Cannot use 'pageids' at the same time as '$dataSource'", 'multisource' );
194 $dataSource = 'pageids';
196 if ( isset( $this->mParams
['revids'] ) ) {
197 if ( isset( $dataSource ) ) {
198 $this->dieUsage( "Cannot use 'revids' at the same time as '$dataSource'", 'multisource' );
200 $dataSource = 'revids';
204 // Populate page information with the original user input
205 switch ( $dataSource ) {
207 $this->initFromTitles( $this->mParams
['titles'] );
210 $this->initFromPageIds( $this->mParams
['pageids'] );
213 if ( $this->mResolveRedirects
) {
214 $this->setWarning( 'Redirect resolution cannot be used ' .
215 'together with the revids= parameter. Any redirects ' .
216 'the revids= point to have not been resolved.' );
218 $this->mResolveRedirects
= false;
219 $this->initFromRevIDs( $this->mParams
['revids'] );
222 // Do nothing - some queries do not need any of the data sources.
230 * Check whether this PageSet is resolving redirects
233 public function isResolvingRedirects() {
234 return $this->mResolveRedirects
;
238 * Return the parameter name that is the source of data for this PageSet
240 * If multiple source parameters are specified (e.g. titles and pageids),
241 * one will be named arbitrarily.
243 * @return string|null
245 public function getDataSource() {
246 if ( $this->mAllowGenerator
&& isset( $this->mParams
['generator'] ) ) {
249 if ( isset( $this->mParams
['titles'] ) ) {
252 if ( isset( $this->mParams
['pageids'] ) ) {
255 if ( isset( $this->mParams
['revids'] ) ) {
263 * Request an additional field from the page table.
264 * Must be called before execute()
265 * @param string $fieldName Field name
267 public function requestField( $fieldName ) {
268 $this->mRequestedPageFields
[$fieldName] = null;
272 * Get the value of a custom field previously requested through
274 * @param string $fieldName Field name
275 * @return mixed Field value
277 public function getCustomField( $fieldName ) {
278 return $this->mRequestedPageFields
[$fieldName];
282 * Get the fields that have to be queried from the page table:
283 * the ones requested through requestField() and a few basic ones
285 * @return array Array of field names
287 public function getPageTableFields() {
288 // Ensure we get minimum required fields
289 // DON'T change this order
291 'page_namespace' => null,
292 'page_title' => null,
296 if ( $this->mResolveRedirects
) {
297 $pageFlds['page_is_redirect'] = null;
300 if ( $this->getConfig()->get( 'ContentHandlerUseDB' ) ) {
301 $pageFlds['page_content_model'] = null;
304 // only store non-default fields
305 $this->mRequestedPageFields
= array_diff_key( $this->mRequestedPageFields
, $pageFlds );
307 $pageFlds = array_merge( $pageFlds, $this->mRequestedPageFields
);
309 return array_keys( $pageFlds );
313 * Returns an array [ns][dbkey] => page_id for all requested titles.
314 * page_id is a unique negative number in case title was not found.
315 * Invalid titles will also have negative page IDs and will be in namespace 0
318 public function getAllTitlesByNamespace() {
319 return $this->mAllPages
;
323 * All Title objects provided.
326 public function getTitles() {
327 return $this->mTitles
;
331 * Returns the number of unique pages (not revisions) in the set.
334 public function getTitleCount() {
335 return count( $this->mTitles
);
339 * Returns an array [ns][dbkey] => page_id for all good titles.
342 public function getGoodTitlesByNamespace() {
343 return $this->mGoodPages
;
347 * Title objects that were found in the database.
348 * @return Title[] Array page_id (int) => Title (obj)
350 public function getGoodTitles() {
351 return $this->mGoodTitles
;
355 * Returns the number of found unique pages (not revisions) in the set.
358 public function getGoodTitleCount() {
359 return count( $this->mGoodTitles
);
363 * Returns an array [ns][dbkey] => fake_page_id for all missing titles.
364 * fake_page_id is a unique negative number.
367 public function getMissingTitlesByNamespace() {
368 return $this->mMissingPages
;
372 * Title objects that were NOT found in the database.
373 * The array's index will be negative for each item
376 public function getMissingTitles() {
377 return $this->mMissingTitles
;
381 * Returns an array [ns][dbkey] => page_id for all good and missing titles.
384 public function getGoodAndMissingTitlesByNamespace() {
385 return $this->mGoodAndMissingPages
;
389 * Title objects for good and missing titles.
392 public function getGoodAndMissingTitles() {
393 return $this->mGoodTitles +
$this->mMissingTitles
;
397 * Titles that were deemed invalid by Title::newFromText()
398 * The array's index will be unique and negative for each item
399 * @return string[] Array of strings (not Title objects)
401 public function getInvalidTitles() {
402 return $this->mInvalidTitles
;
406 * Page IDs that were not found in the database
407 * @return array Array of page IDs
409 public function getMissingPageIDs() {
410 return $this->mMissingPageIDs
;
414 * Get a list of redirect resolutions - maps a title to its redirect
415 * target, as an array of output-ready arrays
418 public function getRedirectTitles() {
419 return $this->mRedirectTitles
;
423 * Get a list of redirect resolutions - maps a title to its redirect
425 * @param ApiResult $result
426 * @return array Array of prefixed_title (string) => Title object
429 public function getRedirectTitlesAsResult( $result = null ) {
431 foreach ( $this->getRedirectTitles() as $titleStrFrom => $titleTo ) {
433 'from' => strval( $titleStrFrom ),
434 'to' => $titleTo->getPrefixedText(),
436 if ( $titleTo->hasFragment() ) {
437 $r['tofragment'] = $titleTo->getFragment();
439 if ( $titleTo->isExternal() ) {
440 $r['tointerwiki'] = $titleTo->getInterwiki();
444 if ( !empty( $values ) && $result ) {
445 $result->setIndexedTagName( $values, 'r' );
452 * Get a list of title normalizations - maps a title to its normalized
454 * @return array Array of raw_prefixed_title (string) => prefixed_title (string)
456 public function getNormalizedTitles() {
457 return $this->mNormalizedTitles
;
461 * Get a list of title normalizations - maps a title to its normalized
462 * version in the form of result array.
463 * @param ApiResult $result
464 * @return array Array of raw_prefixed_title (string) => prefixed_title (string)
467 public function getNormalizedTitlesAsResult( $result = null ) {
469 foreach ( $this->getNormalizedTitles() as $rawTitleStr => $titleStr ) {
471 'from' => $rawTitleStr,
475 if ( !empty( $values ) && $result ) {
476 $result->setIndexedTagName( $values, 'n' );
483 * Get a list of title conversions - maps a title to its converted
485 * @return array Array of raw_prefixed_title (string) => prefixed_title (string)
487 public function getConvertedTitles() {
488 return $this->mConvertedTitles
;
492 * Get a list of title conversions - maps a title to its converted
493 * version as a result array.
494 * @param ApiResult $result
495 * @return array Array of (from, to) strings
498 public function getConvertedTitlesAsResult( $result = null ) {
500 foreach ( $this->getConvertedTitles() as $rawTitleStr => $titleStr ) {
502 'from' => $rawTitleStr,
506 if ( !empty( $values ) && $result ) {
507 $result->setIndexedTagName( $values, 'c' );
514 * Get a list of interwiki titles - maps a title to its interwiki
516 * @return array Array of raw_prefixed_title (string) => interwiki_prefix (string)
518 public function getInterwikiTitles() {
519 return $this->mInterwikiTitles
;
523 * Get a list of interwiki titles - maps a title to its interwiki
525 * @param ApiResult $result
527 * @return array Array of raw_prefixed_title (string) => interwiki_prefix (string)
530 public function getInterwikiTitlesAsResult( $result = null, $iwUrl = false ) {
532 foreach ( $this->getInterwikiTitles() as $rawTitleStr => $interwikiStr ) {
534 'title' => $rawTitleStr,
535 'iw' => $interwikiStr,
538 $title = Title
::newFromText( $rawTitleStr );
539 $item['url'] = $title->getFullURL( '', false, PROTO_CURRENT
);
543 if ( !empty( $values ) && $result ) {
544 $result->setIndexedTagName( $values, 'i' );
551 * Get an array of invalid/special/missing titles.
553 * @param array $invalidChecks List of types of invalid titles to include.
554 * Recognized values are:
555 * - invalidTitles: Titles from $this->getInvalidTitles()
556 * - special: Titles from $this->getSpecialTitles()
557 * - missingIds: ids from $this->getMissingPageIDs()
558 * - missingRevIds: ids from $this->getMissingRevisionIDs()
559 * - missingTitles: Titles from $this->getMissingTitles()
560 * - interwikiTitles: Titles from $this->getInterwikiTitlesAsResult()
561 * @return array Array suitable for inclusion in the response
564 public function getInvalidTitlesAndRevisions( $invalidChecks = array( 'invalidTitles',
565 'special', 'missingIds', 'missingRevIds', 'missingTitles', 'interwikiTitles' )
568 if ( in_array( "invalidTitles", $invalidChecks ) ) {
569 self
::addValues( $result, $this->getInvalidTitles(), 'invalid', 'title' );
571 if ( in_array( "special", $invalidChecks ) ) {
572 self
::addValues( $result, $this->getSpecialTitles(), 'special', 'title' );
574 if ( in_array( "missingIds", $invalidChecks ) ) {
575 self
::addValues( $result, $this->getMissingPageIDs(), 'missing', 'pageid' );
577 if ( in_array( "missingRevIds", $invalidChecks ) ) {
578 self
::addValues( $result, $this->getMissingRevisionIDs(), 'missing', 'revid' );
580 if ( in_array( "missingTitles", $invalidChecks ) ) {
581 self
::addValues( $result, $this->getMissingTitles(), 'missing' );
583 if ( in_array( "interwikiTitles", $invalidChecks ) ) {
584 self
::addValues( $result, $this->getInterwikiTitlesAsResult() );
591 * Get the list of valid revision IDs (requested with the revids= parameter)
592 * @return array Array of revID (int) => pageID (int)
594 public function getRevisionIDs() {
595 return $this->mGoodRevIDs
;
599 * Get the list of non-deleted revision IDs (requested with the revids= parameter)
600 * @return array Array of revID (int) => pageID (int)
602 public function getLiveRevisionIDs() {
603 return $this->mLiveRevIDs
;
607 * Get the list of revision IDs that were associated with deleted titles.
608 * @return array Array of revID (int) => pageID (int)
610 public function getDeletedRevisionIDs() {
611 return $this->mDeletedRevIDs
;
615 * Revision IDs that were not found in the database
616 * @return array Array of revision IDs
618 public function getMissingRevisionIDs() {
619 return $this->mMissingRevIDs
;
623 * Revision IDs that were not found in the database as result array.
624 * @param ApiResult $result
625 * @return array Array of revision IDs
628 public function getMissingRevisionIDsAsResult( $result = null ) {
630 foreach ( $this->getMissingRevisionIDs() as $revid ) {
631 $values[$revid] = array(
635 if ( !empty( $values ) && $result ) {
636 $result->setIndexedTagName( $values, 'rev' );
643 * Get the list of titles with negative namespace
646 public function getSpecialTitles() {
647 return $this->mSpecialTitles
;
651 * Returns the number of revisions (requested with revids= parameter).
652 * @return int Number of revisions.
654 public function getRevisionCount() {
655 return count( $this->getRevisionIDs() );
659 * Populate this PageSet from a list of Titles
660 * @param array $titles Array of Title objects
662 public function populateFromTitles( $titles ) {
663 $this->initFromTitles( $titles );
667 * Populate this PageSet from a list of page IDs
668 * @param array $pageIDs Array of page IDs
670 public function populateFromPageIDs( $pageIDs ) {
671 $this->initFromPageIds( $pageIDs );
675 * Populate this PageSet from a rowset returned from the database
677 * Note that the query result must include the columns returned by
678 * $this->getPageTableFields().
680 * @param DatabaseBase $db
681 * @param ResultWrapper $queryResult Query result object
683 public function populateFromQueryResult( $db, $queryResult ) {
684 $this->initFromQueryResult( $queryResult );
688 * Populate this PageSet from a list of revision IDs
689 * @param array $revIDs Array of revision IDs
691 public function populateFromRevisionIDs( $revIDs ) {
692 $this->initFromRevIDs( $revIDs );
696 * Extract all requested fields from the row received from the database
697 * @param stdClass $row Result row
699 public function processDbRow( $row ) {
700 // Store Title object in various data structures
701 $title = Title
::newFromRow( $row );
703 $pageId = intval( $row->page_id
);
704 $this->mAllPages
[$row->page_namespace
][$row->page_title
] = $pageId;
705 $this->mTitles
[] = $title;
707 if ( $this->mResolveRedirects
&& $row->page_is_redirect
== '1' ) {
708 $this->mPendingRedirectIDs
[$pageId] = $title;
710 $this->mGoodPages
[$row->page_namespace
][$row->page_title
] = $pageId;
711 $this->mGoodAndMissingPages
[$row->page_namespace
][$row->page_title
] = $pageId;
712 $this->mGoodTitles
[$pageId] = $title;
715 foreach ( $this->mRequestedPageFields
as $fieldName => &$fieldValues ) {
716 $fieldValues[$pageId] = $row->$fieldName;
721 * Do not use, does nothing, will be removed
722 * @deprecated since 1.21
724 public function finishPageSetGeneration() {
725 wfDeprecated( __METHOD__
, '1.21' );
729 * This method populates internal variables with page information
730 * based on the given array of title strings.
733 * #1 For each title, get data from `page` table
734 * #2 If page was not found in the DB, store it as missing
736 * Additionally, when resolving redirects:
737 * #3 If no more redirects left, stop.
738 * #4 For each redirect, get its target from the `redirect` table.
739 * #5 Substitute the original LinkBatch object with the new list
740 * #6 Repeat from step #1
742 * @param array $titles Array of Title objects or strings
744 private function initFromTitles( $titles ) {
745 // Get validated and normalized title objects
746 $linkBatch = $this->processTitlesArray( $titles );
747 if ( $linkBatch->isEmpty() ) {
751 $db = $this->getDB();
752 $set = $linkBatch->constructSet( 'page', $db );
754 // Get pageIDs data from the `page` table
755 $res = $db->select( 'page', $this->getPageTableFields(), $set,
758 // Hack: get the ns:titles stored in array(ns => array(titles)) format
759 $this->initFromQueryResult( $res, $linkBatch->data
, true ); // process Titles
761 // Resolve any found redirects
762 $this->resolvePendingRedirects();
766 * Does the same as initFromTitles(), but is based on page IDs instead
767 * @param array $pageids Array of page IDs
769 private function initFromPageIds( $pageids ) {
774 $pageids = array_map( 'intval', $pageids ); // paranoia
775 $remaining = array_flip( $pageids );
777 $pageids = self
::getPositiveIntegers( $pageids );
780 if ( !empty( $pageids ) ) {
782 'page_id' => $pageids
784 $db = $this->getDB();
786 // Get pageIDs data from the `page` table
787 $res = $db->select( 'page', $this->getPageTableFields(), $set,
791 $this->initFromQueryResult( $res, $remaining, false ); // process PageIDs
793 // Resolve any found redirects
794 $this->resolvePendingRedirects();
798 * Iterate through the result of the query on 'page' table,
799 * and for each row create and store title object and save any extra fields requested.
800 * @param ResultWrapper $res DB Query result
801 * @param array $remaining Array of either pageID or ns/title elements (optional).
802 * If given, any missing items will go to $mMissingPageIDs and $mMissingTitles
803 * @param bool $processTitles Must be provided together with $remaining.
804 * If true, treat $remaining as an array of [ns][title]
805 * If false, treat it as an array of [pageIDs]
807 private function initFromQueryResult( $res, &$remaining = null, $processTitles = null ) {
808 if ( !is_null( $remaining ) && is_null( $processTitles ) ) {
809 ApiBase
::dieDebug( __METHOD__
, 'Missing $processTitles parameter when $remaining is provided' );
812 $usernames = array();
814 foreach ( $res as $row ) {
815 $pageId = intval( $row->page_id
);
817 // Remove found page from the list of remaining items
818 if ( isset( $remaining ) ) {
819 if ( $processTitles ) {
820 unset( $remaining[$row->page_namespace
][$row->page_title
] );
822 unset( $remaining[$pageId] );
826 // Store any extra fields requested by modules
827 $this->processDbRow( $row );
829 // Need gender information
830 if ( MWNamespace
::hasGenderDistinction( $row->page_namespace
) ) {
831 $usernames[] = $row->page_title
;
836 if ( isset( $remaining ) ) {
837 // Any items left in the $remaining list are added as missing
838 if ( $processTitles ) {
839 // The remaining titles in $remaining are non-existent pages
840 foreach ( $remaining as $ns => $dbkeys ) {
841 foreach ( array_keys( $dbkeys ) as $dbkey ) {
842 $title = Title
::makeTitle( $ns, $dbkey );
843 $this->mAllPages
[$ns][$dbkey] = $this->mFakePageId
;
844 $this->mMissingPages
[$ns][$dbkey] = $this->mFakePageId
;
845 $this->mGoodAndMissingPages
[$ns][$dbkey] = $this->mFakePageId
;
846 $this->mMissingTitles
[$this->mFakePageId
] = $title;
847 $this->mFakePageId
--;
848 $this->mTitles
[] = $title;
850 // need gender information
851 if ( MWNamespace
::hasGenderDistinction( $ns ) ) {
852 $usernames[] = $dbkey;
857 // The remaining pageids do not exist
858 if ( !$this->mMissingPageIDs
) {
859 $this->mMissingPageIDs
= array_keys( $remaining );
861 $this->mMissingPageIDs
= array_merge( $this->mMissingPageIDs
, array_keys( $remaining ) );
866 // Get gender information
867 $genderCache = GenderCache
::singleton();
868 $genderCache->doQuery( $usernames, __METHOD__
);
872 * Does the same as initFromTitles(), but is based on revision IDs
874 * @param array $revids Array of revision IDs
876 private function initFromRevIDs( $revids ) {
881 $revids = array_map( 'intval', $revids ); // paranoia
882 $db = $this->getDB();
884 $remaining = array_flip( $revids );
886 $revids = self
::getPositiveIntegers( $revids );
888 if ( !empty( $revids ) ) {
889 $tables = array( 'revision', 'page' );
890 $fields = array( 'rev_id', 'rev_page' );
891 $where = array( 'rev_id' => $revids, 'rev_page = page_id' );
893 // Get pageIDs data from the `page` table
894 $res = $db->select( $tables, $fields, $where, __METHOD__
);
895 foreach ( $res as $row ) {
896 $revid = intval( $row->rev_id
);
897 $pageid = intval( $row->rev_page
);
898 $this->mGoodRevIDs
[$revid] = $pageid;
899 $this->mLiveRevIDs
[$revid] = $pageid;
900 $pageids[$pageid] = '';
901 unset( $remaining[$revid] );
905 $this->mMissingRevIDs
= array_keys( $remaining );
907 // Populate all the page information
908 $this->initFromPageIds( array_keys( $pageids ) );
910 // If the user can see deleted revisions, pull out the corresponding
911 // titles from the archive table and include them too. We ignore
912 // ar_page_id because deleted revisions are tied by title, not page_id.
913 if ( !empty( $this->mMissingRevIDs
) && $this->getUser()->isAllowed( 'deletedhistory' ) ) {
914 $remaining = array_flip( $this->mMissingRevIDs
);
915 $tables = array( 'archive' );
916 $fields = array( 'ar_rev_id', 'ar_namespace', 'ar_title' );
917 $where = array( 'ar_rev_id' => $this->mMissingRevIDs
);
919 $res = $db->select( $tables, $fields, $where, __METHOD__
);
921 foreach ( $res as $row ) {
922 $revid = intval( $row->ar_rev_id
);
923 $titles[$revid] = Title
::makeTitle( $row->ar_namespace
, $row->ar_title
);
924 unset( $remaining[$revid] );
927 $this->initFromTitles( $titles );
929 foreach ( $titles as $revid => $title ) {
930 $ns = $title->getNamespace();
931 $dbkey = $title->getDBkey();
933 // Handle converted titles
934 if ( !isset( $this->mAllPages
[$ns][$dbkey] ) &&
935 isset( $this->mConvertedTitles
[$title->getPrefixedText()] )
937 $title = Title
::newFromText( $this->mConvertedTitles
[$title->getPrefixedText()] );
938 $ns = $title->getNamespace();
939 $dbkey = $title->getDBkey();
942 if ( isset( $this->mAllPages
[$ns][$dbkey] ) ) {
943 $this->mGoodRevIDs
[$revid] = $this->mAllPages
[$ns][$dbkey];
944 $this->mDeletedRevIDs
[$revid] = $this->mAllPages
[$ns][$dbkey];
946 $remaining[$revid] = true;
950 $this->mMissingRevIDs
= array_keys( $remaining );
955 * Resolve any redirects in the result if redirect resolution was
956 * requested. This function is called repeatedly until all redirects
957 * have been resolved.
959 private function resolvePendingRedirects() {
960 if ( $this->mResolveRedirects
) {
961 $db = $this->getDB();
962 $pageFlds = $this->getPageTableFields();
964 // Repeat until all redirects have been resolved
965 // The infinite loop is prevented by keeping all known pages in $this->mAllPages
966 while ( $this->mPendingRedirectIDs
) {
967 // Resolve redirects by querying the pagelinks table, and repeat the process
968 // Create a new linkBatch object for the next pass
969 $linkBatch = $this->getRedirectTargets();
971 if ( $linkBatch->isEmpty() ) {
975 $set = $linkBatch->constructSet( 'page', $db );
976 if ( $set === false ) {
980 // Get pageIDs data from the `page` table
981 $res = $db->select( 'page', $pageFlds, $set, __METHOD__
);
983 // Hack: get the ns:titles stored in array(ns => array(titles)) format
984 $this->initFromQueryResult( $res, $linkBatch->data
, true );
990 * Get the targets of the pending redirects from the database
992 * Also creates entries in the redirect table for redirects that don't
996 private function getRedirectTargets() {
997 $lb = new LinkBatch();
998 $db = $this->getDB();
1008 ), array( 'rd_from' => array_keys( $this->mPendingRedirectIDs
) ),
1011 foreach ( $res as $row ) {
1012 $rdfrom = intval( $row->rd_from
);
1013 $from = $this->mPendingRedirectIDs
[$rdfrom]->getPrefixedText();
1014 $to = Title
::makeTitle(
1020 unset( $this->mPendingRedirectIDs
[$rdfrom] );
1021 if ( $to->isExternal() ) {
1022 $this->mInterwikiTitles
[$to->getPrefixedText()] = $to->getInterwiki();
1023 } elseif ( !isset( $this->mAllPages
[$row->rd_namespace
][$row->rd_title
] ) ) {
1024 $lb->add( $row->rd_namespace
, $row->rd_title
);
1026 $this->mRedirectTitles
[$from] = $to;
1029 if ( $this->mPendingRedirectIDs
) {
1030 // We found pages that aren't in the redirect table
1032 foreach ( $this->mPendingRedirectIDs
as $id => $title ) {
1033 $page = WikiPage
::factory( $title );
1034 $rt = $page->insertRedirect();
1036 // What the hell. Let's just ignore this
1040 $this->mRedirectTitles
[$title->getPrefixedText()] = $rt;
1041 unset( $this->mPendingRedirectIDs
[$id] );
1049 * Get the cache mode for the data generated by this module.
1050 * All PageSet users should take into account whether this returns a more-restrictive
1051 * cache mode than the using module itself. For possible return values and other
1052 * details about cache modes, see ApiMain::setCacheMode()
1054 * Public caching will only be allowed if *all* the modules that supply
1055 * data for a given request return a cache mode of public.
1057 * @param array|null $params
1061 public function getCacheMode( $params = null ) {
1062 return $this->mCacheMode
;
1066 * Given an array of title strings, convert them into Title objects.
1067 * Alternatively, an array of Title objects may be given.
1068 * This method validates access rights for the title,
1069 * and appends normalization values to the output.
1071 * @param array $titles Array of Title objects or strings
1074 private function processTitlesArray( $titles ) {
1075 $usernames = array();
1076 $linkBatch = new LinkBatch();
1078 foreach ( $titles as $title ) {
1079 if ( is_string( $title ) ) {
1080 $titleObj = Title
::newFromText( $title, $this->mDefaultNamespace
);
1085 // Handle invalid titles gracefully
1086 $this->mAllPages
[0][$title] = $this->mFakePageId
;
1087 $this->mInvalidTitles
[$this->mFakePageId
] = $title;
1088 $this->mFakePageId
--;
1089 continue; // There's nothing else we can do
1091 $unconvertedTitle = $titleObj->getPrefixedText();
1092 $titleWasConverted = false;
1093 if ( $titleObj->isExternal() ) {
1094 // This title is an interwiki link.
1095 $this->mInterwikiTitles
[$unconvertedTitle] = $titleObj->getInterwiki();
1097 // Variants checking
1099 if ( $this->mConvertTitles
&&
1100 count( $wgContLang->getVariants() ) > 1 &&
1101 !$titleObj->exists()
1103 // Language::findVariantLink will modify titleText and titleObj into
1104 // the canonical variant if possible
1105 $titleText = is_string( $title ) ?
$title : $titleObj->getPrefixedText();
1106 $wgContLang->findVariantLink( $titleText, $titleObj );
1107 $titleWasConverted = $unconvertedTitle !== $titleObj->getPrefixedText();
1110 if ( $titleObj->getNamespace() < 0 ) {
1111 // Handle Special and Media pages
1112 $titleObj = $titleObj->fixSpecialName();
1113 $this->mSpecialTitles
[$this->mFakePageId
] = $titleObj;
1114 $this->mFakePageId
--;
1117 $linkBatch->addObj( $titleObj );
1121 // Make sure we remember the original title that was
1122 // given to us. This way the caller can correlate new
1123 // titles with the originally requested when e.g. the
1124 // namespace is localized or the capitalization is
1126 if ( $titleWasConverted ) {
1127 $this->mConvertedTitles
[$unconvertedTitle] = $titleObj->getPrefixedText();
1128 // In this case the page can't be Special.
1129 if ( is_string( $title ) && $title !== $unconvertedTitle ) {
1130 $this->mNormalizedTitles
[$title] = $unconvertedTitle;
1132 } elseif ( is_string( $title ) && $title !== $titleObj->getPrefixedText() ) {
1133 $this->mNormalizedTitles
[$title] = $titleObj->getPrefixedText();
1136 // Need gender information
1137 if ( MWNamespace
::hasGenderDistinction( $titleObj->getNamespace() ) ) {
1138 $usernames[] = $titleObj->getText();
1141 // Get gender information
1142 $genderCache = GenderCache
::singleton();
1143 $genderCache->doQuery( $usernames, __METHOD__
);
1149 * Set data for a title.
1151 * This data may be extracted into an ApiResult using
1152 * self::populateGeneratorData. This should generally be limited to
1153 * data that is likely to be particularly useful to end users rather than
1154 * just being a dump of everything returned in non-generator mode.
1156 * Redirects here will *not* be followed, even if 'redirects' was
1157 * specified, since in the case of multiple redirects we can't know which
1158 * source's data to use on the target.
1160 * @param Title $title
1161 * @param array $data
1163 public function setGeneratorData( Title
$title, array $data ) {
1164 $ns = $title->getNamespace();
1165 $dbkey = $title->getDBkey();
1166 $this->mGeneratorData
[$ns][$dbkey] = $data;
1170 * Populate the generator data for all titles in the result
1172 * The page data may be inserted into an ApiResult object or into an
1173 * associative array. The $path parameter specifies the path within the
1174 * ApiResult or array to find the "pages" node.
1176 * The "pages" node itself must be an associative array mapping the page ID
1177 * or fake page ID values returned by this pageset (see
1178 * self::getAllTitlesByNamespace() and self::getSpecialTitles()) to
1179 * associative arrays of page data. Each of those subarrays will have the
1180 * data from self::setGeneratorData() merged in.
1182 * Data that was set by self::setGeneratorData() for pages not in the
1183 * "pages" node will be ignored.
1185 * @param ApiResult|array &$result
1186 * @param array $path
1187 * @return bool Whether the data fit
1189 public function populateGeneratorData( &$result, array $path = array() ) {
1190 if ( $result instanceof ApiResult
) {
1191 $data = $result->getData();
1195 foreach ( $path as $key ) {
1196 if ( !isset( $data[$key] ) ) {
1197 // Path isn't in $result, so nothing to add, so everything
1201 $data = &$data[$key];
1203 foreach ( $this->mGeneratorData
as $ns => $dbkeys ) {
1206 foreach ( $this->mSpecialTitles
as $id => $title ) {
1207 $pages[$title->getDBkey()] = $id;
1210 if ( !isset( $this->mAllPages
[$ns] ) ) {
1211 // No known titles in the whole namespace. Skip it.
1214 $pages = $this->mAllPages
[$ns];
1216 foreach ( $dbkeys as $dbkey => $genData ) {
1217 if ( !isset( $pages[$dbkey] ) ) {
1218 // Unknown title. Forget it.
1221 $pageId = $pages[$dbkey];
1222 if ( !isset( $data[$pageId] ) ) {
1223 // $pageId didn't make it into the result. Ignore it.
1227 if ( $result instanceof ApiResult
) {
1228 $path2 = array_merge( $path, array( $pageId ) );
1229 foreach ( $genData as $key => $value ) {
1230 if ( !$result->addValue( $path2, $key, $value ) ) {
1235 $data[$pageId] = array_merge( $data[$pageId], $genData );
1243 * Get the database connection (read-only)
1244 * @return DatabaseBase
1246 protected function getDB() {
1247 return $this->mDbSource
->getDB();
1251 * Returns the input array of integers with all values < 0 removed
1253 * @param array $array
1256 private static function getPositiveIntegers( $array ) {
1257 // bug 25734 API: possible issue with revids validation
1258 // It seems with a load of revision rows, MySQL gets upset
1259 // Remove any < 0 integers, as they can't be valid
1260 foreach ( $array as $i => $int ) {
1262 unset( $array[$i] );
1269 public function getAllowedParams( $flags = 0 ) {
1272 ApiBase
::PARAM_ISMULTI
=> true,
1273 ApiBase
::PARAM_HELP_MSG
=> 'api-pageset-param-titles',
1276 ApiBase
::PARAM_TYPE
=> 'integer',
1277 ApiBase
::PARAM_ISMULTI
=> true,
1278 ApiBase
::PARAM_HELP_MSG
=> 'api-pageset-param-pageids',
1281 ApiBase
::PARAM_TYPE
=> 'integer',
1282 ApiBase
::PARAM_ISMULTI
=> true,
1283 ApiBase
::PARAM_HELP_MSG
=> 'api-pageset-param-revids',
1285 'generator' => array(
1286 ApiBase
::PARAM_TYPE
=> null,
1287 ApiBase
::PARAM_VALUE_LINKS
=> array(),
1288 ApiBase
::PARAM_HELP_MSG
=> 'api-pageset-param-generator',
1290 'redirects' => array(
1291 ApiBase
::PARAM_DFLT
=> false,
1292 ApiBase
::PARAM_HELP_MSG
=> $this->mAllowGenerator
1293 ?
'api-pageset-param-redirects-generator'
1294 : 'api-pageset-param-redirects-nogenerator',
1296 'converttitles' => array(
1297 ApiBase
::PARAM_DFLT
=> false,
1298 ApiBase
::PARAM_HELP_MSG
=> array(
1299 'api-pageset-param-converttitles',
1300 new DeferredStringifier(
1301 function ( IContextSource
$context ) {
1302 return $context->getLanguage()
1303 ->commaList( LanguageConverter
::$languagesWithVariants );
1311 if ( !$this->mAllowGenerator
) {
1312 unset( $result['generator'] );
1313 } elseif ( $flags & ApiBase
::GET_VALUES_FOR_HELP
) {
1314 foreach ( $this->getGenerators() as $g ) {
1315 $result['generator'][ApiBase
::PARAM_TYPE
][] = $g;
1316 $result['generator'][ApiBase
::PARAM_VALUE_LINKS
][$g] = "Special:ApiHelp/query+$g";
1323 private static $generators = null;
1326 * Get an array of all available generators
1329 private function getGenerators() {
1330 if ( self
::$generators === null ) {
1331 $query = $this->mDbSource
;
1332 if ( !( $query instanceof ApiQuery
) ) {
1333 // If the parent container of this pageset is not ApiQuery,
1334 // we must create it to get module manager
1335 $query = $this->getMain()->getModuleManager()->getModule( 'query' );
1338 $mgr = $query->getModuleManager();
1339 foreach ( $mgr->getNamesWithClasses() as $name => $class ) {
1340 if ( is_subclass_of( $class, 'ApiQueryGeneratorBase' ) ) {
1345 self
::$generators = $gens;
1348 return self
::$generators;