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