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