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;
49 private $mDbSource, $mParams;
50 private $mResolveRedirects, $mConvertTitles, $mAllowGenerator;
52 private $mAllPages = array(); // [ns][dbkey] => page_id or negative when missing
53 private $mTitles = array();
54 private $mGoodTitles = array();
55 private $mMissingTitles = array();
56 private $mInvalidTitles = array();
57 private $mMissingPageIDs = array();
58 private $mRedirectTitles = array();
59 private $mSpecialTitles = array();
60 private $mNormalizedTitles = array();
61 private $mInterwikiTitles = array();
62 private $mPendingRedirectIDs = array();
63 private $mConvertedTitles = array();
64 private $mGoodRevIDs = array();
65 private $mMissingRevIDs = array();
66 private $mFakePageId = -1;
67 private $mCacheMode = 'public';
68 private $mRequestedPageFields = array();
72 * @param $dbSource ApiBase Module implementing getDB().
73 * Allows PageSet to reuse existing db connection from the shared state like ApiQuery.
74 * @param $flags int Zero or more flags like DISABLE_GENERATORS
75 * @since 1.21 accepts $flags instead of two boolean values
77 public function __construct( ApiBase
$dbSource, $flags = 0 ) {
78 parent
::__construct( $dbSource->getMain(), $dbSource->getModuleName() );
79 $this->mDbSource
= $dbSource;
80 $this->mAllowGenerator
= ( $flags & ApiPageSet
::DISABLE_GENERATORS
) == 0;
83 $this->mParams
= $this->extractRequestParams();
84 $this->mResolveRedirects
= $this->mParams
['redirects'];
85 $this->mConvertTitles
= $this->mParams
['converttitles'];
90 * Populate the PageSet from the request parameters.
92 public function execute() {
95 $generatorName = $this->mAllowGenerator ?
$this->mParams
['generator'] : null;
96 if ( isset( $generatorName ) ) {
97 $dbSource = $this->mDbSource
;
98 $isQuery = $dbSource instanceof ApiQuery
;
100 // If the parent container of this pageset is not ApiQuery, we must create it to run generator
101 $dbSource = $this->getMain()->getModuleManager()->getModule( 'query' );
102 // Enable profiling for query module because it will be used for db sql profiling
103 $dbSource->profileIn();
105 $generator = $dbSource->getModuleManager()->getModule( $generatorName, null, true );
106 if ( $generator === null ) {
107 $this->dieUsage( 'Unknown generator=' . $generatorName, 'badgenerator' );
109 if ( !$generator instanceof ApiQueryGeneratorBase
) {
110 $this->dieUsage( "Module $generatorName cannot be used as a generator", 'badgenerator' );
112 // Create a temporary pageset to store generator's output,
113 // add any additional fields generator may need, and execute pageset to populate titles/pageids
114 $tmpPageSet = new ApiPageSet( $dbSource, ApiPageSet
::DISABLE_GENERATORS
);
115 $generator->setGeneratorMode( $tmpPageSet );
116 $this->mCacheMode
= $generator->getCacheMode( $generator->extractRequestParams() );
117 $generator->requestExtraData( $tmpPageSet );
118 $tmpPageSet->execute();
120 // populate this pageset with the generator output
122 $generator->profileIn();
123 $generator->executeGenerator( $this );
124 wfRunHooks( 'APIQueryGeneratorAfterExecute', array( &$generator, &$this ) );
125 $this->resolvePendingRedirects();
126 $generator->profileOut();
130 // If this pageset is not part of the query, we called profileIn() above
131 $dbSource->profileOut();
134 // Only one of the titles/pageids/revids is allowed at the same time
136 if ( isset( $this->mParams
['titles'] ) ) {
137 $dataSource = 'titles';
139 if ( isset( $this->mParams
['pageids'] ) ) {
140 if ( isset( $dataSource ) ) {
141 $this->dieUsage( "Cannot use 'pageids' at the same time as '$dataSource'", 'multisource' );
143 $dataSource = 'pageids';
145 if ( isset( $this->mParams
['revids'] ) ) {
146 if ( isset( $dataSource ) ) {
147 $this->dieUsage( "Cannot use 'revids' at the same time as '$dataSource'", 'multisource' );
149 $dataSource = 'revids';
151 // Populate page information with the original user input
152 switch( $dataSource ) {
154 $this->initFromTitles( $this->mParams
['titles'] );
157 $this->initFromPageIds( $this->mParams
['pageids'] );
160 if ( $this->mResolveRedirects
) {
161 $this->setWarning( 'Redirect resolution cannot be used together with the revids= parameter. ' .
162 'Any redirects the revids= point to have not been resolved.' );
164 $this->mResolveRedirects
= false;
165 $this->initFromRevIDs( $this->mParams
['revids'] );
168 // Do nothing - some queries do not need any of the data sources.
176 * Check whether this PageSet is resolving redirects
179 public function isResolvingRedirects() {
180 return $this->mResolveRedirects
;
184 * Request an additional field from the page table.
185 * Must be called before execute()
186 * @param $fieldName string Field name
188 public function requestField( $fieldName ) {
189 $this->mRequestedPageFields
[$fieldName] = null;
193 * Get the value of a custom field previously requested through
195 * @param $fieldName string Field name
196 * @return mixed Field value
198 public function getCustomField( $fieldName ) {
199 return $this->mRequestedPageFields
[$fieldName];
203 * Get the fields that have to be queried from the page table:
204 * the ones requested through requestField() and a few basic ones
206 * @return array of field names
208 public function getPageTableFields() {
209 // Ensure we get minimum required fields
210 // DON'T change this order
212 'page_namespace' => null,
213 'page_title' => null,
217 if ( $this->mResolveRedirects
) {
218 $pageFlds['page_is_redirect'] = null;
221 // only store non-default fields
222 $this->mRequestedPageFields
= array_diff_key( $this->mRequestedPageFields
, $pageFlds );
224 $pageFlds = array_merge( $pageFlds, $this->mRequestedPageFields
);
225 return array_keys( $pageFlds );
229 * Returns an array [ns][dbkey] => page_id for all requested titles.
230 * page_id is a unique negative number in case title was not found.
231 * Invalid titles will also have negative page IDs and will be in namespace 0
234 public function getAllTitlesByNamespace() {
235 return $this->mAllPages
;
239 * All Title objects provided.
240 * @return array of Title objects
242 public function getTitles() {
243 return $this->mTitles
;
247 * Returns the number of unique pages (not revisions) in the set.
250 public function getTitleCount() {
251 return count( $this->mTitles
);
255 * Title objects that were found in the database.
256 * @return array page_id (int) => Title (obj)
258 public function getGoodTitles() {
259 return $this->mGoodTitles
;
263 * Returns the number of found unique pages (not revisions) in the set.
266 public function getGoodTitleCount() {
267 return count( $this->mGoodTitles
);
271 * Title objects that were NOT found in the database.
272 * The array's index will be negative for each item
273 * @return array of Title objects
275 public function getMissingTitles() {
276 return $this->mMissingTitles
;
280 * Titles that were deemed invalid by Title::newFromText()
281 * The array's index will be unique and negative for each item
282 * @return array of strings (not Title objects)
284 public function getInvalidTitles() {
285 return $this->mInvalidTitles
;
289 * Page IDs that were not found in the database
290 * @return array of page IDs
292 public function getMissingPageIDs() {
293 return $this->mMissingPageIDs
;
297 * Get a list of redirect resolutions - maps a title to its redirect
298 * target, as an array of output-ready arrays
301 public function getRedirectTitles() {
302 return $this->mRedirectTitles
;
306 * Get a list of redirect resolutions - maps a title to its redirect
308 * @param $result ApiResult
309 * @return array of prefixed_title (string) => Title object
312 public function getRedirectTitlesAsResult( $result = null ) {
314 foreach ( $this->getRedirectTitles() as $titleStrFrom => $titleTo ) {
316 'from' => strval( $titleStrFrom ),
317 'to' => $titleTo->getPrefixedText(),
319 if ( $titleTo->getFragment() !== '' ) {
320 $r['tofragment'] = $titleTo->getFragment();
324 if ( !empty( $values ) && $result ) {
325 $result->setIndexedTagName( $values, 'r' );
331 * Get a list of title normalizations - maps a title to its normalized
333 * @return array raw_prefixed_title (string) => prefixed_title (string)
335 public function getNormalizedTitles() {
336 return $this->mNormalizedTitles
;
340 * Get a list of title normalizations - maps a title to its normalized
341 * version in the form of result array.
342 * @param $result ApiResult
343 * @return array of raw_prefixed_title (string) => prefixed_title (string)
346 public function getNormalizedTitlesAsResult( $result = null ) {
348 foreach ( $this->getNormalizedTitles() as $rawTitleStr => $titleStr ) {
350 'from' => $rawTitleStr,
354 if ( !empty( $values ) && $result ) {
355 $result->setIndexedTagName( $values, 'n' );
361 * Get a list of title conversions - maps a title to its converted
363 * @return array raw_prefixed_title (string) => prefixed_title (string)
365 public function getConvertedTitles() {
366 return $this->mConvertedTitles
;
370 * Get a list of title conversions - maps a title to its converted
371 * version as a result array.
372 * @param $result ApiResult
373 * @return array of (from, to) strings
376 public function getConvertedTitlesAsResult( $result = null ) {
378 foreach ( $this->getConvertedTitles() as $rawTitleStr => $titleStr ) {
380 'from' => $rawTitleStr,
384 if ( !empty( $values ) && $result ) {
385 $result->setIndexedTagName( $values, 'c' );
391 * Get a list of interwiki titles - maps a title to its interwiki
393 * @return array raw_prefixed_title (string) => interwiki_prefix (string)
395 public function getInterwikiTitles() {
396 return $this->mInterwikiTitles
;
400 * Get a list of interwiki titles - maps a title to its interwiki
402 * @param $result ApiResult
403 * @param $iwUrl boolean
404 * @return array raw_prefixed_title (string) => interwiki_prefix (string)
407 public function getInterwikiTitlesAsResult( $result = null, $iwUrl = false ) {
409 foreach ( $this->getInterwikiTitles() as $rawTitleStr => $interwikiStr ) {
411 'title' => $rawTitleStr,
412 'iw' => $interwikiStr,
415 $title = Title
::newFromText( $rawTitleStr );
416 $item['url'] = $title->getFullURL( '', false, PROTO_CURRENT
);
420 if ( !empty( $values ) && $result ) {
421 $result->setIndexedTagName( $values, 'i' );
427 * Get the list of revision IDs (requested with the revids= parameter)
428 * @return array revID (int) => pageID (int)
430 public function getRevisionIDs() {
431 return $this->mGoodRevIDs
;
435 * Revision IDs that were not found in the database
436 * @return array of revision IDs
438 public function getMissingRevisionIDs() {
439 return $this->mMissingRevIDs
;
443 * Revision IDs that were not found in the database as result array.
444 * @param $result ApiResult
445 * @return array of revision IDs
448 public function getMissingRevisionIDsAsResult( $result = null ) {
450 foreach ( $this->getMissingRevisionIDs() as $revid ) {
451 $values[$revid] = array(
455 if ( !empty( $values ) && $result ) {
456 $result->setIndexedTagName( $values, 'rev' );
462 * Get the list of titles with negative namespace
463 * @return array Title
465 public function getSpecialTitles() {
466 return $this->mSpecialTitles
;
470 * Returns the number of revisions (requested with revids= parameter).
471 * @return int Number of revisions.
473 public function getRevisionCount() {
474 return count( $this->getRevisionIDs() );
478 * Populate this PageSet from a list of Titles
479 * @param $titles array of Title objects
481 public function populateFromTitles( $titles ) {
483 $this->initFromTitles( $titles );
488 * Populate this PageSet from a list of page IDs
489 * @param $pageIDs array of page IDs
491 public function populateFromPageIDs( $pageIDs ) {
493 $this->initFromPageIds( $pageIDs );
498 * Populate this PageSet from a rowset returned from the database
499 * @param $db DatabaseBase object
500 * @param $queryResult ResultWrapper Query result object
502 public function populateFromQueryResult( $db, $queryResult ) {
504 $this->initFromQueryResult( $queryResult );
509 * Populate this PageSet from a list of revision IDs
510 * @param $revIDs array of revision IDs
512 public function populateFromRevisionIDs( $revIDs ) {
514 $this->initFromRevIDs( $revIDs );
519 * Extract all requested fields from the row received from the database
520 * @param $row Result row
522 public function processDbRow( $row ) {
523 // Store Title object in various data structures
524 $title = Title
::newFromRow( $row );
526 $pageId = intval( $row->page_id
);
527 $this->mAllPages
[$row->page_namespace
][$row->page_title
] = $pageId;
528 $this->mTitles
[] = $title;
530 if ( $this->mResolveRedirects
&& $row->page_is_redirect
== '1' ) {
531 $this->mPendingRedirectIDs
[$pageId] = $title;
533 $this->mGoodTitles
[$pageId] = $title;
536 foreach ( $this->mRequestedPageFields
as $fieldName => &$fieldValues ) {
537 $fieldValues[$pageId] = $row-> $fieldName;
542 * Do not use, does nothing, will be removed
545 public function finishPageSetGeneration() {
546 wfDeprecated( __METHOD__
, '1.21' );
550 * This method populates internal variables with page information
551 * based on the given array of title strings.
554 * #1 For each title, get data from `page` table
555 * #2 If page was not found in the DB, store it as missing
557 * Additionally, when resolving redirects:
558 * #3 If no more redirects left, stop.
559 * #4 For each redirect, get its target from the `redirect` table.
560 * #5 Substitute the original LinkBatch object with the new list
561 * #6 Repeat from step #1
563 * @param $titles array of Title objects or strings
565 private function initFromTitles( $titles ) {
566 // Get validated and normalized title objects
567 $linkBatch = $this->processTitlesArray( $titles );
568 if ( $linkBatch->isEmpty() ) {
572 $db = $this->getDB();
573 $set = $linkBatch->constructSet( 'page', $db );
575 // Get pageIDs data from the `page` table
576 $this->profileDBIn();
577 $res = $db->select( 'page', $this->getPageTableFields(), $set,
579 $this->profileDBOut();
581 // Hack: get the ns:titles stored in array(ns => array(titles)) format
582 $this->initFromQueryResult( $res, $linkBatch->data
, true ); // process Titles
584 // Resolve any found redirects
585 $this->resolvePendingRedirects();
589 * Does the same as initFromTitles(), but is based on page IDs instead
590 * @param $pageids array of page IDs
592 private function initFromPageIds( $pageids ) {
597 $pageids = array_map( 'intval', $pageids ); // paranoia
598 $remaining = array_flip( $pageids );
600 $pageids = self
::getPositiveIntegers( $pageids );
603 if ( !empty( $pageids ) ) {
605 'page_id' => $pageids
607 $db = $this->getDB();
609 // Get pageIDs data from the `page` table
610 $this->profileDBIn();
611 $res = $db->select( 'page', $this->getPageTableFields(), $set,
613 $this->profileDBOut();
616 $this->initFromQueryResult( $res, $remaining, false ); // process PageIDs
618 // Resolve any found redirects
619 $this->resolvePendingRedirects();
623 * Iterate through the result of the query on 'page' table,
624 * and for each row create and store title object and save any extra fields requested.
625 * @param $res ResultWrapper DB Query result
626 * @param $remaining array of either pageID or ns/title elements (optional).
627 * If given, any missing items will go to $mMissingPageIDs and $mMissingTitles
628 * @param $processTitles bool Must be provided together with $remaining.
629 * If true, treat $remaining as an array of [ns][title]
630 * If false, treat it as an array of [pageIDs]
632 private function initFromQueryResult( $res, &$remaining = null, $processTitles = null ) {
633 if ( !is_null( $remaining ) && is_null( $processTitles ) ) {
634 ApiBase
::dieDebug( __METHOD__
, 'Missing $processTitles parameter when $remaining is provided' );
637 $usernames = array();
639 foreach ( $res as $row ) {
640 $pageId = intval( $row->page_id
);
642 // Remove found page from the list of remaining items
643 if ( isset( $remaining ) ) {
644 if ( $processTitles ) {
645 unset( $remaining[$row->page_namespace
][$row->page_title
] );
647 unset( $remaining[$pageId] );
651 // Store any extra fields requested by modules
652 $this->processDbRow( $row );
654 // Need gender information
655 if ( MWNamespace
::hasGenderDistinction( $row->page_namespace
) ) {
656 $usernames[] = $row->page_title
;
661 if ( isset( $remaining ) ) {
662 // Any items left in the $remaining list are added as missing
663 if ( $processTitles ) {
664 // The remaining titles in $remaining are non-existent pages
665 foreach ( $remaining as $ns => $dbkeys ) {
666 foreach ( array_keys( $dbkeys ) as $dbkey ) {
667 $title = Title
::makeTitle( $ns, $dbkey );
668 $this->mAllPages
[$ns][$dbkey] = $this->mFakePageId
;
669 $this->mMissingTitles
[$this->mFakePageId
] = $title;
670 $this->mFakePageId
--;
671 $this->mTitles
[] = $title;
673 // need gender information
674 if ( MWNamespace
::hasGenderDistinction( $ns ) ) {
675 $usernames[] = $dbkey;
680 // The remaining pageids do not exist
681 if ( !$this->mMissingPageIDs
) {
682 $this->mMissingPageIDs
= array_keys( $remaining );
684 $this->mMissingPageIDs
= array_merge( $this->mMissingPageIDs
, array_keys( $remaining ) );
689 // Get gender information
690 $genderCache = GenderCache
::singleton();
691 $genderCache->doQuery( $usernames, __METHOD__
);
695 * Does the same as initFromTitles(), but is based on revision IDs
697 * @param $revids array of revision IDs
699 private function initFromRevIDs( $revids ) {
704 $revids = array_map( 'intval', $revids ); // paranoia
705 $db = $this->getDB();
707 $remaining = array_flip( $revids );
709 $revids = self
::getPositiveIntegers( $revids );
711 if ( !empty( $revids ) ) {
712 $tables = array( 'revision', 'page' );
713 $fields = array( 'rev_id', 'rev_page' );
714 $where = array( 'rev_id' => $revids, 'rev_page = page_id' );
716 // Get pageIDs data from the `page` table
717 $this->profileDBIn();
718 $res = $db->select( $tables, $fields, $where, __METHOD__
);
719 foreach ( $res as $row ) {
720 $revid = intval( $row->rev_id
);
721 $pageid = intval( $row->rev_page
);
722 $this->mGoodRevIDs
[$revid] = $pageid;
723 $pageids[$pageid] = '';
724 unset( $remaining[$revid] );
726 $this->profileDBOut();
729 $this->mMissingRevIDs
= array_keys( $remaining );
731 // Populate all the page information
732 $this->initFromPageIds( array_keys( $pageids ) );
736 * Resolve any redirects in the result if redirect resolution was
737 * requested. This function is called repeatedly until all redirects
738 * have been resolved.
740 private function resolvePendingRedirects() {
741 if ( $this->mResolveRedirects
) {
742 $db = $this->getDB();
743 $pageFlds = $this->getPageTableFields();
745 // Repeat until all redirects have been resolved
746 // The infinite loop is prevented by keeping all known pages in $this->mAllPages
747 while ( $this->mPendingRedirectIDs
) {
748 // Resolve redirects by querying the pagelinks table, and repeat the process
749 // Create a new linkBatch object for the next pass
750 $linkBatch = $this->getRedirectTargets();
752 if ( $linkBatch->isEmpty() ) {
756 $set = $linkBatch->constructSet( 'page', $db );
757 if ( $set === false ) {
761 // Get pageIDs data from the `page` table
762 $this->profileDBIn();
763 $res = $db->select( 'page', $pageFlds, $set, __METHOD__
);
764 $this->profileDBOut();
766 // Hack: get the ns:titles stored in array(ns => array(titles)) format
767 $this->initFromQueryResult( $res, $linkBatch->data
, true );
773 * Get the targets of the pending redirects from the database
775 * Also creates entries in the redirect table for redirects that don't
779 private function getRedirectTargets() {
780 $lb = new LinkBatch();
781 $db = $this->getDB();
783 $this->profileDBIn();
792 ), array( 'rd_from' => array_keys( $this->mPendingRedirectIDs
) ),
795 $this->profileDBOut();
796 foreach ( $res as $row ) {
797 $rdfrom = intval( $row->rd_from
);
798 $from = $this->mPendingRedirectIDs
[$rdfrom]->getPrefixedText();
799 $to = Title
::makeTitle( $row->rd_namespace
, $row->rd_title
, $row->rd_fragment
, $row->rd_interwiki
);
800 unset( $this->mPendingRedirectIDs
[$rdfrom] );
801 if ( !isset( $this->mAllPages
[$row->rd_namespace
][$row->rd_title
] ) ) {
802 $lb->add( $row->rd_namespace
, $row->rd_title
);
804 $this->mRedirectTitles
[$from] = $to;
807 if ( $this->mPendingRedirectIDs
) {
808 // We found pages that aren't in the redirect table
810 foreach ( $this->mPendingRedirectIDs
as $id => $title ) {
811 $page = WikiPage
::factory( $title );
812 $rt = $page->insertRedirect();
814 // What the hell. Let's just ignore this
818 $this->mRedirectTitles
[$title->getPrefixedText()] = $rt;
819 unset( $this->mPendingRedirectIDs
[$id] );
826 * Get the cache mode for the data generated by this module.
827 * All PageSet users should take into account whether this returns a more-restrictive
828 * cache mode than the using module itself. For possible return values and other
829 * details about cache modes, see ApiMain::setCacheMode()
831 * Public caching will only be allowed if *all* the modules that supply
832 * data for a given request return a cache mode of public.
838 public function getCacheMode( $params = null ) {
839 return $this->mCacheMode
;
843 * Given an array of title strings, convert them into Title objects.
844 * Alternativelly, an array of Title objects may be given.
845 * This method validates access rights for the title,
846 * and appends normalization values to the output.
848 * @param $titles array of Title objects or strings
851 private function processTitlesArray( $titles ) {
852 $genderCache = GenderCache
::singleton();
853 $genderCache->doTitlesArray( $titles, __METHOD__
);
855 $linkBatch = new LinkBatch();
857 foreach ( $titles as $title ) {
858 $titleObj = is_string( $title ) ? Title
::newFromText( $title ) : $title;
860 // Handle invalid titles gracefully
861 $this->mAllpages
[0][$title] = $this->mFakePageId
;
862 $this->mInvalidTitles
[$this->mFakePageId
] = $title;
863 $this->mFakePageId
--;
864 continue; // There's nothing else we can do
866 $unconvertedTitle = $titleObj->getPrefixedText();
867 $titleWasConverted = false;
868 $iw = $titleObj->getInterwiki();
869 if ( strval( $iw ) !== '' ) {
870 // This title is an interwiki link.
871 $this->mInterwikiTitles
[$titleObj->getPrefixedText()] = $iw;
875 if ( $this->mConvertTitles
&&
876 count( $wgContLang->getVariants() ) > 1 &&
877 !$titleObj->exists() ) {
878 // Language::findVariantLink will modify titleText and titleObj into
879 // the canonical variant if possible
880 $titleText = is_string( $title ) ?
$title : $titleObj->getPrefixedText();
881 $wgContLang->findVariantLink( $titleText, $titleObj );
882 $titleWasConverted = $unconvertedTitle !== $titleObj->getPrefixedText();
885 if ( $titleObj->getNamespace() < 0 ) {
886 // Handle Special and Media pages
887 $titleObj = $titleObj->fixSpecialName();
888 $this->mSpecialTitles
[$this->mFakePageId
] = $titleObj;
889 $this->mFakePageId
--;
892 $linkBatch->addObj( $titleObj );
896 // Make sure we remember the original title that was
897 // given to us. This way the caller can correlate new
898 // titles with the originally requested when e.g. the
899 // namespace is localized or the capitalization is
901 if ( $titleWasConverted ) {
902 $this->mConvertedTitles
[$unconvertedTitle] = $titleObj->getPrefixedText();
903 // In this case the page can't be Special.
904 if ( is_string( $title ) && $title !== $unconvertedTitle ) {
905 $this->mNormalizedTitles
[$title] = $unconvertedTitle;
907 } elseif ( is_string( $title ) && $title !== $titleObj->getPrefixedText() ) {
908 $this->mNormalizedTitles
[$title] = $titleObj->getPrefixedText();
916 * Get the database connection (read-only)
917 * @return DatabaseBase
919 protected function getDB() {
920 return $this->mDbSource
->getDB();
924 * Returns the input array of integers with all values < 0 removed
926 * @param $array array
929 private static function getPositiveIntegers( $array ) {
930 // bug 25734 API: possible issue with revids validation
931 // It seems with a load of revision rows, MySQL gets upset
932 // Remove any < 0 integers, as they can't be valid
933 foreach ( $array as $i => $int ) {
942 public function getAllowedParams( $flags = 0 ) {
945 ApiBase
::PARAM_ISMULTI
=> true
948 ApiBase
::PARAM_TYPE
=> 'integer',
949 ApiBase
::PARAM_ISMULTI
=> true
952 ApiBase
::PARAM_TYPE
=> 'integer',
953 ApiBase
::PARAM_ISMULTI
=> true
955 'redirects' => false,
956 'converttitles' => false,
958 if ( $this->mAllowGenerator
) {
959 if ( $flags & ApiBase
::GET_VALUES_FOR_HELP
) {
960 $result['generator'] = array(
961 ApiBase
::PARAM_TYPE
=> $this->getGenerators()
964 $result['generator'] = null;
970 private static $generators = null;
973 * Get an array of all available generators
976 private function getGenerators() {
977 if ( self
::$generators === null ) {
978 $query = $this->mDbSource
;
979 if ( !( $query instanceof ApiQuery
) ) {
980 // If the parent container of this pageset is not ApiQuery,
981 // we must create it to get module manager
982 $query = $this->getMain()->getModuleManager()->getModule( 'query' );
985 $mgr = $query->getModuleManager();
986 foreach ( $mgr->getNamesWithClasses() as $name => $class ) {
987 if ( is_subclass_of( $class, 'ApiQueryGeneratorBase' ) ) {
992 self
::$generators = $gens;
994 return self
::$generators;
997 public function getParamDescription() {
999 'titles' => 'A list of titles to work on',
1000 'pageids' => 'A list of page IDs to work on',
1001 'revids' => 'A list of revision IDs to work on',
1002 'generator' => array( 'Get the list of pages to work on by executing the specified query module.',
1003 'NOTE: generator parameter names must be prefixed with a \'g\', see examples' ),
1004 'redirects' => 'Automatically resolve redirects',
1005 'converttitles' => array( 'Convert titles to other variants if necessary. Only works if the wiki\'s content language supports variant conversion.',
1006 'Languages that support variant conversion include ' . implode( ', ', LanguageConverter
::$languagesWithVariants ) ),
1010 public function getPossibleErrors() {
1011 return array_merge( parent
::getPossibleErrors(), array(
1012 array( 'code' => 'multisource', 'info' => "Cannot use 'pageids' at the same time as 'dataSource'" ),
1013 array( 'code' => 'multisource', 'info' => "Cannot use 'revids' at the same time as 'dataSource'" ),
1014 array( 'code' => 'badgenerator', 'info' => 'Module $generatorName cannot be used as a generator' ),