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 $mNormalizedTitles = [];
68 private $mInterwikiTitles = [];
70 private $mPendingRedirectIDs = [];
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';
80 private $mRequestedPageFields = [];
82 private $mDefaultNamespace = NS_MAIN
;
83 /** @var callable|null */
84 private $mRedirectMergePolicy;
87 * Add all items from $values into the result
88 * @param array $result Output
89 * @param array $values Values to add
90 * @param string[] $flags The names of boolean flags to mark this element
91 * @param string $name If given, name of the value
93 private static function addValues( array &$result, $values, $flags = [], $name = null ) {
94 foreach ( $values as $val ) {
95 if ( $val instanceof Title
) {
97 ApiQueryBase
::addTitleInfo( $v, $val );
98 } elseif ( $name !== null ) {
99 $v = [ $name => $val ];
103 foreach ( $flags as $flag ) {
111 * @param ApiBase $dbSource Module implementing getDB().
112 * Allows PageSet to reuse existing db connection from the shared state like ApiQuery.
113 * @param int $flags Zero or more flags like DISABLE_GENERATORS
114 * @param int $defaultNamespace The namespace to use if none is specified by a prefix.
115 * @since 1.21 accepts $flags instead of two boolean values
117 public function __construct( ApiBase
$dbSource, $flags = 0, $defaultNamespace = NS_MAIN
) {
118 parent
::__construct( $dbSource->getMain(), $dbSource->getModuleName() );
119 $this->mDbSource
= $dbSource;
120 $this->mAllowGenerator
= ( $flags & ApiPageSet
::DISABLE_GENERATORS
) == 0;
121 $this->mDefaultNamespace
= $defaultNamespace;
123 $this->mParams
= $this->extractRequestParams();
124 $this->mResolveRedirects
= $this->mParams
['redirects'];
125 $this->mConvertTitles
= $this->mParams
['converttitles'];
129 * In case execute() is not called, call this method to mark all relevant parameters as used
130 * This prevents unused parameters from being reported as warnings
132 public function executeDryRun() {
133 $this->executeInternal( true );
137 * Populate the PageSet from the request parameters.
139 public function execute() {
140 $this->executeInternal( false );
144 * Populate the PageSet from the request parameters.
145 * @param bool $isDryRun If true, instantiates generator, but only to mark
146 * relevant parameters as used
148 private function executeInternal( $isDryRun ) {
149 $generatorName = $this->mAllowGenerator ?
$this->mParams
['generator'] : null;
150 if ( isset( $generatorName ) ) {
151 $dbSource = $this->mDbSource
;
152 if ( !$dbSource instanceof ApiQuery
) {
153 // If the parent container of this pageset is not ApiQuery, we must create it to run generator
154 $dbSource = $this->getMain()->getModuleManager()->getModule( 'query' );
156 $generator = $dbSource->getModuleManager()->getModule( $generatorName, null, true );
157 if ( $generator === null ) {
158 $this->dieUsage( 'Unknown generator=' . $generatorName, 'badgenerator' );
160 if ( !$generator instanceof ApiQueryGeneratorBase
) {
161 $this->dieUsage( "Module $generatorName cannot be used as a generator", 'badgenerator' );
163 // Create a temporary pageset to store generator's output,
164 // add any additional fields generator may need, and execute pageset to populate titles/pageids
165 $tmpPageSet = new ApiPageSet( $dbSource, ApiPageSet
::DISABLE_GENERATORS
);
166 $generator->setGeneratorMode( $tmpPageSet );
167 $this->mCacheMode
= $generator->getCacheMode( $generator->extractRequestParams() );
170 $generator->requestExtraData( $tmpPageSet );
172 $tmpPageSet->executeInternal( $isDryRun );
174 // populate this pageset with the generator output
176 $generator->executeGenerator( $this );
177 Hooks
::run( 'APIQueryGeneratorAfterExecute', [ &$generator, &$this ] );
179 // Prevent warnings from being reported on these parameters
180 $main = $this->getMain();
181 foreach ( $generator->extractRequestParams() as $paramName => $param ) {
182 $main->markParamsUsed( $generator->encodeParamName( $paramName ) );
187 $this->resolvePendingRedirects();
190 // Only one of the titles/pageids/revids is allowed at the same time
192 if ( isset( $this->mParams
['titles'] ) ) {
193 $dataSource = 'titles';
195 if ( isset( $this->mParams
['pageids'] ) ) {
196 if ( isset( $dataSource ) ) {
197 $this->dieUsage( "Cannot use 'pageids' at the same time as '$dataSource'", 'multisource' );
199 $dataSource = 'pageids';
201 if ( isset( $this->mParams
['revids'] ) ) {
202 if ( isset( $dataSource ) ) {
203 $this->dieUsage( "Cannot use 'revids' at the same time as '$dataSource'", 'multisource' );
205 $dataSource = 'revids';
209 // Populate page information with the original user input
210 switch ( $dataSource ) {
212 $this->initFromTitles( $this->mParams
['titles'] );
215 $this->initFromPageIds( $this->mParams
['pageids'] );
218 if ( $this->mResolveRedirects
) {
219 $this->setWarning( 'Redirect resolution cannot be used ' .
220 'together with the revids= parameter. Any redirects ' .
221 'the revids= point to have not been resolved.' );
223 $this->mResolveRedirects
= false;
224 $this->initFromRevIDs( $this->mParams
['revids'] );
227 // Do nothing - some queries do not need any of the data sources.
235 * Check whether this PageSet is resolving redirects
238 public function isResolvingRedirects() {
239 return $this->mResolveRedirects
;
243 * Return the parameter name that is the source of data for this PageSet
245 * If multiple source parameters are specified (e.g. titles and pageids),
246 * one will be named arbitrarily.
248 * @return string|null
250 public function getDataSource() {
251 if ( $this->mAllowGenerator
&& isset( $this->mParams
['generator'] ) ) {
254 if ( isset( $this->mParams
['titles'] ) ) {
257 if ( isset( $this->mParams
['pageids'] ) ) {
260 if ( isset( $this->mParams
['revids'] ) ) {
268 * Request an additional field from the page table.
269 * Must be called before execute()
270 * @param string $fieldName Field name
272 public function requestField( $fieldName ) {
273 $this->mRequestedPageFields
[$fieldName] = null;
277 * Get the value of a custom field previously requested through
279 * @param string $fieldName Field name
280 * @return mixed Field value
282 public function getCustomField( $fieldName ) {
283 return $this->mRequestedPageFields
[$fieldName];
287 * Get the fields that have to be queried from the page table:
288 * the ones requested through requestField() and a few basic ones
290 * @return array Array of field names
292 public function getPageTableFields() {
293 // Ensure we get minimum required fields
294 // DON'T change this order
296 'page_namespace' => null,
297 'page_title' => null,
301 if ( $this->mResolveRedirects
) {
302 $pageFlds['page_is_redirect'] = null;
305 if ( $this->getConfig()->get( 'ContentHandlerUseDB' ) ) {
306 $pageFlds['page_content_model'] = null;
309 if ( $this->getConfig()->get( 'PageLanguageUseDB' ) ) {
310 $pageFlds['page_lang'] = null;
313 foreach ( LinkCache
::getSelectFields() as $field ) {
314 $pageFlds[$field] = null;
317 $pageFlds = array_merge( $pageFlds, $this->mRequestedPageFields
);
319 return array_keys( $pageFlds );
323 * Returns an array [ns][dbkey] => page_id for all requested titles.
324 * page_id is a unique negative number in case title was not found.
325 * Invalid titles will also have negative page IDs and will be in namespace 0
328 public function getAllTitlesByNamespace() {
329 return $this->mAllPages
;
333 * All Title objects provided.
336 public function getTitles() {
337 return $this->mTitles
;
341 * Returns the number of unique pages (not revisions) in the set.
344 public function getTitleCount() {
345 return count( $this->mTitles
);
349 * Returns an array [ns][dbkey] => page_id for all good titles.
352 public function getGoodTitlesByNamespace() {
353 return $this->mGoodPages
;
357 * Title objects that were found in the database.
358 * @return Title[] Array page_id (int) => Title (obj)
360 public function getGoodTitles() {
361 return $this->mGoodTitles
;
365 * Returns the number of found unique pages (not revisions) in the set.
368 public function getGoodTitleCount() {
369 return count( $this->mGoodTitles
);
373 * Returns an array [ns][dbkey] => fake_page_id for all missing titles.
374 * fake_page_id is a unique negative number.
377 public function getMissingTitlesByNamespace() {
378 return $this->mMissingPages
;
382 * Title objects that were NOT found in the database.
383 * The array's index will be negative for each item
386 public function getMissingTitles() {
387 return $this->mMissingTitles
;
391 * Returns an array [ns][dbkey] => page_id for all good and missing titles.
394 public function getGoodAndMissingTitlesByNamespace() {
395 return $this->mGoodAndMissingPages
;
399 * Title objects for good and missing titles.
402 public function getGoodAndMissingTitles() {
403 return $this->mGoodTitles +
$this->mMissingTitles
;
407 * Titles that were deemed invalid by Title::newFromText()
408 * The array's index will be unique and negative for each item
409 * @deprecated since 1.26, use self::getInvalidTitlesAndReasons()
410 * @return string[] Array of strings (not Title objects)
412 public function getInvalidTitles() {
413 wfDeprecated( __METHOD__
, '1.26' );
414 return array_map( function ( $t ) {
416 }, $this->mInvalidTitles
);
420 * Titles that were deemed invalid by Title::newFromText()
421 * The array's index will be unique and negative for each item
422 * @return array[] Array of arrays with 'title' and 'invalidreason' properties
424 public function getInvalidTitlesAndReasons() {
425 return $this->mInvalidTitles
;
429 * Page IDs that were not found in the database
430 * @return array Array of page IDs
432 public function getMissingPageIDs() {
433 return $this->mMissingPageIDs
;
437 * Get a list of redirect resolutions - maps a title to its redirect
438 * target, as an array of output-ready arrays
441 public function getRedirectTitles() {
442 return $this->mRedirectTitles
;
446 * Get a list of redirect resolutions - maps a title to its redirect
447 * target. Includes generator data for redirect source when available.
448 * @param ApiResult $result
449 * @return array Array of prefixed_title (string) => Title object
452 public function getRedirectTitlesAsResult( $result = null ) {
454 foreach ( $this->getRedirectTitles() as $titleStrFrom => $titleTo ) {
456 'from' => strval( $titleStrFrom ),
457 'to' => $titleTo->getPrefixedText(),
459 if ( $titleTo->hasFragment() ) {
460 $r['tofragment'] = $titleTo->getFragment();
462 if ( $titleTo->isExternal() ) {
463 $r['tointerwiki'] = $titleTo->getInterwiki();
465 if ( isset( $this->mResolvedRedirectTitles
[$titleStrFrom] ) ) {
466 $titleFrom = $this->mResolvedRedirectTitles
[$titleStrFrom];
467 $ns = $titleFrom->getNamespace();
468 $dbkey = $titleFrom->getDBkey();
469 if ( isset( $this->mGeneratorData
[$ns][$dbkey] ) ) {
470 $r = array_merge( $this->mGeneratorData
[$ns][$dbkey], $r );
476 if ( !empty( $values ) && $result ) {
477 ApiResult
::setIndexedTagName( $values, 'r' );
484 * Get a list of title normalizations - maps a title to its normalized
486 * @return array Array of raw_prefixed_title (string) => prefixed_title (string)
488 public function getNormalizedTitles() {
489 return $this->mNormalizedTitles
;
493 * Get a list of title normalizations - maps a title to its normalized
494 * version in the form of result array.
495 * @param ApiResult $result
496 * @return array Array of raw_prefixed_title (string) => prefixed_title (string)
499 public function getNormalizedTitlesAsResult( $result = null ) {
503 foreach ( $this->getNormalizedTitles() as $rawTitleStr => $titleStr ) {
504 $encode = ( $wgContLang->normalize( $rawTitleStr ) !== $rawTitleStr );
506 'fromencoded' => $encode,
507 'from' => $encode ?
rawurlencode( $rawTitleStr ) : $rawTitleStr,
511 if ( !empty( $values ) && $result ) {
512 ApiResult
::setIndexedTagName( $values, 'n' );
519 * Get a list of title conversions - maps a title to its converted
521 * @return array Array of raw_prefixed_title (string) => prefixed_title (string)
523 public function getConvertedTitles() {
524 return $this->mConvertedTitles
;
528 * Get a list of title conversions - maps a title to its converted
529 * version as a result array.
530 * @param ApiResult $result
531 * @return array Array of (from, to) strings
534 public function getConvertedTitlesAsResult( $result = null ) {
536 foreach ( $this->getConvertedTitles() as $rawTitleStr => $titleStr ) {
538 'from' => $rawTitleStr,
542 if ( !empty( $values ) && $result ) {
543 ApiResult
::setIndexedTagName( $values, 'c' );
550 * Get a list of interwiki titles - maps a title to its interwiki
552 * @return array Array of raw_prefixed_title (string) => interwiki_prefix (string)
554 public function getInterwikiTitles() {
555 return $this->mInterwikiTitles
;
559 * Get a list of interwiki titles - maps a title to its interwiki
561 * @param ApiResult $result
563 * @return array Array of raw_prefixed_title (string) => interwiki_prefix (string)
566 public function getInterwikiTitlesAsResult( $result = null, $iwUrl = false ) {
568 foreach ( $this->getInterwikiTitles() as $rawTitleStr => $interwikiStr ) {
570 'title' => $rawTitleStr,
571 'iw' => $interwikiStr,
574 $title = Title
::newFromText( $rawTitleStr );
575 $item['url'] = $title->getFullURL( '', false, PROTO_CURRENT
);
579 if ( !empty( $values ) && $result ) {
580 ApiResult
::setIndexedTagName( $values, 'i' );
587 * Get an array of invalid/special/missing titles.
589 * @param array $invalidChecks List of types of invalid titles to include.
590 * Recognized values are:
591 * - invalidTitles: Titles and reasons from $this->getInvalidTitlesAndReasons()
592 * - special: Titles from $this->getSpecialTitles()
593 * - missingIds: ids from $this->getMissingPageIDs()
594 * - missingRevIds: ids from $this->getMissingRevisionIDs()
595 * - missingTitles: Titles from $this->getMissingTitles()
596 * - interwikiTitles: Titles from $this->getInterwikiTitlesAsResult()
597 * @return array Array suitable for inclusion in the response
600 public function getInvalidTitlesAndRevisions( $invalidChecks = [ 'invalidTitles',
601 'special', 'missingIds', 'missingRevIds', 'missingTitles', 'interwikiTitles' ]
604 if ( in_array( 'invalidTitles', $invalidChecks ) ) {
605 self
::addValues( $result, $this->getInvalidTitlesAndReasons(), [ 'invalid' ] );
607 if ( in_array( 'special', $invalidChecks ) ) {
610 foreach ( $this->getSpecialTitles() as $title ) {
611 if ( $title->isKnown() ) {
617 self
::addValues( $result, $unknown, [ 'special', 'missing' ] );
618 self
::addValues( $result, $known, [ 'special' ] );
620 if ( in_array( 'missingIds', $invalidChecks ) ) {
621 self
::addValues( $result, $this->getMissingPageIDs(), [ 'missing' ], 'pageid' );
623 if ( in_array( 'missingRevIds', $invalidChecks ) ) {
624 self
::addValues( $result, $this->getMissingRevisionIDs(), [ 'missing' ], 'revid' );
626 if ( in_array( 'missingTitles', $invalidChecks ) ) {
629 foreach ( $this->getMissingTitles() as $title ) {
630 if ( $title->isKnown() ) {
636 self
::addValues( $result, $unknown, [ 'missing' ] );
637 self
::addValues( $result, $known, [ 'missing', 'known' ] );
639 if ( in_array( 'interwikiTitles', $invalidChecks ) ) {
640 self
::addValues( $result, $this->getInterwikiTitlesAsResult() );
647 * Get the list of valid revision IDs (requested with the revids= parameter)
648 * @return array Array of revID (int) => pageID (int)
650 public function getRevisionIDs() {
651 return $this->mGoodRevIDs
;
655 * Get the list of non-deleted revision IDs (requested with the revids= parameter)
656 * @return array Array of revID (int) => pageID (int)
658 public function getLiveRevisionIDs() {
659 return $this->mLiveRevIDs
;
663 * Get the list of revision IDs that were associated with deleted titles.
664 * @return array Array of revID (int) => pageID (int)
666 public function getDeletedRevisionIDs() {
667 return $this->mDeletedRevIDs
;
671 * Revision IDs that were not found in the database
672 * @return array Array of revision IDs
674 public function getMissingRevisionIDs() {
675 return $this->mMissingRevIDs
;
679 * Revision IDs that were not found in the database as result array.
680 * @param ApiResult $result
681 * @return array Array of revision IDs
684 public function getMissingRevisionIDsAsResult( $result = null ) {
686 foreach ( $this->getMissingRevisionIDs() as $revid ) {
691 if ( !empty( $values ) && $result ) {
692 ApiResult
::setIndexedTagName( $values, 'rev' );
699 * Get the list of titles with negative namespace
702 public function getSpecialTitles() {
703 return $this->mSpecialTitles
;
707 * Returns the number of revisions (requested with revids= parameter).
708 * @return int Number of revisions.
710 public function getRevisionCount() {
711 return count( $this->getRevisionIDs() );
715 * Populate this PageSet from a list of Titles
716 * @param array $titles Array of Title objects
718 public function populateFromTitles( $titles ) {
719 $this->initFromTitles( $titles );
723 * Populate this PageSet from a list of page IDs
724 * @param array $pageIDs Array of page IDs
726 public function populateFromPageIDs( $pageIDs ) {
727 $this->initFromPageIds( $pageIDs );
731 * Populate this PageSet from a rowset returned from the database
733 * Note that the query result must include the columns returned by
734 * $this->getPageTableFields().
736 * @param IDatabase $db
737 * @param ResultWrapper $queryResult Query result object
739 public function populateFromQueryResult( $db, $queryResult ) {
740 $this->initFromQueryResult( $queryResult );
744 * Populate this PageSet from a list of revision IDs
745 * @param array $revIDs Array of revision IDs
747 public function populateFromRevisionIDs( $revIDs ) {
748 $this->initFromRevIDs( $revIDs );
752 * Extract all requested fields from the row received from the database
753 * @param stdClass $row Result row
755 public function processDbRow( $row ) {
756 // Store Title object in various data structures
757 $title = Title
::newFromRow( $row );
759 LinkCache
::singleton()->addGoodLinkObjFromRow( $title, $row );
761 $pageId = intval( $row->page_id
);
762 $this->mAllPages
[$row->page_namespace
][$row->page_title
] = $pageId;
763 $this->mTitles
[] = $title;
765 if ( $this->mResolveRedirects
&& $row->page_is_redirect
== '1' ) {
766 $this->mPendingRedirectIDs
[$pageId] = $title;
768 $this->mGoodPages
[$row->page_namespace
][$row->page_title
] = $pageId;
769 $this->mGoodAndMissingPages
[$row->page_namespace
][$row->page_title
] = $pageId;
770 $this->mGoodTitles
[$pageId] = $title;
773 foreach ( $this->mRequestedPageFields
as $fieldName => &$fieldValues ) {
774 $fieldValues[$pageId] = $row->$fieldName;
779 * This method populates internal variables with page information
780 * based on the given array of title strings.
783 * #1 For each title, get data from `page` table
784 * #2 If page was not found in the DB, store it as missing
786 * Additionally, when resolving redirects:
787 * #3 If no more redirects left, stop.
788 * #4 For each redirect, get its target from the `redirect` table.
789 * #5 Substitute the original LinkBatch object with the new list
790 * #6 Repeat from step #1
792 * @param array $titles Array of Title objects or strings
794 private function initFromTitles( $titles ) {
795 // Get validated and normalized title objects
796 $linkBatch = $this->processTitlesArray( $titles );
797 if ( $linkBatch->isEmpty() ) {
801 $db = $this->getDB();
802 $set = $linkBatch->constructSet( 'page', $db );
804 // Get pageIDs data from the `page` table
805 $res = $db->select( 'page', $this->getPageTableFields(), $set,
808 // Hack: get the ns:titles stored in [ ns => [ titles ] ] format
809 $this->initFromQueryResult( $res, $linkBatch->data
, true ); // process Titles
811 // Resolve any found redirects
812 $this->resolvePendingRedirects();
816 * Does the same as initFromTitles(), but is based on page IDs instead
817 * @param array $pageids Array of page IDs
819 private function initFromPageIds( $pageids ) {
824 $pageids = array_map( 'intval', $pageids ); // paranoia
825 $remaining = array_flip( $pageids );
827 $pageids = self
::getPositiveIntegers( $pageids );
830 if ( !empty( $pageids ) ) {
832 'page_id' => $pageids
834 $db = $this->getDB();
836 // Get pageIDs data from the `page` table
837 $res = $db->select( 'page', $this->getPageTableFields(), $set,
841 $this->initFromQueryResult( $res, $remaining, false ); // process PageIDs
843 // Resolve any found redirects
844 $this->resolvePendingRedirects();
848 * Iterate through the result of the query on 'page' table,
849 * and for each row create and store title object and save any extra fields requested.
850 * @param ResultWrapper $res DB Query result
851 * @param array $remaining Array of either pageID or ns/title elements (optional).
852 * If given, any missing items will go to $mMissingPageIDs and $mMissingTitles
853 * @param bool $processTitles Must be provided together with $remaining.
854 * If true, treat $remaining as an array of [ns][title]
855 * If false, treat it as an array of [pageIDs]
857 private function initFromQueryResult( $res, &$remaining = null, $processTitles = null ) {
858 if ( !is_null( $remaining ) && is_null( $processTitles ) ) {
859 ApiBase
::dieDebug( __METHOD__
, 'Missing $processTitles parameter when $remaining is provided' );
864 foreach ( $res as $row ) {
865 $pageId = intval( $row->page_id
);
867 // Remove found page from the list of remaining items
868 if ( isset( $remaining ) ) {
869 if ( $processTitles ) {
870 unset( $remaining[$row->page_namespace
][$row->page_title
] );
872 unset( $remaining[$pageId] );
876 // Store any extra fields requested by modules
877 $this->processDbRow( $row );
879 // Need gender information
880 if ( MWNamespace
::hasGenderDistinction( $row->page_namespace
) ) {
881 $usernames[] = $row->page_title
;
886 if ( isset( $remaining ) ) {
887 // Any items left in the $remaining list are added as missing
888 if ( $processTitles ) {
889 // The remaining titles in $remaining are non-existent pages
890 $linkCache = LinkCache
::singleton();
891 foreach ( $remaining as $ns => $dbkeys ) {
892 foreach ( array_keys( $dbkeys ) as $dbkey ) {
893 $title = Title
::makeTitle( $ns, $dbkey );
894 $linkCache->addBadLinkObj( $title );
895 $this->mAllPages
[$ns][$dbkey] = $this->mFakePageId
;
896 $this->mMissingPages
[$ns][$dbkey] = $this->mFakePageId
;
897 $this->mGoodAndMissingPages
[$ns][$dbkey] = $this->mFakePageId
;
898 $this->mMissingTitles
[$this->mFakePageId
] = $title;
899 $this->mFakePageId
--;
900 $this->mTitles
[] = $title;
902 // need gender information
903 if ( MWNamespace
::hasGenderDistinction( $ns ) ) {
904 $usernames[] = $dbkey;
909 // The remaining pageids do not exist
910 if ( !$this->mMissingPageIDs
) {
911 $this->mMissingPageIDs
= array_keys( $remaining );
913 $this->mMissingPageIDs
= array_merge( $this->mMissingPageIDs
, array_keys( $remaining ) );
918 // Get gender information
919 $genderCache = MediaWikiServices
::getInstance()->getGenderCache();
920 $genderCache->doQuery( $usernames, __METHOD__
);
924 * Does the same as initFromTitles(), but is based on revision IDs
926 * @param array $revids Array of revision IDs
928 private function initFromRevIDs( $revids ) {
933 $revids = array_map( 'intval', $revids ); // paranoia
934 $db = $this->getDB();
936 $remaining = array_flip( $revids );
938 $revids = self
::getPositiveIntegers( $revids );
940 if ( !empty( $revids ) ) {
941 $tables = [ 'revision', 'page' ];
942 $fields = [ 'rev_id', 'rev_page' ];
943 $where = [ 'rev_id' => $revids, 'rev_page = page_id' ];
945 // Get pageIDs data from the `page` table
946 $res = $db->select( $tables, $fields, $where, __METHOD__
);
947 foreach ( $res as $row ) {
948 $revid = intval( $row->rev_id
);
949 $pageid = intval( $row->rev_page
);
950 $this->mGoodRevIDs
[$revid] = $pageid;
951 $this->mLiveRevIDs
[$revid] = $pageid;
952 $pageids[$pageid] = '';
953 unset( $remaining[$revid] );
957 $this->mMissingRevIDs
= array_keys( $remaining );
959 // Populate all the page information
960 $this->initFromPageIds( array_keys( $pageids ) );
962 // If the user can see deleted revisions, pull out the corresponding
963 // titles from the archive table and include them too. We ignore
964 // ar_page_id because deleted revisions are tied by title, not page_id.
965 if ( !empty( $this->mMissingRevIDs
) && $this->getUser()->isAllowed( 'deletedhistory' ) ) {
966 $remaining = array_flip( $this->mMissingRevIDs
);
967 $tables = [ 'archive' ];
968 $fields = [ 'ar_rev_id', 'ar_namespace', 'ar_title' ];
969 $where = [ 'ar_rev_id' => $this->mMissingRevIDs
];
971 $res = $db->select( $tables, $fields, $where, __METHOD__
);
973 foreach ( $res as $row ) {
974 $revid = intval( $row->ar_rev_id
);
975 $titles[$revid] = Title
::makeTitle( $row->ar_namespace
, $row->ar_title
);
976 unset( $remaining[$revid] );
979 $this->initFromTitles( $titles );
981 foreach ( $titles as $revid => $title ) {
982 $ns = $title->getNamespace();
983 $dbkey = $title->getDBkey();
985 // Handle converted titles
986 if ( !isset( $this->mAllPages
[$ns][$dbkey] ) &&
987 isset( $this->mConvertedTitles
[$title->getPrefixedText()] )
989 $title = Title
::newFromText( $this->mConvertedTitles
[$title->getPrefixedText()] );
990 $ns = $title->getNamespace();
991 $dbkey = $title->getDBkey();
994 if ( isset( $this->mAllPages
[$ns][$dbkey] ) ) {
995 $this->mGoodRevIDs
[$revid] = $this->mAllPages
[$ns][$dbkey];
996 $this->mDeletedRevIDs
[$revid] = $this->mAllPages
[$ns][$dbkey];
998 $remaining[$revid] = true;
1002 $this->mMissingRevIDs
= array_keys( $remaining );
1007 * Resolve any redirects in the result if redirect resolution was
1008 * requested. This function is called repeatedly until all redirects
1009 * have been resolved.
1011 private function resolvePendingRedirects() {
1012 if ( $this->mResolveRedirects
) {
1013 $db = $this->getDB();
1014 $pageFlds = $this->getPageTableFields();
1016 // Repeat until all redirects have been resolved
1017 // The infinite loop is prevented by keeping all known pages in $this->mAllPages
1018 while ( $this->mPendingRedirectIDs
) {
1019 // Resolve redirects by querying the pagelinks table, and repeat the process
1020 // Create a new linkBatch object for the next pass
1021 $linkBatch = $this->getRedirectTargets();
1023 if ( $linkBatch->isEmpty() ) {
1027 $set = $linkBatch->constructSet( 'page', $db );
1028 if ( $set === false ) {
1032 // Get pageIDs data from the `page` table
1033 $res = $db->select( 'page', $pageFlds, $set, __METHOD__
);
1035 // Hack: get the ns:titles stored in [ns => array(titles)] format
1036 $this->initFromQueryResult( $res, $linkBatch->data
, true );
1042 * Get the targets of the pending redirects from the database
1044 * Also creates entries in the redirect table for redirects that don't
1048 private function getRedirectTargets() {
1049 $lb = new LinkBatch();
1050 $db = $this->getDB();
1060 ], [ 'rd_from' => array_keys( $this->mPendingRedirectIDs
) ],
1063 foreach ( $res as $row ) {
1064 $rdfrom = intval( $row->rd_from
);
1065 $from = $this->mPendingRedirectIDs
[$rdfrom]->getPrefixedText();
1066 $to = Title
::makeTitle(
1072 $this->mResolvedRedirectTitles
[$from] = $this->mPendingRedirectIDs
[$rdfrom];
1073 unset( $this->mPendingRedirectIDs
[$rdfrom] );
1074 if ( $to->isExternal() ) {
1075 $this->mInterwikiTitles
[$to->getPrefixedText()] = $to->getInterwiki();
1076 } elseif ( !isset( $this->mAllPages
[$row->rd_namespace
][$row->rd_title
] ) ) {
1077 $lb->add( $row->rd_namespace
, $row->rd_title
);
1079 $this->mRedirectTitles
[$from] = $to;
1082 if ( $this->mPendingRedirectIDs
) {
1083 // We found pages that aren't in the redirect table
1085 foreach ( $this->mPendingRedirectIDs
as $id => $title ) {
1086 $page = WikiPage
::factory( $title );
1087 $rt = $page->insertRedirect();
1089 // What the hell. Let's just ignore this
1093 $from = $title->getPrefixedText();
1094 $this->mResolvedRedirectTitles
[$from] = $title;
1095 $this->mRedirectTitles
[$from] = $rt;
1096 unset( $this->mPendingRedirectIDs
[$id] );
1104 * Get the cache mode for the data generated by this module.
1105 * All PageSet users should take into account whether this returns a more-restrictive
1106 * cache mode than the using module itself. For possible return values and other
1107 * details about cache modes, see ApiMain::setCacheMode()
1109 * Public caching will only be allowed if *all* the modules that supply
1110 * data for a given request return a cache mode of public.
1112 * @param array|null $params
1116 public function getCacheMode( $params = null ) {
1117 return $this->mCacheMode
;
1121 * Given an array of title strings, convert them into Title objects.
1122 * Alternatively, an array of Title objects may be given.
1123 * This method validates access rights for the title,
1124 * and appends normalization values to the output.
1126 * @param array $titles Array of Title objects or strings
1129 private function processTitlesArray( $titles ) {
1131 $linkBatch = new LinkBatch();
1133 foreach ( $titles as $title ) {
1134 if ( is_string( $title ) ) {
1136 $titleObj = Title
::newFromTextThrow( $title, $this->mDefaultNamespace
);
1137 } catch ( MalformedTitleException
$ex ) {
1138 // Handle invalid titles gracefully
1139 $this->mAllPages
[0][$title] = $this->mFakePageId
;
1140 $this->mInvalidTitles
[$this->mFakePageId
] = [
1142 'invalidreason' => $ex->getMessage(),
1144 $this->mFakePageId
--;
1145 continue; // There's nothing else we can do
1150 $unconvertedTitle = $titleObj->getPrefixedText();
1151 $titleWasConverted = false;
1152 if ( $titleObj->isExternal() ) {
1153 // This title is an interwiki link.
1154 $this->mInterwikiTitles
[$unconvertedTitle] = $titleObj->getInterwiki();
1156 // Variants checking
1158 if ( $this->mConvertTitles
&&
1159 count( $wgContLang->getVariants() ) > 1 &&
1160 !$titleObj->exists()
1162 // Language::findVariantLink will modify titleText and titleObj into
1163 // the canonical variant if possible
1164 $titleText = is_string( $title ) ?
$title : $titleObj->getPrefixedText();
1165 $wgContLang->findVariantLink( $titleText, $titleObj );
1166 $titleWasConverted = $unconvertedTitle !== $titleObj->getPrefixedText();
1169 if ( $titleObj->getNamespace() < 0 ) {
1170 // Handle Special and Media pages
1171 $titleObj = $titleObj->fixSpecialName();
1172 $this->mSpecialTitles
[$this->mFakePageId
] = $titleObj;
1173 $this->mFakePageId
--;
1176 $linkBatch->addObj( $titleObj );
1180 // Make sure we remember the original title that was
1181 // given to us. This way the caller can correlate new
1182 // titles with the originally requested when e.g. the
1183 // namespace is localized or the capitalization is
1185 if ( $titleWasConverted ) {
1186 $this->mConvertedTitles
[$unconvertedTitle] = $titleObj->getPrefixedText();
1187 // In this case the page can't be Special.
1188 if ( is_string( $title ) && $title !== $unconvertedTitle ) {
1189 $this->mNormalizedTitles
[$title] = $unconvertedTitle;
1191 } elseif ( is_string( $title ) && $title !== $titleObj->getPrefixedText() ) {
1192 $this->mNormalizedTitles
[$title] = $titleObj->getPrefixedText();
1195 // Need gender information
1196 if ( MWNamespace
::hasGenderDistinction( $titleObj->getNamespace() ) ) {
1197 $usernames[] = $titleObj->getText();
1200 // Get gender information
1201 $genderCache = MediaWikiServices
::getInstance()->getGenderCache();
1202 $genderCache->doQuery( $usernames, __METHOD__
);
1208 * Set data for a title.
1210 * This data may be extracted into an ApiResult using
1211 * self::populateGeneratorData. This should generally be limited to
1212 * data that is likely to be particularly useful to end users rather than
1213 * just being a dump of everything returned in non-generator mode.
1215 * Redirects here will *not* be followed, even if 'redirects' was
1216 * specified, since in the case of multiple redirects we can't know which
1217 * source's data to use on the target.
1219 * @param Title $title
1220 * @param array $data
1222 public function setGeneratorData( Title
$title, array $data ) {
1223 $ns = $title->getNamespace();
1224 $dbkey = $title->getDBkey();
1225 $this->mGeneratorData
[$ns][$dbkey] = $data;
1229 * Controls how generator data about a redirect source is merged into
1230 * the generator data for the redirect target. When not set no data
1231 * is merged. Note that if multiple titles redirect to the same target
1232 * the order of operations is undefined.
1234 * Example to include generated data from redirect in target, prefering
1235 * the data generated for the destination when there is a collision:
1237 * $pageSet->setRedirectMergePolicy( function( array $current, array $new ) {
1238 * return $current + $new;
1242 * @param callable|null $callable Recieves two array arguments, first the
1243 * generator data for the redirect target and second the generator data
1244 * for the redirect source. Returns the resulting generator data to use
1245 * for the redirect target.
1247 public function setRedirectMergePolicy( $callable ) {
1248 $this->mRedirectMergePolicy
= $callable;
1252 * Populate the generator data for all titles in the result
1254 * The page data may be inserted into an ApiResult object or into an
1255 * associative array. The $path parameter specifies the path within the
1256 * ApiResult or array to find the "pages" node.
1258 * The "pages" node itself must be an associative array mapping the page ID
1259 * or fake page ID values returned by this pageset (see
1260 * self::getAllTitlesByNamespace() and self::getSpecialTitles()) to
1261 * associative arrays of page data. Each of those subarrays will have the
1262 * data from self::setGeneratorData() merged in.
1264 * Data that was set by self::setGeneratorData() for pages not in the
1265 * "pages" node will be ignored.
1267 * @param ApiResult|array &$result
1268 * @param array $path
1269 * @return bool Whether the data fit
1271 public function populateGeneratorData( &$result, array $path = [] ) {
1272 if ( $result instanceof ApiResult
) {
1273 $data = $result->getResultData( $path );
1274 if ( $data === null ) {
1279 foreach ( $path as $key ) {
1280 if ( !isset( $data[$key] ) ) {
1281 // Path isn't in $result, so nothing to add, so everything
1285 $data = &$data[$key];
1288 foreach ( $this->mGeneratorData
as $ns => $dbkeys ) {
1291 foreach ( $this->mSpecialTitles
as $id => $title ) {
1292 $pages[$title->getDBkey()] = $id;
1295 if ( !isset( $this->mAllPages
[$ns] ) ) {
1296 // No known titles in the whole namespace. Skip it.
1299 $pages = $this->mAllPages
[$ns];
1301 foreach ( $dbkeys as $dbkey => $genData ) {
1302 if ( !isset( $pages[$dbkey] ) ) {
1303 // Unknown title. Forget it.
1306 $pageId = $pages[$dbkey];
1307 if ( !isset( $data[$pageId] ) ) {
1308 // $pageId didn't make it into the result. Ignore it.
1312 if ( $result instanceof ApiResult
) {
1313 $path2 = array_merge( $path, [ $pageId ] );
1314 foreach ( $genData as $key => $value ) {
1315 if ( !$result->addValue( $path2, $key, $value ) ) {
1320 $data[$pageId] = array_merge( $data[$pageId], $genData );
1325 // Merge data generated about redirect titles into the redirect destination
1326 if ( $this->mRedirectMergePolicy
) {
1327 foreach ( $this->mResolvedRedirectTitles
as $titleFrom ) {
1329 while ( isset( $this->mRedirectTitles
[$dest->getPrefixedText()] ) ) {
1330 $dest = $this->mRedirectTitles
[$dest->getPrefixedText()];
1332 $fromNs = $titleFrom->getNamespace();
1333 $fromDBkey = $titleFrom->getDBkey();
1334 $toPageId = $dest->getArticleID();
1335 if ( isset( $data[$toPageId] ) &&
1336 isset( $this->mGeneratorData
[$fromNs][$fromDBkey] )
1338 // It is necesary to set both $data and add to $result, if an ApiResult,
1339 // to ensure multiple redirects to the same destination are all merged.
1340 $data[$toPageId] = call_user_func(
1341 $this->mRedirectMergePolicy
,
1343 $this->mGeneratorData
[$fromNs][$fromDBkey]
1345 if ( $result instanceof ApiResult
) {
1346 if ( !$result->addValue( $path, $toPageId, $data[$toPageId], ApiResult
::OVERRIDE
) ) {
1358 * Get the database connection (read-only)
1361 protected function getDB() {
1362 return $this->mDbSource
->getDB();
1366 * Returns the input array of integers with all values < 0 removed
1368 * @param array $array
1371 private static function getPositiveIntegers( $array ) {
1372 // bug 25734 API: possible issue with revids validation
1373 // It seems with a load of revision rows, MySQL gets upset
1374 // Remove any < 0 integers, as they can't be valid
1375 foreach ( $array as $i => $int ) {
1377 unset( $array[$i] );
1384 public function getAllowedParams( $flags = 0 ) {
1387 ApiBase
::PARAM_ISMULTI
=> true,
1388 ApiBase
::PARAM_HELP_MSG
=> 'api-pageset-param-titles',
1391 ApiBase
::PARAM_TYPE
=> 'integer',
1392 ApiBase
::PARAM_ISMULTI
=> true,
1393 ApiBase
::PARAM_HELP_MSG
=> 'api-pageset-param-pageids',
1396 ApiBase
::PARAM_TYPE
=> 'integer',
1397 ApiBase
::PARAM_ISMULTI
=> true,
1398 ApiBase
::PARAM_HELP_MSG
=> 'api-pageset-param-revids',
1401 ApiBase
::PARAM_TYPE
=> null,
1402 ApiBase
::PARAM_HELP_MSG
=> 'api-pageset-param-generator',
1403 ApiBase
::PARAM_SUBMODULE_PARAM_PREFIX
=> 'g',
1406 ApiBase
::PARAM_DFLT
=> false,
1407 ApiBase
::PARAM_HELP_MSG
=> $this->mAllowGenerator
1408 ?
'api-pageset-param-redirects-generator'
1409 : 'api-pageset-param-redirects-nogenerator',
1411 'converttitles' => [
1412 ApiBase
::PARAM_DFLT
=> false,
1413 ApiBase
::PARAM_HELP_MSG
=> [
1414 'api-pageset-param-converttitles',
1415 new DeferredStringifier(
1416 function ( IContextSource
$context ) {
1417 return $context->getLanguage()
1418 ->commaList( LanguageConverter
::$languagesWithVariants );
1426 if ( !$this->mAllowGenerator
) {
1427 unset( $result['generator'] );
1428 } elseif ( $flags & ApiBase
::GET_VALUES_FOR_HELP
) {
1429 $result['generator'][ApiBase
::PARAM_TYPE
] = 'submodule';
1430 $result['generator'][ApiBase
::PARAM_SUBMODULE_MAP
] = $this->getGenerators();
1436 protected function handleParamNormalization( $paramName, $value, $rawValue ) {
1437 parent
::handleParamNormalization( $paramName, $value, $rawValue );
1439 if ( $paramName === 'titles' ) {
1440 // For the 'titles' parameter, we want to split it like ApiBase would
1441 // and add any changed titles to $this->mNormalizedTitles
1442 $value = $this->explodeMultiValue( $value, self
::LIMIT_SML2 +
1 );
1443 $l = count( $value );
1444 $rawValue = $this->explodeMultiValue( $rawValue, $l );
1445 for ( $i = 0; $i < $l; $i++
) {
1446 if ( $value[$i] !== $rawValue[$i] ) {
1447 $this->mNormalizedTitles
[$rawValue[$i]] = $value[$i];
1453 private static $generators = null;
1456 * Get an array of all available generators
1459 private function getGenerators() {
1460 if ( self
::$generators === null ) {
1461 $query = $this->mDbSource
;
1462 if ( !( $query instanceof ApiQuery
) ) {
1463 // If the parent container of this pageset is not ApiQuery,
1464 // we must create it to get module manager
1465 $query = $this->getMain()->getModuleManager()->getModule( 'query' );
1468 $prefix = $query->getModulePath() . '+';
1469 $mgr = $query->getModuleManager();
1470 foreach ( $mgr->getNamesWithClasses() as $name => $class ) {
1471 if ( is_subclass_of( $class, 'ApiQueryGeneratorBase' ) ) {
1472 $gens[$name] = $prefix . $name;
1476 self
::$generators = $gens;
1479 return self
::$generators;