Merge "CookieSessionProvider: It's persisted if we have a 'Token' cookie"
[lhc/web/wiklou.git] / includes / api / ApiPageSet.php
1 <?php
2 /**
3 *
4 *
5 * Created on Sep 24, 2006
6 *
7 * Copyright © 2006, 2013 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
8 *
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.
13 *
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.
18 *
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
23 *
24 * @file
25 */
26
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.
33 *
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.
37 *
38 * @ingroup API
39 * @since 1.21 derives from ApiBase instead of ApiQueryBase
40 */
41 class ApiPageSet extends ApiBase {
42 /**
43 * Constructor flag: The new instance of ApiPageSet will ignore the 'generator=' parameter
44 * @since 1.21
45 */
46 const DISABLE_GENERATORS = 1;
47
48 private $mDbSource;
49 private $mParams;
50 private $mResolveRedirects;
51 private $mConvertTitles;
52 private $mAllowGenerator;
53
54 private $mAllPages = array(); // [ns][dbkey] => page_id or negative when missing
55 private $mTitles = array();
56 private $mGoodAndMissingPages = array(); // [ns][dbkey] => page_id or negative when missing
57 private $mGoodPages = array(); // [ns][dbkey] => page_id
58 private $mGoodTitles = array();
59 private $mMissingPages = array(); // [ns][dbkey] => fake page_id
60 private $mMissingTitles = array();
61 /** @var array [fake_page_id] => array( 'title' => $title, 'invalidreason' => $reason ) */
62 private $mInvalidTitles = array();
63 private $mMissingPageIDs = array();
64 private $mRedirectTitles = array();
65 private $mSpecialTitles = array();
66 private $mNormalizedTitles = array();
67 private $mInterwikiTitles = array();
68 /** @var Title[] */
69 private $mPendingRedirectIDs = array();
70 private $mResolvedRedirectTitles = array();
71 private $mConvertedTitles = array();
72 private $mGoodRevIDs = array();
73 private $mLiveRevIDs = array();
74 private $mDeletedRevIDs = array();
75 private $mMissingRevIDs = array();
76 private $mGeneratorData = array(); // [ns][dbkey] => data array
77 private $mFakePageId = -1;
78 private $mCacheMode = 'public';
79 private $mRequestedPageFields = array();
80 /** @var int */
81 private $mDefaultNamespace = NS_MAIN;
82 /** @var callable|null */
83 private $mRedirectMergePolicy;
84
85 /**
86 * Add all items from $values into the result
87 * @param array $result Output
88 * @param array $values Values to add
89 * @param string $flag The name of the boolean flag to mark this element
90 * @param string $name If given, name of the value
91 */
92 private static function addValues( array &$result, $values, $flag = null, $name = null ) {
93 foreach ( $values as $val ) {
94 if ( $val instanceof Title ) {
95 $v = array();
96 ApiQueryBase::addTitleInfo( $v, $val );
97 } elseif ( $name !== null ) {
98 $v = array( $name => $val );
99 } else {
100 $v = $val;
101 }
102 if ( $flag !== null ) {
103 $v[$flag] = true;
104 }
105 $result[] = $v;
106 }
107 }
108
109 /**
110 * @param ApiBase $dbSource Module implementing getDB().
111 * Allows PageSet to reuse existing db connection from the shared state like ApiQuery.
112 * @param int $flags Zero or more flags like DISABLE_GENERATORS
113 * @param int $defaultNamespace The namespace to use if none is specified by a prefix.
114 * @since 1.21 accepts $flags instead of two boolean values
115 */
116 public function __construct( ApiBase $dbSource, $flags = 0, $defaultNamespace = NS_MAIN ) {
117 parent::__construct( $dbSource->getMain(), $dbSource->getModuleName() );
118 $this->mDbSource = $dbSource;
119 $this->mAllowGenerator = ( $flags & ApiPageSet::DISABLE_GENERATORS ) == 0;
120 $this->mDefaultNamespace = $defaultNamespace;
121
122 $this->mParams = $this->extractRequestParams();
123 $this->mResolveRedirects = $this->mParams['redirects'];
124 $this->mConvertTitles = $this->mParams['converttitles'];
125 }
126
127 /**
128 * In case execute() is not called, call this method to mark all relevant parameters as used
129 * This prevents unused parameters from being reported as warnings
130 */
131 public function executeDryRun() {
132 $this->executeInternal( true );
133 }
134
135 /**
136 * Populate the PageSet from the request parameters.
137 */
138 public function execute() {
139 $this->executeInternal( false );
140 }
141
142 /**
143 * Populate the PageSet from the request parameters.
144 * @param bool $isDryRun If true, instantiates generator, but only to mark
145 * relevant parameters as used
146 */
147 private function executeInternal( $isDryRun ) {
148 $generatorName = $this->mAllowGenerator ? $this->mParams['generator'] : null;
149 if ( isset( $generatorName ) ) {
150 $dbSource = $this->mDbSource;
151 if ( !$dbSource instanceof ApiQuery ) {
152 // If the parent container of this pageset is not ApiQuery, we must create it to run generator
153 $dbSource = $this->getMain()->getModuleManager()->getModule( 'query' );
154 }
155 $generator = $dbSource->getModuleManager()->getModule( $generatorName, null, true );
156 if ( $generator === null ) {
157 $this->dieUsage( 'Unknown generator=' . $generatorName, 'badgenerator' );
158 }
159 if ( !$generator instanceof ApiQueryGeneratorBase ) {
160 $this->dieUsage( "Module $generatorName cannot be used as a generator", 'badgenerator' );
161 }
162 // Create a temporary pageset to store generator's output,
163 // add any additional fields generator may need, and execute pageset to populate titles/pageids
164 $tmpPageSet = new ApiPageSet( $dbSource, ApiPageSet::DISABLE_GENERATORS );
165 $generator->setGeneratorMode( $tmpPageSet );
166 $this->mCacheMode = $generator->getCacheMode( $generator->extractRequestParams() );
167
168 if ( !$isDryRun ) {
169 $generator->requestExtraData( $tmpPageSet );
170 }
171 $tmpPageSet->executeInternal( $isDryRun );
172
173 // populate this pageset with the generator output
174 if ( !$isDryRun ) {
175 $generator->executeGenerator( $this );
176 Hooks::run( 'APIQueryGeneratorAfterExecute', array( &$generator, &$this ) );
177 } else {
178 // Prevent warnings from being reported on these parameters
179 $main = $this->getMain();
180 foreach ( $generator->extractRequestParams() as $paramName => $param ) {
181 $main->getVal( $generator->encodeParamName( $paramName ) );
182 }
183 }
184
185 if ( !$isDryRun ) {
186 $this->resolvePendingRedirects();
187 }
188 } else {
189 // Only one of the titles/pageids/revids is allowed at the same time
190 $dataSource = null;
191 if ( isset( $this->mParams['titles'] ) ) {
192 $dataSource = 'titles';
193 }
194 if ( isset( $this->mParams['pageids'] ) ) {
195 if ( isset( $dataSource ) ) {
196 $this->dieUsage( "Cannot use 'pageids' at the same time as '$dataSource'", 'multisource' );
197 }
198 $dataSource = 'pageids';
199 }
200 if ( isset( $this->mParams['revids'] ) ) {
201 if ( isset( $dataSource ) ) {
202 $this->dieUsage( "Cannot use 'revids' at the same time as '$dataSource'", 'multisource' );
203 }
204 $dataSource = 'revids';
205 }
206
207 if ( !$isDryRun ) {
208 // Populate page information with the original user input
209 switch ( $dataSource ) {
210 case 'titles':
211 $this->initFromTitles( $this->mParams['titles'] );
212 break;
213 case 'pageids':
214 $this->initFromPageIds( $this->mParams['pageids'] );
215 break;
216 case 'revids':
217 if ( $this->mResolveRedirects ) {
218 $this->setWarning( 'Redirect resolution cannot be used ' .
219 'together with the revids= parameter. Any redirects ' .
220 'the revids= point to have not been resolved.' );
221 }
222 $this->mResolveRedirects = false;
223 $this->initFromRevIDs( $this->mParams['revids'] );
224 break;
225 default:
226 // Do nothing - some queries do not need any of the data sources.
227 break;
228 }
229 }
230 }
231 }
232
233 /**
234 * Check whether this PageSet is resolving redirects
235 * @return bool
236 */
237 public function isResolvingRedirects() {
238 return $this->mResolveRedirects;
239 }
240
241 /**
242 * Return the parameter name that is the source of data for this PageSet
243 *
244 * If multiple source parameters are specified (e.g. titles and pageids),
245 * one will be named arbitrarily.
246 *
247 * @return string|null
248 */
249 public function getDataSource() {
250 if ( $this->mAllowGenerator && isset( $this->mParams['generator'] ) ) {
251 return 'generator';
252 }
253 if ( isset( $this->mParams['titles'] ) ) {
254 return 'titles';
255 }
256 if ( isset( $this->mParams['pageids'] ) ) {
257 return 'pageids';
258 }
259 if ( isset( $this->mParams['revids'] ) ) {
260 return 'revids';
261 }
262
263 return null;
264 }
265
266 /**
267 * Request an additional field from the page table.
268 * Must be called before execute()
269 * @param string $fieldName Field name
270 */
271 public function requestField( $fieldName ) {
272 $this->mRequestedPageFields[$fieldName] = null;
273 }
274
275 /**
276 * Get the value of a custom field previously requested through
277 * requestField()
278 * @param string $fieldName Field name
279 * @return mixed Field value
280 */
281 public function getCustomField( $fieldName ) {
282 return $this->mRequestedPageFields[$fieldName];
283 }
284
285 /**
286 * Get the fields that have to be queried from the page table:
287 * the ones requested through requestField() and a few basic ones
288 * we always need
289 * @return array Array of field names
290 */
291 public function getPageTableFields() {
292 // Ensure we get minimum required fields
293 // DON'T change this order
294 $pageFlds = array(
295 'page_namespace' => null,
296 'page_title' => null,
297 'page_id' => null,
298 );
299
300 if ( $this->mResolveRedirects ) {
301 $pageFlds['page_is_redirect'] = null;
302 }
303
304 if ( $this->getConfig()->get( 'ContentHandlerUseDB' ) ) {
305 $pageFlds['page_content_model'] = null;
306 }
307
308 if ( $this->getConfig()->get( 'PageLanguageUseDB' ) ) {
309 $pageFlds['page_lang'] = null;
310 }
311
312 // only store non-default fields
313 $this->mRequestedPageFields = array_diff_key( $this->mRequestedPageFields, $pageFlds );
314
315 $pageFlds = array_merge( $pageFlds, $this->mRequestedPageFields );
316
317 return array_keys( $pageFlds );
318 }
319
320 /**
321 * Returns an array [ns][dbkey] => page_id for all requested titles.
322 * page_id is a unique negative number in case title was not found.
323 * Invalid titles will also have negative page IDs and will be in namespace 0
324 * @return array
325 */
326 public function getAllTitlesByNamespace() {
327 return $this->mAllPages;
328 }
329
330 /**
331 * All Title objects provided.
332 * @return Title[]
333 */
334 public function getTitles() {
335 return $this->mTitles;
336 }
337
338 /**
339 * Returns the number of unique pages (not revisions) in the set.
340 * @return int
341 */
342 public function getTitleCount() {
343 return count( $this->mTitles );
344 }
345
346 /**
347 * Returns an array [ns][dbkey] => page_id for all good titles.
348 * @return array
349 */
350 public function getGoodTitlesByNamespace() {
351 return $this->mGoodPages;
352 }
353
354 /**
355 * Title objects that were found in the database.
356 * @return Title[] Array page_id (int) => Title (obj)
357 */
358 public function getGoodTitles() {
359 return $this->mGoodTitles;
360 }
361
362 /**
363 * Returns the number of found unique pages (not revisions) in the set.
364 * @return int
365 */
366 public function getGoodTitleCount() {
367 return count( $this->mGoodTitles );
368 }
369
370 /**
371 * Returns an array [ns][dbkey] => fake_page_id for all missing titles.
372 * fake_page_id is a unique negative number.
373 * @return array
374 */
375 public function getMissingTitlesByNamespace() {
376 return $this->mMissingPages;
377 }
378
379 /**
380 * Title objects that were NOT found in the database.
381 * The array's index will be negative for each item
382 * @return Title[]
383 */
384 public function getMissingTitles() {
385 return $this->mMissingTitles;
386 }
387
388 /**
389 * Returns an array [ns][dbkey] => page_id for all good and missing titles.
390 * @return array
391 */
392 public function getGoodAndMissingTitlesByNamespace() {
393 return $this->mGoodAndMissingPages;
394 }
395
396 /**
397 * Title objects for good and missing titles.
398 * @return array
399 */
400 public function getGoodAndMissingTitles() {
401 return $this->mGoodTitles + $this->mMissingTitles;
402 }
403
404 /**
405 * Titles that were deemed invalid by Title::newFromText()
406 * The array's index will be unique and negative for each item
407 * @deprecated since 1.26, use self::getInvalidTitlesAndReasons()
408 * @return string[] Array of strings (not Title objects)
409 */
410 public function getInvalidTitles() {
411 wfDeprecated( __METHOD__, '1.26' );
412 return array_map( function ( $t ) {
413 return $t['title'];
414 }, $this->mInvalidTitles );
415 }
416
417 /**
418 * Titles that were deemed invalid by Title::newFromText()
419 * The array's index will be unique and negative for each item
420 * @return array[] Array of arrays with 'title' and 'invalidreason' properties
421 */
422 public function getInvalidTitlesAndReasons() {
423 return $this->mInvalidTitles;
424 }
425
426 /**
427 * Page IDs that were not found in the database
428 * @return array Array of page IDs
429 */
430 public function getMissingPageIDs() {
431 return $this->mMissingPageIDs;
432 }
433
434 /**
435 * Get a list of redirect resolutions - maps a title to its redirect
436 * target, as an array of output-ready arrays
437 * @return Title[]
438 */
439 public function getRedirectTitles() {
440 return $this->mRedirectTitles;
441 }
442
443 /**
444 * Get a list of redirect resolutions - maps a title to its redirect
445 * target. Includes generator data for redirect source when available.
446 * @param ApiResult $result
447 * @return array Array of prefixed_title (string) => Title object
448 * @since 1.21
449 */
450 public function getRedirectTitlesAsResult( $result = null ) {
451 $values = array();
452 foreach ( $this->getRedirectTitles() as $titleStrFrom => $titleTo ) {
453 $r = array(
454 'from' => strval( $titleStrFrom ),
455 'to' => $titleTo->getPrefixedText(),
456 );
457 if ( $titleTo->hasFragment() ) {
458 $r['tofragment'] = $titleTo->getFragment();
459 }
460 if ( $titleTo->isExternal() ) {
461 $r['tointerwiki'] = $titleTo->getInterwiki();
462 }
463 if ( isset( $this->mResolvedRedirectTitles[$titleStrFrom] ) ) {
464 $titleFrom = $this->mResolvedRedirectTitles[$titleStrFrom];
465 $ns = $titleFrom->getNamespace();
466 $dbkey = $titleFrom->getDBkey();
467 if ( isset( $this->mGeneratorData[$ns][$dbkey] ) ) {
468 $r = array_merge( $this->mGeneratorData[$ns][$dbkey], $r );
469 }
470 }
471
472 $values[] = $r;
473 }
474 if ( !empty( $values ) && $result ) {
475 ApiResult::setIndexedTagName( $values, 'r' );
476 }
477
478 return $values;
479 }
480
481 /**
482 * Get a list of title normalizations - maps a title to its normalized
483 * version.
484 * @return array Array of raw_prefixed_title (string) => prefixed_title (string)
485 */
486 public function getNormalizedTitles() {
487 return $this->mNormalizedTitles;
488 }
489
490 /**
491 * Get a list of title normalizations - maps a title to its normalized
492 * version in the form of result array.
493 * @param ApiResult $result
494 * @return array Array of raw_prefixed_title (string) => prefixed_title (string)
495 * @since 1.21
496 */
497 public function getNormalizedTitlesAsResult( $result = null ) {
498 $values = array();
499 foreach ( $this->getNormalizedTitles() as $rawTitleStr => $titleStr ) {
500 $values[] = array(
501 'from' => $rawTitleStr,
502 'to' => $titleStr
503 );
504 }
505 if ( !empty( $values ) && $result ) {
506 ApiResult::setIndexedTagName( $values, 'n' );
507 }
508
509 return $values;
510 }
511
512 /**
513 * Get a list of title conversions - maps a title to its converted
514 * version.
515 * @return array Array of raw_prefixed_title (string) => prefixed_title (string)
516 */
517 public function getConvertedTitles() {
518 return $this->mConvertedTitles;
519 }
520
521 /**
522 * Get a list of title conversions - maps a title to its converted
523 * version as a result array.
524 * @param ApiResult $result
525 * @return array Array of (from, to) strings
526 * @since 1.21
527 */
528 public function getConvertedTitlesAsResult( $result = null ) {
529 $values = array();
530 foreach ( $this->getConvertedTitles() as $rawTitleStr => $titleStr ) {
531 $values[] = array(
532 'from' => $rawTitleStr,
533 'to' => $titleStr
534 );
535 }
536 if ( !empty( $values ) && $result ) {
537 ApiResult::setIndexedTagName( $values, 'c' );
538 }
539
540 return $values;
541 }
542
543 /**
544 * Get a list of interwiki titles - maps a title to its interwiki
545 * prefix.
546 * @return array Array of raw_prefixed_title (string) => interwiki_prefix (string)
547 */
548 public function getInterwikiTitles() {
549 return $this->mInterwikiTitles;
550 }
551
552 /**
553 * Get a list of interwiki titles - maps a title to its interwiki
554 * prefix as result.
555 * @param ApiResult $result
556 * @param bool $iwUrl
557 * @return array Array of raw_prefixed_title (string) => interwiki_prefix (string)
558 * @since 1.21
559 */
560 public function getInterwikiTitlesAsResult( $result = null, $iwUrl = false ) {
561 $values = array();
562 foreach ( $this->getInterwikiTitles() as $rawTitleStr => $interwikiStr ) {
563 $item = array(
564 'title' => $rawTitleStr,
565 'iw' => $interwikiStr,
566 );
567 if ( $iwUrl ) {
568 $title = Title::newFromText( $rawTitleStr );
569 $item['url'] = $title->getFullURL( '', false, PROTO_CURRENT );
570 }
571 $values[] = $item;
572 }
573 if ( !empty( $values ) && $result ) {
574 ApiResult::setIndexedTagName( $values, 'i' );
575 }
576
577 return $values;
578 }
579
580 /**
581 * Get an array of invalid/special/missing titles.
582 *
583 * @param array $invalidChecks List of types of invalid titles to include.
584 * Recognized values are:
585 * - invalidTitles: Titles and reasons from $this->getInvalidTitlesAndReasons()
586 * - special: Titles from $this->getSpecialTitles()
587 * - missingIds: ids from $this->getMissingPageIDs()
588 * - missingRevIds: ids from $this->getMissingRevisionIDs()
589 * - missingTitles: Titles from $this->getMissingTitles()
590 * - interwikiTitles: Titles from $this->getInterwikiTitlesAsResult()
591 * @return array Array suitable for inclusion in the response
592 * @since 1.23
593 */
594 public function getInvalidTitlesAndRevisions( $invalidChecks = array( 'invalidTitles',
595 'special', 'missingIds', 'missingRevIds', 'missingTitles', 'interwikiTitles' )
596 ) {
597 $result = array();
598 if ( in_array( "invalidTitles", $invalidChecks ) ) {
599 self::addValues( $result, $this->getInvalidTitlesAndReasons(), 'invalid' );
600 }
601 if ( in_array( "special", $invalidChecks ) ) {
602 self::addValues( $result, $this->getSpecialTitles(), 'special', 'title' );
603 }
604 if ( in_array( "missingIds", $invalidChecks ) ) {
605 self::addValues( $result, $this->getMissingPageIDs(), 'missing', 'pageid' );
606 }
607 if ( in_array( "missingRevIds", $invalidChecks ) ) {
608 self::addValues( $result, $this->getMissingRevisionIDs(), 'missing', 'revid' );
609 }
610 if ( in_array( "missingTitles", $invalidChecks ) ) {
611 self::addValues( $result, $this->getMissingTitles(), 'missing' );
612 }
613 if ( in_array( "interwikiTitles", $invalidChecks ) ) {
614 self::addValues( $result, $this->getInterwikiTitlesAsResult() );
615 }
616
617 return $result;
618 }
619
620 /**
621 * Get the list of valid revision IDs (requested with the revids= parameter)
622 * @return array Array of revID (int) => pageID (int)
623 */
624 public function getRevisionIDs() {
625 return $this->mGoodRevIDs;
626 }
627
628 /**
629 * Get the list of non-deleted revision IDs (requested with the revids= parameter)
630 * @return array Array of revID (int) => pageID (int)
631 */
632 public function getLiveRevisionIDs() {
633 return $this->mLiveRevIDs;
634 }
635
636 /**
637 * Get the list of revision IDs that were associated with deleted titles.
638 * @return array Array of revID (int) => pageID (int)
639 */
640 public function getDeletedRevisionIDs() {
641 return $this->mDeletedRevIDs;
642 }
643
644 /**
645 * Revision IDs that were not found in the database
646 * @return array Array of revision IDs
647 */
648 public function getMissingRevisionIDs() {
649 return $this->mMissingRevIDs;
650 }
651
652 /**
653 * Revision IDs that were not found in the database as result array.
654 * @param ApiResult $result
655 * @return array Array of revision IDs
656 * @since 1.21
657 */
658 public function getMissingRevisionIDsAsResult( $result = null ) {
659 $values = array();
660 foreach ( $this->getMissingRevisionIDs() as $revid ) {
661 $values[$revid] = array(
662 'revid' => $revid
663 );
664 }
665 if ( !empty( $values ) && $result ) {
666 ApiResult::setIndexedTagName( $values, 'rev' );
667 }
668
669 return $values;
670 }
671
672 /**
673 * Get the list of titles with negative namespace
674 * @return Title[]
675 */
676 public function getSpecialTitles() {
677 return $this->mSpecialTitles;
678 }
679
680 /**
681 * Returns the number of revisions (requested with revids= parameter).
682 * @return int Number of revisions.
683 */
684 public function getRevisionCount() {
685 return count( $this->getRevisionIDs() );
686 }
687
688 /**
689 * Populate this PageSet from a list of Titles
690 * @param array $titles Array of Title objects
691 */
692 public function populateFromTitles( $titles ) {
693 $this->initFromTitles( $titles );
694 }
695
696 /**
697 * Populate this PageSet from a list of page IDs
698 * @param array $pageIDs Array of page IDs
699 */
700 public function populateFromPageIDs( $pageIDs ) {
701 $this->initFromPageIds( $pageIDs );
702 }
703
704 /**
705 * Populate this PageSet from a rowset returned from the database
706 *
707 * Note that the query result must include the columns returned by
708 * $this->getPageTableFields().
709 *
710 * @param IDatabase $db
711 * @param ResultWrapper $queryResult Query result object
712 */
713 public function populateFromQueryResult( $db, $queryResult ) {
714 $this->initFromQueryResult( $queryResult );
715 }
716
717 /**
718 * Populate this PageSet from a list of revision IDs
719 * @param array $revIDs Array of revision IDs
720 */
721 public function populateFromRevisionIDs( $revIDs ) {
722 $this->initFromRevIDs( $revIDs );
723 }
724
725 /**
726 * Extract all requested fields from the row received from the database
727 * @param stdClass $row Result row
728 */
729 public function processDbRow( $row ) {
730 // Store Title object in various data structures
731 $title = Title::newFromRow( $row );
732
733 $pageId = intval( $row->page_id );
734 $this->mAllPages[$row->page_namespace][$row->page_title] = $pageId;
735 $this->mTitles[] = $title;
736
737 if ( $this->mResolveRedirects && $row->page_is_redirect == '1' ) {
738 $this->mPendingRedirectIDs[$pageId] = $title;
739 } else {
740 $this->mGoodPages[$row->page_namespace][$row->page_title] = $pageId;
741 $this->mGoodAndMissingPages[$row->page_namespace][$row->page_title] = $pageId;
742 $this->mGoodTitles[$pageId] = $title;
743 }
744
745 foreach ( $this->mRequestedPageFields as $fieldName => &$fieldValues ) {
746 $fieldValues[$pageId] = $row->$fieldName;
747 }
748 }
749
750 /**
751 * Do not use, does nothing, will be removed
752 * @deprecated since 1.21
753 */
754 public function finishPageSetGeneration() {
755 wfDeprecated( __METHOD__, '1.21' );
756 }
757
758 /**
759 * This method populates internal variables with page information
760 * based on the given array of title strings.
761 *
762 * Steps:
763 * #1 For each title, get data from `page` table
764 * #2 If page was not found in the DB, store it as missing
765 *
766 * Additionally, when resolving redirects:
767 * #3 If no more redirects left, stop.
768 * #4 For each redirect, get its target from the `redirect` table.
769 * #5 Substitute the original LinkBatch object with the new list
770 * #6 Repeat from step #1
771 *
772 * @param array $titles Array of Title objects or strings
773 */
774 private function initFromTitles( $titles ) {
775 // Get validated and normalized title objects
776 $linkBatch = $this->processTitlesArray( $titles );
777 if ( $linkBatch->isEmpty() ) {
778 return;
779 }
780
781 $db = $this->getDB();
782 $set = $linkBatch->constructSet( 'page', $db );
783
784 // Get pageIDs data from the `page` table
785 $res = $db->select( 'page', $this->getPageTableFields(), $set,
786 __METHOD__ );
787
788 // Hack: get the ns:titles stored in array(ns => array(titles)) format
789 $this->initFromQueryResult( $res, $linkBatch->data, true ); // process Titles
790
791 // Resolve any found redirects
792 $this->resolvePendingRedirects();
793 }
794
795 /**
796 * Does the same as initFromTitles(), but is based on page IDs instead
797 * @param array $pageids Array of page IDs
798 */
799 private function initFromPageIds( $pageids ) {
800 if ( !$pageids ) {
801 return;
802 }
803
804 $pageids = array_map( 'intval', $pageids ); // paranoia
805 $remaining = array_flip( $pageids );
806
807 $pageids = self::getPositiveIntegers( $pageids );
808
809 $res = null;
810 if ( !empty( $pageids ) ) {
811 $set = array(
812 'page_id' => $pageids
813 );
814 $db = $this->getDB();
815
816 // Get pageIDs data from the `page` table
817 $res = $db->select( 'page', $this->getPageTableFields(), $set,
818 __METHOD__ );
819 }
820
821 $this->initFromQueryResult( $res, $remaining, false ); // process PageIDs
822
823 // Resolve any found redirects
824 $this->resolvePendingRedirects();
825 }
826
827 /**
828 * Iterate through the result of the query on 'page' table,
829 * and for each row create and store title object and save any extra fields requested.
830 * @param ResultWrapper $res DB Query result
831 * @param array $remaining Array of either pageID or ns/title elements (optional).
832 * If given, any missing items will go to $mMissingPageIDs and $mMissingTitles
833 * @param bool $processTitles Must be provided together with $remaining.
834 * If true, treat $remaining as an array of [ns][title]
835 * If false, treat it as an array of [pageIDs]
836 */
837 private function initFromQueryResult( $res, &$remaining = null, $processTitles = null ) {
838 if ( !is_null( $remaining ) && is_null( $processTitles ) ) {
839 ApiBase::dieDebug( __METHOD__, 'Missing $processTitles parameter when $remaining is provided' );
840 }
841
842 $usernames = array();
843 if ( $res ) {
844 foreach ( $res as $row ) {
845 $pageId = intval( $row->page_id );
846
847 // Remove found page from the list of remaining items
848 if ( isset( $remaining ) ) {
849 if ( $processTitles ) {
850 unset( $remaining[$row->page_namespace][$row->page_title] );
851 } else {
852 unset( $remaining[$pageId] );
853 }
854 }
855
856 // Store any extra fields requested by modules
857 $this->processDbRow( $row );
858
859 // Need gender information
860 if ( MWNamespace::hasGenderDistinction( $row->page_namespace ) ) {
861 $usernames[] = $row->page_title;
862 }
863 }
864 }
865
866 if ( isset( $remaining ) ) {
867 // Any items left in the $remaining list are added as missing
868 if ( $processTitles ) {
869 // The remaining titles in $remaining are non-existent pages
870 foreach ( $remaining as $ns => $dbkeys ) {
871 foreach ( array_keys( $dbkeys ) as $dbkey ) {
872 $title = Title::makeTitle( $ns, $dbkey );
873 $this->mAllPages[$ns][$dbkey] = $this->mFakePageId;
874 $this->mMissingPages[$ns][$dbkey] = $this->mFakePageId;
875 $this->mGoodAndMissingPages[$ns][$dbkey] = $this->mFakePageId;
876 $this->mMissingTitles[$this->mFakePageId] = $title;
877 $this->mFakePageId--;
878 $this->mTitles[] = $title;
879
880 // need gender information
881 if ( MWNamespace::hasGenderDistinction( $ns ) ) {
882 $usernames[] = $dbkey;
883 }
884 }
885 }
886 } else {
887 // The remaining pageids do not exist
888 if ( !$this->mMissingPageIDs ) {
889 $this->mMissingPageIDs = array_keys( $remaining );
890 } else {
891 $this->mMissingPageIDs = array_merge( $this->mMissingPageIDs, array_keys( $remaining ) );
892 }
893 }
894 }
895
896 // Get gender information
897 $genderCache = GenderCache::singleton();
898 $genderCache->doQuery( $usernames, __METHOD__ );
899 }
900
901 /**
902 * Does the same as initFromTitles(), but is based on revision IDs
903 * instead
904 * @param array $revids Array of revision IDs
905 */
906 private function initFromRevIDs( $revids ) {
907 if ( !$revids ) {
908 return;
909 }
910
911 $revids = array_map( 'intval', $revids ); // paranoia
912 $db = $this->getDB();
913 $pageids = array();
914 $remaining = array_flip( $revids );
915
916 $revids = self::getPositiveIntegers( $revids );
917
918 if ( !empty( $revids ) ) {
919 $tables = array( 'revision', 'page' );
920 $fields = array( 'rev_id', 'rev_page' );
921 $where = array( 'rev_id' => $revids, 'rev_page = page_id' );
922
923 // Get pageIDs data from the `page` table
924 $res = $db->select( $tables, $fields, $where, __METHOD__ );
925 foreach ( $res as $row ) {
926 $revid = intval( $row->rev_id );
927 $pageid = intval( $row->rev_page );
928 $this->mGoodRevIDs[$revid] = $pageid;
929 $this->mLiveRevIDs[$revid] = $pageid;
930 $pageids[$pageid] = '';
931 unset( $remaining[$revid] );
932 }
933 }
934
935 $this->mMissingRevIDs = array_keys( $remaining );
936
937 // Populate all the page information
938 $this->initFromPageIds( array_keys( $pageids ) );
939
940 // If the user can see deleted revisions, pull out the corresponding
941 // titles from the archive table and include them too. We ignore
942 // ar_page_id because deleted revisions are tied by title, not page_id.
943 if ( !empty( $this->mMissingRevIDs ) && $this->getUser()->isAllowed( 'deletedhistory' ) ) {
944 $remaining = array_flip( $this->mMissingRevIDs );
945 $tables = array( 'archive' );
946 $fields = array( 'ar_rev_id', 'ar_namespace', 'ar_title' );
947 $where = array( 'ar_rev_id' => $this->mMissingRevIDs );
948
949 $res = $db->select( $tables, $fields, $where, __METHOD__ );
950 $titles = array();
951 foreach ( $res as $row ) {
952 $revid = intval( $row->ar_rev_id );
953 $titles[$revid] = Title::makeTitle( $row->ar_namespace, $row->ar_title );
954 unset( $remaining[$revid] );
955 }
956
957 $this->initFromTitles( $titles );
958
959 foreach ( $titles as $revid => $title ) {
960 $ns = $title->getNamespace();
961 $dbkey = $title->getDBkey();
962
963 // Handle converted titles
964 if ( !isset( $this->mAllPages[$ns][$dbkey] ) &&
965 isset( $this->mConvertedTitles[$title->getPrefixedText()] )
966 ) {
967 $title = Title::newFromText( $this->mConvertedTitles[$title->getPrefixedText()] );
968 $ns = $title->getNamespace();
969 $dbkey = $title->getDBkey();
970 }
971
972 if ( isset( $this->mAllPages[$ns][$dbkey] ) ) {
973 $this->mGoodRevIDs[$revid] = $this->mAllPages[$ns][$dbkey];
974 $this->mDeletedRevIDs[$revid] = $this->mAllPages[$ns][$dbkey];
975 } else {
976 $remaining[$revid] = true;
977 }
978 }
979
980 $this->mMissingRevIDs = array_keys( $remaining );
981 }
982 }
983
984 /**
985 * Resolve any redirects in the result if redirect resolution was
986 * requested. This function is called repeatedly until all redirects
987 * have been resolved.
988 */
989 private function resolvePendingRedirects() {
990 if ( $this->mResolveRedirects ) {
991 $db = $this->getDB();
992 $pageFlds = $this->getPageTableFields();
993
994 // Repeat until all redirects have been resolved
995 // The infinite loop is prevented by keeping all known pages in $this->mAllPages
996 while ( $this->mPendingRedirectIDs ) {
997 // Resolve redirects by querying the pagelinks table, and repeat the process
998 // Create a new linkBatch object for the next pass
999 $linkBatch = $this->getRedirectTargets();
1000
1001 if ( $linkBatch->isEmpty() ) {
1002 break;
1003 }
1004
1005 $set = $linkBatch->constructSet( 'page', $db );
1006 if ( $set === false ) {
1007 break;
1008 }
1009
1010 // Get pageIDs data from the `page` table
1011 $res = $db->select( 'page', $pageFlds, $set, __METHOD__ );
1012
1013 // Hack: get the ns:titles stored in array(ns => array(titles)) format
1014 $this->initFromQueryResult( $res, $linkBatch->data, true );
1015 }
1016 }
1017 }
1018
1019 /**
1020 * Get the targets of the pending redirects from the database
1021 *
1022 * Also creates entries in the redirect table for redirects that don't
1023 * have one.
1024 * @return LinkBatch
1025 */
1026 private function getRedirectTargets() {
1027 $lb = new LinkBatch();
1028 $db = $this->getDB();
1029
1030 $res = $db->select(
1031 'redirect',
1032 array(
1033 'rd_from',
1034 'rd_namespace',
1035 'rd_fragment',
1036 'rd_interwiki',
1037 'rd_title'
1038 ), array( 'rd_from' => array_keys( $this->mPendingRedirectIDs ) ),
1039 __METHOD__
1040 );
1041 foreach ( $res as $row ) {
1042 $rdfrom = intval( $row->rd_from );
1043 $from = $this->mPendingRedirectIDs[$rdfrom]->getPrefixedText();
1044 $to = Title::makeTitle(
1045 $row->rd_namespace,
1046 $row->rd_title,
1047 $row->rd_fragment,
1048 $row->rd_interwiki
1049 );
1050 $this->mResolvedRedirectTitles[$from] = $this->mPendingRedirectIDs[$rdfrom];
1051 unset( $this->mPendingRedirectIDs[$rdfrom] );
1052 if ( $to->isExternal() ) {
1053 $this->mInterwikiTitles[$to->getPrefixedText()] = $to->getInterwiki();
1054 } elseif ( !isset( $this->mAllPages[$row->rd_namespace][$row->rd_title] ) ) {
1055 $lb->add( $row->rd_namespace, $row->rd_title );
1056 }
1057 $this->mRedirectTitles[$from] = $to;
1058 }
1059
1060 if ( $this->mPendingRedirectIDs ) {
1061 // We found pages that aren't in the redirect table
1062 // Add them
1063 foreach ( $this->mPendingRedirectIDs as $id => $title ) {
1064 $page = WikiPage::factory( $title );
1065 $rt = $page->insertRedirect();
1066 if ( !$rt ) {
1067 // What the hell. Let's just ignore this
1068 continue;
1069 }
1070 $lb->addObj( $rt );
1071 $from = $title->getPrefixedText();
1072 $this->mResolvedRedirectTitles[$from] = $title;
1073 $this->mRedirectTitles[$from] = $rt;
1074 unset( $this->mPendingRedirectIDs[$id] );
1075 }
1076 }
1077
1078 return $lb;
1079 }
1080
1081 /**
1082 * Get the cache mode for the data generated by this module.
1083 * All PageSet users should take into account whether this returns a more-restrictive
1084 * cache mode than the using module itself. For possible return values and other
1085 * details about cache modes, see ApiMain::setCacheMode()
1086 *
1087 * Public caching will only be allowed if *all* the modules that supply
1088 * data for a given request return a cache mode of public.
1089 *
1090 * @param array|null $params
1091 * @return string
1092 * @since 1.21
1093 */
1094 public function getCacheMode( $params = null ) {
1095 return $this->mCacheMode;
1096 }
1097
1098 /**
1099 * Given an array of title strings, convert them into Title objects.
1100 * Alternatively, an array of Title objects may be given.
1101 * This method validates access rights for the title,
1102 * and appends normalization values to the output.
1103 *
1104 * @param array $titles Array of Title objects or strings
1105 * @return LinkBatch
1106 */
1107 private function processTitlesArray( $titles ) {
1108 $usernames = array();
1109 $linkBatch = new LinkBatch();
1110
1111 foreach ( $titles as $title ) {
1112 if ( is_string( $title ) ) {
1113 try {
1114 $titleObj = Title::newFromTextThrow( $title, $this->mDefaultNamespace );
1115 } catch ( MalformedTitleException $ex ) {
1116 // Handle invalid titles gracefully
1117 $this->mAllPages[0][$title] = $this->mFakePageId;
1118 $this->mInvalidTitles[$this->mFakePageId] = array(
1119 'title' => $title,
1120 'invalidreason' => $ex->getMessage(),
1121 );
1122 $this->mFakePageId--;
1123 continue; // There's nothing else we can do
1124 }
1125 } else {
1126 $titleObj = $title;
1127 }
1128 $unconvertedTitle = $titleObj->getPrefixedText();
1129 $titleWasConverted = false;
1130 if ( $titleObj->isExternal() ) {
1131 // This title is an interwiki link.
1132 $this->mInterwikiTitles[$unconvertedTitle] = $titleObj->getInterwiki();
1133 } else {
1134 // Variants checking
1135 global $wgContLang;
1136 if ( $this->mConvertTitles &&
1137 count( $wgContLang->getVariants() ) > 1 &&
1138 !$titleObj->exists()
1139 ) {
1140 // Language::findVariantLink will modify titleText and titleObj into
1141 // the canonical variant if possible
1142 $titleText = is_string( $title ) ? $title : $titleObj->getPrefixedText();
1143 $wgContLang->findVariantLink( $titleText, $titleObj );
1144 $titleWasConverted = $unconvertedTitle !== $titleObj->getPrefixedText();
1145 }
1146
1147 if ( $titleObj->getNamespace() < 0 ) {
1148 // Handle Special and Media pages
1149 $titleObj = $titleObj->fixSpecialName();
1150 $this->mSpecialTitles[$this->mFakePageId] = $titleObj;
1151 $this->mFakePageId--;
1152 } else {
1153 // Regular page
1154 $linkBatch->addObj( $titleObj );
1155 }
1156 }
1157
1158 // Make sure we remember the original title that was
1159 // given to us. This way the caller can correlate new
1160 // titles with the originally requested when e.g. the
1161 // namespace is localized or the capitalization is
1162 // different
1163 if ( $titleWasConverted ) {
1164 $this->mConvertedTitles[$unconvertedTitle] = $titleObj->getPrefixedText();
1165 // In this case the page can't be Special.
1166 if ( is_string( $title ) && $title !== $unconvertedTitle ) {
1167 $this->mNormalizedTitles[$title] = $unconvertedTitle;
1168 }
1169 } elseif ( is_string( $title ) && $title !== $titleObj->getPrefixedText() ) {
1170 $this->mNormalizedTitles[$title] = $titleObj->getPrefixedText();
1171 }
1172
1173 // Need gender information
1174 if ( MWNamespace::hasGenderDistinction( $titleObj->getNamespace() ) ) {
1175 $usernames[] = $titleObj->getText();
1176 }
1177 }
1178 // Get gender information
1179 $genderCache = GenderCache::singleton();
1180 $genderCache->doQuery( $usernames, __METHOD__ );
1181
1182 return $linkBatch;
1183 }
1184
1185 /**
1186 * Set data for a title.
1187 *
1188 * This data may be extracted into an ApiResult using
1189 * self::populateGeneratorData. This should generally be limited to
1190 * data that is likely to be particularly useful to end users rather than
1191 * just being a dump of everything returned in non-generator mode.
1192 *
1193 * Redirects here will *not* be followed, even if 'redirects' was
1194 * specified, since in the case of multiple redirects we can't know which
1195 * source's data to use on the target.
1196 *
1197 * @param Title $title
1198 * @param array $data
1199 */
1200 public function setGeneratorData( Title $title, array $data ) {
1201 $ns = $title->getNamespace();
1202 $dbkey = $title->getDBkey();
1203 $this->mGeneratorData[$ns][$dbkey] = $data;
1204 }
1205
1206 /**
1207 * Controls how generator data about a redirect source is merged into
1208 * the generator data for the redirect target. When not set no data
1209 * is merged. Note that if multiple titles redirect to the same target
1210 * the order of operations is undefined.
1211 *
1212 * Example to include generated data from redirect in target, prefering
1213 * the data generated for the destination when there is a collision:
1214 * @code
1215 * $pageSet->setRedirectMergePolicy( function( array $current, array $new ) {
1216 * return $current + $new;
1217 * } );
1218 * @endcode
1219 *
1220 * @param callable|null $callable Recieves two array arguments, first the
1221 * generator data for the redirect target and second the generator data
1222 * for the redirect source. Returns the resulting generator data to use
1223 * for the redirect target.
1224 */
1225 public function setRedirectMergePolicy( $callable ) {
1226 $this->mRedirectMergePolicy = $callable;
1227 }
1228
1229 /**
1230 * Populate the generator data for all titles in the result
1231 *
1232 * The page data may be inserted into an ApiResult object or into an
1233 * associative array. The $path parameter specifies the path within the
1234 * ApiResult or array to find the "pages" node.
1235 *
1236 * The "pages" node itself must be an associative array mapping the page ID
1237 * or fake page ID values returned by this pageset (see
1238 * self::getAllTitlesByNamespace() and self::getSpecialTitles()) to
1239 * associative arrays of page data. Each of those subarrays will have the
1240 * data from self::setGeneratorData() merged in.
1241 *
1242 * Data that was set by self::setGeneratorData() for pages not in the
1243 * "pages" node will be ignored.
1244 *
1245 * @param ApiResult|array &$result
1246 * @param array $path
1247 * @return bool Whether the data fit
1248 */
1249 public function populateGeneratorData( &$result, array $path = array() ) {
1250 if ( $result instanceof ApiResult ) {
1251 $data = $result->getResultData( $path );
1252 if ( $data === null ) {
1253 return true;
1254 }
1255 } else {
1256 $data = &$result;
1257 foreach ( $path as $key ) {
1258 if ( !isset( $data[$key] ) ) {
1259 // Path isn't in $result, so nothing to add, so everything
1260 // "fits"
1261 return true;
1262 }
1263 $data = &$data[$key];
1264 }
1265 }
1266 foreach ( $this->mGeneratorData as $ns => $dbkeys ) {
1267 if ( $ns === -1 ) {
1268 $pages = array();
1269 foreach ( $this->mSpecialTitles as $id => $title ) {
1270 $pages[$title->getDBkey()] = $id;
1271 }
1272 } else {
1273 if ( !isset( $this->mAllPages[$ns] ) ) {
1274 // No known titles in the whole namespace. Skip it.
1275 continue;
1276 }
1277 $pages = $this->mAllPages[$ns];
1278 }
1279 foreach ( $dbkeys as $dbkey => $genData ) {
1280 if ( !isset( $pages[$dbkey] ) ) {
1281 // Unknown title. Forget it.
1282 continue;
1283 }
1284 $pageId = $pages[$dbkey];
1285 if ( !isset( $data[$pageId] ) ) {
1286 // $pageId didn't make it into the result. Ignore it.
1287 continue;
1288 }
1289
1290 if ( $result instanceof ApiResult ) {
1291 $path2 = array_merge( $path, array( $pageId ) );
1292 foreach ( $genData as $key => $value ) {
1293 if ( !$result->addValue( $path2, $key, $value ) ) {
1294 return false;
1295 }
1296 }
1297 } else {
1298 $data[$pageId] = array_merge( $data[$pageId], $genData );
1299 }
1300 }
1301 }
1302
1303 // Merge data generated about redirect titles into the redirect destination
1304 if ( $this->mRedirectMergePolicy ) {
1305 foreach ( $this->mResolvedRedirectTitles as $titleFrom ) {
1306 $dest = $titleFrom;
1307 while ( isset( $this->mRedirectTitles[$dest->getPrefixedText()] ) ) {
1308 $dest = $this->mRedirectTitles[$dest->getPrefixedText()];
1309 }
1310 $fromNs = $titleFrom->getNamespace();
1311 $fromDBkey = $titleFrom->getDBkey();
1312 $toPageId = $dest->getArticleID();
1313 if ( isset( $data[$toPageId] ) &&
1314 isset( $this->mGeneratorData[$fromNs][$fromDBkey] )
1315 ) {
1316 // It is necesary to set both $data and add to $result, if an ApiResult,
1317 // to ensure multiple redirects to the same destination are all merged.
1318 $data[$toPageId] = call_user_func(
1319 $this->mRedirectMergePolicy,
1320 $data[$toPageId],
1321 $this->mGeneratorData[$fromNs][$fromDBkey]
1322 );
1323 if ( $result instanceof ApiResult ) {
1324 if ( !$result->addValue( $path, $toPageId, $data[$toPageId], ApiResult::OVERRIDE ) ) {
1325 return false;
1326 }
1327 }
1328 }
1329 }
1330 }
1331
1332 return true;
1333 }
1334
1335 /**
1336 * Get the database connection (read-only)
1337 * @return DatabaseBase
1338 */
1339 protected function getDB() {
1340 return $this->mDbSource->getDB();
1341 }
1342
1343 /**
1344 * Returns the input array of integers with all values < 0 removed
1345 *
1346 * @param array $array
1347 * @return array
1348 */
1349 private static function getPositiveIntegers( $array ) {
1350 // bug 25734 API: possible issue with revids validation
1351 // It seems with a load of revision rows, MySQL gets upset
1352 // Remove any < 0 integers, as they can't be valid
1353 foreach ( $array as $i => $int ) {
1354 if ( $int < 0 ) {
1355 unset( $array[$i] );
1356 }
1357 }
1358
1359 return $array;
1360 }
1361
1362 public function getAllowedParams( $flags = 0 ) {
1363 $result = array(
1364 'titles' => array(
1365 ApiBase::PARAM_ISMULTI => true,
1366 ApiBase::PARAM_HELP_MSG => 'api-pageset-param-titles',
1367 ),
1368 'pageids' => array(
1369 ApiBase::PARAM_TYPE => 'integer',
1370 ApiBase::PARAM_ISMULTI => true,
1371 ApiBase::PARAM_HELP_MSG => 'api-pageset-param-pageids',
1372 ),
1373 'revids' => array(
1374 ApiBase::PARAM_TYPE => 'integer',
1375 ApiBase::PARAM_ISMULTI => true,
1376 ApiBase::PARAM_HELP_MSG => 'api-pageset-param-revids',
1377 ),
1378 'generator' => array(
1379 ApiBase::PARAM_TYPE => null,
1380 ApiBase::PARAM_HELP_MSG => 'api-pageset-param-generator',
1381 ApiBase::PARAM_SUBMODULE_PARAM_PREFIX => 'g',
1382 ),
1383 'redirects' => array(
1384 ApiBase::PARAM_DFLT => false,
1385 ApiBase::PARAM_HELP_MSG => $this->mAllowGenerator
1386 ? 'api-pageset-param-redirects-generator'
1387 : 'api-pageset-param-redirects-nogenerator',
1388 ),
1389 'converttitles' => array(
1390 ApiBase::PARAM_DFLT => false,
1391 ApiBase::PARAM_HELP_MSG => array(
1392 'api-pageset-param-converttitles',
1393 new DeferredStringifier(
1394 function ( IContextSource $context ) {
1395 return $context->getLanguage()
1396 ->commaList( LanguageConverter::$languagesWithVariants );
1397 },
1398 $this
1399 )
1400 ),
1401 ),
1402 );
1403
1404 if ( !$this->mAllowGenerator ) {
1405 unset( $result['generator'] );
1406 } elseif ( $flags & ApiBase::GET_VALUES_FOR_HELP ) {
1407 $result['generator'][ApiBase::PARAM_TYPE] = 'submodule';
1408 $result['generator'][ApiBase::PARAM_SUBMODULE_MAP] = $this->getGenerators();
1409 }
1410
1411 return $result;
1412 }
1413
1414 private static $generators = null;
1415
1416 /**
1417 * Get an array of all available generators
1418 * @return array
1419 */
1420 private function getGenerators() {
1421 if ( self::$generators === null ) {
1422 $query = $this->mDbSource;
1423 if ( !( $query instanceof ApiQuery ) ) {
1424 // If the parent container of this pageset is not ApiQuery,
1425 // we must create it to get module manager
1426 $query = $this->getMain()->getModuleManager()->getModule( 'query' );
1427 }
1428 $gens = array();
1429 $prefix = $query->getModulePath() . '+';
1430 $mgr = $query->getModuleManager();
1431 foreach ( $mgr->getNamesWithClasses() as $name => $class ) {
1432 if ( is_subclass_of( $class, 'ApiQueryGeneratorBase' ) ) {
1433 $gens[$name] = $prefix . $name;
1434 }
1435 }
1436 ksort( $gens );
1437 self::$generators = $gens;
1438 }
1439
1440 return self::$generators;
1441 }
1442 }