5 * Created on Sep 25, 2006
7 * Copyright © 2006 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
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.
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.
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
28 * A query module to show basic page information.
32 class ApiQueryInfo
extends ApiQueryBase
{
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;
50 private $pageRestrictions, $pageIsRedir, $pageIsNew, $pageTouched,
51 $pageLatest, $pageLength;
53 private $protections, $restrictionTypes, $watched, $watchers, $visitingwatchers,
54 $notificationtimestamps, $talkids, $subjectids, $displaytitles;
55 private $showZeroWatchers = false;
57 private $tokenFunctions;
59 private $countTestedActions = 0;
61 public function __construct( ApiQuery
$query, $moduleName ) {
62 parent
::__construct( $query, $moduleName, 'in' );
66 * @param ApiPageSet $pageSet
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' );
83 if ( $config->get( 'PageLanguageUseDB' ) ) {
84 $pageSet->requestField( 'page_lang' );
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)
95 protected function getTokenFunctions() {
96 // Don't call the hooks twice
97 if ( isset( $this->tokenFunctions
) ) {
98 return $this->tokenFunctions
;
101 // If we're in a mode that breaks the same-origin policy, no tokens can
103 if ( $this->lacksSameOriginSecurity() ) {
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' ],
118 Hooks
::run( 'APIQueryInfoTokens', [ &$this->tokenFunctions
] );
120 return $this->tokenFunctions
;
123 static protected $cachedTokens = [];
126 * @deprecated since 1.24
128 public static function resetTokenCache() {
129 ApiQueryInfo
::$cachedTokens = [];
133 * @deprecated since 1.24
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
140 if ( !$wgUser->isAllowed( 'edit' ) ) {
144 // The token is always the same, let's exploit that
145 if ( !isset( ApiQueryInfo
::$cachedTokens['edit'] ) ) {
146 ApiQueryInfo
::$cachedTokens['edit'] = $wgUser->getEditToken();
149 return ApiQueryInfo
::$cachedTokens['edit'];
153 * @deprecated since 1.24
155 public static function getDeleteToken( $pageid, $title ) {
157 if ( !$wgUser->isAllowed( 'delete' ) ) {
161 // The token is always the same, let's exploit that
162 if ( !isset( ApiQueryInfo
::$cachedTokens['delete'] ) ) {
163 ApiQueryInfo
::$cachedTokens['delete'] = $wgUser->getEditToken();
166 return ApiQueryInfo
::$cachedTokens['delete'];
170 * @deprecated since 1.24
172 public static function getProtectToken( $pageid, $title ) {
174 if ( !$wgUser->isAllowed( 'protect' ) ) {
178 // The token is always the same, let's exploit that
179 if ( !isset( ApiQueryInfo
::$cachedTokens['protect'] ) ) {
180 ApiQueryInfo
::$cachedTokens['protect'] = $wgUser->getEditToken();
183 return ApiQueryInfo
::$cachedTokens['protect'];
187 * @deprecated since 1.24
189 public static function getMoveToken( $pageid, $title ) {
191 if ( !$wgUser->isAllowed( 'move' ) ) {
195 // The token is always the same, let's exploit that
196 if ( !isset( ApiQueryInfo
::$cachedTokens['move'] ) ) {
197 ApiQueryInfo
::$cachedTokens['move'] = $wgUser->getEditToken();
200 return ApiQueryInfo
::$cachedTokens['move'];
204 * @deprecated since 1.24
206 public static function getBlockToken( $pageid, $title ) {
208 if ( !$wgUser->isAllowed( 'block' ) ) {
212 // The token is always the same, let's exploit that
213 if ( !isset( ApiQueryInfo
::$cachedTokens['block'] ) ) {
214 ApiQueryInfo
::$cachedTokens['block'] = $wgUser->getEditToken();
217 return ApiQueryInfo
::$cachedTokens['block'];
221 * @deprecated since 1.24
223 public static function getUnblockToken( $pageid, $title ) {
224 // Currently, this is exactly the same as the block token
225 return self
::getBlockToken( $pageid, $title );
229 * @deprecated since 1.24
231 public static function getEmailToken( $pageid, $title ) {
233 if ( !$wgUser->canSendEmail() ||
$wgUser->isBlockedFromEmailuser() ) {
237 // The token is always the same, let's exploit that
238 if ( !isset( ApiQueryInfo
::$cachedTokens['email'] ) ) {
239 ApiQueryInfo
::$cachedTokens['email'] = $wgUser->getEditToken();
242 return ApiQueryInfo
::$cachedTokens['email'];
246 * @deprecated since 1.24
248 public static function getImportToken( $pageid, $title ) {
250 if ( !$wgUser->isAllowedAny( 'import', 'importupload' ) ) {
254 // The token is always the same, let's exploit that
255 if ( !isset( ApiQueryInfo
::$cachedTokens['import'] ) ) {
256 ApiQueryInfo
::$cachedTokens['import'] = $wgUser->getEditToken();
259 return ApiQueryInfo
::$cachedTokens['import'];
263 * @deprecated since 1.24
265 public static function getWatchToken( $pageid, $title ) {
267 if ( !$wgUser->isLoggedIn() ) {
271 // The token is always the same, let's exploit that
272 if ( !isset( ApiQueryInfo
::$cachedTokens['watch'] ) ) {
273 ApiQueryInfo
::$cachedTokens['watch'] = $wgUser->getEditToken( 'watch' );
276 return ApiQueryInfo
::$cachedTokens['watch'];
280 * @deprecated since 1.24
282 public static function getOptionsToken( $pageid, $title ) {
284 if ( !$wgUser->isLoggedIn() ) {
288 // The token is always the same, let's exploit that
289 if ( !isset( ApiQueryInfo
::$cachedTokens['options'] ) ) {
290 ApiQueryInfo
::$cachedTokens['options'] = $wgUser->getEditToken();
293 return ApiQueryInfo
::$cachedTokens['options'];
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'] );
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();
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
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 ) {
330 unset( $this->titles
[$pageid] );
331 unset( $this->missing
[$pageid] );
332 unset( $this->everything
[$pageid] );
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' )
341 $this->pageIsNew
= $pageSet->getCustomField( 'page_is_new' );
343 $this->pageTouched
= $pageSet->getCustomField( 'page_touched' );
344 $this->pageLatest
= $pageSet->getCustomField( 'page_latest' );
345 $this->pageLength
= $pageSet->getCustomField( 'page_len' );
347 // Get protection info if requested
348 if ( $this->fld_protection
) {
349 $this->getProtectionInfo();
352 if ( $this->fld_watched ||
$this->fld_notificationtimestamp
) {
353 $this->getWatchedInfo();
356 if ( $this->fld_watchers
) {
357 $this->getWatcherInfo();
360 if ( $this->fld_visitingwatchers
) {
361 $this->getVisitingWatcherInfo();
364 // Run the talkid/subjectid query if requested
365 if ( $this->fld_talkid ||
$this->fld_subjectid
) {
369 if ( $this->fld_displaytitle
) {
370 $this->getDisplayTitle();
373 /** @var $title Title */
374 foreach ( $this->everything
as $pageid => $title ) {
375 $pageInfo = $this->extractPageInfo( $pageid, $title );
376 $fit = $pageInfo !== null && $result->addValue( [
379 ], $pageid, $pageInfo );
381 $this->setContinueEnumParameter( 'continue',
382 $title->getNamespace() . '|' .
390 * Get a result array with information about a title
391 * @param int $pageid Page ID (negative for missing titles)
392 * @param Title $title
395 private function extractPageInfo( $pageid, $title ) {
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();
402 $pageInfo['contentmodel'] = $title->getContentModel();
404 $pageLanguage = $title->getPageLanguage();
405 $pageInfo['pagelanguage'] = $pageLanguage->getCode();
406 $pageInfo['pagelanguagehtmlcode'] = $pageLanguage->getHtmlCode();
407 $pageInfo['pagelanguagedir'] = $pageLanguage->getDir();
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] );
414 if ( isset( $this->pageIsRedir
[$pageid] ) && $this->pageIsRedir
[$pageid] ) {
415 $pageInfo['redirect'] = true;
417 if ( $this->pageIsNew
[$pageid] ) {
418 $pageInfo['new'] = true;
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" );
430 $pageInfo[$t . 'token'] = $val;
435 if ( $this->fld_protection
) {
436 $pageInfo['protection'] = [];
437 if ( isset( $this->protections
[$ns][$dbkey] ) ) {
438 $pageInfo['protection'] =
439 $this->protections
[$ns][$dbkey];
441 ApiResult
::setIndexedTagName( $pageInfo['protection'], 'pr' );
443 $pageInfo['restrictiontypes'] = [];
444 if ( isset( $this->restrictionTypes
[$ns][$dbkey] ) ) {
445 $pageInfo['restrictiontypes'] =
446 $this->restrictionTypes
[$ns][$dbkey];
448 ApiResult
::setIndexedTagName( $pageInfo['restrictiontypes'], 'rt' );
451 if ( $this->fld_watched
) {
452 $pageInfo['watched'] = isset( $this->watched
[$ns][$dbkey] );
455 if ( $this->fld_watchers
) {
456 if ( $this->watchers
[$ns][$dbkey] !== 0 ||
$this->showZeroWatchers
) {
457 $pageInfo['watchers'] = $this->watchers
[$ns][$dbkey];
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;
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] );
477 if ( $this->fld_talkid
&& isset( $this->talkids
[$ns][$dbkey] ) ) {
478 $pageInfo['talkid'] = $this->talkids
[$ns][$dbkey];
481 if ( $this->fld_subjectid
&& isset( $this->subjectids
[$ns][$dbkey] ) ) {
482 $pageInfo['subjectid'] = $this->subjectids
[$ns][$dbkey];
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
);
490 if ( $this->fld_readable
) {
491 $pageInfo['readable'] = $title->userCan( 'read', $this->getUser() );
494 if ( $this->fld_preload
) {
495 if ( $titleExists ) {
496 $pageInfo['preload'] = '';
499 Hooks
::run( 'EditFormPreloadText', [ &$text, &$title ] );
501 $pageInfo['preload'] = $text;
505 if ( $this->fld_displaytitle
) {
506 if ( isset( $this->displaytitles
[$pageid] ) ) {
507 $pageInfo['displaytitle'] = $this->displaytitles
[$pageid];
509 $pageInfo['displaytitle'] = $title->getPrefixedText();
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
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 );
531 * Get information about protections and put it in $protections
533 private function getProtectionInfo() {
535 $this->protections
= [];
536 $db = $this->getDB();
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
) );
546 $res = $this->select( __METHOD__
);
547 foreach ( $res as $row ) {
548 /** @var $title Title */
549 $title = $this->titles
[$row->pr_page
];
551 'type' => $row->pr_type
,
552 'level' => $row->pr_level
,
553 'expiry' => $wgContLang->formatExpiry( $row->pr_expiry
, TS_ISO_8601
)
555 if ( $row->pr_cascade
) {
556 $a['cascade'] = true;
558 $this->protections
[$title->getNamespace()][$title->getDBkey()][] = $a;
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] );
572 if ( $restriction == '' ) {
575 $this->protections
[$namespace][$dbKey][] = [
577 'level' => $restriction,
578 'expiry' => 'infinity',
580 $this->protections
[$namespace][$dbKey][] = [
582 'level' => $restriction,
583 'expiry' => 'infinity',
586 $restriction = trim( $temp[1] );
587 if ( $restriction == '' ) {
590 $this->protections
[$namespace][$dbKey][] = [
592 'level' => $restriction,
593 'expiry' => 'infinity',
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
][] = [
612 'level' => $row->pt_create_perm
,
613 'expiry' => $wgContLang->formatExpiry( $row->pt_expiry
, TS_ISO_8601
)
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();
627 // Applicable protection types
628 $this->restrictionTypes
[$title->getNamespace()][$title->getDBkey()] =
629 array_values( $title->getRestrictionTypes() );
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 );
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()
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 );
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()
682 * Get talk page IDs (if requested) and subject page IDs (if requested)
683 * and put them in $talkids and $subjectids
685 private function getTSIDs() {
686 $getTitles = $this->talkids
= $this->subjectids
= [];
689 foreach ( $this->everything
as $t ) {
690 if ( MWNamespace
::isTalk( $t->getNamespace() ) ) {
691 if ( $this->fld_subjectid
) {
692 $getTitles[] = $t->getSubjectPage();
694 } elseif ( $this->fld_talkid
) {
695 $getTitles[] = $t->getTalkPage();
698 if ( !count( $getTitles ) ) {
702 $db = $this->getDB();
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
);
717 $this->subjectids
[MWNamespace
::getTalk( $row->page_namespace
)][$row->page_title
] =
718 intval( $row->page_id
);
723 private function getDisplayTitle() {
724 $this->displaytitles
= [];
726 $pageIds = array_keys( $this->titles
);
728 if ( !count( $pageIds ) ) {
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__
);
739 foreach ( $res as $row ) {
740 $this->displaytitles
[$row->pp_page
] = $row->pp_value
;
745 * Get information about watched status and put it in $this->watched
746 * and $this->notificationtimestamps
748 private function getWatchedInfo() {
749 $user = $this->getUser();
751 if ( $user->isAnon() ||
count( $this->everything
) == 0
752 ||
!$user->isAllowed( 'viewmywatchlist' )
758 $this->notificationtimestamps
= [];
759 $db = $this->getDB();
761 $lb = new LinkBatch( $this->everything
);
763 $this->resetQueryParams();
764 $this->addTables( [ 'watchlist' ] );
765 $this->addFields( [ 'wl_title', 'wl_namespace' ] );
766 $this->addFieldsIf( 'wl_notificationtimestamp', $this->fld_notificationtimestamp
);
768 $lb->constructSet( 'wl', $db ),
769 'wl_user' => $user->getId()
772 $res = $this->select( __METHOD__
);
774 foreach ( $res as $row ) {
775 if ( $this->fld_watched
) {
776 $this->watched
[$row->wl_namespace
][$row->wl_title
] = true;
778 if ( $this->fld_notificationtimestamp
) {
779 $this->notificationtimestamps
[$row->wl_namespace
][$row->wl_title
] =
780 $row->wl_notificationtimestamp
;
786 * Get the count of watchers and put it in $this->watchers
788 private function getWatcherInfo() {
789 if ( count( $this->everything
) == 0 ) {
793 $user = $this->getUser();
794 $canUnwatchedpages = $user->isAllowed( 'unwatchedpages' );
795 $unwatchedPageThreshold = $this->getConfig()->get( 'UnwatchedPageThreshold' );
796 if ( !$canUnwatchedpages && !is_int( $unwatchedPageThreshold ) ) {
800 $this->showZeroWatchers
= $canUnwatchedpages;
803 if ( !$canUnwatchedpages ) {
804 $countOptions['minimumWatchers'] = $unwatchedPageThreshold;
807 $this->watchers
= WatchedItemStore
::getDefaultInstance()->countWatchersMultiple(
814 * Get the count of watchers who have visited recent edits and put it in
815 * $this->visitingwatchers
817 * Based on InfoAction::pageCounts
819 private function getVisitingWatcherInfo() {
820 $config = $this->getConfig();
821 $user = $this->getUser();
822 $db = $this->getDB();
824 $canUnwatchedpages = $user->isAllowed( 'unwatchedpages' );
825 $unwatchedPageThreshold = $this->getConfig()->get( 'UnwatchedPageThreshold' );
826 if ( !$canUnwatchedpages && !is_int( $unwatchedPageThreshold ) ) {
830 $this->showZeroWatchers
= $canUnwatchedpages;
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
839 // For pages that exist
840 if ( $this->titles
) {
841 $lb = new LinkBatch( $this->titles
);
843 // Fetch last edit timestamps for pages
844 $this->resetQueryParams();
845 $this->addTables( [ 'page', 'revision' ] );
846 $this->addFields( [ 'page_namespace', 'page_title', 'rev_timestamp' ] );
848 'page_latest = rev_id',
849 $lb->constructSet( 'page', $db ),
851 $this->addOption( 'GROUP BY', [ 'page_namespace', 'page_title' ] );
852 $timestampRes = $this->select( __METHOD__
);
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' );
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;
864 foreach ( $timestamps as $ns_key => $namespace ) {
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)';
872 $whereStrings[] = "wl_namespace = '$ns_key' AND (" .
873 $db->makeList( $pageStrings, LIST_OR
) . ')';
877 // For nonexistant pages
878 if ( $this->missing
) {
879 $lb = new LinkBatch( $this->missing
);
880 $whereStrings[] = $lb->constructSet( 'wl', $db );
883 // Make the actual string and do the query
884 $whereString = $db->makeList( $whereStrings, LIST_OR
);
886 $this->resetQueryParams();
887 $this->addTables( [ 'watchlist' ] );
891 'count' => 'COUNT(*)'
893 $this->addWhere( [ $whereString ] );
894 $this->addOption( 'GROUP BY', [ 'wl_namespace', 'wl_title' ] );
895 if ( !$canUnwatchedpages ) {
896 $this->addOption( 'HAVING', "COUNT(*) >= $unwatchedPageThreshold" );
899 $res = $this->select( __METHOD__
);
900 foreach ( $res as $row ) {
901 $this->visitingwatchers
[$row->wl_namespace
][$row->wl_title
] = (int)$row->count
;
905 public function getCacheMode( $params ) {
906 // Other props depend on something about the current user
915 if ( array_diff( (array)$params['prop'], $publicProps ) ) {
919 // testactions also depends on the current user
920 if ( $params['testactions'] ) {
924 if ( !is_null( $params['token'] ) ) {
931 public function getAllowedParams() {
934 ApiBase
::PARAM_ISMULTI
=> true,
935 ApiBase
::PARAM_TYPE
=> [
939 'watchers', # private
940 'visitingwatchers', # private
941 'notificationtimestamp', # private
944 'readable', # private
947 // If you add more properties here, please consider whether they
948 // need to be added to getCacheMode()
950 ApiBase
::PARAM_HELP_MSG_PER_VALUE
=> [],
953 ApiBase
::PARAM_TYPE
=> 'string',
954 ApiBase
::PARAM_ISMULTI
=> true,
957 ApiBase
::PARAM_DEPRECATED
=> true,
958 ApiBase
::PARAM_ISMULTI
=> true,
959 ApiBase
::PARAM_TYPE
=> array_keys( $this->getTokenFunctions() )
962 ApiBase
::PARAM_HELP_MSG
=> 'api-help-param-continue',
967 protected function getExamplesMessages() {
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',
976 public function getHelpUrls() {
977 return 'https://www.mediawiki.org/wiki/API:Info';