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