Merge "Don't check namespace in SpecialWantedtemplates"
[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 $pageInfo['pagelanguage'] = $title->getPageLanguage()->getCode();
391
392 if ( $titleExists ) {
393 $pageInfo['touched'] = wfTimestamp( TS_ISO_8601, $this->pageTouched[$pageid] );
394 $pageInfo['lastrevid'] = intval( $this->pageLatest[$pageid] );
395 $pageInfo['length'] = intval( $this->pageLength[$pageid] );
396
397 if ( isset( $this->pageIsRedir[$pageid] ) && $this->pageIsRedir[$pageid] ) {
398 $pageInfo['redirect'] = true;
399 }
400 if ( $this->pageIsNew[$pageid] ) {
401 $pageInfo['new'] = true;
402 }
403 }
404
405 if ( !is_null( $this->params['token'] ) ) {
406 $tokenFunctions = $this->getTokenFunctions();
407 $pageInfo['starttimestamp'] = wfTimestamp( TS_ISO_8601, time() );
408 foreach ( $this->params['token'] as $t ) {
409 $val = call_user_func( $tokenFunctions[$t], $pageid, $title );
410 if ( $val === false ) {
411 $this->setWarning( "Action '$t' is not allowed for the current user" );
412 } else {
413 $pageInfo[$t . 'token'] = $val;
414 }
415 }
416 }
417
418 if ( $this->fld_protection ) {
419 $pageInfo['protection'] = array();
420 if ( isset( $this->protections[$ns][$dbkey] ) ) {
421 $pageInfo['protection'] =
422 $this->protections[$ns][$dbkey];
423 }
424 ApiResult::setIndexedTagName( $pageInfo['protection'], 'pr' );
425
426 $pageInfo['restrictiontypes'] = array();
427 if ( isset( $this->restrictionTypes[$ns][$dbkey] ) ) {
428 $pageInfo['restrictiontypes'] =
429 $this->restrictionTypes[$ns][$dbkey];
430 }
431 ApiResult::setIndexedTagName( $pageInfo['restrictiontypes'], 'rt' );
432 }
433
434 if ( $this->fld_watched ) {
435 $pageInfo['watched'] = isset( $this->watched[$ns][$dbkey] );
436 }
437
438 if ( $this->fld_watchers ) {
439 if ( isset( $this->watchers[$ns][$dbkey] ) ) {
440 $pageInfo['watchers'] = $this->watchers[$ns][$dbkey];
441 } elseif ( $this->showZeroWatchers ) {
442 $pageInfo['watchers'] = 0;
443 }
444 }
445
446 if ( $this->fld_notificationtimestamp ) {
447 $pageInfo['notificationtimestamp'] = '';
448 if ( isset( $this->notificationtimestamps[$ns][$dbkey] ) ) {
449 $pageInfo['notificationtimestamp'] =
450 wfTimestamp( TS_ISO_8601, $this->notificationtimestamps[$ns][$dbkey] );
451 }
452 }
453
454 if ( $this->fld_talkid && isset( $this->talkids[$ns][$dbkey] ) ) {
455 $pageInfo['talkid'] = $this->talkids[$ns][$dbkey];
456 }
457
458 if ( $this->fld_subjectid && isset( $this->subjectids[$ns][$dbkey] ) ) {
459 $pageInfo['subjectid'] = $this->subjectids[$ns][$dbkey];
460 }
461
462 if ( $this->fld_url ) {
463 $pageInfo['fullurl'] = wfExpandUrl( $title->getFullURL(), PROTO_CURRENT );
464 $pageInfo['editurl'] = wfExpandUrl( $title->getFullURL( 'action=edit' ), PROTO_CURRENT );
465 $pageInfo['canonicalurl'] = wfExpandUrl( $title->getFullURL(), PROTO_CANONICAL );
466 }
467 if ( $this->fld_readable ) {
468 $pageInfo['readable'] = $title->userCan( 'read', $this->getUser() );
469 }
470
471 if ( $this->fld_preload ) {
472 if ( $titleExists ) {
473 $pageInfo['preload'] = '';
474 } else {
475 $text = null;
476 Hooks::run( 'EditFormPreloadText', array( &$text, &$title ) );
477
478 $pageInfo['preload'] = $text;
479 }
480 }
481
482 if ( $this->fld_displaytitle ) {
483 if ( isset( $this->displaytitles[$pageid] ) ) {
484 $pageInfo['displaytitle'] = $this->displaytitles[$pageid];
485 } else {
486 $pageInfo['displaytitle'] = $title->getPrefixedText();
487 }
488 }
489
490 if ( $this->params['testactions'] ) {
491 $limit = $this->getMain()->canApiHighLimits() ? self::LIMIT_SML1 : self::LIMIT_SML2;
492 if ( $this->countTestedActions >= $limit ) {
493 return null; // force a continuation
494 }
495
496 $user = $this->getUser();
497 $pageInfo['actions'] = array();
498 foreach ( $this->params['testactions'] as $action ) {
499 $this->countTestedActions++;
500 $pageInfo['actions'][$action] = $title->userCan( $action, $user );
501 }
502 }
503
504 return $pageInfo;
505 }
506
507 /**
508 * Get information about protections and put it in $protections
509 */
510 private function getProtectionInfo() {
511 global $wgContLang;
512 $this->protections = array();
513 $db = $this->getDB();
514
515 // Get normal protections for existing titles
516 if ( count( $this->titles ) ) {
517 $this->resetQueryParams();
518 $this->addTables( 'page_restrictions' );
519 $this->addFields( array( 'pr_page', 'pr_type', 'pr_level',
520 'pr_expiry', 'pr_cascade' ) );
521 $this->addWhereFld( 'pr_page', array_keys( $this->titles ) );
522
523 $res = $this->select( __METHOD__ );
524 foreach ( $res as $row ) {
525 /** @var $title Title */
526 $title = $this->titles[$row->pr_page];
527 $a = array(
528 'type' => $row->pr_type,
529 'level' => $row->pr_level,
530 'expiry' => $wgContLang->formatExpiry( $row->pr_expiry, TS_ISO_8601 )
531 );
532 if ( $row->pr_cascade ) {
533 $a['cascade'] = true;
534 }
535 $this->protections[$title->getNamespace()][$title->getDBkey()][] = $a;
536 }
537 // Also check old restrictions
538 foreach ( $this->titles as $pageId => $title ) {
539 if ( $this->pageRestrictions[$pageId] ) {
540 $namespace = $title->getNamespace();
541 $dbKey = $title->getDBkey();
542 $restrictions = explode( ':', trim( $this->pageRestrictions[$pageId] ) );
543 foreach ( $restrictions as $restrict ) {
544 $temp = explode( '=', trim( $restrict ) );
545 if ( count( $temp ) == 1 ) {
546 // old old format should be treated as edit/move restriction
547 $restriction = trim( $temp[0] );
548
549 if ( $restriction == '' ) {
550 continue;
551 }
552 $this->protections[$namespace][$dbKey][] = array(
553 'type' => 'edit',
554 'level' => $restriction,
555 'expiry' => 'infinity',
556 );
557 $this->protections[$namespace][$dbKey][] = array(
558 'type' => 'move',
559 'level' => $restriction,
560 'expiry' => 'infinity',
561 );
562 } else {
563 $restriction = trim( $temp[1] );
564 if ( $restriction == '' ) {
565 continue;
566 }
567 $this->protections[$namespace][$dbKey][] = array(
568 'type' => $temp[0],
569 'level' => $restriction,
570 'expiry' => 'infinity',
571 );
572 }
573 }
574 }
575 }
576 }
577
578 // Get protections for missing titles
579 if ( count( $this->missing ) ) {
580 $this->resetQueryParams();
581 $lb = new LinkBatch( $this->missing );
582 $this->addTables( 'protected_titles' );
583 $this->addFields( array( 'pt_title', 'pt_namespace', 'pt_create_perm', 'pt_expiry' ) );
584 $this->addWhere( $lb->constructSet( 'pt', $db ) );
585 $res = $this->select( __METHOD__ );
586 foreach ( $res as $row ) {
587 $this->protections[$row->pt_namespace][$row->pt_title][] = array(
588 'type' => 'create',
589 'level' => $row->pt_create_perm,
590 'expiry' => $wgContLang->formatExpiry( $row->pt_expiry, TS_ISO_8601 )
591 );
592 }
593 }
594
595 // Separate good and missing titles into files and other pages
596 // and populate $this->restrictionTypes
597 $images = $others = array();
598 foreach ( $this->everything as $title ) {
599 if ( $title->getNamespace() == NS_FILE ) {
600 $images[] = $title->getDBkey();
601 } else {
602 $others[] = $title;
603 }
604 // Applicable protection types
605 $this->restrictionTypes[$title->getNamespace()][$title->getDBkey()] =
606 array_values( $title->getRestrictionTypes() );
607 }
608
609 if ( count( $others ) ) {
610 // Non-images: check templatelinks
611 $lb = new LinkBatch( $others );
612 $this->resetQueryParams();
613 $this->addTables( array( 'page_restrictions', 'page', 'templatelinks' ) );
614 $this->addFields( array( 'pr_type', 'pr_level', 'pr_expiry',
615 'page_title', 'page_namespace',
616 'tl_title', 'tl_namespace' ) );
617 $this->addWhere( $lb->constructSet( 'tl', $db ) );
618 $this->addWhere( 'pr_page = page_id' );
619 $this->addWhere( 'pr_page = tl_from' );
620 $this->addWhereFld( 'pr_cascade', 1 );
621
622 $res = $this->select( __METHOD__ );
623 foreach ( $res as $row ) {
624 $source = Title::makeTitle( $row->page_namespace, $row->page_title );
625 $this->protections[$row->tl_namespace][$row->tl_title][] = array(
626 'type' => $row->pr_type,
627 'level' => $row->pr_level,
628 'expiry' => $wgContLang->formatExpiry( $row->pr_expiry, TS_ISO_8601 ),
629 'source' => $source->getPrefixedText()
630 );
631 }
632 }
633
634 if ( count( $images ) ) {
635 // Images: check imagelinks
636 $this->resetQueryParams();
637 $this->addTables( array( 'page_restrictions', 'page', 'imagelinks' ) );
638 $this->addFields( array( 'pr_type', 'pr_level', 'pr_expiry',
639 'page_title', 'page_namespace', 'il_to' ) );
640 $this->addWhere( 'pr_page = page_id' );
641 $this->addWhere( 'pr_page = il_from' );
642 $this->addWhereFld( 'pr_cascade', 1 );
643 $this->addWhereFld( 'il_to', $images );
644
645 $res = $this->select( __METHOD__ );
646 foreach ( $res as $row ) {
647 $source = Title::makeTitle( $row->page_namespace, $row->page_title );
648 $this->protections[NS_FILE][$row->il_to][] = array(
649 'type' => $row->pr_type,
650 'level' => $row->pr_level,
651 'expiry' => $wgContLang->formatExpiry( $row->pr_expiry, TS_ISO_8601 ),
652 'source' => $source->getPrefixedText()
653 );
654 }
655 }
656 }
657
658 /**
659 * Get talk page IDs (if requested) and subject page IDs (if requested)
660 * and put them in $talkids and $subjectids
661 */
662 private function getTSIDs() {
663 $getTitles = $this->talkids = $this->subjectids = array();
664
665 /** @var $t Title */
666 foreach ( $this->everything as $t ) {
667 if ( MWNamespace::isTalk( $t->getNamespace() ) ) {
668 if ( $this->fld_subjectid ) {
669 $getTitles[] = $t->getSubjectPage();
670 }
671 } elseif ( $this->fld_talkid ) {
672 $getTitles[] = $t->getTalkPage();
673 }
674 }
675 if ( !count( $getTitles ) ) {
676 return;
677 }
678
679 $db = $this->getDB();
680
681 // Construct a custom WHERE clause that matches
682 // all titles in $getTitles
683 $lb = new LinkBatch( $getTitles );
684 $this->resetQueryParams();
685 $this->addTables( 'page' );
686 $this->addFields( array( 'page_title', 'page_namespace', 'page_id' ) );
687 $this->addWhere( $lb->constructSet( 'page', $db ) );
688 $res = $this->select( __METHOD__ );
689 foreach ( $res as $row ) {
690 if ( MWNamespace::isTalk( $row->page_namespace ) ) {
691 $this->talkids[MWNamespace::getSubject( $row->page_namespace )][$row->page_title] =
692 intval( $row->page_id );
693 } else {
694 $this->subjectids[MWNamespace::getTalk( $row->page_namespace )][$row->page_title] =
695 intval( $row->page_id );
696 }
697 }
698 }
699
700 private function getDisplayTitle() {
701 $this->displaytitles = array();
702
703 $pageIds = array_keys( $this->titles );
704
705 if ( !count( $pageIds ) ) {
706 return;
707 }
708
709 $this->resetQueryParams();
710 $this->addTables( 'page_props' );
711 $this->addFields( array( 'pp_page', 'pp_value' ) );
712 $this->addWhereFld( 'pp_page', $pageIds );
713 $this->addWhereFld( 'pp_propname', 'displaytitle' );
714 $res = $this->select( __METHOD__ );
715
716 foreach ( $res as $row ) {
717 $this->displaytitles[$row->pp_page] = $row->pp_value;
718 }
719 }
720
721 /**
722 * Get information about watched status and put it in $this->watched
723 * and $this->notificationtimestamps
724 */
725 private function getWatchedInfo() {
726 $user = $this->getUser();
727
728 if ( $user->isAnon() || count( $this->everything ) == 0
729 || !$user->isAllowed( 'viewmywatchlist' )
730 ) {
731 return;
732 }
733
734 $this->watched = array();
735 $this->notificationtimestamps = array();
736 $db = $this->getDB();
737
738 $lb = new LinkBatch( $this->everything );
739
740 $this->resetQueryParams();
741 $this->addTables( array( 'watchlist' ) );
742 $this->addFields( array( 'wl_title', 'wl_namespace' ) );
743 $this->addFieldsIf( 'wl_notificationtimestamp', $this->fld_notificationtimestamp );
744 $this->addWhere( array(
745 $lb->constructSet( 'wl', $db ),
746 'wl_user' => $user->getID()
747 ) );
748
749 $res = $this->select( __METHOD__ );
750
751 foreach ( $res as $row ) {
752 if ( $this->fld_watched ) {
753 $this->watched[$row->wl_namespace][$row->wl_title] = true;
754 }
755 if ( $this->fld_notificationtimestamp ) {
756 $this->notificationtimestamps[$row->wl_namespace][$row->wl_title] =
757 $row->wl_notificationtimestamp;
758 }
759 }
760 }
761
762 /**
763 * Get the count of watchers and put it in $this->watchers
764 */
765 private function getWatcherInfo() {
766 if ( count( $this->everything ) == 0 ) {
767 return;
768 }
769
770 $user = $this->getUser();
771 $canUnwatchedpages = $user->isAllowed( 'unwatchedpages' );
772 $unwatchedPageThreshold = $this->getConfig()->get( 'UnwatchedPageThreshold' );
773 if ( !$canUnwatchedpages && !is_int( $unwatchedPageThreshold ) ) {
774 return;
775 }
776
777 $this->watchers = array();
778 $this->showZeroWatchers = $canUnwatchedpages;
779 $db = $this->getDB();
780
781 $lb = new LinkBatch( $this->everything );
782
783 $this->resetQueryParams();
784 $this->addTables( array( 'watchlist' ) );
785 $this->addFields( array( 'wl_title', 'wl_namespace', 'count' => 'COUNT(*)' ) );
786 $this->addWhere( array(
787 $lb->constructSet( 'wl', $db )
788 ) );
789 $this->addOption( 'GROUP BY', array( 'wl_namespace', 'wl_title' ) );
790 if ( !$canUnwatchedpages ) {
791 $this->addOption( 'HAVING', "COUNT(*) >= $unwatchedPageThreshold" );
792 }
793
794 $res = $this->select( __METHOD__ );
795
796 foreach ( $res as $row ) {
797 $this->watchers[$row->wl_namespace][$row->wl_title] = (int)$row->count;
798 }
799 }
800
801 public function getCacheMode( $params ) {
802 // Other props depend on something about the current user
803 $publicProps = array(
804 'protection',
805 'talkid',
806 'subjectid',
807 'url',
808 'preload',
809 'displaytitle',
810 );
811 if ( array_diff( (array)$params['prop'], $publicProps ) ) {
812 return 'private';
813 }
814
815 // testactions also depends on the current user
816 if ( $params['testactions'] ) {
817 return 'private';
818 }
819
820 if ( !is_null( $params['token'] ) ) {
821 return 'private';
822 }
823
824 return 'public';
825 }
826
827 public function getAllowedParams() {
828 return array(
829 'prop' => array(
830 ApiBase::PARAM_DFLT => null,
831 ApiBase::PARAM_ISMULTI => true,
832 ApiBase::PARAM_TYPE => array(
833 'protection',
834 'talkid',
835 'watched', # private
836 'watchers', # private
837 'notificationtimestamp', # private
838 'subjectid',
839 'url',
840 'readable', # private
841 'preload',
842 'displaytitle',
843 // If you add more properties here, please consider whether they
844 // need to be added to getCacheMode()
845 ),
846 ApiBase::PARAM_HELP_MSG_PER_VALUE => array(),
847 ),
848 'testactions' => array(
849 ApiBase::PARAM_TYPE => 'string',
850 ApiBase::PARAM_ISMULTI => true,
851 ),
852 'token' => array(
853 ApiBase::PARAM_DEPRECATED => true,
854 ApiBase::PARAM_DFLT => null,
855 ApiBase::PARAM_ISMULTI => true,
856 ApiBase::PARAM_TYPE => array_keys( $this->getTokenFunctions() )
857 ),
858 'continue' => array(
859 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
860 ),
861 );
862 }
863
864 protected function getExamplesMessages() {
865 return array(
866 'action=query&prop=info&titles=Main%20Page'
867 => 'apihelp-query+info-example-simple',
868 'action=query&prop=info&inprop=protection&titles=Main%20Page'
869 => 'apihelp-query+info-example-protection',
870 );
871 }
872
873 public function getHelpUrls() {
874 return 'https://www.mediawiki.org/wiki/API:Info';
875 }
876 }