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