3 * Copyright © 2006, 2013 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
22 use MediaWiki\MediaWikiServices
;
23 use Wikimedia\Rdbms\IResultWrapper
;
24 use Wikimedia\Rdbms\IDatabase
;
27 * This class contains a list of pages that the client has requested.
28 * Initially, when the client passes in titles=, pageids=, or revisions=
29 * parameter, an instance of the ApiPageSet class will normalize titles,
30 * determine if the pages/revisions exist, and prefetch any additional page
33 * When a generator is used, the result of the generator will become the input
34 * for the second instance of this class, and all subsequent actions will use
35 * the second instance for all their work.
38 * @since 1.21 derives from ApiBase instead of ApiQueryBase
40 class ApiPageSet
extends ApiBase
{
42 * Constructor flag: The new instance of ApiPageSet will ignore the 'generator=' parameter
45 const DISABLE_GENERATORS
= 1;
49 private $mResolveRedirects;
50 private $mConvertTitles;
51 private $mAllowGenerator;
53 private $mAllPages = []; // [ns][dbkey] => page_id or negative when missing
54 private $mTitles = [];
55 private $mGoodAndMissingPages = []; // [ns][dbkey] => page_id or negative when missing
56 private $mGoodPages = []; // [ns][dbkey] => page_id
57 private $mGoodTitles = [];
58 private $mMissingPages = []; // [ns][dbkey] => fake page_id
59 private $mMissingTitles = [];
60 /** @var array [fake_page_id] => [ 'title' => $title, 'invalidreason' => $reason ] */
61 private $mInvalidTitles = [];
62 private $mMissingPageIDs = [];
63 private $mRedirectTitles = [];
64 private $mSpecialTitles = [];
65 private $mAllSpecials = []; // separate from mAllPages to avoid breaking getAllTitlesByNamespace()
66 private $mNormalizedTitles = [];
67 private $mInterwikiTitles = [];
69 private $mPendingRedirectIDs = [];
70 private $mPendingRedirectSpecialPages = []; // [dbkey] => [ Title $from, Title $to ]
71 private $mResolvedRedirectTitles = [];
72 private $mConvertedTitles = [];
73 private $mGoodRevIDs = [];
74 private $mLiveRevIDs = [];
75 private $mDeletedRevIDs = [];
76 private $mMissingRevIDs = [];
77 private $mGeneratorData = []; // [ns][dbkey] => data array
78 private $mFakePageId = -1;
79 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 & self
::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, self
::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 * @return array[] Array of arrays with 'title' and 'invalidreason' properties
427 public function getInvalidTitlesAndReasons() {
428 return $this->mInvalidTitles
;
432 * Page IDs that were not found in the database
433 * @return array Array of page IDs
435 public function getMissingPageIDs() {
436 return $this->mMissingPageIDs
;
440 * Get a list of redirect resolutions - maps a title to its redirect
441 * target, as an array of output-ready arrays
444 public function getRedirectTitles() {
445 return $this->mRedirectTitles
;
449 * Get a list of redirect resolutions - maps a title to its redirect
450 * target. Includes generator data for redirect source when available.
451 * @param ApiResult|null $result
452 * @return array Array of prefixed_title (string) => Title object
455 public function getRedirectTitlesAsResult( $result = null ) {
457 foreach ( $this->getRedirectTitles() as $titleStrFrom => $titleTo ) {
459 'from' => strval( $titleStrFrom ),
460 'to' => $titleTo->getPrefixedText(),
462 if ( $titleTo->hasFragment() ) {
463 $r['tofragment'] = $titleTo->getFragment();
465 if ( $titleTo->isExternal() ) {
466 $r['tointerwiki'] = $titleTo->getInterwiki();
468 if ( isset( $this->mResolvedRedirectTitles
[$titleStrFrom] ) ) {
469 $titleFrom = $this->mResolvedRedirectTitles
[$titleStrFrom];
470 $ns = $titleFrom->getNamespace();
471 $dbkey = $titleFrom->getDBkey();
472 if ( isset( $this->mGeneratorData
[$ns][$dbkey] ) ) {
473 $r = array_merge( $this->mGeneratorData
[$ns][$dbkey], $r );
479 if ( !empty( $values ) && $result ) {
480 ApiResult
::setIndexedTagName( $values, 'r' );
487 * Get a list of title normalizations - maps a title to its normalized
489 * @return array Array of raw_prefixed_title (string) => prefixed_title (string)
491 public function getNormalizedTitles() {
492 return $this->mNormalizedTitles
;
496 * Get a list of title normalizations - maps a title to its normalized
497 * version in the form of result array.
498 * @param ApiResult|null $result
499 * @return array Array of raw_prefixed_title (string) => prefixed_title (string)
502 public function getNormalizedTitlesAsResult( $result = null ) {
504 $contLang = MediaWikiServices
::getInstance()->getContentLanguage();
505 foreach ( $this->getNormalizedTitles() as $rawTitleStr => $titleStr ) {
506 $encode = $contLang->normalize( $rawTitleStr ) !== $rawTitleStr;
508 'fromencoded' => $encode,
509 'from' => $encode ?
rawurlencode( $rawTitleStr ) : $rawTitleStr,
513 if ( !empty( $values ) && $result ) {
514 ApiResult
::setIndexedTagName( $values, 'n' );
521 * Get a list of title conversions - maps a title to its converted
523 * @return array Array of raw_prefixed_title (string) => prefixed_title (string)
525 public function getConvertedTitles() {
526 return $this->mConvertedTitles
;
530 * Get a list of title conversions - maps a title to its converted
531 * version as a result array.
532 * @param ApiResult|null $result
533 * @return array Array of (from, to) strings
536 public function getConvertedTitlesAsResult( $result = null ) {
538 foreach ( $this->getConvertedTitles() as $rawTitleStr => $titleStr ) {
540 'from' => $rawTitleStr,
544 if ( !empty( $values ) && $result ) {
545 ApiResult
::setIndexedTagName( $values, 'c' );
552 * Get a list of interwiki titles - maps a title to its interwiki
554 * @return array Array of raw_prefixed_title (string) => interwiki_prefix (string)
556 public function getInterwikiTitles() {
557 return $this->mInterwikiTitles
;
561 * Get a list of interwiki titles - maps a title to its interwiki
563 * @param ApiResult|null $result
565 * @return array Array of raw_prefixed_title (string) => interwiki_prefix (string)
568 public function getInterwikiTitlesAsResult( $result = null, $iwUrl = false ) {
570 foreach ( $this->getInterwikiTitles() as $rawTitleStr => $interwikiStr ) {
572 'title' => $rawTitleStr,
573 'iw' => $interwikiStr,
576 $title = Title
::newFromText( $rawTitleStr );
577 $item['url'] = $title->getFullURL( '', false, PROTO_CURRENT
);
581 if ( !empty( $values ) && $result ) {
582 ApiResult
::setIndexedTagName( $values, 'i' );
589 * Get an array of invalid/special/missing titles.
591 * @param array $invalidChecks List of types of invalid titles to include.
592 * Recognized values are:
593 * - invalidTitles: Titles and reasons from $this->getInvalidTitlesAndReasons()
594 * - special: Titles from $this->getSpecialTitles()
595 * - missingIds: ids from $this->getMissingPageIDs()
596 * - missingRevIds: ids from $this->getMissingRevisionIDs()
597 * - missingTitles: Titles from $this->getMissingTitles()
598 * - interwikiTitles: Titles from $this->getInterwikiTitlesAsResult()
599 * @return array Array suitable for inclusion in the response
602 public function getInvalidTitlesAndRevisions( $invalidChecks = [ 'invalidTitles',
603 'special', 'missingIds', 'missingRevIds', 'missingTitles', 'interwikiTitles' ]
606 if ( in_array( 'invalidTitles', $invalidChecks ) ) {
607 self
::addValues( $result, $this->getInvalidTitlesAndReasons(), [ 'invalid' ] );
609 if ( in_array( 'special', $invalidChecks ) ) {
612 foreach ( $this->getSpecialTitles() as $title ) {
613 if ( $title->isKnown() ) {
619 self
::addValues( $result, $unknown, [ 'special', 'missing' ] );
620 self
::addValues( $result, $known, [ 'special' ] );
622 if ( in_array( 'missingIds', $invalidChecks ) ) {
623 self
::addValues( $result, $this->getMissingPageIDs(), [ 'missing' ], 'pageid' );
625 if ( in_array( 'missingRevIds', $invalidChecks ) ) {
626 self
::addValues( $result, $this->getMissingRevisionIDs(), [ 'missing' ], 'revid' );
628 if ( in_array( 'missingTitles', $invalidChecks ) ) {
631 foreach ( $this->getMissingTitles() as $title ) {
632 if ( $title->isKnown() ) {
638 self
::addValues( $result, $unknown, [ 'missing' ] );
639 self
::addValues( $result, $known, [ 'missing', 'known' ] );
641 if ( in_array( 'interwikiTitles', $invalidChecks ) ) {
642 self
::addValues( $result, $this->getInterwikiTitlesAsResult() );
649 * Get the list of valid revision IDs (requested with the revids= parameter)
650 * @return array Array of revID (int) => pageID (int)
652 public function getRevisionIDs() {
653 return $this->mGoodRevIDs
;
657 * Get the list of non-deleted revision IDs (requested with the revids= parameter)
658 * @return array Array of revID (int) => pageID (int)
660 public function getLiveRevisionIDs() {
661 return $this->mLiveRevIDs
;
665 * Get the list of revision IDs that were associated with deleted titles.
666 * @return array Array of revID (int) => pageID (int)
668 public function getDeletedRevisionIDs() {
669 return $this->mDeletedRevIDs
;
673 * Revision IDs that were not found in the database
674 * @return array Array of revision IDs
676 public function getMissingRevisionIDs() {
677 return $this->mMissingRevIDs
;
681 * Revision IDs that were not found in the database as result array.
682 * @param ApiResult|null $result
683 * @return array Array of revision IDs
686 public function getMissingRevisionIDsAsResult( $result = null ) {
688 foreach ( $this->getMissingRevisionIDs() as $revid ) {
693 if ( !empty( $values ) && $result ) {
694 ApiResult
::setIndexedTagName( $values, 'rev' );
701 * Get the list of titles with negative namespace
704 public function getSpecialTitles() {
705 return $this->mSpecialTitles
;
709 * Returns the number of revisions (requested with revids= parameter).
710 * @return int Number of revisions.
712 public function getRevisionCount() {
713 return count( $this->getRevisionIDs() );
717 * Populate this PageSet from a list of Titles
718 * @param array $titles Array of Title objects
720 public function populateFromTitles( $titles ) {
721 $this->initFromTitles( $titles );
725 * Populate this PageSet from a list of page IDs
726 * @param array $pageIDs Array of page IDs
728 public function populateFromPageIDs( $pageIDs ) {
729 $this->initFromPageIds( $pageIDs );
733 * Populate this PageSet from a rowset returned from the database
735 * Note that the query result must include the columns returned by
736 * $this->getPageTableFields().
738 * @param IDatabase $db
739 * @param IResultWrapper $queryResult
741 public function populateFromQueryResult( $db, $queryResult ) {
742 $this->initFromQueryResult( $queryResult );
746 * Populate this PageSet from a list of revision IDs
747 * @param array $revIDs Array of revision IDs
749 public function populateFromRevisionIDs( $revIDs ) {
750 $this->initFromRevIDs( $revIDs );
754 * Extract all requested fields from the row received from the database
755 * @param stdClass $row Result row
757 public function processDbRow( $row ) {
758 // Store Title object in various data structures
759 $title = Title
::newFromRow( $row );
761 $linkCache = MediaWikiServices
::getInstance()->getLinkCache();
762 $linkCache->addGoodLinkObjFromRow( $title, $row );
764 $pageId = (int)$row->page_id
;
765 $this->mAllPages
[$row->page_namespace
][$row->page_title
] = $pageId;
766 $this->mTitles
[] = $title;
768 if ( $this->mResolveRedirects
&& $row->page_is_redirect
== '1' ) {
769 $this->mPendingRedirectIDs
[$pageId] = $title;
771 $this->mGoodPages
[$row->page_namespace
][$row->page_title
] = $pageId;
772 $this->mGoodAndMissingPages
[$row->page_namespace
][$row->page_title
] = $pageId;
773 $this->mGoodTitles
[$pageId] = $title;
776 foreach ( $this->mRequestedPageFields
as $fieldName => &$fieldValues ) {
777 $fieldValues[$pageId] = $row->$fieldName;
782 * This method populates internal variables with page information
783 * based on the given array of title strings.
786 * #1 For each title, get data from `page` table
787 * #2 If page was not found in the DB, store it as missing
789 * Additionally, when resolving redirects:
790 * #3 If no more redirects left, stop.
791 * #4 For each redirect, get its target from the `redirect` table.
792 * #5 Substitute the original LinkBatch object with the new list
793 * #6 Repeat from step #1
795 * @param array $titles Array of Title objects or strings
797 private function initFromTitles( $titles ) {
798 // Get validated and normalized title objects
799 $linkBatch = $this->processTitlesArray( $titles );
800 if ( $linkBatch->isEmpty() ) {
801 // There might be special-page redirects
802 $this->resolvePendingRedirects();
806 $db = $this->getDB();
807 $set = $linkBatch->constructSet( 'page', $db );
809 // Get pageIDs data from the `page` table
810 $res = $db->select( 'page', $this->getPageTableFields(), $set,
813 // Hack: get the ns:titles stored in [ ns => [ titles ] ] format
814 $this->initFromQueryResult( $res, $linkBatch->data
, true ); // process Titles
816 // Resolve any found redirects
817 $this->resolvePendingRedirects();
821 * Does the same as initFromTitles(), but is based on page IDs instead
822 * @param array $pageids Array of page IDs
823 * @param bool $filterIds Whether the IDs need filtering
825 private function initFromPageIds( $pageids, $filterIds = true ) {
830 $pageids = array_map( 'intval', $pageids ); // paranoia
831 $remaining = array_flip( $pageids );
834 $pageids = $this->filterIDs( [ [ 'page', 'page_id' ] ], $pageids );
838 if ( !empty( $pageids ) ) {
840 'page_id' => $pageids
842 $db = $this->getDB();
844 // Get pageIDs data from the `page` table
845 $res = $db->select( 'page', $this->getPageTableFields(), $set,
849 $this->initFromQueryResult( $res, $remaining, false ); // process PageIDs
851 // Resolve any found redirects
852 $this->resolvePendingRedirects();
856 * Iterate through the result of the query on 'page' table,
857 * and for each row create and store title object and save any extra fields requested.
858 * @param IResultWrapper $res DB Query result
859 * @param array $remaining Array of either pageID or ns/title elements (optional).
860 * If given, any missing items will go to $mMissingPageIDs and $mMissingTitles
861 * @param bool $processTitles Must be provided together with $remaining.
862 * If true, treat $remaining as an array of [ns][title]
863 * If false, treat it as an array of [pageIDs]
865 private function initFromQueryResult( $res, &$remaining = null, $processTitles = null ) {
866 if ( !is_null( $remaining ) && is_null( $processTitles ) ) {
867 ApiBase
::dieDebug( __METHOD__
, 'Missing $processTitles parameter when $remaining is provided' );
870 $nsInfo = MediaWikiServices
::getInstance()->getNamespaceInfo();
874 foreach ( $res as $row ) {
875 $pageId = (int)$row->page_id
;
877 // Remove found page from the list of remaining items
878 if ( isset( $remaining ) ) {
879 if ( $processTitles ) {
880 unset( $remaining[$row->page_namespace
][$row->page_title
] );
882 unset( $remaining[$pageId] );
886 // Store any extra fields requested by modules
887 $this->processDbRow( $row );
889 // Need gender information
890 if ( $nsInfo->hasGenderDistinction( $row->page_namespace
) ) {
891 $usernames[] = $row->page_title
;
896 if ( isset( $remaining ) ) {
897 // Any items left in the $remaining list are added as missing
898 if ( $processTitles ) {
899 // The remaining titles in $remaining are non-existent pages
900 $linkCache = MediaWikiServices
::getInstance()->getLinkCache();
901 foreach ( $remaining as $ns => $dbkeys ) {
902 foreach ( array_keys( $dbkeys ) as $dbkey ) {
903 $title = Title
::makeTitle( $ns, $dbkey );
904 $linkCache->addBadLinkObj( $title );
905 $this->mAllPages
[$ns][$dbkey] = $this->mFakePageId
;
906 $this->mMissingPages
[$ns][$dbkey] = $this->mFakePageId
;
907 $this->mGoodAndMissingPages
[$ns][$dbkey] = $this->mFakePageId
;
908 $this->mMissingTitles
[$this->mFakePageId
] = $title;
909 $this->mFakePageId
--;
910 $this->mTitles
[] = $title;
912 // need gender information
913 if ( $nsInfo->hasGenderDistinction( $ns ) ) {
914 $usernames[] = $dbkey;
919 // The remaining pageids do not exist
920 if ( !$this->mMissingPageIDs
) {
921 $this->mMissingPageIDs
= array_keys( $remaining );
923 $this->mMissingPageIDs
= array_merge( $this->mMissingPageIDs
, array_keys( $remaining ) );
928 // Get gender information
929 $genderCache = MediaWikiServices
::getInstance()->getGenderCache();
930 $genderCache->doQuery( $usernames, __METHOD__
);
934 * Does the same as initFromTitles(), but is based on revision IDs
936 * @param array $revids Array of revision IDs
938 private function initFromRevIDs( $revids ) {
943 $revids = array_map( 'intval', $revids ); // paranoia
944 $db = $this->getDB();
946 $remaining = array_flip( $revids );
948 $revids = $this->filterIDs( [ [ 'revision', 'rev_id' ], [ 'archive', 'ar_rev_id' ] ], $revids );
949 $goodRemaining = array_flip( $revids );
952 $tables = [ 'revision', 'page' ];
953 $fields = [ 'rev_id', 'rev_page' ];
954 $where = [ 'rev_id' => $revids, 'rev_page = page_id' ];
956 // Get pageIDs data from the `page` table
957 $res = $db->select( $tables, $fields, $where, __METHOD__
);
958 foreach ( $res as $row ) {
959 $revid = (int)$row->rev_id
;
960 $pageid = (int)$row->rev_page
;
961 $this->mGoodRevIDs
[$revid] = $pageid;
962 $this->mLiveRevIDs
[$revid] = $pageid;
963 $pageids[$pageid] = '';
964 unset( $remaining[$revid] );
965 unset( $goodRemaining[$revid] );
969 // Populate all the page information
970 $this->initFromPageIds( array_keys( $pageids ), false );
972 // If the user can see deleted revisions, pull out the corresponding
973 // titles from the archive table and include them too. We ignore
974 // ar_page_id because deleted revisions are tied by title, not page_id.
975 if ( $goodRemaining &&
976 $this->getPermissionManager()->userHasRight( $this->getUser(), 'deletedhistory' ) ) {
977 $tables = [ 'archive' ];
978 $fields = [ 'ar_rev_id', 'ar_namespace', 'ar_title' ];
979 $where = [ 'ar_rev_id' => array_keys( $goodRemaining ) ];
981 $res = $db->select( $tables, $fields, $where, __METHOD__
);
983 foreach ( $res as $row ) {
984 $revid = (int)$row->ar_rev_id
;
985 $titles[$revid] = Title
::makeTitle( $row->ar_namespace
, $row->ar_title
);
986 unset( $remaining[$revid] );
989 $this->initFromTitles( $titles );
991 foreach ( $titles as $revid => $title ) {
992 $ns = $title->getNamespace();
993 $dbkey = $title->getDBkey();
995 // Handle converted titles
996 if ( !isset( $this->mAllPages
[$ns][$dbkey] ) &&
997 isset( $this->mConvertedTitles
[$title->getPrefixedText()] )
999 $title = Title
::newFromText( $this->mConvertedTitles
[$title->getPrefixedText()] );
1000 $ns = $title->getNamespace();
1001 $dbkey = $title->getDBkey();
1004 if ( isset( $this->mAllPages
[$ns][$dbkey] ) ) {
1005 $this->mGoodRevIDs
[$revid] = $this->mAllPages
[$ns][$dbkey];
1006 $this->mDeletedRevIDs
[$revid] = $this->mAllPages
[$ns][$dbkey];
1008 $remaining[$revid] = true;
1013 $this->mMissingRevIDs
= array_keys( $remaining );
1017 * Resolve any redirects in the result if redirect resolution was
1018 * requested. This function is called repeatedly until all redirects
1019 * have been resolved.
1021 private function resolvePendingRedirects() {
1022 if ( $this->mResolveRedirects
) {
1023 $db = $this->getDB();
1024 $pageFlds = $this->getPageTableFields();
1026 // Repeat until all redirects have been resolved
1027 // The infinite loop is prevented by keeping all known pages in $this->mAllPages
1028 while ( $this->mPendingRedirectIDs ||
$this->mPendingRedirectSpecialPages
) {
1029 // Resolve redirects by querying the pagelinks table, and repeat the process
1030 // Create a new linkBatch object for the next pass
1031 $linkBatch = $this->getRedirectTargets();
1033 if ( $linkBatch->isEmpty() ) {
1037 $set = $linkBatch->constructSet( 'page', $db );
1038 if ( $set === false ) {
1042 // Get pageIDs data from the `page` table
1043 $res = $db->select( 'page', $pageFlds, $set, __METHOD__
);
1045 // Hack: get the ns:titles stored in [ns => array(titles)] format
1046 $this->initFromQueryResult( $res, $linkBatch->data
, true );
1052 * Get the targets of the pending redirects from the database
1054 * Also creates entries in the redirect table for redirects that don't
1058 private function getRedirectTargets() {
1059 $titlesToResolve = [];
1060 $db = $this->getDB();
1062 if ( $this->mPendingRedirectIDs
) {
1071 ], [ 'rd_from' => array_keys( $this->mPendingRedirectIDs
) ],
1074 foreach ( $res as $row ) {
1075 $rdfrom = (int)$row->rd_from
;
1076 $from = $this->mPendingRedirectIDs
[$rdfrom]->getPrefixedText();
1077 $to = Title
::makeTitle(
1083 $this->mResolvedRedirectTitles
[$from] = $this->mPendingRedirectIDs
[$rdfrom];
1084 unset( $this->mPendingRedirectIDs
[$rdfrom] );
1085 if ( $to->isExternal() ) {
1086 $this->mInterwikiTitles
[$to->getPrefixedText()] = $to->getInterwiki();
1087 } elseif ( !isset( $this->mAllPages
[$to->getNamespace()][$to->getDBkey()] ) ) {
1088 $titlesToResolve[] = $to;
1090 $this->mRedirectTitles
[$from] = $to;
1093 if ( $this->mPendingRedirectIDs
) {
1094 // We found pages that aren't in the redirect table
1096 foreach ( $this->mPendingRedirectIDs
as $id => $title ) {
1097 $page = WikiPage
::factory( $title );
1098 $rt = $page->insertRedirect();
1100 // What the hell. Let's just ignore this
1103 if ( $rt->isExternal() ) {
1104 $this->mInterwikiTitles
[$rt->getPrefixedText()] = $rt->getInterwiki();
1105 } elseif ( !isset( $this->mAllPages
[$rt->getNamespace()][$rt->getDBkey()] ) ) {
1106 $titlesToResolve[] = $rt;
1108 $from = $title->getPrefixedText();
1109 $this->mResolvedRedirectTitles
[$from] = $title;
1110 $this->mRedirectTitles
[$from] = $rt;
1111 unset( $this->mPendingRedirectIDs
[$id] );
1116 if ( $this->mPendingRedirectSpecialPages
) {
1117 foreach ( $this->mPendingRedirectSpecialPages
as $key => list( $from, $to ) ) {
1118 $fromKey = $from->getPrefixedText();
1119 $this->mResolvedRedirectTitles
[$fromKey] = $from;
1120 $this->mRedirectTitles
[$fromKey] = $to;
1121 if ( $to->isExternal() ) {
1122 $this->mInterwikiTitles
[$to->getPrefixedText()] = $to->getInterwiki();
1123 } elseif ( !isset( $this->mAllPages
[$to->getNamespace()][$to->getDBkey()] ) ) {
1124 $titlesToResolve[] = $to;
1127 $this->mPendingRedirectSpecialPages
= [];
1129 // Set private caching since we don't know what criteria the
1130 // special pages used to decide on these redirects.
1131 $this->mCacheMode
= 'private';
1134 return $this->processTitlesArray( $titlesToResolve );
1138 * Get the cache mode for the data generated by this module.
1139 * All PageSet users should take into account whether this returns a more-restrictive
1140 * cache mode than the using module itself. For possible return values and other
1141 * details about cache modes, see ApiMain::setCacheMode()
1143 * Public caching will only be allowed if *all* the modules that supply
1144 * data for a given request return a cache mode of public.
1146 * @param array|null $params
1150 public function getCacheMode( $params = null ) {
1151 return $this->mCacheMode
;
1155 * Given an array of title strings, convert them into Title objects.
1156 * Alternatively, an array of Title objects may be given.
1157 * This method validates access rights for the title,
1158 * and appends normalization values to the output.
1160 * @param array $titles Array of Title objects or strings
1163 private function processTitlesArray( $titles ) {
1165 $linkBatch = new LinkBatch();
1166 $services = MediaWikiServices
::getInstance();
1167 $contLang = $services->getContentLanguage();
1170 foreach ( $titles as $index => $title ) {
1171 if ( is_string( $title ) ) {
1173 $titleObj = Title
::newFromTextThrow( $title, $this->mDefaultNamespace
);
1174 } catch ( MalformedTitleException
$ex ) {
1175 // Handle invalid titles gracefully
1176 if ( !isset( $this->mAllPages
[0][$title] ) ) {
1177 $this->mAllPages
[0][$title] = $this->mFakePageId
;
1178 $this->mInvalidTitles
[$this->mFakePageId
] = [
1180 'invalidreason' => $this->getErrorFormatter()->formatException( $ex, [ 'bc' => true ] ),
1182 $this->mFakePageId
--;
1184 continue; // There's nothing else we can do
1190 $titleObjects[$index] = $titleObj;
1193 // Get gender information
1194 $genderCache = $services->getGenderCache();
1195 $genderCache->doTitlesArray( $titleObjects, __METHOD__
);
1197 foreach ( $titleObjects as $index => $titleObj ) {
1198 $title = is_string( $titles[$index] ) ?
$titles[$index] : false;
1199 $unconvertedTitle = $titleObj->getPrefixedText();
1200 $titleWasConverted = false;
1201 if ( $titleObj->isExternal() ) {
1202 // This title is an interwiki link.
1203 $this->mInterwikiTitles
[$unconvertedTitle] = $titleObj->getInterwiki();
1205 // Variants checking
1207 $this->mConvertTitles
&& $contLang->hasVariants() && !$titleObj->exists()
1209 // Language::findVariantLink will modify titleText and titleObj into
1210 // the canonical variant if possible
1211 $titleText = $title !== false ?
$title : $titleObj->getPrefixedText();
1212 $contLang->findVariantLink( $titleText, $titleObj );
1213 $titleWasConverted = $unconvertedTitle !== $titleObj->getPrefixedText();
1216 if ( $titleObj->getNamespace() < 0 ) {
1217 // Handle Special and Media pages
1218 $titleObj = $titleObj->fixSpecialName();
1219 $ns = $titleObj->getNamespace();
1220 $dbkey = $titleObj->getDBkey();
1221 if ( !isset( $this->mAllSpecials
[$ns][$dbkey] ) ) {
1222 $this->mAllSpecials
[$ns][$dbkey] = $this->mFakePageId
;
1224 if ( $ns === NS_SPECIAL
&& $this->mResolveRedirects
) {
1225 $spFactory = $services->getSpecialPageFactory();
1226 $special = $spFactory->getPage( $dbkey );
1227 if ( $special instanceof RedirectSpecialArticle
) {
1228 // Only RedirectSpecialArticle is intended to redirect to an article, other kinds of
1229 // RedirectSpecialPage are probably applying weird URL parameters we don't want to handle.
1230 $context = new DerivativeContext( $this );
1231 $context->setTitle( $titleObj );
1232 $context->setRequest( new FauxRequest
);
1233 $special->setContext( $context );
1234 list( /* $alias */, $subpage ) = $spFactory->resolveAlias( $dbkey );
1235 $target = $special->getRedirect( $subpage );
1239 $this->mPendingRedirectSpecialPages
[$dbkey] = [ $titleObj, $target ];
1241 $this->mSpecialTitles
[$this->mFakePageId
] = $titleObj;
1242 $this->mFakePageId
--;
1247 $linkBatch->addObj( $titleObj );
1251 // Make sure we remember the original title that was
1252 // given to us. This way the caller can correlate new
1253 // titles with the originally requested when e.g. the
1254 // namespace is localized or the capitalization is
1256 if ( $titleWasConverted ) {
1257 $this->mConvertedTitles
[$unconvertedTitle] = $titleObj->getPrefixedText();
1258 // In this case the page can't be Special.
1259 if ( $title !== false && $title !== $unconvertedTitle ) {
1260 $this->mNormalizedTitles
[$title] = $unconvertedTitle;
1262 } elseif ( $title !== false && $title !== $titleObj->getPrefixedText() ) {
1263 $this->mNormalizedTitles
[$title] = $titleObj->getPrefixedText();
1271 * Set data for a title.
1273 * This data may be extracted into an ApiResult using
1274 * self::populateGeneratorData. This should generally be limited to
1275 * data that is likely to be particularly useful to end users rather than
1276 * just being a dump of everything returned in non-generator mode.
1278 * Redirects here will *not* be followed, even if 'redirects' was
1279 * specified, since in the case of multiple redirects we can't know which
1280 * source's data to use on the target.
1282 * @param Title $title
1283 * @param array $data
1285 public function setGeneratorData( Title
$title, array $data ) {
1286 $ns = $title->getNamespace();
1287 $dbkey = $title->getDBkey();
1288 $this->mGeneratorData
[$ns][$dbkey] = $data;
1292 * Controls how generator data about a redirect source is merged into
1293 * the generator data for the redirect target. When not set no data
1294 * is merged. Note that if multiple titles redirect to the same target
1295 * the order of operations is undefined.
1297 * Example to include generated data from redirect in target, prefering
1298 * the data generated for the destination when there is a collision:
1300 * $pageSet->setRedirectMergePolicy( function( array $current, array $new ) {
1301 * return $current + $new;
1305 * @param callable|null $callable Recieves two array arguments, first the
1306 * generator data for the redirect target and second the generator data
1307 * for the redirect source. Returns the resulting generator data to use
1308 * for the redirect target.
1310 public function setRedirectMergePolicy( $callable ) {
1311 $this->mRedirectMergePolicy
= $callable;
1315 * Populate the generator data for all titles in the result
1317 * The page data may be inserted into an ApiResult object or into an
1318 * associative array. The $path parameter specifies the path within the
1319 * ApiResult or array to find the "pages" node.
1321 * The "pages" node itself must be an associative array mapping the page ID
1322 * or fake page ID values returned by this pageset (see
1323 * self::getAllTitlesByNamespace() and self::getSpecialTitles()) to
1324 * associative arrays of page data. Each of those subarrays will have the
1325 * data from self::setGeneratorData() merged in.
1327 * Data that was set by self::setGeneratorData() for pages not in the
1328 * "pages" node will be ignored.
1330 * @param ApiResult|array &$result
1331 * @param array $path
1332 * @return bool Whether the data fit
1334 public function populateGeneratorData( &$result, array $path = [] ) {
1335 if ( $result instanceof ApiResult
) {
1336 $data = $result->getResultData( $path );
1337 if ( $data === null ) {
1342 foreach ( $path as $key ) {
1343 if ( !isset( $data[$key] ) ) {
1344 // Path isn't in $result, so nothing to add, so everything
1348 $data = &$data[$key];
1351 foreach ( $this->mGeneratorData
as $ns => $dbkeys ) {
1352 if ( $ns === NS_SPECIAL
) {
1354 foreach ( $this->mSpecialTitles
as $id => $title ) {
1355 $pages[$title->getDBkey()] = $id;
1358 if ( !isset( $this->mAllPages
[$ns] ) ) {
1359 // No known titles in the whole namespace. Skip it.
1362 $pages = $this->mAllPages
[$ns];
1364 foreach ( $dbkeys as $dbkey => $genData ) {
1365 if ( !isset( $pages[$dbkey] ) ) {
1366 // Unknown title. Forget it.
1369 $pageId = $pages[$dbkey];
1370 if ( !isset( $data[$pageId] ) ) {
1371 // $pageId didn't make it into the result. Ignore it.
1375 if ( $result instanceof ApiResult
) {
1376 $path2 = array_merge( $path, [ $pageId ] );
1377 foreach ( $genData as $key => $value ) {
1378 if ( !$result->addValue( $path2, $key, $value ) ) {
1383 $data[$pageId] = array_merge( $data[$pageId], $genData );
1388 // Merge data generated about redirect titles into the redirect destination
1389 if ( $this->mRedirectMergePolicy
) {
1390 foreach ( $this->mResolvedRedirectTitles
as $titleFrom ) {
1392 while ( isset( $this->mRedirectTitles
[$dest->getPrefixedText()] ) ) {
1393 $dest = $this->mRedirectTitles
[$dest->getPrefixedText()];
1395 $fromNs = $titleFrom->getNamespace();
1396 $fromDBkey = $titleFrom->getDBkey();
1397 $toPageId = $dest->getArticleID();
1398 if ( isset( $data[$toPageId] ) &&
1399 isset( $this->mGeneratorData
[$fromNs][$fromDBkey] )
1401 // It is necessary to set both $data and add to $result, if an ApiResult,
1402 // to ensure multiple redirects to the same destination are all merged.
1403 $data[$toPageId] = call_user_func(
1404 $this->mRedirectMergePolicy
,
1406 $this->mGeneratorData
[$fromNs][$fromDBkey]
1408 if ( $result instanceof ApiResult
&&
1409 !$result->addValue( $path, $toPageId, $data[$toPageId], ApiResult
::OVERRIDE
)
1421 * Get the database connection (read-only)
1424 protected function getDB() {
1425 return $this->mDbSource
->getDB();
1428 public function getAllowedParams( $flags = 0 ) {
1431 ApiBase
::PARAM_ISMULTI
=> true,
1432 ApiBase
::PARAM_HELP_MSG
=> 'api-pageset-param-titles',
1435 ApiBase
::PARAM_TYPE
=> 'integer',
1436 ApiBase
::PARAM_ISMULTI
=> true,
1437 ApiBase
::PARAM_HELP_MSG
=> 'api-pageset-param-pageids',
1440 ApiBase
::PARAM_TYPE
=> 'integer',
1441 ApiBase
::PARAM_ISMULTI
=> true,
1442 ApiBase
::PARAM_HELP_MSG
=> 'api-pageset-param-revids',
1445 ApiBase
::PARAM_TYPE
=> null,
1446 ApiBase
::PARAM_HELP_MSG
=> 'api-pageset-param-generator',
1447 ApiBase
::PARAM_SUBMODULE_PARAM_PREFIX
=> 'g',
1450 ApiBase
::PARAM_DFLT
=> false,
1451 ApiBase
::PARAM_HELP_MSG
=> $this->mAllowGenerator
1452 ?
'api-pageset-param-redirects-generator'
1453 : 'api-pageset-param-redirects-nogenerator',
1455 'converttitles' => [
1456 ApiBase
::PARAM_DFLT
=> false,
1457 ApiBase
::PARAM_HELP_MSG
=> [
1458 'api-pageset-param-converttitles',
1459 [ Message
::listParam( LanguageConverter
::$languagesWithVariants, 'text' ) ],
1464 if ( !$this->mAllowGenerator
) {
1465 unset( $result['generator'] );
1466 } elseif ( $flags & ApiBase
::GET_VALUES_FOR_HELP
) {
1467 $result['generator'][ApiBase
::PARAM_TYPE
] = 'submodule';
1468 $result['generator'][ApiBase
::PARAM_SUBMODULE_MAP
] = $this->getGenerators();
1474 protected function handleParamNormalization( $paramName, $value, $rawValue ) {
1475 parent
::handleParamNormalization( $paramName, $value, $rawValue );
1477 if ( $paramName === 'titles' ) {
1478 // For the 'titles' parameter, we want to split it like ApiBase would
1479 // and add any changed titles to $this->mNormalizedTitles
1480 $value = $this->explodeMultiValue( $value, self
::LIMIT_SML2 +
1 );
1481 $l = count( $value );
1482 $rawValue = $this->explodeMultiValue( $rawValue, $l );
1483 for ( $i = 0; $i < $l; $i++
) {
1484 if ( $value[$i] !== $rawValue[$i] ) {
1485 $this->mNormalizedTitles
[$rawValue[$i]] = $value[$i];
1491 private static $generators = null;
1494 * Get an array of all available generators
1497 private function getGenerators() {
1498 if ( self
::$generators === null ) {
1499 $query = $this->mDbSource
;
1500 if ( !( $query instanceof ApiQuery
) ) {
1501 // If the parent container of this pageset is not ApiQuery,
1502 // we must create it to get module manager
1503 $query = $this->getMain()->getModuleManager()->getModule( 'query' );
1506 $prefix = $query->getModulePath() . '+';
1507 $mgr = $query->getModuleManager();
1508 foreach ( $mgr->getNamesWithClasses() as $name => $class ) {
1509 if ( is_subclass_of( $class, ApiQueryGeneratorBase
::class ) ) {
1510 $gens[$name] = $prefix . $name;
1514 self
::$generators = $gens;
1517 return self
::$generators;