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
{
44 * Constructor flag: The new instance of ApiPageSet will ignore the 'generator=' parameter
47 const DISABLE_GENERATORS
= 1;
51 private $mResolveRedirects;
52 private $mConvertTitles;
53 private $mAllowGenerator;
55 private $mAllPages = array(); // [ns][dbkey] => page_id or negative when missing
56 private $mTitles = array();
57 private $mGoodTitles = array();
58 private $mMissingTitles = array();
59 private $mInvalidTitles = array();
60 private $mMissingPageIDs = array();
61 private $mRedirectTitles = array();
62 private $mSpecialTitles = array();
63 private $mNormalizedTitles = array();
64 private $mInterwikiTitles = array();
65 private $mPendingRedirectIDs = array();
66 private $mConvertedTitles = array();
67 private $mGoodRevIDs = array();
68 private $mMissingRevIDs = array();
69 private $mFakePageId = -1;
70 private $mCacheMode = 'public';
71 private $mRequestedPageFields = array();
72 private $mDefaultNamespace = NS_MAIN
;
76 * @param $dbSource ApiBase Module implementing getDB().
77 * Allows PageSet to reuse existing db connection from the shared state like ApiQuery.
78 * @param int $flags Zero or more flags like DISABLE_GENERATORS
79 * @param int $defaultNamespace the namespace to use if none is specified by a prefix.
80 * @since 1.21 accepts $flags instead of two boolean values
82 public function __construct( ApiBase
$dbSource, $flags = 0, $defaultNamespace = NS_MAIN
) {
83 parent
::__construct( $dbSource->getMain(), $dbSource->getModuleName() );
84 $this->mDbSource
= $dbSource;
85 $this->mAllowGenerator
= ( $flags & ApiPageSet
::DISABLE_GENERATORS
) == 0;
86 $this->mDefaultNamespace
= $defaultNamespace;
89 $this->mParams
= $this->extractRequestParams();
90 $this->mResolveRedirects
= $this->mParams
['redirects'];
91 $this->mConvertTitles
= $this->mParams
['converttitles'];
96 * In case execute() is not called, call this method to mark all relevant parameters as used
97 * This prevents unused parameters from being reported as warnings
99 public function executeDryRun() {
100 $this->executeInternal( true );
104 * Populate the PageSet from the request parameters.
106 public function execute() {
107 $this->executeInternal( false );
111 * Populate the PageSet from the request parameters.
112 * @param bool $isDryRun If true, instantiates generator, but only to mark relevant parameters as used
114 private function executeInternal( $isDryRun ) {
117 $generatorName = $this->mAllowGenerator ?
$this->mParams
['generator'] : null;
118 if ( isset( $generatorName ) ) {
119 $dbSource = $this->mDbSource
;
120 $isQuery = $dbSource instanceof ApiQuery
;
122 // If the parent container of this pageset is not ApiQuery, we must create it to run generator
123 $dbSource = $this->getMain()->getModuleManager()->getModule( 'query' );
124 // Enable profiling for query module because it will be used for db sql profiling
125 $dbSource->profileIn();
127 $generator = $dbSource->getModuleManager()->getModule( $generatorName, null, true );
128 if ( $generator === null ) {
129 $this->dieUsage( 'Unknown generator=' . $generatorName, 'badgenerator' );
131 if ( !$generator instanceof ApiQueryGeneratorBase
) {
132 $this->dieUsage( "Module $generatorName cannot be used as a generator", 'badgenerator' );
134 // Create a temporary pageset to store generator's output,
135 // add any additional fields generator may need, and execute pageset to populate titles/pageids
136 $tmpPageSet = new ApiPageSet( $dbSource, ApiPageSet
::DISABLE_GENERATORS
);
137 $generator->setGeneratorMode( $tmpPageSet );
138 $this->mCacheMode
= $generator->getCacheMode( $generator->extractRequestParams() );
141 $generator->requestExtraData( $tmpPageSet );
143 $tmpPageSet->executeInternal( $isDryRun );
145 // populate this pageset with the generator output
147 $generator->profileIn();
150 $generator->executeGenerator( $this );
151 wfRunHooks( 'APIQueryGeneratorAfterExecute', array( &$generator, &$this ) );
152 $this->resolvePendingRedirects();
154 // Prevent warnings from being reported on these parameters
155 $main = $this->getMain();
156 foreach ( $generator->extractRequestParams() as $paramName => $param ) {
157 $main->getVal( $generator->encodeParamName( $paramName ) );
160 $generator->profileOut();
164 // If this pageset is not part of the query, we called profileIn() above
165 $dbSource->profileOut();
168 // Only one of the titles/pageids/revids is allowed at the same time
170 if ( isset( $this->mParams
['titles'] ) ) {
171 $dataSource = 'titles';
173 if ( isset( $this->mParams
['pageids'] ) ) {
174 if ( isset( $dataSource ) ) {
175 $this->dieUsage( "Cannot use 'pageids' at the same time as '$dataSource'", 'multisource' );
177 $dataSource = 'pageids';
179 if ( isset( $this->mParams
['revids'] ) ) {
180 if ( isset( $dataSource ) ) {
181 $this->dieUsage( "Cannot use 'revids' at the same time as '$dataSource'", 'multisource' );
183 $dataSource = 'revids';
187 // Populate page information with the original user input
188 switch( $dataSource ) {
190 $this->initFromTitles( $this->mParams
['titles'] );
193 $this->initFromPageIds( $this->mParams
['pageids'] );
196 if ( $this->mResolveRedirects
) {
197 $this->setWarning( 'Redirect resolution cannot be used together with the revids= parameter. ' .
198 'Any redirects the revids= point to have not been resolved.' );
200 $this->mResolveRedirects
= false;
201 $this->initFromRevIDs( $this->mParams
['revids'] );
204 // Do nothing - some queries do not need any of the data sources.
213 * Check whether this PageSet is resolving redirects
216 public function isResolvingRedirects() {
217 return $this->mResolveRedirects
;
221 * Return the parameter name that is the source of data for this PageSet
223 * If multiple source parameters are specified (e.g. titles and pageids),
224 * one will be named arbitrarily.
226 * @return string|null
228 public function getDataSource() {
229 if ( $this->mAllowGenerator
&& isset( $this->mParams
['generator'] ) ) {
232 if ( isset( $this->mParams
['titles'] ) ) {
235 if ( isset( $this->mParams
['pageids'] ) ) {
238 if ( isset( $this->mParams
['revids'] ) ) {
245 * Request an additional field from the page table.
246 * Must be called before execute()
247 * @param string $fieldName Field name
249 public function requestField( $fieldName ) {
250 $this->mRequestedPageFields
[$fieldName] = null;
254 * Get the value of a custom field previously requested through
256 * @param string $fieldName Field name
257 * @return mixed Field value
259 public function getCustomField( $fieldName ) {
260 return $this->mRequestedPageFields
[$fieldName];
264 * Get the fields that have to be queried from the page table:
265 * the ones requested through requestField() and a few basic ones
267 * @return array of field names
269 public function getPageTableFields() {
270 // Ensure we get minimum required fields
271 // DON'T change this order
273 'page_namespace' => null,
274 'page_title' => null,
278 if ( $this->mResolveRedirects
) {
279 $pageFlds['page_is_redirect'] = null;
282 // only store non-default fields
283 $this->mRequestedPageFields
= array_diff_key( $this->mRequestedPageFields
, $pageFlds );
285 $pageFlds = array_merge( $pageFlds, $this->mRequestedPageFields
);
286 return array_keys( $pageFlds );
290 * Returns an array [ns][dbkey] => page_id for all requested titles.
291 * page_id is a unique negative number in case title was not found.
292 * Invalid titles will also have negative page IDs and will be in namespace 0
295 public function getAllTitlesByNamespace() {
296 return $this->mAllPages
;
300 * All Title objects provided.
301 * @return array of Title objects
303 public function getTitles() {
304 return $this->mTitles
;
308 * Returns the number of unique pages (not revisions) in the set.
311 public function getTitleCount() {
312 return count( $this->mTitles
);
316 * Title objects that were found in the database.
317 * @return array page_id (int) => Title (obj)
319 public function getGoodTitles() {
320 return $this->mGoodTitles
;
324 * Returns the number of found unique pages (not revisions) in the set.
327 public function getGoodTitleCount() {
328 return count( $this->mGoodTitles
);
332 * Title objects that were NOT found in the database.
333 * The array's index will be negative for each item
334 * @return array of Title objects
336 public function getMissingTitles() {
337 return $this->mMissingTitles
;
341 * Titles that were deemed invalid by Title::newFromText()
342 * The array's index will be unique and negative for each item
343 * @return array of strings (not Title objects)
345 public function getInvalidTitles() {
346 return $this->mInvalidTitles
;
350 * Page IDs that were not found in the database
351 * @return array of page IDs
353 public function getMissingPageIDs() {
354 return $this->mMissingPageIDs
;
358 * Get a list of redirect resolutions - maps a title to its redirect
359 * target, as an array of output-ready arrays
362 public function getRedirectTitles() {
363 return $this->mRedirectTitles
;
367 * Get a list of redirect resolutions - maps a title to its redirect
369 * @param $result ApiResult
370 * @return array of prefixed_title (string) => Title object
373 public function getRedirectTitlesAsResult( $result = null ) {
375 foreach ( $this->getRedirectTitles() as $titleStrFrom => $titleTo ) {
377 'from' => strval( $titleStrFrom ),
378 'to' => $titleTo->getPrefixedText(),
380 if ( $titleTo->getFragment() !== '' ) {
381 $r['tofragment'] = $titleTo->getFragment();
385 if ( !empty( $values ) && $result ) {
386 $result->setIndexedTagName( $values, 'r' );
392 * Get a list of title normalizations - maps a title to its normalized
394 * @return array raw_prefixed_title (string) => prefixed_title (string)
396 public function getNormalizedTitles() {
397 return $this->mNormalizedTitles
;
401 * Get a list of title normalizations - maps a title to its normalized
402 * version in the form of result array.
403 * @param $result ApiResult
404 * @return array of raw_prefixed_title (string) => prefixed_title (string)
407 public function getNormalizedTitlesAsResult( $result = null ) {
409 foreach ( $this->getNormalizedTitles() as $rawTitleStr => $titleStr ) {
411 'from' => $rawTitleStr,
415 if ( !empty( $values ) && $result ) {
416 $result->setIndexedTagName( $values, 'n' );
422 * Get a list of title conversions - maps a title to its converted
424 * @return array raw_prefixed_title (string) => prefixed_title (string)
426 public function getConvertedTitles() {
427 return $this->mConvertedTitles
;
431 * Get a list of title conversions - maps a title to its converted
432 * version as a result array.
433 * @param $result ApiResult
434 * @return array of (from, to) strings
437 public function getConvertedTitlesAsResult( $result = null ) {
439 foreach ( $this->getConvertedTitles() as $rawTitleStr => $titleStr ) {
441 'from' => $rawTitleStr,
445 if ( !empty( $values ) && $result ) {
446 $result->setIndexedTagName( $values, 'c' );
452 * Get a list of interwiki titles - maps a title to its interwiki
454 * @return array raw_prefixed_title (string) => interwiki_prefix (string)
456 public function getInterwikiTitles() {
457 return $this->mInterwikiTitles
;
461 * Get a list of interwiki titles - maps a title to its interwiki
463 * @param $result ApiResult
464 * @param $iwUrl boolean
465 * @return array raw_prefixed_title (string) => interwiki_prefix (string)
468 public function getInterwikiTitlesAsResult( $result = null, $iwUrl = false ) {
470 foreach ( $this->getInterwikiTitles() as $rawTitleStr => $interwikiStr ) {
472 'title' => $rawTitleStr,
473 'iw' => $interwikiStr,
476 $title = Title
::newFromText( $rawTitleStr );
477 $item['url'] = $title->getFullURL( '', false, PROTO_CURRENT
);
481 if ( !empty( $values ) && $result ) {
482 $result->setIndexedTagName( $values, 'i' );
488 * Get the list of revision IDs (requested with the revids= parameter)
489 * @return array revID (int) => pageID (int)
491 public function getRevisionIDs() {
492 return $this->mGoodRevIDs
;
496 * Revision IDs that were not found in the database
497 * @return array of revision IDs
499 public function getMissingRevisionIDs() {
500 return $this->mMissingRevIDs
;
504 * Revision IDs that were not found in the database as result array.
505 * @param $result ApiResult
506 * @return array of revision IDs
509 public function getMissingRevisionIDsAsResult( $result = null ) {
511 foreach ( $this->getMissingRevisionIDs() as $revid ) {
512 $values[$revid] = array(
516 if ( !empty( $values ) && $result ) {
517 $result->setIndexedTagName( $values, 'rev' );
523 * Get the list of titles with negative namespace
524 * @return array Title
526 public function getSpecialTitles() {
527 return $this->mSpecialTitles
;
531 * Returns the number of revisions (requested with revids= parameter).
532 * @return int Number of revisions.
534 public function getRevisionCount() {
535 return count( $this->getRevisionIDs() );
539 * Populate this PageSet from a list of Titles
540 * @param array $titles of Title objects
542 public function populateFromTitles( $titles ) {
544 $this->initFromTitles( $titles );
549 * Populate this PageSet from a list of page IDs
550 * @param array $pageIDs of page IDs
552 public function populateFromPageIDs( $pageIDs ) {
554 $this->initFromPageIds( $pageIDs );
559 * Populate this PageSet from a rowset returned from the database
560 * @param $db DatabaseBase object
561 * @param $queryResult ResultWrapper Query result object
563 public function populateFromQueryResult( $db, $queryResult ) {
565 $this->initFromQueryResult( $queryResult );
570 * Populate this PageSet from a list of revision IDs
571 * @param array $revIDs of revision IDs
573 public function populateFromRevisionIDs( $revIDs ) {
575 $this->initFromRevIDs( $revIDs );
580 * Extract all requested fields from the row received from the database
581 * @param $row Result row
583 public function processDbRow( $row ) {
584 // Store Title object in various data structures
585 $title = Title
::newFromRow( $row );
587 $pageId = intval( $row->page_id
);
588 $this->mAllPages
[$row->page_namespace
][$row->page_title
] = $pageId;
589 $this->mTitles
[] = $title;
591 if ( $this->mResolveRedirects
&& $row->page_is_redirect
== '1' ) {
592 $this->mPendingRedirectIDs
[$pageId] = $title;
594 $this->mGoodTitles
[$pageId] = $title;
597 foreach ( $this->mRequestedPageFields
as $fieldName => &$fieldValues ) {
598 $fieldValues[$pageId] = $row->$fieldName;
603 * Do not use, does nothing, will be removed
606 public function finishPageSetGeneration() {
607 wfDeprecated( __METHOD__
, '1.21' );
611 * This method populates internal variables with page information
612 * based on the given array of title strings.
615 * #1 For each title, get data from `page` table
616 * #2 If page was not found in the DB, store it as missing
618 * Additionally, when resolving redirects:
619 * #3 If no more redirects left, stop.
620 * #4 For each redirect, get its target from the `redirect` table.
621 * #5 Substitute the original LinkBatch object with the new list
622 * #6 Repeat from step #1
624 * @param array $titles of Title objects or strings
626 private function initFromTitles( $titles ) {
627 // Get validated and normalized title objects
628 $linkBatch = $this->processTitlesArray( $titles );
629 if ( $linkBatch->isEmpty() ) {
633 $db = $this->getDB();
634 $set = $linkBatch->constructSet( 'page', $db );
636 // Get pageIDs data from the `page` table
637 $this->profileDBIn();
638 $res = $db->select( 'page', $this->getPageTableFields(), $set,
640 $this->profileDBOut();
642 // Hack: get the ns:titles stored in array(ns => array(titles)) format
643 $this->initFromQueryResult( $res, $linkBatch->data
, true ); // process Titles
645 // Resolve any found redirects
646 $this->resolvePendingRedirects();
650 * Does the same as initFromTitles(), but is based on page IDs instead
651 * @param array $pageids of page IDs
653 private function initFromPageIds( $pageids ) {
658 $pageids = array_map( 'intval', $pageids ); // paranoia
659 $remaining = array_flip( $pageids );
661 $pageids = self
::getPositiveIntegers( $pageids );
664 if ( !empty( $pageids ) ) {
666 'page_id' => $pageids
668 $db = $this->getDB();
670 // Get pageIDs data from the `page` table
671 $this->profileDBIn();
672 $res = $db->select( 'page', $this->getPageTableFields(), $set,
674 $this->profileDBOut();
677 $this->initFromQueryResult( $res, $remaining, false ); // process PageIDs
679 // Resolve any found redirects
680 $this->resolvePendingRedirects();
684 * Iterate through the result of the query on 'page' table,
685 * and for each row create and store title object and save any extra fields requested.
686 * @param $res ResultWrapper DB Query result
687 * @param array $remaining of either pageID or ns/title elements (optional).
688 * If given, any missing items will go to $mMissingPageIDs and $mMissingTitles
689 * @param bool $processTitles Must be provided together with $remaining.
690 * If true, treat $remaining as an array of [ns][title]
691 * If false, treat it as an array of [pageIDs]
693 private function initFromQueryResult( $res, &$remaining = null, $processTitles = null ) {
694 if ( !is_null( $remaining ) && is_null( $processTitles ) ) {
695 ApiBase
::dieDebug( __METHOD__
, 'Missing $processTitles parameter when $remaining is provided' );
698 $usernames = array();
700 foreach ( $res as $row ) {
701 $pageId = intval( $row->page_id
);
703 // Remove found page from the list of remaining items
704 if ( isset( $remaining ) ) {
705 if ( $processTitles ) {
706 unset( $remaining[$row->page_namespace
][$row->page_title
] );
708 unset( $remaining[$pageId] );
712 // Store any extra fields requested by modules
713 $this->processDbRow( $row );
715 // Need gender information
716 if ( MWNamespace
::hasGenderDistinction( $row->page_namespace
) ) {
717 $usernames[] = $row->page_title
;
722 if ( isset( $remaining ) ) {
723 // Any items left in the $remaining list are added as missing
724 if ( $processTitles ) {
725 // The remaining titles in $remaining are non-existent pages
726 foreach ( $remaining as $ns => $dbkeys ) {
727 foreach ( array_keys( $dbkeys ) as $dbkey ) {
728 $title = Title
::makeTitle( $ns, $dbkey );
729 $this->mAllPages
[$ns][$dbkey] = $this->mFakePageId
;
730 $this->mMissingTitles
[$this->mFakePageId
] = $title;
731 $this->mFakePageId
--;
732 $this->mTitles
[] = $title;
734 // need gender information
735 if ( MWNamespace
::hasGenderDistinction( $ns ) ) {
736 $usernames[] = $dbkey;
741 // The remaining pageids do not exist
742 if ( !$this->mMissingPageIDs
) {
743 $this->mMissingPageIDs
= array_keys( $remaining );
745 $this->mMissingPageIDs
= array_merge( $this->mMissingPageIDs
, array_keys( $remaining ) );
750 // Get gender information
751 $genderCache = GenderCache
::singleton();
752 $genderCache->doQuery( $usernames, __METHOD__
);
756 * Does the same as initFromTitles(), but is based on revision IDs
758 * @param array $revids of revision IDs
760 private function initFromRevIDs( $revids ) {
765 $revids = array_map( 'intval', $revids ); // paranoia
766 $db = $this->getDB();
768 $remaining = array_flip( $revids );
770 $revids = self
::getPositiveIntegers( $revids );
772 if ( !empty( $revids ) ) {
773 $tables = array( 'revision', 'page' );
774 $fields = array( 'rev_id', 'rev_page' );
775 $where = array( 'rev_id' => $revids, 'rev_page = page_id' );
777 // Get pageIDs data from the `page` table
778 $this->profileDBIn();
779 $res = $db->select( $tables, $fields, $where, __METHOD__
);
780 foreach ( $res as $row ) {
781 $revid = intval( $row->rev_id
);
782 $pageid = intval( $row->rev_page
);
783 $this->mGoodRevIDs
[$revid] = $pageid;
784 $pageids[$pageid] = '';
785 unset( $remaining[$revid] );
787 $this->profileDBOut();
790 $this->mMissingRevIDs
= array_keys( $remaining );
792 // Populate all the page information
793 $this->initFromPageIds( array_keys( $pageids ) );
797 * Resolve any redirects in the result if redirect resolution was
798 * requested. This function is called repeatedly until all redirects
799 * have been resolved.
801 private function resolvePendingRedirects() {
802 if ( $this->mResolveRedirects
) {
803 $db = $this->getDB();
804 $pageFlds = $this->getPageTableFields();
806 // Repeat until all redirects have been resolved
807 // The infinite loop is prevented by keeping all known pages in $this->mAllPages
808 while ( $this->mPendingRedirectIDs
) {
809 // Resolve redirects by querying the pagelinks table, and repeat the process
810 // Create a new linkBatch object for the next pass
811 $linkBatch = $this->getRedirectTargets();
813 if ( $linkBatch->isEmpty() ) {
817 $set = $linkBatch->constructSet( 'page', $db );
818 if ( $set === false ) {
822 // Get pageIDs data from the `page` table
823 $this->profileDBIn();
824 $res = $db->select( 'page', $pageFlds, $set, __METHOD__
);
825 $this->profileDBOut();
827 // Hack: get the ns:titles stored in array(ns => array(titles)) format
828 $this->initFromQueryResult( $res, $linkBatch->data
, true );
834 * Get the targets of the pending redirects from the database
836 * Also creates entries in the redirect table for redirects that don't
840 private function getRedirectTargets() {
841 $lb = new LinkBatch();
842 $db = $this->getDB();
844 $this->profileDBIn();
853 ), array( 'rd_from' => array_keys( $this->mPendingRedirectIDs
) ),
856 $this->profileDBOut();
857 foreach ( $res as $row ) {
858 $rdfrom = intval( $row->rd_from
);
859 $from = $this->mPendingRedirectIDs
[$rdfrom]->getPrefixedText();
860 $to = Title
::makeTitle( $row->rd_namespace
, $row->rd_title
, $row->rd_fragment
, $row->rd_interwiki
);
861 unset( $this->mPendingRedirectIDs
[$rdfrom] );
862 if ( !isset( $this->mAllPages
[$row->rd_namespace
][$row->rd_title
] ) ) {
863 $lb->add( $row->rd_namespace
, $row->rd_title
);
865 $this->mRedirectTitles
[$from] = $to;
868 if ( $this->mPendingRedirectIDs
) {
869 // We found pages that aren't in the redirect table
871 foreach ( $this->mPendingRedirectIDs
as $id => $title ) {
872 $page = WikiPage
::factory( $title );
873 $rt = $page->insertRedirect();
875 // What the hell. Let's just ignore this
879 $this->mRedirectTitles
[$title->getPrefixedText()] = $rt;
880 unset( $this->mPendingRedirectIDs
[$id] );
887 * Get the cache mode for the data generated by this module.
888 * All PageSet users should take into account whether this returns a more-restrictive
889 * cache mode than the using module itself. For possible return values and other
890 * details about cache modes, see ApiMain::setCacheMode()
892 * Public caching will only be allowed if *all* the modules that supply
893 * data for a given request return a cache mode of public.
899 public function getCacheMode( $params = null ) {
900 return $this->mCacheMode
;
904 * Given an array of title strings, convert them into Title objects.
905 * Alternatively, an array of Title objects may be given.
906 * This method validates access rights for the title,
907 * and appends normalization values to the output.
909 * @param array $titles of Title objects or strings
912 private function processTitlesArray( $titles ) {
913 $usernames = array();
914 $linkBatch = new LinkBatch();
916 foreach ( $titles as $title ) {
917 if ( is_string( $title ) ) {
918 $titleObj = Title
::newFromText( $title, $this->mDefaultNamespace
);
923 // Handle invalid titles gracefully
924 $this->mAllPages
[0][$title] = $this->mFakePageId
;
925 $this->mInvalidTitles
[$this->mFakePageId
] = $title;
926 $this->mFakePageId
--;
927 continue; // There's nothing else we can do
929 $unconvertedTitle = $titleObj->getPrefixedText();
930 $titleWasConverted = false;
931 if ( $titleObj->isExternal() ) {
932 // This title is an interwiki link.
933 $this->mInterwikiTitles
[$unconvertedTitle] = $titleObj->getInterwiki();
937 if ( $this->mConvertTitles
&&
938 count( $wgContLang->getVariants() ) > 1 &&
939 !$titleObj->exists() ) {
940 // Language::findVariantLink will modify titleText and titleObj into
941 // the canonical variant if possible
942 $titleText = is_string( $title ) ?
$title : $titleObj->getPrefixedText();
943 $wgContLang->findVariantLink( $titleText, $titleObj );
944 $titleWasConverted = $unconvertedTitle !== $titleObj->getPrefixedText();
947 if ( $titleObj->getNamespace() < 0 ) {
948 // Handle Special and Media pages
949 $titleObj = $titleObj->fixSpecialName();
950 $this->mSpecialTitles
[$this->mFakePageId
] = $titleObj;
951 $this->mFakePageId
--;
954 $linkBatch->addObj( $titleObj );
958 // Make sure we remember the original title that was
959 // given to us. This way the caller can correlate new
960 // titles with the originally requested when e.g. the
961 // namespace is localized or the capitalization is
963 if ( $titleWasConverted ) {
964 $this->mConvertedTitles
[$unconvertedTitle] = $titleObj->getPrefixedText();
965 // In this case the page can't be Special.
966 if ( is_string( $title ) && $title !== $unconvertedTitle ) {
967 $this->mNormalizedTitles
[$title] = $unconvertedTitle;
969 } elseif ( is_string( $title ) && $title !== $titleObj->getPrefixedText() ) {
970 $this->mNormalizedTitles
[$title] = $titleObj->getPrefixedText();
973 // Need gender information
974 if ( MWNamespace
::hasGenderDistinction( $titleObj->getNamespace() ) ) {
975 $usernames[] = $titleObj->getText();
978 // Get gender information
979 $genderCache = GenderCache
::singleton();
980 $genderCache->doQuery( $usernames, __METHOD__
);
986 * Get the database connection (read-only)
987 * @return DatabaseBase
989 protected function getDB() {
990 return $this->mDbSource
->getDB();
994 * Returns the input array of integers with all values < 0 removed
996 * @param $array array
999 private static function getPositiveIntegers( $array ) {
1000 // bug 25734 API: possible issue with revids validation
1001 // It seems with a load of revision rows, MySQL gets upset
1002 // Remove any < 0 integers, as they can't be valid
1003 foreach ( $array as $i => $int ) {
1005 unset( $array[$i] );
1012 public function getAllowedParams( $flags = 0 ) {
1015 ApiBase
::PARAM_ISMULTI
=> true
1018 ApiBase
::PARAM_TYPE
=> 'integer',
1019 ApiBase
::PARAM_ISMULTI
=> true
1022 ApiBase
::PARAM_TYPE
=> 'integer',
1023 ApiBase
::PARAM_ISMULTI
=> true
1025 'redirects' => false,
1026 'converttitles' => false,
1028 if ( $this->mAllowGenerator
) {
1029 if ( $flags & ApiBase
::GET_VALUES_FOR_HELP
) {
1030 $result['generator'] = array(
1031 ApiBase
::PARAM_TYPE
=> $this->getGenerators()
1034 $result['generator'] = null;
1040 private static $generators = null;
1043 * Get an array of all available generators
1046 private function getGenerators() {
1047 if ( self
::$generators === null ) {
1048 $query = $this->mDbSource
;
1049 if ( !( $query instanceof ApiQuery
) ) {
1050 // If the parent container of this pageset is not ApiQuery,
1051 // we must create it to get module manager
1052 $query = $this->getMain()->getModuleManager()->getModule( 'query' );
1055 $mgr = $query->getModuleManager();
1056 foreach ( $mgr->getNamesWithClasses() as $name => $class ) {
1057 if ( is_subclass_of( $class, 'ApiQueryGeneratorBase' ) ) {
1062 self
::$generators = $gens;
1064 return self
::$generators;
1067 public function getParamDescription() {
1069 'titles' => 'A list of titles to work on',
1070 'pageids' => 'A list of page IDs to work on',
1071 'revids' => 'A list of revision IDs to work on',
1072 'generator' => array( 'Get the list of pages to work on by executing the specified query module.',
1073 'NOTE: generator parameter names must be prefixed with a \'g\', see examples' ),
1074 'redirects' => 'Automatically resolve redirects',
1075 'converttitles' => array( 'Convert titles to other variants if necessary. Only works if the wiki\'s content language supports variant conversion.',
1076 'Languages that support variant conversion include ' . implode( ', ', LanguageConverter
::$languagesWithVariants ) ),
1080 public function getPossibleErrors() {
1081 return array_merge( parent
::getPossibleErrors(), array(
1082 array( 'code' => 'multisource', 'info' => "Cannot use 'pageids' at the same time as 'dataSource'" ),
1083 array( 'code' => 'multisource', 'info' => "Cannot use 'revids' at the same time as 'dataSource'" ),
1084 array( 'code' => 'badgenerator', 'info' => 'Module $generatorName cannot be used as a generator' ),