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