e95e680b0359f94c5d72cc3414a61b3b46dbf9f6
5 * Created on Sep 24, 2006
7 * Copyright © 2006, 2013 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * http://www.gnu.org/copyleft/gpl.html
28 * This class contains a list of pages that the client has requested.
29 * Initially, when the client passes in titles=, pageids=, or revisions=
30 * parameter, an instance of the ApiPageSet class will normalize titles,
31 * determine if the pages/revisions exist, and prefetch any additional page
34 * When a generator is used, the result of the generator will become the input
35 * for the second instance of this class, and all subsequent actions will use
36 * the second instance for all their work.
39 * @since 1.21 derives from ApiBase instead of ApiQueryBase
41 class ApiPageSet
extends ApiBase
{
44 * Constructor flag: The new instance of ApiPageSet will ignore the 'generator=' parameter
47 const DISABLE_GENERATORS
= 1;
51 private $mResolveRedirects;
52 private $mConvertTitles;
53 private $mAllowGenerator;
55 private $mAllPages = array(); // [ns][dbkey] => page_id or negative when missing
56 private $mTitles = array();
57 private $mGoodTitles = array();
58 private $mMissingTitles = array();
59 private $mInvalidTitles = array();
60 private $mMissingPageIDs = array();
61 private $mRedirectTitles = array();
62 private $mSpecialTitles = array();
63 private $mNormalizedTitles = array();
64 private $mInterwikiTitles = array();
65 private $mPendingRedirectIDs = array();
66 private $mConvertedTitles = array();
67 private $mGoodRevIDs = array();
68 private $mMissingRevIDs = array();
69 private $mFakePageId = -1;
70 private $mCacheMode = 'public';
71 private $mRequestedPageFields = array();
75 private $mDefaultNamespace = NS_MAIN
;
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;
92 $this->mParams
= $this->extractRequestParams();
93 $this->mResolveRedirects
= $this->mParams
['redirects'];
94 $this->mConvertTitles
= $this->mParams
['converttitles'];
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
116 * relevant parameters as used
118 private function executeInternal( $isDryRun ) {
121 $generatorName = $this->mAllowGenerator ?
$this->mParams
['generator'] : null;
122 if ( isset( $generatorName ) ) {
123 $dbSource = $this->mDbSource
;
124 $isQuery = $dbSource instanceof ApiQuery
;
126 // If the parent container of this pageset is not ApiQuery, we must create it to run generator
127 $dbSource = $this->getMain()->getModuleManager()->getModule( 'query' );
128 // Enable profiling for query module because it will be used for db sql profiling
129 $dbSource->profileIn();
131 $generator = $dbSource->getModuleManager()->getModule( $generatorName, null, true );
132 if ( $generator === null ) {
133 $this->dieUsage( 'Unknown generator=' . $generatorName, 'badgenerator' );
135 if ( !$generator instanceof ApiQueryGeneratorBase
) {
136 $this->dieUsage( "Module $generatorName cannot be used as a generator", 'badgenerator' );
138 // Create a temporary pageset to store generator's output,
139 // add any additional fields generator may need, and execute pageset to populate titles/pageids
140 $tmpPageSet = new ApiPageSet( $dbSource, ApiPageSet
::DISABLE_GENERATORS
);
141 $generator->setGeneratorMode( $tmpPageSet );
142 $this->mCacheMode
= $generator->getCacheMode( $generator->extractRequestParams() );
145 $generator->requestExtraData( $tmpPageSet );
147 $tmpPageSet->executeInternal( $isDryRun );
149 // populate this pageset with the generator output
151 $generator->profileIn();
154 $generator->executeGenerator( $this );
155 wfRunHooks( 'APIQueryGeneratorAfterExecute', array( &$generator, &$this ) );
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();
167 $this->resolvePendingRedirects();
171 // If this pageset is not part of the query, we called profileIn() above
172 $dbSource->profileOut();
175 // Only one of the titles/pageids/revids is allowed at the same time
177 if ( isset( $this->mParams
['titles'] ) ) {
178 $dataSource = 'titles';
180 if ( isset( $this->mParams
['pageids'] ) ) {
181 if ( isset( $dataSource ) ) {
182 $this->dieUsage( "Cannot use 'pageids' at the same time as '$dataSource'", 'multisource' );
184 $dataSource = 'pageids';
186 if ( isset( $this->mParams
['revids'] ) ) {
187 if ( isset( $dataSource ) ) {
188 $this->dieUsage( "Cannot use 'revids' at the same time as '$dataSource'", 'multisource' );
190 $dataSource = 'revids';
194 // Populate page information with the original user input
195 switch ( $dataSource ) {
197 $this->initFromTitles( $this->mParams
['titles'] );
200 $this->initFromPageIds( $this->mParams
['pageids'] );
203 if ( $this->mResolveRedirects
) {
204 $this->setWarning( 'Redirect resolution cannot be used ' .
205 'together with the revids= parameter. Any redirects ' .
206 'the revids= point to have not been resolved.' );
208 $this->mResolveRedirects
= false;
209 $this->initFromRevIDs( $this->mParams
['revids'] );
212 // Do nothing - some queries do not need any of the data sources.
221 * Check whether this PageSet is resolving redirects
224 public function isResolvingRedirects() {
225 return $this->mResolveRedirects
;
229 * Return the parameter name that is the source of data for this PageSet
231 * If multiple source parameters are specified (e.g. titles and pageids),
232 * one will be named arbitrarily.
234 * @return string|null
236 public function getDataSource() {
237 if ( $this->mAllowGenerator
&& isset( $this->mParams
['generator'] ) ) {
240 if ( isset( $this->mParams
['titles'] ) ) {
243 if ( isset( $this->mParams
['pageids'] ) ) {
246 if ( isset( $this->mParams
['revids'] ) ) {
254 * Request an additional field from the page table.
255 * Must be called before execute()
256 * @param string $fieldName Field name
258 public function requestField( $fieldName ) {
259 $this->mRequestedPageFields
[$fieldName] = null;
263 * Get the value of a custom field previously requested through
265 * @param string $fieldName Field name
266 * @return mixed Field value
268 public function getCustomField( $fieldName ) {
269 return $this->mRequestedPageFields
[$fieldName];
273 * Get the fields that have to be queried from the page table:
274 * the ones requested through requestField() and a few basic ones
276 * @return array of field names
278 public function getPageTableFields() {
279 // Ensure we get minimum required fields
280 // DON'T change this order
282 'page_namespace' => null,
283 'page_title' => null,
287 if ( $this->mResolveRedirects
) {
288 $pageFlds['page_is_redirect'] = null;
291 // only store non-default fields
292 $this->mRequestedPageFields
= array_diff_key( $this->mRequestedPageFields
, $pageFlds );
294 $pageFlds = array_merge( $pageFlds, $this->mRequestedPageFields
);
296 return array_keys( $pageFlds );
300 * Returns an array [ns][dbkey] => page_id for all requested titles.
301 * page_id is a unique negative number in case title was not found.
302 * Invalid titles will also have negative page IDs and will be in namespace 0
305 public function getAllTitlesByNamespace() {
306 return $this->mAllPages
;
310 * All Title objects provided.
311 * @return array of Title objects
313 public function getTitles() {
314 return $this->mTitles
;
318 * Returns the number of unique pages (not revisions) in the set.
321 public function getTitleCount() {
322 return count( $this->mTitles
);
326 * Title objects that were found in the database.
327 * @return array page_id (int) => Title (obj)
329 public function getGoodTitles() {
330 return $this->mGoodTitles
;
334 * Returns the number of found unique pages (not revisions) in the set.
337 public function getGoodTitleCount() {
338 return count( $this->mGoodTitles
);
342 * Title objects that were NOT found in the database.
343 * The array's index will be negative for each item
344 * @return array of Title objects
346 public function getMissingTitles() {
347 return $this->mMissingTitles
;
351 * Titles that were deemed invalid by Title::newFromText()
352 * The array's index will be unique and negative for each item
353 * @return array of strings (not Title objects)
355 public function getInvalidTitles() {
356 return $this->mInvalidTitles
;
360 * Page IDs that were not found in the database
361 * @return array of page IDs
363 public function getMissingPageIDs() {
364 return $this->mMissingPageIDs
;
368 * Get a list of redirect resolutions - maps a title to its redirect
369 * target, as an array of output-ready arrays
372 public function getRedirectTitles() {
373 return $this->mRedirectTitles
;
377 * Get a list of redirect resolutions - maps a title to its redirect
379 * @param $result ApiResult
380 * @return array of prefixed_title (string) => Title object
383 public function getRedirectTitlesAsResult( $result = null ) {
385 foreach ( $this->getRedirectTitles() as $titleStrFrom => $titleTo ) {
387 'from' => strval( $titleStrFrom ),
388 'to' => $titleTo->getPrefixedText(),
390 if ( $titleTo->getFragment() !== '' ) {
391 $r['tofragment'] = $titleTo->getFragment();
395 if ( !empty( $values ) && $result ) {
396 $result->setIndexedTagName( $values, 'r' );
403 * Get a list of title normalizations - maps a title to its normalized
405 * @return array raw_prefixed_title (string) => prefixed_title (string)
407 public function getNormalizedTitles() {
408 return $this->mNormalizedTitles
;
412 * Get a list of title normalizations - maps a title to its normalized
413 * version in the form of result array.
414 * @param $result ApiResult
415 * @return array of raw_prefixed_title (string) => prefixed_title (string)
418 public function getNormalizedTitlesAsResult( $result = null ) {
420 foreach ( $this->getNormalizedTitles() as $rawTitleStr => $titleStr ) {
422 'from' => $rawTitleStr,
426 if ( !empty( $values ) && $result ) {
427 $result->setIndexedTagName( $values, 'n' );
434 * Get a list of title conversions - maps a title to its converted
436 * @return array raw_prefixed_title (string) => prefixed_title (string)
438 public function getConvertedTitles() {
439 return $this->mConvertedTitles
;
443 * Get a list of title conversions - maps a title to its converted
444 * version as a result array.
445 * @param $result ApiResult
446 * @return array of (from, to) strings
449 public function getConvertedTitlesAsResult( $result = null ) {
451 foreach ( $this->getConvertedTitles() as $rawTitleStr => $titleStr ) {
453 'from' => $rawTitleStr,
457 if ( !empty( $values ) && $result ) {
458 $result->setIndexedTagName( $values, 'c' );
465 * Get a list of interwiki titles - maps a title to its interwiki
467 * @return array raw_prefixed_title (string) => interwiki_prefix (string)
469 public function getInterwikiTitles() {
470 return $this->mInterwikiTitles
;
474 * Get a list of interwiki titles - maps a title to its interwiki
476 * @param $result ApiResult
477 * @param $iwUrl boolean
478 * @return array raw_prefixed_title (string) => interwiki_prefix (string)
481 public function getInterwikiTitlesAsResult( $result = null, $iwUrl = false ) {
483 foreach ( $this->getInterwikiTitles() as $rawTitleStr => $interwikiStr ) {
485 'title' => $rawTitleStr,
486 'iw' => $interwikiStr,
489 $title = Title
::newFromText( $rawTitleStr );
490 $item['url'] = $title->getFullURL( '', false, PROTO_CURRENT
);
494 if ( !empty( $values ) && $result ) {
495 $result->setIndexedTagName( $values, 'i' );
502 * Get the list of revision IDs (requested with the revids= parameter)
503 * @return array revID (int) => pageID (int)
505 public function getRevisionIDs() {
506 return $this->mGoodRevIDs
;
510 * Revision IDs that were not found in the database
511 * @return array of revision IDs
513 public function getMissingRevisionIDs() {
514 return $this->mMissingRevIDs
;
518 * Revision IDs that were not found in the database as result array.
519 * @param $result ApiResult
520 * @return array of revision IDs
523 public function getMissingRevisionIDsAsResult( $result = null ) {
525 foreach ( $this->getMissingRevisionIDs() as $revid ) {
526 $values[$revid] = array(
530 if ( !empty( $values ) && $result ) {
531 $result->setIndexedTagName( $values, 'rev' );
538 * Get the list of titles with negative namespace
539 * @return array Title
541 public function getSpecialTitles() {
542 return $this->mSpecialTitles
;
546 * Returns the number of revisions (requested with revids= parameter).
547 * @return int Number of revisions.
549 public function getRevisionCount() {
550 return count( $this->getRevisionIDs() );
554 * Populate this PageSet from a list of Titles
555 * @param array $titles of Title objects
557 public function populateFromTitles( $titles ) {
559 $this->initFromTitles( $titles );
564 * Populate this PageSet from a list of page IDs
565 * @param array $pageIDs of page IDs
567 public function populateFromPageIDs( $pageIDs ) {
569 $this->initFromPageIds( $pageIDs );
574 * Populate this PageSet from a rowset returned from the database
575 * @param $db DatabaseBase object
576 * @param $queryResult ResultWrapper Query result object
578 public function populateFromQueryResult( $db, $queryResult ) {
580 $this->initFromQueryResult( $queryResult );
585 * Populate this PageSet from a list of revision IDs
586 * @param array $revIDs of revision IDs
588 public function populateFromRevisionIDs( $revIDs ) {
590 $this->initFromRevIDs( $revIDs );
595 * Extract all requested fields from the row received from the database
596 * @param stdClass $row Result row
598 public function processDbRow( $row ) {
599 // Store Title object in various data structures
600 $title = Title
::newFromRow( $row );
602 $pageId = intval( $row->page_id
);
603 $this->mAllPages
[$row->page_namespace
][$row->page_title
] = $pageId;
604 $this->mTitles
[] = $title;
606 if ( $this->mResolveRedirects
&& $row->page_is_redirect
== '1' ) {
607 $this->mPendingRedirectIDs
[$pageId] = $title;
609 $this->mGoodTitles
[$pageId] = $title;
612 foreach ( $this->mRequestedPageFields
as $fieldName => &$fieldValues ) {
613 $fieldValues[$pageId] = $row->$fieldName;
618 * Do not use, does nothing, will be removed
619 * @deprecated since 1.21
621 public function finishPageSetGeneration() {
622 wfDeprecated( __METHOD__
, '1.21' );
626 * This method populates internal variables with page information
627 * based on the given array of title strings.
630 * #1 For each title, get data from `page` table
631 * #2 If page was not found in the DB, store it as missing
633 * Additionally, when resolving redirects:
634 * #3 If no more redirects left, stop.
635 * #4 For each redirect, get its target from the `redirect` table.
636 * #5 Substitute the original LinkBatch object with the new list
637 * #6 Repeat from step #1
639 * @param array $titles of Title objects or strings
641 private function initFromTitles( $titles ) {
642 // Get validated and normalized title objects
643 $linkBatch = $this->processTitlesArray( $titles );
644 if ( $linkBatch->isEmpty() ) {
648 $db = $this->getDB();
649 $set = $linkBatch->constructSet( 'page', $db );
651 // Get pageIDs data from the `page` table
652 $this->profileDBIn();
653 $res = $db->select( 'page', $this->getPageTableFields(), $set,
655 $this->profileDBOut();
657 // Hack: get the ns:titles stored in array(ns => array(titles)) format
658 $this->initFromQueryResult( $res, $linkBatch->data
, true ); // process Titles
660 // Resolve any found redirects
661 $this->resolvePendingRedirects();
665 * Does the same as initFromTitles(), but is based on page IDs instead
666 * @param array $pageids of page IDs
668 private function initFromPageIds( $pageids ) {
673 $pageids = array_map( 'intval', $pageids ); // paranoia
674 $remaining = array_flip( $pageids );
676 $pageids = self
::getPositiveIntegers( $pageids );
679 if ( !empty( $pageids ) ) {
681 'page_id' => $pageids
683 $db = $this->getDB();
685 // Get pageIDs data from the `page` table
686 $this->profileDBIn();
687 $res = $db->select( 'page', $this->getPageTableFields(), $set,
689 $this->profileDBOut();
692 $this->initFromQueryResult( $res, $remaining, false ); // process PageIDs
694 // Resolve any found redirects
695 $this->resolvePendingRedirects();
699 * Iterate through the result of the query on 'page' table,
700 * and for each row create and store title object and save any extra fields requested.
701 * @param $res ResultWrapper DB Query result
702 * @param array $remaining of either pageID or ns/title elements (optional).
703 * If given, any missing items will go to $mMissingPageIDs and $mMissingTitles
704 * @param bool $processTitles Must be provided together with $remaining.
705 * If true, treat $remaining as an array of [ns][title]
706 * If false, treat it as an array of [pageIDs]
708 private function initFromQueryResult( $res, &$remaining = null, $processTitles = null ) {
709 if ( !is_null( $remaining ) && is_null( $processTitles ) ) {
710 ApiBase
::dieDebug( __METHOD__
, 'Missing $processTitles parameter when $remaining is provided' );
713 $usernames = array();
715 foreach ( $res as $row ) {
716 $pageId = intval( $row->page_id
);
718 // Remove found page from the list of remaining items
719 if ( isset( $remaining ) ) {
720 if ( $processTitles ) {
721 unset( $remaining[$row->page_namespace
][$row->page_title
] );
723 unset( $remaining[$pageId] );
727 // Store any extra fields requested by modules
728 $this->processDbRow( $row );
730 // Need gender information
731 if ( MWNamespace
::hasGenderDistinction( $row->page_namespace
) ) {
732 $usernames[] = $row->page_title
;
737 if ( isset( $remaining ) ) {
738 // Any items left in the $remaining list are added as missing
739 if ( $processTitles ) {
740 // The remaining titles in $remaining are non-existent pages
741 foreach ( $remaining as $ns => $dbkeys ) {
742 foreach ( array_keys( $dbkeys ) as $dbkey ) {
743 $title = Title
::makeTitle( $ns, $dbkey );
744 $this->mAllPages
[$ns][$dbkey] = $this->mFakePageId
;
745 $this->mMissingTitles
[$this->mFakePageId
] = $title;
746 $this->mFakePageId
--;
747 $this->mTitles
[] = $title;
749 // need gender information
750 if ( MWNamespace
::hasGenderDistinction( $ns ) ) {
751 $usernames[] = $dbkey;
756 // The remaining pageids do not exist
757 if ( !$this->mMissingPageIDs
) {
758 $this->mMissingPageIDs
= array_keys( $remaining );
760 $this->mMissingPageIDs
= array_merge( $this->mMissingPageIDs
, array_keys( $remaining ) );
765 // Get gender information
766 $genderCache = GenderCache
::singleton();
767 $genderCache->doQuery( $usernames, __METHOD__
);
771 * Does the same as initFromTitles(), but is based on revision IDs
773 * @param array $revids of revision IDs
775 private function initFromRevIDs( $revids ) {
780 $revids = array_map( 'intval', $revids ); // paranoia
781 $db = $this->getDB();
783 $remaining = array_flip( $revids );
785 $revids = self
::getPositiveIntegers( $revids );
787 if ( !empty( $revids ) ) {
788 $tables = array( 'revision', 'page' );
789 $fields = array( 'rev_id', 'rev_page' );
790 $where = array( 'rev_id' => $revids, 'rev_page = page_id' );
792 // Get pageIDs data from the `page` table
793 $this->profileDBIn();
794 $res = $db->select( $tables, $fields, $where, __METHOD__
);
795 foreach ( $res as $row ) {
796 $revid = intval( $row->rev_id
);
797 $pageid = intval( $row->rev_page
);
798 $this->mGoodRevIDs
[$revid] = $pageid;
799 $pageids[$pageid] = '';
800 unset( $remaining[$revid] );
802 $this->profileDBOut();
805 $this->mMissingRevIDs
= array_keys( $remaining );
807 // Populate all the page information
808 $this->initFromPageIds( array_keys( $pageids ) );
812 * Resolve any redirects in the result if redirect resolution was
813 * requested. This function is called repeatedly until all redirects
814 * have been resolved.
816 private function resolvePendingRedirects() {
817 if ( $this->mResolveRedirects
) {
818 $db = $this->getDB();
819 $pageFlds = $this->getPageTableFields();
821 // Repeat until all redirects have been resolved
822 // The infinite loop is prevented by keeping all known pages in $this->mAllPages
823 while ( $this->mPendingRedirectIDs
) {
824 // Resolve redirects by querying the pagelinks table, and repeat the process
825 // Create a new linkBatch object for the next pass
826 $linkBatch = $this->getRedirectTargets();
828 if ( $linkBatch->isEmpty() ) {
832 $set = $linkBatch->constructSet( 'page', $db );
833 if ( $set === false ) {
837 // Get pageIDs data from the `page` table
838 $this->profileDBIn();
839 $res = $db->select( 'page', $pageFlds, $set, __METHOD__
);
840 $this->profileDBOut();
842 // Hack: get the ns:titles stored in array(ns => array(titles)) format
843 $this->initFromQueryResult( $res, $linkBatch->data
, true );
849 * Get the targets of the pending redirects from the database
851 * Also creates entries in the redirect table for redirects that don't
855 private function getRedirectTargets() {
856 $lb = new LinkBatch();
857 $db = $this->getDB();
859 $this->profileDBIn();
868 ), array( 'rd_from' => array_keys( $this->mPendingRedirectIDs
) ),
871 $this->profileDBOut();
872 foreach ( $res as $row ) {
873 $rdfrom = intval( $row->rd_from
);
874 $from = $this->mPendingRedirectIDs
[$rdfrom]->getPrefixedText();
875 $to = Title
::makeTitle(
881 unset( $this->mPendingRedirectIDs
[$rdfrom] );
882 if ( !$to->isExternal() && !isset( $this->mAllPages
[$row->rd_namespace
][$row->rd_title
] ) ) {
883 $lb->add( $row->rd_namespace
, $row->rd_title
);
885 $this->mRedirectTitles
[$from] = $to;
888 if ( $this->mPendingRedirectIDs
) {
889 // We found pages that aren't in the redirect table
891 foreach ( $this->mPendingRedirectIDs
as $id => $title ) {
892 $page = WikiPage
::factory( $title );
893 $rt = $page->insertRedirect();
895 // What the hell. Let's just ignore this
899 $this->mRedirectTitles
[$title->getPrefixedText()] = $rt;
900 unset( $this->mPendingRedirectIDs
[$id] );
908 * Get the cache mode for the data generated by this module.
909 * All PageSet users should take into account whether this returns a more-restrictive
910 * cache mode than the using module itself. For possible return values and other
911 * details about cache modes, see ApiMain::setCacheMode()
913 * Public caching will only be allowed if *all* the modules that supply
914 * data for a given request return a cache mode of public.
920 public function getCacheMode( $params = null ) {
921 return $this->mCacheMode
;
925 * Given an array of title strings, convert them into Title objects.
926 * Alternatively, an array of Title objects may be given.
927 * This method validates access rights for the title,
928 * and appends normalization values to the output.
930 * @param array $titles of Title objects or strings
933 private function processTitlesArray( $titles ) {
934 $usernames = array();
935 $linkBatch = new LinkBatch();
937 foreach ( $titles as $title ) {
938 if ( is_string( $title ) ) {
939 $titleObj = Title
::newFromText( $title, $this->mDefaultNamespace
);
944 // Handle invalid titles gracefully
945 $this->mAllPages
[0][$title] = $this->mFakePageId
;
946 $this->mInvalidTitles
[$this->mFakePageId
] = $title;
947 $this->mFakePageId
--;
948 continue; // There's nothing else we can do
950 $unconvertedTitle = $titleObj->getPrefixedText();
951 $titleWasConverted = false;
952 if ( $titleObj->isExternal() ) {
953 // This title is an interwiki link.
954 $this->mInterwikiTitles
[$unconvertedTitle] = $titleObj->getInterwiki();
958 if ( $this->mConvertTitles
&&
959 count( $wgContLang->getVariants() ) > 1 &&
962 // Language::findVariantLink will modify titleText and titleObj into
963 // the canonical variant if possible
964 $titleText = is_string( $title ) ?
$title : $titleObj->getPrefixedText();
965 $wgContLang->findVariantLink( $titleText, $titleObj );
966 $titleWasConverted = $unconvertedTitle !== $titleObj->getPrefixedText();
969 if ( $titleObj->getNamespace() < 0 ) {
970 // Handle Special and Media pages
971 $titleObj = $titleObj->fixSpecialName();
972 $this->mSpecialTitles
[$this->mFakePageId
] = $titleObj;
973 $this->mFakePageId
--;
976 $linkBatch->addObj( $titleObj );
980 // Make sure we remember the original title that was
981 // given to us. This way the caller can correlate new
982 // titles with the originally requested when e.g. the
983 // namespace is localized or the capitalization is
985 if ( $titleWasConverted ) {
986 $this->mConvertedTitles
[$unconvertedTitle] = $titleObj->getPrefixedText();
987 // In this case the page can't be Special.
988 if ( is_string( $title ) && $title !== $unconvertedTitle ) {
989 $this->mNormalizedTitles
[$title] = $unconvertedTitle;
991 } elseif ( is_string( $title ) && $title !== $titleObj->getPrefixedText() ) {
992 $this->mNormalizedTitles
[$title] = $titleObj->getPrefixedText();
995 // Need gender information
996 if ( MWNamespace
::hasGenderDistinction( $titleObj->getNamespace() ) ) {
997 $usernames[] = $titleObj->getText();
1000 // Get gender information
1001 $genderCache = GenderCache
::singleton();
1002 $genderCache->doQuery( $usernames, __METHOD__
);
1008 * Get the database connection (read-only)
1009 * @return DatabaseBase
1011 protected function getDB() {
1012 return $this->mDbSource
->getDB();
1016 * Returns the input array of integers with all values < 0 removed
1018 * @param $array array
1021 private static function getPositiveIntegers( $array ) {
1022 // bug 25734 API: possible issue with revids validation
1023 // It seems with a load of revision rows, MySQL gets upset
1024 // Remove any < 0 integers, as they can't be valid
1025 foreach ( $array as $i => $int ) {
1027 unset( $array[$i] );
1034 public function getAllowedParams( $flags = 0 ) {
1037 ApiBase
::PARAM_ISMULTI
=> true
1040 ApiBase
::PARAM_TYPE
=> 'integer',
1041 ApiBase
::PARAM_ISMULTI
=> true
1044 ApiBase
::PARAM_TYPE
=> 'integer',
1045 ApiBase
::PARAM_ISMULTI
=> true
1047 'redirects' => false,
1048 'converttitles' => false,
1050 if ( $this->mAllowGenerator
) {
1051 if ( $flags & ApiBase
::GET_VALUES_FOR_HELP
) {
1052 $result['generator'] = array(
1053 ApiBase
::PARAM_TYPE
=> $this->getGenerators()
1056 $result['generator'] = null;
1063 private static $generators = null;
1066 * Get an array of all available generators
1069 private function getGenerators() {
1070 if ( self
::$generators === null ) {
1071 $query = $this->mDbSource
;
1072 if ( !( $query instanceof ApiQuery
) ) {
1073 // If the parent container of this pageset is not ApiQuery,
1074 // we must create it to get module manager
1075 $query = $this->getMain()->getModuleManager()->getModule( 'query' );
1078 $mgr = $query->getModuleManager();
1079 foreach ( $mgr->getNamesWithClasses() as $name => $class ) {
1080 if ( is_subclass_of( $class, 'ApiQueryGeneratorBase' ) ) {
1085 self
::$generators = $gens;
1088 return self
::$generators;
1091 public function getParamDescription() {
1093 'titles' => 'A list of titles to work on',
1094 'pageids' => 'A list of page IDs to work on',
1095 'revids' => 'A list of revision IDs to work on',
1096 'generator' => array(
1097 'Get the list of pages to work on by executing the specified query module.',
1098 'NOTE: generator parameter names must be prefixed with a \'g\', see examples'
1100 'redirects' => 'Automatically resolve redirects',
1101 'converttitles' => array(
1102 'Convert titles to other variants if necessary. Only works if ' .
1103 'the wiki\'s content language supports variant conversion.',
1104 'Languages that support variant conversion include ' .
1105 implode( ', ', LanguageConverter
::$languagesWithVariants )
1110 public function getPossibleErrors() {
1111 return array_merge( parent
::getPossibleErrors(), array(
1113 'code' => 'multisource',
1114 'info' => "Cannot use 'pageids' at the same time as 'dataSource'"
1117 'code' => 'multisource',
1118 'info' => "Cannot use 'revids' at the same time as 'dataSource'"
1121 'code' => 'badgenerator',
1122 'info' => 'Module $generatorName cannot be used as a generator'