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