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