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