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();
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;
119 $this->mParams
= $this->extractRequestParams();
120 $this->mResolveRedirects
= $this->mParams
['redirects'];
121 $this->mConvertTitles
= $this->mParams
['converttitles'];
126 * In case execute() is not called, call this method to mark all relevant parameters as used
127 * This prevents unused parameters from being reported as warnings
129 public function executeDryRun() {
130 $this->executeInternal( true );
134 * Populate the PageSet from the request parameters.
136 public function execute() {
137 $this->executeInternal( false );
141 * Populate the PageSet from the request parameters.
142 * @param bool $isDryRun If true, instantiates generator, but only to mark
143 * relevant parameters as used
145 private function executeInternal( $isDryRun ) {
148 $generatorName = $this->mAllowGenerator ?
$this->mParams
['generator'] : null;
149 if ( isset( $generatorName ) ) {
150 $dbSource = $this->mDbSource
;
151 $isQuery = $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' );
155 // Enable profiling for query module because it will be used for db sql profiling
156 $dbSource->profileIn();
158 $generator = $dbSource->getModuleManager()->getModule( $generatorName, null, true );
159 if ( $generator === null ) {
160 $this->dieUsage( 'Unknown generator=' . $generatorName, 'badgenerator' );
162 if ( !$generator instanceof ApiQueryGeneratorBase
) {
163 $this->dieUsage( "Module $generatorName cannot be used as a generator", 'badgenerator' );
165 // Create a temporary pageset to store generator's output,
166 // add any additional fields generator may need, and execute pageset to populate titles/pageids
167 $tmpPageSet = new ApiPageSet( $dbSource, ApiPageSet
::DISABLE_GENERATORS
);
168 $generator->setGeneratorMode( $tmpPageSet );
169 $this->mCacheMode
= $generator->getCacheMode( $generator->extractRequestParams() );
172 $generator->requestExtraData( $tmpPageSet );
174 $tmpPageSet->executeInternal( $isDryRun );
176 // populate this pageset with the generator output
178 $generator->profileIn();
181 $generator->executeGenerator( $this );
182 Hooks
::run( 'APIQueryGeneratorAfterExecute', array( &$generator, &$this ) );
184 // Prevent warnings from being reported on these parameters
185 $main = $this->getMain();
186 foreach ( $generator->extractRequestParams() as $paramName => $param ) {
187 $main->getVal( $generator->encodeParamName( $paramName ) );
190 $generator->profileOut();
194 $this->resolvePendingRedirects();
198 // If this pageset is not part of the query, we called profileIn() above
199 $dbSource->profileOut();
202 // Only one of the titles/pageids/revids is allowed at the same time
204 if ( isset( $this->mParams
['titles'] ) ) {
205 $dataSource = 'titles';
207 if ( isset( $this->mParams
['pageids'] ) ) {
208 if ( isset( $dataSource ) ) {
209 $this->dieUsage( "Cannot use 'pageids' at the same time as '$dataSource'", 'multisource' );
211 $dataSource = 'pageids';
213 if ( isset( $this->mParams
['revids'] ) ) {
214 if ( isset( $dataSource ) ) {
215 $this->dieUsage( "Cannot use 'revids' at the same time as '$dataSource'", 'multisource' );
217 $dataSource = 'revids';
221 // Populate page information with the original user input
222 switch ( $dataSource ) {
224 $this->initFromTitles( $this->mParams
['titles'] );
227 $this->initFromPageIds( $this->mParams
['pageids'] );
230 if ( $this->mResolveRedirects
) {
231 $this->setWarning( 'Redirect resolution cannot be used ' .
232 'together with the revids= parameter. Any redirects ' .
233 'the revids= point to have not been resolved.' );
235 $this->mResolveRedirects
= false;
236 $this->initFromRevIDs( $this->mParams
['revids'] );
239 // Do nothing - some queries do not need any of the data sources.
248 * Check whether this PageSet is resolving redirects
251 public function isResolvingRedirects() {
252 return $this->mResolveRedirects
;
256 * Return the parameter name that is the source of data for this PageSet
258 * If multiple source parameters are specified (e.g. titles and pageids),
259 * one will be named arbitrarily.
261 * @return string|null
263 public function getDataSource() {
264 if ( $this->mAllowGenerator
&& isset( $this->mParams
['generator'] ) ) {
267 if ( isset( $this->mParams
['titles'] ) ) {
270 if ( isset( $this->mParams
['pageids'] ) ) {
273 if ( isset( $this->mParams
['revids'] ) ) {
281 * Request an additional field from the page table.
282 * Must be called before execute()
283 * @param string $fieldName Field name
285 public function requestField( $fieldName ) {
286 $this->mRequestedPageFields
[$fieldName] = null;
290 * Get the value of a custom field previously requested through
292 * @param string $fieldName Field name
293 * @return mixed Field value
295 public function getCustomField( $fieldName ) {
296 return $this->mRequestedPageFields
[$fieldName];
300 * Get the fields that have to be queried from the page table:
301 * the ones requested through requestField() and a few basic ones
303 * @return array Array of field names
305 public function getPageTableFields() {
306 // Ensure we get minimum required fields
307 // DON'T change this order
309 'page_namespace' => null,
310 'page_title' => null,
314 if ( $this->mResolveRedirects
) {
315 $pageFlds['page_is_redirect'] = null;
318 if ( $this->getConfig()->get( 'ContentHandlerUseDB' ) ) {
319 $pageFlds['page_content_model'] = null;
322 // only store non-default fields
323 $this->mRequestedPageFields
= array_diff_key( $this->mRequestedPageFields
, $pageFlds );
325 $pageFlds = array_merge( $pageFlds, $this->mRequestedPageFields
);
327 return array_keys( $pageFlds );
331 * Returns an array [ns][dbkey] => page_id for all requested titles.
332 * page_id is a unique negative number in case title was not found.
333 * Invalid titles will also have negative page IDs and will be in namespace 0
336 public function getAllTitlesByNamespace() {
337 return $this->mAllPages
;
341 * All Title objects provided.
344 public function getTitles() {
345 return $this->mTitles
;
349 * Returns the number of unique pages (not revisions) in the set.
352 public function getTitleCount() {
353 return count( $this->mTitles
);
357 * Returns an array [ns][dbkey] => page_id for all good titles.
360 public function getGoodTitlesByNamespace() {
361 return $this->mGoodPages
;
365 * Title objects that were found in the database.
366 * @return Title[] Array page_id (int) => Title (obj)
368 public function getGoodTitles() {
369 return $this->mGoodTitles
;
373 * Returns the number of found unique pages (not revisions) in the set.
376 public function getGoodTitleCount() {
377 return count( $this->mGoodTitles
);
381 * Returns an array [ns][dbkey] => fake_page_id for all missing titles.
382 * fake_page_id is a unique negative number.
385 public function getMissingTitlesByNamespace() {
386 return $this->mMissingPages
;
390 * Title objects that were NOT found in the database.
391 * The array's index will be negative for each item
394 public function getMissingTitles() {
395 return $this->mMissingTitles
;
399 * Returns an array [ns][dbkey] => page_id for all good and missing titles.
402 public function getGoodAndMissingTitlesByNamespace() {
403 return $this->mGoodAndMissingPages
;
407 * Title objects for good and missing titles.
410 public function getGoodAndMissingTitles() {
411 return $this->mGoodTitles +
$this->mMissingTitles
;
415 * Titles that were deemed invalid by Title::newFromText()
416 * The array's index will be unique and negative for each item
417 * @return string[] Array of strings (not Title objects)
419 public function getInvalidTitles() {
420 return $this->mInvalidTitles
;
424 * Page IDs that were not found in the database
425 * @return array Array of page IDs
427 public function getMissingPageIDs() {
428 return $this->mMissingPageIDs
;
432 * Get a list of redirect resolutions - maps a title to its redirect
433 * target, as an array of output-ready arrays
436 public function getRedirectTitles() {
437 return $this->mRedirectTitles
;
441 * Get a list of redirect resolutions - maps a title to its redirect
443 * @param ApiResult $result
444 * @return array Array of prefixed_title (string) => Title object
447 public function getRedirectTitlesAsResult( $result = null ) {
449 foreach ( $this->getRedirectTitles() as $titleStrFrom => $titleTo ) {
451 'from' => strval( $titleStrFrom ),
452 'to' => $titleTo->getPrefixedText(),
454 if ( $titleTo->hasFragment() ) {
455 $r['tofragment'] = $titleTo->getFragment();
457 if ( $titleTo->isExternal() ) {
458 $r['tointerwiki'] = $titleTo->getInterwiki();
462 if ( !empty( $values ) && $result ) {
463 $result->setIndexedTagName( $values, 'r' );
470 * Get a list of title normalizations - maps a title to its normalized
472 * @return array Array of raw_prefixed_title (string) => prefixed_title (string)
474 public function getNormalizedTitles() {
475 return $this->mNormalizedTitles
;
479 * Get a list of title normalizations - maps a title to its normalized
480 * version in the form of result array.
481 * @param ApiResult $result
482 * @return array Array of raw_prefixed_title (string) => prefixed_title (string)
485 public function getNormalizedTitlesAsResult( $result = null ) {
487 foreach ( $this->getNormalizedTitles() as $rawTitleStr => $titleStr ) {
489 'from' => $rawTitleStr,
493 if ( !empty( $values ) && $result ) {
494 $result->setIndexedTagName( $values, 'n' );
501 * Get a list of title conversions - maps a title to its converted
503 * @return array Array of raw_prefixed_title (string) => prefixed_title (string)
505 public function getConvertedTitles() {
506 return $this->mConvertedTitles
;
510 * Get a list of title conversions - maps a title to its converted
511 * version as a result array.
512 * @param ApiResult $result
513 * @return array Array of (from, to) strings
516 public function getConvertedTitlesAsResult( $result = null ) {
518 foreach ( $this->getConvertedTitles() as $rawTitleStr => $titleStr ) {
520 'from' => $rawTitleStr,
524 if ( !empty( $values ) && $result ) {
525 $result->setIndexedTagName( $values, 'c' );
532 * Get a list of interwiki titles - maps a title to its interwiki
534 * @return array Array of raw_prefixed_title (string) => interwiki_prefix (string)
536 public function getInterwikiTitles() {
537 return $this->mInterwikiTitles
;
541 * Get a list of interwiki titles - maps a title to its interwiki
543 * @param ApiResult $result
545 * @return array Array of raw_prefixed_title (string) => interwiki_prefix (string)
548 public function getInterwikiTitlesAsResult( $result = null, $iwUrl = false ) {
550 foreach ( $this->getInterwikiTitles() as $rawTitleStr => $interwikiStr ) {
552 'title' => $rawTitleStr,
553 'iw' => $interwikiStr,
556 $title = Title
::newFromText( $rawTitleStr );
557 $item['url'] = $title->getFullURL( '', false, PROTO_CURRENT
);
561 if ( !empty( $values ) && $result ) {
562 $result->setIndexedTagName( $values, 'i' );
569 * Get an array of invalid/special/missing titles.
571 * @param array $invalidChecks List of types of invalid titles to include.
572 * Recognized values are:
573 * - invalidTitles: Titles from $this->getInvalidTitles()
574 * - special: Titles from $this->getSpecialTitles()
575 * - missingIds: ids from $this->getMissingPageIDs()
576 * - missingRevIds: ids from $this->getMissingRevisionIDs()
577 * - missingTitles: Titles from $this->getMissingTitles()
578 * - interwikiTitles: Titles from $this->getInterwikiTitlesAsResult()
579 * @return array Array suitable for inclusion in the response
582 public function getInvalidTitlesAndRevisions( $invalidChecks = array( 'invalidTitles',
583 'special', 'missingIds', 'missingRevIds', 'missingTitles', 'interwikiTitles' )
586 if ( in_array( "invalidTitles", $invalidChecks ) ) {
587 self
::addValues( $result, $this->getInvalidTitles(), 'invalid', 'title' );
589 if ( in_array( "special", $invalidChecks ) ) {
590 self
::addValues( $result, $this->getSpecialTitles(), 'special', 'title' );
592 if ( in_array( "missingIds", $invalidChecks ) ) {
593 self
::addValues( $result, $this->getMissingPageIDs(), 'missing', 'pageid' );
595 if ( in_array( "missingRevIds", $invalidChecks ) ) {
596 self
::addValues( $result, $this->getMissingRevisionIDs(), 'missing', 'revid' );
598 if ( in_array( "missingTitles", $invalidChecks ) ) {
599 self
::addValues( $result, $this->getMissingTitles(), 'missing' );
601 if ( in_array( "interwikiTitles", $invalidChecks ) ) {
602 self
::addValues( $result, $this->getInterwikiTitlesAsResult() );
609 * Get the list of valid revision IDs (requested with the revids= parameter)
610 * @return array Array of revID (int) => pageID (int)
612 public function getRevisionIDs() {
613 return $this->mGoodRevIDs
;
617 * Get the list of non-deleted revision IDs (requested with the revids= parameter)
618 * @return array Array of revID (int) => pageID (int)
620 public function getLiveRevisionIDs() {
621 return $this->mLiveRevIDs
;
625 * Get the list of revision IDs that were associated with deleted titles.
626 * @return array Array of revID (int) => pageID (int)
628 public function getDeletedRevisionIDs() {
629 return $this->mDeletedRevIDs
;
633 * Revision IDs that were not found in the database
634 * @return array Array of revision IDs
636 public function getMissingRevisionIDs() {
637 return $this->mMissingRevIDs
;
641 * Revision IDs that were not found in the database as result array.
642 * @param ApiResult $result
643 * @return array Array of revision IDs
646 public function getMissingRevisionIDsAsResult( $result = null ) {
648 foreach ( $this->getMissingRevisionIDs() as $revid ) {
649 $values[$revid] = array(
653 if ( !empty( $values ) && $result ) {
654 $result->setIndexedTagName( $values, 'rev' );
661 * Get the list of titles with negative namespace
664 public function getSpecialTitles() {
665 return $this->mSpecialTitles
;
669 * Returns the number of revisions (requested with revids= parameter).
670 * @return int Number of revisions.
672 public function getRevisionCount() {
673 return count( $this->getRevisionIDs() );
677 * Populate this PageSet from a list of Titles
678 * @param array $titles Array of Title objects
680 public function populateFromTitles( $titles ) {
682 $this->initFromTitles( $titles );
687 * Populate this PageSet from a list of page IDs
688 * @param array $pageIDs Array of page IDs
690 public function populateFromPageIDs( $pageIDs ) {
692 $this->initFromPageIds( $pageIDs );
697 * Populate this PageSet from a rowset returned from the database
699 * Note that the query result must include the columns returned by
700 * $this->getPageTableFields().
702 * @param DatabaseBase $db
703 * @param ResultWrapper $queryResult Query result object
705 public function populateFromQueryResult( $db, $queryResult ) {
707 $this->initFromQueryResult( $queryResult );
712 * Populate this PageSet from a list of revision IDs
713 * @param array $revIDs Array of revision IDs
715 public function populateFromRevisionIDs( $revIDs ) {
717 $this->initFromRevIDs( $revIDs );
722 * Extract all requested fields from the row received from the database
723 * @param stdClass $row Result row
725 public function processDbRow( $row ) {
726 // Store Title object in various data structures
727 $title = Title
::newFromRow( $row );
729 $pageId = intval( $row->page_id
);
730 $this->mAllPages
[$row->page_namespace
][$row->page_title
] = $pageId;
731 $this->mTitles
[] = $title;
733 if ( $this->mResolveRedirects
&& $row->page_is_redirect
== '1' ) {
734 $this->mPendingRedirectIDs
[$pageId] = $title;
736 $this->mGoodPages
[$row->page_namespace
][$row->page_title
] = $pageId;
737 $this->mGoodAndMissingPages
[$row->page_namespace
][$row->page_title
] = $pageId;
738 $this->mGoodTitles
[$pageId] = $title;
741 foreach ( $this->mRequestedPageFields
as $fieldName => &$fieldValues ) {
742 $fieldValues[$pageId] = $row->$fieldName;
747 * Do not use, does nothing, will be removed
748 * @deprecated since 1.21
750 public function finishPageSetGeneration() {
751 wfDeprecated( __METHOD__
, '1.21' );
755 * This method populates internal variables with page information
756 * based on the given array of title strings.
759 * #1 For each title, get data from `page` table
760 * #2 If page was not found in the DB, store it as missing
762 * Additionally, when resolving redirects:
763 * #3 If no more redirects left, stop.
764 * #4 For each redirect, get its target from the `redirect` table.
765 * #5 Substitute the original LinkBatch object with the new list
766 * #6 Repeat from step #1
768 * @param array $titles Array of Title objects or strings
770 private function initFromTitles( $titles ) {
771 // Get validated and normalized title objects
772 $linkBatch = $this->processTitlesArray( $titles );
773 if ( $linkBatch->isEmpty() ) {
777 $db = $this->getDB();
778 $set = $linkBatch->constructSet( 'page', $db );
780 // Get pageIDs data from the `page` table
781 $this->profileDBIn();
782 $res = $db->select( 'page', $this->getPageTableFields(), $set,
784 $this->profileDBOut();
786 // Hack: get the ns:titles stored in array(ns => array(titles)) format
787 $this->initFromQueryResult( $res, $linkBatch->data
, true ); // process Titles
789 // Resolve any found redirects
790 $this->resolvePendingRedirects();
794 * Does the same as initFromTitles(), but is based on page IDs instead
795 * @param array $pageids Array of page IDs
797 private function initFromPageIds( $pageids ) {
802 $pageids = array_map( 'intval', $pageids ); // paranoia
803 $remaining = array_flip( $pageids );
805 $pageids = self
::getPositiveIntegers( $pageids );
808 if ( !empty( $pageids ) ) {
810 'page_id' => $pageids
812 $db = $this->getDB();
814 // Get pageIDs data from the `page` table
815 $this->profileDBIn();
816 $res = $db->select( 'page', $this->getPageTableFields(), $set,
818 $this->profileDBOut();
821 $this->initFromQueryResult( $res, $remaining, false ); // process PageIDs
823 // Resolve any found redirects
824 $this->resolvePendingRedirects();
828 * Iterate through the result of the query on 'page' table,
829 * and for each row create and store title object and save any extra fields requested.
830 * @param ResultWrapper $res DB Query result
831 * @param array $remaining Array of either pageID or ns/title elements (optional).
832 * If given, any missing items will go to $mMissingPageIDs and $mMissingTitles
833 * @param bool $processTitles Must be provided together with $remaining.
834 * If true, treat $remaining as an array of [ns][title]
835 * If false, treat it as an array of [pageIDs]
837 private function initFromQueryResult( $res, &$remaining = null, $processTitles = null ) {
838 if ( !is_null( $remaining ) && is_null( $processTitles ) ) {
839 ApiBase
::dieDebug( __METHOD__
, 'Missing $processTitles parameter when $remaining is provided' );
842 $usernames = array();
844 foreach ( $res as $row ) {
845 $pageId = intval( $row->page_id
);
847 // Remove found page from the list of remaining items
848 if ( isset( $remaining ) ) {
849 if ( $processTitles ) {
850 unset( $remaining[$row->page_namespace
][$row->page_title
] );
852 unset( $remaining[$pageId] );
856 // Store any extra fields requested by modules
857 $this->processDbRow( $row );
859 // Need gender information
860 if ( MWNamespace
::hasGenderDistinction( $row->page_namespace
) ) {
861 $usernames[] = $row->page_title
;
866 if ( isset( $remaining ) ) {
867 // Any items left in the $remaining list are added as missing
868 if ( $processTitles ) {
869 // The remaining titles in $remaining are non-existent pages
870 foreach ( $remaining as $ns => $dbkeys ) {
871 foreach ( array_keys( $dbkeys ) as $dbkey ) {
872 $title = Title
::makeTitle( $ns, $dbkey );
873 $this->mAllPages
[$ns][$dbkey] = $this->mFakePageId
;
874 $this->mMissingPages
[$ns][$dbkey] = $this->mFakePageId
;
875 $this->mGoodAndMissingPages
[$ns][$dbkey] = $this->mFakePageId
;
876 $this->mMissingTitles
[$this->mFakePageId
] = $title;
877 $this->mFakePageId
--;
878 $this->mTitles
[] = $title;
880 // need gender information
881 if ( MWNamespace
::hasGenderDistinction( $ns ) ) {
882 $usernames[] = $dbkey;
887 // The remaining pageids do not exist
888 if ( !$this->mMissingPageIDs
) {
889 $this->mMissingPageIDs
= array_keys( $remaining );
891 $this->mMissingPageIDs
= array_merge( $this->mMissingPageIDs
, array_keys( $remaining ) );
896 // Get gender information
897 $genderCache = GenderCache
::singleton();
898 $genderCache->doQuery( $usernames, __METHOD__
);
902 * Does the same as initFromTitles(), but is based on revision IDs
904 * @param array $revids Array of revision IDs
906 private function initFromRevIDs( $revids ) {
911 $revids = array_map( 'intval', $revids ); // paranoia
912 $db = $this->getDB();
914 $remaining = array_flip( $revids );
916 $revids = self
::getPositiveIntegers( $revids );
918 if ( !empty( $revids ) ) {
919 $tables = array( 'revision', 'page' );
920 $fields = array( 'rev_id', 'rev_page' );
921 $where = array( 'rev_id' => $revids, 'rev_page = page_id' );
923 // Get pageIDs data from the `page` table
924 $this->profileDBIn();
925 $res = $db->select( $tables, $fields, $where, __METHOD__
);
926 foreach ( $res as $row ) {
927 $revid = intval( $row->rev_id
);
928 $pageid = intval( $row->rev_page
);
929 $this->mGoodRevIDs
[$revid] = $pageid;
930 $this->mLiveRevIDs
[$revid] = $pageid;
931 $pageids[$pageid] = '';
932 unset( $remaining[$revid] );
934 $this->profileDBOut();
937 $this->mMissingRevIDs
= array_keys( $remaining );
939 // Populate all the page information
940 $this->initFromPageIds( array_keys( $pageids ) );
942 // If the user can see deleted revisions, pull out the corresponding
943 // titles from the archive table and include them too. We ignore
944 // ar_page_id because deleted revisions are tied by title, not page_id.
945 if ( !empty( $this->mMissingRevIDs
) && $this->getUser()->isAllowed( 'deletedhistory' ) ) {
946 $remaining = array_flip( $this->mMissingRevIDs
);
947 $tables = array( 'archive' );
948 $fields = array( 'ar_rev_id', 'ar_namespace', 'ar_title' );
949 $where = array( 'ar_rev_id' => $this->mMissingRevIDs
);
951 $this->profileDBIn();
952 $res = $db->select( $tables, $fields, $where, __METHOD__
);
954 foreach ( $res as $row ) {
955 $revid = intval( $row->ar_rev_id
);
956 $titles[$revid] = Title
::makeTitle( $row->ar_namespace
, $row->ar_title
);
957 unset( $remaining[$revid] );
959 $this->profileDBOut();
961 $this->initFromTitles( $titles );
963 foreach ( $titles as $revid => $title ) {
964 $ns = $title->getNamespace();
965 $dbkey = $title->getDBkey();
967 // Handle converted titles
968 if ( !isset( $this->mAllPages
[$ns][$dbkey] ) &&
969 isset( $this->mConvertedTitles
[$title->getPrefixedText()] )
971 $title = Title
::newFromText( $this->mConvertedTitles
[$title->getPrefixedText()] );
972 $ns = $title->getNamespace();
973 $dbkey = $title->getDBkey();
976 if ( isset( $this->mAllPages
[$ns][$dbkey] ) ) {
977 $this->mGoodRevIDs
[$revid] = $this->mAllPages
[$ns][$dbkey];
978 $this->mDeletedRevIDs
[$revid] = $this->mAllPages
[$ns][$dbkey];
980 $remaining[$revid] = true;
984 $this->mMissingRevIDs
= array_keys( $remaining );
989 * Resolve any redirects in the result if redirect resolution was
990 * requested. This function is called repeatedly until all redirects
991 * have been resolved.
993 private function resolvePendingRedirects() {
994 if ( $this->mResolveRedirects
) {
995 $db = $this->getDB();
996 $pageFlds = $this->getPageTableFields();
998 // Repeat until all redirects have been resolved
999 // The infinite loop is prevented by keeping all known pages in $this->mAllPages
1000 while ( $this->mPendingRedirectIDs
) {
1001 // Resolve redirects by querying the pagelinks table, and repeat the process
1002 // Create a new linkBatch object for the next pass
1003 $linkBatch = $this->getRedirectTargets();
1005 if ( $linkBatch->isEmpty() ) {
1009 $set = $linkBatch->constructSet( 'page', $db );
1010 if ( $set === false ) {
1014 // Get pageIDs data from the `page` table
1015 $this->profileDBIn();
1016 $res = $db->select( 'page', $pageFlds, $set, __METHOD__
);
1017 $this->profileDBOut();
1019 // Hack: get the ns:titles stored in array(ns => array(titles)) format
1020 $this->initFromQueryResult( $res, $linkBatch->data
, true );
1026 * Get the targets of the pending redirects from the database
1028 * Also creates entries in the redirect table for redirects that don't
1032 private function getRedirectTargets() {
1033 $lb = new LinkBatch();
1034 $db = $this->getDB();
1036 $this->profileDBIn();
1045 ), array( 'rd_from' => array_keys( $this->mPendingRedirectIDs
) ),
1048 $this->profileDBOut();
1049 foreach ( $res as $row ) {
1050 $rdfrom = intval( $row->rd_from
);
1051 $from = $this->mPendingRedirectIDs
[$rdfrom]->getPrefixedText();
1052 $to = Title
::makeTitle(
1058 unset( $this->mPendingRedirectIDs
[$rdfrom] );
1059 if ( $to->isExternal() ) {
1060 $this->mInterwikiTitles
[$to->getPrefixedText()] = $to->getInterwiki();
1061 } elseif ( !isset( $this->mAllPages
[$row->rd_namespace
][$row->rd_title
] ) ) {
1062 $lb->add( $row->rd_namespace
, $row->rd_title
);
1064 $this->mRedirectTitles
[$from] = $to;
1067 if ( $this->mPendingRedirectIDs
) {
1068 // We found pages that aren't in the redirect table
1070 foreach ( $this->mPendingRedirectIDs
as $id => $title ) {
1071 $page = WikiPage
::factory( $title );
1072 $rt = $page->insertRedirect();
1074 // What the hell. Let's just ignore this
1078 $this->mRedirectTitles
[$title->getPrefixedText()] = $rt;
1079 unset( $this->mPendingRedirectIDs
[$id] );
1087 * Get the cache mode for the data generated by this module.
1088 * All PageSet users should take into account whether this returns a more-restrictive
1089 * cache mode than the using module itself. For possible return values and other
1090 * details about cache modes, see ApiMain::setCacheMode()
1092 * Public caching will only be allowed if *all* the modules that supply
1093 * data for a given request return a cache mode of public.
1095 * @param array|null $params
1099 public function getCacheMode( $params = null ) {
1100 return $this->mCacheMode
;
1104 * Given an array of title strings, convert them into Title objects.
1105 * Alternatively, an array of Title objects may be given.
1106 * This method validates access rights for the title,
1107 * and appends normalization values to the output.
1109 * @param array $titles Array of Title objects or strings
1112 private function processTitlesArray( $titles ) {
1113 $usernames = array();
1114 $linkBatch = new LinkBatch();
1116 foreach ( $titles as $title ) {
1117 if ( is_string( $title ) ) {
1118 $titleObj = Title
::newFromText( $title, $this->mDefaultNamespace
);
1123 // Handle invalid titles gracefully
1124 $this->mAllPages
[0][$title] = $this->mFakePageId
;
1125 $this->mInvalidTitles
[$this->mFakePageId
] = $title;
1126 $this->mFakePageId
--;
1127 continue; // There's nothing else we can do
1129 $unconvertedTitle = $titleObj->getPrefixedText();
1130 $titleWasConverted = false;
1131 if ( $titleObj->isExternal() ) {
1132 // This title is an interwiki link.
1133 $this->mInterwikiTitles
[$unconvertedTitle] = $titleObj->getInterwiki();
1135 // Variants checking
1137 if ( $this->mConvertTitles
&&
1138 count( $wgContLang->getVariants() ) > 1 &&
1139 !$titleObj->exists()
1141 // Language::findVariantLink will modify titleText and titleObj into
1142 // the canonical variant if possible
1143 $titleText = is_string( $title ) ?
$title : $titleObj->getPrefixedText();
1144 $wgContLang->findVariantLink( $titleText, $titleObj );
1145 $titleWasConverted = $unconvertedTitle !== $titleObj->getPrefixedText();
1148 if ( $titleObj->getNamespace() < 0 ) {
1149 // Handle Special and Media pages
1150 $titleObj = $titleObj->fixSpecialName();
1151 $this->mSpecialTitles
[$this->mFakePageId
] = $titleObj;
1152 $this->mFakePageId
--;
1155 $linkBatch->addObj( $titleObj );
1159 // Make sure we remember the original title that was
1160 // given to us. This way the caller can correlate new
1161 // titles with the originally requested when e.g. the
1162 // namespace is localized or the capitalization is
1164 if ( $titleWasConverted ) {
1165 $this->mConvertedTitles
[$unconvertedTitle] = $titleObj->getPrefixedText();
1166 // In this case the page can't be Special.
1167 if ( is_string( $title ) && $title !== $unconvertedTitle ) {
1168 $this->mNormalizedTitles
[$title] = $unconvertedTitle;
1170 } elseif ( is_string( $title ) && $title !== $titleObj->getPrefixedText() ) {
1171 $this->mNormalizedTitles
[$title] = $titleObj->getPrefixedText();
1174 // Need gender information
1175 if ( MWNamespace
::hasGenderDistinction( $titleObj->getNamespace() ) ) {
1176 $usernames[] = $titleObj->getText();
1179 // Get gender information
1180 $genderCache = GenderCache
::singleton();
1181 $genderCache->doQuery( $usernames, __METHOD__
);
1187 * Set data for a title.
1189 * This data may be extracted into an ApiResult using
1190 * self::populateGeneratorData. This should generally be limited to
1191 * data that is likely to be particularly useful to end users rather than
1192 * just being a dump of everything returned in non-generator mode.
1194 * Redirects here will *not* be followed, even if 'redirects' was
1195 * specified, since in the case of multiple redirects we can't know which
1196 * source's data to use on the target.
1198 * @param Title $title
1199 * @param array $data
1201 public function setGeneratorData( Title
$title, array $data ) {
1202 $ns = $title->getNamespace();
1203 $dbkey = $title->getDBkey();
1204 $this->mGeneratorData
[$ns][$dbkey] = $data;
1208 * Populate the generator data for all titles in the result
1210 * The page data may be inserted into an ApiResult object or into an
1211 * associative array. The $path parameter specifies the path within the
1212 * ApiResult or array to find the "pages" node.
1214 * The "pages" node itself must be an associative array mapping the page ID
1215 * or fake page ID values returned by this pageset (see
1216 * self::getAllTitlesByNamespace() and self::getSpecialTitles()) to
1217 * associative arrays of page data. Each of those subarrays will have the
1218 * data from self::setGeneratorData() merged in.
1220 * Data that was set by self::setGeneratorData() for pages not in the
1221 * "pages" node will be ignored.
1223 * @param ApiResult|array &$result
1224 * @param array $path
1225 * @return bool Whether the data fit
1227 public function populateGeneratorData( &$result, array $path = array() ) {
1228 if ( $result instanceof ApiResult
) {
1229 $data = $result->getData();
1233 foreach ( $path as $key ) {
1234 if ( !isset( $data[$key] ) ) {
1235 // Path isn't in $result, so nothing to add, so everything
1239 $data = &$data[$key];
1241 foreach ( $this->mGeneratorData
as $ns => $dbkeys ) {
1244 foreach ( $this->mSpecialTitles
as $id => $title ) {
1245 $pages[$title->getDBkey()] = $id;
1248 if ( !isset( $this->mAllPages
[$ns] ) ) {
1249 // No known titles in the whole namespace. Skip it.
1252 $pages = $this->mAllPages
[$ns];
1254 foreach ( $dbkeys as $dbkey => $genData ) {
1255 if ( !isset( $pages[$dbkey] ) ) {
1256 // Unknown title. Forget it.
1259 $pageId = $pages[$dbkey];
1260 if ( !isset( $data[$pageId] ) ) {
1261 // $pageId didn't make it into the result. Ignore it.
1265 if ( $result instanceof ApiResult
) {
1266 $path2 = array_merge( $path, array( $pageId ) );
1267 foreach ( $genData as $key => $value ) {
1268 if ( !$result->addValue( $path2, $key, $value ) ) {
1273 $data[$pageId] = array_merge( $data[$pageId], $genData );
1281 * Get the database connection (read-only)
1282 * @return DatabaseBase
1284 protected function getDB() {
1285 return $this->mDbSource
->getDB();
1289 * Returns the input array of integers with all values < 0 removed
1291 * @param array $array
1294 private static function getPositiveIntegers( $array ) {
1295 // bug 25734 API: possible issue with revids validation
1296 // It seems with a load of revision rows, MySQL gets upset
1297 // Remove any < 0 integers, as they can't be valid
1298 foreach ( $array as $i => $int ) {
1300 unset( $array[$i] );
1307 public function getAllowedParams( $flags = 0 ) {
1310 ApiBase
::PARAM_ISMULTI
=> true,
1311 ApiBase
::PARAM_HELP_MSG
=> 'api-pageset-param-titles',
1314 ApiBase
::PARAM_TYPE
=> 'integer',
1315 ApiBase
::PARAM_ISMULTI
=> true,
1316 ApiBase
::PARAM_HELP_MSG
=> 'api-pageset-param-pageids',
1319 ApiBase
::PARAM_TYPE
=> 'integer',
1320 ApiBase
::PARAM_ISMULTI
=> true,
1321 ApiBase
::PARAM_HELP_MSG
=> 'api-pageset-param-revids',
1323 'generator' => array(
1324 ApiBase
::PARAM_TYPE
=> null,
1325 ApiBase
::PARAM_VALUE_LINKS
=> array(),
1326 ApiBase
::PARAM_HELP_MSG
=> 'api-pageset-param-generator',
1328 'redirects' => array(
1329 ApiBase
::PARAM_DFLT
=> false,
1330 ApiBase
::PARAM_HELP_MSG
=> $this->mAllowGenerator
1331 ?
'api-pageset-param-redirects-generator'
1332 : 'api-pageset-param-redirects-nogenerator',
1334 'converttitles' => array(
1335 ApiBase
::PARAM_DFLT
=> false,
1336 ApiBase
::PARAM_HELP_MSG
=> array(
1337 'api-pageset-param-converttitles',
1338 new DeferredStringifier(
1339 function ( IContextSource
$context ) {
1340 return $context->getLanguage()
1341 ->commaList( LanguageConverter
::$languagesWithVariants );
1349 if ( !$this->mAllowGenerator
) {
1350 unset( $result['generator'] );
1351 } elseif ( $flags & ApiBase
::GET_VALUES_FOR_HELP
) {
1352 foreach ( $this->getGenerators() as $g ) {
1353 $result['generator'][ApiBase
::PARAM_TYPE
][] = $g;
1354 $result['generator'][ApiBase
::PARAM_VALUE_LINKS
][$g] = "Special:ApiHelp/query+$g";
1361 private static $generators = null;
1364 * Get an array of all available generators
1367 private function getGenerators() {
1368 if ( self
::$generators === null ) {
1369 $query = $this->mDbSource
;
1370 if ( !( $query instanceof ApiQuery
) ) {
1371 // If the parent container of this pageset is not ApiQuery,
1372 // we must create it to get module manager
1373 $query = $this->getMain()->getModuleManager()->getModule( 'query' );
1376 $mgr = $query->getModuleManager();
1377 foreach ( $mgr->getNamesWithClasses() as $name => $class ) {
1378 if ( is_subclass_of( $class, 'ApiQueryGeneratorBase' ) ) {
1383 self
::$generators = $gens;
1386 return self
::$generators;