Merge "mediawiki.inspect#dumpTable: fix broken FF workaround"
[mediawiki.git] / includes / api / ApiPageSet.php
blobb05cb2b67bdfaf5fdb9aa459ae09588a9dd85476
1 <?php
2 /**
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
24 * @file
27 /**
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
32 * data requested.
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.
38 * @ingroup API
39 * @since 1.21 derives from ApiBase instead of ApiQueryBase
41 class ApiPageSet extends ApiBase {
43 /**
44 * Constructor flag: The new instance of ApiPageSet will ignore the 'generator=' parameter
45 * @since 1.21
47 const DISABLE_GENERATORS = 1;
49 private $mDbSource;
50 private $mParams;
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 /**
73 * @var int
75 private $mDefaultNamespace = NS_MAIN;
77 /**
78 * Constructor
79 * @param $dbSource ApiBase Module implementing getDB().
80 * Allows PageSet to reuse existing db connection from the shared state like ApiQuery.
81 * @param int $flags Zero or more flags like DISABLE_GENERATORS
82 * @param int $defaultNamespace the namespace to use if none is specified by a prefix.
83 * @since 1.21 accepts $flags instead of two boolean values
85 public function __construct( ApiBase $dbSource, $flags = 0, $defaultNamespace = NS_MAIN ) {
86 parent::__construct( $dbSource->getMain(), $dbSource->getModuleName() );
87 $this->mDbSource = $dbSource;
88 $this->mAllowGenerator = ( $flags & ApiPageSet::DISABLE_GENERATORS ) == 0;
89 $this->mDefaultNamespace = $defaultNamespace;
91 $this->profileIn();
92 $this->mParams = $this->extractRequestParams();
93 $this->mResolveRedirects = $this->mParams['redirects'];
94 $this->mConvertTitles = $this->mParams['converttitles'];
95 $this->profileOut();
98 /**
99 * In case execute() is not called, call this method to mark all relevant parameters as used
100 * This prevents unused parameters from being reported as warnings
102 public function executeDryRun() {
103 $this->executeInternal( true );
107 * Populate the PageSet from the request parameters.
109 public function execute() {
110 $this->executeInternal( false );
114 * Populate the PageSet from the request parameters.
115 * @param bool $isDryRun If true, instantiates generator, but only to mark relevant parameters as used
117 private function executeInternal( $isDryRun ) {
118 $this->profileIn();
120 $generatorName = $this->mAllowGenerator ? $this->mParams['generator'] : null;
121 if ( isset( $generatorName ) ) {
122 $dbSource = $this->mDbSource;
123 $isQuery = $dbSource instanceof ApiQuery;
124 if ( !$isQuery ) {
125 // If the parent container of this pageset is not ApiQuery, we must create it to run generator
126 $dbSource = $this->getMain()->getModuleManager()->getModule( 'query' );
127 // Enable profiling for query module because it will be used for db sql profiling
128 $dbSource->profileIn();
130 $generator = $dbSource->getModuleManager()->getModule( $generatorName, null, true );
131 if ( $generator === null ) {
132 $this->dieUsage( 'Unknown generator=' . $generatorName, 'badgenerator' );
134 if ( !$generator instanceof ApiQueryGeneratorBase ) {
135 $this->dieUsage( "Module $generatorName cannot be used as a generator", 'badgenerator' );
137 // Create a temporary pageset to store generator's output,
138 // add any additional fields generator may need, and execute pageset to populate titles/pageids
139 $tmpPageSet = new ApiPageSet( $dbSource, ApiPageSet::DISABLE_GENERATORS );
140 $generator->setGeneratorMode( $tmpPageSet );
141 $this->mCacheMode = $generator->getCacheMode( $generator->extractRequestParams() );
143 if ( !$isDryRun ) {
144 $generator->requestExtraData( $tmpPageSet );
146 $tmpPageSet->executeInternal( $isDryRun );
148 // populate this pageset with the generator output
149 $this->profileOut();
150 $generator->profileIn();
152 if ( !$isDryRun ) {
153 $generator->executeGenerator( $this );
154 wfRunHooks( 'APIQueryGeneratorAfterExecute', array( &$generator, &$this ) );
155 } else {
156 // Prevent warnings from being reported on these parameters
157 $main = $this->getMain();
158 foreach ( $generator->extractRequestParams() as $paramName => $param ) {
159 $main->getVal( $generator->encodeParamName( $paramName ) );
162 $generator->profileOut();
163 $this->profileIn();
165 if ( !$isDryRun ) {
166 $this->resolvePendingRedirects();
169 if ( !$isQuery ) {
170 // If this pageset is not part of the query, we called profileIn() above
171 $dbSource->profileOut();
173 } else {
174 // Only one of the titles/pageids/revids is allowed at the same time
175 $dataSource = null;
176 if ( isset( $this->mParams['titles'] ) ) {
177 $dataSource = 'titles';
179 if ( isset( $this->mParams['pageids'] ) ) {
180 if ( isset( $dataSource ) ) {
181 $this->dieUsage( "Cannot use 'pageids' at the same time as '$dataSource'", 'multisource' );
183 $dataSource = 'pageids';
185 if ( isset( $this->mParams['revids'] ) ) {
186 if ( isset( $dataSource ) ) {
187 $this->dieUsage( "Cannot use 'revids' at the same time as '$dataSource'", 'multisource' );
189 $dataSource = 'revids';
192 if ( !$isDryRun ) {
193 // Populate page information with the original user input
194 switch ( $dataSource ) {
195 case 'titles':
196 $this->initFromTitles( $this->mParams['titles'] );
197 break;
198 case 'pageids':
199 $this->initFromPageIds( $this->mParams['pageids'] );
200 break;
201 case 'revids':
202 if ( $this->mResolveRedirects ) {
203 $this->setWarning( 'Redirect resolution cannot be used together with the revids= parameter. ' .
204 'Any redirects the revids= point to have not been resolved.' );
206 $this->mResolveRedirects = false;
207 $this->initFromRevIDs( $this->mParams['revids'] );
208 break;
209 default:
210 // Do nothing - some queries do not need any of the data sources.
211 break;
215 $this->profileOut();
219 * Check whether this PageSet is resolving redirects
220 * @return bool
222 public function isResolvingRedirects() {
223 return $this->mResolveRedirects;
227 * Return the parameter name that is the source of data for this PageSet
229 * If multiple source parameters are specified (e.g. titles and pageids),
230 * one will be named arbitrarily.
232 * @return string|null
234 public function getDataSource() {
235 if ( $this->mAllowGenerator && isset( $this->mParams['generator'] ) ) {
236 return 'generator';
238 if ( isset( $this->mParams['titles'] ) ) {
239 return 'titles';
241 if ( isset( $this->mParams['pageids'] ) ) {
242 return 'pageids';
244 if ( isset( $this->mParams['revids'] ) ) {
245 return 'revids';
247 return null;
251 * Request an additional field from the page table.
252 * Must be called before execute()
253 * @param string $fieldName Field name
255 public function requestField( $fieldName ) {
256 $this->mRequestedPageFields[$fieldName] = null;
260 * Get the value of a custom field previously requested through
261 * requestField()
262 * @param string $fieldName Field name
263 * @return mixed Field value
265 public function getCustomField( $fieldName ) {
266 return $this->mRequestedPageFields[$fieldName];
270 * Get the fields that have to be queried from the page table:
271 * the ones requested through requestField() and a few basic ones
272 * we always need
273 * @return array of field names
275 public function getPageTableFields() {
276 // Ensure we get minimum required fields
277 // DON'T change this order
278 $pageFlds = array(
279 'page_namespace' => null,
280 'page_title' => null,
281 'page_id' => null,
284 if ( $this->mResolveRedirects ) {
285 $pageFlds['page_is_redirect'] = null;
288 // only store non-default fields
289 $this->mRequestedPageFields = array_diff_key( $this->mRequestedPageFields, $pageFlds );
291 $pageFlds = array_merge( $pageFlds, $this->mRequestedPageFields );
292 return array_keys( $pageFlds );
296 * Returns an array [ns][dbkey] => page_id for all requested titles.
297 * page_id is a unique negative number in case title was not found.
298 * Invalid titles will also have negative page IDs and will be in namespace 0
299 * @return array
301 public function getAllTitlesByNamespace() {
302 return $this->mAllPages;
306 * All Title objects provided.
307 * @return array of Title objects
309 public function getTitles() {
310 return $this->mTitles;
314 * Returns the number of unique pages (not revisions) in the set.
315 * @return int
317 public function getTitleCount() {
318 return count( $this->mTitles );
322 * Title objects that were found in the database.
323 * @return array page_id (int) => Title (obj)
325 public function getGoodTitles() {
326 return $this->mGoodTitles;
330 * Returns the number of found unique pages (not revisions) in the set.
331 * @return int
333 public function getGoodTitleCount() {
334 return count( $this->mGoodTitles );
338 * Title objects that were NOT found in the database.
339 * The array's index will be negative for each item
340 * @return array of Title objects
342 public function getMissingTitles() {
343 return $this->mMissingTitles;
347 * Titles that were deemed invalid by Title::newFromText()
348 * The array's index will be unique and negative for each item
349 * @return array of strings (not Title objects)
351 public function getInvalidTitles() {
352 return $this->mInvalidTitles;
356 * Page IDs that were not found in the database
357 * @return array of page IDs
359 public function getMissingPageIDs() {
360 return $this->mMissingPageIDs;
364 * Get a list of redirect resolutions - maps a title to its redirect
365 * target, as an array of output-ready arrays
366 * @return array
368 public function getRedirectTitles() {
369 return $this->mRedirectTitles;
373 * Get a list of redirect resolutions - maps a title to its redirect
374 * target.
375 * @param $result ApiResult
376 * @return array of prefixed_title (string) => Title object
377 * @since 1.21
379 public function getRedirectTitlesAsResult( $result = null ) {
380 $values = array();
381 foreach ( $this->getRedirectTitles() as $titleStrFrom => $titleTo ) {
382 $r = array(
383 'from' => strval( $titleStrFrom ),
384 'to' => $titleTo->getPrefixedText(),
386 if ( $titleTo->getFragment() !== '' ) {
387 $r['tofragment'] = $titleTo->getFragment();
389 $values[] = $r;
391 if ( !empty( $values ) && $result ) {
392 $result->setIndexedTagName( $values, 'r' );
394 return $values;
398 * Get a list of title normalizations - maps a title to its normalized
399 * version.
400 * @return array raw_prefixed_title (string) => prefixed_title (string)
402 public function getNormalizedTitles() {
403 return $this->mNormalizedTitles;
407 * Get a list of title normalizations - maps a title to its normalized
408 * version in the form of result array.
409 * @param $result ApiResult
410 * @return array of raw_prefixed_title (string) => prefixed_title (string)
411 * @since 1.21
413 public function getNormalizedTitlesAsResult( $result = null ) {
414 $values = array();
415 foreach ( $this->getNormalizedTitles() as $rawTitleStr => $titleStr ) {
416 $values[] = array(
417 'from' => $rawTitleStr,
418 'to' => $titleStr
421 if ( !empty( $values ) && $result ) {
422 $result->setIndexedTagName( $values, 'n' );
424 return $values;
428 * Get a list of title conversions - maps a title to its converted
429 * version.
430 * @return array raw_prefixed_title (string) => prefixed_title (string)
432 public function getConvertedTitles() {
433 return $this->mConvertedTitles;
437 * Get a list of title conversions - maps a title to its converted
438 * version as a result array.
439 * @param $result ApiResult
440 * @return array of (from, to) strings
441 * @since 1.21
443 public function getConvertedTitlesAsResult( $result = null ) {
444 $values = array();
445 foreach ( $this->getConvertedTitles() as $rawTitleStr => $titleStr ) {
446 $values[] = array(
447 'from' => $rawTitleStr,
448 'to' => $titleStr
451 if ( !empty( $values ) && $result ) {
452 $result->setIndexedTagName( $values, 'c' );
454 return $values;
458 * Get a list of interwiki titles - maps a title to its interwiki
459 * prefix.
460 * @return array raw_prefixed_title (string) => interwiki_prefix (string)
462 public function getInterwikiTitles() {
463 return $this->mInterwikiTitles;
467 * Get a list of interwiki titles - maps a title to its interwiki
468 * prefix as result.
469 * @param $result ApiResult
470 * @param $iwUrl boolean
471 * @return array raw_prefixed_title (string) => interwiki_prefix (string)
472 * @since 1.21
474 public function getInterwikiTitlesAsResult( $result = null, $iwUrl = false ) {
475 $values = array();
476 foreach ( $this->getInterwikiTitles() as $rawTitleStr => $interwikiStr ) {
477 $item = array(
478 'title' => $rawTitleStr,
479 'iw' => $interwikiStr,
481 if ( $iwUrl ) {
482 $title = Title::newFromText( $rawTitleStr );
483 $item['url'] = $title->getFullURL( '', false, PROTO_CURRENT );
485 $values[] = $item;
487 if ( !empty( $values ) && $result ) {
488 $result->setIndexedTagName( $values, 'i' );
490 return $values;
494 * Get the list of revision IDs (requested with the revids= parameter)
495 * @return array revID (int) => pageID (int)
497 public function getRevisionIDs() {
498 return $this->mGoodRevIDs;
502 * Revision IDs that were not found in the database
503 * @return array of revision IDs
505 public function getMissingRevisionIDs() {
506 return $this->mMissingRevIDs;
510 * Revision IDs that were not found in the database as result array.
511 * @param $result ApiResult
512 * @return array of revision IDs
513 * @since 1.21
515 public function getMissingRevisionIDsAsResult( $result = null ) {
516 $values = array();
517 foreach ( $this->getMissingRevisionIDs() as $revid ) {
518 $values[$revid] = array(
519 'revid' => $revid
522 if ( !empty( $values ) && $result ) {
523 $result->setIndexedTagName( $values, 'rev' );
525 return $values;
529 * Get the list of titles with negative namespace
530 * @return array Title
532 public function getSpecialTitles() {
533 return $this->mSpecialTitles;
537 * Returns the number of revisions (requested with revids= parameter).
538 * @return int Number of revisions.
540 public function getRevisionCount() {
541 return count( $this->getRevisionIDs() );
545 * Populate this PageSet from a list of Titles
546 * @param array $titles of Title objects
548 public function populateFromTitles( $titles ) {
549 $this->profileIn();
550 $this->initFromTitles( $titles );
551 $this->profileOut();
555 * Populate this PageSet from a list of page IDs
556 * @param array $pageIDs of page IDs
558 public function populateFromPageIDs( $pageIDs ) {
559 $this->profileIn();
560 $this->initFromPageIds( $pageIDs );
561 $this->profileOut();
565 * Populate this PageSet from a rowset returned from the database
566 * @param $db DatabaseBase object
567 * @param $queryResult ResultWrapper Query result object
569 public function populateFromQueryResult( $db, $queryResult ) {
570 $this->profileIn();
571 $this->initFromQueryResult( $queryResult );
572 $this->profileOut();
576 * Populate this PageSet from a list of revision IDs
577 * @param array $revIDs of revision IDs
579 public function populateFromRevisionIDs( $revIDs ) {
580 $this->profileIn();
581 $this->initFromRevIDs( $revIDs );
582 $this->profileOut();
586 * Extract all requested fields from the row received from the database
587 * @param $row Result row
589 public function processDbRow( $row ) {
590 // Store Title object in various data structures
591 $title = Title::newFromRow( $row );
593 $pageId = intval( $row->page_id );
594 $this->mAllPages[$row->page_namespace][$row->page_title] = $pageId;
595 $this->mTitles[] = $title;
597 if ( $this->mResolveRedirects && $row->page_is_redirect == '1' ) {
598 $this->mPendingRedirectIDs[$pageId] = $title;
599 } else {
600 $this->mGoodTitles[$pageId] = $title;
603 foreach ( $this->mRequestedPageFields as $fieldName => &$fieldValues ) {
604 $fieldValues[$pageId] = $row->$fieldName;
609 * Do not use, does nothing, will be removed
610 * @deprecated since 1.21
612 public function finishPageSetGeneration() {
613 wfDeprecated( __METHOD__, '1.21' );
617 * This method populates internal variables with page information
618 * based on the given array of title strings.
620 * Steps:
621 * #1 For each title, get data from `page` table
622 * #2 If page was not found in the DB, store it as missing
624 * Additionally, when resolving redirects:
625 * #3 If no more redirects left, stop.
626 * #4 For each redirect, get its target from the `redirect` table.
627 * #5 Substitute the original LinkBatch object with the new list
628 * #6 Repeat from step #1
630 * @param array $titles of Title objects or strings
632 private function initFromTitles( $titles ) {
633 // Get validated and normalized title objects
634 $linkBatch = $this->processTitlesArray( $titles );
635 if ( $linkBatch->isEmpty() ) {
636 return;
639 $db = $this->getDB();
640 $set = $linkBatch->constructSet( 'page', $db );
642 // Get pageIDs data from the `page` table
643 $this->profileDBIn();
644 $res = $db->select( 'page', $this->getPageTableFields(), $set,
645 __METHOD__ );
646 $this->profileDBOut();
648 // Hack: get the ns:titles stored in array(ns => array(titles)) format
649 $this->initFromQueryResult( $res, $linkBatch->data, true ); // process Titles
651 // Resolve any found redirects
652 $this->resolvePendingRedirects();
656 * Does the same as initFromTitles(), but is based on page IDs instead
657 * @param array $pageids of page IDs
659 private function initFromPageIds( $pageids ) {
660 if ( !$pageids ) {
661 return;
664 $pageids = array_map( 'intval', $pageids ); // paranoia
665 $remaining = array_flip( $pageids );
667 $pageids = self::getPositiveIntegers( $pageids );
669 $res = null;
670 if ( !empty( $pageids ) ) {
671 $set = array(
672 'page_id' => $pageids
674 $db = $this->getDB();
676 // Get pageIDs data from the `page` table
677 $this->profileDBIn();
678 $res = $db->select( 'page', $this->getPageTableFields(), $set,
679 __METHOD__ );
680 $this->profileDBOut();
683 $this->initFromQueryResult( $res, $remaining, false ); // process PageIDs
685 // Resolve any found redirects
686 $this->resolvePendingRedirects();
690 * Iterate through the result of the query on 'page' table,
691 * and for each row create and store title object and save any extra fields requested.
692 * @param $res ResultWrapper DB Query result
693 * @param array $remaining of either pageID or ns/title elements (optional).
694 * If given, any missing items will go to $mMissingPageIDs and $mMissingTitles
695 * @param bool $processTitles Must be provided together with $remaining.
696 * If true, treat $remaining as an array of [ns][title]
697 * If false, treat it as an array of [pageIDs]
699 private function initFromQueryResult( $res, &$remaining = null, $processTitles = null ) {
700 if ( !is_null( $remaining ) && is_null( $processTitles ) ) {
701 ApiBase::dieDebug( __METHOD__, 'Missing $processTitles parameter when $remaining is provided' );
704 $usernames = array();
705 if ( $res ) {
706 foreach ( $res as $row ) {
707 $pageId = intval( $row->page_id );
709 // Remove found page from the list of remaining items
710 if ( isset( $remaining ) ) {
711 if ( $processTitles ) {
712 unset( $remaining[$row->page_namespace][$row->page_title] );
713 } else {
714 unset( $remaining[$pageId] );
718 // Store any extra fields requested by modules
719 $this->processDbRow( $row );
721 // Need gender information
722 if ( MWNamespace::hasGenderDistinction( $row->page_namespace ) ) {
723 $usernames[] = $row->page_title;
728 if ( isset( $remaining ) ) {
729 // Any items left in the $remaining list are added as missing
730 if ( $processTitles ) {
731 // The remaining titles in $remaining are non-existent pages
732 foreach ( $remaining as $ns => $dbkeys ) {
733 foreach ( array_keys( $dbkeys ) as $dbkey ) {
734 $title = Title::makeTitle( $ns, $dbkey );
735 $this->mAllPages[$ns][$dbkey] = $this->mFakePageId;
736 $this->mMissingTitles[$this->mFakePageId] = $title;
737 $this->mFakePageId--;
738 $this->mTitles[] = $title;
740 // need gender information
741 if ( MWNamespace::hasGenderDistinction( $ns ) ) {
742 $usernames[] = $dbkey;
746 } else {
747 // The remaining pageids do not exist
748 if ( !$this->mMissingPageIDs ) {
749 $this->mMissingPageIDs = array_keys( $remaining );
750 } else {
751 $this->mMissingPageIDs = array_merge( $this->mMissingPageIDs, array_keys( $remaining ) );
756 // Get gender information
757 $genderCache = GenderCache::singleton();
758 $genderCache->doQuery( $usernames, __METHOD__ );
762 * Does the same as initFromTitles(), but is based on revision IDs
763 * instead
764 * @param array $revids of revision IDs
766 private function initFromRevIDs( $revids ) {
767 if ( !$revids ) {
768 return;
771 $revids = array_map( 'intval', $revids ); // paranoia
772 $db = $this->getDB();
773 $pageids = array();
774 $remaining = array_flip( $revids );
776 $revids = self::getPositiveIntegers( $revids );
778 if ( !empty( $revids ) ) {
779 $tables = array( 'revision', 'page' );
780 $fields = array( 'rev_id', 'rev_page' );
781 $where = array( 'rev_id' => $revids, 'rev_page = page_id' );
783 // Get pageIDs data from the `page` table
784 $this->profileDBIn();
785 $res = $db->select( $tables, $fields, $where, __METHOD__ );
786 foreach ( $res as $row ) {
787 $revid = intval( $row->rev_id );
788 $pageid = intval( $row->rev_page );
789 $this->mGoodRevIDs[$revid] = $pageid;
790 $pageids[$pageid] = '';
791 unset( $remaining[$revid] );
793 $this->profileDBOut();
796 $this->mMissingRevIDs = array_keys( $remaining );
798 // Populate all the page information
799 $this->initFromPageIds( array_keys( $pageids ) );
803 * Resolve any redirects in the result if redirect resolution was
804 * requested. This function is called repeatedly until all redirects
805 * have been resolved.
807 private function resolvePendingRedirects() {
808 if ( $this->mResolveRedirects ) {
809 $db = $this->getDB();
810 $pageFlds = $this->getPageTableFields();
812 // Repeat until all redirects have been resolved
813 // The infinite loop is prevented by keeping all known pages in $this->mAllPages
814 while ( $this->mPendingRedirectIDs ) {
815 // Resolve redirects by querying the pagelinks table, and repeat the process
816 // Create a new linkBatch object for the next pass
817 $linkBatch = $this->getRedirectTargets();
819 if ( $linkBatch->isEmpty() ) {
820 break;
823 $set = $linkBatch->constructSet( 'page', $db );
824 if ( $set === false ) {
825 break;
828 // Get pageIDs data from the `page` table
829 $this->profileDBIn();
830 $res = $db->select( 'page', $pageFlds, $set, __METHOD__ );
831 $this->profileDBOut();
833 // Hack: get the ns:titles stored in array(ns => array(titles)) format
834 $this->initFromQueryResult( $res, $linkBatch->data, true );
840 * Get the targets of the pending redirects from the database
842 * Also creates entries in the redirect table for redirects that don't
843 * have one.
844 * @return LinkBatch
846 private function getRedirectTargets() {
847 $lb = new LinkBatch();
848 $db = $this->getDB();
850 $this->profileDBIn();
851 $res = $db->select(
852 'redirect',
853 array(
854 'rd_from',
855 'rd_namespace',
856 'rd_fragment',
857 'rd_interwiki',
858 'rd_title'
859 ), array( 'rd_from' => array_keys( $this->mPendingRedirectIDs ) ),
860 __METHOD__
862 $this->profileDBOut();
863 foreach ( $res as $row ) {
864 $rdfrom = intval( $row->rd_from );
865 $from = $this->mPendingRedirectIDs[$rdfrom]->getPrefixedText();
866 $to = Title::makeTitle( $row->rd_namespace, $row->rd_title, $row->rd_fragment, $row->rd_interwiki );
867 unset( $this->mPendingRedirectIDs[$rdfrom] );
868 if ( !$to->isExternal() && !isset( $this->mAllPages[$row->rd_namespace][$row->rd_title] ) ) {
869 $lb->add( $row->rd_namespace, $row->rd_title );
871 $this->mRedirectTitles[$from] = $to;
874 if ( $this->mPendingRedirectIDs ) {
875 // We found pages that aren't in the redirect table
876 // Add them
877 foreach ( $this->mPendingRedirectIDs as $id => $title ) {
878 $page = WikiPage::factory( $title );
879 $rt = $page->insertRedirect();
880 if ( !$rt ) {
881 // What the hell. Let's just ignore this
882 continue;
884 $lb->addObj( $rt );
885 $this->mRedirectTitles[$title->getPrefixedText()] = $rt;
886 unset( $this->mPendingRedirectIDs[$id] );
889 return $lb;
893 * Get the cache mode for the data generated by this module.
894 * All PageSet users should take into account whether this returns a more-restrictive
895 * cache mode than the using module itself. For possible return values and other
896 * details about cache modes, see ApiMain::setCacheMode()
898 * Public caching will only be allowed if *all* the modules that supply
899 * data for a given request return a cache mode of public.
901 * @param $params
902 * @return string
903 * @since 1.21
905 public function getCacheMode( $params = null ) {
906 return $this->mCacheMode;
910 * Given an array of title strings, convert them into Title objects.
911 * Alternatively, an array of Title objects may be given.
912 * This method validates access rights for the title,
913 * and appends normalization values to the output.
915 * @param array $titles of Title objects or strings
916 * @return LinkBatch
918 private function processTitlesArray( $titles ) {
919 $usernames = array();
920 $linkBatch = new LinkBatch();
922 foreach ( $titles as $title ) {
923 if ( is_string( $title ) ) {
924 $titleObj = Title::newFromText( $title, $this->mDefaultNamespace );
925 } else {
926 $titleObj = $title;
928 if ( !$titleObj ) {
929 // Handle invalid titles gracefully
930 $this->mAllPages[0][$title] = $this->mFakePageId;
931 $this->mInvalidTitles[$this->mFakePageId] = $title;
932 $this->mFakePageId--;
933 continue; // There's nothing else we can do
935 $unconvertedTitle = $titleObj->getPrefixedText();
936 $titleWasConverted = false;
937 if ( $titleObj->isExternal() ) {
938 // This title is an interwiki link.
939 $this->mInterwikiTitles[$unconvertedTitle] = $titleObj->getInterwiki();
940 } else {
941 // Variants checking
942 global $wgContLang;
943 if ( $this->mConvertTitles &&
944 count( $wgContLang->getVariants() ) > 1 &&
945 !$titleObj->exists() ) {
946 // Language::findVariantLink will modify titleText and titleObj into
947 // the canonical variant if possible
948 $titleText = is_string( $title ) ? $title : $titleObj->getPrefixedText();
949 $wgContLang->findVariantLink( $titleText, $titleObj );
950 $titleWasConverted = $unconvertedTitle !== $titleObj->getPrefixedText();
953 if ( $titleObj->getNamespace() < 0 ) {
954 // Handle Special and Media pages
955 $titleObj = $titleObj->fixSpecialName();
956 $this->mSpecialTitles[$this->mFakePageId] = $titleObj;
957 $this->mFakePageId--;
958 } else {
959 // Regular page
960 $linkBatch->addObj( $titleObj );
964 // Make sure we remember the original title that was
965 // given to us. This way the caller can correlate new
966 // titles with the originally requested when e.g. the
967 // namespace is localized or the capitalization is
968 // different
969 if ( $titleWasConverted ) {
970 $this->mConvertedTitles[$unconvertedTitle] = $titleObj->getPrefixedText();
971 // In this case the page can't be Special.
972 if ( is_string( $title ) && $title !== $unconvertedTitle ) {
973 $this->mNormalizedTitles[$title] = $unconvertedTitle;
975 } elseif ( is_string( $title ) && $title !== $titleObj->getPrefixedText() ) {
976 $this->mNormalizedTitles[$title] = $titleObj->getPrefixedText();
979 // Need gender information
980 if ( MWNamespace::hasGenderDistinction( $titleObj->getNamespace() ) ) {
981 $usernames[] = $titleObj->getText();
984 // Get gender information
985 $genderCache = GenderCache::singleton();
986 $genderCache->doQuery( $usernames, __METHOD__ );
988 return $linkBatch;
992 * Get the database connection (read-only)
993 * @return DatabaseBase
995 protected function getDB() {
996 return $this->mDbSource->getDB();
1000 * Returns the input array of integers with all values < 0 removed
1002 * @param $array array
1003 * @return array
1005 private static function getPositiveIntegers( $array ) {
1006 // bug 25734 API: possible issue with revids validation
1007 // It seems with a load of revision rows, MySQL gets upset
1008 // Remove any < 0 integers, as they can't be valid
1009 foreach ( $array as $i => $int ) {
1010 if ( $int < 0 ) {
1011 unset( $array[$i] );
1015 return $array;
1018 public function getAllowedParams( $flags = 0 ) {
1019 $result = array(
1020 'titles' => array(
1021 ApiBase::PARAM_ISMULTI => true
1023 'pageids' => array(
1024 ApiBase::PARAM_TYPE => 'integer',
1025 ApiBase::PARAM_ISMULTI => true
1027 'revids' => array(
1028 ApiBase::PARAM_TYPE => 'integer',
1029 ApiBase::PARAM_ISMULTI => true
1031 'redirects' => false,
1032 'converttitles' => false,
1034 if ( $this->mAllowGenerator ) {
1035 if ( $flags & ApiBase::GET_VALUES_FOR_HELP ) {
1036 $result['generator'] = array(
1037 ApiBase::PARAM_TYPE => $this->getGenerators()
1039 } else {
1040 $result['generator'] = null;
1043 return $result;
1046 private static $generators = null;
1049 * Get an array of all available generators
1050 * @return array
1052 private function getGenerators() {
1053 if ( self::$generators === null ) {
1054 $query = $this->mDbSource;
1055 if ( !( $query instanceof ApiQuery ) ) {
1056 // If the parent container of this pageset is not ApiQuery,
1057 // we must create it to get module manager
1058 $query = $this->getMain()->getModuleManager()->getModule( 'query' );
1060 $gens = array();
1061 $mgr = $query->getModuleManager();
1062 foreach ( $mgr->getNamesWithClasses() as $name => $class ) {
1063 if ( is_subclass_of( $class, 'ApiQueryGeneratorBase' ) ) {
1064 $gens[] = $name;
1067 sort( $gens );
1068 self::$generators = $gens;
1070 return self::$generators;
1073 public function getParamDescription() {
1074 return array(
1075 'titles' => 'A list of titles to work on',
1076 'pageids' => 'A list of page IDs to work on',
1077 'revids' => 'A list of revision IDs to work on',
1078 'generator' => array( 'Get the list of pages to work on by executing the specified query module.',
1079 'NOTE: generator parameter names must be prefixed with a \'g\', see examples' ),
1080 'redirects' => 'Automatically resolve redirects',
1081 'converttitles' => array( 'Convert titles to other variants if necessary. Only works if the wiki\'s content language supports variant conversion.',
1082 'Languages that support variant conversion include ' . implode( ', ', LanguageConverter::$languagesWithVariants ) ),
1086 public function getPossibleErrors() {
1087 return array_merge( parent::getPossibleErrors(), array(
1088 array( 'code' => 'multisource', 'info' => "Cannot use 'pageids' at the same time as 'dataSource'" ),
1089 array( 'code' => 'multisource', 'info' => "Cannot use 'revids' at the same time as 'dataSource'" ),
1090 array( 'code' => 'badgenerator', 'info' => 'Module $generatorName cannot be used as a generator' ),
1091 ) );