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