5 * Created on Sep 24, 2006
7 * Copyright © 2006, 2013 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * http://www.gnu.org/copyleft/gpl.html
28 * This class contains a list of pages that the client has requested.
29 * Initially, when the client passes in titles=, pageids=, or revisions=
30 * parameter, an instance of the ApiPageSet class will normalize titles,
31 * determine if the pages/revisions exist, and prefetch any additional page
34 * When a generator is used, the result of the generator will become the input
35 * for the second instance of this class, and all subsequent actions will use
36 * the second instance for all their work.
39 * @since 1.21 derives from ApiBase instead of ApiQueryBase
41 class ApiPageSet
extends ApiBase
{
43 * Constructor flag: The new instance of ApiPageSet will ignore the 'generator=' parameter
46 const DISABLE_GENERATORS
= 1;
50 private $mResolveRedirects;
51 private $mConvertTitles;
52 private $mAllowGenerator;
54 private $mAllPages = array(); // [ns][dbkey] => page_id or negative when missing
55 private $mTitles = array();
56 private $mGoodAndMissingPages = array(); // [ns][dbkey] => page_id or negative when missing
57 private $mGoodPages = array(); // [ns][dbkey] => page_id
58 private $mGoodTitles = array();
59 private $mMissingPages = array(); // [ns][dbkey] => fake page_id
60 private $mMissingTitles = array();
61 private $mInvalidTitles = array(); // [fake_page_id] => array( 'title' => $title, 'invalidreason' => $reason )
62 private $mMissingPageIDs = array();
63 private $mRedirectTitles = array();
64 private $mSpecialTitles = array();
65 private $mNormalizedTitles = array();
66 private $mInterwikiTitles = array();
68 private $mPendingRedirectIDs = array();
69 private $mResolvedRedirectTitles = array();
70 private $mConvertedTitles = array();
71 private $mGoodRevIDs = array();
72 private $mLiveRevIDs = array();
73 private $mDeletedRevIDs = array();
74 private $mMissingRevIDs = array();
75 private $mGeneratorData = array(); // [ns][dbkey] => data array
76 private $mFakePageId = -1;
77 private $mCacheMode = 'public';
78 private $mRequestedPageFields = array();
80 private $mDefaultNamespace = NS_MAIN
;
83 * Add all items from $values into the result
84 * @param array $result Output
85 * @param array $values Values to add
86 * @param string $flag The name of the boolean flag to mark this element
87 * @param string $name If given, name of the value
89 private static function addValues( array &$result, $values, $flag = null, $name = null ) {
90 foreach ( $values as $val ) {
91 if ( $val instanceof Title
) {
93 ApiQueryBase
::addTitleInfo( $v, $val );
94 } elseif ( $name !== null ) {
95 $v = array( $name => $val );
99 if ( $flag !== null ) {
107 * @param ApiBase $dbSource Module implementing getDB().
108 * Allows PageSet to reuse existing db connection from the shared state like ApiQuery.
109 * @param int $flags Zero or more flags like DISABLE_GENERATORS
110 * @param int $defaultNamespace The namespace to use if none is specified by a prefix.
111 * @since 1.21 accepts $flags instead of two boolean values
113 public function __construct( ApiBase
$dbSource, $flags = 0, $defaultNamespace = NS_MAIN
) {
114 parent
::__construct( $dbSource->getMain(), $dbSource->getModuleName() );
115 $this->mDbSource
= $dbSource;
116 $this->mAllowGenerator
= ( $flags & ApiPageSet
::DISABLE_GENERATORS
) == 0;
117 $this->mDefaultNamespace
= $defaultNamespace;
119 $this->mParams
= $this->extractRequestParams();
120 $this->mResolveRedirects
= $this->mParams
['redirects'];
121 $this->mConvertTitles
= $this->mParams
['converttitles'];
125 * In case execute() is not called, call this method to mark all relevant parameters as used
126 * This prevents unused parameters from being reported as warnings
128 public function executeDryRun() {
129 $this->executeInternal( true );
133 * Populate the PageSet from the request parameters.
135 public function execute() {
136 $this->executeInternal( false );
140 * Populate the PageSet from the request parameters.
141 * @param bool $isDryRun If true, instantiates generator, but only to mark
142 * relevant parameters as used
144 private function executeInternal( $isDryRun ) {
145 $generatorName = $this->mAllowGenerator ?
$this->mParams
['generator'] : null;
146 if ( isset( $generatorName ) ) {
147 $dbSource = $this->mDbSource
;
148 if ( !$dbSource instanceof ApiQuery
) {
149 // If the parent container of this pageset is not ApiQuery, we must create it to run generator
150 $dbSource = $this->getMain()->getModuleManager()->getModule( 'query' );
152 $generator = $dbSource->getModuleManager()->getModule( $generatorName, null, true );
153 if ( $generator === null ) {
154 $this->dieUsage( 'Unknown generator=' . $generatorName, 'badgenerator' );
156 if ( !$generator instanceof ApiQueryGeneratorBase
) {
157 $this->dieUsage( "Module $generatorName cannot be used as a generator", 'badgenerator' );
159 // Create a temporary pageset to store generator's output,
160 // add any additional fields generator may need, and execute pageset to populate titles/pageids
161 $tmpPageSet = new ApiPageSet( $dbSource, ApiPageSet
::DISABLE_GENERATORS
);
162 $generator->setGeneratorMode( $tmpPageSet );
163 $this->mCacheMode
= $generator->getCacheMode( $generator->extractRequestParams() );
166 $generator->requestExtraData( $tmpPageSet );
168 $tmpPageSet->executeInternal( $isDryRun );
170 // populate this pageset with the generator output
172 $generator->executeGenerator( $this );
173 Hooks
::run( 'APIQueryGeneratorAfterExecute', array( &$generator, &$this ) );
175 // Prevent warnings from being reported on these parameters
176 $main = $this->getMain();
177 foreach ( $generator->extractRequestParams() as $paramName => $param ) {
178 $main->getVal( $generator->encodeParamName( $paramName ) );
183 $this->resolvePendingRedirects();
186 // Only one of the titles/pageids/revids is allowed at the same time
188 if ( isset( $this->mParams
['titles'] ) ) {
189 $dataSource = 'titles';
191 if ( isset( $this->mParams
['pageids'] ) ) {
192 if ( isset( $dataSource ) ) {
193 $this->dieUsage( "Cannot use 'pageids' at the same time as '$dataSource'", 'multisource' );
195 $dataSource = 'pageids';
197 if ( isset( $this->mParams
['revids'] ) ) {
198 if ( isset( $dataSource ) ) {
199 $this->dieUsage( "Cannot use 'revids' at the same time as '$dataSource'", 'multisource' );
201 $dataSource = 'revids';
205 // Populate page information with the original user input
206 switch ( $dataSource ) {
208 $this->initFromTitles( $this->mParams
['titles'] );
211 $this->initFromPageIds( $this->mParams
['pageids'] );
214 if ( $this->mResolveRedirects
) {
215 $this->setWarning( 'Redirect resolution cannot be used ' .
216 'together with the revids= parameter. Any redirects ' .
217 'the revids= point to have not been resolved.' );
219 $this->mResolveRedirects
= false;
220 $this->initFromRevIDs( $this->mParams
['revids'] );
223 // Do nothing - some queries do not need any of the data sources.
231 * Check whether this PageSet is resolving redirects
234 public function isResolvingRedirects() {
235 return $this->mResolveRedirects
;
239 * Return the parameter name that is the source of data for this PageSet
241 * If multiple source parameters are specified (e.g. titles and pageids),
242 * one will be named arbitrarily.
244 * @return string|null
246 public function getDataSource() {
247 if ( $this->mAllowGenerator
&& isset( $this->mParams
['generator'] ) ) {
250 if ( isset( $this->mParams
['titles'] ) ) {
253 if ( isset( $this->mParams
['pageids'] ) ) {
256 if ( isset( $this->mParams
['revids'] ) ) {
264 * Request an additional field from the page table.
265 * Must be called before execute()
266 * @param string $fieldName Field name
268 public function requestField( $fieldName ) {
269 $this->mRequestedPageFields
[$fieldName] = null;
273 * Get the value of a custom field previously requested through
275 * @param string $fieldName Field name
276 * @return mixed Field value
278 public function getCustomField( $fieldName ) {
279 return $this->mRequestedPageFields
[$fieldName];
283 * Get the fields that have to be queried from the page table:
284 * the ones requested through requestField() and a few basic ones
286 * @return array Array of field names
288 public function getPageTableFields() {
289 // Ensure we get minimum required fields
290 // DON'T change this order
292 'page_namespace' => null,
293 'page_title' => null,
297 if ( $this->mResolveRedirects
) {
298 $pageFlds['page_is_redirect'] = null;
301 if ( $this->getConfig()->get( 'ContentHandlerUseDB' ) ) {
302 $pageFlds['page_content_model'] = null;
305 // only store non-default fields
306 $this->mRequestedPageFields
= array_diff_key( $this->mRequestedPageFields
, $pageFlds );
308 $pageFlds = array_merge( $pageFlds, $this->mRequestedPageFields
);
310 return array_keys( $pageFlds );
314 * Returns an array [ns][dbkey] => page_id for all requested titles.
315 * page_id is a unique negative number in case title was not found.
316 * Invalid titles will also have negative page IDs and will be in namespace 0
319 public function getAllTitlesByNamespace() {
320 return $this->mAllPages
;
324 * All Title objects provided.
327 public function getTitles() {
328 return $this->mTitles
;
332 * Returns the number of unique pages (not revisions) in the set.
335 public function getTitleCount() {
336 return count( $this->mTitles
);
340 * Returns an array [ns][dbkey] => page_id for all good titles.
343 public function getGoodTitlesByNamespace() {
344 return $this->mGoodPages
;
348 * Title objects that were found in the database.
349 * @return Title[] Array page_id (int) => Title (obj)
351 public function getGoodTitles() {
352 return $this->mGoodTitles
;
356 * Returns the number of found unique pages (not revisions) in the set.
359 public function getGoodTitleCount() {
360 return count( $this->mGoodTitles
);
364 * Returns an array [ns][dbkey] => fake_page_id for all missing titles.
365 * fake_page_id is a unique negative number.
368 public function getMissingTitlesByNamespace() {
369 return $this->mMissingPages
;
373 * Title objects that were NOT found in the database.
374 * The array's index will be negative for each item
377 public function getMissingTitles() {
378 return $this->mMissingTitles
;
382 * Returns an array [ns][dbkey] => page_id for all good and missing titles.
385 public function getGoodAndMissingTitlesByNamespace() {
386 return $this->mGoodAndMissingPages
;
390 * Title objects for good and missing titles.
393 public function getGoodAndMissingTitles() {
394 return $this->mGoodTitles +
$this->mMissingTitles
;
398 * Titles that were deemed invalid by Title::newFromText()
399 * The array's index will be unique and negative for each item
400 * @deprecated since 1.26, use self::getInvalidTitlesAndReasons()
401 * @return string[] Array of strings (not Title objects)
403 public function getInvalidTitles() {
404 wfDeprecated( __METHOD__
, '1.26' );
405 return array_map( function ( $t ) {
407 }, $this->mInvalidTitles
);
411 * Titles that were deemed invalid by Title::newFromText()
412 * The array's index will be unique and negative for each item
413 * @return array[] Array of arrays with 'title' and 'invalidreason' properties
415 public function getInvalidTitlesAndReasons() {
416 return $this->mInvalidTitles
;
420 * Page IDs that were not found in the database
421 * @return array Array of page IDs
423 public function getMissingPageIDs() {
424 return $this->mMissingPageIDs
;
428 * Get a list of redirect resolutions - maps a title to its redirect
429 * target, as an array of output-ready arrays
432 public function getRedirectTitles() {
433 return $this->mRedirectTitles
;
437 * Get a list of redirect resolutions - maps a title to its redirect
438 * target. Includes generator data for redirect source when available.
439 * @param ApiResult $result
440 * @return array Array of prefixed_title (string) => Title object
443 public function getRedirectTitlesAsResult( $result = null ) {
445 foreach ( $this->getRedirectTitles() as $titleStrFrom => $titleTo ) {
447 'from' => strval( $titleStrFrom ),
448 'to' => $titleTo->getPrefixedText(),
450 if ( $titleTo->hasFragment() ) {
451 $r['tofragment'] = $titleTo->getFragment();
453 if ( $titleTo->isExternal() ) {
454 $r['tointerwiki'] = $titleTo->getInterwiki();
456 if ( isset( $this->mResolvedRedirectTitles
[$titleStrFrom] ) ) {
457 $titleFrom = $this->mResolvedRedirectTitles
[$titleStrFrom];
458 $ns = $titleFrom->getNamespace();
459 $dbkey = $titleFrom->getDBkey();
460 if ( isset( $this->mGeneratorData
[$ns][$dbkey] ) ) {
461 $r = array_merge( $this->mGeneratorData
[$ns][$dbkey], $r );
467 if ( !empty( $values ) && $result ) {
468 ApiResult
::setIndexedTagName( $values, 'r' );
475 * Get a list of title normalizations - maps a title to its normalized
477 * @return array Array of raw_prefixed_title (string) => prefixed_title (string)
479 public function getNormalizedTitles() {
480 return $this->mNormalizedTitles
;
484 * Get a list of title normalizations - maps a title to its normalized
485 * version in the form of result array.
486 * @param ApiResult $result
487 * @return array Array of raw_prefixed_title (string) => prefixed_title (string)
490 public function getNormalizedTitlesAsResult( $result = null ) {
492 foreach ( $this->getNormalizedTitles() as $rawTitleStr => $titleStr ) {
494 'from' => $rawTitleStr,
498 if ( !empty( $values ) && $result ) {
499 ApiResult
::setIndexedTagName( $values, 'n' );
506 * Get a list of title conversions - maps a title to its converted
508 * @return array Array of raw_prefixed_title (string) => prefixed_title (string)
510 public function getConvertedTitles() {
511 return $this->mConvertedTitles
;
515 * Get a list of title conversions - maps a title to its converted
516 * version as a result array.
517 * @param ApiResult $result
518 * @return array Array of (from, to) strings
521 public function getConvertedTitlesAsResult( $result = null ) {
523 foreach ( $this->getConvertedTitles() as $rawTitleStr => $titleStr ) {
525 'from' => $rawTitleStr,
529 if ( !empty( $values ) && $result ) {
530 ApiResult
::setIndexedTagName( $values, 'c' );
537 * Get a list of interwiki titles - maps a title to its interwiki
539 * @return array Array of raw_prefixed_title (string) => interwiki_prefix (string)
541 public function getInterwikiTitles() {
542 return $this->mInterwikiTitles
;
546 * Get a list of interwiki titles - maps a title to its interwiki
548 * @param ApiResult $result
550 * @return array Array of raw_prefixed_title (string) => interwiki_prefix (string)
553 public function getInterwikiTitlesAsResult( $result = null, $iwUrl = false ) {
555 foreach ( $this->getInterwikiTitles() as $rawTitleStr => $interwikiStr ) {
557 'title' => $rawTitleStr,
558 'iw' => $interwikiStr,
561 $title = Title
::newFromText( $rawTitleStr );
562 $item['url'] = $title->getFullURL( '', false, PROTO_CURRENT
);
566 if ( !empty( $values ) && $result ) {
567 ApiResult
::setIndexedTagName( $values, 'i' );
574 * Get an array of invalid/special/missing titles.
576 * @param array $invalidChecks List of types of invalid titles to include.
577 * Recognized values are:
578 * - invalidTitles: Titles and reasons from $this->getInvalidTitlesAndReasons()
579 * - special: Titles from $this->getSpecialTitles()
580 * - missingIds: ids from $this->getMissingPageIDs()
581 * - missingRevIds: ids from $this->getMissingRevisionIDs()
582 * - missingTitles: Titles from $this->getMissingTitles()
583 * - interwikiTitles: Titles from $this->getInterwikiTitlesAsResult()
584 * @return array Array suitable for inclusion in the response
587 public function getInvalidTitlesAndRevisions( $invalidChecks = array( 'invalidTitles',
588 'special', 'missingIds', 'missingRevIds', 'missingTitles', 'interwikiTitles' )
591 if ( in_array( "invalidTitles", $invalidChecks ) ) {
592 self
::addValues( $result, $this->getInvalidTitlesAndReasons(), 'invalid' );
594 if ( in_array( "special", $invalidChecks ) ) {
595 self
::addValues( $result, $this->getSpecialTitles(), 'special', 'title' );
597 if ( in_array( "missingIds", $invalidChecks ) ) {
598 self
::addValues( $result, $this->getMissingPageIDs(), 'missing', 'pageid' );
600 if ( in_array( "missingRevIds", $invalidChecks ) ) {
601 self
::addValues( $result, $this->getMissingRevisionIDs(), 'missing', 'revid' );
603 if ( in_array( "missingTitles", $invalidChecks ) ) {
604 self
::addValues( $result, $this->getMissingTitles(), 'missing' );
606 if ( in_array( "interwikiTitles", $invalidChecks ) ) {
607 self
::addValues( $result, $this->getInterwikiTitlesAsResult() );
614 * Get the list of valid revision IDs (requested with the revids= parameter)
615 * @return array Array of revID (int) => pageID (int)
617 public function getRevisionIDs() {
618 return $this->mGoodRevIDs
;
622 * Get the list of non-deleted revision IDs (requested with the revids= parameter)
623 * @return array Array of revID (int) => pageID (int)
625 public function getLiveRevisionIDs() {
626 return $this->mLiveRevIDs
;
630 * Get the list of revision IDs that were associated with deleted titles.
631 * @return array Array of revID (int) => pageID (int)
633 public function getDeletedRevisionIDs() {
634 return $this->mDeletedRevIDs
;
638 * Revision IDs that were not found in the database
639 * @return array Array of revision IDs
641 public function getMissingRevisionIDs() {
642 return $this->mMissingRevIDs
;
646 * Revision IDs that were not found in the database as result array.
647 * @param ApiResult $result
648 * @return array Array of revision IDs
651 public function getMissingRevisionIDsAsResult( $result = null ) {
653 foreach ( $this->getMissingRevisionIDs() as $revid ) {
654 $values[$revid] = array(
658 if ( !empty( $values ) && $result ) {
659 ApiResult
::setIndexedTagName( $values, 'rev' );
666 * Get the list of titles with negative namespace
669 public function getSpecialTitles() {
670 return $this->mSpecialTitles
;
674 * Returns the number of revisions (requested with revids= parameter).
675 * @return int Number of revisions.
677 public function getRevisionCount() {
678 return count( $this->getRevisionIDs() );
682 * Populate this PageSet from a list of Titles
683 * @param array $titles Array of Title objects
685 public function populateFromTitles( $titles ) {
686 $this->initFromTitles( $titles );
690 * Populate this PageSet from a list of page IDs
691 * @param array $pageIDs Array of page IDs
693 public function populateFromPageIDs( $pageIDs ) {
694 $this->initFromPageIds( $pageIDs );
698 * Populate this PageSet from a rowset returned from the database
700 * Note that the query result must include the columns returned by
701 * $this->getPageTableFields().
703 * @param DatabaseBase $db
704 * @param ResultWrapper $queryResult Query result object
706 public function populateFromQueryResult( $db, $queryResult ) {
707 $this->initFromQueryResult( $queryResult );
711 * Populate this PageSet from a list of revision IDs
712 * @param array $revIDs Array of revision IDs
714 public function populateFromRevisionIDs( $revIDs ) {
715 $this->initFromRevIDs( $revIDs );
719 * Extract all requested fields from the row received from the database
720 * @param stdClass $row Result row
722 public function processDbRow( $row ) {
723 // Store Title object in various data structures
724 $title = Title
::newFromRow( $row );
726 $pageId = intval( $row->page_id
);
727 $this->mAllPages
[$row->page_namespace
][$row->page_title
] = $pageId;
728 $this->mTitles
[] = $title;
730 if ( $this->mResolveRedirects
&& $row->page_is_redirect
== '1' ) {
731 $this->mPendingRedirectIDs
[$pageId] = $title;
733 $this->mGoodPages
[$row->page_namespace
][$row->page_title
] = $pageId;
734 $this->mGoodAndMissingPages
[$row->page_namespace
][$row->page_title
] = $pageId;
735 $this->mGoodTitles
[$pageId] = $title;
738 foreach ( $this->mRequestedPageFields
as $fieldName => &$fieldValues ) {
739 $fieldValues[$pageId] = $row->$fieldName;
744 * Do not use, does nothing, will be removed
745 * @deprecated since 1.21
747 public function finishPageSetGeneration() {
748 wfDeprecated( __METHOD__
, '1.21' );
752 * This method populates internal variables with page information
753 * based on the given array of title strings.
756 * #1 For each title, get data from `page` table
757 * #2 If page was not found in the DB, store it as missing
759 * Additionally, when resolving redirects:
760 * #3 If no more redirects left, stop.
761 * #4 For each redirect, get its target from the `redirect` table.
762 * #5 Substitute the original LinkBatch object with the new list
763 * #6 Repeat from step #1
765 * @param array $titles Array of Title objects or strings
767 private function initFromTitles( $titles ) {
768 // Get validated and normalized title objects
769 $linkBatch = $this->processTitlesArray( $titles );
770 if ( $linkBatch->isEmpty() ) {
774 $db = $this->getDB();
775 $set = $linkBatch->constructSet( 'page', $db );
777 // Get pageIDs data from the `page` table
778 $res = $db->select( 'page', $this->getPageTableFields(), $set,
781 // Hack: get the ns:titles stored in array(ns => array(titles)) format
782 $this->initFromQueryResult( $res, $linkBatch->data
, true ); // process Titles
784 // Resolve any found redirects
785 $this->resolvePendingRedirects();
789 * Does the same as initFromTitles(), but is based on page IDs instead
790 * @param array $pageids Array of page IDs
792 private function initFromPageIds( $pageids ) {
797 $pageids = array_map( 'intval', $pageids ); // paranoia
798 $remaining = array_flip( $pageids );
800 $pageids = self
::getPositiveIntegers( $pageids );
803 if ( !empty( $pageids ) ) {
805 'page_id' => $pageids
807 $db = $this->getDB();
809 // Get pageIDs data from the `page` table
810 $res = $db->select( 'page', $this->getPageTableFields(), $set,
814 $this->initFromQueryResult( $res, $remaining, false ); // process PageIDs
816 // Resolve any found redirects
817 $this->resolvePendingRedirects();
821 * Iterate through the result of the query on 'page' table,
822 * and for each row create and store title object and save any extra fields requested.
823 * @param ResultWrapper $res DB Query result
824 * @param array $remaining Array of either pageID or ns/title elements (optional).
825 * If given, any missing items will go to $mMissingPageIDs and $mMissingTitles
826 * @param bool $processTitles Must be provided together with $remaining.
827 * If true, treat $remaining as an array of [ns][title]
828 * If false, treat it as an array of [pageIDs]
830 private function initFromQueryResult( $res, &$remaining = null, $processTitles = null ) {
831 if ( !is_null( $remaining ) && is_null( $processTitles ) ) {
832 ApiBase
::dieDebug( __METHOD__
, 'Missing $processTitles parameter when $remaining is provided' );
835 $usernames = array();
837 foreach ( $res as $row ) {
838 $pageId = intval( $row->page_id
);
840 // Remove found page from the list of remaining items
841 if ( isset( $remaining ) ) {
842 if ( $processTitles ) {
843 unset( $remaining[$row->page_namespace
][$row->page_title
] );
845 unset( $remaining[$pageId] );
849 // Store any extra fields requested by modules
850 $this->processDbRow( $row );
852 // Need gender information
853 if ( MWNamespace
::hasGenderDistinction( $row->page_namespace
) ) {
854 $usernames[] = $row->page_title
;
859 if ( isset( $remaining ) ) {
860 // Any items left in the $remaining list are added as missing
861 if ( $processTitles ) {
862 // The remaining titles in $remaining are non-existent pages
863 foreach ( $remaining as $ns => $dbkeys ) {
864 foreach ( array_keys( $dbkeys ) as $dbkey ) {
865 $title = Title
::makeTitle( $ns, $dbkey );
866 $this->mAllPages
[$ns][$dbkey] = $this->mFakePageId
;
867 $this->mMissingPages
[$ns][$dbkey] = $this->mFakePageId
;
868 $this->mGoodAndMissingPages
[$ns][$dbkey] = $this->mFakePageId
;
869 $this->mMissingTitles
[$this->mFakePageId
] = $title;
870 $this->mFakePageId
--;
871 $this->mTitles
[] = $title;
873 // need gender information
874 if ( MWNamespace
::hasGenderDistinction( $ns ) ) {
875 $usernames[] = $dbkey;
880 // The remaining pageids do not exist
881 if ( !$this->mMissingPageIDs
) {
882 $this->mMissingPageIDs
= array_keys( $remaining );
884 $this->mMissingPageIDs
= array_merge( $this->mMissingPageIDs
, array_keys( $remaining ) );
889 // Get gender information
890 $genderCache = GenderCache
::singleton();
891 $genderCache->doQuery( $usernames, __METHOD__
);
895 * Does the same as initFromTitles(), but is based on revision IDs
897 * @param array $revids Array of revision IDs
899 private function initFromRevIDs( $revids ) {
904 $revids = array_map( 'intval', $revids ); // paranoia
905 $db = $this->getDB();
907 $remaining = array_flip( $revids );
909 $revids = self
::getPositiveIntegers( $revids );
911 if ( !empty( $revids ) ) {
912 $tables = array( 'revision', 'page' );
913 $fields = array( 'rev_id', 'rev_page' );
914 $where = array( 'rev_id' => $revids, 'rev_page = page_id' );
916 // Get pageIDs data from the `page` table
917 $res = $db->select( $tables, $fields, $where, __METHOD__
);
918 foreach ( $res as $row ) {
919 $revid = intval( $row->rev_id
);
920 $pageid = intval( $row->rev_page
);
921 $this->mGoodRevIDs
[$revid] = $pageid;
922 $this->mLiveRevIDs
[$revid] = $pageid;
923 $pageids[$pageid] = '';
924 unset( $remaining[$revid] );
928 $this->mMissingRevIDs
= array_keys( $remaining );
930 // Populate all the page information
931 $this->initFromPageIds( array_keys( $pageids ) );
933 // If the user can see deleted revisions, pull out the corresponding
934 // titles from the archive table and include them too. We ignore
935 // ar_page_id because deleted revisions are tied by title, not page_id.
936 if ( !empty( $this->mMissingRevIDs
) && $this->getUser()->isAllowed( 'deletedhistory' ) ) {
937 $remaining = array_flip( $this->mMissingRevIDs
);
938 $tables = array( 'archive' );
939 $fields = array( 'ar_rev_id', 'ar_namespace', 'ar_title' );
940 $where = array( 'ar_rev_id' => $this->mMissingRevIDs
);
942 $res = $db->select( $tables, $fields, $where, __METHOD__
);
944 foreach ( $res as $row ) {
945 $revid = intval( $row->ar_rev_id
);
946 $titles[$revid] = Title
::makeTitle( $row->ar_namespace
, $row->ar_title
);
947 unset( $remaining[$revid] );
950 $this->initFromTitles( $titles );
952 foreach ( $titles as $revid => $title ) {
953 $ns = $title->getNamespace();
954 $dbkey = $title->getDBkey();
956 // Handle converted titles
957 if ( !isset( $this->mAllPages
[$ns][$dbkey] ) &&
958 isset( $this->mConvertedTitles
[$title->getPrefixedText()] )
960 $title = Title
::newFromText( $this->mConvertedTitles
[$title->getPrefixedText()] );
961 $ns = $title->getNamespace();
962 $dbkey = $title->getDBkey();
965 if ( isset( $this->mAllPages
[$ns][$dbkey] ) ) {
966 $this->mGoodRevIDs
[$revid] = $this->mAllPages
[$ns][$dbkey];
967 $this->mDeletedRevIDs
[$revid] = $this->mAllPages
[$ns][$dbkey];
969 $remaining[$revid] = true;
973 $this->mMissingRevIDs
= array_keys( $remaining );
978 * Resolve any redirects in the result if redirect resolution was
979 * requested. This function is called repeatedly until all redirects
980 * have been resolved.
982 private function resolvePendingRedirects() {
983 if ( $this->mResolveRedirects
) {
984 $db = $this->getDB();
985 $pageFlds = $this->getPageTableFields();
987 // Repeat until all redirects have been resolved
988 // The infinite loop is prevented by keeping all known pages in $this->mAllPages
989 while ( $this->mPendingRedirectIDs
) {
990 // Resolve redirects by querying the pagelinks table, and repeat the process
991 // Create a new linkBatch object for the next pass
992 $linkBatch = $this->getRedirectTargets();
994 if ( $linkBatch->isEmpty() ) {
998 $set = $linkBatch->constructSet( 'page', $db );
999 if ( $set === false ) {
1003 // Get pageIDs data from the `page` table
1004 $res = $db->select( 'page', $pageFlds, $set, __METHOD__
);
1006 // Hack: get the ns:titles stored in array(ns => array(titles)) format
1007 $this->initFromQueryResult( $res, $linkBatch->data
, true );
1013 * Get the targets of the pending redirects from the database
1015 * Also creates entries in the redirect table for redirects that don't
1019 private function getRedirectTargets() {
1020 $lb = new LinkBatch();
1021 $db = $this->getDB();
1031 ), array( 'rd_from' => array_keys( $this->mPendingRedirectIDs
) ),
1034 foreach ( $res as $row ) {
1035 $rdfrom = intval( $row->rd_from
);
1036 $from = $this->mPendingRedirectIDs
[$rdfrom]->getPrefixedText();
1037 $to = Title
::makeTitle(
1043 $this->mResolvedRedirectTitles
[$from] = $this->mPendingRedirectIDs
[$rdfrom];
1044 unset( $this->mPendingRedirectIDs
[$rdfrom] );
1045 if ( $to->isExternal() ) {
1046 $this->mInterwikiTitles
[$to->getPrefixedText()] = $to->getInterwiki();
1047 } elseif ( !isset( $this->mAllPages
[$row->rd_namespace
][$row->rd_title
] ) ) {
1048 $lb->add( $row->rd_namespace
, $row->rd_title
);
1050 $this->mRedirectTitles
[$from] = $to;
1053 if ( $this->mPendingRedirectIDs
) {
1054 // We found pages that aren't in the redirect table
1056 foreach ( $this->mPendingRedirectIDs
as $id => $title ) {
1057 $page = WikiPage
::factory( $title );
1058 $rt = $page->insertRedirect();
1060 // What the hell. Let's just ignore this
1064 $from = $title->getPrefixedText();
1065 $this->mResolvedRedirectTitles
[$from] = $title;
1066 $this->mRedirectTitles
[$from] = $rt;
1067 unset( $this->mPendingRedirectIDs
[$id] );
1075 * Get the cache mode for the data generated by this module.
1076 * All PageSet users should take into account whether this returns a more-restrictive
1077 * cache mode than the using module itself. For possible return values and other
1078 * details about cache modes, see ApiMain::setCacheMode()
1080 * Public caching will only be allowed if *all* the modules that supply
1081 * data for a given request return a cache mode of public.
1083 * @param array|null $params
1087 public function getCacheMode( $params = null ) {
1088 return $this->mCacheMode
;
1092 * Given an array of title strings, convert them into Title objects.
1093 * Alternatively, an array of Title objects may be given.
1094 * This method validates access rights for the title,
1095 * and appends normalization values to the output.
1097 * @param array $titles Array of Title objects or strings
1100 private function processTitlesArray( $titles ) {
1101 $usernames = array();
1102 $linkBatch = new LinkBatch();
1104 foreach ( $titles as $title ) {
1105 if ( is_string( $title ) ) {
1107 $titleObj = Title
::newFromTextThrow( $title, $this->mDefaultNamespace
);
1108 } catch ( MalformedTitleException
$ex ) {
1109 // Handle invalid titles gracefully
1110 $this->mAllPages
[0][$title] = $this->mFakePageId
;
1111 $this->mInvalidTitles
[$this->mFakePageId
] = array(
1113 'invalidreason' => $ex->getMessage(),
1115 $this->mFakePageId
--;
1116 continue; // There's nothing else we can do
1121 $unconvertedTitle = $titleObj->getPrefixedText();
1122 $titleWasConverted = false;
1123 if ( $titleObj->isExternal() ) {
1124 // This title is an interwiki link.
1125 $this->mInterwikiTitles
[$unconvertedTitle] = $titleObj->getInterwiki();
1127 // Variants checking
1129 if ( $this->mConvertTitles
&&
1130 count( $wgContLang->getVariants() ) > 1 &&
1131 !$titleObj->exists()
1133 // Language::findVariantLink will modify titleText and titleObj into
1134 // the canonical variant if possible
1135 $titleText = is_string( $title ) ?
$title : $titleObj->getPrefixedText();
1136 $wgContLang->findVariantLink( $titleText, $titleObj );
1137 $titleWasConverted = $unconvertedTitle !== $titleObj->getPrefixedText();
1140 if ( $titleObj->getNamespace() < 0 ) {
1141 // Handle Special and Media pages
1142 $titleObj = $titleObj->fixSpecialName();
1143 $this->mSpecialTitles
[$this->mFakePageId
] = $titleObj;
1144 $this->mFakePageId
--;
1147 $linkBatch->addObj( $titleObj );
1151 // Make sure we remember the original title that was
1152 // given to us. This way the caller can correlate new
1153 // titles with the originally requested when e.g. the
1154 // namespace is localized or the capitalization is
1156 if ( $titleWasConverted ) {
1157 $this->mConvertedTitles
[$unconvertedTitle] = $titleObj->getPrefixedText();
1158 // In this case the page can't be Special.
1159 if ( is_string( $title ) && $title !== $unconvertedTitle ) {
1160 $this->mNormalizedTitles
[$title] = $unconvertedTitle;
1162 } elseif ( is_string( $title ) && $title !== $titleObj->getPrefixedText() ) {
1163 $this->mNormalizedTitles
[$title] = $titleObj->getPrefixedText();
1166 // Need gender information
1167 if ( MWNamespace
::hasGenderDistinction( $titleObj->getNamespace() ) ) {
1168 $usernames[] = $titleObj->getText();
1171 // Get gender information
1172 $genderCache = GenderCache
::singleton();
1173 $genderCache->doQuery( $usernames, __METHOD__
);
1179 * Set data for a title.
1181 * This data may be extracted into an ApiResult using
1182 * self::populateGeneratorData. This should generally be limited to
1183 * data that is likely to be particularly useful to end users rather than
1184 * just being a dump of everything returned in non-generator mode.
1186 * Redirects here will *not* be followed, even if 'redirects' was
1187 * specified, since in the case of multiple redirects we can't know which
1188 * source's data to use on the target.
1190 * @param Title $title
1191 * @param array $data
1193 public function setGeneratorData( Title
$title, array $data ) {
1194 $ns = $title->getNamespace();
1195 $dbkey = $title->getDBkey();
1196 $this->mGeneratorData
[$ns][$dbkey] = $data;
1200 * Populate the generator data for all titles in the result
1202 * The page data may be inserted into an ApiResult object or into an
1203 * associative array. The $path parameter specifies the path within the
1204 * ApiResult or array to find the "pages" node.
1206 * The "pages" node itself must be an associative array mapping the page ID
1207 * or fake page ID values returned by this pageset (see
1208 * self::getAllTitlesByNamespace() and self::getSpecialTitles()) to
1209 * associative arrays of page data. Each of those subarrays will have the
1210 * data from self::setGeneratorData() merged in.
1212 * Data that was set by self::setGeneratorData() for pages not in the
1213 * "pages" node will be ignored.
1215 * @param ApiResult|array &$result
1216 * @param array $path
1217 * @return bool Whether the data fit
1219 public function populateGeneratorData( &$result, array $path = array() ) {
1220 if ( $result instanceof ApiResult
) {
1221 $data = $result->getResultData( $path );
1222 if ( $data === null ) {
1227 foreach ( $path as $key ) {
1228 if ( !isset( $data[$key] ) ) {
1229 // Path isn't in $result, so nothing to add, so everything
1233 $data = &$data[$key];
1236 foreach ( $this->mGeneratorData
as $ns => $dbkeys ) {
1239 foreach ( $this->mSpecialTitles
as $id => $title ) {
1240 $pages[$title->getDBkey()] = $id;
1243 if ( !isset( $this->mAllPages
[$ns] ) ) {
1244 // No known titles in the whole namespace. Skip it.
1247 $pages = $this->mAllPages
[$ns];
1249 foreach ( $dbkeys as $dbkey => $genData ) {
1250 if ( !isset( $pages[$dbkey] ) ) {
1251 // Unknown title. Forget it.
1254 $pageId = $pages[$dbkey];
1255 if ( !isset( $data[$pageId] ) ) {
1256 // $pageId didn't make it into the result. Ignore it.
1260 if ( $result instanceof ApiResult
) {
1261 $path2 = array_merge( $path, array( $pageId ) );
1262 foreach ( $genData as $key => $value ) {
1263 if ( !$result->addValue( $path2, $key, $value ) ) {
1268 $data[$pageId] = array_merge( $data[$pageId], $genData );
1276 * Get the database connection (read-only)
1277 * @return DatabaseBase
1279 protected function getDB() {
1280 return $this->mDbSource
->getDB();
1284 * Returns the input array of integers with all values < 0 removed
1286 * @param array $array
1289 private static function getPositiveIntegers( $array ) {
1290 // bug 25734 API: possible issue with revids validation
1291 // It seems with a load of revision rows, MySQL gets upset
1292 // Remove any < 0 integers, as they can't be valid
1293 foreach ( $array as $i => $int ) {
1295 unset( $array[$i] );
1302 public function getAllowedParams( $flags = 0 ) {
1305 ApiBase
::PARAM_ISMULTI
=> true,
1306 ApiBase
::PARAM_HELP_MSG
=> 'api-pageset-param-titles',
1309 ApiBase
::PARAM_TYPE
=> 'integer',
1310 ApiBase
::PARAM_ISMULTI
=> true,
1311 ApiBase
::PARAM_HELP_MSG
=> 'api-pageset-param-pageids',
1314 ApiBase
::PARAM_TYPE
=> 'integer',
1315 ApiBase
::PARAM_ISMULTI
=> true,
1316 ApiBase
::PARAM_HELP_MSG
=> 'api-pageset-param-revids',
1318 'generator' => array(
1319 ApiBase
::PARAM_TYPE
=> null,
1320 ApiBase
::PARAM_HELP_MSG
=> 'api-pageset-param-generator',
1321 ApiBase
::PARAM_SUBMODULE_PARAM_PREFIX
=> 'g',
1323 'redirects' => array(
1324 ApiBase
::PARAM_DFLT
=> false,
1325 ApiBase
::PARAM_HELP_MSG
=> $this->mAllowGenerator
1326 ?
'api-pageset-param-redirects-generator'
1327 : 'api-pageset-param-redirects-nogenerator',
1329 'converttitles' => array(
1330 ApiBase
::PARAM_DFLT
=> false,
1331 ApiBase
::PARAM_HELP_MSG
=> array(
1332 'api-pageset-param-converttitles',
1333 new DeferredStringifier(
1334 function ( IContextSource
$context ) {
1335 return $context->getLanguage()
1336 ->commaList( LanguageConverter
::$languagesWithVariants );
1344 if ( !$this->mAllowGenerator
) {
1345 unset( $result['generator'] );
1346 } elseif ( $flags & ApiBase
::GET_VALUES_FOR_HELP
) {
1347 $result['generator'][ApiBase
::PARAM_TYPE
] = 'submodule';
1348 $result['generator'][ApiBase
::PARAM_SUBMODULE_MAP
] = $this->getGenerators();
1354 private static $generators = null;
1357 * Get an array of all available generators
1360 private function getGenerators() {
1361 if ( self
::$generators === null ) {
1362 $query = $this->mDbSource
;
1363 if ( !( $query instanceof ApiQuery
) ) {
1364 // If the parent container of this pageset is not ApiQuery,
1365 // we must create it to get module manager
1366 $query = $this->getMain()->getModuleManager()->getModule( 'query' );
1369 $prefix = $query->getModulePath() . '+';
1370 $mgr = $query->getModuleManager();
1371 foreach ( $mgr->getNamesWithClasses() as $name => $class ) {
1372 if ( is_subclass_of( $class, 'ApiQueryGeneratorBase' ) ) {
1373 $gens[$name] = $prefix . $name;
1377 self
::$generators = $gens;
1380 return self
::$generators;