Merge "Add supportsDirectEditing methods to ContentHandler"
[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, $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'] = '';
399 }
400 if ( $this->pageIsNew[$pageid] ) {
401 $pageInfo['new'] = '';
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 && isset( $this->watched[$ns][$dbkey] ) ) {
435 $pageInfo['watched'] = '';
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 && $title->userCan( 'read', $this->getUser() ) ) {
468 $pageInfo['readable'] = '';
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 if ( $title->userCan( $action, $user ) ) {
501 $pageInfo['actions'][$action] = '';
502 }
503 }
504 }
505
506 return $pageInfo;
507 }
508
509 /**
510 * Get information about protections and put it in $protections
511 */
512 private function getProtectionInfo() {
513 global $wgContLang;
514 $this->protections = array();
515 $db = $this->getDB();
516
517 // Get normal protections for existing titles
518 if ( count( $this->titles ) ) {
519 $this->resetQueryParams();
520 $this->addTables( 'page_restrictions' );
521 $this->addFields( array( 'pr_page', 'pr_type', 'pr_level',
522 'pr_expiry', 'pr_cascade' ) );
523 $this->addWhereFld( 'pr_page', array_keys( $this->titles ) );
524
525 $res = $this->select( __METHOD__ );
526 foreach ( $res as $row ) {
527 /** @var $title Title */
528 $title = $this->titles[$row->pr_page];
529 $a = array(
530 'type' => $row->pr_type,
531 'level' => $row->pr_level,
532 'expiry' => $wgContLang->formatExpiry( $row->pr_expiry, TS_ISO_8601 )
533 );
534 if ( $row->pr_cascade ) {
535 $a['cascade'] = '';
536 }
537 $this->protections[$title->getNamespace()][$title->getDBkey()][] = $a;
538 }
539 // Also check old restrictions
540 foreach ( $this->titles as $pageId => $title ) {
541 if ( $this->pageRestrictions[$pageId] ) {
542 $namespace = $title->getNamespace();
543 $dbKey = $title->getDBkey();
544 $restrictions = explode( ':', trim( $this->pageRestrictions[$pageId] ) );
545 foreach ( $restrictions as $restrict ) {
546 $temp = explode( '=', trim( $restrict ) );
547 if ( count( $temp ) == 1 ) {
548 // old old format should be treated as edit/move restriction
549 $restriction = trim( $temp[0] );
550
551 if ( $restriction == '' ) {
552 continue;
553 }
554 $this->protections[$namespace][$dbKey][] = array(
555 'type' => 'edit',
556 'level' => $restriction,
557 'expiry' => 'infinity',
558 );
559 $this->protections[$namespace][$dbKey][] = array(
560 'type' => 'move',
561 'level' => $restriction,
562 'expiry' => 'infinity',
563 );
564 } else {
565 $restriction = trim( $temp[1] );
566 if ( $restriction == '' ) {
567 continue;
568 }
569 $this->protections[$namespace][$dbKey][] = array(
570 'type' => $temp[0],
571 'level' => $restriction,
572 'expiry' => 'infinity',
573 );
574 }
575 }
576 }
577 }
578 }
579
580 // Get protections for missing titles
581 if ( count( $this->missing ) ) {
582 $this->resetQueryParams();
583 $lb = new LinkBatch( $this->missing );
584 $this->addTables( 'protected_titles' );
585 $this->addFields( array( 'pt_title', 'pt_namespace', 'pt_create_perm', 'pt_expiry' ) );
586 $this->addWhere( $lb->constructSet( 'pt', $db ) );
587 $res = $this->select( __METHOD__ );
588 foreach ( $res as $row ) {
589 $this->protections[$row->pt_namespace][$row->pt_title][] = array(
590 'type' => 'create',
591 'level' => $row->pt_create_perm,
592 'expiry' => $wgContLang->formatExpiry( $row->pt_expiry, TS_ISO_8601 )
593 );
594 }
595 }
596
597 // Separate good and missing titles into files and other pages
598 // and populate $this->restrictionTypes
599 $images = $others = array();
600 foreach ( $this->everything as $title ) {
601 if ( $title->getNamespace() == NS_FILE ) {
602 $images[] = $title->getDBkey();
603 } else {
604 $others[] = $title;
605 }
606 // Applicable protection types
607 $this->restrictionTypes[$title->getNamespace()][$title->getDBkey()] =
608 array_values( $title->getRestrictionTypes() );
609 }
610
611 if ( count( $others ) ) {
612 // Non-images: check templatelinks
613 $lb = new LinkBatch( $others );
614 $this->resetQueryParams();
615 $this->addTables( array( 'page_restrictions', 'page', 'templatelinks' ) );
616 $this->addFields( array( 'pr_type', 'pr_level', 'pr_expiry',
617 'page_title', 'page_namespace',
618 'tl_title', 'tl_namespace' ) );
619 $this->addWhere( $lb->constructSet( 'tl', $db ) );
620 $this->addWhere( 'pr_page = page_id' );
621 $this->addWhere( 'pr_page = tl_from' );
622 $this->addWhereFld( 'pr_cascade', 1 );
623
624 $res = $this->select( __METHOD__ );
625 foreach ( $res as $row ) {
626 $source = Title::makeTitle( $row->page_namespace, $row->page_title );
627 $this->protections[$row->tl_namespace][$row->tl_title][] = array(
628 'type' => $row->pr_type,
629 'level' => $row->pr_level,
630 'expiry' => $wgContLang->formatExpiry( $row->pr_expiry, TS_ISO_8601 ),
631 'source' => $source->getPrefixedText()
632 );
633 }
634 }
635
636 if ( count( $images ) ) {
637 // Images: check imagelinks
638 $this->resetQueryParams();
639 $this->addTables( array( 'page_restrictions', 'page', 'imagelinks' ) );
640 $this->addFields( array( 'pr_type', 'pr_level', 'pr_expiry',
641 'page_title', 'page_namespace', 'il_to' ) );
642 $this->addWhere( 'pr_page = page_id' );
643 $this->addWhere( 'pr_page = il_from' );
644 $this->addWhereFld( 'pr_cascade', 1 );
645 $this->addWhereFld( 'il_to', $images );
646
647 $res = $this->select( __METHOD__ );
648 foreach ( $res as $row ) {
649 $source = Title::makeTitle( $row->page_namespace, $row->page_title );
650 $this->protections[NS_FILE][$row->il_to][] = array(
651 'type' => $row->pr_type,
652 'level' => $row->pr_level,
653 'expiry' => $wgContLang->formatExpiry( $row->pr_expiry, TS_ISO_8601 ),
654 'source' => $source->getPrefixedText()
655 );
656 }
657 }
658 }
659
660 /**
661 * Get talk page IDs (if requested) and subject page IDs (if requested)
662 * and put them in $talkids and $subjectids
663 */
664 private function getTSIDs() {
665 $getTitles = $this->talkids = $this->subjectids = array();
666
667 /** @var $t Title */
668 foreach ( $this->everything as $t ) {
669 if ( MWNamespace::isTalk( $t->getNamespace() ) ) {
670 if ( $this->fld_subjectid ) {
671 $getTitles[] = $t->getSubjectPage();
672 }
673 } elseif ( $this->fld_talkid ) {
674 $getTitles[] = $t->getTalkPage();
675 }
676 }
677 if ( !count( $getTitles ) ) {
678 return;
679 }
680
681 $db = $this->getDB();
682
683 // Construct a custom WHERE clause that matches
684 // all titles in $getTitles
685 $lb = new LinkBatch( $getTitles );
686 $this->resetQueryParams();
687 $this->addTables( 'page' );
688 $this->addFields( array( 'page_title', 'page_namespace', 'page_id' ) );
689 $this->addWhere( $lb->constructSet( 'page', $db ) );
690 $res = $this->select( __METHOD__ );
691 foreach ( $res as $row ) {
692 if ( MWNamespace::isTalk( $row->page_namespace ) ) {
693 $this->talkids[MWNamespace::getSubject( $row->page_namespace )][$row->page_title] =
694 intval( $row->page_id );
695 } else {
696 $this->subjectids[MWNamespace::getTalk( $row->page_namespace )][$row->page_title] =
697 intval( $row->page_id );
698 }
699 }
700 }
701
702 private function getDisplayTitle() {
703 $this->displaytitles = array();
704
705 $pageIds = array_keys( $this->titles );
706
707 if ( !count( $pageIds ) ) {
708 return;
709 }
710
711 $this->resetQueryParams();
712 $this->addTables( 'page_props' );
713 $this->addFields( array( 'pp_page', 'pp_value' ) );
714 $this->addWhereFld( 'pp_page', $pageIds );
715 $this->addWhereFld( 'pp_propname', 'displaytitle' );
716 $res = $this->select( __METHOD__ );
717
718 foreach ( $res as $row ) {
719 $this->displaytitles[$row->pp_page] = $row->pp_value;
720 }
721 }
722
723 /**
724 * Get information about watched status and put it in $this->watched
725 * and $this->notificationtimestamps
726 */
727 private function getWatchedInfo() {
728 $user = $this->getUser();
729
730 if ( $user->isAnon() || count( $this->everything ) == 0
731 || !$user->isAllowed( 'viewmywatchlist' )
732 ) {
733 return;
734 }
735
736 $this->watched = array();
737 $this->notificationtimestamps = array();
738 $db = $this->getDB();
739
740 $lb = new LinkBatch( $this->everything );
741
742 $this->resetQueryParams();
743 $this->addTables( array( 'watchlist' ) );
744 $this->addFields( array( 'wl_title', 'wl_namespace' ) );
745 $this->addFieldsIf( 'wl_notificationtimestamp', $this->fld_notificationtimestamp );
746 $this->addWhere( array(
747 $lb->constructSet( 'wl', $db ),
748 'wl_user' => $user->getID()
749 ) );
750
751 $res = $this->select( __METHOD__ );
752
753 foreach ( $res as $row ) {
754 if ( $this->fld_watched ) {
755 $this->watched[$row->wl_namespace][$row->wl_title] = true;
756 }
757 if ( $this->fld_notificationtimestamp ) {
758 $this->notificationtimestamps[$row->wl_namespace][$row->wl_title] =
759 $row->wl_notificationtimestamp;
760 }
761 }
762 }
763
764 /**
765 * Get the count of watchers and put it in $this->watchers
766 */
767 private function getWatcherInfo() {
768 if ( count( $this->everything ) == 0 ) {
769 return;
770 }
771
772 $user = $this->getUser();
773 $canUnwatchedpages = $user->isAllowed( 'unwatchedpages' );
774 $unwatchedPageThreshold = $this->getConfig()->get( 'UnwatchedPageThreshold' );
775 if ( !$canUnwatchedpages && !is_int( $unwatchedPageThreshold ) ) {
776 return;
777 }
778
779 $this->watchers = array();
780 $this->showZeroWatchers = $canUnwatchedpages;
781 $db = $this->getDB();
782
783 $lb = new LinkBatch( $this->everything );
784
785 $this->resetQueryParams();
786 $this->addTables( array( 'watchlist' ) );
787 $this->addFields( array( 'wl_title', 'wl_namespace', 'count' => 'COUNT(*)' ) );
788 $this->addWhere( array(
789 $lb->constructSet( 'wl', $db )
790 ) );
791 $this->addOption( 'GROUP BY', array( 'wl_namespace', 'wl_title' ) );
792 if ( !$canUnwatchedpages ) {
793 $this->addOption( 'HAVING', "COUNT(*) >= $unwatchedPageThreshold" );
794 }
795
796 $res = $this->select( __METHOD__ );
797
798 foreach ( $res as $row ) {
799 $this->watchers[$row->wl_namespace][$row->wl_title] = (int)$row->count;
800 }
801 }
802
803 public function getCacheMode( $params ) {
804 $publicProps = array(
805 'protection',
806 'talkid',
807 'subjectid',
808 'url',
809 'preload',
810 'displaytitle',
811 );
812 if ( !is_null( $params['prop'] ) ) {
813 foreach ( $params['prop'] as $prop ) {
814 if ( !in_array( $prop, $publicProps ) ) {
815 return 'private';
816 }
817 }
818 }
819 if ( !is_null( $params['token'] ) ) {
820 return 'private';
821 }
822
823 return 'public';
824 }
825
826 public function getAllowedParams() {
827 return array(
828 'prop' => array(
829 ApiBase::PARAM_DFLT => null,
830 ApiBase::PARAM_ISMULTI => true,
831 ApiBase::PARAM_TYPE => array(
832 'protection',
833 'talkid',
834 'watched', # private
835 'watchers', # private
836 'notificationtimestamp', # private
837 'subjectid',
838 'url',
839 'readable', # private
840 'preload',
841 'displaytitle',
842 // If you add more properties here, please consider whether they
843 // need to be added to getCacheMode()
844 ),
845 ApiBase::PARAM_HELP_MSG_PER_VALUE => array(),
846 ),
847 'testactions' => array(
848 ApiBase::PARAM_TYPE => 'string',
849 ApiBase::PARAM_ISMULTI => true,
850 ),
851 'token' => array(
852 ApiBase::PARAM_DEPRECATED => true,
853 ApiBase::PARAM_DFLT => null,
854 ApiBase::PARAM_ISMULTI => true,
855 ApiBase::PARAM_TYPE => array_keys( $this->getTokenFunctions() )
856 ),
857 'continue' => array(
858 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
859 ),
860 );
861 }
862
863 protected function getExamplesMessages() {
864 return array(
865 'action=query&prop=info&titles=Main%20Page'
866 => 'apihelp-query+info-example-simple',
867 'action=query&prop=info&inprop=protection&titles=Main%20Page'
868 => 'apihelp-query+info-example-protection',
869 );
870 }
871
872 public function getHelpUrls() {
873 return 'https://www.mediawiki.org/wiki/API:Properties#info_.2F_in';
874 }
875 }