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