Fixed spacing in actions/cache/filebackend/filerepo/job folder
[mediawiki.git] / includes / api / ApiPageSet.php
blob0645edbed81eed108eb4d46ceee2e49d9385f95f
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 $this->resolvePendingRedirects();
156 } else {
157 // Prevent warnings from being reported on these parameters
158 $main = $this->getMain();
159 foreach ( $generator->extractRequestParams() as $paramName => $param ) {
160 $main->getVal( $generator->encodeParamName( $paramName ) );
163 $generator->profileOut();
164 $this->profileIn();
166 if ( !$isQuery ) {
167 // If this pageset is not part of the query, we called profileIn() above
168 $dbSource->profileOut();
170 } else {
171 // Only one of the titles/pageids/revids is allowed at the same time
172 $dataSource = null;
173 if ( isset( $this->mParams['titles'] ) ) {
174 $dataSource = 'titles';
176 if ( isset( $this->mParams['pageids'] ) ) {
177 if ( isset( $dataSource ) ) {
178 $this->dieUsage( "Cannot use 'pageids' at the same time as '$dataSource'", 'multisource' );
180 $dataSource = 'pageids';
182 if ( isset( $this->mParams['revids'] ) ) {
183 if ( isset( $dataSource ) ) {
184 $this->dieUsage( "Cannot use 'revids' at the same time as '$dataSource'", 'multisource' );
186 $dataSource = 'revids';
189 if ( !$isDryRun ) {
190 // Populate page information with the original user input
191 switch( $dataSource ) {
192 case 'titles':
193 $this->initFromTitles( $this->mParams['titles'] );
194 break;
195 case 'pageids':
196 $this->initFromPageIds( $this->mParams['pageids'] );
197 break;
198 case 'revids':
199 if ( $this->mResolveRedirects ) {
200 $this->setWarning( 'Redirect resolution cannot be used together with the revids= parameter. ' .
201 'Any redirects the revids= point to have not been resolved.' );
203 $this->mResolveRedirects = false;
204 $this->initFromRevIDs( $this->mParams['revids'] );
205 break;
206 default:
207 // Do nothing - some queries do not need any of the data sources.
208 break;
212 $this->profileOut();
216 * Check whether this PageSet is resolving redirects
217 * @return bool
219 public function isResolvingRedirects() {
220 return $this->mResolveRedirects;
224 * Return the parameter name that is the source of data for this PageSet
226 * If multiple source parameters are specified (e.g. titles and pageids),
227 * one will be named arbitrarily.
229 * @return string|null
231 public function getDataSource() {
232 if ( $this->mAllowGenerator && isset( $this->mParams['generator'] ) ) {
233 return 'generator';
235 if ( isset( $this->mParams['titles'] ) ) {
236 return 'titles';
238 if ( isset( $this->mParams['pageids'] ) ) {
239 return 'pageids';
241 if ( isset( $this->mParams['revids'] ) ) {
242 return 'revids';
244 return null;
248 * Request an additional field from the page table.
249 * Must be called before execute()
250 * @param string $fieldName Field name
252 public function requestField( $fieldName ) {
253 $this->mRequestedPageFields[$fieldName] = null;
257 * Get the value of a custom field previously requested through
258 * requestField()
259 * @param string $fieldName Field name
260 * @return mixed Field value
262 public function getCustomField( $fieldName ) {
263 return $this->mRequestedPageFields[$fieldName];
267 * Get the fields that have to be queried from the page table:
268 * the ones requested through requestField() and a few basic ones
269 * we always need
270 * @return array of field names
272 public function getPageTableFields() {
273 // Ensure we get minimum required fields
274 // DON'T change this order
275 $pageFlds = array(
276 'page_namespace' => null,
277 'page_title' => null,
278 'page_id' => null,
281 if ( $this->mResolveRedirects ) {
282 $pageFlds['page_is_redirect'] = null;
285 // only store non-default fields
286 $this->mRequestedPageFields = array_diff_key( $this->mRequestedPageFields, $pageFlds );
288 $pageFlds = array_merge( $pageFlds, $this->mRequestedPageFields );
289 return array_keys( $pageFlds );
293 * Returns an array [ns][dbkey] => page_id for all requested titles.
294 * page_id is a unique negative number in case title was not found.
295 * Invalid titles will also have negative page IDs and will be in namespace 0
296 * @return array
298 public function getAllTitlesByNamespace() {
299 return $this->mAllPages;
303 * All Title objects provided.
304 * @return array of Title objects
306 public function getTitles() {
307 return $this->mTitles;
311 * Returns the number of unique pages (not revisions) in the set.
312 * @return int
314 public function getTitleCount() {
315 return count( $this->mTitles );
319 * Title objects that were found in the database.
320 * @return array page_id (int) => Title (obj)
322 public function getGoodTitles() {
323 return $this->mGoodTitles;
327 * Returns the number of found unique pages (not revisions) in the set.
328 * @return int
330 public function getGoodTitleCount() {
331 return count( $this->mGoodTitles );
335 * Title objects that were NOT found in the database.
336 * The array's index will be negative for each item
337 * @return array of Title objects
339 public function getMissingTitles() {
340 return $this->mMissingTitles;
344 * Titles that were deemed invalid by Title::newFromText()
345 * The array's index will be unique and negative for each item
346 * @return array of strings (not Title objects)
348 public function getInvalidTitles() {
349 return $this->mInvalidTitles;
353 * Page IDs that were not found in the database
354 * @return array of page IDs
356 public function getMissingPageIDs() {
357 return $this->mMissingPageIDs;
361 * Get a list of redirect resolutions - maps a title to its redirect
362 * target, as an array of output-ready arrays
363 * @return array
365 public function getRedirectTitles() {
366 return $this->mRedirectTitles;
370 * Get a list of redirect resolutions - maps a title to its redirect
371 * target.
372 * @param $result ApiResult
373 * @return array of prefixed_title (string) => Title object
374 * @since 1.21
376 public function getRedirectTitlesAsResult( $result = null ) {
377 $values = array();
378 foreach ( $this->getRedirectTitles() as $titleStrFrom => $titleTo ) {
379 $r = array(
380 'from' => strval( $titleStrFrom ),
381 'to' => $titleTo->getPrefixedText(),
383 if ( $titleTo->getFragment() !== '' ) {
384 $r['tofragment'] = $titleTo->getFragment();
386 $values[] = $r;
388 if ( !empty( $values ) && $result ) {
389 $result->setIndexedTagName( $values, 'r' );
391 return $values;
395 * Get a list of title normalizations - maps a title to its normalized
396 * version.
397 * @return array raw_prefixed_title (string) => prefixed_title (string)
399 public function getNormalizedTitles() {
400 return $this->mNormalizedTitles;
404 * Get a list of title normalizations - maps a title to its normalized
405 * version in the form of result array.
406 * @param $result ApiResult
407 * @return array of raw_prefixed_title (string) => prefixed_title (string)
408 * @since 1.21
410 public function getNormalizedTitlesAsResult( $result = null ) {
411 $values = array();
412 foreach ( $this->getNormalizedTitles() as $rawTitleStr => $titleStr ) {
413 $values[] = array(
414 'from' => $rawTitleStr,
415 'to' => $titleStr
418 if ( !empty( $values ) && $result ) {
419 $result->setIndexedTagName( $values, 'n' );
421 return $values;
425 * Get a list of title conversions - maps a title to its converted
426 * version.
427 * @return array raw_prefixed_title (string) => prefixed_title (string)
429 public function getConvertedTitles() {
430 return $this->mConvertedTitles;
434 * Get a list of title conversions - maps a title to its converted
435 * version as a result array.
436 * @param $result ApiResult
437 * @return array of (from, to) strings
438 * @since 1.21
440 public function getConvertedTitlesAsResult( $result = null ) {
441 $values = array();
442 foreach ( $this->getConvertedTitles() as $rawTitleStr => $titleStr ) {
443 $values[] = array(
444 'from' => $rawTitleStr,
445 'to' => $titleStr
448 if ( !empty( $values ) && $result ) {
449 $result->setIndexedTagName( $values, 'c' );
451 return $values;
455 * Get a list of interwiki titles - maps a title to its interwiki
456 * prefix.
457 * @return array raw_prefixed_title (string) => interwiki_prefix (string)
459 public function getInterwikiTitles() {
460 return $this->mInterwikiTitles;
464 * Get a list of interwiki titles - maps a title to its interwiki
465 * prefix as result.
466 * @param $result ApiResult
467 * @param $iwUrl boolean
468 * @return array raw_prefixed_title (string) => interwiki_prefix (string)
469 * @since 1.21
471 public function getInterwikiTitlesAsResult( $result = null, $iwUrl = false ) {
472 $values = array();
473 foreach ( $this->getInterwikiTitles() as $rawTitleStr => $interwikiStr ) {
474 $item = array(
475 'title' => $rawTitleStr,
476 'iw' => $interwikiStr,
478 if ( $iwUrl ) {
479 $title = Title::newFromText( $rawTitleStr );
480 $item['url'] = $title->getFullURL( '', false, PROTO_CURRENT );
482 $values[] = $item;
484 if ( !empty( $values ) && $result ) {
485 $result->setIndexedTagName( $values, 'i' );
487 return $values;
491 * Get the list of revision IDs (requested with the revids= parameter)
492 * @return array revID (int) => pageID (int)
494 public function getRevisionIDs() {
495 return $this->mGoodRevIDs;
499 * Revision IDs that were not found in the database
500 * @return array of revision IDs
502 public function getMissingRevisionIDs() {
503 return $this->mMissingRevIDs;
507 * Revision IDs that were not found in the database as result array.
508 * @param $result ApiResult
509 * @return array of revision IDs
510 * @since 1.21
512 public function getMissingRevisionIDsAsResult( $result = null ) {
513 $values = array();
514 foreach ( $this->getMissingRevisionIDs() as $revid ) {
515 $values[$revid] = array(
516 'revid' => $revid
519 if ( !empty( $values ) && $result ) {
520 $result->setIndexedTagName( $values, 'rev' );
522 return $values;
526 * Get the list of titles with negative namespace
527 * @return array Title
529 public function getSpecialTitles() {
530 return $this->mSpecialTitles;
534 * Returns the number of revisions (requested with revids= parameter).
535 * @return int Number of revisions.
537 public function getRevisionCount() {
538 return count( $this->getRevisionIDs() );
542 * Populate this PageSet from a list of Titles
543 * @param array $titles of Title objects
545 public function populateFromTitles( $titles ) {
546 $this->profileIn();
547 $this->initFromTitles( $titles );
548 $this->profileOut();
552 * Populate this PageSet from a list of page IDs
553 * @param array $pageIDs of page IDs
555 public function populateFromPageIDs( $pageIDs ) {
556 $this->profileIn();
557 $this->initFromPageIds( $pageIDs );
558 $this->profileOut();
562 * Populate this PageSet from a rowset returned from the database
563 * @param $db DatabaseBase object
564 * @param $queryResult ResultWrapper Query result object
566 public function populateFromQueryResult( $db, $queryResult ) {
567 $this->profileIn();
568 $this->initFromQueryResult( $queryResult );
569 $this->profileOut();
573 * Populate this PageSet from a list of revision IDs
574 * @param array $revIDs of revision IDs
576 public function populateFromRevisionIDs( $revIDs ) {
577 $this->profileIn();
578 $this->initFromRevIDs( $revIDs );
579 $this->profileOut();
583 * Extract all requested fields from the row received from the database
584 * @param $row Result row
586 public function processDbRow( $row ) {
587 // Store Title object in various data structures
588 $title = Title::newFromRow( $row );
590 $pageId = intval( $row->page_id );
591 $this->mAllPages[$row->page_namespace][$row->page_title] = $pageId;
592 $this->mTitles[] = $title;
594 if ( $this->mResolveRedirects && $row->page_is_redirect == '1' ) {
595 $this->mPendingRedirectIDs[$pageId] = $title;
596 } else {
597 $this->mGoodTitles[$pageId] = $title;
600 foreach ( $this->mRequestedPageFields as $fieldName => &$fieldValues ) {
601 $fieldValues[$pageId] = $row->$fieldName;
606 * Do not use, does nothing, will be removed
607 * @deprecated 1.21
609 public function finishPageSetGeneration() {
610 wfDeprecated( __METHOD__, '1.21' );
614 * This method populates internal variables with page information
615 * based on the given array of title strings.
617 * Steps:
618 * #1 For each title, get data from `page` table
619 * #2 If page was not found in the DB, store it as missing
621 * Additionally, when resolving redirects:
622 * #3 If no more redirects left, stop.
623 * #4 For each redirect, get its target from the `redirect` table.
624 * #5 Substitute the original LinkBatch object with the new list
625 * #6 Repeat from step #1
627 * @param array $titles of Title objects or strings
629 private function initFromTitles( $titles ) {
630 // Get validated and normalized title objects
631 $linkBatch = $this->processTitlesArray( $titles );
632 if ( $linkBatch->isEmpty() ) {
633 return;
636 $db = $this->getDB();
637 $set = $linkBatch->constructSet( 'page', $db );
639 // Get pageIDs data from the `page` table
640 $this->profileDBIn();
641 $res = $db->select( 'page', $this->getPageTableFields(), $set,
642 __METHOD__ );
643 $this->profileDBOut();
645 // Hack: get the ns:titles stored in array(ns => array(titles)) format
646 $this->initFromQueryResult( $res, $linkBatch->data, true ); // process Titles
648 // Resolve any found redirects
649 $this->resolvePendingRedirects();
653 * Does the same as initFromTitles(), but is based on page IDs instead
654 * @param array $pageids of page IDs
656 private function initFromPageIds( $pageids ) {
657 if ( !$pageids ) {
658 return;
661 $pageids = array_map( 'intval', $pageids ); // paranoia
662 $remaining = array_flip( $pageids );
664 $pageids = self::getPositiveIntegers( $pageids );
666 $res = null;
667 if ( !empty( $pageids ) ) {
668 $set = array(
669 'page_id' => $pageids
671 $db = $this->getDB();
673 // Get pageIDs data from the `page` table
674 $this->profileDBIn();
675 $res = $db->select( 'page', $this->getPageTableFields(), $set,
676 __METHOD__ );
677 $this->profileDBOut();
680 $this->initFromQueryResult( $res, $remaining, false ); // process PageIDs
682 // Resolve any found redirects
683 $this->resolvePendingRedirects();
687 * Iterate through the result of the query on 'page' table,
688 * and for each row create and store title object and save any extra fields requested.
689 * @param $res ResultWrapper DB Query result
690 * @param array $remaining of either pageID or ns/title elements (optional).
691 * If given, any missing items will go to $mMissingPageIDs and $mMissingTitles
692 * @param bool $processTitles Must be provided together with $remaining.
693 * If true, treat $remaining as an array of [ns][title]
694 * If false, treat it as an array of [pageIDs]
696 private function initFromQueryResult( $res, &$remaining = null, $processTitles = null ) {
697 if ( !is_null( $remaining ) && is_null( $processTitles ) ) {
698 ApiBase::dieDebug( __METHOD__, 'Missing $processTitles parameter when $remaining is provided' );
701 $usernames = array();
702 if ( $res ) {
703 foreach ( $res as $row ) {
704 $pageId = intval( $row->page_id );
706 // Remove found page from the list of remaining items
707 if ( isset( $remaining ) ) {
708 if ( $processTitles ) {
709 unset( $remaining[$row->page_namespace][$row->page_title] );
710 } else {
711 unset( $remaining[$pageId] );
715 // Store any extra fields requested by modules
716 $this->processDbRow( $row );
718 // Need gender information
719 if ( MWNamespace::hasGenderDistinction( $row->page_namespace ) ) {
720 $usernames[] = $row->page_title;
725 if ( isset( $remaining ) ) {
726 // Any items left in the $remaining list are added as missing
727 if ( $processTitles ) {
728 // The remaining titles in $remaining are non-existent pages
729 foreach ( $remaining as $ns => $dbkeys ) {
730 foreach ( array_keys( $dbkeys ) as $dbkey ) {
731 $title = Title::makeTitle( $ns, $dbkey );
732 $this->mAllPages[$ns][$dbkey] = $this->mFakePageId;
733 $this->mMissingTitles[$this->mFakePageId] = $title;
734 $this->mFakePageId--;
735 $this->mTitles[] = $title;
737 // need gender information
738 if ( MWNamespace::hasGenderDistinction( $ns ) ) {
739 $usernames[] = $dbkey;
743 } else {
744 // The remaining pageids do not exist
745 if ( !$this->mMissingPageIDs ) {
746 $this->mMissingPageIDs = array_keys( $remaining );
747 } else {
748 $this->mMissingPageIDs = array_merge( $this->mMissingPageIDs, array_keys( $remaining ) );
753 // Get gender information
754 $genderCache = GenderCache::singleton();
755 $genderCache->doQuery( $usernames, __METHOD__ );
759 * Does the same as initFromTitles(), but is based on revision IDs
760 * instead
761 * @param array $revids of revision IDs
763 private function initFromRevIDs( $revids ) {
764 if ( !$revids ) {
765 return;
768 $revids = array_map( 'intval', $revids ); // paranoia
769 $db = $this->getDB();
770 $pageids = array();
771 $remaining = array_flip( $revids );
773 $revids = self::getPositiveIntegers( $revids );
775 if ( !empty( $revids ) ) {
776 $tables = array( 'revision', 'page' );
777 $fields = array( 'rev_id', 'rev_page' );
778 $where = array( 'rev_id' => $revids, 'rev_page = page_id' );
780 // Get pageIDs data from the `page` table
781 $this->profileDBIn();
782 $res = $db->select( $tables, $fields, $where, __METHOD__ );
783 foreach ( $res as $row ) {
784 $revid = intval( $row->rev_id );
785 $pageid = intval( $row->rev_page );
786 $this->mGoodRevIDs[$revid] = $pageid;
787 $pageids[$pageid] = '';
788 unset( $remaining[$revid] );
790 $this->profileDBOut();
793 $this->mMissingRevIDs = array_keys( $remaining );
795 // Populate all the page information
796 $this->initFromPageIds( array_keys( $pageids ) );
800 * Resolve any redirects in the result if redirect resolution was
801 * requested. This function is called repeatedly until all redirects
802 * have been resolved.
804 private function resolvePendingRedirects() {
805 if ( $this->mResolveRedirects ) {
806 $db = $this->getDB();
807 $pageFlds = $this->getPageTableFields();
809 // Repeat until all redirects have been resolved
810 // The infinite loop is prevented by keeping all known pages in $this->mAllPages
811 while ( $this->mPendingRedirectIDs ) {
812 // Resolve redirects by querying the pagelinks table, and repeat the process
813 // Create a new linkBatch object for the next pass
814 $linkBatch = $this->getRedirectTargets();
816 if ( $linkBatch->isEmpty() ) {
817 break;
820 $set = $linkBatch->constructSet( 'page', $db );
821 if ( $set === false ) {
822 break;
825 // Get pageIDs data from the `page` table
826 $this->profileDBIn();
827 $res = $db->select( 'page', $pageFlds, $set, __METHOD__ );
828 $this->profileDBOut();
830 // Hack: get the ns:titles stored in array(ns => array(titles)) format
831 $this->initFromQueryResult( $res, $linkBatch->data, true );
837 * Get the targets of the pending redirects from the database
839 * Also creates entries in the redirect table for redirects that don't
840 * have one.
841 * @return LinkBatch
843 private function getRedirectTargets() {
844 $lb = new LinkBatch();
845 $db = $this->getDB();
847 $this->profileDBIn();
848 $res = $db->select(
849 'redirect',
850 array(
851 'rd_from',
852 'rd_namespace',
853 'rd_fragment',
854 'rd_interwiki',
855 'rd_title'
856 ), array( 'rd_from' => array_keys( $this->mPendingRedirectIDs ) ),
857 __METHOD__
859 $this->profileDBOut();
860 foreach ( $res as $row ) {
861 $rdfrom = intval( $row->rd_from );
862 $from = $this->mPendingRedirectIDs[$rdfrom]->getPrefixedText();
863 $to = Title::makeTitle( $row->rd_namespace, $row->rd_title, $row->rd_fragment, $row->rd_interwiki );
864 unset( $this->mPendingRedirectIDs[$rdfrom] );
865 if ( !isset( $this->mAllPages[$row->rd_namespace][$row->rd_title] ) ) {
866 $lb->add( $row->rd_namespace, $row->rd_title );
868 $this->mRedirectTitles[$from] = $to;
871 if ( $this->mPendingRedirectIDs ) {
872 // We found pages that aren't in the redirect table
873 // Add them
874 foreach ( $this->mPendingRedirectIDs as $id => $title ) {
875 $page = WikiPage::factory( $title );
876 $rt = $page->insertRedirect();
877 if ( !$rt ) {
878 // What the hell. Let's just ignore this
879 continue;
881 $lb->addObj( $rt );
882 $this->mRedirectTitles[$title->getPrefixedText()] = $rt;
883 unset( $this->mPendingRedirectIDs[$id] );
886 return $lb;
890 * Get the cache mode for the data generated by this module.
891 * All PageSet users should take into account whether this returns a more-restrictive
892 * cache mode than the using module itself. For possible return values and other
893 * details about cache modes, see ApiMain::setCacheMode()
895 * Public caching will only be allowed if *all* the modules that supply
896 * data for a given request return a cache mode of public.
898 * @param $params
899 * @return string
900 * @since 1.21
902 public function getCacheMode( $params = null ) {
903 return $this->mCacheMode;
907 * Given an array of title strings, convert them into Title objects.
908 * Alternatively, an array of Title objects may be given.
909 * This method validates access rights for the title,
910 * and appends normalization values to the output.
912 * @param array $titles of Title objects or strings
913 * @return LinkBatch
915 private function processTitlesArray( $titles ) {
916 $usernames = array();
917 $linkBatch = new LinkBatch();
919 foreach ( $titles as $title ) {
920 if ( is_string( $title ) ) {
921 $titleObj = Title::newFromText( $title, $this->mDefaultNamespace );
922 } else {
923 $titleObj = $title;
925 if ( !$titleObj ) {
926 // Handle invalid titles gracefully
927 $this->mAllPages[0][$title] = $this->mFakePageId;
928 $this->mInvalidTitles[$this->mFakePageId] = $title;
929 $this->mFakePageId--;
930 continue; // There's nothing else we can do
932 $unconvertedTitle = $titleObj->getPrefixedText();
933 $titleWasConverted = false;
934 if ( $titleObj->isExternal() ) {
935 // This title is an interwiki link.
936 $this->mInterwikiTitles[$unconvertedTitle] = $titleObj->getInterwiki();
937 } else {
938 // Variants checking
939 global $wgContLang;
940 if ( $this->mConvertTitles &&
941 count( $wgContLang->getVariants() ) > 1 &&
942 !$titleObj->exists() ) {
943 // Language::findVariantLink will modify titleText and titleObj into
944 // the canonical variant if possible
945 $titleText = is_string( $title ) ? $title : $titleObj->getPrefixedText();
946 $wgContLang->findVariantLink( $titleText, $titleObj );
947 $titleWasConverted = $unconvertedTitle !== $titleObj->getPrefixedText();
950 if ( $titleObj->getNamespace() < 0 ) {
951 // Handle Special and Media pages
952 $titleObj = $titleObj->fixSpecialName();
953 $this->mSpecialTitles[$this->mFakePageId] = $titleObj;
954 $this->mFakePageId--;
955 } else {
956 // Regular page
957 $linkBatch->addObj( $titleObj );
961 // Make sure we remember the original title that was
962 // given to us. This way the caller can correlate new
963 // titles with the originally requested when e.g. the
964 // namespace is localized or the capitalization is
965 // different
966 if ( $titleWasConverted ) {
967 $this->mConvertedTitles[$unconvertedTitle] = $titleObj->getPrefixedText();
968 // In this case the page can't be Special.
969 if ( is_string( $title ) && $title !== $unconvertedTitle ) {
970 $this->mNormalizedTitles[$title] = $unconvertedTitle;
972 } elseif ( is_string( $title ) && $title !== $titleObj->getPrefixedText() ) {
973 $this->mNormalizedTitles[$title] = $titleObj->getPrefixedText();
976 // Need gender information
977 if ( MWNamespace::hasGenderDistinction( $titleObj->getNamespace() ) ) {
978 $usernames[] = $titleObj->getText();
981 // Get gender information
982 $genderCache = GenderCache::singleton();
983 $genderCache->doQuery( $usernames, __METHOD__ );
985 return $linkBatch;
989 * Get the database connection (read-only)
990 * @return DatabaseBase
992 protected function getDB() {
993 return $this->mDbSource->getDB();
997 * Returns the input array of integers with all values < 0 removed
999 * @param $array array
1000 * @return array
1002 private static function getPositiveIntegers( $array ) {
1003 // bug 25734 API: possible issue with revids validation
1004 // It seems with a load of revision rows, MySQL gets upset
1005 // Remove any < 0 integers, as they can't be valid
1006 foreach ( $array as $i => $int ) {
1007 if ( $int < 0 ) {
1008 unset( $array[$i] );
1012 return $array;
1015 public function getAllowedParams( $flags = 0 ) {
1016 $result = array(
1017 'titles' => array(
1018 ApiBase::PARAM_ISMULTI => true
1020 'pageids' => array(
1021 ApiBase::PARAM_TYPE => 'integer',
1022 ApiBase::PARAM_ISMULTI => true
1024 'revids' => array(
1025 ApiBase::PARAM_TYPE => 'integer',
1026 ApiBase::PARAM_ISMULTI => true
1028 'redirects' => false,
1029 'converttitles' => false,
1031 if ( $this->mAllowGenerator ) {
1032 if ( $flags & ApiBase::GET_VALUES_FOR_HELP ) {
1033 $result['generator'] = array(
1034 ApiBase::PARAM_TYPE => $this->getGenerators()
1036 } else {
1037 $result['generator'] = null;
1040 return $result;
1043 private static $generators = null;
1046 * Get an array of all available generators
1047 * @return array
1049 private function getGenerators() {
1050 if ( self::$generators === null ) {
1051 $query = $this->mDbSource;
1052 if ( !( $query instanceof ApiQuery ) ) {
1053 // If the parent container of this pageset is not ApiQuery,
1054 // we must create it to get module manager
1055 $query = $this->getMain()->getModuleManager()->getModule( 'query' );
1057 $gens = array();
1058 $mgr = $query->getModuleManager();
1059 foreach ( $mgr->getNamesWithClasses() as $name => $class ) {
1060 if ( is_subclass_of( $class, 'ApiQueryGeneratorBase' ) ) {
1061 $gens[] = $name;
1064 sort( $gens );
1065 self::$generators = $gens;
1067 return self::$generators;
1070 public function getParamDescription() {
1071 return array(
1072 'titles' => 'A list of titles to work on',
1073 'pageids' => 'A list of page IDs to work on',
1074 'revids' => 'A list of revision IDs to work on',
1075 'generator' => array( 'Get the list of pages to work on by executing the specified query module.',
1076 'NOTE: generator parameter names must be prefixed with a \'g\', see examples' ),
1077 'redirects' => 'Automatically resolve redirects',
1078 'converttitles' => array( 'Convert titles to other variants if necessary. Only works if the wiki\'s content language supports variant conversion.',
1079 'Languages that support variant conversion include ' . implode( ', ', LanguageConverter::$languagesWithVariants ) ),
1083 public function getPossibleErrors() {
1084 return array_merge( parent::getPossibleErrors(), array(
1085 array( 'code' => 'multisource', 'info' => "Cannot use 'pageids' at the same time as 'dataSource'" ),
1086 array( 'code' => 'multisource', 'info' => "Cannot use 'revids' at the same time as 'dataSource'" ),
1087 array( 'code' => 'badgenerator', 'info' => 'Module $generatorName cannot be used as a generator' ),
1088 ) );