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