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