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