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