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