Followup r70460/r70461
[lhc/web/wiklou.git] / includes / api / ApiPageSet.php
1 <?php
2
3 /**
4 * Created on Sep 24, 2006
5 *
6 * API for MediaWiki 1.8+
7 *
8 * Copyright © 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 * http://www.gnu.org/copyleft/gpl.html
24 */
25
26 if ( !defined( 'MEDIAWIKI' ) ) {
27 // Eclipse helper - will be ignored in production
28 require_once( 'ApiQueryBase.php' );
29 }
30
31 /**
32 * This class contains a list of pages that the client has requested.
33 * Initially, when the client passes in titles=, pageids=, or revisions=
34 * parameter, an instance of the ApiPageSet class will normalize titles,
35 * determine if the pages/revisions exist, and prefetch any additional page
36 * data requested.
37 *
38 * When a generator is used, the result of the generator will become the input
39 * for the second instance of this class, and all subsequent actions will use
40 * the second instance for all their work.
41 *
42 * @ingroup API
43 */
44 class ApiPageSet extends ApiQueryBase {
45
46 private $mAllPages; // [ns][dbkey] => page_id or negative when missing
47 private $mTitles, $mGoodTitles, $mMissingTitles, $mInvalidTitles;
48 private $mMissingPageIDs, $mRedirectTitles, $mSpecialTitles;
49 private $mNormalizedTitles, $mInterwikiTitles;
50 private $mResolveRedirects, $mPendingRedirectIDs;
51 private $mConvertTitles, $mConvertedTitles;
52 private $mGoodRevIDs, $mMissingRevIDs;
53 private $mFakePageId;
54
55 private $mRequestedPageFields;
56
57 /**
58 * Constructor
59 * @param $query ApiQuery
60 * @param $resolveRedirects bool Whether redirects should be resolved
61 */
62 public function __construct( $query, $resolveRedirects = false, $convertTitles = false ) {
63 parent::__construct( $query, 'query' );
64
65 $this->mAllPages = array();
66 $this->mTitles = array();
67 $this->mGoodTitles = array();
68 $this->mMissingTitles = array();
69 $this->mInvalidTitles = array();
70 $this->mMissingPageIDs = array();
71 $this->mRedirectTitles = array();
72 $this->mNormalizedTitles = array();
73 $this->mInterwikiTitles = array();
74 $this->mGoodRevIDs = array();
75 $this->mMissingRevIDs = array();
76 $this->mSpecialTitles = array();
77
78 $this->mRequestedPageFields = array();
79 $this->mResolveRedirects = $resolveRedirects;
80 if ( $resolveRedirects ) {
81 $this->mPendingRedirectIDs = array();
82 }
83
84 $this->mConvertTitles = $convertTitles;
85 $this->mConvertedTitles = array();
86
87 $this->mFakePageId = - 1;
88 }
89
90 /**
91 * Check whether this PageSet is resolving redirects
92 * @return bool
93 */
94 public function isResolvingRedirects() {
95 return $this->mResolveRedirects;
96 }
97
98 /**
99 * Request an additional field from the page table. Must be called
100 * before execute()
101 * @param $fieldName string Field name
102 */
103 public function requestField( $fieldName ) {
104 $this->mRequestedPageFields[$fieldName] = null;
105 }
106
107 /**
108 * Get the value of a custom field previously requested through
109 * requestField()
110 * @param $fieldName string Field name
111 * @return mixed Field value
112 */
113 public function getCustomField( $fieldName ) {
114 return $this->mRequestedPageFields[$fieldName];
115 }
116
117 /**
118 * Get the fields that have to be queried from the page table:
119 * the ones requested through requestField() and a few basic ones
120 * we always need
121 * @return array of field names
122 */
123 public function getPageTableFields() {
124 // Ensure we get minimum required fields
125 // DON'T change this order
126 $pageFlds = array(
127 'page_namespace' => null,
128 'page_title' => null,
129 'page_id' => null,
130 );
131
132 if ( $this->mResolveRedirects ) {
133 $pageFlds['page_is_redirect'] = null;
134 }
135
136 // only store non-default fields
137 $this->mRequestedPageFields = array_diff_key( $this->mRequestedPageFields, $pageFlds );
138
139 $pageFlds = array_merge( $pageFlds, $this->mRequestedPageFields );
140 return array_keys( $pageFlds );
141 }
142
143 /**
144 * Returns an array [ns][dbkey] => page_id for all requested titles.
145 * page_id is a unique negative number in case title was not found.
146 * Invalid titles will also have negative page IDs and will be in namespace 0
147 * @return array
148 */
149 public function getAllTitlesByNamespace() {
150 return $this->mAllPages;
151 }
152
153 /**
154 * All Title objects provided.
155 * @return array of Title objects
156 */
157 public function getTitles() {
158 return $this->mTitles;
159 }
160
161 /**
162 * Returns the number of unique pages (not revisions) in the set.
163 * @return int
164 */
165 public function getTitleCount() {
166 return count( $this->mTitles );
167 }
168
169 /**
170 * Title objects that were found in the database.
171 * @return array page_id (int) => Title (obj)
172 */
173 public function getGoodTitles() {
174 return $this->mGoodTitles;
175 }
176
177 /**
178 * Returns the number of found unique pages (not revisions) in the set.
179 * @return int
180 */
181 public function getGoodTitleCount() {
182 return count( $this->mGoodTitles );
183 }
184
185 /**
186 * Title objects that were NOT found in the database.
187 * The array's index will be negative for each item
188 * @return array of Title objects
189 */
190 public function getMissingTitles() {
191 return $this->mMissingTitles;
192 }
193
194 /**
195 * Titles that were deemed invalid by Title::newFromText()
196 * The array's index will be unique and negative for each item
197 * @return array of strings (not Title objects)
198 */
199 public function getInvalidTitles() {
200 return $this->mInvalidTitles;
201 }
202
203 /**
204 * Page IDs that were not found in the database
205 * @return array of page IDs
206 */
207 public function getMissingPageIDs() {
208 return $this->mMissingPageIDs;
209 }
210
211 /**
212 * Get a list of redirect resolutions - maps a title to its redirect
213 * target.
214 * @return array prefixed_title (string) => prefixed_title (string)
215 */
216 public function getRedirectTitles() {
217 return $this->mRedirectTitles;
218 }
219
220 /**
221 * Get a list of title normalizations - maps a title to its normalized
222 * version.
223 * @return array raw_prefixed_title (string) => prefixed_title (string)
224 */
225 public function getNormalizedTitles() {
226 return $this->mNormalizedTitles;
227 }
228
229 /**
230 * Get a list of title conversions - maps a title to its converted
231 * version.
232 * @return array raw_prefixed_title (string) => prefixed_title (string)
233 */
234 public function getConvertedTitles() {
235 return $this->mConvertedTitles;
236 }
237
238 /**
239 * Get a list of interwiki titles - maps a title to its interwiki
240 * prefix.
241 * @return array raw_prefixed_title (string) => interwiki_prefix (string)
242 */
243 public function getInterwikiTitles() {
244 return $this->mInterwikiTitles;
245 }
246
247 /**
248 * Get the list of revision IDs (requested with the revids= parameter)
249 * @return array revID (int) => pageID (int)
250 */
251 public function getRevisionIDs() {
252 return $this->mGoodRevIDs;
253 }
254
255 /**
256 * Revision IDs that were not found in the database
257 * @return array of revision IDs
258 */
259 public function getMissingRevisionIDs() {
260 return $this->mMissingRevIDs;
261 }
262
263 /**
264 * Get the list of titles with negative namespace
265 * @return array Title
266 */
267 public function getSpecialTitles() {
268 return $this->mSpecialTitles;
269 }
270
271 /**
272 * Returns the number of revisions (requested with revids= parameter)\
273 * @return int
274 */
275 public function getRevisionCount() {
276 return count( $this->getRevisionIDs() );
277 }
278
279 /**
280 * Populate the PageSet from the request parameters.
281 */
282 public function execute() {
283 $this->profileIn();
284 $params = $this->extractRequestParams();
285
286 // Only one of the titles/pageids/revids is allowed at the same time
287 $dataSource = null;
288 if ( isset( $params['titles'] ) ) {
289 $dataSource = 'titles';
290 }
291 if ( isset( $params['pageids'] ) ) {
292 if ( isset( $dataSource ) ) {
293 $this->dieUsage( "Cannot use 'pageids' at the same time as '$dataSource'", 'multisource' );
294 }
295 $dataSource = 'pageids';
296 }
297 if ( isset( $params['revids'] ) ) {
298 if ( isset( $dataSource ) ) {
299 $this->dieUsage( "Cannot use 'revids' at the same time as '$dataSource'", 'multisource' );
300 }
301 $dataSource = 'revids';
302 }
303
304 switch ( $dataSource ) {
305 case 'titles':
306 $this->initFromTitles( $params['titles'] );
307 break;
308 case 'pageids':
309 $this->initFromPageIds( $params['pageids'] );
310 break;
311 case 'revids':
312 if ( $this->mResolveRedirects ) {
313 $this->setWarning( 'Redirect resolution cannot be used together with the revids= parameter. ' .
314 'Any redirects the revids= point to have not been resolved.' );
315 }
316 $this->mResolveRedirects = false;
317 $this->initFromRevIDs( $params['revids'] );
318 break;
319 default:
320 // Do nothing - some queries do not need any of the data sources.
321 break;
322 }
323 $this->profileOut();
324 }
325
326 /**
327 * Populate this PageSet from a list of Titles
328 * @param $titles array of Title objects
329 */
330 public function populateFromTitles( $titles ) {
331 $this->profileIn();
332 $this->initFromTitles( $titles );
333 $this->profileOut();
334 }
335
336 /**
337 * Populate this PageSet from a list of page IDs
338 * @param $pageIDs array of page IDs
339 */
340 public function populateFromPageIDs( $pageIDs ) {
341 $this->profileIn();
342 $this->initFromPageIds( $pageIDs );
343 $this->profileOut();
344 }
345
346 /**
347 * Populate this PageSet from a rowset returned from the database
348 * @param $db Database object
349 * @param $queryResult Query result object
350 */
351 public function populateFromQueryResult( $db, $queryResult ) {
352 $this->profileIn();
353 $this->initFromQueryResult( $db, $queryResult );
354 $this->profileOut();
355 }
356
357 /**
358 * Populate this PageSet from a list of revision IDs
359 * @param $revIDs array of revision IDs
360 */
361 public function populateFromRevisionIDs( $revIDs ) {
362 $this->profileIn();
363 $this->initFromRevIDs( $revIDs );
364 $this->profileOut();
365 }
366
367 /**
368 * Extract all requested fields from the row received from the database
369 * @param $row Result row
370 */
371 public function processDbRow( $row ) {
372 // Store Title object in various data structures
373 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
374
375 $pageId = intval( $row->page_id );
376 $this->mAllPages[$row->page_namespace][$row->page_title] = $pageId;
377 $this->mTitles[] = $title;
378
379 if ( $this->mResolveRedirects && $row->page_is_redirect == '1' ) {
380 $this->mPendingRedirectIDs[$pageId] = $title;
381 } else {
382 $this->mGoodTitles[$pageId] = $title;
383 }
384
385 foreach ( $this->mRequestedPageFields as $fieldName => &$fieldValues ) {
386 $fieldValues[$pageId] = $row-> $fieldName;
387 }
388 }
389
390 /**
391 * Resolve redirects, if applicable
392 */
393 public function finishPageSetGeneration() {
394 $this->profileIn();
395 $this->resolvePendingRedirects();
396 $this->profileOut();
397 }
398
399 /**
400 * This method populates internal variables with page information
401 * based on the given array of title strings.
402 *
403 * Steps:
404 * #1 For each title, get data from `page` table
405 * #2 If page was not found in the DB, store it as missing
406 *
407 * Additionally, when resolving redirects:
408 * #3 If no more redirects left, stop.
409 * #4 For each redirect, get its target from the `redirect` table.
410 * #5 Substitute the original LinkBatch object with the new list
411 * #6 Repeat from step #1
412 *
413 * @param $titles array of Title objects or strings
414 */
415 private function initFromTitles( $titles ) {
416 // Get validated and normalized title objects
417 $linkBatch = $this->processTitlesArray( $titles );
418 if ( $linkBatch->isEmpty() ) {
419 return;
420 }
421
422 $db = $this->getDB();
423 $set = $linkBatch->constructSet( 'page', $db );
424
425 // Get pageIDs data from the `page` table
426 $this->profileDBIn();
427 $res = $db->select( 'page', $this->getPageTableFields(), $set,
428 __METHOD__ );
429 $this->profileDBOut();
430
431 // Hack: get the ns:titles stored in array(ns => array(titles)) format
432 $this->initFromQueryResult( $db, $res, $linkBatch->data, true ); // process Titles
433
434 // Resolve any found redirects
435 $this->resolvePendingRedirects();
436 }
437
438 /**
439 * Does the same as initFromTitles(), but is based on page IDs instead
440 * @param $pageids array of page IDs
441 */
442 private function initFromPageIds( $pageids ) {
443 if ( !count( $pageids ) ) {
444 return;
445 }
446
447 $pageids = array_map( 'intval', $pageids ); // paranoia
448 $set = array(
449 'page_id' => $pageids
450 );
451 $db = $this->getDB();
452
453 // Get pageIDs data from the `page` table
454 $this->profileDBIn();
455 $res = $db->select( 'page', $this->getPageTableFields(), $set,
456 __METHOD__ );
457 $this->profileDBOut();
458
459 $remaining = array_flip( $pageids );
460 $this->initFromQueryResult( $db, $res, $remaining, false ); // process PageIDs
461
462 // Resolve any found redirects
463 $this->resolvePendingRedirects();
464 }
465
466 /**
467 * Iterate through the result of the query on 'page' table,
468 * and for each row create and store title object and save any extra fields requested.
469 * @param $db Database
470 * @param $res DB Query result
471 * @param $remaining array of either pageID or ns/title elements (optional).
472 * If given, any missing items will go to $mMissingPageIDs and $mMissingTitles
473 * @param $processTitles bool Must be provided together with $remaining.
474 * If true, treat $remaining as an array of [ns][title]
475 * If false, treat it as an array of [pageIDs]
476 */
477 private function initFromQueryResult( $db, $res, &$remaining = null, $processTitles = null ) {
478 if ( !is_null( $remaining ) && is_null( $processTitles ) ) {
479 ApiBase::dieDebug( __METHOD__, 'Missing $processTitles parameter when $remaining is provided' );
480 }
481
482 foreach ( $res as $row ) {
483 $pageId = intval( $row->page_id );
484
485 // Remove found page from the list of remaining items
486 if ( isset( $remaining ) ) {
487 if ( $processTitles ) {
488 unset( $remaining[$row->page_namespace][$row->page_title] );
489 } else {
490 unset( $remaining[$pageId] );
491 }
492 }
493
494 // Store any extra fields requested by modules
495 $this->processDbRow( $row );
496 }
497
498 if ( isset( $remaining ) ) {
499 // Any items left in the $remaining list are added as missing
500 if ( $processTitles ) {
501 // The remaining titles in $remaining are non-existent pages
502 foreach ( $remaining as $ns => $dbkeys ) {
503 foreach ( $dbkeys as $dbkey => $unused ) {
504 $title = Title::makeTitle( $ns, $dbkey );
505 $this->mAllPages[$ns][$dbkey] = $this->mFakePageId;
506 $this->mMissingTitles[$this->mFakePageId] = $title;
507 $this->mFakePageId--;
508 $this->mTitles[] = $title;
509 }
510 }
511 } else {
512 // The remaining pageids do not exist
513 if ( !$this->mMissingPageIDs ) {
514 $this->mMissingPageIDs = array_keys( $remaining );
515 } else {
516 $this->mMissingPageIDs = array_merge( $this->mMissingPageIDs, array_keys( $remaining ) );
517 }
518 }
519 }
520 }
521
522 /**
523 * Does the same as initFromTitles(), but is based on revision IDs
524 * instead
525 * @param $revids array of revision IDs
526 */
527 private function initFromRevIDs( $revids ) {
528 if ( !count( $revids ) ) {
529 return;
530 }
531
532 $revids = array_map( 'intval', $revids ); // paranoia
533 $db = $this->getDB();
534 $pageids = array();
535 $remaining = array_flip( $revids );
536
537 $tables = array( 'revision', 'page' );
538 $fields = array( 'rev_id', 'rev_page' );
539 $where = array( 'rev_id' => $revids, 'rev_page = page_id' );
540
541 // Get pageIDs data from the `page` table
542 $this->profileDBIn();
543 $res = $db->select( $tables, $fields, $where, __METHOD__ );
544 foreach ( $res as $row ) {
545 $revid = intval( $row->rev_id );
546 $pageid = intval( $row->rev_page );
547 $this->mGoodRevIDs[$revid] = $pageid;
548 $pageids[$pageid] = '';
549 unset( $remaining[$revid] );
550 }
551 $this->profileDBOut();
552
553 $this->mMissingRevIDs = array_keys( $remaining );
554
555 // Populate all the page information
556 $this->initFromPageIds( array_keys( $pageids ) );
557 }
558
559 /**
560 * Resolve any redirects in the result if redirect resolution was
561 * requested. This function is called repeatedly until all redirects
562 * have been resolved.
563 */
564 private function resolvePendingRedirects() {
565 if ( $this->mResolveRedirects ) {
566 $db = $this->getDB();
567 $pageFlds = $this->getPageTableFields();
568
569 // Repeat until all redirects have been resolved
570 // The infinite loop is prevented by keeping all known pages in $this->mAllPages
571 while ( $this->mPendingRedirectIDs ) {
572 // Resolve redirects by querying the pagelinks table, and repeat the process
573 // Create a new linkBatch object for the next pass
574 $linkBatch = $this->getRedirectTargets();
575
576 if ( $linkBatch->isEmpty() ) {
577 break;
578 }
579
580 $set = $linkBatch->constructSet( 'page', $db );
581 if ( $set === false ) {
582 break;
583 }
584
585 // Get pageIDs data from the `page` table
586 $this->profileDBIn();
587 $res = $db->select( 'page', $pageFlds, $set, __METHOD__ );
588 $this->profileDBOut();
589
590 // Hack: get the ns:titles stored in array(ns => array(titles)) format
591 $this->initFromQueryResult( $db, $res, $linkBatch->data, true );
592 }
593 }
594 }
595
596 /**
597 * Get the targets of the pending redirects from the database
598 *
599 * Also creates entries in the redirect table for redirects that don't
600 * have one.
601 * @return LinkBatch
602 */
603 private function getRedirectTargets() {
604 $lb = new LinkBatch();
605 $db = $this->getDB();
606
607 $this->profileDBIn();
608 $res = $db->select(
609 'redirect',
610 array(
611 'rd_from',
612 'rd_namespace',
613 'rd_title'
614 ), array( 'rd_from' => array_keys( $this->mPendingRedirectIDs ) ),
615 __METHOD__
616 );
617 $this->profileDBOut();
618
619 foreach ( $res as $row ) {
620 $rdfrom = intval( $row->rd_from );
621 $from = $this->mPendingRedirectIDs[$rdfrom]->getPrefixedText();
622 $to = Title::makeTitle( $row->rd_namespace, $row->rd_title )->getPrefixedText();
623 unset( $this->mPendingRedirectIDs[$rdfrom] );
624 if ( !isset( $this->mAllPages[$row->rd_namespace][$row->rd_title] ) ) {
625 $lb->add( $row->rd_namespace, $row->rd_title );
626 }
627 $this->mRedirectTitles[$from] = $to;
628 }
629
630 if ( $this->mPendingRedirectIDs ) {
631 // We found pages that aren't in the redirect table
632 // Add them
633 foreach ( $this->mPendingRedirectIDs as $id => $title ) {
634 $article = new Article( $title );
635 $rt = $article->insertRedirect();
636 if ( !$rt ) {
637 // What the hell. Let's just ignore this
638 continue;
639 }
640 $lb->addObj( $rt );
641 $this->mRedirectTitles[$title->getPrefixedText()] = $rt->getPrefixedText();
642 unset( $this->mPendingRedirectIDs[$id] );
643 }
644 }
645 return $lb;
646 }
647
648 /**
649 * Given an array of title strings, convert them into Title objects.
650 * Alternativelly, an array of Title objects may be given.
651 * This method validates access rights for the title,
652 * and appends normalization values to the output.
653 *
654 * @param $titles array of Title objects or strings
655 * @return LinkBatch
656 */
657 private function processTitlesArray( $titles ) {
658 $linkBatch = new LinkBatch();
659
660 foreach ( $titles as $title ) {
661 $titleObj = is_string( $title ) ? Title::newFromText( $title ) : $title;
662 if ( !$titleObj ) {
663 // Handle invalid titles gracefully
664 $this->mAllpages[0][$title] = $this->mFakePageId;
665 $this->mInvalidTitles[$this->mFakePageId] = $title;
666 $this->mFakePageId--;
667 continue; // There's nothing else we can do
668 }
669 $unconvertedTitle = $titleObj->getPrefixedText();
670 $titleWasConverted = false;
671 $iw = $titleObj->getInterwiki();
672 if ( strval( $iw ) !== '' ) {
673 // This title is an interwiki link.
674 $this->mInterwikiTitles[$titleObj->getPrefixedText()] = $iw;
675 } else {
676 // Variants checking
677 global $wgContLang;
678 if ( $this->mConvertTitles &&
679 count( $wgContLang->getVariants() ) > 1 &&
680 !$titleObj->exists() ) {
681 // Language::findVariantLink will modify titleObj into
682 // the canonical variant if possible
683 $wgContLang->findVariantLink( $title, $titleObj );
684 $titleWasConverted = $unconvertedTitle !== $titleObj->getPrefixedText();
685 }
686
687
688 if ( $titleObj->getNamespace() < 0 ) {
689 // Handle Special and Media pages
690 $titleObj = $titleObj->fixSpecialName();
691 $this->mSpecialTitles[$this->mFakePageId] = $titleObj;
692 $this->mFakePageId--;
693 } else {
694 // Regular page
695 $linkBatch->addObj( $titleObj );
696 }
697 }
698
699 // Make sure we remember the original title that was
700 // given to us. This way the caller can correlate new
701 // titles with the originally requested when e.g. the
702 // namespace is localized or the capitalization is
703 // different
704 if ( $titleWasConverted ) {
705 $this->mConvertedTitles[$title] = $titleObj->getPrefixedText();
706 } elseif ( is_string( $title ) && $title !== $titleObj->getPrefixedText() ) {
707 $this->mNormalizedTitles[$title] = $titleObj->getPrefixedText();
708 }
709 }
710
711 return $linkBatch;
712 }
713
714 protected function getAllowedParams() {
715 return array(
716 'titles' => array(
717 ApiBase::PARAM_ISMULTI => true
718 ),
719 'pageids' => array(
720 ApiBase::PARAM_TYPE => 'integer',
721 ApiBase::PARAM_ISMULTI => true
722 ),
723 'revids' => array(
724 ApiBase::PARAM_TYPE => 'integer',
725 ApiBase::PARAM_ISMULTI => true
726 )
727 );
728 }
729
730 protected function getParamDescription() {
731 return array(
732 'titles' => 'A list of titles to work on',
733 'pageids' => 'A list of page IDs to work on',
734 'revids' => 'A list of revision IDs to work on'
735 );
736 }
737
738 public function getPossibleErrors() {
739 return array_merge( parent::getPossibleErrors(), array(
740 array( 'code' => 'multisource', 'info' => "Cannot use 'pageids' at the same time as 'dataSource'" ),
741 array( 'code' => 'multisource', 'info' => "Cannot use 'revids' at the same time as 'dataSource'" ),
742 ) );
743 }
744
745 public function getVersion() {
746 return __CLASS__ . ': $Id$';
747 }
748 }