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