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
{
43 * Constructor flag: The new instance of ApiPageSet will ignore the 'generator=' parameter
46 const DISABLE_GENERATORS
= 1;
50 private $mResolveRedirects;
51 private $mConvertTitles;
52 private $mAllowGenerator;
54 private $mAllPages = array(); // [ns][dbkey] => page_id or negative when missing
55 private $mTitles = array();
56 private $mGoodTitles = array();
57 private $mMissingTitles = array();
58 private $mInvalidTitles = array();
59 private $mMissingPageIDs = array();
60 private $mRedirectTitles = array();
61 private $mSpecialTitles = array();
62 private $mNormalizedTitles = array();
63 private $mInterwikiTitles = array();
64 private $mPendingRedirectIDs = array();
65 private $mConvertedTitles = array();
66 private $mGoodRevIDs = array();
67 private $mMissingRevIDs = array();
68 private $mFakePageId = -1;
69 private $mCacheMode = 'public';
70 private $mRequestedPageFields = array();
74 private $mDefaultNamespace = NS_MAIN
;
77 * Add all items from $values into the result
78 * @param array $result Output
79 * @param array $values Values to add
80 * @param string $flag The name of the boolean flag to mark this element
81 * @param string $name If given, name of the value
83 private static function addValues( array &$result, $values, $flag = null, $name = null ) {
84 foreach ( $values as $val ) {
85 if ( $val instanceof Title
) {
87 ApiQueryBase
::addTitleInfo( $v, $val );
88 } elseif ( $name !== null ) {
89 $v = array( $name => $val );
93 if ( $flag !== null ) {
101 * @param ApiBase $dbSource Module implementing getDB().
102 * Allows PageSet to reuse existing db connection from the shared state like ApiQuery.
103 * @param int $flags Zero or more flags like DISABLE_GENERATORS
104 * @param int $defaultNamespace The namespace to use if none is specified by a prefix.
105 * @since 1.21 accepts $flags instead of two boolean values
107 public function __construct( ApiBase
$dbSource, $flags = 0, $defaultNamespace = NS_MAIN
) {
108 parent
::__construct( $dbSource->getMain(), $dbSource->getModuleName() );
109 $this->mDbSource
= $dbSource;
110 $this->mAllowGenerator
= ( $flags & ApiPageSet
::DISABLE_GENERATORS
) == 0;
111 $this->mDefaultNamespace
= $defaultNamespace;
114 $this->mParams
= $this->extractRequestParams();
115 $this->mResolveRedirects
= $this->mParams
['redirects'];
116 $this->mConvertTitles
= $this->mParams
['converttitles'];
121 * In case execute() is not called, call this method to mark all relevant parameters as used
122 * This prevents unused parameters from being reported as warnings
124 public function executeDryRun() {
125 $this->executeInternal( true );
129 * Populate the PageSet from the request parameters.
131 public function execute() {
132 $this->executeInternal( false );
136 * Populate the PageSet from the request parameters.
137 * @param bool $isDryRun If true, instantiates generator, but only to mark
138 * relevant parameters as used
140 private function executeInternal( $isDryRun ) {
143 $generatorName = $this->mAllowGenerator ?
$this->mParams
['generator'] : null;
144 if ( isset( $generatorName ) ) {
145 $dbSource = $this->mDbSource
;
146 $isQuery = $dbSource instanceof ApiQuery
;
148 // If the parent container of this pageset is not ApiQuery, we must create it to run generator
149 $dbSource = $this->getMain()->getModuleManager()->getModule( 'query' );
150 // Enable profiling for query module because it will be used for db sql profiling
151 $dbSource->profileIn();
153 $generator = $dbSource->getModuleManager()->getModule( $generatorName, null, true );
154 if ( $generator === null ) {
155 $this->dieUsage( 'Unknown generator=' . $generatorName, 'badgenerator' );
157 if ( !$generator instanceof ApiQueryGeneratorBase
) {
158 $this->dieUsage( "Module $generatorName cannot be used as a generator", 'badgenerator' );
160 // Create a temporary pageset to store generator's output,
161 // add any additional fields generator may need, and execute pageset to populate titles/pageids
162 $tmpPageSet = new ApiPageSet( $dbSource, ApiPageSet
::DISABLE_GENERATORS
);
163 $generator->setGeneratorMode( $tmpPageSet );
164 $this->mCacheMode
= $generator->getCacheMode( $generator->extractRequestParams() );
167 $generator->requestExtraData( $tmpPageSet );
169 $tmpPageSet->executeInternal( $isDryRun );
171 // populate this pageset with the generator output
173 $generator->profileIn();
176 $generator->executeGenerator( $this );
177 wfRunHooks( 'APIQueryGeneratorAfterExecute', array( &$generator, &$this ) );
179 // Prevent warnings from being reported on these parameters
180 $main = $this->getMain();
181 foreach ( $generator->extractRequestParams() as $paramName => $param ) {
182 $main->getVal( $generator->encodeParamName( $paramName ) );
185 $generator->profileOut();
189 $this->resolvePendingRedirects();
193 // If this pageset is not part of the query, we called profileIn() above
194 $dbSource->profileOut();
197 // Only one of the titles/pageids/revids is allowed at the same time
199 if ( isset( $this->mParams
['titles'] ) ) {
200 $dataSource = 'titles';
202 if ( isset( $this->mParams
['pageids'] ) ) {
203 if ( isset( $dataSource ) ) {
204 $this->dieUsage( "Cannot use 'pageids' at the same time as '$dataSource'", 'multisource' );
206 $dataSource = 'pageids';
208 if ( isset( $this->mParams
['revids'] ) ) {
209 if ( isset( $dataSource ) ) {
210 $this->dieUsage( "Cannot use 'revids' at the same time as '$dataSource'", 'multisource' );
212 $dataSource = 'revids';
216 // Populate page information with the original user input
217 switch ( $dataSource ) {
219 $this->initFromTitles( $this->mParams
['titles'] );
222 $this->initFromPageIds( $this->mParams
['pageids'] );
225 if ( $this->mResolveRedirects
) {
226 $this->setWarning( 'Redirect resolution cannot be used ' .
227 'together with the revids= parameter. Any redirects ' .
228 'the revids= point to have not been resolved.' );
230 $this->mResolveRedirects
= false;
231 $this->initFromRevIDs( $this->mParams
['revids'] );
234 // Do nothing - some queries do not need any of the data sources.
243 * Check whether this PageSet is resolving redirects
246 public function isResolvingRedirects() {
247 return $this->mResolveRedirects
;
251 * Return the parameter name that is the source of data for this PageSet
253 * If multiple source parameters are specified (e.g. titles and pageids),
254 * one will be named arbitrarily.
256 * @return string|null
258 public function getDataSource() {
259 if ( $this->mAllowGenerator
&& isset( $this->mParams
['generator'] ) ) {
262 if ( isset( $this->mParams
['titles'] ) ) {
265 if ( isset( $this->mParams
['pageids'] ) ) {
268 if ( isset( $this->mParams
['revids'] ) ) {
276 * Request an additional field from the page table.
277 * Must be called before execute()
278 * @param string $fieldName Field name
280 public function requestField( $fieldName ) {
281 $this->mRequestedPageFields
[$fieldName] = null;
285 * Get the value of a custom field previously requested through
287 * @param string $fieldName Field name
288 * @return mixed Field value
290 public function getCustomField( $fieldName ) {
291 return $this->mRequestedPageFields
[$fieldName];
295 * Get the fields that have to be queried from the page table:
296 * the ones requested through requestField() and a few basic ones
298 * @return array Array of field names
300 public function getPageTableFields() {
301 // Ensure we get minimum required fields
302 // DON'T change this order
304 'page_namespace' => null,
305 'page_title' => null,
309 if ( $this->mResolveRedirects
) {
310 $pageFlds['page_is_redirect'] = null;
313 // only store non-default fields
314 $this->mRequestedPageFields
= array_diff_key( $this->mRequestedPageFields
, $pageFlds );
316 $pageFlds = array_merge( $pageFlds, $this->mRequestedPageFields
);
318 return array_keys( $pageFlds );
322 * Returns an array [ns][dbkey] => page_id for all requested titles.
323 * page_id is a unique negative number in case title was not found.
324 * Invalid titles will also have negative page IDs and will be in namespace 0
327 public function getAllTitlesByNamespace() {
328 return $this->mAllPages
;
332 * All Title objects provided.
335 public function getTitles() {
336 return $this->mTitles
;
340 * Returns the number of unique pages (not revisions) in the set.
343 public function getTitleCount() {
344 return count( $this->mTitles
);
348 * Title objects that were found in the database.
349 * @return Title[] Array page_id (int) => Title (obj)
351 public function getGoodTitles() {
352 return $this->mGoodTitles
;
356 * Returns the number of found unique pages (not revisions) in the set.
359 public function getGoodTitleCount() {
360 return count( $this->mGoodTitles
);
364 * Title objects that were NOT found in the database.
365 * The array's index will be negative for each item
368 public function getMissingTitles() {
369 return $this->mMissingTitles
;
373 * Titles that were deemed invalid by Title::newFromText()
374 * The array's index will be unique and negative for each item
375 * @return string[] Array of strings (not Title objects)
377 public function getInvalidTitles() {
378 return $this->mInvalidTitles
;
382 * Page IDs that were not found in the database
383 * @return array Array of page IDs
385 public function getMissingPageIDs() {
386 return $this->mMissingPageIDs
;
390 * Get a list of redirect resolutions - maps a title to its redirect
391 * target, as an array of output-ready arrays
394 public function getRedirectTitles() {
395 return $this->mRedirectTitles
;
399 * Get a list of redirect resolutions - maps a title to its redirect
401 * @param ApiResult $result
402 * @return array Array of prefixed_title (string) => Title object
405 public function getRedirectTitlesAsResult( $result = null ) {
407 foreach ( $this->getRedirectTitles() as $titleStrFrom => $titleTo ) {
409 'from' => strval( $titleStrFrom ),
410 'to' => $titleTo->getPrefixedText(),
412 if ( $titleTo->hasFragment() ) {
413 $r['tofragment'] = $titleTo->getFragment();
417 if ( !empty( $values ) && $result ) {
418 $result->setIndexedTagName( $values, 'r' );
425 * Get a list of title normalizations - maps a title to its normalized
427 * @return array Array of raw_prefixed_title (string) => prefixed_title (string)
429 public function getNormalizedTitles() {
430 return $this->mNormalizedTitles
;
434 * Get a list of title normalizations - maps a title to its normalized
435 * version in the form of result array.
436 * @param ApiResult $result
437 * @return array Array of raw_prefixed_title (string) => prefixed_title (string)
440 public function getNormalizedTitlesAsResult( $result = null ) {
442 foreach ( $this->getNormalizedTitles() as $rawTitleStr => $titleStr ) {
444 'from' => $rawTitleStr,
448 if ( !empty( $values ) && $result ) {
449 $result->setIndexedTagName( $values, 'n' );
456 * Get a list of title conversions - maps a title to its converted
458 * @return array Array of raw_prefixed_title (string) => prefixed_title (string)
460 public function getConvertedTitles() {
461 return $this->mConvertedTitles
;
465 * Get a list of title conversions - maps a title to its converted
466 * version as a result array.
467 * @param ApiResult $result
468 * @return array Array of (from, to) strings
471 public function getConvertedTitlesAsResult( $result = null ) {
473 foreach ( $this->getConvertedTitles() as $rawTitleStr => $titleStr ) {
475 'from' => $rawTitleStr,
479 if ( !empty( $values ) && $result ) {
480 $result->setIndexedTagName( $values, 'c' );
487 * Get a list of interwiki titles - maps a title to its interwiki
489 * @return array Array of raw_prefixed_title (string) => interwiki_prefix (string)
491 public function getInterwikiTitles() {
492 return $this->mInterwikiTitles
;
496 * Get a list of interwiki titles - maps a title to its interwiki
498 * @param ApiResult $result
500 * @return array Array of raw_prefixed_title (string) => interwiki_prefix (string)
503 public function getInterwikiTitlesAsResult( $result = null, $iwUrl = false ) {
505 foreach ( $this->getInterwikiTitles() as $rawTitleStr => $interwikiStr ) {
507 'title' => $rawTitleStr,
508 'iw' => $interwikiStr,
511 $title = Title
::newFromText( $rawTitleStr );
512 $item['url'] = $title->getFullURL( '', false, PROTO_CURRENT
);
516 if ( !empty( $values ) && $result ) {
517 $result->setIndexedTagName( $values, 'i' );
524 * Get an array of invalid/special/missing titles.
526 * @param array $invalidChecks List of types of invalid titles to include.
527 * Recognized values are:
528 * - invalidTitles: Titles from $this->getInvalidTitles()
529 * - special: Titles from $this->getSpecialTitles()
530 * - missingIds: ids from $this->getMissingPageIDs()
531 * - missingRevIds: ids from $this->getMissingRevisionIDs()
532 * - missingTitles: Titles from $this->getMissingTitles()
533 * - interwikiTitles: Titles from $this->getInterwikiTitlesAsResult()
534 * @return array Array suitable for inclusion in the response
537 public function getInvalidTitlesAndRevisions( $invalidChecks = array( 'invalidTitles',
538 'special', 'missingIds', 'missingRevIds', 'missingTitles', 'interwikiTitles' )
541 if ( in_array( "invalidTitles", $invalidChecks ) ) {
542 self
::addValues( $result, $this->getInvalidTitles(), 'invalid', 'title' );
544 if ( in_array( "special", $invalidChecks ) ) {
545 self
::addValues( $result, $this->getSpecialTitles(), 'special', 'title' );
547 if ( in_array( "missingIds", $invalidChecks ) ) {
548 self
::addValues( $result, $this->getMissingPageIDs(), 'missing', 'pageid' );
550 if ( in_array( "missingRevIds", $invalidChecks ) ) {
551 self
::addValues( $result, $this->getMissingRevisionIDs(), 'missing', 'revid' );
553 if ( in_array( "missingTitles", $invalidChecks ) ) {
554 self
::addValues( $result, $this->getMissingTitles(), 'missing' );
556 if ( in_array( "interwikiTitles", $invalidChecks ) ) {
557 self
::addValues( $result, $this->getInterwikiTitlesAsResult() );
564 * Get the list of revision IDs (requested with the revids= parameter)
565 * @return array Array of revID (int) => pageID (int)
567 public function getRevisionIDs() {
568 return $this->mGoodRevIDs
;
572 * Revision IDs that were not found in the database
573 * @return array Array of revision IDs
575 public function getMissingRevisionIDs() {
576 return $this->mMissingRevIDs
;
580 * Revision IDs that were not found in the database as result array.
581 * @param ApiResult $result
582 * @return array Array of revision IDs
585 public function getMissingRevisionIDsAsResult( $result = null ) {
587 foreach ( $this->getMissingRevisionIDs() as $revid ) {
588 $values[$revid] = array(
592 if ( !empty( $values ) && $result ) {
593 $result->setIndexedTagName( $values, 'rev' );
600 * Get the list of titles with negative namespace
601 * @return array Title
603 public function getSpecialTitles() {
604 return $this->mSpecialTitles
;
608 * Returns the number of revisions (requested with revids= parameter).
609 * @return int Number of revisions.
611 public function getRevisionCount() {
612 return count( $this->getRevisionIDs() );
616 * Populate this PageSet from a list of Titles
617 * @param array $titles Array of Title objects
619 public function populateFromTitles( $titles ) {
621 $this->initFromTitles( $titles );
626 * Populate this PageSet from a list of page IDs
627 * @param array $pageIDs Array of page IDs
629 public function populateFromPageIDs( $pageIDs ) {
631 $this->initFromPageIds( $pageIDs );
636 * Populate this PageSet from a rowset returned from the database
637 * @param DatabaseBase $db
638 * @param ResultWrapper $queryResult Query result object
640 public function populateFromQueryResult( $db, $queryResult ) {
642 $this->initFromQueryResult( $queryResult );
647 * Populate this PageSet from a list of revision IDs
648 * @param array $revIDs Array of revision IDs
650 public function populateFromRevisionIDs( $revIDs ) {
652 $this->initFromRevIDs( $revIDs );
657 * Extract all requested fields from the row received from the database
658 * @param stdClass $row Result row
660 public function processDbRow( $row ) {
661 // Store Title object in various data structures
662 $title = Title
::newFromRow( $row );
664 $pageId = intval( $row->page_id
);
665 $this->mAllPages
[$row->page_namespace
][$row->page_title
] = $pageId;
666 $this->mTitles
[] = $title;
668 if ( $this->mResolveRedirects
&& $row->page_is_redirect
== '1' ) {
669 $this->mPendingRedirectIDs
[$pageId] = $title;
671 $this->mGoodTitles
[$pageId] = $title;
674 foreach ( $this->mRequestedPageFields
as $fieldName => &$fieldValues ) {
675 $fieldValues[$pageId] = $row->$fieldName;
680 * Do not use, does nothing, will be removed
681 * @deprecated since 1.21
683 public function finishPageSetGeneration() {
684 wfDeprecated( __METHOD__
, '1.21' );
688 * This method populates internal variables with page information
689 * based on the given array of title strings.
692 * #1 For each title, get data from `page` table
693 * #2 If page was not found in the DB, store it as missing
695 * Additionally, when resolving redirects:
696 * #3 If no more redirects left, stop.
697 * #4 For each redirect, get its target from the `redirect` table.
698 * #5 Substitute the original LinkBatch object with the new list
699 * #6 Repeat from step #1
701 * @param array $titles Array of Title objects or strings
703 private function initFromTitles( $titles ) {
704 // Get validated and normalized title objects
705 $linkBatch = $this->processTitlesArray( $titles );
706 if ( $linkBatch->isEmpty() ) {
710 $db = $this->getDB();
711 $set = $linkBatch->constructSet( 'page', $db );
713 // Get pageIDs data from the `page` table
714 $this->profileDBIn();
715 $res = $db->select( 'page', $this->getPageTableFields(), $set,
717 $this->profileDBOut();
719 // Hack: get the ns:titles stored in array(ns => array(titles)) format
720 $this->initFromQueryResult( $res, $linkBatch->data
, true ); // process Titles
722 // Resolve any found redirects
723 $this->resolvePendingRedirects();
727 * Does the same as initFromTitles(), but is based on page IDs instead
728 * @param array $pageids Array of page IDs
730 private function initFromPageIds( $pageids ) {
735 $pageids = array_map( 'intval', $pageids ); // paranoia
736 $remaining = array_flip( $pageids );
738 $pageids = self
::getPositiveIntegers( $pageids );
741 if ( !empty( $pageids ) ) {
743 'page_id' => $pageids
745 $db = $this->getDB();
747 // Get pageIDs data from the `page` table
748 $this->profileDBIn();
749 $res = $db->select( 'page', $this->getPageTableFields(), $set,
751 $this->profileDBOut();
754 $this->initFromQueryResult( $res, $remaining, false ); // process PageIDs
756 // Resolve any found redirects
757 $this->resolvePendingRedirects();
761 * Iterate through the result of the query on 'page' table,
762 * and for each row create and store title object and save any extra fields requested.
763 * @param ResultWrapper $res DB Query result
764 * @param array $remaining Array of either pageID or ns/title elements (optional).
765 * If given, any missing items will go to $mMissingPageIDs and $mMissingTitles
766 * @param bool $processTitles Must be provided together with $remaining.
767 * If true, treat $remaining as an array of [ns][title]
768 * If false, treat it as an array of [pageIDs]
770 private function initFromQueryResult( $res, &$remaining = null, $processTitles = null ) {
771 if ( !is_null( $remaining ) && is_null( $processTitles ) ) {
772 ApiBase
::dieDebug( __METHOD__
, 'Missing $processTitles parameter when $remaining is provided' );
775 $usernames = array();
777 foreach ( $res as $row ) {
778 $pageId = intval( $row->page_id
);
780 // Remove found page from the list of remaining items
781 if ( isset( $remaining ) ) {
782 if ( $processTitles ) {
783 unset( $remaining[$row->page_namespace
][$row->page_title
] );
785 unset( $remaining[$pageId] );
789 // Store any extra fields requested by modules
790 $this->processDbRow( $row );
792 // Need gender information
793 if ( MWNamespace
::hasGenderDistinction( $row->page_namespace
) ) {
794 $usernames[] = $row->page_title
;
799 if ( isset( $remaining ) ) {
800 // Any items left in the $remaining list are added as missing
801 if ( $processTitles ) {
802 // The remaining titles in $remaining are non-existent pages
803 foreach ( $remaining as $ns => $dbkeys ) {
804 foreach ( array_keys( $dbkeys ) as $dbkey ) {
805 $title = Title
::makeTitle( $ns, $dbkey );
806 $this->mAllPages
[$ns][$dbkey] = $this->mFakePageId
;
807 $this->mMissingTitles
[$this->mFakePageId
] = $title;
808 $this->mFakePageId
--;
809 $this->mTitles
[] = $title;
811 // need gender information
812 if ( MWNamespace
::hasGenderDistinction( $ns ) ) {
813 $usernames[] = $dbkey;
818 // The remaining pageids do not exist
819 if ( !$this->mMissingPageIDs
) {
820 $this->mMissingPageIDs
= array_keys( $remaining );
822 $this->mMissingPageIDs
= array_merge( $this->mMissingPageIDs
, array_keys( $remaining ) );
827 // Get gender information
828 $genderCache = GenderCache
::singleton();
829 $genderCache->doQuery( $usernames, __METHOD__
);
833 * Does the same as initFromTitles(), but is based on revision IDs
835 * @param array $revids Array of revision IDs
837 private function initFromRevIDs( $revids ) {
842 $revids = array_map( 'intval', $revids ); // paranoia
843 $db = $this->getDB();
845 $remaining = array_flip( $revids );
847 $revids = self
::getPositiveIntegers( $revids );
849 if ( !empty( $revids ) ) {
850 $tables = array( 'revision', 'page' );
851 $fields = array( 'rev_id', 'rev_page' );
852 $where = array( 'rev_id' => $revids, 'rev_page = page_id' );
854 // Get pageIDs data from the `page` table
855 $this->profileDBIn();
856 $res = $db->select( $tables, $fields, $where, __METHOD__
);
857 foreach ( $res as $row ) {
858 $revid = intval( $row->rev_id
);
859 $pageid = intval( $row->rev_page
);
860 $this->mGoodRevIDs
[$revid] = $pageid;
861 $pageids[$pageid] = '';
862 unset( $remaining[$revid] );
864 $this->profileDBOut();
867 $this->mMissingRevIDs
= array_keys( $remaining );
869 // Populate all the page information
870 $this->initFromPageIds( array_keys( $pageids ) );
874 * Resolve any redirects in the result if redirect resolution was
875 * requested. This function is called repeatedly until all redirects
876 * have been resolved.
878 private function resolvePendingRedirects() {
879 if ( $this->mResolveRedirects
) {
880 $db = $this->getDB();
881 $pageFlds = $this->getPageTableFields();
883 // Repeat until all redirects have been resolved
884 // The infinite loop is prevented by keeping all known pages in $this->mAllPages
885 while ( $this->mPendingRedirectIDs
) {
886 // Resolve redirects by querying the pagelinks table, and repeat the process
887 // Create a new linkBatch object for the next pass
888 $linkBatch = $this->getRedirectTargets();
890 if ( $linkBatch->isEmpty() ) {
894 $set = $linkBatch->constructSet( 'page', $db );
895 if ( $set === false ) {
899 // Get pageIDs data from the `page` table
900 $this->profileDBIn();
901 $res = $db->select( 'page', $pageFlds, $set, __METHOD__
);
902 $this->profileDBOut();
904 // Hack: get the ns:titles stored in array(ns => array(titles)) format
905 $this->initFromQueryResult( $res, $linkBatch->data
, true );
911 * Get the targets of the pending redirects from the database
913 * Also creates entries in the redirect table for redirects that don't
917 private function getRedirectTargets() {
918 $lb = new LinkBatch();
919 $db = $this->getDB();
921 $this->profileDBIn();
930 ), array( 'rd_from' => array_keys( $this->mPendingRedirectIDs
) ),
933 $this->profileDBOut();
934 foreach ( $res as $row ) {
935 $rdfrom = intval( $row->rd_from
);
936 $from = $this->mPendingRedirectIDs
[$rdfrom]->getPrefixedText();
937 $to = Title
::makeTitle(
943 unset( $this->mPendingRedirectIDs
[$rdfrom] );
944 if ( !$to->isExternal() && !isset( $this->mAllPages
[$row->rd_namespace
][$row->rd_title
] ) ) {
945 $lb->add( $row->rd_namespace
, $row->rd_title
);
947 $this->mRedirectTitles
[$from] = $to;
950 if ( $this->mPendingRedirectIDs
) {
951 // We found pages that aren't in the redirect table
953 foreach ( $this->mPendingRedirectIDs
as $id => $title ) {
954 $page = WikiPage
::factory( $title );
955 $rt = $page->insertRedirect();
957 // What the hell. Let's just ignore this
961 $this->mRedirectTitles
[$title->getPrefixedText()] = $rt;
962 unset( $this->mPendingRedirectIDs
[$id] );
970 * Get the cache mode for the data generated by this module.
971 * All PageSet users should take into account whether this returns a more-restrictive
972 * cache mode than the using module itself. For possible return values and other
973 * details about cache modes, see ApiMain::setCacheMode()
975 * Public caching will only be allowed if *all* the modules that supply
976 * data for a given request return a cache mode of public.
978 * @param array|null $params
982 public function getCacheMode( $params = null ) {
983 return $this->mCacheMode
;
987 * Given an array of title strings, convert them into Title objects.
988 * Alternatively, an array of Title objects may be given.
989 * This method validates access rights for the title,
990 * and appends normalization values to the output.
992 * @param array $titles Array of Title objects or strings
995 private function processTitlesArray( $titles ) {
996 $usernames = array();
997 $linkBatch = new LinkBatch();
999 foreach ( $titles as $title ) {
1000 if ( is_string( $title ) ) {
1001 $titleObj = Title
::newFromText( $title, $this->mDefaultNamespace
);
1006 // Handle invalid titles gracefully
1007 $this->mAllPages
[0][$title] = $this->mFakePageId
;
1008 $this->mInvalidTitles
[$this->mFakePageId
] = $title;
1009 $this->mFakePageId
--;
1010 continue; // There's nothing else we can do
1012 $unconvertedTitle = $titleObj->getPrefixedText();
1013 $titleWasConverted = false;
1014 if ( $titleObj->isExternal() ) {
1015 // This title is an interwiki link.
1016 $this->mInterwikiTitles
[$unconvertedTitle] = $titleObj->getInterwiki();
1018 // Variants checking
1020 if ( $this->mConvertTitles
&&
1021 count( $wgContLang->getVariants() ) > 1 &&
1022 !$titleObj->exists()
1024 // Language::findVariantLink will modify titleText and titleObj into
1025 // the canonical variant if possible
1026 $titleText = is_string( $title ) ?
$title : $titleObj->getPrefixedText();
1027 $wgContLang->findVariantLink( $titleText, $titleObj );
1028 $titleWasConverted = $unconvertedTitle !== $titleObj->getPrefixedText();
1031 if ( $titleObj->getNamespace() < 0 ) {
1032 // Handle Special and Media pages
1033 $titleObj = $titleObj->fixSpecialName();
1034 $this->mSpecialTitles
[$this->mFakePageId
] = $titleObj;
1035 $this->mFakePageId
--;
1038 $linkBatch->addObj( $titleObj );
1042 // Make sure we remember the original title that was
1043 // given to us. This way the caller can correlate new
1044 // titles with the originally requested when e.g. the
1045 // namespace is localized or the capitalization is
1047 if ( $titleWasConverted ) {
1048 $this->mConvertedTitles
[$unconvertedTitle] = $titleObj->getPrefixedText();
1049 // In this case the page can't be Special.
1050 if ( is_string( $title ) && $title !== $unconvertedTitle ) {
1051 $this->mNormalizedTitles
[$title] = $unconvertedTitle;
1053 } elseif ( is_string( $title ) && $title !== $titleObj->getPrefixedText() ) {
1054 $this->mNormalizedTitles
[$title] = $titleObj->getPrefixedText();
1057 // Need gender information
1058 if ( MWNamespace
::hasGenderDistinction( $titleObj->getNamespace() ) ) {
1059 $usernames[] = $titleObj->getText();
1062 // Get gender information
1063 $genderCache = GenderCache
::singleton();
1064 $genderCache->doQuery( $usernames, __METHOD__
);
1070 * Get the database connection (read-only)
1071 * @return DatabaseBase
1073 protected function getDB() {
1074 return $this->mDbSource
->getDB();
1078 * Returns the input array of integers with all values < 0 removed
1080 * @param array $array
1083 private static function getPositiveIntegers( $array ) {
1084 // bug 25734 API: possible issue with revids validation
1085 // It seems with a load of revision rows, MySQL gets upset
1086 // Remove any < 0 integers, as they can't be valid
1087 foreach ( $array as $i => $int ) {
1089 unset( $array[$i] );
1096 public function getAllowedParams( $flags = 0 ) {
1099 ApiBase
::PARAM_ISMULTI
=> true
1102 ApiBase
::PARAM_TYPE
=> 'integer',
1103 ApiBase
::PARAM_ISMULTI
=> true
1106 ApiBase
::PARAM_TYPE
=> 'integer',
1107 ApiBase
::PARAM_ISMULTI
=> true
1109 'redirects' => false,
1110 'converttitles' => false,
1112 if ( $this->mAllowGenerator
) {
1113 if ( $flags & ApiBase
::GET_VALUES_FOR_HELP
) {
1114 $result['generator'] = array(
1115 ApiBase
::PARAM_TYPE
=> $this->getGenerators()
1118 $result['generator'] = null;
1125 private static $generators = null;
1128 * Get an array of all available generators
1131 private function getGenerators() {
1132 if ( self
::$generators === null ) {
1133 $query = $this->mDbSource
;
1134 if ( !( $query instanceof ApiQuery
) ) {
1135 // If the parent container of this pageset is not ApiQuery,
1136 // we must create it to get module manager
1137 $query = $this->getMain()->getModuleManager()->getModule( 'query' );
1140 $mgr = $query->getModuleManager();
1141 foreach ( $mgr->getNamesWithClasses() as $name => $class ) {
1142 if ( is_subclass_of( $class, 'ApiQueryGeneratorBase' ) ) {
1147 self
::$generators = $gens;
1150 return self
::$generators;
1153 public function getParamDescription() {
1155 'titles' => 'A list of titles to work on',
1156 'pageids' => 'A list of page IDs to work on',
1157 'revids' => 'A list of revision IDs to work on',
1158 'generator' => array(
1159 'Get the list of pages to work on by executing the specified query module.',
1160 'NOTE: generator parameter names must be prefixed with a \'g\', see examples'
1162 'redirects' => 'Automatically resolve redirects',
1163 'converttitles' => array(
1164 'Convert titles to other variants if necessary. Only works if ' .
1165 'the wiki\'s content language supports variant conversion.',
1166 'Languages that support variant conversion include ' .
1167 implode( ', ', LanguageConverter
::$languagesWithVariants )
1172 public function getPossibleErrors() {
1173 return array_merge( parent
::getPossibleErrors(), array(
1175 'code' => 'multisource',
1176 'info' => "Cannot use 'pageids' at the same time as 'dataSource'"
1179 'code' => 'multisource',
1180 'info' => "Cannot use 'revids' at the same time as 'dataSource'"
1183 'code' => 'badgenerator',
1184 'info' => 'Module $generatorName cannot be used as a generator'