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 $mConvertedTitles = array();
70 private $mGoodRevIDs = array();
71 private $mLiveRevIDs = array();
72 private $mDeletedRevIDs = array();
73 private $mMissingRevIDs = array();
74 private $mGeneratorData = array(); // [ns][dbkey] => data array
75 private $mFakePageId = -1;
76 private $mCacheMode = 'public';
77 private $mRequestedPageFields = array();
79 private $mDefaultNamespace = NS_MAIN
;
82 * Add all items from $values into the result
83 * @param array $result Output
84 * @param array $values Values to add
85 * @param string $flag The name of the boolean flag to mark this element
86 * @param string $name If given, name of the value
88 private static function addValues( array &$result, $values, $flag = null, $name = null ) {
89 foreach ( $values as $val ) {
90 if ( $val instanceof Title
) {
92 ApiQueryBase
::addTitleInfo( $v, $val );
93 } elseif ( $name !== null ) {
94 $v = array( $name => $val );
98 if ( $flag !== null ) {
106 * @param ApiBase $dbSource Module implementing getDB().
107 * Allows PageSet to reuse existing db connection from the shared state like ApiQuery.
108 * @param int $flags Zero or more flags like DISABLE_GENERATORS
109 * @param int $defaultNamespace The namespace to use if none is specified by a prefix.
110 * @since 1.21 accepts $flags instead of two boolean values
112 public function __construct( ApiBase
$dbSource, $flags = 0, $defaultNamespace = NS_MAIN
) {
113 parent
::__construct( $dbSource->getMain(), $dbSource->getModuleName() );
114 $this->mDbSource
= $dbSource;
115 $this->mAllowGenerator
= ( $flags & ApiPageSet
::DISABLE_GENERATORS
) == 0;
116 $this->mDefaultNamespace
= $defaultNamespace;
118 $this->mParams
= $this->extractRequestParams();
119 $this->mResolveRedirects
= $this->mParams
['redirects'];
120 $this->mConvertTitles
= $this->mParams
['converttitles'];
124 * In case execute() is not called, call this method to mark all relevant parameters as used
125 * This prevents unused parameters from being reported as warnings
127 public function executeDryRun() {
128 $this->executeInternal( true );
132 * Populate the PageSet from the request parameters.
134 public function execute() {
135 $this->executeInternal( false );
139 * Populate the PageSet from the request parameters.
140 * @param bool $isDryRun If true, instantiates generator, but only to mark
141 * relevant parameters as used
143 private function executeInternal( $isDryRun ) {
144 $generatorName = $this->mAllowGenerator ?
$this->mParams
['generator'] : null;
145 if ( isset( $generatorName ) ) {
146 $dbSource = $this->mDbSource
;
147 if ( !$dbSource instanceof ApiQuery
) {
148 // If the parent container of this pageset is not ApiQuery, we must create it to run generator
149 $dbSource = $this->getMain()->getModuleManager()->getModule( 'query' );
151 $generator = $dbSource->getModuleManager()->getModule( $generatorName, null, true );
152 if ( $generator === null ) {
153 $this->dieUsage( 'Unknown generator=' . $generatorName, 'badgenerator' );
155 if ( !$generator instanceof ApiQueryGeneratorBase
) {
156 $this->dieUsage( "Module $generatorName cannot be used as a generator", 'badgenerator' );
158 // Create a temporary pageset to store generator's output,
159 // add any additional fields generator may need, and execute pageset to populate titles/pageids
160 $tmpPageSet = new ApiPageSet( $dbSource, ApiPageSet
::DISABLE_GENERATORS
);
161 $generator->setGeneratorMode( $tmpPageSet );
162 $this->mCacheMode
= $generator->getCacheMode( $generator->extractRequestParams() );
165 $generator->requestExtraData( $tmpPageSet );
167 $tmpPageSet->executeInternal( $isDryRun );
169 // populate this pageset with the generator output
171 $generator->executeGenerator( $this );
172 Hooks
::run( 'APIQueryGeneratorAfterExecute', array( &$generator, &$this ) );
174 // Prevent warnings from being reported on these parameters
175 $main = $this->getMain();
176 foreach ( $generator->extractRequestParams() as $paramName => $param ) {
177 $main->getVal( $generator->encodeParamName( $paramName ) );
182 $this->resolvePendingRedirects();
185 // Only one of the titles/pageids/revids is allowed at the same time
187 if ( isset( $this->mParams
['titles'] ) ) {
188 $dataSource = 'titles';
190 if ( isset( $this->mParams
['pageids'] ) ) {
191 if ( isset( $dataSource ) ) {
192 $this->dieUsage( "Cannot use 'pageids' at the same time as '$dataSource'", 'multisource' );
194 $dataSource = 'pageids';
196 if ( isset( $this->mParams
['revids'] ) ) {
197 if ( isset( $dataSource ) ) {
198 $this->dieUsage( "Cannot use 'revids' at the same time as '$dataSource'", 'multisource' );
200 $dataSource = 'revids';
204 // Populate page information with the original user input
205 switch ( $dataSource ) {
207 $this->initFromTitles( $this->mParams
['titles'] );
210 $this->initFromPageIds( $this->mParams
['pageids'] );
213 if ( $this->mResolveRedirects
) {
214 $this->setWarning( 'Redirect resolution cannot be used ' .
215 'together with the revids= parameter. Any redirects ' .
216 'the revids= point to have not been resolved.' );
218 $this->mResolveRedirects
= false;
219 $this->initFromRevIDs( $this->mParams
['revids'] );
222 // Do nothing - some queries do not need any of the data sources.
230 * Check whether this PageSet is resolving redirects
233 public function isResolvingRedirects() {
234 return $this->mResolveRedirects
;
238 * Return the parameter name that is the source of data for this PageSet
240 * If multiple source parameters are specified (e.g. titles and pageids),
241 * one will be named arbitrarily.
243 * @return string|null
245 public function getDataSource() {
246 if ( $this->mAllowGenerator
&& isset( $this->mParams
['generator'] ) ) {
249 if ( isset( $this->mParams
['titles'] ) ) {
252 if ( isset( $this->mParams
['pageids'] ) ) {
255 if ( isset( $this->mParams
['revids'] ) ) {
263 * Request an additional field from the page table.
264 * Must be called before execute()
265 * @param string $fieldName Field name
267 public function requestField( $fieldName ) {
268 $this->mRequestedPageFields
[$fieldName] = null;
272 * Get the value of a custom field previously requested through
274 * @param string $fieldName Field name
275 * @return mixed Field value
277 public function getCustomField( $fieldName ) {
278 return $this->mRequestedPageFields
[$fieldName];
282 * Get the fields that have to be queried from the page table:
283 * the ones requested through requestField() and a few basic ones
285 * @return array Array of field names
287 public function getPageTableFields() {
288 // Ensure we get minimum required fields
289 // DON'T change this order
291 'page_namespace' => null,
292 'page_title' => null,
296 if ( $this->mResolveRedirects
) {
297 $pageFlds['page_is_redirect'] = null;
300 if ( $this->getConfig()->get( 'ContentHandlerUseDB' ) ) {
301 $pageFlds['page_content_model'] = null;
304 // only store non-default fields
305 $this->mRequestedPageFields
= array_diff_key( $this->mRequestedPageFields
, $pageFlds );
307 $pageFlds = array_merge( $pageFlds, $this->mRequestedPageFields
);
309 return array_keys( $pageFlds );
313 * Returns an array [ns][dbkey] => page_id for all requested titles.
314 * page_id is a unique negative number in case title was not found.
315 * Invalid titles will also have negative page IDs and will be in namespace 0
318 public function getAllTitlesByNamespace() {
319 return $this->mAllPages
;
323 * All Title objects provided.
326 public function getTitles() {
327 return $this->mTitles
;
331 * Returns the number of unique pages (not revisions) in the set.
334 public function getTitleCount() {
335 return count( $this->mTitles
);
339 * Returns an array [ns][dbkey] => page_id for all good titles.
342 public function getGoodTitlesByNamespace() {
343 return $this->mGoodPages
;
347 * Title objects that were found in the database.
348 * @return Title[] Array page_id (int) => Title (obj)
350 public function getGoodTitles() {
351 return $this->mGoodTitles
;
355 * Returns the number of found unique pages (not revisions) in the set.
358 public function getGoodTitleCount() {
359 return count( $this->mGoodTitles
);
363 * Returns an array [ns][dbkey] => fake_page_id for all missing titles.
364 * fake_page_id is a unique negative number.
367 public function getMissingTitlesByNamespace() {
368 return $this->mMissingPages
;
372 * Title objects that were NOT found in the database.
373 * The array's index will be negative for each item
376 public function getMissingTitles() {
377 return $this->mMissingTitles
;
381 * Returns an array [ns][dbkey] => page_id for all good and missing titles.
384 public function getGoodAndMissingTitlesByNamespace() {
385 return $this->mGoodAndMissingPages
;
389 * Title objects for good and missing titles.
392 public function getGoodAndMissingTitles() {
393 return $this->mGoodTitles +
$this->mMissingTitles
;
397 * Titles that were deemed invalid by Title::newFromText()
398 * The array's index will be unique and negative for each item
399 * @deprecated since 1.26, use self::getInvalidTitlesAndReasons()
400 * @return string[] Array of strings (not Title objects)
402 public function getInvalidTitles() {
403 wfDeprecated( __METHOD__
, '1.26' );
404 return array_map( function ( $t ) {
406 }, $this->mInvalidTitles
);
410 * Titles that were deemed invalid by Title::newFromText()
411 * The array's index will be unique and negative for each item
412 * @return array[] Array of arrays with 'title' and 'invalidreason' properties
414 public function getInvalidTitlesAndReasons() {
415 return $this->mInvalidTitles
;
419 * Page IDs that were not found in the database
420 * @return array Array of page IDs
422 public function getMissingPageIDs() {
423 return $this->mMissingPageIDs
;
427 * Get a list of redirect resolutions - maps a title to its redirect
428 * target, as an array of output-ready arrays
431 public function getRedirectTitles() {
432 return $this->mRedirectTitles
;
436 * Get a list of redirect resolutions - maps a title to its redirect
438 * @param ApiResult $result
439 * @return array Array of prefixed_title (string) => Title object
442 public function getRedirectTitlesAsResult( $result = null ) {
444 foreach ( $this->getRedirectTitles() as $titleStrFrom => $titleTo ) {
446 'from' => strval( $titleStrFrom ),
447 'to' => $titleTo->getPrefixedText(),
449 if ( $titleTo->hasFragment() ) {
450 $r['tofragment'] = $titleTo->getFragment();
452 if ( $titleTo->isExternal() ) {
453 $r['tointerwiki'] = $titleTo->getInterwiki();
457 if ( !empty( $values ) && $result ) {
458 ApiResult
::setIndexedTagName( $values, 'r' );
465 * Get a list of title normalizations - maps a title to its normalized
467 * @return array Array of raw_prefixed_title (string) => prefixed_title (string)
469 public function getNormalizedTitles() {
470 return $this->mNormalizedTitles
;
474 * Get a list of title normalizations - maps a title to its normalized
475 * version in the form of result array.
476 * @param ApiResult $result
477 * @return array Array of raw_prefixed_title (string) => prefixed_title (string)
480 public function getNormalizedTitlesAsResult( $result = null ) {
482 foreach ( $this->getNormalizedTitles() as $rawTitleStr => $titleStr ) {
484 'from' => $rawTitleStr,
488 if ( !empty( $values ) && $result ) {
489 ApiResult
::setIndexedTagName( $values, 'n' );
496 * Get a list of title conversions - maps a title to its converted
498 * @return array Array of raw_prefixed_title (string) => prefixed_title (string)
500 public function getConvertedTitles() {
501 return $this->mConvertedTitles
;
505 * Get a list of title conversions - maps a title to its converted
506 * version as a result array.
507 * @param ApiResult $result
508 * @return array Array of (from, to) strings
511 public function getConvertedTitlesAsResult( $result = null ) {
513 foreach ( $this->getConvertedTitles() as $rawTitleStr => $titleStr ) {
515 'from' => $rawTitleStr,
519 if ( !empty( $values ) && $result ) {
520 ApiResult
::setIndexedTagName( $values, 'c' );
527 * Get a list of interwiki titles - maps a title to its interwiki
529 * @return array Array of raw_prefixed_title (string) => interwiki_prefix (string)
531 public function getInterwikiTitles() {
532 return $this->mInterwikiTitles
;
536 * Get a list of interwiki titles - maps a title to its interwiki
538 * @param ApiResult $result
540 * @return array Array of raw_prefixed_title (string) => interwiki_prefix (string)
543 public function getInterwikiTitlesAsResult( $result = null, $iwUrl = false ) {
545 foreach ( $this->getInterwikiTitles() as $rawTitleStr => $interwikiStr ) {
547 'title' => $rawTitleStr,
548 'iw' => $interwikiStr,
551 $title = Title
::newFromText( $rawTitleStr );
552 $item['url'] = $title->getFullURL( '', false, PROTO_CURRENT
);
556 if ( !empty( $values ) && $result ) {
557 ApiResult
::setIndexedTagName( $values, 'i' );
564 * Get an array of invalid/special/missing titles.
566 * @param array $invalidChecks List of types of invalid titles to include.
567 * Recognized values are:
568 * - invalidTitles: Titles and reasons from $this->getInvalidTitlesAndReasons()
569 * - special: Titles from $this->getSpecialTitles()
570 * - missingIds: ids from $this->getMissingPageIDs()
571 * - missingRevIds: ids from $this->getMissingRevisionIDs()
572 * - missingTitles: Titles from $this->getMissingTitles()
573 * - interwikiTitles: Titles from $this->getInterwikiTitlesAsResult()
574 * @return array Array suitable for inclusion in the response
577 public function getInvalidTitlesAndRevisions( $invalidChecks = array( 'invalidTitles',
578 'special', 'missingIds', 'missingRevIds', 'missingTitles', 'interwikiTitles' )
581 if ( in_array( "invalidTitles", $invalidChecks ) ) {
582 self
::addValues( $result, $this->getInvalidTitlesAndReasons(), 'invalid' );
584 if ( in_array( "special", $invalidChecks ) ) {
585 self
::addValues( $result, $this->getSpecialTitles(), 'special', 'title' );
587 if ( in_array( "missingIds", $invalidChecks ) ) {
588 self
::addValues( $result, $this->getMissingPageIDs(), 'missing', 'pageid' );
590 if ( in_array( "missingRevIds", $invalidChecks ) ) {
591 self
::addValues( $result, $this->getMissingRevisionIDs(), 'missing', 'revid' );
593 if ( in_array( "missingTitles", $invalidChecks ) ) {
594 self
::addValues( $result, $this->getMissingTitles(), 'missing' );
596 if ( in_array( "interwikiTitles", $invalidChecks ) ) {
597 self
::addValues( $result, $this->getInterwikiTitlesAsResult() );
604 * Get the list of valid revision IDs (requested with the revids= parameter)
605 * @return array Array of revID (int) => pageID (int)
607 public function getRevisionIDs() {
608 return $this->mGoodRevIDs
;
612 * Get the list of non-deleted revision IDs (requested with the revids= parameter)
613 * @return array Array of revID (int) => pageID (int)
615 public function getLiveRevisionIDs() {
616 return $this->mLiveRevIDs
;
620 * Get the list of revision IDs that were associated with deleted titles.
621 * @return array Array of revID (int) => pageID (int)
623 public function getDeletedRevisionIDs() {
624 return $this->mDeletedRevIDs
;
628 * Revision IDs that were not found in the database
629 * @return array Array of revision IDs
631 public function getMissingRevisionIDs() {
632 return $this->mMissingRevIDs
;
636 * Revision IDs that were not found in the database as result array.
637 * @param ApiResult $result
638 * @return array Array of revision IDs
641 public function getMissingRevisionIDsAsResult( $result = null ) {
643 foreach ( $this->getMissingRevisionIDs() as $revid ) {
644 $values[$revid] = array(
648 if ( !empty( $values ) && $result ) {
649 ApiResult
::setIndexedTagName( $values, 'rev' );
656 * Get the list of titles with negative namespace
659 public function getSpecialTitles() {
660 return $this->mSpecialTitles
;
664 * Returns the number of revisions (requested with revids= parameter).
665 * @return int Number of revisions.
667 public function getRevisionCount() {
668 return count( $this->getRevisionIDs() );
672 * Populate this PageSet from a list of Titles
673 * @param array $titles Array of Title objects
675 public function populateFromTitles( $titles ) {
676 $this->initFromTitles( $titles );
680 * Populate this PageSet from a list of page IDs
681 * @param array $pageIDs Array of page IDs
683 public function populateFromPageIDs( $pageIDs ) {
684 $this->initFromPageIds( $pageIDs );
688 * Populate this PageSet from a rowset returned from the database
690 * Note that the query result must include the columns returned by
691 * $this->getPageTableFields().
693 * @param DatabaseBase $db
694 * @param ResultWrapper $queryResult Query result object
696 public function populateFromQueryResult( $db, $queryResult ) {
697 $this->initFromQueryResult( $queryResult );
701 * Populate this PageSet from a list of revision IDs
702 * @param array $revIDs Array of revision IDs
704 public function populateFromRevisionIDs( $revIDs ) {
705 $this->initFromRevIDs( $revIDs );
709 * Extract all requested fields from the row received from the database
710 * @param stdClass $row Result row
712 public function processDbRow( $row ) {
713 // Store Title object in various data structures
714 $title = Title
::newFromRow( $row );
716 $pageId = intval( $row->page_id
);
717 $this->mAllPages
[$row->page_namespace
][$row->page_title
] = $pageId;
718 $this->mTitles
[] = $title;
720 if ( $this->mResolveRedirects
&& $row->page_is_redirect
== '1' ) {
721 $this->mPendingRedirectIDs
[$pageId] = $title;
723 $this->mGoodPages
[$row->page_namespace
][$row->page_title
] = $pageId;
724 $this->mGoodAndMissingPages
[$row->page_namespace
][$row->page_title
] = $pageId;
725 $this->mGoodTitles
[$pageId] = $title;
728 foreach ( $this->mRequestedPageFields
as $fieldName => &$fieldValues ) {
729 $fieldValues[$pageId] = $row->$fieldName;
734 * Do not use, does nothing, will be removed
735 * @deprecated since 1.21
737 public function finishPageSetGeneration() {
738 wfDeprecated( __METHOD__
, '1.21' );
742 * This method populates internal variables with page information
743 * based on the given array of title strings.
746 * #1 For each title, get data from `page` table
747 * #2 If page was not found in the DB, store it as missing
749 * Additionally, when resolving redirects:
750 * #3 If no more redirects left, stop.
751 * #4 For each redirect, get its target from the `redirect` table.
752 * #5 Substitute the original LinkBatch object with the new list
753 * #6 Repeat from step #1
755 * @param array $titles Array of Title objects or strings
757 private function initFromTitles( $titles ) {
758 // Get validated and normalized title objects
759 $linkBatch = $this->processTitlesArray( $titles );
760 if ( $linkBatch->isEmpty() ) {
764 $db = $this->getDB();
765 $set = $linkBatch->constructSet( 'page', $db );
767 // Get pageIDs data from the `page` table
768 $res = $db->select( 'page', $this->getPageTableFields(), $set,
771 // Hack: get the ns:titles stored in array(ns => array(titles)) format
772 $this->initFromQueryResult( $res, $linkBatch->data
, true ); // process Titles
774 // Resolve any found redirects
775 $this->resolvePendingRedirects();
779 * Does the same as initFromTitles(), but is based on page IDs instead
780 * @param array $pageids Array of page IDs
782 private function initFromPageIds( $pageids ) {
787 $pageids = array_map( 'intval', $pageids ); // paranoia
788 $remaining = array_flip( $pageids );
790 $pageids = self
::getPositiveIntegers( $pageids );
793 if ( !empty( $pageids ) ) {
795 'page_id' => $pageids
797 $db = $this->getDB();
799 // Get pageIDs data from the `page` table
800 $res = $db->select( 'page', $this->getPageTableFields(), $set,
804 $this->initFromQueryResult( $res, $remaining, false ); // process PageIDs
806 // Resolve any found redirects
807 $this->resolvePendingRedirects();
811 * Iterate through the result of the query on 'page' table,
812 * and for each row create and store title object and save any extra fields requested.
813 * @param ResultWrapper $res DB Query result
814 * @param array $remaining Array of either pageID or ns/title elements (optional).
815 * If given, any missing items will go to $mMissingPageIDs and $mMissingTitles
816 * @param bool $processTitles Must be provided together with $remaining.
817 * If true, treat $remaining as an array of [ns][title]
818 * If false, treat it as an array of [pageIDs]
820 private function initFromQueryResult( $res, &$remaining = null, $processTitles = null ) {
821 if ( !is_null( $remaining ) && is_null( $processTitles ) ) {
822 ApiBase
::dieDebug( __METHOD__
, 'Missing $processTitles parameter when $remaining is provided' );
825 $usernames = array();
827 foreach ( $res as $row ) {
828 $pageId = intval( $row->page_id
);
830 // Remove found page from the list of remaining items
831 if ( isset( $remaining ) ) {
832 if ( $processTitles ) {
833 unset( $remaining[$row->page_namespace
][$row->page_title
] );
835 unset( $remaining[$pageId] );
839 // Store any extra fields requested by modules
840 $this->processDbRow( $row );
842 // Need gender information
843 if ( MWNamespace
::hasGenderDistinction( $row->page_namespace
) ) {
844 $usernames[] = $row->page_title
;
849 if ( isset( $remaining ) ) {
850 // Any items left in the $remaining list are added as missing
851 if ( $processTitles ) {
852 // The remaining titles in $remaining are non-existent pages
853 foreach ( $remaining as $ns => $dbkeys ) {
854 foreach ( array_keys( $dbkeys ) as $dbkey ) {
855 $title = Title
::makeTitle( $ns, $dbkey );
856 $this->mAllPages
[$ns][$dbkey] = $this->mFakePageId
;
857 $this->mMissingPages
[$ns][$dbkey] = $this->mFakePageId
;
858 $this->mGoodAndMissingPages
[$ns][$dbkey] = $this->mFakePageId
;
859 $this->mMissingTitles
[$this->mFakePageId
] = $title;
860 $this->mFakePageId
--;
861 $this->mTitles
[] = $title;
863 // need gender information
864 if ( MWNamespace
::hasGenderDistinction( $ns ) ) {
865 $usernames[] = $dbkey;
870 // The remaining pageids do not exist
871 if ( !$this->mMissingPageIDs
) {
872 $this->mMissingPageIDs
= array_keys( $remaining );
874 $this->mMissingPageIDs
= array_merge( $this->mMissingPageIDs
, array_keys( $remaining ) );
879 // Get gender information
880 $genderCache = GenderCache
::singleton();
881 $genderCache->doQuery( $usernames, __METHOD__
);
885 * Does the same as initFromTitles(), but is based on revision IDs
887 * @param array $revids Array of revision IDs
889 private function initFromRevIDs( $revids ) {
894 $revids = array_map( 'intval', $revids ); // paranoia
895 $db = $this->getDB();
897 $remaining = array_flip( $revids );
899 $revids = self
::getPositiveIntegers( $revids );
901 if ( !empty( $revids ) ) {
902 $tables = array( 'revision', 'page' );
903 $fields = array( 'rev_id', 'rev_page' );
904 $where = array( 'rev_id' => $revids, 'rev_page = page_id' );
906 // Get pageIDs data from the `page` table
907 $res = $db->select( $tables, $fields, $where, __METHOD__
);
908 foreach ( $res as $row ) {
909 $revid = intval( $row->rev_id
);
910 $pageid = intval( $row->rev_page
);
911 $this->mGoodRevIDs
[$revid] = $pageid;
912 $this->mLiveRevIDs
[$revid] = $pageid;
913 $pageids[$pageid] = '';
914 unset( $remaining[$revid] );
918 $this->mMissingRevIDs
= array_keys( $remaining );
920 // Populate all the page information
921 $this->initFromPageIds( array_keys( $pageids ) );
923 // If the user can see deleted revisions, pull out the corresponding
924 // titles from the archive table and include them too. We ignore
925 // ar_page_id because deleted revisions are tied by title, not page_id.
926 if ( !empty( $this->mMissingRevIDs
) && $this->getUser()->isAllowed( 'deletedhistory' ) ) {
927 $remaining = array_flip( $this->mMissingRevIDs
);
928 $tables = array( 'archive' );
929 $fields = array( 'ar_rev_id', 'ar_namespace', 'ar_title' );
930 $where = array( 'ar_rev_id' => $this->mMissingRevIDs
);
932 $res = $db->select( $tables, $fields, $where, __METHOD__
);
934 foreach ( $res as $row ) {
935 $revid = intval( $row->ar_rev_id
);
936 $titles[$revid] = Title
::makeTitle( $row->ar_namespace
, $row->ar_title
);
937 unset( $remaining[$revid] );
940 $this->initFromTitles( $titles );
942 foreach ( $titles as $revid => $title ) {
943 $ns = $title->getNamespace();
944 $dbkey = $title->getDBkey();
946 // Handle converted titles
947 if ( !isset( $this->mAllPages
[$ns][$dbkey] ) &&
948 isset( $this->mConvertedTitles
[$title->getPrefixedText()] )
950 $title = Title
::newFromText( $this->mConvertedTitles
[$title->getPrefixedText()] );
951 $ns = $title->getNamespace();
952 $dbkey = $title->getDBkey();
955 if ( isset( $this->mAllPages
[$ns][$dbkey] ) ) {
956 $this->mGoodRevIDs
[$revid] = $this->mAllPages
[$ns][$dbkey];
957 $this->mDeletedRevIDs
[$revid] = $this->mAllPages
[$ns][$dbkey];
959 $remaining[$revid] = true;
963 $this->mMissingRevIDs
= array_keys( $remaining );
968 * Resolve any redirects in the result if redirect resolution was
969 * requested. This function is called repeatedly until all redirects
970 * have been resolved.
972 private function resolvePendingRedirects() {
973 if ( $this->mResolveRedirects
) {
974 $db = $this->getDB();
975 $pageFlds = $this->getPageTableFields();
977 // Repeat until all redirects have been resolved
978 // The infinite loop is prevented by keeping all known pages in $this->mAllPages
979 while ( $this->mPendingRedirectIDs
) {
980 // Resolve redirects by querying the pagelinks table, and repeat the process
981 // Create a new linkBatch object for the next pass
982 $linkBatch = $this->getRedirectTargets();
984 if ( $linkBatch->isEmpty() ) {
988 $set = $linkBatch->constructSet( 'page', $db );
989 if ( $set === false ) {
993 // Get pageIDs data from the `page` table
994 $res = $db->select( 'page', $pageFlds, $set, __METHOD__
);
996 // Hack: get the ns:titles stored in array(ns => array(titles)) format
997 $this->initFromQueryResult( $res, $linkBatch->data
, true );
1003 * Get the targets of the pending redirects from the database
1005 * Also creates entries in the redirect table for redirects that don't
1009 private function getRedirectTargets() {
1010 $lb = new LinkBatch();
1011 $db = $this->getDB();
1021 ), array( 'rd_from' => array_keys( $this->mPendingRedirectIDs
) ),
1024 foreach ( $res as $row ) {
1025 $rdfrom = intval( $row->rd_from
);
1026 $from = $this->mPendingRedirectIDs
[$rdfrom]->getPrefixedText();
1027 $to = Title
::makeTitle(
1033 unset( $this->mPendingRedirectIDs
[$rdfrom] );
1034 if ( $to->isExternal() ) {
1035 $this->mInterwikiTitles
[$to->getPrefixedText()] = $to->getInterwiki();
1036 } elseif ( !isset( $this->mAllPages
[$row->rd_namespace
][$row->rd_title
] ) ) {
1037 $lb->add( $row->rd_namespace
, $row->rd_title
);
1039 $this->mRedirectTitles
[$from] = $to;
1042 if ( $this->mPendingRedirectIDs
) {
1043 // We found pages that aren't in the redirect table
1045 foreach ( $this->mPendingRedirectIDs
as $id => $title ) {
1046 $page = WikiPage
::factory( $title );
1047 $rt = $page->insertRedirect();
1049 // What the hell. Let's just ignore this
1053 $this->mRedirectTitles
[$title->getPrefixedText()] = $rt;
1054 unset( $this->mPendingRedirectIDs
[$id] );
1062 * Get the cache mode for the data generated by this module.
1063 * All PageSet users should take into account whether this returns a more-restrictive
1064 * cache mode than the using module itself. For possible return values and other
1065 * details about cache modes, see ApiMain::setCacheMode()
1067 * Public caching will only be allowed if *all* the modules that supply
1068 * data for a given request return a cache mode of public.
1070 * @param array|null $params
1074 public function getCacheMode( $params = null ) {
1075 return $this->mCacheMode
;
1079 * Given an array of title strings, convert them into Title objects.
1080 * Alternatively, an array of Title objects may be given.
1081 * This method validates access rights for the title,
1082 * and appends normalization values to the output.
1084 * @param array $titles Array of Title objects or strings
1087 private function processTitlesArray( $titles ) {
1088 $usernames = array();
1089 $linkBatch = new LinkBatch();
1091 foreach ( $titles as $title ) {
1092 if ( is_string( $title ) ) {
1094 $titleObj = Title
::newFromTextThrow( $title, $this->mDefaultNamespace
);
1095 } catch ( MalformedTitleException
$ex ) {
1096 // Handle invalid titles gracefully
1097 $this->mAllPages
[0][$title] = $this->mFakePageId
;
1098 $this->mInvalidTitles
[$this->mFakePageId
] = array(
1100 'invalidreason' => $ex->getMessage(),
1102 $this->mFakePageId
--;
1103 continue; // There's nothing else we can do
1108 $unconvertedTitle = $titleObj->getPrefixedText();
1109 $titleWasConverted = false;
1110 if ( $titleObj->isExternal() ) {
1111 // This title is an interwiki link.
1112 $this->mInterwikiTitles
[$unconvertedTitle] = $titleObj->getInterwiki();
1114 // Variants checking
1116 if ( $this->mConvertTitles
&&
1117 count( $wgContLang->getVariants() ) > 1 &&
1118 !$titleObj->exists()
1120 // Language::findVariantLink will modify titleText and titleObj into
1121 // the canonical variant if possible
1122 $titleText = is_string( $title ) ?
$title : $titleObj->getPrefixedText();
1123 $wgContLang->findVariantLink( $titleText, $titleObj );
1124 $titleWasConverted = $unconvertedTitle !== $titleObj->getPrefixedText();
1127 if ( $titleObj->getNamespace() < 0 ) {
1128 // Handle Special and Media pages
1129 $titleObj = $titleObj->fixSpecialName();
1130 $this->mSpecialTitles
[$this->mFakePageId
] = $titleObj;
1131 $this->mFakePageId
--;
1134 $linkBatch->addObj( $titleObj );
1138 // Make sure we remember the original title that was
1139 // given to us. This way the caller can correlate new
1140 // titles with the originally requested when e.g. the
1141 // namespace is localized or the capitalization is
1143 if ( $titleWasConverted ) {
1144 $this->mConvertedTitles
[$unconvertedTitle] = $titleObj->getPrefixedText();
1145 // In this case the page can't be Special.
1146 if ( is_string( $title ) && $title !== $unconvertedTitle ) {
1147 $this->mNormalizedTitles
[$title] = $unconvertedTitle;
1149 } elseif ( is_string( $title ) && $title !== $titleObj->getPrefixedText() ) {
1150 $this->mNormalizedTitles
[$title] = $titleObj->getPrefixedText();
1153 // Need gender information
1154 if ( MWNamespace
::hasGenderDistinction( $titleObj->getNamespace() ) ) {
1155 $usernames[] = $titleObj->getText();
1158 // Get gender information
1159 $genderCache = GenderCache
::singleton();
1160 $genderCache->doQuery( $usernames, __METHOD__
);
1166 * Set data for a title.
1168 * This data may be extracted into an ApiResult using
1169 * self::populateGeneratorData. This should generally be limited to
1170 * data that is likely to be particularly useful to end users rather than
1171 * just being a dump of everything returned in non-generator mode.
1173 * Redirects here will *not* be followed, even if 'redirects' was
1174 * specified, since in the case of multiple redirects we can't know which
1175 * source's data to use on the target.
1177 * @param Title $title
1178 * @param array $data
1180 public function setGeneratorData( Title
$title, array $data ) {
1181 $ns = $title->getNamespace();
1182 $dbkey = $title->getDBkey();
1183 $this->mGeneratorData
[$ns][$dbkey] = $data;
1187 * Populate the generator data for all titles in the result
1189 * The page data may be inserted into an ApiResult object or into an
1190 * associative array. The $path parameter specifies the path within the
1191 * ApiResult or array to find the "pages" node.
1193 * The "pages" node itself must be an associative array mapping the page ID
1194 * or fake page ID values returned by this pageset (see
1195 * self::getAllTitlesByNamespace() and self::getSpecialTitles()) to
1196 * associative arrays of page data. Each of those subarrays will have the
1197 * data from self::setGeneratorData() merged in.
1199 * Data that was set by self::setGeneratorData() for pages not in the
1200 * "pages" node will be ignored.
1202 * @param ApiResult|array &$result
1203 * @param array $path
1204 * @return bool Whether the data fit
1206 public function populateGeneratorData( &$result, array $path = array() ) {
1207 if ( $result instanceof ApiResult
) {
1208 $data = $result->getResultData( $path );
1209 if ( $data === null ) {
1214 foreach ( $path as $key ) {
1215 if ( !isset( $data[$key] ) ) {
1216 // Path isn't in $result, so nothing to add, so everything
1220 $data = &$data[$key];
1223 foreach ( $this->mGeneratorData
as $ns => $dbkeys ) {
1226 foreach ( $this->mSpecialTitles
as $id => $title ) {
1227 $pages[$title->getDBkey()] = $id;
1230 if ( !isset( $this->mAllPages
[$ns] ) ) {
1231 // No known titles in the whole namespace. Skip it.
1234 $pages = $this->mAllPages
[$ns];
1236 foreach ( $dbkeys as $dbkey => $genData ) {
1237 if ( !isset( $pages[$dbkey] ) ) {
1238 // Unknown title. Forget it.
1241 $pageId = $pages[$dbkey];
1242 if ( !isset( $data[$pageId] ) ) {
1243 // $pageId didn't make it into the result. Ignore it.
1247 if ( $result instanceof ApiResult
) {
1248 $path2 = array_merge( $path, array( $pageId ) );
1249 foreach ( $genData as $key => $value ) {
1250 if ( !$result->addValue( $path2, $key, $value ) ) {
1255 $data[$pageId] = array_merge( $data[$pageId], $genData );
1263 * Get the database connection (read-only)
1264 * @return DatabaseBase
1266 protected function getDB() {
1267 return $this->mDbSource
->getDB();
1271 * Returns the input array of integers with all values < 0 removed
1273 * @param array $array
1276 private static function getPositiveIntegers( $array ) {
1277 // bug 25734 API: possible issue with revids validation
1278 // It seems with a load of revision rows, MySQL gets upset
1279 // Remove any < 0 integers, as they can't be valid
1280 foreach ( $array as $i => $int ) {
1282 unset( $array[$i] );
1289 public function getAllowedParams( $flags = 0 ) {
1292 ApiBase
::PARAM_ISMULTI
=> true,
1293 ApiBase
::PARAM_HELP_MSG
=> 'api-pageset-param-titles',
1296 ApiBase
::PARAM_TYPE
=> 'integer',
1297 ApiBase
::PARAM_ISMULTI
=> true,
1298 ApiBase
::PARAM_HELP_MSG
=> 'api-pageset-param-pageids',
1301 ApiBase
::PARAM_TYPE
=> 'integer',
1302 ApiBase
::PARAM_ISMULTI
=> true,
1303 ApiBase
::PARAM_HELP_MSG
=> 'api-pageset-param-revids',
1305 'generator' => array(
1306 ApiBase
::PARAM_TYPE
=> null,
1307 ApiBase
::PARAM_HELP_MSG
=> 'api-pageset-param-generator',
1308 ApiBase
::PARAM_SUBMODULE_PARAM_PREFIX
=> 'g',
1310 'redirects' => array(
1311 ApiBase
::PARAM_DFLT
=> false,
1312 ApiBase
::PARAM_HELP_MSG
=> $this->mAllowGenerator
1313 ?
'api-pageset-param-redirects-generator'
1314 : 'api-pageset-param-redirects-nogenerator',
1316 'converttitles' => array(
1317 ApiBase
::PARAM_DFLT
=> false,
1318 ApiBase
::PARAM_HELP_MSG
=> array(
1319 'api-pageset-param-converttitles',
1320 new DeferredStringifier(
1321 function ( IContextSource
$context ) {
1322 return $context->getLanguage()
1323 ->commaList( LanguageConverter
::$languagesWithVariants );
1331 if ( !$this->mAllowGenerator
) {
1332 unset( $result['generator'] );
1333 } elseif ( $flags & ApiBase
::GET_VALUES_FOR_HELP
) {
1334 $result['generator'][ApiBase
::PARAM_TYPE
] = 'submodule';
1335 $result['generator'][ApiBase
::PARAM_SUBMODULE_MAP
] = $this->getGenerators();
1341 private static $generators = null;
1344 * Get an array of all available generators
1347 private function getGenerators() {
1348 if ( self
::$generators === null ) {
1349 $query = $this->mDbSource
;
1350 if ( !( $query instanceof ApiQuery
) ) {
1351 // If the parent container of this pageset is not ApiQuery,
1352 // we must create it to get module manager
1353 $query = $this->getMain()->getModuleManager()->getModule( 'query' );
1356 $prefix = $query->getModulePath() . '+';
1357 $mgr = $query->getModuleManager();
1358 foreach ( $mgr->getNamesWithClasses() as $name => $class ) {
1359 if ( is_subclass_of( $class, 'ApiQueryGeneratorBase' ) ) {
1360 $gens[$name] = $prefix . $name;
1364 self
::$generators = $gens;
1367 return self
::$generators;