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
26 use MediaWiki\MediaWikiServices
;
29 * This class contains a list of pages that the client has requested.
30 * Initially, when the client passes in titles=, pageids=, or revisions=
31 * parameter, an instance of the ApiPageSet class will normalize titles,
32 * determine if the pages/revisions exist, and prefetch any additional page
35 * When a generator is used, the result of the generator will become the input
36 * for the second instance of this class, and all subsequent actions will use
37 * the second instance for all their work.
40 * @since 1.21 derives from ApiBase instead of ApiQueryBase
42 class ApiPageSet
extends ApiBase
{
44 * Constructor flag: The new instance of ApiPageSet will ignore the 'generator=' parameter
47 const DISABLE_GENERATORS
= 1;
51 private $mResolveRedirects;
52 private $mConvertTitles;
53 private $mAllowGenerator;
55 private $mAllPages = []; // [ns][dbkey] => page_id or negative when missing
56 private $mTitles = [];
57 private $mGoodAndMissingPages = []; // [ns][dbkey] => page_id or negative when missing
58 private $mGoodPages = []; // [ns][dbkey] => page_id
59 private $mGoodTitles = [];
60 private $mMissingPages = []; // [ns][dbkey] => fake page_id
61 private $mMissingTitles = [];
62 /** @var array [fake_page_id] => [ 'title' => $title, 'invalidreason' => $reason ] */
63 private $mInvalidTitles = [];
64 private $mMissingPageIDs = [];
65 private $mRedirectTitles = [];
66 private $mSpecialTitles = [];
67 private $mAllSpecials = []; // separate from mAllPages to avoid breaking getAllTitlesByNamespace()
68 private $mNormalizedTitles = [];
69 private $mInterwikiTitles = [];
71 private $mPendingRedirectIDs = [];
72 private $mResolvedRedirectTitles = [];
73 private $mConvertedTitles = [];
74 private $mGoodRevIDs = [];
75 private $mLiveRevIDs = [];
76 private $mDeletedRevIDs = [];
77 private $mMissingRevIDs = [];
78 private $mGeneratorData = []; // [ns][dbkey] => data array
79 private $mFakePageId = -1;
80 private $mCacheMode = 'public';
81 private $mRequestedPageFields = [];
83 private $mDefaultNamespace = NS_MAIN
;
84 /** @var callable|null */
85 private $mRedirectMergePolicy;
88 * Add all items from $values into the result
89 * @param array $result Output
90 * @param array $values Values to add
91 * @param string[] $flags The names of boolean flags to mark this element
92 * @param string $name If given, name of the value
94 private static function addValues( array &$result, $values, $flags = [], $name = null ) {
95 foreach ( $values as $val ) {
96 if ( $val instanceof Title
) {
98 ApiQueryBase
::addTitleInfo( $v, $val );
99 } elseif ( $name !== null ) {
100 $v = [ $name => $val ];
104 foreach ( $flags as $flag ) {
112 * @param ApiBase $dbSource Module implementing getDB().
113 * Allows PageSet to reuse existing db connection from the shared state like ApiQuery.
114 * @param int $flags Zero or more flags like DISABLE_GENERATORS
115 * @param int $defaultNamespace The namespace to use if none is specified by a prefix.
116 * @since 1.21 accepts $flags instead of two boolean values
118 public function __construct( ApiBase
$dbSource, $flags = 0, $defaultNamespace = NS_MAIN
) {
119 parent
::__construct( $dbSource->getMain(), $dbSource->getModuleName() );
120 $this->mDbSource
= $dbSource;
121 $this->mAllowGenerator
= ( $flags & ApiPageSet
::DISABLE_GENERATORS
) == 0;
122 $this->mDefaultNamespace
= $defaultNamespace;
124 $this->mParams
= $this->extractRequestParams();
125 $this->mResolveRedirects
= $this->mParams
['redirects'];
126 $this->mConvertTitles
= $this->mParams
['converttitles'];
130 * In case execute() is not called, call this method to mark all relevant parameters as used
131 * This prevents unused parameters from being reported as warnings
133 public function executeDryRun() {
134 $this->executeInternal( true );
138 * Populate the PageSet from the request parameters.
140 public function execute() {
141 $this->executeInternal( false );
145 * Populate the PageSet from the request parameters.
146 * @param bool $isDryRun If true, instantiates generator, but only to mark
147 * relevant parameters as used
149 private function executeInternal( $isDryRun ) {
150 $generatorName = $this->mAllowGenerator ?
$this->mParams
['generator'] : null;
151 if ( isset( $generatorName ) ) {
152 $dbSource = $this->mDbSource
;
153 if ( !$dbSource instanceof ApiQuery
) {
154 // If the parent container of this pageset is not ApiQuery, we must create it to run generator
155 $dbSource = $this->getMain()->getModuleManager()->getModule( 'query' );
157 $generator = $dbSource->getModuleManager()->getModule( $generatorName, null, true );
158 if ( $generator === null ) {
159 $this->dieWithError( [ 'apierror-badgenerator-unknown', $generatorName ], 'badgenerator' );
161 if ( !$generator instanceof ApiQueryGeneratorBase
) {
162 $this->dieWithError( [ 'apierror-badgenerator-notgenerator', $generatorName ], 'badgenerator' );
164 // Create a temporary pageset to store generator's output,
165 // add any additional fields generator may need, and execute pageset to populate titles/pageids
166 $tmpPageSet = new ApiPageSet( $dbSource, ApiPageSet
::DISABLE_GENERATORS
);
167 $generator->setGeneratorMode( $tmpPageSet );
168 $this->mCacheMode
= $generator->getCacheMode( $generator->extractRequestParams() );
171 $generator->requestExtraData( $tmpPageSet );
173 $tmpPageSet->executeInternal( $isDryRun );
175 // populate this pageset with the generator output
177 $generator->executeGenerator( $this );
179 // Avoid PHP 7.1 warning of passing $this by reference
181 Hooks
::run( 'APIQueryGeneratorAfterExecute', [ &$generator, &$apiModule ] );
183 // Prevent warnings from being reported on these parameters
184 $main = $this->getMain();
185 foreach ( $generator->extractRequestParams() as $paramName => $param ) {
186 $main->markParamsUsed( $generator->encodeParamName( $paramName ) );
191 $this->resolvePendingRedirects();
194 // Only one of the titles/pageids/revids is allowed at the same time
196 if ( isset( $this->mParams
['titles'] ) ) {
197 $dataSource = 'titles';
199 if ( isset( $this->mParams
['pageids'] ) ) {
200 if ( isset( $dataSource ) ) {
203 'apierror-invalidparammix-cannotusewith',
204 $this->encodeParamName( 'pageids' ),
205 $this->encodeParamName( $dataSource )
210 $dataSource = 'pageids';
212 if ( isset( $this->mParams
['revids'] ) ) {
213 if ( isset( $dataSource ) ) {
216 'apierror-invalidparammix-cannotusewith',
217 $this->encodeParamName( 'revids' ),
218 $this->encodeParamName( $dataSource )
223 $dataSource = 'revids';
227 // Populate page information with the original user input
228 switch ( $dataSource ) {
230 $this->initFromTitles( $this->mParams
['titles'] );
233 $this->initFromPageIds( $this->mParams
['pageids'] );
236 if ( $this->mResolveRedirects
) {
237 $this->addWarning( 'apiwarn-redirectsandrevids' );
239 $this->mResolveRedirects
= false;
240 $this->initFromRevIDs( $this->mParams
['revids'] );
243 // Do nothing - some queries do not need any of the data sources.
251 * Check whether this PageSet is resolving redirects
254 public function isResolvingRedirects() {
255 return $this->mResolveRedirects
;
259 * Return the parameter name that is the source of data for this PageSet
261 * If multiple source parameters are specified (e.g. titles and pageids),
262 * one will be named arbitrarily.
264 * @return string|null
266 public function getDataSource() {
267 if ( $this->mAllowGenerator
&& isset( $this->mParams
['generator'] ) ) {
270 if ( isset( $this->mParams
['titles'] ) ) {
273 if ( isset( $this->mParams
['pageids'] ) ) {
276 if ( isset( $this->mParams
['revids'] ) ) {
284 * Request an additional field from the page table.
285 * Must be called before execute()
286 * @param string $fieldName Field name
288 public function requestField( $fieldName ) {
289 $this->mRequestedPageFields
[$fieldName] = null;
293 * Get the value of a custom field previously requested through
295 * @param string $fieldName Field name
296 * @return mixed Field value
298 public function getCustomField( $fieldName ) {
299 return $this->mRequestedPageFields
[$fieldName];
303 * Get the fields that have to be queried from the page table:
304 * the ones requested through requestField() and a few basic ones
306 * @return array Array of field names
308 public function getPageTableFields() {
309 // Ensure we get minimum required fields
310 // DON'T change this order
312 'page_namespace' => null,
313 'page_title' => null,
317 if ( $this->mResolveRedirects
) {
318 $pageFlds['page_is_redirect'] = null;
321 if ( $this->getConfig()->get( 'ContentHandlerUseDB' ) ) {
322 $pageFlds['page_content_model'] = null;
325 if ( $this->getConfig()->get( 'PageLanguageUseDB' ) ) {
326 $pageFlds['page_lang'] = null;
329 foreach ( LinkCache
::getSelectFields() as $field ) {
330 $pageFlds[$field] = null;
333 $pageFlds = array_merge( $pageFlds, $this->mRequestedPageFields
);
335 return array_keys( $pageFlds );
339 * Returns an array [ns][dbkey] => page_id for all requested titles.
340 * page_id is a unique negative number in case title was not found.
341 * Invalid titles will also have negative page IDs and will be in namespace 0
344 public function getAllTitlesByNamespace() {
345 return $this->mAllPages
;
349 * All Title objects provided.
352 public function getTitles() {
353 return $this->mTitles
;
357 * Returns the number of unique pages (not revisions) in the set.
360 public function getTitleCount() {
361 return count( $this->mTitles
);
365 * Returns an array [ns][dbkey] => page_id for all good titles.
368 public function getGoodTitlesByNamespace() {
369 return $this->mGoodPages
;
373 * Title objects that were found in the database.
374 * @return Title[] Array page_id (int) => Title (obj)
376 public function getGoodTitles() {
377 return $this->mGoodTitles
;
381 * Returns the number of found unique pages (not revisions) in the set.
384 public function getGoodTitleCount() {
385 return count( $this->mGoodTitles
);
389 * Returns an array [ns][dbkey] => fake_page_id for all missing titles.
390 * fake_page_id is a unique negative number.
393 public function getMissingTitlesByNamespace() {
394 return $this->mMissingPages
;
398 * Title objects that were NOT found in the database.
399 * The array's index will be negative for each item
402 public function getMissingTitles() {
403 return $this->mMissingTitles
;
407 * Returns an array [ns][dbkey] => page_id for all good and missing titles.
410 public function getGoodAndMissingTitlesByNamespace() {
411 return $this->mGoodAndMissingPages
;
415 * Title objects for good and missing titles.
418 public function getGoodAndMissingTitles() {
419 return $this->mGoodTitles +
$this->mMissingTitles
;
423 * Titles that were deemed invalid by Title::newFromText()
424 * The array's index will be unique and negative for each item
425 * @deprecated since 1.26, use self::getInvalidTitlesAndReasons()
426 * @return string[] Array of strings (not Title objects)
428 public function getInvalidTitles() {
429 wfDeprecated( __METHOD__
, '1.26' );
430 return array_map( function ( $t ) {
432 }, $this->mInvalidTitles
);
436 * Titles that were deemed invalid by Title::newFromText()
437 * The array's index will be unique and negative for each item
438 * @return array[] Array of arrays with 'title' and 'invalidreason' properties
440 public function getInvalidTitlesAndReasons() {
441 return $this->mInvalidTitles
;
445 * Page IDs that were not found in the database
446 * @return array Array of page IDs
448 public function getMissingPageIDs() {
449 return $this->mMissingPageIDs
;
453 * Get a list of redirect resolutions - maps a title to its redirect
454 * target, as an array of output-ready arrays
457 public function getRedirectTitles() {
458 return $this->mRedirectTitles
;
462 * Get a list of redirect resolutions - maps a title to its redirect
463 * target. Includes generator data for redirect source when available.
464 * @param ApiResult $result
465 * @return array Array of prefixed_title (string) => Title object
468 public function getRedirectTitlesAsResult( $result = null ) {
470 foreach ( $this->getRedirectTitles() as $titleStrFrom => $titleTo ) {
472 'from' => strval( $titleStrFrom ),
473 'to' => $titleTo->getPrefixedText(),
475 if ( $titleTo->hasFragment() ) {
476 $r['tofragment'] = $titleTo->getFragment();
478 if ( $titleTo->isExternal() ) {
479 $r['tointerwiki'] = $titleTo->getInterwiki();
481 if ( isset( $this->mResolvedRedirectTitles
[$titleStrFrom] ) ) {
482 $titleFrom = $this->mResolvedRedirectTitles
[$titleStrFrom];
483 $ns = $titleFrom->getNamespace();
484 $dbkey = $titleFrom->getDBkey();
485 if ( isset( $this->mGeneratorData
[$ns][$dbkey] ) ) {
486 $r = array_merge( $this->mGeneratorData
[$ns][$dbkey], $r );
492 if ( !empty( $values ) && $result ) {
493 ApiResult
::setIndexedTagName( $values, 'r' );
500 * Get a list of title normalizations - maps a title to its normalized
502 * @return array Array of raw_prefixed_title (string) => prefixed_title (string)
504 public function getNormalizedTitles() {
505 return $this->mNormalizedTitles
;
509 * Get a list of title normalizations - maps a title to its normalized
510 * version in the form of result array.
511 * @param ApiResult $result
512 * @return array Array of raw_prefixed_title (string) => prefixed_title (string)
515 public function getNormalizedTitlesAsResult( $result = null ) {
519 foreach ( $this->getNormalizedTitles() as $rawTitleStr => $titleStr ) {
520 $encode = ( $wgContLang->normalize( $rawTitleStr ) !== $rawTitleStr );
522 'fromencoded' => $encode,
523 'from' => $encode ?
rawurlencode( $rawTitleStr ) : $rawTitleStr,
527 if ( !empty( $values ) && $result ) {
528 ApiResult
::setIndexedTagName( $values, 'n' );
535 * Get a list of title conversions - maps a title to its converted
537 * @return array Array of raw_prefixed_title (string) => prefixed_title (string)
539 public function getConvertedTitles() {
540 return $this->mConvertedTitles
;
544 * Get a list of title conversions - maps a title to its converted
545 * version as a result array.
546 * @param ApiResult $result
547 * @return array Array of (from, to) strings
550 public function getConvertedTitlesAsResult( $result = null ) {
552 foreach ( $this->getConvertedTitles() as $rawTitleStr => $titleStr ) {
554 'from' => $rawTitleStr,
558 if ( !empty( $values ) && $result ) {
559 ApiResult
::setIndexedTagName( $values, 'c' );
566 * Get a list of interwiki titles - maps a title to its interwiki
568 * @return array Array of raw_prefixed_title (string) => interwiki_prefix (string)
570 public function getInterwikiTitles() {
571 return $this->mInterwikiTitles
;
575 * Get a list of interwiki titles - maps a title to its interwiki
577 * @param ApiResult $result
579 * @return array Array of raw_prefixed_title (string) => interwiki_prefix (string)
582 public function getInterwikiTitlesAsResult( $result = null, $iwUrl = false ) {
584 foreach ( $this->getInterwikiTitles() as $rawTitleStr => $interwikiStr ) {
586 'title' => $rawTitleStr,
587 'iw' => $interwikiStr,
590 $title = Title
::newFromText( $rawTitleStr );
591 $item['url'] = $title->getFullURL( '', false, PROTO_CURRENT
);
595 if ( !empty( $values ) && $result ) {
596 ApiResult
::setIndexedTagName( $values, 'i' );
603 * Get an array of invalid/special/missing titles.
605 * @param array $invalidChecks List of types of invalid titles to include.
606 * Recognized values are:
607 * - invalidTitles: Titles and reasons from $this->getInvalidTitlesAndReasons()
608 * - special: Titles from $this->getSpecialTitles()
609 * - missingIds: ids from $this->getMissingPageIDs()
610 * - missingRevIds: ids from $this->getMissingRevisionIDs()
611 * - missingTitles: Titles from $this->getMissingTitles()
612 * - interwikiTitles: Titles from $this->getInterwikiTitlesAsResult()
613 * @return array Array suitable for inclusion in the response
616 public function getInvalidTitlesAndRevisions( $invalidChecks = [ 'invalidTitles',
617 'special', 'missingIds', 'missingRevIds', 'missingTitles', 'interwikiTitles' ]
620 if ( in_array( 'invalidTitles', $invalidChecks ) ) {
621 self
::addValues( $result, $this->getInvalidTitlesAndReasons(), [ 'invalid' ] );
623 if ( in_array( 'special', $invalidChecks ) ) {
626 foreach ( $this->getSpecialTitles() as $title ) {
627 if ( $title->isKnown() ) {
633 self
::addValues( $result, $unknown, [ 'special', 'missing' ] );
634 self
::addValues( $result, $known, [ 'special' ] );
636 if ( in_array( 'missingIds', $invalidChecks ) ) {
637 self
::addValues( $result, $this->getMissingPageIDs(), [ 'missing' ], 'pageid' );
639 if ( in_array( 'missingRevIds', $invalidChecks ) ) {
640 self
::addValues( $result, $this->getMissingRevisionIDs(), [ 'missing' ], 'revid' );
642 if ( in_array( 'missingTitles', $invalidChecks ) ) {
645 foreach ( $this->getMissingTitles() as $title ) {
646 if ( $title->isKnown() ) {
652 self
::addValues( $result, $unknown, [ 'missing' ] );
653 self
::addValues( $result, $known, [ 'missing', 'known' ] );
655 if ( in_array( 'interwikiTitles', $invalidChecks ) ) {
656 self
::addValues( $result, $this->getInterwikiTitlesAsResult() );
663 * Get the list of valid revision IDs (requested with the revids= parameter)
664 * @return array Array of revID (int) => pageID (int)
666 public function getRevisionIDs() {
667 return $this->mGoodRevIDs
;
671 * Get the list of non-deleted revision IDs (requested with the revids= parameter)
672 * @return array Array of revID (int) => pageID (int)
674 public function getLiveRevisionIDs() {
675 return $this->mLiveRevIDs
;
679 * Get the list of revision IDs that were associated with deleted titles.
680 * @return array Array of revID (int) => pageID (int)
682 public function getDeletedRevisionIDs() {
683 return $this->mDeletedRevIDs
;
687 * Revision IDs that were not found in the database
688 * @return array Array of revision IDs
690 public function getMissingRevisionIDs() {
691 return $this->mMissingRevIDs
;
695 * Revision IDs that were not found in the database as result array.
696 * @param ApiResult $result
697 * @return array Array of revision IDs
700 public function getMissingRevisionIDsAsResult( $result = null ) {
702 foreach ( $this->getMissingRevisionIDs() as $revid ) {
707 if ( !empty( $values ) && $result ) {
708 ApiResult
::setIndexedTagName( $values, 'rev' );
715 * Get the list of titles with negative namespace
718 public function getSpecialTitles() {
719 return $this->mSpecialTitles
;
723 * Returns the number of revisions (requested with revids= parameter).
724 * @return int Number of revisions.
726 public function getRevisionCount() {
727 return count( $this->getRevisionIDs() );
731 * Populate this PageSet from a list of Titles
732 * @param array $titles Array of Title objects
734 public function populateFromTitles( $titles ) {
735 $this->initFromTitles( $titles );
739 * Populate this PageSet from a list of page IDs
740 * @param array $pageIDs Array of page IDs
742 public function populateFromPageIDs( $pageIDs ) {
743 $this->initFromPageIds( $pageIDs );
747 * Populate this PageSet from a rowset returned from the database
749 * Note that the query result must include the columns returned by
750 * $this->getPageTableFields().
752 * @param IDatabase $db
753 * @param ResultWrapper $queryResult Query result object
755 public function populateFromQueryResult( $db, $queryResult ) {
756 $this->initFromQueryResult( $queryResult );
760 * Populate this PageSet from a list of revision IDs
761 * @param array $revIDs Array of revision IDs
763 public function populateFromRevisionIDs( $revIDs ) {
764 $this->initFromRevIDs( $revIDs );
768 * Extract all requested fields from the row received from the database
769 * @param stdClass $row Result row
771 public function processDbRow( $row ) {
772 // Store Title object in various data structures
773 $title = Title
::newFromRow( $row );
775 LinkCache
::singleton()->addGoodLinkObjFromRow( $title, $row );
777 $pageId = intval( $row->page_id
);
778 $this->mAllPages
[$row->page_namespace
][$row->page_title
] = $pageId;
779 $this->mTitles
[] = $title;
781 if ( $this->mResolveRedirects
&& $row->page_is_redirect
== '1' ) {
782 $this->mPendingRedirectIDs
[$pageId] = $title;
784 $this->mGoodPages
[$row->page_namespace
][$row->page_title
] = $pageId;
785 $this->mGoodAndMissingPages
[$row->page_namespace
][$row->page_title
] = $pageId;
786 $this->mGoodTitles
[$pageId] = $title;
789 foreach ( $this->mRequestedPageFields
as $fieldName => &$fieldValues ) {
790 $fieldValues[$pageId] = $row->$fieldName;
795 * This method populates internal variables with page information
796 * based on the given array of title strings.
799 * #1 For each title, get data from `page` table
800 * #2 If page was not found in the DB, store it as missing
802 * Additionally, when resolving redirects:
803 * #3 If no more redirects left, stop.
804 * #4 For each redirect, get its target from the `redirect` table.
805 * #5 Substitute the original LinkBatch object with the new list
806 * #6 Repeat from step #1
808 * @param array $titles Array of Title objects or strings
810 private function initFromTitles( $titles ) {
811 // Get validated and normalized title objects
812 $linkBatch = $this->processTitlesArray( $titles );
813 if ( $linkBatch->isEmpty() ) {
817 $db = $this->getDB();
818 $set = $linkBatch->constructSet( 'page', $db );
820 // Get pageIDs data from the `page` table
821 $res = $db->select( 'page', $this->getPageTableFields(), $set,
824 // Hack: get the ns:titles stored in [ ns => [ titles ] ] format
825 $this->initFromQueryResult( $res, $linkBatch->data
, true ); // process Titles
827 // Resolve any found redirects
828 $this->resolvePendingRedirects();
832 * Does the same as initFromTitles(), but is based on page IDs instead
833 * @param array $pageids Array of page IDs
835 private function initFromPageIds( $pageids ) {
840 $pageids = array_map( 'intval', $pageids ); // paranoia
841 $remaining = array_flip( $pageids );
843 $pageids = self
::getPositiveIntegers( $pageids );
846 if ( !empty( $pageids ) ) {
848 'page_id' => $pageids
850 $db = $this->getDB();
852 // Get pageIDs data from the `page` table
853 $res = $db->select( 'page', $this->getPageTableFields(), $set,
857 $this->initFromQueryResult( $res, $remaining, false ); // process PageIDs
859 // Resolve any found redirects
860 $this->resolvePendingRedirects();
864 * Iterate through the result of the query on 'page' table,
865 * and for each row create and store title object and save any extra fields requested.
866 * @param ResultWrapper $res DB Query result
867 * @param array $remaining Array of either pageID or ns/title elements (optional).
868 * If given, any missing items will go to $mMissingPageIDs and $mMissingTitles
869 * @param bool $processTitles Must be provided together with $remaining.
870 * If true, treat $remaining as an array of [ns][title]
871 * If false, treat it as an array of [pageIDs]
873 private function initFromQueryResult( $res, &$remaining = null, $processTitles = null ) {
874 if ( !is_null( $remaining ) && is_null( $processTitles ) ) {
875 ApiBase
::dieDebug( __METHOD__
, 'Missing $processTitles parameter when $remaining is provided' );
880 foreach ( $res as $row ) {
881 $pageId = intval( $row->page_id
);
883 // Remove found page from the list of remaining items
884 if ( isset( $remaining ) ) {
885 if ( $processTitles ) {
886 unset( $remaining[$row->page_namespace
][$row->page_title
] );
888 unset( $remaining[$pageId] );
892 // Store any extra fields requested by modules
893 $this->processDbRow( $row );
895 // Need gender information
896 if ( MWNamespace
::hasGenderDistinction( $row->page_namespace
) ) {
897 $usernames[] = $row->page_title
;
902 if ( isset( $remaining ) ) {
903 // Any items left in the $remaining list are added as missing
904 if ( $processTitles ) {
905 // The remaining titles in $remaining are non-existent pages
906 $linkCache = LinkCache
::singleton();
907 foreach ( $remaining as $ns => $dbkeys ) {
908 foreach ( array_keys( $dbkeys ) as $dbkey ) {
909 $title = Title
::makeTitle( $ns, $dbkey );
910 $linkCache->addBadLinkObj( $title );
911 $this->mAllPages
[$ns][$dbkey] = $this->mFakePageId
;
912 $this->mMissingPages
[$ns][$dbkey] = $this->mFakePageId
;
913 $this->mGoodAndMissingPages
[$ns][$dbkey] = $this->mFakePageId
;
914 $this->mMissingTitles
[$this->mFakePageId
] = $title;
915 $this->mFakePageId
--;
916 $this->mTitles
[] = $title;
918 // need gender information
919 if ( MWNamespace
::hasGenderDistinction( $ns ) ) {
920 $usernames[] = $dbkey;
925 // The remaining pageids do not exist
926 if ( !$this->mMissingPageIDs
) {
927 $this->mMissingPageIDs
= array_keys( $remaining );
929 $this->mMissingPageIDs
= array_merge( $this->mMissingPageIDs
, array_keys( $remaining ) );
934 // Get gender information
935 $genderCache = MediaWikiServices
::getInstance()->getGenderCache();
936 $genderCache->doQuery( $usernames, __METHOD__
);
940 * Does the same as initFromTitles(), but is based on revision IDs
942 * @param array $revids Array of revision IDs
944 private function initFromRevIDs( $revids ) {
949 $revids = array_map( 'intval', $revids ); // paranoia
950 $db = $this->getDB();
952 $remaining = array_flip( $revids );
954 $revids = self
::getPositiveIntegers( $revids );
956 if ( !empty( $revids ) ) {
957 $tables = [ 'revision', 'page' ];
958 $fields = [ 'rev_id', 'rev_page' ];
959 $where = [ 'rev_id' => $revids, 'rev_page = page_id' ];
961 // Get pageIDs data from the `page` table
962 $res = $db->select( $tables, $fields, $where, __METHOD__
);
963 foreach ( $res as $row ) {
964 $revid = intval( $row->rev_id
);
965 $pageid = intval( $row->rev_page
);
966 $this->mGoodRevIDs
[$revid] = $pageid;
967 $this->mLiveRevIDs
[$revid] = $pageid;
968 $pageids[$pageid] = '';
969 unset( $remaining[$revid] );
973 $this->mMissingRevIDs
= array_keys( $remaining );
975 // Populate all the page information
976 $this->initFromPageIds( array_keys( $pageids ) );
978 // If the user can see deleted revisions, pull out the corresponding
979 // titles from the archive table and include them too. We ignore
980 // ar_page_id because deleted revisions are tied by title, not page_id.
981 if ( !empty( $this->mMissingRevIDs
) && $this->getUser()->isAllowed( 'deletedhistory' ) ) {
982 $remaining = array_flip( $this->mMissingRevIDs
);
983 $tables = [ 'archive' ];
984 $fields = [ 'ar_rev_id', 'ar_namespace', 'ar_title' ];
985 $where = [ 'ar_rev_id' => $this->mMissingRevIDs
];
987 $res = $db->select( $tables, $fields, $where, __METHOD__
);
989 foreach ( $res as $row ) {
990 $revid = intval( $row->ar_rev_id
);
991 $titles[$revid] = Title
::makeTitle( $row->ar_namespace
, $row->ar_title
);
992 unset( $remaining[$revid] );
995 $this->initFromTitles( $titles );
997 foreach ( $titles as $revid => $title ) {
998 $ns = $title->getNamespace();
999 $dbkey = $title->getDBkey();
1001 // Handle converted titles
1002 if ( !isset( $this->mAllPages
[$ns][$dbkey] ) &&
1003 isset( $this->mConvertedTitles
[$title->getPrefixedText()] )
1005 $title = Title
::newFromText( $this->mConvertedTitles
[$title->getPrefixedText()] );
1006 $ns = $title->getNamespace();
1007 $dbkey = $title->getDBkey();
1010 if ( isset( $this->mAllPages
[$ns][$dbkey] ) ) {
1011 $this->mGoodRevIDs
[$revid] = $this->mAllPages
[$ns][$dbkey];
1012 $this->mDeletedRevIDs
[$revid] = $this->mAllPages
[$ns][$dbkey];
1014 $remaining[$revid] = true;
1018 $this->mMissingRevIDs
= array_keys( $remaining );
1023 * Resolve any redirects in the result if redirect resolution was
1024 * requested. This function is called repeatedly until all redirects
1025 * have been resolved.
1027 private function resolvePendingRedirects() {
1028 if ( $this->mResolveRedirects
) {
1029 $db = $this->getDB();
1030 $pageFlds = $this->getPageTableFields();
1032 // Repeat until all redirects have been resolved
1033 // The infinite loop is prevented by keeping all known pages in $this->mAllPages
1034 while ( $this->mPendingRedirectIDs
) {
1035 // Resolve redirects by querying the pagelinks table, and repeat the process
1036 // Create a new linkBatch object for the next pass
1037 $linkBatch = $this->getRedirectTargets();
1039 if ( $linkBatch->isEmpty() ) {
1043 $set = $linkBatch->constructSet( 'page', $db );
1044 if ( $set === false ) {
1048 // Get pageIDs data from the `page` table
1049 $res = $db->select( 'page', $pageFlds, $set, __METHOD__
);
1051 // Hack: get the ns:titles stored in [ns => array(titles)] format
1052 $this->initFromQueryResult( $res, $linkBatch->data
, true );
1058 * Get the targets of the pending redirects from the database
1060 * Also creates entries in the redirect table for redirects that don't
1064 private function getRedirectTargets() {
1065 $titlesToResolve = [];
1066 $db = $this->getDB();
1076 ], [ 'rd_from' => array_keys( $this->mPendingRedirectIDs
) ],
1079 foreach ( $res as $row ) {
1080 $rdfrom = intval( $row->rd_from
);
1081 $from = $this->mPendingRedirectIDs
[$rdfrom]->getPrefixedText();
1082 $to = Title
::makeTitle(
1088 $this->mResolvedRedirectTitles
[$from] = $this->mPendingRedirectIDs
[$rdfrom];
1089 unset( $this->mPendingRedirectIDs
[$rdfrom] );
1090 if ( $to->isExternal() ) {
1091 $this->mInterwikiTitles
[$to->getPrefixedText()] = $to->getInterwiki();
1092 } elseif ( !isset( $this->mAllPages
[$to->getNamespace()][$to->getDBkey()] ) ) {
1093 $titlesToResolve[] = $to;
1095 $this->mRedirectTitles
[$from] = $to;
1098 if ( $this->mPendingRedirectIDs
) {
1099 // We found pages that aren't in the redirect table
1101 foreach ( $this->mPendingRedirectIDs
as $id => $title ) {
1102 $page = WikiPage
::factory( $title );
1103 $rt = $page->insertRedirect();
1105 // What the hell. Let's just ignore this
1108 if ( $rt->isExternal() ) {
1109 $this->mInterwikiTitles
[$rt->getPrefixedText()] = $rt->getInterwiki();
1110 } elseif ( !isset( $this->mAllPages
[$rt->getNamespace()][$rt->getDBkey()] ) ) {
1111 $titlesToResolve[] = $rt;
1113 $from = $title->getPrefixedText();
1114 $this->mResolvedRedirectTitles
[$from] = $title;
1115 $this->mRedirectTitles
[$from] = $rt;
1116 unset( $this->mPendingRedirectIDs
[$id] );
1120 return $this->processTitlesArray( $titlesToResolve );
1124 * Get the cache mode for the data generated by this module.
1125 * All PageSet users should take into account whether this returns a more-restrictive
1126 * cache mode than the using module itself. For possible return values and other
1127 * details about cache modes, see ApiMain::setCacheMode()
1129 * Public caching will only be allowed if *all* the modules that supply
1130 * data for a given request return a cache mode of public.
1132 * @param array|null $params
1136 public function getCacheMode( $params = null ) {
1137 return $this->mCacheMode
;
1141 * Given an array of title strings, convert them into Title objects.
1142 * Alternatively, an array of Title objects may be given.
1143 * This method validates access rights for the title,
1144 * and appends normalization values to the output.
1146 * @param array $titles Array of Title objects or strings
1149 private function processTitlesArray( $titles ) {
1151 $linkBatch = new LinkBatch();
1153 foreach ( $titles as $title ) {
1154 if ( is_string( $title ) ) {
1156 $titleObj = Title
::newFromTextThrow( $title, $this->mDefaultNamespace
);
1157 } catch ( MalformedTitleException
$ex ) {
1158 // Handle invalid titles gracefully
1159 if ( !isset( $this->mAllPages
[0][$title] ) ) {
1160 $this->mAllPages
[0][$title] = $this->mFakePageId
;
1161 $this->mInvalidTitles
[$this->mFakePageId
] = [
1163 'invalidreason' => $this->getErrorFormatter()->formatException( $ex, [ 'bc' => true ] ),
1165 $this->mFakePageId
--;
1167 continue; // There's nothing else we can do
1172 $unconvertedTitle = $titleObj->getPrefixedText();
1173 $titleWasConverted = false;
1174 if ( $titleObj->isExternal() ) {
1175 // This title is an interwiki link.
1176 $this->mInterwikiTitles
[$unconvertedTitle] = $titleObj->getInterwiki();
1178 // Variants checking
1180 if ( $this->mConvertTitles
&&
1181 count( $wgContLang->getVariants() ) > 1 &&
1182 !$titleObj->exists()
1184 // Language::findVariantLink will modify titleText and titleObj into
1185 // the canonical variant if possible
1186 $titleText = is_string( $title ) ?
$title : $titleObj->getPrefixedText();
1187 $wgContLang->findVariantLink( $titleText, $titleObj );
1188 $titleWasConverted = $unconvertedTitle !== $titleObj->getPrefixedText();
1191 if ( $titleObj->getNamespace() < 0 ) {
1192 // Handle Special and Media pages
1193 $titleObj = $titleObj->fixSpecialName();
1194 $ns = $titleObj->getNamespace();
1195 $dbkey = $titleObj->getDBkey();
1196 if ( !isset( $this->mAllSpecials
[$ns][$dbkey] ) ) {
1197 $this->mAllSpecials
[$ns][$dbkey] = $this->mFakePageId
;
1198 $this->mSpecialTitles
[$this->mFakePageId
] = $titleObj;
1199 $this->mFakePageId
--;
1203 $linkBatch->addObj( $titleObj );
1207 // Make sure we remember the original title that was
1208 // given to us. This way the caller can correlate new
1209 // titles with the originally requested when e.g. the
1210 // namespace is localized or the capitalization is
1212 if ( $titleWasConverted ) {
1213 $this->mConvertedTitles
[$unconvertedTitle] = $titleObj->getPrefixedText();
1214 // In this case the page can't be Special.
1215 if ( is_string( $title ) && $title !== $unconvertedTitle ) {
1216 $this->mNormalizedTitles
[$title] = $unconvertedTitle;
1218 } elseif ( is_string( $title ) && $title !== $titleObj->getPrefixedText() ) {
1219 $this->mNormalizedTitles
[$title] = $titleObj->getPrefixedText();
1222 // Need gender information
1223 if ( MWNamespace
::hasGenderDistinction( $titleObj->getNamespace() ) ) {
1224 $usernames[] = $titleObj->getText();
1227 // Get gender information
1228 $genderCache = MediaWikiServices
::getInstance()->getGenderCache();
1229 $genderCache->doQuery( $usernames, __METHOD__
);
1235 * Set data for a title.
1237 * This data may be extracted into an ApiResult using
1238 * self::populateGeneratorData. This should generally be limited to
1239 * data that is likely to be particularly useful to end users rather than
1240 * just being a dump of everything returned in non-generator mode.
1242 * Redirects here will *not* be followed, even if 'redirects' was
1243 * specified, since in the case of multiple redirects we can't know which
1244 * source's data to use on the target.
1246 * @param Title $title
1247 * @param array $data
1249 public function setGeneratorData( Title
$title, array $data ) {
1250 $ns = $title->getNamespace();
1251 $dbkey = $title->getDBkey();
1252 $this->mGeneratorData
[$ns][$dbkey] = $data;
1256 * Controls how generator data about a redirect source is merged into
1257 * the generator data for the redirect target. When not set no data
1258 * is merged. Note that if multiple titles redirect to the same target
1259 * the order of operations is undefined.
1261 * Example to include generated data from redirect in target, prefering
1262 * the data generated for the destination when there is a collision:
1264 * $pageSet->setRedirectMergePolicy( function( array $current, array $new ) {
1265 * return $current + $new;
1269 * @param callable|null $callable Recieves two array arguments, first the
1270 * generator data for the redirect target and second the generator data
1271 * for the redirect source. Returns the resulting generator data to use
1272 * for the redirect target.
1274 public function setRedirectMergePolicy( $callable ) {
1275 $this->mRedirectMergePolicy
= $callable;
1279 * Populate the generator data for all titles in the result
1281 * The page data may be inserted into an ApiResult object or into an
1282 * associative array. The $path parameter specifies the path within the
1283 * ApiResult or array to find the "pages" node.
1285 * The "pages" node itself must be an associative array mapping the page ID
1286 * or fake page ID values returned by this pageset (see
1287 * self::getAllTitlesByNamespace() and self::getSpecialTitles()) to
1288 * associative arrays of page data. Each of those subarrays will have the
1289 * data from self::setGeneratorData() merged in.
1291 * Data that was set by self::setGeneratorData() for pages not in the
1292 * "pages" node will be ignored.
1294 * @param ApiResult|array &$result
1295 * @param array $path
1296 * @return bool Whether the data fit
1298 public function populateGeneratorData( &$result, array $path = [] ) {
1299 if ( $result instanceof ApiResult
) {
1300 $data = $result->getResultData( $path );
1301 if ( $data === null ) {
1306 foreach ( $path as $key ) {
1307 if ( !isset( $data[$key] ) ) {
1308 // Path isn't in $result, so nothing to add, so everything
1312 $data = &$data[$key];
1315 foreach ( $this->mGeneratorData
as $ns => $dbkeys ) {
1318 foreach ( $this->mSpecialTitles
as $id => $title ) {
1319 $pages[$title->getDBkey()] = $id;
1322 if ( !isset( $this->mAllPages
[$ns] ) ) {
1323 // No known titles in the whole namespace. Skip it.
1326 $pages = $this->mAllPages
[$ns];
1328 foreach ( $dbkeys as $dbkey => $genData ) {
1329 if ( !isset( $pages[$dbkey] ) ) {
1330 // Unknown title. Forget it.
1333 $pageId = $pages[$dbkey];
1334 if ( !isset( $data[$pageId] ) ) {
1335 // $pageId didn't make it into the result. Ignore it.
1339 if ( $result instanceof ApiResult
) {
1340 $path2 = array_merge( $path, [ $pageId ] );
1341 foreach ( $genData as $key => $value ) {
1342 if ( !$result->addValue( $path2, $key, $value ) ) {
1347 $data[$pageId] = array_merge( $data[$pageId], $genData );
1352 // Merge data generated about redirect titles into the redirect destination
1353 if ( $this->mRedirectMergePolicy
) {
1354 foreach ( $this->mResolvedRedirectTitles
as $titleFrom ) {
1356 while ( isset( $this->mRedirectTitles
[$dest->getPrefixedText()] ) ) {
1357 $dest = $this->mRedirectTitles
[$dest->getPrefixedText()];
1359 $fromNs = $titleFrom->getNamespace();
1360 $fromDBkey = $titleFrom->getDBkey();
1361 $toPageId = $dest->getArticleID();
1362 if ( isset( $data[$toPageId] ) &&
1363 isset( $this->mGeneratorData
[$fromNs][$fromDBkey] )
1365 // It is necesary to set both $data and add to $result, if an ApiResult,
1366 // to ensure multiple redirects to the same destination are all merged.
1367 $data[$toPageId] = call_user_func(
1368 $this->mRedirectMergePolicy
,
1370 $this->mGeneratorData
[$fromNs][$fromDBkey]
1372 if ( $result instanceof ApiResult
) {
1373 if ( !$result->addValue( $path, $toPageId, $data[$toPageId], ApiResult
::OVERRIDE
) ) {
1385 * Get the database connection (read-only)
1388 protected function getDB() {
1389 return $this->mDbSource
->getDB();
1393 * Returns the input array of integers with all values < 0 removed
1395 * @param array $array
1398 private static function getPositiveIntegers( $array ) {
1399 // T27734 API: possible issue with revids validation
1400 // It seems with a load of revision rows, MySQL gets upset
1401 // Remove any < 0 integers, as they can't be valid
1402 foreach ( $array as $i => $int ) {
1404 unset( $array[$i] );
1411 public function getAllowedParams( $flags = 0 ) {
1414 ApiBase
::PARAM_ISMULTI
=> true,
1415 ApiBase
::PARAM_HELP_MSG
=> 'api-pageset-param-titles',
1418 ApiBase
::PARAM_TYPE
=> 'integer',
1419 ApiBase
::PARAM_ISMULTI
=> true,
1420 ApiBase
::PARAM_HELP_MSG
=> 'api-pageset-param-pageids',
1423 ApiBase
::PARAM_TYPE
=> 'integer',
1424 ApiBase
::PARAM_ISMULTI
=> true,
1425 ApiBase
::PARAM_HELP_MSG
=> 'api-pageset-param-revids',
1428 ApiBase
::PARAM_TYPE
=> null,
1429 ApiBase
::PARAM_HELP_MSG
=> 'api-pageset-param-generator',
1430 ApiBase
::PARAM_SUBMODULE_PARAM_PREFIX
=> 'g',
1433 ApiBase
::PARAM_DFLT
=> false,
1434 ApiBase
::PARAM_HELP_MSG
=> $this->mAllowGenerator
1435 ?
'api-pageset-param-redirects-generator'
1436 : 'api-pageset-param-redirects-nogenerator',
1438 'converttitles' => [
1439 ApiBase
::PARAM_DFLT
=> false,
1440 ApiBase
::PARAM_HELP_MSG
=> [
1441 'api-pageset-param-converttitles',
1442 [ Message
::listParam( LanguageConverter
::$languagesWithVariants, 'text' ) ],
1447 if ( !$this->mAllowGenerator
) {
1448 unset( $result['generator'] );
1449 } elseif ( $flags & ApiBase
::GET_VALUES_FOR_HELP
) {
1450 $result['generator'][ApiBase
::PARAM_TYPE
] = 'submodule';
1451 $result['generator'][ApiBase
::PARAM_SUBMODULE_MAP
] = $this->getGenerators();
1457 protected function handleParamNormalization( $paramName, $value, $rawValue ) {
1458 parent
::handleParamNormalization( $paramName, $value, $rawValue );
1460 if ( $paramName === 'titles' ) {
1461 // For the 'titles' parameter, we want to split it like ApiBase would
1462 // and add any changed titles to $this->mNormalizedTitles
1463 $value = $this->explodeMultiValue( $value, self
::LIMIT_SML2 +
1 );
1464 $l = count( $value );
1465 $rawValue = $this->explodeMultiValue( $rawValue, $l );
1466 for ( $i = 0; $i < $l; $i++
) {
1467 if ( $value[$i] !== $rawValue[$i] ) {
1468 $this->mNormalizedTitles
[$rawValue[$i]] = $value[$i];
1474 private static $generators = null;
1477 * Get an array of all available generators
1480 private function getGenerators() {
1481 if ( self
::$generators === null ) {
1482 $query = $this->mDbSource
;
1483 if ( !( $query instanceof ApiQuery
) ) {
1484 // If the parent container of this pageset is not ApiQuery,
1485 // we must create it to get module manager
1486 $query = $this->getMain()->getModuleManager()->getModule( 'query' );
1489 $prefix = $query->getModulePath() . '+';
1490 $mgr = $query->getModuleManager();
1491 foreach ( $mgr->getNamesWithClasses() as $name => $class ) {
1492 if ( is_subclass_of( $class, 'ApiQueryGeneratorBase' ) ) {
1493 $gens[$name] = $prefix . $name;
1497 self
::$generators = $gens;
1500 return self
::$generators;