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