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