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 $mMissingRevIDs = array();
72 private $mFakePageId = -1;
73 private $mCacheMode = 'public';
74 private $mRequestedPageFields = array();
76 private $mDefaultNamespace = NS_MAIN
;
79 * Add all items from $values into the result
80 * @param array $result Output
81 * @param array $values Values to add
82 * @param string $flag The name of the boolean flag to mark this element
83 * @param string $name If given, name of the value
85 private static function addValues( array &$result, $values, $flag = null, $name = null ) {
86 foreach ( $values as $val ) {
87 if ( $val instanceof Title
) {
89 ApiQueryBase
::addTitleInfo( $v, $val );
90 } elseif ( $name !== null ) {
91 $v = array( $name => $val );
95 if ( $flag !== null ) {
103 * @param ApiBase $dbSource Module implementing getDB().
104 * Allows PageSet to reuse existing db connection from the shared state like ApiQuery.
105 * @param int $flags Zero or more flags like DISABLE_GENERATORS
106 * @param int $defaultNamespace The namespace to use if none is specified by a prefix.
107 * @since 1.21 accepts $flags instead of two boolean values
109 public function __construct( ApiBase
$dbSource, $flags = 0, $defaultNamespace = NS_MAIN
) {
110 parent
::__construct( $dbSource->getMain(), $dbSource->getModuleName() );
111 $this->mDbSource
= $dbSource;
112 $this->mAllowGenerator
= ( $flags & ApiPageSet
::DISABLE_GENERATORS
) == 0;
113 $this->mDefaultNamespace
= $defaultNamespace;
116 $this->mParams
= $this->extractRequestParams();
117 $this->mResolveRedirects
= $this->mParams
['redirects'];
118 $this->mConvertTitles
= $this->mParams
['converttitles'];
123 * In case execute() is not called, call this method to mark all relevant parameters as used
124 * This prevents unused parameters from being reported as warnings
126 public function executeDryRun() {
127 $this->executeInternal( true );
131 * Populate the PageSet from the request parameters.
133 public function execute() {
134 $this->executeInternal( false );
138 * Populate the PageSet from the request parameters.
139 * @param bool $isDryRun If true, instantiates generator, but only to mark
140 * relevant parameters as used
142 private function executeInternal( $isDryRun ) {
145 $generatorName = $this->mAllowGenerator ?
$this->mParams
['generator'] : null;
146 if ( isset( $generatorName ) ) {
147 $dbSource = $this->mDbSource
;
148 $isQuery = $dbSource instanceof ApiQuery
;
150 // If the parent container of this pageset is not ApiQuery, we must create it to run generator
151 $dbSource = $this->getMain()->getModuleManager()->getModule( 'query' );
152 // Enable profiling for query module because it will be used for db sql profiling
153 $dbSource->profileIn();
155 $generator = $dbSource->getModuleManager()->getModule( $generatorName, null, true );
156 if ( $generator === null ) {
157 $this->dieUsage( 'Unknown generator=' . $generatorName, 'badgenerator' );
159 if ( !$generator instanceof ApiQueryGeneratorBase
) {
160 $this->dieUsage( "Module $generatorName cannot be used as a generator", 'badgenerator' );
162 // Create a temporary pageset to store generator's output,
163 // add any additional fields generator may need, and execute pageset to populate titles/pageids
164 $tmpPageSet = new ApiPageSet( $dbSource, ApiPageSet
::DISABLE_GENERATORS
);
165 $generator->setGeneratorMode( $tmpPageSet );
166 $this->mCacheMode
= $generator->getCacheMode( $generator->extractRequestParams() );
169 $generator->requestExtraData( $tmpPageSet );
171 $tmpPageSet->executeInternal( $isDryRun );
173 // populate this pageset with the generator output
175 $generator->profileIn();
178 $generator->executeGenerator( $this );
179 wfRunHooks( 'APIQueryGeneratorAfterExecute', array( &$generator, &$this ) );
181 // Prevent warnings from being reported on these parameters
182 $main = $this->getMain();
183 foreach ( $generator->extractRequestParams() as $paramName => $param ) {
184 $main->getVal( $generator->encodeParamName( $paramName ) );
187 $generator->profileOut();
191 $this->resolvePendingRedirects();
195 // If this pageset is not part of the query, we called profileIn() above
196 $dbSource->profileOut();
199 // Only one of the titles/pageids/revids is allowed at the same time
201 if ( isset( $this->mParams
['titles'] ) ) {
202 $dataSource = 'titles';
204 if ( isset( $this->mParams
['pageids'] ) ) {
205 if ( isset( $dataSource ) ) {
206 $this->dieUsage( "Cannot use 'pageids' at the same time as '$dataSource'", 'multisource' );
208 $dataSource = 'pageids';
210 if ( isset( $this->mParams
['revids'] ) ) {
211 if ( isset( $dataSource ) ) {
212 $this->dieUsage( "Cannot use 'revids' at the same time as '$dataSource'", 'multisource' );
214 $dataSource = 'revids';
218 // Populate page information with the original user input
219 switch ( $dataSource ) {
221 $this->initFromTitles( $this->mParams
['titles'] );
224 $this->initFromPageIds( $this->mParams
['pageids'] );
227 if ( $this->mResolveRedirects
) {
228 $this->setWarning( 'Redirect resolution cannot be used ' .
229 'together with the revids= parameter. Any redirects ' .
230 'the revids= point to have not been resolved.' );
232 $this->mResolveRedirects
= false;
233 $this->initFromRevIDs( $this->mParams
['revids'] );
236 // Do nothing - some queries do not need any of the data sources.
245 * Check whether this PageSet is resolving redirects
248 public function isResolvingRedirects() {
249 return $this->mResolveRedirects
;
253 * Return the parameter name that is the source of data for this PageSet
255 * If multiple source parameters are specified (e.g. titles and pageids),
256 * one will be named arbitrarily.
258 * @return string|null
260 public function getDataSource() {
261 if ( $this->mAllowGenerator
&& isset( $this->mParams
['generator'] ) ) {
264 if ( isset( $this->mParams
['titles'] ) ) {
267 if ( isset( $this->mParams
['pageids'] ) ) {
270 if ( isset( $this->mParams
['revids'] ) ) {
278 * Request an additional field from the page table.
279 * Must be called before execute()
280 * @param string $fieldName Field name
282 public function requestField( $fieldName ) {
283 $this->mRequestedPageFields
[$fieldName] = null;
287 * Get the value of a custom field previously requested through
289 * @param string $fieldName Field name
290 * @return mixed Field value
292 public function getCustomField( $fieldName ) {
293 return $this->mRequestedPageFields
[$fieldName];
297 * Get the fields that have to be queried from the page table:
298 * the ones requested through requestField() and a few basic ones
300 * @return array Array of field names
302 public function getPageTableFields() {
303 // Ensure we get minimum required fields
304 // DON'T change this order
306 'page_namespace' => null,
307 'page_title' => null,
311 if ( $this->mResolveRedirects
) {
312 $pageFlds['page_is_redirect'] = null;
315 // only store non-default fields
316 $this->mRequestedPageFields
= array_diff_key( $this->mRequestedPageFields
, $pageFlds );
318 $pageFlds = array_merge( $pageFlds, $this->mRequestedPageFields
);
320 return array_keys( $pageFlds );
324 * Returns an array [ns][dbkey] => page_id for all requested titles.
325 * page_id is a unique negative number in case title was not found.
326 * Invalid titles will also have negative page IDs and will be in namespace 0
329 public function getAllTitlesByNamespace() {
330 return $this->mAllPages
;
334 * All Title objects provided.
337 public function getTitles() {
338 return $this->mTitles
;
342 * Returns the number of unique pages (not revisions) in the set.
345 public function getTitleCount() {
346 return count( $this->mTitles
);
350 * Returns an array [ns][dbkey] => page_id for all good titles.
353 public function getGoodTitlesByNamespace() {
354 return $this->mGoodPages
;
358 * Title objects that were found in the database.
359 * @return Title[] Array page_id (int) => Title (obj)
361 public function getGoodTitles() {
362 return $this->mGoodTitles
;
366 * Returns the number of found unique pages (not revisions) in the set.
369 public function getGoodTitleCount() {
370 return count( $this->mGoodTitles
);
374 * Returns an array [ns][dbkey] => fake_page_id for all missing titles.
375 * fake_page_id is a unique negative number.
378 public function getMissingTitlesByNamespace() {
379 return $this->mMissingPages
;
383 * Title objects that were NOT found in the database.
384 * The array's index will be negative for each item
387 public function getMissingTitles() {
388 return $this->mMissingTitles
;
392 * Returns an array [ns][dbkey] => page_id for all good and missing titles.
395 public function getGoodAndMissingTitlesByNamespace() {
396 return $this->mGoodAndMissingPages
;
400 * Title objects for good and missing titles.
403 public function getGoodAndMissingTitles() {
404 return $this->mGoodTitles +
$this->mMissingTitles
;
408 * Titles that were deemed invalid by Title::newFromText()
409 * The array's index will be unique and negative for each item
410 * @return string[] Array of strings (not Title objects)
412 public function getInvalidTitles() {
413 return $this->mInvalidTitles
;
417 * Page IDs that were not found in the database
418 * @return array Array of page IDs
420 public function getMissingPageIDs() {
421 return $this->mMissingPageIDs
;
425 * Get a list of redirect resolutions - maps a title to its redirect
426 * target, as an array of output-ready arrays
429 public function getRedirectTitles() {
430 return $this->mRedirectTitles
;
434 * Get a list of redirect resolutions - maps a title to its redirect
436 * @param ApiResult $result
437 * @return array Array of prefixed_title (string) => Title object
440 public function getRedirectTitlesAsResult( $result = null ) {
442 foreach ( $this->getRedirectTitles() as $titleStrFrom => $titleTo ) {
444 'from' => strval( $titleStrFrom ),
445 'to' => $titleTo->getPrefixedText(),
447 if ( $titleTo->hasFragment() ) {
448 $r['tofragment'] = $titleTo->getFragment();
452 if ( !empty( $values ) && $result ) {
453 $result->setIndexedTagName( $values, 'r' );
460 * Get a list of title normalizations - maps a title to its normalized
462 * @return array Array of raw_prefixed_title (string) => prefixed_title (string)
464 public function getNormalizedTitles() {
465 return $this->mNormalizedTitles
;
469 * Get a list of title normalizations - maps a title to its normalized
470 * version in the form of result array.
471 * @param ApiResult $result
472 * @return array Array of raw_prefixed_title (string) => prefixed_title (string)
475 public function getNormalizedTitlesAsResult( $result = null ) {
477 foreach ( $this->getNormalizedTitles() as $rawTitleStr => $titleStr ) {
479 'from' => $rawTitleStr,
483 if ( !empty( $values ) && $result ) {
484 $result->setIndexedTagName( $values, 'n' );
491 * Get a list of title conversions - maps a title to its converted
493 * @return array Array of raw_prefixed_title (string) => prefixed_title (string)
495 public function getConvertedTitles() {
496 return $this->mConvertedTitles
;
500 * Get a list of title conversions - maps a title to its converted
501 * version as a result array.
502 * @param ApiResult $result
503 * @return array Array of (from, to) strings
506 public function getConvertedTitlesAsResult( $result = null ) {
508 foreach ( $this->getConvertedTitles() as $rawTitleStr => $titleStr ) {
510 'from' => $rawTitleStr,
514 if ( !empty( $values ) && $result ) {
515 $result->setIndexedTagName( $values, 'c' );
522 * Get a list of interwiki titles - maps a title to its interwiki
524 * @return array Array of raw_prefixed_title (string) => interwiki_prefix (string)
526 public function getInterwikiTitles() {
527 return $this->mInterwikiTitles
;
531 * Get a list of interwiki titles - maps a title to its interwiki
533 * @param ApiResult $result
535 * @return array Array of raw_prefixed_title (string) => interwiki_prefix (string)
538 public function getInterwikiTitlesAsResult( $result = null, $iwUrl = false ) {
540 foreach ( $this->getInterwikiTitles() as $rawTitleStr => $interwikiStr ) {
542 'title' => $rawTitleStr,
543 'iw' => $interwikiStr,
546 $title = Title
::newFromText( $rawTitleStr );
547 $item['url'] = $title->getFullURL( '', false, PROTO_CURRENT
);
551 if ( !empty( $values ) && $result ) {
552 $result->setIndexedTagName( $values, 'i' );
559 * Get an array of invalid/special/missing titles.
561 * @param array $invalidChecks List of types of invalid titles to include.
562 * Recognized values are:
563 * - invalidTitles: Titles from $this->getInvalidTitles()
564 * - special: Titles from $this->getSpecialTitles()
565 * - missingIds: ids from $this->getMissingPageIDs()
566 * - missingRevIds: ids from $this->getMissingRevisionIDs()
567 * - missingTitles: Titles from $this->getMissingTitles()
568 * - interwikiTitles: Titles from $this->getInterwikiTitlesAsResult()
569 * @return array Array suitable for inclusion in the response
572 public function getInvalidTitlesAndRevisions( $invalidChecks = array( 'invalidTitles',
573 'special', 'missingIds', 'missingRevIds', 'missingTitles', 'interwikiTitles' )
576 if ( in_array( "invalidTitles", $invalidChecks ) ) {
577 self
::addValues( $result, $this->getInvalidTitles(), 'invalid', 'title' );
579 if ( in_array( "special", $invalidChecks ) ) {
580 self
::addValues( $result, $this->getSpecialTitles(), 'special', 'title' );
582 if ( in_array( "missingIds", $invalidChecks ) ) {
583 self
::addValues( $result, $this->getMissingPageIDs(), 'missing', 'pageid' );
585 if ( in_array( "missingRevIds", $invalidChecks ) ) {
586 self
::addValues( $result, $this->getMissingRevisionIDs(), 'missing', 'revid' );
588 if ( in_array( "missingTitles", $invalidChecks ) ) {
589 self
::addValues( $result, $this->getMissingTitles(), 'missing' );
591 if ( in_array( "interwikiTitles", $invalidChecks ) ) {
592 self
::addValues( $result, $this->getInterwikiTitlesAsResult() );
599 * Get the list of revision IDs (requested with the revids= parameter)
600 * @return array Array of revID (int) => pageID (int)
602 public function getRevisionIDs() {
603 return $this->mGoodRevIDs
;
607 * Revision IDs that were not found in the database
608 * @return array Array of revision IDs
610 public function getMissingRevisionIDs() {
611 return $this->mMissingRevIDs
;
615 * Revision IDs that were not found in the database as result array.
616 * @param ApiResult $result
617 * @return array Array of revision IDs
620 public function getMissingRevisionIDsAsResult( $result = null ) {
622 foreach ( $this->getMissingRevisionIDs() as $revid ) {
623 $values[$revid] = array(
627 if ( !empty( $values ) && $result ) {
628 $result->setIndexedTagName( $values, 'rev' );
635 * Get the list of titles with negative namespace
638 public function getSpecialTitles() {
639 return $this->mSpecialTitles
;
643 * Returns the number of revisions (requested with revids= parameter).
644 * @return int Number of revisions.
646 public function getRevisionCount() {
647 return count( $this->getRevisionIDs() );
651 * Populate this PageSet from a list of Titles
652 * @param array $titles Array of Title objects
654 public function populateFromTitles( $titles ) {
656 $this->initFromTitles( $titles );
661 * Populate this PageSet from a list of page IDs
662 * @param array $pageIDs Array of page IDs
664 public function populateFromPageIDs( $pageIDs ) {
666 $this->initFromPageIds( $pageIDs );
671 * Populate this PageSet from a rowset returned from the database
672 * @param DatabaseBase $db
673 * @param ResultWrapper $queryResult Query result object
675 public function populateFromQueryResult( $db, $queryResult ) {
677 $this->initFromQueryResult( $queryResult );
682 * Populate this PageSet from a list of revision IDs
683 * @param array $revIDs Array of revision IDs
685 public function populateFromRevisionIDs( $revIDs ) {
687 $this->initFromRevIDs( $revIDs );
692 * Extract all requested fields from the row received from the database
693 * @param stdClass $row Result row
695 public function processDbRow( $row ) {
696 // Store Title object in various data structures
697 $title = Title
::newFromRow( $row );
699 $pageId = intval( $row->page_id
);
700 $this->mAllPages
[$row->page_namespace
][$row->page_title
] = $pageId;
701 $this->mTitles
[] = $title;
703 if ( $this->mResolveRedirects
&& $row->page_is_redirect
== '1' ) {
704 $this->mPendingRedirectIDs
[$pageId] = $title;
706 $this->mGoodPages
[$row->page_namespace
][$row->page_title
] = $pageId;
707 $this->mGoodAndMissingPages
[$row->page_namespace
][$row->page_title
] = $pageId;
708 $this->mGoodTitles
[$pageId] = $title;
711 foreach ( $this->mRequestedPageFields
as $fieldName => &$fieldValues ) {
712 $fieldValues[$pageId] = $row->$fieldName;
717 * Do not use, does nothing, will be removed
718 * @deprecated since 1.21
720 public function finishPageSetGeneration() {
721 wfDeprecated( __METHOD__
, '1.21' );
725 * This method populates internal variables with page information
726 * based on the given array of title strings.
729 * #1 For each title, get data from `page` table
730 * #2 If page was not found in the DB, store it as missing
732 * Additionally, when resolving redirects:
733 * #3 If no more redirects left, stop.
734 * #4 For each redirect, get its target from the `redirect` table.
735 * #5 Substitute the original LinkBatch object with the new list
736 * #6 Repeat from step #1
738 * @param array $titles Array of Title objects or strings
740 private function initFromTitles( $titles ) {
741 // Get validated and normalized title objects
742 $linkBatch = $this->processTitlesArray( $titles );
743 if ( $linkBatch->isEmpty() ) {
747 $db = $this->getDB();
748 $set = $linkBatch->constructSet( 'page', $db );
750 // Get pageIDs data from the `page` table
751 $this->profileDBIn();
752 $res = $db->select( 'page', $this->getPageTableFields(), $set,
754 $this->profileDBOut();
756 // Hack: get the ns:titles stored in array(ns => array(titles)) format
757 $this->initFromQueryResult( $res, $linkBatch->data
, true ); // process Titles
759 // Resolve any found redirects
760 $this->resolvePendingRedirects();
764 * Does the same as initFromTitles(), but is based on page IDs instead
765 * @param array $pageids Array of page IDs
767 private function initFromPageIds( $pageids ) {
772 $pageids = array_map( 'intval', $pageids ); // paranoia
773 $remaining = array_flip( $pageids );
775 $pageids = self
::getPositiveIntegers( $pageids );
778 if ( !empty( $pageids ) ) {
780 'page_id' => $pageids
782 $db = $this->getDB();
784 // Get pageIDs data from the `page` table
785 $this->profileDBIn();
786 $res = $db->select( 'page', $this->getPageTableFields(), $set,
788 $this->profileDBOut();
791 $this->initFromQueryResult( $res, $remaining, false ); // process PageIDs
793 // Resolve any found redirects
794 $this->resolvePendingRedirects();
798 * Iterate through the result of the query on 'page' table,
799 * and for each row create and store title object and save any extra fields requested.
800 * @param ResultWrapper $res DB Query result
801 * @param array $remaining Array of either pageID or ns/title elements (optional).
802 * If given, any missing items will go to $mMissingPageIDs and $mMissingTitles
803 * @param bool $processTitles Must be provided together with $remaining.
804 * If true, treat $remaining as an array of [ns][title]
805 * If false, treat it as an array of [pageIDs]
807 private function initFromQueryResult( $res, &$remaining = null, $processTitles = null ) {
808 if ( !is_null( $remaining ) && is_null( $processTitles ) ) {
809 ApiBase
::dieDebug( __METHOD__
, 'Missing $processTitles parameter when $remaining is provided' );
812 $usernames = array();
814 foreach ( $res as $row ) {
815 $pageId = intval( $row->page_id
);
817 // Remove found page from the list of remaining items
818 if ( isset( $remaining ) ) {
819 if ( $processTitles ) {
820 unset( $remaining[$row->page_namespace
][$row->page_title
] );
822 unset( $remaining[$pageId] );
826 // Store any extra fields requested by modules
827 $this->processDbRow( $row );
829 // Need gender information
830 if ( MWNamespace
::hasGenderDistinction( $row->page_namespace
) ) {
831 $usernames[] = $row->page_title
;
836 if ( isset( $remaining ) ) {
837 // Any items left in the $remaining list are added as missing
838 if ( $processTitles ) {
839 // The remaining titles in $remaining are non-existent pages
840 foreach ( $remaining as $ns => $dbkeys ) {
841 foreach ( array_keys( $dbkeys ) as $dbkey ) {
842 $title = Title
::makeTitle( $ns, $dbkey );
843 $this->mAllPages
[$ns][$dbkey] = $this->mFakePageId
;
844 $this->mMissingPages
[$ns][$dbkey] = $this->mFakePageId
;
845 $this->mGoodAndMissingPages
[$ns][$dbkey] = $this->mFakePageId
;
846 $this->mMissingTitles
[$this->mFakePageId
] = $title;
847 $this->mFakePageId
--;
848 $this->mTitles
[] = $title;
850 // need gender information
851 if ( MWNamespace
::hasGenderDistinction( $ns ) ) {
852 $usernames[] = $dbkey;
857 // The remaining pageids do not exist
858 if ( !$this->mMissingPageIDs
) {
859 $this->mMissingPageIDs
= array_keys( $remaining );
861 $this->mMissingPageIDs
= array_merge( $this->mMissingPageIDs
, array_keys( $remaining ) );
866 // Get gender information
867 $genderCache = GenderCache
::singleton();
868 $genderCache->doQuery( $usernames, __METHOD__
);
872 * Does the same as initFromTitles(), but is based on revision IDs
874 * @param array $revids Array of revision IDs
876 private function initFromRevIDs( $revids ) {
881 $revids = array_map( 'intval', $revids ); // paranoia
882 $db = $this->getDB();
884 $remaining = array_flip( $revids );
886 $revids = self
::getPositiveIntegers( $revids );
888 if ( !empty( $revids ) ) {
889 $tables = array( 'revision', 'page' );
890 $fields = array( 'rev_id', 'rev_page' );
891 $where = array( 'rev_id' => $revids, 'rev_page = page_id' );
893 // Get pageIDs data from the `page` table
894 $this->profileDBIn();
895 $res = $db->select( $tables, $fields, $where, __METHOD__
);
896 foreach ( $res as $row ) {
897 $revid = intval( $row->rev_id
);
898 $pageid = intval( $row->rev_page
);
899 $this->mGoodRevIDs
[$revid] = $pageid;
900 $pageids[$pageid] = '';
901 unset( $remaining[$revid] );
903 $this->profileDBOut();
906 $this->mMissingRevIDs
= array_keys( $remaining );
908 // Populate all the page information
909 $this->initFromPageIds( array_keys( $pageids ) );
913 * Resolve any redirects in the result if redirect resolution was
914 * requested. This function is called repeatedly until all redirects
915 * have been resolved.
917 private function resolvePendingRedirects() {
918 if ( $this->mResolveRedirects
) {
919 $db = $this->getDB();
920 $pageFlds = $this->getPageTableFields();
922 // Repeat until all redirects have been resolved
923 // The infinite loop is prevented by keeping all known pages in $this->mAllPages
924 while ( $this->mPendingRedirectIDs
) {
925 // Resolve redirects by querying the pagelinks table, and repeat the process
926 // Create a new linkBatch object for the next pass
927 $linkBatch = $this->getRedirectTargets();
929 if ( $linkBatch->isEmpty() ) {
933 $set = $linkBatch->constructSet( 'page', $db );
934 if ( $set === false ) {
938 // Get pageIDs data from the `page` table
939 $this->profileDBIn();
940 $res = $db->select( 'page', $pageFlds, $set, __METHOD__
);
941 $this->profileDBOut();
943 // Hack: get the ns:titles stored in array(ns => array(titles)) format
944 $this->initFromQueryResult( $res, $linkBatch->data
, true );
950 * Get the targets of the pending redirects from the database
952 * Also creates entries in the redirect table for redirects that don't
956 private function getRedirectTargets() {
957 $lb = new LinkBatch();
958 $db = $this->getDB();
960 $this->profileDBIn();
969 ), array( 'rd_from' => array_keys( $this->mPendingRedirectIDs
) ),
972 $this->profileDBOut();
973 foreach ( $res as $row ) {
974 $rdfrom = intval( $row->rd_from
);
975 $from = $this->mPendingRedirectIDs
[$rdfrom]->getPrefixedText();
976 $to = Title
::makeTitle(
982 unset( $this->mPendingRedirectIDs
[$rdfrom] );
983 if ( !$to->isExternal() && !isset( $this->mAllPages
[$row->rd_namespace
][$row->rd_title
] ) ) {
984 $lb->add( $row->rd_namespace
, $row->rd_title
);
986 $this->mRedirectTitles
[$from] = $to;
989 if ( $this->mPendingRedirectIDs
) {
990 // We found pages that aren't in the redirect table
992 foreach ( $this->mPendingRedirectIDs
as $id => $title ) {
993 $page = WikiPage
::factory( $title );
994 $rt = $page->insertRedirect();
996 // What the hell. Let's just ignore this
1000 $this->mRedirectTitles
[$title->getPrefixedText()] = $rt;
1001 unset( $this->mPendingRedirectIDs
[$id] );
1009 * Get the cache mode for the data generated by this module.
1010 * All PageSet users should take into account whether this returns a more-restrictive
1011 * cache mode than the using module itself. For possible return values and other
1012 * details about cache modes, see ApiMain::setCacheMode()
1014 * Public caching will only be allowed if *all* the modules that supply
1015 * data for a given request return a cache mode of public.
1017 * @param array|null $params
1021 public function getCacheMode( $params = null ) {
1022 return $this->mCacheMode
;
1026 * Given an array of title strings, convert them into Title objects.
1027 * Alternatively, an array of Title objects may be given.
1028 * This method validates access rights for the title,
1029 * and appends normalization values to the output.
1031 * @param array $titles Array of Title objects or strings
1034 private function processTitlesArray( $titles ) {
1035 $usernames = array();
1036 $linkBatch = new LinkBatch();
1038 foreach ( $titles as $title ) {
1039 if ( is_string( $title ) ) {
1040 $titleObj = Title
::newFromText( $title, $this->mDefaultNamespace
);
1045 // Handle invalid titles gracefully
1046 $this->mAllPages
[0][$title] = $this->mFakePageId
;
1047 $this->mInvalidTitles
[$this->mFakePageId
] = $title;
1048 $this->mFakePageId
--;
1049 continue; // There's nothing else we can do
1051 $unconvertedTitle = $titleObj->getPrefixedText();
1052 $titleWasConverted = false;
1053 if ( $titleObj->isExternal() ) {
1054 // This title is an interwiki link.
1055 $this->mInterwikiTitles
[$unconvertedTitle] = $titleObj->getInterwiki();
1057 // Variants checking
1059 if ( $this->mConvertTitles
&&
1060 count( $wgContLang->getVariants() ) > 1 &&
1061 !$titleObj->exists()
1063 // Language::findVariantLink will modify titleText and titleObj into
1064 // the canonical variant if possible
1065 $titleText = is_string( $title ) ?
$title : $titleObj->getPrefixedText();
1066 $wgContLang->findVariantLink( $titleText, $titleObj );
1067 $titleWasConverted = $unconvertedTitle !== $titleObj->getPrefixedText();
1070 if ( $titleObj->getNamespace() < 0 ) {
1071 // Handle Special and Media pages
1072 $titleObj = $titleObj->fixSpecialName();
1073 $this->mSpecialTitles
[$this->mFakePageId
] = $titleObj;
1074 $this->mFakePageId
--;
1077 $linkBatch->addObj( $titleObj );
1081 // Make sure we remember the original title that was
1082 // given to us. This way the caller can correlate new
1083 // titles with the originally requested when e.g. the
1084 // namespace is localized or the capitalization is
1086 if ( $titleWasConverted ) {
1087 $this->mConvertedTitles
[$unconvertedTitle] = $titleObj->getPrefixedText();
1088 // In this case the page can't be Special.
1089 if ( is_string( $title ) && $title !== $unconvertedTitle ) {
1090 $this->mNormalizedTitles
[$title] = $unconvertedTitle;
1092 } elseif ( is_string( $title ) && $title !== $titleObj->getPrefixedText() ) {
1093 $this->mNormalizedTitles
[$title] = $titleObj->getPrefixedText();
1096 // Need gender information
1097 if ( MWNamespace
::hasGenderDistinction( $titleObj->getNamespace() ) ) {
1098 $usernames[] = $titleObj->getText();
1101 // Get gender information
1102 $genderCache = GenderCache
::singleton();
1103 $genderCache->doQuery( $usernames, __METHOD__
);
1109 * Get the database connection (read-only)
1110 * @return DatabaseBase
1112 protected function getDB() {
1113 return $this->mDbSource
->getDB();
1117 * Returns the input array of integers with all values < 0 removed
1119 * @param array $array
1122 private static function getPositiveIntegers( $array ) {
1123 // bug 25734 API: possible issue with revids validation
1124 // It seems with a load of revision rows, MySQL gets upset
1125 // Remove any < 0 integers, as they can't be valid
1126 foreach ( $array as $i => $int ) {
1128 unset( $array[$i] );
1135 public function getAllowedParams( $flags = 0 ) {
1138 ApiBase
::PARAM_ISMULTI
=> true,
1139 ApiBase
::PARAM_HELP_MSG
=> 'api-pageset-param-titles',
1142 ApiBase
::PARAM_TYPE
=> 'integer',
1143 ApiBase
::PARAM_ISMULTI
=> true,
1144 ApiBase
::PARAM_HELP_MSG
=> 'api-pageset-param-pageids',
1147 ApiBase
::PARAM_TYPE
=> 'integer',
1148 ApiBase
::PARAM_ISMULTI
=> true,
1149 ApiBase
::PARAM_HELP_MSG
=> 'api-pageset-param-revids',
1151 'generator' => array(
1152 ApiBase
::PARAM_TYPE
=> null,
1153 ApiBase
::PARAM_VALUE_LINKS
=> array(),
1154 ApiBase
::PARAM_HELP_MSG
=> 'api-pageset-param-generator',
1156 'redirects' => array(
1157 ApiBase
::PARAM_DFLT
=> false,
1158 ApiBase
::PARAM_HELP_MSG
=> $this->mAllowGenerator
1159 ?
'api-pageset-param-redirects-generator'
1160 : 'api-pageset-param-redirects-nogenerator',
1162 'converttitles' => array(
1163 ApiBase
::PARAM_DFLT
=> false,
1164 ApiBase
::PARAM_HELP_MSG
=> array(
1165 'api-pageset-param-converttitles',
1166 $this->getLanguage()->commaList( LanguageConverter
::$languagesWithVariants ),
1171 if ( !$this->mAllowGenerator
) {
1172 unset( $result['generator'] );
1173 } elseif ( $flags & ApiBase
::GET_VALUES_FOR_HELP
) {
1174 $result['generator'][ApiBase
::PARAM_TYPE
] = $this->getGenerators();
1175 foreach ( $result['generator'][ApiBase
::PARAM_TYPE
] as $g ) {
1176 $result['generator'][ApiBase
::PARAM_TYPE
][] = $g;
1177 $result['generator'][ApiBase
::PARAM_VALUE_LINKS
][$g] = "Special:ApiHelp/query+$g";
1184 private static $generators = null;
1187 * Get an array of all available generators
1190 private function getGenerators() {
1191 if ( self
::$generators === null ) {
1192 $query = $this->mDbSource
;
1193 if ( !( $query instanceof ApiQuery
) ) {
1194 // If the parent container of this pageset is not ApiQuery,
1195 // we must create it to get module manager
1196 $query = $this->getMain()->getModuleManager()->getModule( 'query' );
1199 $mgr = $query->getModuleManager();
1200 foreach ( $mgr->getNamesWithClasses() as $name => $class ) {
1201 if ( is_subclass_of( $class, 'ApiQueryGeneratorBase' ) ) {
1206 self
::$generators = $gens;
1209 return self
::$generators;