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