Merge "Tweaked BagOStuff::lock() retry times slightly to be faster"
[lhc/web/wiklou.git] / includes / api / ApiQueryInfo.php
1 <?php
2 /**
3 *
4 *
5 * Created on Sep 25, 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 /**
28 * A query module to show basic page information.
29 *
30 * @ingroup API
31 */
32 class ApiQueryInfo extends ApiQueryBase {
33
34 private $fld_protection = false, $fld_talkid = false,
35 $fld_subjectid = false, $fld_url = false,
36 $fld_readable = false, $fld_watched = false, $fld_watchers = false,
37 $fld_notificationtimestamp = false,
38 $fld_preload = false, $fld_displaytitle = false;
39
40 private $params, $titles, $missing, $everything, $pageCounter;
41
42 private $pageRestrictions, $pageIsRedir, $pageIsNew, $pageTouched,
43 $pageLatest, $pageLength;
44
45 private $protections, $restrictionTypes, $watched, $watchers, $notificationtimestamps,
46 $talkids, $subjectids, $displaytitles;
47 private $showZeroWatchers = false;
48
49 private $tokenFunctions;
50
51 public function __construct( ApiQuery $query, $moduleName ) {
52 parent::__construct( $query, $moduleName, 'in' );
53 }
54
55 /**
56 * @param ApiPageSet $pageSet
57 * @return void
58 */
59 public function requestExtraData( $pageSet ) {
60 $pageSet->requestField( 'page_restrictions' );
61 // If the pageset is resolving redirects we won't get page_is_redirect.
62 // But we can't know for sure until the pageset is executed (revids may
63 // turn it off), so request it unconditionally.
64 $pageSet->requestField( 'page_is_redirect' );
65 $pageSet->requestField( 'page_is_new' );
66 $config = $this->getConfig();
67 $pageSet->requestField( 'page_touched' );
68 $pageSet->requestField( 'page_latest' );
69 $pageSet->requestField( 'page_len' );
70 if ( $config->get( 'ContentHandlerUseDB' ) ) {
71 $pageSet->requestField( 'page_content_model' );
72 }
73 if ( $config->get( 'PageLanguageUseDB' ) ) {
74 $pageSet->requestField( 'page_lang' );
75 }
76 }
77
78 /**
79 * Get an array mapping token names to their handler functions.
80 * The prototype for a token function is func($pageid, $title)
81 * it should return a token or false (permission denied)
82 * @deprecated since 1.24
83 * @return array Array(tokenname => function)
84 */
85 protected function getTokenFunctions() {
86 // Don't call the hooks twice
87 if ( isset( $this->tokenFunctions ) ) {
88 return $this->tokenFunctions;
89 }
90
91 // If we're in JSON callback mode, no tokens can be obtained
92 if ( !is_null( $this->getMain()->getRequest()->getVal( 'callback' ) ) ) {
93 return array();
94 }
95
96 $this->tokenFunctions = array(
97 'edit' => array( 'ApiQueryInfo', 'getEditToken' ),
98 'delete' => array( 'ApiQueryInfo', 'getDeleteToken' ),
99 'protect' => array( 'ApiQueryInfo', 'getProtectToken' ),
100 'move' => array( 'ApiQueryInfo', 'getMoveToken' ),
101 'block' => array( 'ApiQueryInfo', 'getBlockToken' ),
102 'unblock' => array( 'ApiQueryInfo', 'getUnblockToken' ),
103 'email' => array( 'ApiQueryInfo', 'getEmailToken' ),
104 'import' => array( 'ApiQueryInfo', 'getImportToken' ),
105 'watch' => array( 'ApiQueryInfo', 'getWatchToken' ),
106 );
107 Hooks::run( 'APIQueryInfoTokens', array( &$this->tokenFunctions ) );
108
109 return $this->tokenFunctions;
110 }
111
112 static protected $cachedTokens = array();
113
114 /**
115 * @deprecated since 1.24
116 */
117 public static function resetTokenCache() {
118 ApiQueryInfo::$cachedTokens = array();
119 }
120
121 /**
122 * @deprecated since 1.24
123 */
124 public static function getEditToken( $pageid, $title ) {
125 // We could check for $title->userCan('edit') here,
126 // but that's too expensive for this purpose
127 // and would break caching
128 global $wgUser;
129 if ( !$wgUser->isAllowed( 'edit' ) ) {
130 return false;
131 }
132
133 // The token is always the same, let's exploit that
134 if ( !isset( ApiQueryInfo::$cachedTokens['edit'] ) ) {
135 ApiQueryInfo::$cachedTokens['edit'] = $wgUser->getEditToken();
136 }
137
138 return ApiQueryInfo::$cachedTokens['edit'];
139 }
140
141 /**
142 * @deprecated since 1.24
143 */
144 public static function getDeleteToken( $pageid, $title ) {
145 global $wgUser;
146 if ( !$wgUser->isAllowed( 'delete' ) ) {
147 return false;
148 }
149
150 // The token is always the same, let's exploit that
151 if ( !isset( ApiQueryInfo::$cachedTokens['delete'] ) ) {
152 ApiQueryInfo::$cachedTokens['delete'] = $wgUser->getEditToken();
153 }
154
155 return ApiQueryInfo::$cachedTokens['delete'];
156 }
157
158 /**
159 * @deprecated since 1.24
160 */
161 public static function getProtectToken( $pageid, $title ) {
162 global $wgUser;
163 if ( !$wgUser->isAllowed( 'protect' ) ) {
164 return false;
165 }
166
167 // The token is always the same, let's exploit that
168 if ( !isset( ApiQueryInfo::$cachedTokens['protect'] ) ) {
169 ApiQueryInfo::$cachedTokens['protect'] = $wgUser->getEditToken();
170 }
171
172 return ApiQueryInfo::$cachedTokens['protect'];
173 }
174
175 /**
176 * @deprecated since 1.24
177 */
178 public static function getMoveToken( $pageid, $title ) {
179 global $wgUser;
180 if ( !$wgUser->isAllowed( 'move' ) ) {
181 return false;
182 }
183
184 // The token is always the same, let's exploit that
185 if ( !isset( ApiQueryInfo::$cachedTokens['move'] ) ) {
186 ApiQueryInfo::$cachedTokens['move'] = $wgUser->getEditToken();
187 }
188
189 return ApiQueryInfo::$cachedTokens['move'];
190 }
191
192 /**
193 * @deprecated since 1.24
194 */
195 public static function getBlockToken( $pageid, $title ) {
196 global $wgUser;
197 if ( !$wgUser->isAllowed( 'block' ) ) {
198 return false;
199 }
200
201 // The token is always the same, let's exploit that
202 if ( !isset( ApiQueryInfo::$cachedTokens['block'] ) ) {
203 ApiQueryInfo::$cachedTokens['block'] = $wgUser->getEditToken();
204 }
205
206 return ApiQueryInfo::$cachedTokens['block'];
207 }
208
209 /**
210 * @deprecated since 1.24
211 */
212 public static function getUnblockToken( $pageid, $title ) {
213 // Currently, this is exactly the same as the block token
214 return self::getBlockToken( $pageid, $title );
215 }
216
217 /**
218 * @deprecated since 1.24
219 */
220 public static function getEmailToken( $pageid, $title ) {
221 global $wgUser;
222 if ( !$wgUser->canSendEmail() || $wgUser->isBlockedFromEmailUser() ) {
223 return false;
224 }
225
226 // The token is always the same, let's exploit that
227 if ( !isset( ApiQueryInfo::$cachedTokens['email'] ) ) {
228 ApiQueryInfo::$cachedTokens['email'] = $wgUser->getEditToken();
229 }
230
231 return ApiQueryInfo::$cachedTokens['email'];
232 }
233
234 /**
235 * @deprecated since 1.24
236 */
237 public static function getImportToken( $pageid, $title ) {
238 global $wgUser;
239 if ( !$wgUser->isAllowedAny( 'import', 'importupload' ) ) {
240 return false;
241 }
242
243 // The token is always the same, let's exploit that
244 if ( !isset( ApiQueryInfo::$cachedTokens['import'] ) ) {
245 ApiQueryInfo::$cachedTokens['import'] = $wgUser->getEditToken();
246 }
247
248 return ApiQueryInfo::$cachedTokens['import'];
249 }
250
251 /**
252 * @deprecated since 1.24
253 */
254 public static function getWatchToken( $pageid, $title ) {
255 global $wgUser;
256 if ( !$wgUser->isLoggedIn() ) {
257 return false;
258 }
259
260 // The token is always the same, let's exploit that
261 if ( !isset( ApiQueryInfo::$cachedTokens['watch'] ) ) {
262 ApiQueryInfo::$cachedTokens['watch'] = $wgUser->getEditToken( 'watch' );
263 }
264
265 return ApiQueryInfo::$cachedTokens['watch'];
266 }
267
268 /**
269 * @deprecated since 1.24
270 */
271 public static function getOptionsToken( $pageid, $title ) {
272 global $wgUser;
273 if ( !$wgUser->isLoggedIn() ) {
274 return false;
275 }
276
277 // The token is always the same, let's exploit that
278 if ( !isset( ApiQueryInfo::$cachedTokens['options'] ) ) {
279 ApiQueryInfo::$cachedTokens['options'] = $wgUser->getEditToken();
280 }
281
282 return ApiQueryInfo::$cachedTokens['options'];
283 }
284
285 public function execute() {
286 $this->params = $this->extractRequestParams();
287 if ( !is_null( $this->params['prop'] ) ) {
288 $prop = array_flip( $this->params['prop'] );
289 $this->fld_protection = isset( $prop['protection'] );
290 $this->fld_watched = isset( $prop['watched'] );
291 $this->fld_watchers = isset( $prop['watchers'] );
292 $this->fld_notificationtimestamp = isset( $prop['notificationtimestamp'] );
293 $this->fld_talkid = isset( $prop['talkid'] );
294 $this->fld_subjectid = isset( $prop['subjectid'] );
295 $this->fld_url = isset( $prop['url'] );
296 $this->fld_readable = isset( $prop['readable'] );
297 $this->fld_preload = isset( $prop['preload'] );
298 $this->fld_displaytitle = isset( $prop['displaytitle'] );
299 }
300
301 $pageSet = $this->getPageSet();
302 $this->titles = $pageSet->getGoodTitles();
303 $this->missing = $pageSet->getMissingTitles();
304 $this->everything = $this->titles + $this->missing;
305 $result = $this->getResult();
306
307 uasort( $this->everything, array( 'Title', 'compare' ) );
308 if ( !is_null( $this->params['continue'] ) ) {
309 // Throw away any titles we're gonna skip so they don't
310 // clutter queries
311 $cont = explode( '|', $this->params['continue'] );
312 $this->dieContinueUsageIf( count( $cont ) != 2 );
313 $conttitle = Title::makeTitleSafe( $cont[0], $cont[1] );
314 foreach ( $this->everything as $pageid => $title ) {
315 if ( Title::compare( $title, $conttitle ) >= 0 ) {
316 break;
317 }
318 unset( $this->titles[$pageid] );
319 unset( $this->missing[$pageid] );
320 unset( $this->everything[$pageid] );
321 }
322 }
323
324 $this->pageRestrictions = $pageSet->getCustomField( 'page_restrictions' );
325 // when resolving redirects, no page will have this field
326 $this->pageIsRedir = !$pageSet->isResolvingRedirects()
327 ? $pageSet->getCustomField( 'page_is_redirect' )
328 : array();
329 $this->pageIsNew = $pageSet->getCustomField( 'page_is_new' );
330
331 $this->pageTouched = $pageSet->getCustomField( 'page_touched' );
332 $this->pageLatest = $pageSet->getCustomField( 'page_latest' );
333 $this->pageLength = $pageSet->getCustomField( 'page_len' );
334
335 // Get protection info if requested
336 if ( $this->fld_protection ) {
337 $this->getProtectionInfo();
338 }
339
340 if ( $this->fld_watched || $this->fld_notificationtimestamp ) {
341 $this->getWatchedInfo();
342 }
343
344 if ( $this->fld_watchers ) {
345 $this->getWatcherInfo();
346 }
347
348 // Run the talkid/subjectid query if requested
349 if ( $this->fld_talkid || $this->fld_subjectid ) {
350 $this->getTSIDs();
351 }
352
353 if ( $this->fld_displaytitle ) {
354 $this->getDisplayTitle();
355 }
356
357 /** @var $title Title */
358 foreach ( $this->everything as $pageid => $title ) {
359 $pageInfo = $this->extractPageInfo( $pageid, $title );
360 $fit = $result->addValue( array(
361 'query',
362 'pages'
363 ), $pageid, $pageInfo );
364 if ( !$fit ) {
365 $this->setContinueEnumParameter( 'continue',
366 $title->getNamespace() . '|' .
367 $title->getText() );
368 break;
369 }
370 }
371 }
372
373 /**
374 * Get a result array with information about a title
375 * @param int $pageid Page ID (negative for missing titles)
376 * @param Title $title
377 * @return array
378 */
379 private function extractPageInfo( $pageid, $title ) {
380 $pageInfo = array();
381 // $title->exists() needs pageid, which is not set for all title objects
382 $titleExists = $pageid > 0;
383 $ns = $title->getNamespace();
384 $dbkey = $title->getDBkey();
385
386 $pageInfo['contentmodel'] = $title->getContentModel();
387 $pageInfo['pagelanguage'] = $title->getPageLanguage()->getCode();
388
389 if ( $titleExists ) {
390 $pageInfo['touched'] = wfTimestamp( TS_ISO_8601, $this->pageTouched[$pageid] );
391 $pageInfo['lastrevid'] = intval( $this->pageLatest[$pageid] );
392 $pageInfo['length'] = intval( $this->pageLength[$pageid] );
393
394 if ( isset( $this->pageIsRedir[$pageid] ) && $this->pageIsRedir[$pageid] ) {
395 $pageInfo['redirect'] = '';
396 }
397 if ( $this->pageIsNew[$pageid] ) {
398 $pageInfo['new'] = '';
399 }
400 }
401
402 if ( !is_null( $this->params['token'] ) ) {
403 $tokenFunctions = $this->getTokenFunctions();
404 $pageInfo['starttimestamp'] = wfTimestamp( TS_ISO_8601, time() );
405 foreach ( $this->params['token'] as $t ) {
406 $val = call_user_func( $tokenFunctions[$t], $pageid, $title );
407 if ( $val === false ) {
408 $this->setWarning( "Action '$t' is not allowed for the current user" );
409 } else {
410 $pageInfo[$t . 'token'] = $val;
411 }
412 }
413 }
414
415 if ( $this->fld_protection ) {
416 $pageInfo['protection'] = array();
417 if ( isset( $this->protections[$ns][$dbkey] ) ) {
418 $pageInfo['protection'] =
419 $this->protections[$ns][$dbkey];
420 }
421 $this->getResult()->setIndexedTagName( $pageInfo['protection'], 'pr' );
422
423 $pageInfo['restrictiontypes'] = array();
424 if ( isset( $this->restrictionTypes[$ns][$dbkey] ) ) {
425 $pageInfo['restrictiontypes'] =
426 $this->restrictionTypes[$ns][$dbkey];
427 }
428 $this->getResult()->setIndexedTagName( $pageInfo['restrictiontypes'], 'rt' );
429 }
430
431 if ( $this->fld_watched && isset( $this->watched[$ns][$dbkey] ) ) {
432 $pageInfo['watched'] = '';
433 }
434
435 if ( $this->fld_watchers ) {
436 if ( isset( $this->watchers[$ns][$dbkey] ) ) {
437 $pageInfo['watchers'] = $this->watchers[$ns][$dbkey];
438 } elseif ( $this->showZeroWatchers ) {
439 $pageInfo['watchers'] = 0;
440 }
441 }
442
443 if ( $this->fld_notificationtimestamp ) {
444 $pageInfo['notificationtimestamp'] = '';
445 if ( isset( $this->notificationtimestamps[$ns][$dbkey] ) ) {
446 $pageInfo['notificationtimestamp'] =
447 wfTimestamp( TS_ISO_8601, $this->notificationtimestamps[$ns][$dbkey] );
448 }
449 }
450
451 if ( $this->fld_talkid && isset( $this->talkids[$ns][$dbkey] ) ) {
452 $pageInfo['talkid'] = $this->talkids[$ns][$dbkey];
453 }
454
455 if ( $this->fld_subjectid && isset( $this->subjectids[$ns][$dbkey] ) ) {
456 $pageInfo['subjectid'] = $this->subjectids[$ns][$dbkey];
457 }
458
459 if ( $this->fld_url ) {
460 $pageInfo['fullurl'] = wfExpandUrl( $title->getFullURL(), PROTO_CURRENT );
461 $pageInfo['editurl'] = wfExpandUrl( $title->getFullURL( 'action=edit' ), PROTO_CURRENT );
462 $pageInfo['canonicalurl'] = wfExpandUrl( $title->getFullURL(), PROTO_CANONICAL );
463 }
464 if ( $this->fld_readable && $title->userCan( 'read', $this->getUser() ) ) {
465 $pageInfo['readable'] = '';
466 }
467
468 if ( $this->fld_preload ) {
469 if ( $titleExists ) {
470 $pageInfo['preload'] = '';
471 } else {
472 $text = null;
473 Hooks::run( 'EditFormPreloadText', array( &$text, &$title ) );
474
475 $pageInfo['preload'] = $text;
476 }
477 }
478
479 if ( $this->fld_displaytitle ) {
480 if ( isset( $this->displaytitles[$pageid] ) ) {
481 $pageInfo['displaytitle'] = $this->displaytitles[$pageid];
482 } else {
483 $pageInfo['displaytitle'] = $title->getPrefixedText();
484 }
485 }
486
487 return $pageInfo;
488 }
489
490 /**
491 * Get information about protections and put it in $protections
492 */
493 private function getProtectionInfo() {
494 global $wgContLang;
495 $this->protections = array();
496 $db = $this->getDB();
497
498 // Get normal protections for existing titles
499 if ( count( $this->titles ) ) {
500 $this->resetQueryParams();
501 $this->addTables( 'page_restrictions' );
502 $this->addFields( array( 'pr_page', 'pr_type', 'pr_level',
503 'pr_expiry', 'pr_cascade' ) );
504 $this->addWhereFld( 'pr_page', array_keys( $this->titles ) );
505
506 $res = $this->select( __METHOD__ );
507 foreach ( $res as $row ) {
508 /** @var $title Title */
509 $title = $this->titles[$row->pr_page];
510 $a = array(
511 'type' => $row->pr_type,
512 'level' => $row->pr_level,
513 'expiry' => $wgContLang->formatExpiry( $row->pr_expiry, TS_ISO_8601 )
514 );
515 if ( $row->pr_cascade ) {
516 $a['cascade'] = '';
517 }
518 $this->protections[$title->getNamespace()][$title->getDBkey()][] = $a;
519 }
520 // Also check old restrictions
521 foreach ( $this->titles as $pageId => $title ) {
522 if ( $this->pageRestrictions[$pageId] ) {
523 $namespace = $title->getNamespace();
524 $dbKey = $title->getDBkey();
525 $restrictions = explode( ':', trim( $this->pageRestrictions[$pageId] ) );
526 foreach ( $restrictions as $restrict ) {
527 $temp = explode( '=', trim( $restrict ) );
528 if ( count( $temp ) == 1 ) {
529 // old old format should be treated as edit/move restriction
530 $restriction = trim( $temp[0] );
531
532 if ( $restriction == '' ) {
533 continue;
534 }
535 $this->protections[$namespace][$dbKey][] = array(
536 'type' => 'edit',
537 'level' => $restriction,
538 'expiry' => 'infinity',
539 );
540 $this->protections[$namespace][$dbKey][] = array(
541 'type' => 'move',
542 'level' => $restriction,
543 'expiry' => 'infinity',
544 );
545 } else {
546 $restriction = trim( $temp[1] );
547 if ( $restriction == '' ) {
548 continue;
549 }
550 $this->protections[$namespace][$dbKey][] = array(
551 'type' => $temp[0],
552 'level' => $restriction,
553 'expiry' => 'infinity',
554 );
555 }
556 }
557 }
558 }
559 }
560
561 // Get protections for missing titles
562 if ( count( $this->missing ) ) {
563 $this->resetQueryParams();
564 $lb = new LinkBatch( $this->missing );
565 $this->addTables( 'protected_titles' );
566 $this->addFields( array( 'pt_title', 'pt_namespace', 'pt_create_perm', 'pt_expiry' ) );
567 $this->addWhere( $lb->constructSet( 'pt', $db ) );
568 $res = $this->select( __METHOD__ );
569 foreach ( $res as $row ) {
570 $this->protections[$row->pt_namespace][$row->pt_title][] = array(
571 'type' => 'create',
572 'level' => $row->pt_create_perm,
573 'expiry' => $wgContLang->formatExpiry( $row->pt_expiry, TS_ISO_8601 )
574 );
575 }
576 }
577
578 // Separate good and missing titles into files and other pages
579 // and populate $this->restrictionTypes
580 $images = $others = array();
581 foreach ( $this->everything as $title ) {
582 if ( $title->getNamespace() == NS_FILE ) {
583 $images[] = $title->getDBkey();
584 } else {
585 $others[] = $title;
586 }
587 // Applicable protection types
588 $this->restrictionTypes[$title->getNamespace()][$title->getDBkey()] =
589 array_values( $title->getRestrictionTypes() );
590 }
591
592 if ( count( $others ) ) {
593 // Non-images: check templatelinks
594 $lb = new LinkBatch( $others );
595 $this->resetQueryParams();
596 $this->addTables( array( 'page_restrictions', 'page', 'templatelinks' ) );
597 $this->addFields( array( 'pr_type', 'pr_level', 'pr_expiry',
598 'page_title', 'page_namespace',
599 'tl_title', 'tl_namespace' ) );
600 $this->addWhere( $lb->constructSet( 'tl', $db ) );
601 $this->addWhere( 'pr_page = page_id' );
602 $this->addWhere( 'pr_page = tl_from' );
603 $this->addWhereFld( 'pr_cascade', 1 );
604
605 $res = $this->select( __METHOD__ );
606 foreach ( $res as $row ) {
607 $source = Title::makeTitle( $row->page_namespace, $row->page_title );
608 $this->protections[$row->tl_namespace][$row->tl_title][] = array(
609 'type' => $row->pr_type,
610 'level' => $row->pr_level,
611 'expiry' => $wgContLang->formatExpiry( $row->pr_expiry, TS_ISO_8601 ),
612 'source' => $source->getPrefixedText()
613 );
614 }
615 }
616
617 if ( count( $images ) ) {
618 // Images: check imagelinks
619 $this->resetQueryParams();
620 $this->addTables( array( 'page_restrictions', 'page', 'imagelinks' ) );
621 $this->addFields( array( 'pr_type', 'pr_level', 'pr_expiry',
622 'page_title', 'page_namespace', 'il_to' ) );
623 $this->addWhere( 'pr_page = page_id' );
624 $this->addWhere( 'pr_page = il_from' );
625 $this->addWhereFld( 'pr_cascade', 1 );
626 $this->addWhereFld( 'il_to', $images );
627
628 $res = $this->select( __METHOD__ );
629 foreach ( $res as $row ) {
630 $source = Title::makeTitle( $row->page_namespace, $row->page_title );
631 $this->protections[NS_FILE][$row->il_to][] = array(
632 'type' => $row->pr_type,
633 'level' => $row->pr_level,
634 'expiry' => $wgContLang->formatExpiry( $row->pr_expiry, TS_ISO_8601 ),
635 'source' => $source->getPrefixedText()
636 );
637 }
638 }
639 }
640
641 /**
642 * Get talk page IDs (if requested) and subject page IDs (if requested)
643 * and put them in $talkids and $subjectids
644 */
645 private function getTSIDs() {
646 $getTitles = $this->talkids = $this->subjectids = array();
647
648 /** @var $t Title */
649 foreach ( $this->everything as $t ) {
650 if ( MWNamespace::isTalk( $t->getNamespace() ) ) {
651 if ( $this->fld_subjectid ) {
652 $getTitles[] = $t->getSubjectPage();
653 }
654 } elseif ( $this->fld_talkid ) {
655 $getTitles[] = $t->getTalkPage();
656 }
657 }
658 if ( !count( $getTitles ) ) {
659 return;
660 }
661
662 $db = $this->getDB();
663
664 // Construct a custom WHERE clause that matches
665 // all titles in $getTitles
666 $lb = new LinkBatch( $getTitles );
667 $this->resetQueryParams();
668 $this->addTables( 'page' );
669 $this->addFields( array( 'page_title', 'page_namespace', 'page_id' ) );
670 $this->addWhere( $lb->constructSet( 'page', $db ) );
671 $res = $this->select( __METHOD__ );
672 foreach ( $res as $row ) {
673 if ( MWNamespace::isTalk( $row->page_namespace ) ) {
674 $this->talkids[MWNamespace::getSubject( $row->page_namespace )][$row->page_title] =
675 intval( $row->page_id );
676 } else {
677 $this->subjectids[MWNamespace::getTalk( $row->page_namespace )][$row->page_title] =
678 intval( $row->page_id );
679 }
680 }
681 }
682
683 private function getDisplayTitle() {
684 $this->displaytitles = array();
685
686 $pageIds = array_keys( $this->titles );
687
688 if ( !count( $pageIds ) ) {
689 return;
690 }
691
692 $this->resetQueryParams();
693 $this->addTables( 'page_props' );
694 $this->addFields( array( 'pp_page', 'pp_value' ) );
695 $this->addWhereFld( 'pp_page', $pageIds );
696 $this->addWhereFld( 'pp_propname', 'displaytitle' );
697 $res = $this->select( __METHOD__ );
698
699 foreach ( $res as $row ) {
700 $this->displaytitles[$row->pp_page] = $row->pp_value;
701 }
702 }
703
704 /**
705 * Get information about watched status and put it in $this->watched
706 * and $this->notificationtimestamps
707 */
708 private function getWatchedInfo() {
709 $user = $this->getUser();
710
711 if ( $user->isAnon() || count( $this->everything ) == 0
712 || !$user->isAllowed( 'viewmywatchlist' )
713 ) {
714 return;
715 }
716
717 $this->watched = array();
718 $this->notificationtimestamps = array();
719 $db = $this->getDB();
720
721 $lb = new LinkBatch( $this->everything );
722
723 $this->resetQueryParams();
724 $this->addTables( array( 'watchlist' ) );
725 $this->addFields( array( 'wl_title', 'wl_namespace' ) );
726 $this->addFieldsIf( 'wl_notificationtimestamp', $this->fld_notificationtimestamp );
727 $this->addWhere( array(
728 $lb->constructSet( 'wl', $db ),
729 'wl_user' => $user->getID()
730 ) );
731
732 $res = $this->select( __METHOD__ );
733
734 foreach ( $res as $row ) {
735 if ( $this->fld_watched ) {
736 $this->watched[$row->wl_namespace][$row->wl_title] = true;
737 }
738 if ( $this->fld_notificationtimestamp ) {
739 $this->notificationtimestamps[$row->wl_namespace][$row->wl_title] =
740 $row->wl_notificationtimestamp;
741 }
742 }
743 }
744
745 /**
746 * Get the count of watchers and put it in $this->watchers
747 */
748 private function getWatcherInfo() {
749 if ( count( $this->everything ) == 0 ) {
750 return;
751 }
752
753 $user = $this->getUser();
754 $canUnwatchedpages = $user->isAllowed( 'unwatchedpages' );
755 $unwatchedPageThreshold = $this->getConfig()->get( 'UnwatchedPageThreshold' );
756 if ( !$canUnwatchedpages && !is_int( $unwatchedPageThreshold ) ) {
757 return;
758 }
759
760 $this->watchers = array();
761 $this->showZeroWatchers = $canUnwatchedpages;
762 $db = $this->getDB();
763
764 $lb = new LinkBatch( $this->everything );
765
766 $this->resetQueryParams();
767 $this->addTables( array( 'watchlist' ) );
768 $this->addFields( array( 'wl_title', 'wl_namespace', 'count' => 'COUNT(*)' ) );
769 $this->addWhere( array(
770 $lb->constructSet( 'wl', $db )
771 ) );
772 $this->addOption( 'GROUP BY', array( 'wl_namespace', 'wl_title' ) );
773 if ( !$canUnwatchedpages ) {
774 $this->addOption( 'HAVING', "COUNT(*) >= $unwatchedPageThreshold" );
775 }
776
777 $res = $this->select( __METHOD__ );
778
779 foreach ( $res as $row ) {
780 $this->watchers[$row->wl_namespace][$row->wl_title] = (int)$row->count;
781 }
782 }
783
784 public function getCacheMode( $params ) {
785 $publicProps = array(
786 'protection',
787 'talkid',
788 'subjectid',
789 'url',
790 'preload',
791 'displaytitle',
792 );
793 if ( !is_null( $params['prop'] ) ) {
794 foreach ( $params['prop'] as $prop ) {
795 if ( !in_array( $prop, $publicProps ) ) {
796 return 'private';
797 }
798 }
799 }
800 if ( !is_null( $params['token'] ) ) {
801 return 'private';
802 }
803
804 return 'public';
805 }
806
807 public function getAllowedParams() {
808 return array(
809 'prop' => array(
810 ApiBase::PARAM_DFLT => null,
811 ApiBase::PARAM_ISMULTI => true,
812 ApiBase::PARAM_TYPE => array(
813 'protection',
814 'talkid',
815 'watched', # private
816 'watchers', # private
817 'notificationtimestamp', # private
818 'subjectid',
819 'url',
820 'readable', # private
821 'preload',
822 'displaytitle',
823 // If you add more properties here, please consider whether they
824 // need to be added to getCacheMode()
825 ),
826 ApiBase::PARAM_HELP_MSG_PER_VALUE => array(),
827 ),
828 'token' => array(
829 ApiBase::PARAM_DEPRECATED => true,
830 ApiBase::PARAM_DFLT => null,
831 ApiBase::PARAM_ISMULTI => true,
832 ApiBase::PARAM_TYPE => array_keys( $this->getTokenFunctions() )
833 ),
834 'continue' => array(
835 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
836 ),
837 );
838 }
839
840 protected function getExamplesMessages() {
841 return array(
842 'action=query&prop=info&titles=Main%20Page'
843 => 'apihelp-query+info-example-simple',
844 'action=query&prop=info&inprop=protection&titles=Main%20Page'
845 => 'apihelp-query+info-example-protection',
846 );
847 }
848
849 public function getHelpUrls() {
850 return 'https://www.mediawiki.org/wiki/API:Properties#info_.2F_in';
851 }
852 }