(bug 27862; follow-up r77714) Make emailuser api module not freak out when SpecialEma...
[lhc/web/wiklou.git] / includes / api / ApiPageSet.php
1 <?php
2 /**
3 *
4 *
5 * Created on Sep 24, 2006
6 *
7 * Copyright © 2006 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 if ( !defined( 'MEDIAWIKI' ) ) {
28 // Eclipse helper - will be ignored in production
29 require_once( 'ApiQueryBase.php' );
30 }
31
32 /**
33 * This class contains a list of pages that the client has requested.
34 * Initially, when the client passes in titles=, pageids=, or revisions=
35 * parameter, an instance of the ApiPageSet class will normalize titles,
36 * determine if the pages/revisions exist, and prefetch any additional page
37 * data requested.
38 *
39 * When a generator is used, the result of the generator will become the input
40 * for the second instance of this class, and all subsequent actions will use
41 * the second instance for all their work.
42 *
43 * @ingroup API
44 */
45 class ApiPageSet extends ApiQueryBase {
46
47 private $mAllPages; // [ns][dbkey] => page_id or negative when missing
48 private $mTitles, $mGoodTitles, $mMissingTitles, $mInvalidTitles;
49 private $mMissingPageIDs, $mRedirectTitles, $mSpecialTitles;
50 private $mNormalizedTitles, $mInterwikiTitles;
51 private $mResolveRedirects, $mPendingRedirectIDs;
52 private $mConvertTitles, $mConvertedTitles;
53 private $mGoodRevIDs, $mMissingRevIDs;
54 private $mFakePageId;
55
56 private $mRequestedPageFields;
57
58 /**
59 * Constructor
60 * @param $query ApiQuery
61 * @param $resolveRedirects bool Whether redirects should be resolved
62 */
63 public function __construct( $query, $resolveRedirects = false, $convertTitles = false ) {
64 parent::__construct( $query, 'query' );
65
66 $this->mAllPages = array();
67 $this->mTitles = array();
68 $this->mGoodTitles = array();
69 $this->mMissingTitles = array();
70 $this->mInvalidTitles = array();
71 $this->mMissingPageIDs = array();
72 $this->mRedirectTitles = array();
73 $this->mNormalizedTitles = array();
74 $this->mInterwikiTitles = array();
75 $this->mGoodRevIDs = array();
76 $this->mMissingRevIDs = array();
77 $this->mSpecialTitles = array();
78
79 $this->mRequestedPageFields = array();
80 $this->mResolveRedirects = $resolveRedirects;
81 if ( $resolveRedirects ) {
82 $this->mPendingRedirectIDs = array();
83 }
84
85 $this->mConvertTitles = $convertTitles;
86 $this->mConvertedTitles = array();
87
88 $this->mFakePageId = - 1;
89 }
90
91 /**
92 * Check whether this PageSet is resolving redirects
93 * @return bool
94 */
95 public function isResolvingRedirects() {
96 return $this->mResolveRedirects;
97 }
98
99 /**
100 * Request an additional field from the page table. Must be called
101 * before execute()
102 * @param $fieldName string Field name
103 */
104 public function requestField( $fieldName ) {
105 $this->mRequestedPageFields[$fieldName] = null;
106 }
107
108 /**
109 * Get the value of a custom field previously requested through
110 * requestField()
111 * @param $fieldName string Field name
112 * @return mixed Field value
113 */
114 public function getCustomField( $fieldName ) {
115 return $this->mRequestedPageFields[$fieldName];
116 }
117
118 /**
119 * Get the fields that have to be queried from the page table:
120 * the ones requested through requestField() and a few basic ones
121 * we always need
122 * @return array of field names
123 */
124 public function getPageTableFields() {
125 // Ensure we get minimum required fields
126 // DON'T change this order
127 $pageFlds = array(
128 'page_namespace' => null,
129 'page_title' => null,
130 'page_id' => null,
131 );
132
133 if ( $this->mResolveRedirects ) {
134 $pageFlds['page_is_redirect'] = null;
135 }
136
137 // only store non-default fields
138 $this->mRequestedPageFields = array_diff_key( $this->mRequestedPageFields, $pageFlds );
139
140 $pageFlds = array_merge( $pageFlds, $this->mRequestedPageFields );
141 return array_keys( $pageFlds );
142 }
143
144 /**
145 * Returns an array [ns][dbkey] => page_id for all requested titles.
146 * page_id is a unique negative number in case title was not found.
147 * Invalid titles will also have negative page IDs and will be in namespace 0
148 * @return array
149 */
150 public function getAllTitlesByNamespace() {
151 return $this->mAllPages;
152 }
153
154 /**
155 * All Title objects provided.
156 * @return array of Title objects
157 */
158 public function getTitles() {
159 return $this->mTitles;
160 }
161
162 /**
163 * Returns the number of unique pages (not revisions) in the set.
164 * @return int
165 */
166 public function getTitleCount() {
167 return count( $this->mTitles );
168 }
169
170 /**
171 * Title objects that were found in the database.
172 * @return array page_id (int) => Title (obj)
173 */
174 public function getGoodTitles() {
175 return $this->mGoodTitles;
176 }
177
178 /**
179 * Returns the number of found unique pages (not revisions) in the set.
180 * @return int
181 */
182 public function getGoodTitleCount() {
183 return count( $this->mGoodTitles );
184 }
185
186 /**
187 * Title objects that were NOT found in the database.
188 * The array's index will be negative for each item
189 * @return array of Title objects
190 */
191 public function getMissingTitles() {
192 return $this->mMissingTitles;
193 }
194
195 /**
196 * Titles that were deemed invalid by Title::newFromText()
197 * The array's index will be unique and negative for each item
198 * @return array of strings (not Title objects)
199 */
200 public function getInvalidTitles() {
201 return $this->mInvalidTitles;
202 }
203
204 /**
205 * Page IDs that were not found in the database
206 * @return array of page IDs
207 */
208 public function getMissingPageIDs() {
209 return $this->mMissingPageIDs;
210 }
211
212 /**
213 * Get a list of redirect resolutions - maps a title to its redirect
214 * target.
215 * @return array prefixed_title (string) => prefixed_title (string)
216 */
217 public function getRedirectTitles() {
218 return $this->mRedirectTitles;
219 }
220
221 /**
222 * Get a list of title normalizations - maps a title to its normalized
223 * version.
224 * @return array raw_prefixed_title (string) => prefixed_title (string)
225 */
226 public function getNormalizedTitles() {
227 return $this->mNormalizedTitles;
228 }
229
230 /**
231 * Get a list of title conversions - maps a title to its converted
232 * version.
233 * @return array raw_prefixed_title (string) => prefixed_title (string)
234 */
235 public function getConvertedTitles() {
236 return $this->mConvertedTitles;
237 }
238
239 /**
240 * Get a list of interwiki titles - maps a title to its interwiki
241 * prefix.
242 * @return array raw_prefixed_title (string) => interwiki_prefix (string)
243 */
244 public function getInterwikiTitles() {
245 return $this->mInterwikiTitles;
246 }
247
248 /**
249 * Get the list of revision IDs (requested with the revids= parameter)
250 * @return array revID (int) => pageID (int)
251 */
252 public function getRevisionIDs() {
253 return $this->mGoodRevIDs;
254 }
255
256 /**
257 * Revision IDs that were not found in the database
258 * @return array of revision IDs
259 */
260 public function getMissingRevisionIDs() {
261 return $this->mMissingRevIDs;
262 }
263
264 /**
265 * Get the list of titles with negative namespace
266 * @return array Title
267 */
268 public function getSpecialTitles() {
269 return $this->mSpecialTitles;
270 }
271
272 /**
273 * Returns the number of revisions (requested with revids= parameter)\
274 * @return int
275 */
276 public function getRevisionCount() {
277 return count( $this->getRevisionIDs() );
278 }
279
280 /**
281 * Populate the PageSet from the request parameters.
282 */
283 public function execute() {
284 $this->profileIn();
285 $params = $this->extractRequestParams();
286
287 // Only one of the titles/pageids/revids is allowed at the same time
288 $dataSource = null;
289 if ( isset( $params['titles'] ) ) {
290 $dataSource = 'titles';
291 }
292 if ( isset( $params['pageids'] ) ) {
293 if ( isset( $dataSource ) ) {
294 $this->dieUsage( "Cannot use 'pageids' at the same time as '$dataSource'", 'multisource' );
295 }
296 $dataSource = 'pageids';
297 }
298 if ( isset( $params['revids'] ) ) {
299 if ( isset( $dataSource ) ) {
300 $this->dieUsage( "Cannot use 'revids' at the same time as '$dataSource'", 'multisource' );
301 }
302 $dataSource = 'revids';
303 }
304
305 switch ( $dataSource ) {
306 case 'titles':
307 $this->initFromTitles( $params['titles'] );
308 break;
309 case 'pageids':
310 $this->initFromPageIds( $params['pageids'] );
311 break;
312 case 'revids':
313 if ( $this->mResolveRedirects ) {
314 $this->setWarning( 'Redirect resolution cannot be used together with the revids= parameter. ' .
315 'Any redirects the revids= point to have not been resolved.' );
316 }
317 $this->mResolveRedirects = false;
318 $this->initFromRevIDs( $params['revids'] );
319 break;
320 default:
321 // Do nothing - some queries do not need any of the data sources.
322 break;
323 }
324 $this->profileOut();
325 }
326
327 /**
328 * Populate this PageSet from a list of Titles
329 * @param $titles array of Title objects
330 */
331 public function populateFromTitles( $titles ) {
332 $this->profileIn();
333 $this->initFromTitles( $titles );
334 $this->profileOut();
335 }
336
337 /**
338 * Populate this PageSet from a list of page IDs
339 * @param $pageIDs array of page IDs
340 */
341 public function populateFromPageIDs( $pageIDs ) {
342 $this->profileIn();
343 $this->initFromPageIds( $pageIDs );
344 $this->profileOut();
345 }
346
347 /**
348 * Populate this PageSet from a rowset returned from the database
349 * @param $db Database object
350 * @param $queryResult ResultWrapper Query result object
351 */
352 public function populateFromQueryResult( $db, $queryResult ) {
353 $this->profileIn();
354 $this->initFromQueryResult( $queryResult );
355 $this->profileOut();
356 }
357
358 /**
359 * Populate this PageSet from a list of revision IDs
360 * @param $revIDs array of revision IDs
361 */
362 public function populateFromRevisionIDs( $revIDs ) {
363 $this->profileIn();
364 $this->initFromRevIDs( $revIDs );
365 $this->profileOut();
366 }
367
368 /**
369 * Extract all requested fields from the row received from the database
370 * @param $row Result row
371 */
372 public function processDbRow( $row ) {
373 // Store Title object in various data structures
374 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
375
376 $pageId = intval( $row->page_id );
377 $this->mAllPages[$row->page_namespace][$row->page_title] = $pageId;
378 $this->mTitles[] = $title;
379
380 if ( $this->mResolveRedirects && $row->page_is_redirect == '1' ) {
381 $this->mPendingRedirectIDs[$pageId] = $title;
382 } else {
383 $this->mGoodTitles[$pageId] = $title;
384 }
385
386 foreach ( $this->mRequestedPageFields as $fieldName => &$fieldValues ) {
387 $fieldValues[$pageId] = $row-> $fieldName;
388 }
389 }
390
391 /**
392 * Resolve redirects, if applicable
393 */
394 public function finishPageSetGeneration() {
395 $this->profileIn();
396 $this->resolvePendingRedirects();
397 $this->profileOut();
398 }
399
400 /**
401 * This method populates internal variables with page information
402 * based on the given array of title strings.
403 *
404 * Steps:
405 * #1 For each title, get data from `page` table
406 * #2 If page was not found in the DB, store it as missing
407 *
408 * Additionally, when resolving redirects:
409 * #3 If no more redirects left, stop.
410 * #4 For each redirect, get its target from the `redirect` table.
411 * #5 Substitute the original LinkBatch object with the new list
412 * #6 Repeat from step #1
413 *
414 * @param $titles array of Title objects or strings
415 */
416 private function initFromTitles( $titles ) {
417 // Get validated and normalized title objects
418 $linkBatch = $this->processTitlesArray( $titles );
419 if ( $linkBatch->isEmpty() ) {
420 return;
421 }
422
423 $db = $this->getDB();
424 $set = $linkBatch->constructSet( 'page', $db );
425
426 // Get pageIDs data from the `page` table
427 $this->profileDBIn();
428 $res = $db->select( 'page', $this->getPageTableFields(), $set,
429 __METHOD__ );
430 $this->profileDBOut();
431
432 // Hack: get the ns:titles stored in array(ns => array(titles)) format
433 $this->initFromQueryResult( $res, $linkBatch->data, true ); // process Titles
434
435 // Resolve any found redirects
436 $this->resolvePendingRedirects();
437 }
438
439 /**
440 * Does the same as initFromTitles(), but is based on page IDs instead
441 * @param $pageids array of page IDs
442 */
443 private function initFromPageIds( $pageids ) {
444 if ( !count( $pageids ) ) {
445 return;
446 }
447
448 $pageids = array_map( 'intval', $pageids ); // paranoia
449 $set = array(
450 'page_id' => $pageids
451 );
452 $db = $this->getDB();
453
454 // Get pageIDs data from the `page` table
455 $this->profileDBIn();
456 $res = $db->select( 'page', $this->getPageTableFields(), $set,
457 __METHOD__ );
458 $this->profileDBOut();
459
460 $remaining = array_flip( $pageids );
461 $this->initFromQueryResult( $res, $remaining, false ); // process PageIDs
462
463 // Resolve any found redirects
464 $this->resolvePendingRedirects();
465 }
466
467 /**
468 * Iterate through the result of the query on 'page' table,
469 * and for each row create and store title object and save any extra fields requested.
470 * @param $res ResultWrapper 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( $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 ( array_keys( $dbkeys ) as $dbkey ) {
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( $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 if ( $titleObj->getNamespace() < 0 ) {
688 // Handle Special and Media pages
689 $titleObj = $titleObj->fixSpecialName();
690 $this->mSpecialTitles[$this->mFakePageId] = $titleObj;
691 $this->mFakePageId--;
692 } else {
693 // Regular page
694 $linkBatch->addObj( $titleObj );
695 }
696 }
697
698 // Make sure we remember the original title that was
699 // given to us. This way the caller can correlate new
700 // titles with the originally requested when e.g. the
701 // namespace is localized or the capitalization is
702 // different
703 if ( $titleWasConverted ) {
704 $this->mConvertedTitles[$title] = $titleObj->getPrefixedText();
705 } elseif ( is_string( $title ) && $title !== $titleObj->getPrefixedText() ) {
706 $this->mNormalizedTitles[$title] = $titleObj->getPrefixedText();
707 }
708 }
709
710 return $linkBatch;
711 }
712
713 protected function getAllowedParams() {
714 return array(
715 'titles' => array(
716 ApiBase::PARAM_ISMULTI => true
717 ),
718 'pageids' => array(
719 ApiBase::PARAM_TYPE => 'integer',
720 ApiBase::PARAM_ISMULTI => true
721 ),
722 'revids' => array(
723 ApiBase::PARAM_TYPE => 'integer',
724 ApiBase::PARAM_ISMULTI => true
725 )
726 );
727 }
728
729 protected function getParamDescription() {
730 return array(
731 'titles' => 'A list of titles to work on',
732 'pageids' => 'A list of page IDs to work on',
733 'revids' => 'A list of revision IDs to work on'
734 );
735 }
736
737 public function getPossibleErrors() {
738 return array_merge( parent::getPossibleErrors(), array(
739 array( 'code' => 'multisource', 'info' => "Cannot use 'pageids' at the same time as 'dataSource'" ),
740 array( 'code' => 'multisource', 'info' => "Cannot use 'revids' at the same time as 'dataSource'" ),
741 ) );
742 }
743
744 public function getVersion() {
745 return __CLASS__ . ': $Id$';
746 }
747 }