c225b1ba7dcf486d88e72fc4d4121b7fea3d4c16
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 $flags int Zero or more flags like DISABLE_GENERATORS
79 * @param $defaultNamespace int 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 * Request an additional field from the page table.
222 * Must be called before execute()
223 * @param $fieldName string Field name
225 public function requestField( $fieldName ) {
226 $this->mRequestedPageFields
[$fieldName] = null;
230 * Get the value of a custom field previously requested through
232 * @param $fieldName string Field name
233 * @return mixed Field value
235 public function getCustomField( $fieldName ) {
236 return $this->mRequestedPageFields
[$fieldName];
240 * Get the fields that have to be queried from the page table:
241 * the ones requested through requestField() and a few basic ones
243 * @return array of field names
245 public function getPageTableFields() {
246 // Ensure we get minimum required fields
247 // DON'T change this order
249 'page_namespace' => null,
250 'page_title' => null,
254 if ( $this->mResolveRedirects
) {
255 $pageFlds['page_is_redirect'] = null;
258 // only store non-default fields
259 $this->mRequestedPageFields
= array_diff_key( $this->mRequestedPageFields
, $pageFlds );
261 $pageFlds = array_merge( $pageFlds, $this->mRequestedPageFields
);
262 return array_keys( $pageFlds );
266 * Returns an array [ns][dbkey] => page_id for all requested titles.
267 * page_id is a unique negative number in case title was not found.
268 * Invalid titles will also have negative page IDs and will be in namespace 0
271 public function getAllTitlesByNamespace() {
272 return $this->mAllPages
;
276 * All Title objects provided.
277 * @return array of Title objects
279 public function getTitles() {
280 return $this->mTitles
;
284 * Returns the number of unique pages (not revisions) in the set.
287 public function getTitleCount() {
288 return count( $this->mTitles
);
292 * Title objects that were found in the database.
293 * @return array page_id (int) => Title (obj)
295 public function getGoodTitles() {
296 return $this->mGoodTitles
;
300 * Returns the number of found unique pages (not revisions) in the set.
303 public function getGoodTitleCount() {
304 return count( $this->mGoodTitles
);
308 * Title objects that were NOT found in the database.
309 * The array's index will be negative for each item
310 * @return array of Title objects
312 public function getMissingTitles() {
313 return $this->mMissingTitles
;
317 * Titles that were deemed invalid by Title::newFromText()
318 * The array's index will be unique and negative for each item
319 * @return array of strings (not Title objects)
321 public function getInvalidTitles() {
322 return $this->mInvalidTitles
;
326 * Page IDs that were not found in the database
327 * @return array of page IDs
329 public function getMissingPageIDs() {
330 return $this->mMissingPageIDs
;
334 * Get a list of redirect resolutions - maps a title to its redirect
335 * target, as an array of output-ready arrays
338 public function getRedirectTitles() {
339 return $this->mRedirectTitles
;
343 * Get a list of redirect resolutions - maps a title to its redirect
345 * @param $result ApiResult
346 * @return array of prefixed_title (string) => Title object
349 public function getRedirectTitlesAsResult( $result = null ) {
351 foreach ( $this->getRedirectTitles() as $titleStrFrom => $titleTo ) {
353 'from' => strval( $titleStrFrom ),
354 'to' => $titleTo->getPrefixedText(),
356 if ( $titleTo->getFragment() !== '' ) {
357 $r['tofragment'] = $titleTo->getFragment();
361 if ( !empty( $values ) && $result ) {
362 $result->setIndexedTagName( $values, 'r' );
368 * Get a list of title normalizations - maps a title to its normalized
370 * @return array raw_prefixed_title (string) => prefixed_title (string)
372 public function getNormalizedTitles() {
373 return $this->mNormalizedTitles
;
377 * Get a list of title normalizations - maps a title to its normalized
378 * version in the form of result array.
379 * @param $result ApiResult
380 * @return array of raw_prefixed_title (string) => prefixed_title (string)
383 public function getNormalizedTitlesAsResult( $result = null ) {
385 foreach ( $this->getNormalizedTitles() as $rawTitleStr => $titleStr ) {
387 'from' => $rawTitleStr,
391 if ( !empty( $values ) && $result ) {
392 $result->setIndexedTagName( $values, 'n' );
398 * Get a list of title conversions - maps a title to its converted
400 * @return array raw_prefixed_title (string) => prefixed_title (string)
402 public function getConvertedTitles() {
403 return $this->mConvertedTitles
;
407 * Get a list of title conversions - maps a title to its converted
408 * version as a result array.
409 * @param $result ApiResult
410 * @return array of (from, to) strings
413 public function getConvertedTitlesAsResult( $result = null ) {
415 foreach ( $this->getConvertedTitles() as $rawTitleStr => $titleStr ) {
417 'from' => $rawTitleStr,
421 if ( !empty( $values ) && $result ) {
422 $result->setIndexedTagName( $values, 'c' );
428 * Get a list of interwiki titles - maps a title to its interwiki
430 * @return array raw_prefixed_title (string) => interwiki_prefix (string)
432 public function getInterwikiTitles() {
433 return $this->mInterwikiTitles
;
437 * Get a list of interwiki titles - maps a title to its interwiki
439 * @param $result ApiResult
440 * @param $iwUrl boolean
441 * @return array raw_prefixed_title (string) => interwiki_prefix (string)
444 public function getInterwikiTitlesAsResult( $result = null, $iwUrl = false ) {
446 foreach ( $this->getInterwikiTitles() as $rawTitleStr => $interwikiStr ) {
448 'title' => $rawTitleStr,
449 'iw' => $interwikiStr,
452 $title = Title
::newFromText( $rawTitleStr );
453 $item['url'] = $title->getFullURL( '', false, PROTO_CURRENT
);
457 if ( !empty( $values ) && $result ) {
458 $result->setIndexedTagName( $values, 'i' );
464 * Get the list of revision IDs (requested with the revids= parameter)
465 * @return array revID (int) => pageID (int)
467 public function getRevisionIDs() {
468 return $this->mGoodRevIDs
;
472 * Revision IDs that were not found in the database
473 * @return array of revision IDs
475 public function getMissingRevisionIDs() {
476 return $this->mMissingRevIDs
;
480 * Revision IDs that were not found in the database as result array.
481 * @param $result ApiResult
482 * @return array of revision IDs
485 public function getMissingRevisionIDsAsResult( $result = null ) {
487 foreach ( $this->getMissingRevisionIDs() as $revid ) {
488 $values[$revid] = array(
492 if ( !empty( $values ) && $result ) {
493 $result->setIndexedTagName( $values, 'rev' );
499 * Get the list of titles with negative namespace
500 * @return array Title
502 public function getSpecialTitles() {
503 return $this->mSpecialTitles
;
507 * Returns the number of revisions (requested with revids= parameter).
508 * @return int Number of revisions.
510 public function getRevisionCount() {
511 return count( $this->getRevisionIDs() );
515 * Populate this PageSet from a list of Titles
516 * @param $titles array of Title objects
518 public function populateFromTitles( $titles ) {
520 $this->initFromTitles( $titles );
525 * Populate this PageSet from a list of page IDs
526 * @param $pageIDs array of page IDs
528 public function populateFromPageIDs( $pageIDs ) {
530 $this->initFromPageIds( $pageIDs );
535 * Populate this PageSet from a rowset returned from the database
536 * @param $db DatabaseBase object
537 * @param $queryResult ResultWrapper Query result object
539 public function populateFromQueryResult( $db, $queryResult ) {
541 $this->initFromQueryResult( $queryResult );
546 * Populate this PageSet from a list of revision IDs
547 * @param $revIDs array of revision IDs
549 public function populateFromRevisionIDs( $revIDs ) {
551 $this->initFromRevIDs( $revIDs );
556 * Extract all requested fields from the row received from the database
557 * @param $row Result row
559 public function processDbRow( $row ) {
560 // Store Title object in various data structures
561 $title = Title
::newFromRow( $row );
563 $pageId = intval( $row->page_id
);
564 $this->mAllPages
[$row->page_namespace
][$row->page_title
] = $pageId;
565 $this->mTitles
[] = $title;
567 if ( $this->mResolveRedirects
&& $row->page_is_redirect
== '1' ) {
568 $this->mPendingRedirectIDs
[$pageId] = $title;
570 $this->mGoodTitles
[$pageId] = $title;
573 foreach ( $this->mRequestedPageFields
as $fieldName => &$fieldValues ) {
574 $fieldValues[$pageId] = $row-> $fieldName;
579 * Do not use, does nothing, will be removed
582 public function finishPageSetGeneration() {
583 wfDeprecated( __METHOD__
, '1.21' );
587 * This method populates internal variables with page information
588 * based on the given array of title strings.
591 * #1 For each title, get data from `page` table
592 * #2 If page was not found in the DB, store it as missing
594 * Additionally, when resolving redirects:
595 * #3 If no more redirects left, stop.
596 * #4 For each redirect, get its target from the `redirect` table.
597 * #5 Substitute the original LinkBatch object with the new list
598 * #6 Repeat from step #1
600 * @param $titles array of Title objects or strings
602 private function initFromTitles( $titles ) {
603 // Get validated and normalized title objects
604 $linkBatch = $this->processTitlesArray( $titles );
605 if ( $linkBatch->isEmpty() ) {
609 $db = $this->getDB();
610 $set = $linkBatch->constructSet( 'page', $db );
612 // Get pageIDs data from the `page` table
613 $this->profileDBIn();
614 $res = $db->select( 'page', $this->getPageTableFields(), $set,
616 $this->profileDBOut();
618 // Hack: get the ns:titles stored in array(ns => array(titles)) format
619 $this->initFromQueryResult( $res, $linkBatch->data
, true ); // process Titles
621 // Resolve any found redirects
622 $this->resolvePendingRedirects();
626 * Does the same as initFromTitles(), but is based on page IDs instead
627 * @param $pageids array of page IDs
629 private function initFromPageIds( $pageids ) {
634 $pageids = array_map( 'intval', $pageids ); // paranoia
635 $remaining = array_flip( $pageids );
637 $pageids = self
::getPositiveIntegers( $pageids );
640 if ( !empty( $pageids ) ) {
642 'page_id' => $pageids
644 $db = $this->getDB();
646 // Get pageIDs data from the `page` table
647 $this->profileDBIn();
648 $res = $db->select( 'page', $this->getPageTableFields(), $set,
650 $this->profileDBOut();
653 $this->initFromQueryResult( $res, $remaining, false ); // process PageIDs
655 // Resolve any found redirects
656 $this->resolvePendingRedirects();
660 * Iterate through the result of the query on 'page' table,
661 * and for each row create and store title object and save any extra fields requested.
662 * @param $res ResultWrapper DB Query result
663 * @param $remaining array of either pageID or ns/title elements (optional).
664 * If given, any missing items will go to $mMissingPageIDs and $mMissingTitles
665 * @param $processTitles bool Must be provided together with $remaining.
666 * If true, treat $remaining as an array of [ns][title]
667 * If false, treat it as an array of [pageIDs]
669 private function initFromQueryResult( $res, &$remaining = null, $processTitles = null ) {
670 if ( !is_null( $remaining ) && is_null( $processTitles ) ) {
671 ApiBase
::dieDebug( __METHOD__
, 'Missing $processTitles parameter when $remaining is provided' );
674 $usernames = array();
676 foreach ( $res as $row ) {
677 $pageId = intval( $row->page_id
);
679 // Remove found page from the list of remaining items
680 if ( isset( $remaining ) ) {
681 if ( $processTitles ) {
682 unset( $remaining[$row->page_namespace
][$row->page_title
] );
684 unset( $remaining[$pageId] );
688 // Store any extra fields requested by modules
689 $this->processDbRow( $row );
691 // Need gender information
692 if ( MWNamespace
::hasGenderDistinction( $row->page_namespace
) ) {
693 $usernames[] = $row->page_title
;
698 if ( isset( $remaining ) ) {
699 // Any items left in the $remaining list are added as missing
700 if ( $processTitles ) {
701 // The remaining titles in $remaining are non-existent pages
702 foreach ( $remaining as $ns => $dbkeys ) {
703 foreach ( array_keys( $dbkeys ) as $dbkey ) {
704 $title = Title
::makeTitle( $ns, $dbkey );
705 $this->mAllPages
[$ns][$dbkey] = $this->mFakePageId
;
706 $this->mMissingTitles
[$this->mFakePageId
] = $title;
707 $this->mFakePageId
--;
708 $this->mTitles
[] = $title;
710 // need gender information
711 if ( MWNamespace
::hasGenderDistinction( $ns ) ) {
712 $usernames[] = $dbkey;
717 // The remaining pageids do not exist
718 if ( !$this->mMissingPageIDs
) {
719 $this->mMissingPageIDs
= array_keys( $remaining );
721 $this->mMissingPageIDs
= array_merge( $this->mMissingPageIDs
, array_keys( $remaining ) );
726 // Get gender information
727 $genderCache = GenderCache
::singleton();
728 $genderCache->doQuery( $usernames, __METHOD__
);
732 * Does the same as initFromTitles(), but is based on revision IDs
734 * @param $revids array of revision IDs
736 private function initFromRevIDs( $revids ) {
741 $revids = array_map( 'intval', $revids ); // paranoia
742 $db = $this->getDB();
744 $remaining = array_flip( $revids );
746 $revids = self
::getPositiveIntegers( $revids );
748 if ( !empty( $revids ) ) {
749 $tables = array( 'revision', 'page' );
750 $fields = array( 'rev_id', 'rev_page' );
751 $where = array( 'rev_id' => $revids, 'rev_page = page_id' );
753 // Get pageIDs data from the `page` table
754 $this->profileDBIn();
755 $res = $db->select( $tables, $fields, $where, __METHOD__
);
756 foreach ( $res as $row ) {
757 $revid = intval( $row->rev_id
);
758 $pageid = intval( $row->rev_page
);
759 $this->mGoodRevIDs
[$revid] = $pageid;
760 $pageids[$pageid] = '';
761 unset( $remaining[$revid] );
763 $this->profileDBOut();
766 $this->mMissingRevIDs
= array_keys( $remaining );
768 // Populate all the page information
769 $this->initFromPageIds( array_keys( $pageids ) );
773 * Resolve any redirects in the result if redirect resolution was
774 * requested. This function is called repeatedly until all redirects
775 * have been resolved.
777 private function resolvePendingRedirects() {
778 if ( $this->mResolveRedirects
) {
779 $db = $this->getDB();
780 $pageFlds = $this->getPageTableFields();
782 // Repeat until all redirects have been resolved
783 // The infinite loop is prevented by keeping all known pages in $this->mAllPages
784 while ( $this->mPendingRedirectIDs
) {
785 // Resolve redirects by querying the pagelinks table, and repeat the process
786 // Create a new linkBatch object for the next pass
787 $linkBatch = $this->getRedirectTargets();
789 if ( $linkBatch->isEmpty() ) {
793 $set = $linkBatch->constructSet( 'page', $db );
794 if ( $set === false ) {
798 // Get pageIDs data from the `page` table
799 $this->profileDBIn();
800 $res = $db->select( 'page', $pageFlds, $set, __METHOD__
);
801 $this->profileDBOut();
803 // Hack: get the ns:titles stored in array(ns => array(titles)) format
804 $this->initFromQueryResult( $res, $linkBatch->data
, true );
810 * Get the targets of the pending redirects from the database
812 * Also creates entries in the redirect table for redirects that don't
816 private function getRedirectTargets() {
817 $lb = new LinkBatch();
818 $db = $this->getDB();
820 $this->profileDBIn();
829 ), array( 'rd_from' => array_keys( $this->mPendingRedirectIDs
) ),
832 $this->profileDBOut();
833 foreach ( $res as $row ) {
834 $rdfrom = intval( $row->rd_from
);
835 $from = $this->mPendingRedirectIDs
[$rdfrom]->getPrefixedText();
836 $to = Title
::makeTitle( $row->rd_namespace
, $row->rd_title
, $row->rd_fragment
, $row->rd_interwiki
);
837 unset( $this->mPendingRedirectIDs
[$rdfrom] );
838 if ( !isset( $this->mAllPages
[$row->rd_namespace
][$row->rd_title
] ) ) {
839 $lb->add( $row->rd_namespace
, $row->rd_title
);
841 $this->mRedirectTitles
[$from] = $to;
844 if ( $this->mPendingRedirectIDs
) {
845 // We found pages that aren't in the redirect table
847 foreach ( $this->mPendingRedirectIDs
as $id => $title ) {
848 $page = WikiPage
::factory( $title );
849 $rt = $page->insertRedirect();
851 // What the hell. Let's just ignore this
855 $this->mRedirectTitles
[$title->getPrefixedText()] = $rt;
856 unset( $this->mPendingRedirectIDs
[$id] );
863 * Get the cache mode for the data generated by this module.
864 * All PageSet users should take into account whether this returns a more-restrictive
865 * cache mode than the using module itself. For possible return values and other
866 * details about cache modes, see ApiMain::setCacheMode()
868 * Public caching will only be allowed if *all* the modules that supply
869 * data for a given request return a cache mode of public.
875 public function getCacheMode( $params = null ) {
876 return $this->mCacheMode
;
880 * Given an array of title strings, convert them into Title objects.
881 * Alternatively, an array of Title objects may be given.
882 * This method validates access rights for the title,
883 * and appends normalization values to the output.
885 * @param $titles array of Title objects or strings
888 private function processTitlesArray( $titles ) {
889 $usernames = array();
890 $linkBatch = new LinkBatch();
892 foreach ( $titles as $title ) {
893 if ( is_string( $title ) ) {
894 $titleObj = Title
::newFromText( $title, $this->mDefaultNamespace
);
899 // Handle invalid titles gracefully
900 $this->mAllpages
[0][$title] = $this->mFakePageId
;
901 $this->mInvalidTitles
[$this->mFakePageId
] = $title;
902 $this->mFakePageId
--;
903 continue; // There's nothing else we can do
905 $unconvertedTitle = $titleObj->getPrefixedText();
906 $titleWasConverted = false;
907 if ( $titleObj->isExternal() ) {
908 // This title is an interwiki link.
909 $this->mInterwikiTitles
[$unconvertedTitle] = $titleObj->getInterwiki();
913 if ( $this->mConvertTitles
&&
914 count( $wgContLang->getVariants() ) > 1 &&
915 !$titleObj->exists() ) {
916 // Language::findVariantLink will modify titleText and titleObj into
917 // the canonical variant if possible
918 $titleText = is_string( $title ) ?
$title : $titleObj->getPrefixedText();
919 $wgContLang->findVariantLink( $titleText, $titleObj );
920 $titleWasConverted = $unconvertedTitle !== $titleObj->getPrefixedText();
923 if ( $titleObj->getNamespace() < 0 ) {
924 // Handle Special and Media pages
925 $titleObj = $titleObj->fixSpecialName();
926 $this->mSpecialTitles
[$this->mFakePageId
] = $titleObj;
927 $this->mFakePageId
--;
930 $linkBatch->addObj( $titleObj );
934 // Make sure we remember the original title that was
935 // given to us. This way the caller can correlate new
936 // titles with the originally requested when e.g. the
937 // namespace is localized or the capitalization is
939 if ( $titleWasConverted ) {
940 $this->mConvertedTitles
[$unconvertedTitle] = $titleObj->getPrefixedText();
941 // In this case the page can't be Special.
942 if ( is_string( $title ) && $title !== $unconvertedTitle ) {
943 $this->mNormalizedTitles
[$title] = $unconvertedTitle;
945 } elseif ( is_string( $title ) && $title !== $titleObj->getPrefixedText() ) {
946 $this->mNormalizedTitles
[$title] = $titleObj->getPrefixedText();
949 // Need gender information
950 if ( MWNamespace
::hasGenderDistinction( $titleObj->getNamespace() ) ) {
951 $usernames[] = $titleObj->getText();
954 // Get gender information
955 $genderCache = GenderCache
::singleton();
956 $genderCache->doQuery( $usernames, __METHOD__
);
962 * Get the database connection (read-only)
963 * @return DatabaseBase
965 protected function getDB() {
966 return $this->mDbSource
->getDB();
970 * Returns the input array of integers with all values < 0 removed
972 * @param $array array
975 private static function getPositiveIntegers( $array ) {
976 // bug 25734 API: possible issue with revids validation
977 // It seems with a load of revision rows, MySQL gets upset
978 // Remove any < 0 integers, as they can't be valid
979 foreach ( $array as $i => $int ) {
988 public function getAllowedParams( $flags = 0 ) {
991 ApiBase
::PARAM_ISMULTI
=> true
994 ApiBase
::PARAM_TYPE
=> 'integer',
995 ApiBase
::PARAM_ISMULTI
=> true
998 ApiBase
::PARAM_TYPE
=> 'integer',
999 ApiBase
::PARAM_ISMULTI
=> true
1001 'redirects' => false,
1002 'converttitles' => false,
1004 if ( $this->mAllowGenerator
) {
1005 if ( $flags & ApiBase
::GET_VALUES_FOR_HELP
) {
1006 $result['generator'] = array(
1007 ApiBase
::PARAM_TYPE
=> $this->getGenerators()
1010 $result['generator'] = null;
1016 private static $generators = null;
1019 * Get an array of all available generators
1022 private function getGenerators() {
1023 if ( self
::$generators === null ) {
1024 $query = $this->mDbSource
;
1025 if ( !( $query instanceof ApiQuery
) ) {
1026 // If the parent container of this pageset is not ApiQuery,
1027 // we must create it to get module manager
1028 $query = $this->getMain()->getModuleManager()->getModule( 'query' );
1031 $mgr = $query->getModuleManager();
1032 foreach ( $mgr->getNamesWithClasses() as $name => $class ) {
1033 if ( is_subclass_of( $class, 'ApiQueryGeneratorBase' ) ) {
1038 self
::$generators = $gens;
1040 return self
::$generators;
1043 public function getParamDescription() {
1045 'titles' => 'A list of titles to work on',
1046 'pageids' => 'A list of page IDs to work on',
1047 'revids' => 'A list of revision IDs to work on',
1048 'generator' => array( 'Get the list of pages to work on by executing the specified query module.',
1049 'NOTE: generator parameter names must be prefixed with a \'g\', see examples' ),
1050 'redirects' => 'Automatically resolve redirects',
1051 'converttitles' => array( 'Convert titles to other variants if necessary. Only works if the wiki\'s content language supports variant conversion.',
1052 'Languages that support variant conversion include ' . implode( ', ', LanguageConverter
::$languagesWithVariants ) ),
1056 public function getPossibleErrors() {
1057 return array_merge( parent
::getPossibleErrors(), array(
1058 array( 'code' => 'multisource', 'info' => "Cannot use 'pageids' at the same time as 'dataSource'" ),
1059 array( 'code' => 'multisource', 'info' => "Cannot use 'revids' at the same time as 'dataSource'" ),
1060 array( 'code' => 'badgenerator', 'info' => 'Module $generatorName cannot be used as a generator' ),