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