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 $mFakePageId = -1;
75 private $mCacheMode = 'public';
76 private $mRequestedPageFields = array();
78 private $mDefaultNamespace = NS_MAIN
;
81 * Add all items from $values into the result
82 * @param array $result Output
83 * @param array $values Values to add
84 * @param string $flag The name of the boolean flag to mark this element
85 * @param string $name If given, name of the value
87 private static function addValues( array &$result, $values, $flag = null, $name = null ) {
88 foreach ( $values as $val ) {
89 if ( $val instanceof Title
) {
91 ApiQueryBase
::addTitleInfo( $v, $val );
92 } elseif ( $name !== null ) {
93 $v = array( $name => $val );
97 if ( $flag !== null ) {
105 * @param ApiBase $dbSource Module implementing getDB().
106 * Allows PageSet to reuse existing db connection from the shared state like ApiQuery.
107 * @param int $flags Zero or more flags like DISABLE_GENERATORS
108 * @param int $defaultNamespace The namespace to use if none is specified by a prefix.
109 * @since 1.21 accepts $flags instead of two boolean values
111 public function __construct( ApiBase
$dbSource, $flags = 0, $defaultNamespace = NS_MAIN
) {
112 parent
::__construct( $dbSource->getMain(), $dbSource->getModuleName() );
113 $this->mDbSource
= $dbSource;
114 $this->mAllowGenerator
= ( $flags & ApiPageSet
::DISABLE_GENERATORS
) == 0;
115 $this->mDefaultNamespace
= $defaultNamespace;
118 $this->mParams
= $this->extractRequestParams();
119 $this->mResolveRedirects
= $this->mParams
['redirects'];
120 $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 ) {
147 $generatorName = $this->mAllowGenerator ?
$this->mParams
['generator'] : null;
148 if ( isset( $generatorName ) ) {
149 $dbSource = $this->mDbSource
;
150 $isQuery = $dbSource instanceof ApiQuery
;
152 // If the parent container of this pageset is not ApiQuery, we must create it to run generator
153 $dbSource = $this->getMain()->getModuleManager()->getModule( 'query' );
154 // Enable profiling for query module because it will be used for db sql profiling
155 $dbSource->profileIn();
157 $generator = $dbSource->getModuleManager()->getModule( $generatorName, null, true );
158 if ( $generator === null ) {
159 $this->dieUsage( 'Unknown generator=' . $generatorName, 'badgenerator' );
161 if ( !$generator instanceof ApiQueryGeneratorBase
) {
162 $this->dieUsage( "Module $generatorName cannot be used as a generator", 'badgenerator' );
164 // Create a temporary pageset to store generator's output,
165 // add any additional fields generator may need, and execute pageset to populate titles/pageids
166 $tmpPageSet = new ApiPageSet( $dbSource, ApiPageSet
::DISABLE_GENERATORS
);
167 $generator->setGeneratorMode( $tmpPageSet );
168 $this->mCacheMode
= $generator->getCacheMode( $generator->extractRequestParams() );
171 $generator->requestExtraData( $tmpPageSet );
173 $tmpPageSet->executeInternal( $isDryRun );
175 // populate this pageset with the generator output
177 $generator->profileIn();
180 $generator->executeGenerator( $this );
181 wfRunHooks( 'APIQueryGeneratorAfterExecute', array( &$generator, &$this ) );
183 // Prevent warnings from being reported on these parameters
184 $main = $this->getMain();
185 foreach ( $generator->extractRequestParams() as $paramName => $param ) {
186 $main->getVal( $generator->encodeParamName( $paramName ) );
189 $generator->profileOut();
193 $this->resolvePendingRedirects();
197 // If this pageset is not part of the query, we called profileIn() above
198 $dbSource->profileOut();
201 // Only one of the titles/pageids/revids is allowed at the same time
203 if ( isset( $this->mParams
['titles'] ) ) {
204 $dataSource = 'titles';
206 if ( isset( $this->mParams
['pageids'] ) ) {
207 if ( isset( $dataSource ) ) {
208 $this->dieUsage( "Cannot use 'pageids' at the same time as '$dataSource'", 'multisource' );
210 $dataSource = 'pageids';
212 if ( isset( $this->mParams
['revids'] ) ) {
213 if ( isset( $dataSource ) ) {
214 $this->dieUsage( "Cannot use 'revids' at the same time as '$dataSource'", 'multisource' );
216 $dataSource = 'revids';
220 // Populate page information with the original user input
221 switch ( $dataSource ) {
223 $this->initFromTitles( $this->mParams
['titles'] );
226 $this->initFromPageIds( $this->mParams
['pageids'] );
229 if ( $this->mResolveRedirects
) {
230 $this->setWarning( 'Redirect resolution cannot be used ' .
231 'together with the revids= parameter. Any redirects ' .
232 'the revids= point to have not been resolved.' );
234 $this->mResolveRedirects
= false;
235 $this->initFromRevIDs( $this->mParams
['revids'] );
238 // Do nothing - some queries do not need any of the data sources.
247 * Check whether this PageSet is resolving redirects
250 public function isResolvingRedirects() {
251 return $this->mResolveRedirects
;
255 * Return the parameter name that is the source of data for this PageSet
257 * If multiple source parameters are specified (e.g. titles and pageids),
258 * one will be named arbitrarily.
260 * @return string|null
262 public function getDataSource() {
263 if ( $this->mAllowGenerator
&& isset( $this->mParams
['generator'] ) ) {
266 if ( isset( $this->mParams
['titles'] ) ) {
269 if ( isset( $this->mParams
['pageids'] ) ) {
272 if ( isset( $this->mParams
['revids'] ) ) {
280 * Request an additional field from the page table.
281 * Must be called before execute()
282 * @param string $fieldName Field name
284 public function requestField( $fieldName ) {
285 $this->mRequestedPageFields
[$fieldName] = null;
289 * Get the value of a custom field previously requested through
291 * @param string $fieldName Field name
292 * @return mixed Field value
294 public function getCustomField( $fieldName ) {
295 return $this->mRequestedPageFields
[$fieldName];
299 * Get the fields that have to be queried from the page table:
300 * the ones requested through requestField() and a few basic ones
302 * @return array Array of field names
304 public function getPageTableFields() {
305 // Ensure we get minimum required fields
306 // DON'T change this order
308 'page_namespace' => null,
309 'page_title' => null,
313 if ( $this->mResolveRedirects
) {
314 $pageFlds['page_is_redirect'] = null;
317 // only store non-default fields
318 $this->mRequestedPageFields
= array_diff_key( $this->mRequestedPageFields
, $pageFlds );
320 $pageFlds = array_merge( $pageFlds, $this->mRequestedPageFields
);
322 return array_keys( $pageFlds );
326 * Returns an array [ns][dbkey] => page_id for all requested titles.
327 * page_id is a unique negative number in case title was not found.
328 * Invalid titles will also have negative page IDs and will be in namespace 0
331 public function getAllTitlesByNamespace() {
332 return $this->mAllPages
;
336 * All Title objects provided.
339 public function getTitles() {
340 return $this->mTitles
;
344 * Returns the number of unique pages (not revisions) in the set.
347 public function getTitleCount() {
348 return count( $this->mTitles
);
352 * Returns an array [ns][dbkey] => page_id for all good titles.
355 public function getGoodTitlesByNamespace() {
356 return $this->mGoodPages
;
360 * Title objects that were found in the database.
361 * @return Title[] Array page_id (int) => Title (obj)
363 public function getGoodTitles() {
364 return $this->mGoodTitles
;
368 * Returns the number of found unique pages (not revisions) in the set.
371 public function getGoodTitleCount() {
372 return count( $this->mGoodTitles
);
376 * Returns an array [ns][dbkey] => fake_page_id for all missing titles.
377 * fake_page_id is a unique negative number.
380 public function getMissingTitlesByNamespace() {
381 return $this->mMissingPages
;
385 * Title objects that were NOT found in the database.
386 * The array's index will be negative for each item
389 public function getMissingTitles() {
390 return $this->mMissingTitles
;
394 * Returns an array [ns][dbkey] => page_id for all good and missing titles.
397 public function getGoodAndMissingTitlesByNamespace() {
398 return $this->mGoodAndMissingPages
;
402 * Title objects for good and missing titles.
405 public function getGoodAndMissingTitles() {
406 return $this->mGoodTitles +
$this->mMissingTitles
;
410 * Titles that were deemed invalid by Title::newFromText()
411 * The array's index will be unique and negative for each item
412 * @return string[] Array of strings (not Title objects)
414 public function getInvalidTitles() {
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();
454 if ( !empty( $values ) && $result ) {
455 $result->setIndexedTagName( $values, 'r' );
462 * Get a list of title normalizations - maps a title to its normalized
464 * @return array Array of raw_prefixed_title (string) => prefixed_title (string)
466 public function getNormalizedTitles() {
467 return $this->mNormalizedTitles
;
471 * Get a list of title normalizations - maps a title to its normalized
472 * version in the form of result array.
473 * @param ApiResult $result
474 * @return array Array of raw_prefixed_title (string) => prefixed_title (string)
477 public function getNormalizedTitlesAsResult( $result = null ) {
479 foreach ( $this->getNormalizedTitles() as $rawTitleStr => $titleStr ) {
481 'from' => $rawTitleStr,
485 if ( !empty( $values ) && $result ) {
486 $result->setIndexedTagName( $values, 'n' );
493 * Get a list of title conversions - maps a title to its converted
495 * @return array Array of raw_prefixed_title (string) => prefixed_title (string)
497 public function getConvertedTitles() {
498 return $this->mConvertedTitles
;
502 * Get a list of title conversions - maps a title to its converted
503 * version as a result array.
504 * @param ApiResult $result
505 * @return array Array of (from, to) strings
508 public function getConvertedTitlesAsResult( $result = null ) {
510 foreach ( $this->getConvertedTitles() as $rawTitleStr => $titleStr ) {
512 'from' => $rawTitleStr,
516 if ( !empty( $values ) && $result ) {
517 $result->setIndexedTagName( $values, 'c' );
524 * Get a list of interwiki titles - maps a title to its interwiki
526 * @return array Array of raw_prefixed_title (string) => interwiki_prefix (string)
528 public function getInterwikiTitles() {
529 return $this->mInterwikiTitles
;
533 * Get a list of interwiki titles - maps a title to its interwiki
535 * @param ApiResult $result
537 * @return array Array of raw_prefixed_title (string) => interwiki_prefix (string)
540 public function getInterwikiTitlesAsResult( $result = null, $iwUrl = false ) {
542 foreach ( $this->getInterwikiTitles() as $rawTitleStr => $interwikiStr ) {
544 'title' => $rawTitleStr,
545 'iw' => $interwikiStr,
548 $title = Title
::newFromText( $rawTitleStr );
549 $item['url'] = $title->getFullURL( '', false, PROTO_CURRENT
);
553 if ( !empty( $values ) && $result ) {
554 $result->setIndexedTagName( $values, 'i' );
561 * Get an array of invalid/special/missing titles.
563 * @param array $invalidChecks List of types of invalid titles to include.
564 * Recognized values are:
565 * - invalidTitles: Titles from $this->getInvalidTitles()
566 * - special: Titles from $this->getSpecialTitles()
567 * - missingIds: ids from $this->getMissingPageIDs()
568 * - missingRevIds: ids from $this->getMissingRevisionIDs()
569 * - missingTitles: Titles from $this->getMissingTitles()
570 * - interwikiTitles: Titles from $this->getInterwikiTitlesAsResult()
571 * @return array Array suitable for inclusion in the response
574 public function getInvalidTitlesAndRevisions( $invalidChecks = array( 'invalidTitles',
575 'special', 'missingIds', 'missingRevIds', 'missingTitles', 'interwikiTitles' )
578 if ( in_array( "invalidTitles", $invalidChecks ) ) {
579 self
::addValues( $result, $this->getInvalidTitles(), 'invalid', 'title' );
581 if ( in_array( "special", $invalidChecks ) ) {
582 self
::addValues( $result, $this->getSpecialTitles(), 'special', 'title' );
584 if ( in_array( "missingIds", $invalidChecks ) ) {
585 self
::addValues( $result, $this->getMissingPageIDs(), 'missing', 'pageid' );
587 if ( in_array( "missingRevIds", $invalidChecks ) ) {
588 self
::addValues( $result, $this->getMissingRevisionIDs(), 'missing', 'revid' );
590 if ( in_array( "missingTitles", $invalidChecks ) ) {
591 self
::addValues( $result, $this->getMissingTitles(), 'missing' );
593 if ( in_array( "interwikiTitles", $invalidChecks ) ) {
594 self
::addValues( $result, $this->getInterwikiTitlesAsResult() );
601 * Get the list of valid revision IDs (requested with the revids= parameter)
602 * @return array Array of revID (int) => pageID (int)
604 public function getRevisionIDs() {
605 return $this->mGoodRevIDs
;
609 * Get the list of non-deleted revision IDs (requested with the revids= parameter)
610 * @return array Array of revID (int) => pageID (int)
612 public function getLiveRevisionIDs() {
613 return $this->mLiveRevIDs
;
617 * Get the list of revision IDs that were associated with deleted titles.
618 * @return array Array of revID (int) => pageID (int)
620 public function getDeletedRevisionIDs() {
621 return $this->mDeletedRevIDs
;
625 * Revision IDs that were not found in the database
626 * @return array Array of revision IDs
628 public function getMissingRevisionIDs() {
629 return $this->mMissingRevIDs
;
633 * Revision IDs that were not found in the database as result array.
634 * @param ApiResult $result
635 * @return array Array of revision IDs
638 public function getMissingRevisionIDsAsResult( $result = null ) {
640 foreach ( $this->getMissingRevisionIDs() as $revid ) {
641 $values[$revid] = array(
645 if ( !empty( $values ) && $result ) {
646 $result->setIndexedTagName( $values, 'rev' );
653 * Get the list of titles with negative namespace
656 public function getSpecialTitles() {
657 return $this->mSpecialTitles
;
661 * Returns the number of revisions (requested with revids= parameter).
662 * @return int Number of revisions.
664 public function getRevisionCount() {
665 return count( $this->getRevisionIDs() );
669 * Populate this PageSet from a list of Titles
670 * @param array $titles Array of Title objects
672 public function populateFromTitles( $titles ) {
674 $this->initFromTitles( $titles );
679 * Populate this PageSet from a list of page IDs
680 * @param array $pageIDs Array of page IDs
682 public function populateFromPageIDs( $pageIDs ) {
684 $this->initFromPageIds( $pageIDs );
689 * Populate this PageSet from a rowset returned from the database
691 * Note that the query result must include the columns returned by
692 * $this->getPageTableFields().
694 * @param DatabaseBase $db
695 * @param ResultWrapper $queryResult Query result object
697 public function populateFromQueryResult( $db, $queryResult ) {
699 $this->initFromQueryResult( $queryResult );
704 * Populate this PageSet from a list of revision IDs
705 * @param array $revIDs Array of revision IDs
707 public function populateFromRevisionIDs( $revIDs ) {
709 $this->initFromRevIDs( $revIDs );
714 * Extract all requested fields from the row received from the database
715 * @param stdClass $row Result row
717 public function processDbRow( $row ) {
718 // Store Title object in various data structures
719 $title = Title
::newFromRow( $row );
721 $pageId = intval( $row->page_id
);
722 $this->mAllPages
[$row->page_namespace
][$row->page_title
] = $pageId;
723 $this->mTitles
[] = $title;
725 if ( $this->mResolveRedirects
&& $row->page_is_redirect
== '1' ) {
726 $this->mPendingRedirectIDs
[$pageId] = $title;
728 $this->mGoodPages
[$row->page_namespace
][$row->page_title
] = $pageId;
729 $this->mGoodAndMissingPages
[$row->page_namespace
][$row->page_title
] = $pageId;
730 $this->mGoodTitles
[$pageId] = $title;
733 foreach ( $this->mRequestedPageFields
as $fieldName => &$fieldValues ) {
734 $fieldValues[$pageId] = $row->$fieldName;
739 * Do not use, does nothing, will be removed
740 * @deprecated since 1.21
742 public function finishPageSetGeneration() {
743 wfDeprecated( __METHOD__
, '1.21' );
747 * This method populates internal variables with page information
748 * based on the given array of title strings.
751 * #1 For each title, get data from `page` table
752 * #2 If page was not found in the DB, store it as missing
754 * Additionally, when resolving redirects:
755 * #3 If no more redirects left, stop.
756 * #4 For each redirect, get its target from the `redirect` table.
757 * #5 Substitute the original LinkBatch object with the new list
758 * #6 Repeat from step #1
760 * @param array $titles Array of Title objects or strings
762 private function initFromTitles( $titles ) {
763 // Get validated and normalized title objects
764 $linkBatch = $this->processTitlesArray( $titles );
765 if ( $linkBatch->isEmpty() ) {
769 $db = $this->getDB();
770 $set = $linkBatch->constructSet( 'page', $db );
772 // Get pageIDs data from the `page` table
773 $this->profileDBIn();
774 $res = $db->select( 'page', $this->getPageTableFields(), $set,
776 $this->profileDBOut();
778 // Hack: get the ns:titles stored in array(ns => array(titles)) format
779 $this->initFromQueryResult( $res, $linkBatch->data
, true ); // process Titles
781 // Resolve any found redirects
782 $this->resolvePendingRedirects();
786 * Does the same as initFromTitles(), but is based on page IDs instead
787 * @param array $pageids Array of page IDs
789 private function initFromPageIds( $pageids ) {
794 $pageids = array_map( 'intval', $pageids ); // paranoia
795 $remaining = array_flip( $pageids );
797 $pageids = self
::getPositiveIntegers( $pageids );
800 if ( !empty( $pageids ) ) {
802 'page_id' => $pageids
804 $db = $this->getDB();
806 // Get pageIDs data from the `page` table
807 $this->profileDBIn();
808 $res = $db->select( 'page', $this->getPageTableFields(), $set,
810 $this->profileDBOut();
813 $this->initFromQueryResult( $res, $remaining, false ); // process PageIDs
815 // Resolve any found redirects
816 $this->resolvePendingRedirects();
820 * Iterate through the result of the query on 'page' table,
821 * and for each row create and store title object and save any extra fields requested.
822 * @param ResultWrapper $res DB Query result
823 * @param array $remaining Array of either pageID or ns/title elements (optional).
824 * If given, any missing items will go to $mMissingPageIDs and $mMissingTitles
825 * @param bool $processTitles Must be provided together with $remaining.
826 * If true, treat $remaining as an array of [ns][title]
827 * If false, treat it as an array of [pageIDs]
829 private function initFromQueryResult( $res, &$remaining = null, $processTitles = null ) {
830 if ( !is_null( $remaining ) && is_null( $processTitles ) ) {
831 ApiBase
::dieDebug( __METHOD__
, 'Missing $processTitles parameter when $remaining is provided' );
834 $usernames = array();
836 foreach ( $res as $row ) {
837 $pageId = intval( $row->page_id
);
839 // Remove found page from the list of remaining items
840 if ( isset( $remaining ) ) {
841 if ( $processTitles ) {
842 unset( $remaining[$row->page_namespace
][$row->page_title
] );
844 unset( $remaining[$pageId] );
848 // Store any extra fields requested by modules
849 $this->processDbRow( $row );
851 // Need gender information
852 if ( MWNamespace
::hasGenderDistinction( $row->page_namespace
) ) {
853 $usernames[] = $row->page_title
;
858 if ( isset( $remaining ) ) {
859 // Any items left in the $remaining list are added as missing
860 if ( $processTitles ) {
861 // The remaining titles in $remaining are non-existent pages
862 foreach ( $remaining as $ns => $dbkeys ) {
863 foreach ( array_keys( $dbkeys ) as $dbkey ) {
864 $title = Title
::makeTitle( $ns, $dbkey );
865 $this->mAllPages
[$ns][$dbkey] = $this->mFakePageId
;
866 $this->mMissingPages
[$ns][$dbkey] = $this->mFakePageId
;
867 $this->mGoodAndMissingPages
[$ns][$dbkey] = $this->mFakePageId
;
868 $this->mMissingTitles
[$this->mFakePageId
] = $title;
869 $this->mFakePageId
--;
870 $this->mTitles
[] = $title;
872 // need gender information
873 if ( MWNamespace
::hasGenderDistinction( $ns ) ) {
874 $usernames[] = $dbkey;
879 // The remaining pageids do not exist
880 if ( !$this->mMissingPageIDs
) {
881 $this->mMissingPageIDs
= array_keys( $remaining );
883 $this->mMissingPageIDs
= array_merge( $this->mMissingPageIDs
, array_keys( $remaining ) );
888 // Get gender information
889 $genderCache = GenderCache
::singleton();
890 $genderCache->doQuery( $usernames, __METHOD__
);
894 * Does the same as initFromTitles(), but is based on revision IDs
896 * @param array $revids Array of revision IDs
898 private function initFromRevIDs( $revids ) {
903 $revids = array_map( 'intval', $revids ); // paranoia
904 $db = $this->getDB();
906 $remaining = array_flip( $revids );
908 $revids = self
::getPositiveIntegers( $revids );
910 if ( !empty( $revids ) ) {
911 $tables = array( 'revision', 'page' );
912 $fields = array( 'rev_id', 'rev_page' );
913 $where = array( 'rev_id' => $revids, 'rev_page = page_id' );
915 // Get pageIDs data from the `page` table
916 $this->profileDBIn();
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] );
926 $this->profileDBOut();
929 $this->mMissingRevIDs
= array_keys( $remaining );
931 // Populate all the page information
932 $this->initFromPageIds( array_keys( $pageids ) );
934 // If the user can see deleted revisions, pull out the corresponding
935 // titles from the archive table and include them too. We ignore
936 // ar_page_id because deleted revisions are tied by title, not page_id.
937 if ( !empty( $this->mMissingRevIDs
) && $this->getUser()->isAllowed( 'deletedhistory' ) ) {
938 $remaining = array_flip( $this->mMissingRevIDs
);
939 $tables = array( 'archive' );
940 $fields = array( 'ar_rev_id', 'ar_namespace', 'ar_title' );
941 $where = array( 'ar_rev_id' => $this->mMissingRevIDs
);
943 $this->profileDBIn();
944 $res = $db->select( $tables, $fields, $where, __METHOD__
);
946 foreach ( $res as $row ) {
947 $revid = intval( $row->ar_rev_id
);
948 $titles[$revid] = Title
::makeTitle( $row->ar_namespace
, $row->ar_title
);
949 unset( $remaining[$revid] );
951 $this->profileDBOut();
953 $this->initFromTitles( $titles );
955 foreach ( $titles as $revid => $title ) {
956 $ns = $title->getNamespace();
957 $dbkey = $title->getDBkey();
959 // Handle converted titles
960 if ( !isset( $this->mAllPages
[$ns][$dbkey] ) &&
961 isset( $this->mConvertedTitles
[$title->getPrefixedText()] )
963 $title = Title
::newFromText( $this->mConvertedTitles
[$title->getPrefixedText()] );
964 $ns = $title->getNamespace();
965 $dbkey = $title->getDBkey();
968 if ( isset( $this->mAllPages
[$ns][$dbkey] ) ) {
969 $this->mGoodRevIDs
[$revid] = $this->mAllPages
[$ns][$dbkey];
970 $this->mDeletedRevIDs
[$revid] = $this->mAllPages
[$ns][$dbkey];
972 $remaining[$revid] = true;
976 $this->mMissingRevIDs
= array_keys( $remaining );
981 * Resolve any redirects in the result if redirect resolution was
982 * requested. This function is called repeatedly until all redirects
983 * have been resolved.
985 private function resolvePendingRedirects() {
986 if ( $this->mResolveRedirects
) {
987 $db = $this->getDB();
988 $pageFlds = $this->getPageTableFields();
990 // Repeat until all redirects have been resolved
991 // The infinite loop is prevented by keeping all known pages in $this->mAllPages
992 while ( $this->mPendingRedirectIDs
) {
993 // Resolve redirects by querying the pagelinks table, and repeat the process
994 // Create a new linkBatch object for the next pass
995 $linkBatch = $this->getRedirectTargets();
997 if ( $linkBatch->isEmpty() ) {
1001 $set = $linkBatch->constructSet( 'page', $db );
1002 if ( $set === false ) {
1006 // Get pageIDs data from the `page` table
1007 $this->profileDBIn();
1008 $res = $db->select( 'page', $pageFlds, $set, __METHOD__
);
1009 $this->profileDBOut();
1011 // Hack: get the ns:titles stored in array(ns => array(titles)) format
1012 $this->initFromQueryResult( $res, $linkBatch->data
, true );
1018 * Get the targets of the pending redirects from the database
1020 * Also creates entries in the redirect table for redirects that don't
1024 private function getRedirectTargets() {
1025 $lb = new LinkBatch();
1026 $db = $this->getDB();
1028 $this->profileDBIn();
1037 ), array( 'rd_from' => array_keys( $this->mPendingRedirectIDs
) ),
1040 $this->profileDBOut();
1041 foreach ( $res as $row ) {
1042 $rdfrom = intval( $row->rd_from
);
1043 $from = $this->mPendingRedirectIDs
[$rdfrom]->getPrefixedText();
1044 $to = Title
::makeTitle(
1050 unset( $this->mPendingRedirectIDs
[$rdfrom] );
1051 if ( !$to->isExternal() && !isset( $this->mAllPages
[$row->rd_namespace
][$row->rd_title
] ) ) {
1052 $lb->add( $row->rd_namespace
, $row->rd_title
);
1054 $this->mRedirectTitles
[$from] = $to;
1057 if ( $this->mPendingRedirectIDs
) {
1058 // We found pages that aren't in the redirect table
1060 foreach ( $this->mPendingRedirectIDs
as $id => $title ) {
1061 $page = WikiPage
::factory( $title );
1062 $rt = $page->insertRedirect();
1064 // What the hell. Let's just ignore this
1068 $this->mRedirectTitles
[$title->getPrefixedText()] = $rt;
1069 unset( $this->mPendingRedirectIDs
[$id] );
1077 * Get the cache mode for the data generated by this module.
1078 * All PageSet users should take into account whether this returns a more-restrictive
1079 * cache mode than the using module itself. For possible return values and other
1080 * details about cache modes, see ApiMain::setCacheMode()
1082 * Public caching will only be allowed if *all* the modules that supply
1083 * data for a given request return a cache mode of public.
1085 * @param array|null $params
1089 public function getCacheMode( $params = null ) {
1090 return $this->mCacheMode
;
1094 * Given an array of title strings, convert them into Title objects.
1095 * Alternatively, an array of Title objects may be given.
1096 * This method validates access rights for the title,
1097 * and appends normalization values to the output.
1099 * @param array $titles Array of Title objects or strings
1102 private function processTitlesArray( $titles ) {
1103 $usernames = array();
1104 $linkBatch = new LinkBatch();
1106 foreach ( $titles as $title ) {
1107 if ( is_string( $title ) ) {
1108 $titleObj = Title
::newFromText( $title, $this->mDefaultNamespace
);
1113 // Handle invalid titles gracefully
1114 $this->mAllPages
[0][$title] = $this->mFakePageId
;
1115 $this->mInvalidTitles
[$this->mFakePageId
] = $title;
1116 $this->mFakePageId
--;
1117 continue; // There's nothing else we can do
1119 $unconvertedTitle = $titleObj->getPrefixedText();
1120 $titleWasConverted = false;
1121 if ( $titleObj->isExternal() ) {
1122 // This title is an interwiki link.
1123 $this->mInterwikiTitles
[$unconvertedTitle] = $titleObj->getInterwiki();
1125 // Variants checking
1127 if ( $this->mConvertTitles
&&
1128 count( $wgContLang->getVariants() ) > 1 &&
1129 !$titleObj->exists()
1131 // Language::findVariantLink will modify titleText and titleObj into
1132 // the canonical variant if possible
1133 $titleText = is_string( $title ) ?
$title : $titleObj->getPrefixedText();
1134 $wgContLang->findVariantLink( $titleText, $titleObj );
1135 $titleWasConverted = $unconvertedTitle !== $titleObj->getPrefixedText();
1138 if ( $titleObj->getNamespace() < 0 ) {
1139 // Handle Special and Media pages
1140 $titleObj = $titleObj->fixSpecialName();
1141 $this->mSpecialTitles
[$this->mFakePageId
] = $titleObj;
1142 $this->mFakePageId
--;
1145 $linkBatch->addObj( $titleObj );
1149 // Make sure we remember the original title that was
1150 // given to us. This way the caller can correlate new
1151 // titles with the originally requested when e.g. the
1152 // namespace is localized or the capitalization is
1154 if ( $titleWasConverted ) {
1155 $this->mConvertedTitles
[$unconvertedTitle] = $titleObj->getPrefixedText();
1156 // In this case the page can't be Special.
1157 if ( is_string( $title ) && $title !== $unconvertedTitle ) {
1158 $this->mNormalizedTitles
[$title] = $unconvertedTitle;
1160 } elseif ( is_string( $title ) && $title !== $titleObj->getPrefixedText() ) {
1161 $this->mNormalizedTitles
[$title] = $titleObj->getPrefixedText();
1164 // Need gender information
1165 if ( MWNamespace
::hasGenderDistinction( $titleObj->getNamespace() ) ) {
1166 $usernames[] = $titleObj->getText();
1169 // Get gender information
1170 $genderCache = GenderCache
::singleton();
1171 $genderCache->doQuery( $usernames, __METHOD__
);
1177 * Get the database connection (read-only)
1178 * @return DatabaseBase
1180 protected function getDB() {
1181 return $this->mDbSource
->getDB();
1185 * Returns the input array of integers with all values < 0 removed
1187 * @param array $array
1190 private static function getPositiveIntegers( $array ) {
1191 // bug 25734 API: possible issue with revids validation
1192 // It seems with a load of revision rows, MySQL gets upset
1193 // Remove any < 0 integers, as they can't be valid
1194 foreach ( $array as $i => $int ) {
1196 unset( $array[$i] );
1203 public function getAllowedParams( $flags = 0 ) {
1206 ApiBase
::PARAM_ISMULTI
=> true,
1207 ApiBase
::PARAM_HELP_MSG
=> 'api-pageset-param-titles',
1210 ApiBase
::PARAM_TYPE
=> 'integer',
1211 ApiBase
::PARAM_ISMULTI
=> true,
1212 ApiBase
::PARAM_HELP_MSG
=> 'api-pageset-param-pageids',
1215 ApiBase
::PARAM_TYPE
=> 'integer',
1216 ApiBase
::PARAM_ISMULTI
=> true,
1217 ApiBase
::PARAM_HELP_MSG
=> 'api-pageset-param-revids',
1219 'generator' => array(
1220 ApiBase
::PARAM_TYPE
=> null,
1221 ApiBase
::PARAM_VALUE_LINKS
=> array(),
1222 ApiBase
::PARAM_HELP_MSG
=> 'api-pageset-param-generator',
1224 'redirects' => array(
1225 ApiBase
::PARAM_DFLT
=> false,
1226 ApiBase
::PARAM_HELP_MSG
=> $this->mAllowGenerator
1227 ?
'api-pageset-param-redirects-generator'
1228 : 'api-pageset-param-redirects-nogenerator',
1230 'converttitles' => array(
1231 ApiBase
::PARAM_DFLT
=> false,
1232 ApiBase
::PARAM_HELP_MSG
=> array(
1233 'api-pageset-param-converttitles',
1234 $this->getLanguage()->commaList( LanguageConverter
::$languagesWithVariants ),
1239 if ( !$this->mAllowGenerator
) {
1240 unset( $result['generator'] );
1241 } elseif ( $flags & ApiBase
::GET_VALUES_FOR_HELP
) {
1242 $result['generator'][ApiBase
::PARAM_TYPE
] = $this->getGenerators();
1243 foreach ( $result['generator'][ApiBase
::PARAM_TYPE
] as $g ) {
1244 $result['generator'][ApiBase
::PARAM_TYPE
][] = $g;
1245 $result['generator'][ApiBase
::PARAM_VALUE_LINKS
][$g] = "Special:ApiHelp/query+$g";
1252 private static $generators = null;
1255 * Get an array of all available generators
1258 private function getGenerators() {
1259 if ( self
::$generators === null ) {
1260 $query = $this->mDbSource
;
1261 if ( !( $query instanceof ApiQuery
) ) {
1262 // If the parent container of this pageset is not ApiQuery,
1263 // we must create it to get module manager
1264 $query = $this->getMain()->getModuleManager()->getModule( 'query' );
1267 $mgr = $query->getModuleManager();
1268 foreach ( $mgr->getNamesWithClasses() as $name => $class ) {
1269 if ( is_subclass_of( $class, 'ApiQueryGeneratorBase' ) ) {
1274 self
::$generators = $gens;
1277 return self
::$generators;