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 ( isset( $this->watchers
[$ns][$dbkey] ) ) {
457 $pageInfo['watchers'] = $this->watchers
[$ns][$dbkey];
458 } elseif ( $this->showZeroWatchers
) {
459 $pageInfo['watchers'] = 0;
463 if ( $this->fld_visitingwatchers
) {
464 if ( isset( $this->visitingwatchers
[$ns][$dbkey] ) ) {
465 $pageInfo['visitingwatchers'] = $this->visitingwatchers
[$ns][$dbkey];
466 } elseif ( $this->showZeroWatchers
) {
467 $pageInfo['visitingwatchers'] = 0;
471 if ( $this->fld_notificationtimestamp
) {
472 $pageInfo['notificationtimestamp'] = '';
473 if ( isset( $this->notificationtimestamps
[$ns][$dbkey] ) ) {
474 $pageInfo['notificationtimestamp'] =
475 wfTimestamp( TS_ISO_8601
, $this->notificationtimestamps
[$ns][$dbkey] );
479 if ( $this->fld_talkid
&& isset( $this->talkids
[$ns][$dbkey] ) ) {
480 $pageInfo['talkid'] = $this->talkids
[$ns][$dbkey];
483 if ( $this->fld_subjectid
&& isset( $this->subjectids
[$ns][$dbkey] ) ) {
484 $pageInfo['subjectid'] = $this->subjectids
[$ns][$dbkey];
487 if ( $this->fld_url
) {
488 $pageInfo['fullurl'] = wfExpandUrl( $title->getFullURL(), PROTO_CURRENT
);
489 $pageInfo['editurl'] = wfExpandUrl( $title->getFullURL( 'action=edit' ), PROTO_CURRENT
);
490 $pageInfo['canonicalurl'] = wfExpandUrl( $title->getFullURL(), PROTO_CANONICAL
);
492 if ( $this->fld_readable
) {
493 $pageInfo['readable'] = $title->userCan( 'read', $this->getUser() );
496 if ( $this->fld_preload
) {
497 if ( $titleExists ) {
498 $pageInfo['preload'] = '';
501 Hooks
::run( 'EditFormPreloadText', [ &$text, &$title ] );
503 $pageInfo['preload'] = $text;
507 if ( $this->fld_displaytitle
) {
508 if ( isset( $this->displaytitles
[$pageid] ) ) {
509 $pageInfo['displaytitle'] = $this->displaytitles
[$pageid];
511 $pageInfo['displaytitle'] = $title->getPrefixedText();
515 if ( $this->params
['testactions'] ) {
516 $limit = $this->getMain()->canApiHighLimits() ? self
::LIMIT_SML1
: self
::LIMIT_SML2
;
517 if ( $this->countTestedActions
>= $limit ) {
518 return null; // force a continuation
521 $user = $this->getUser();
522 $pageInfo['actions'] = [];
523 foreach ( $this->params
['testactions'] as $action ) {
524 $this->countTestedActions++
;
525 $pageInfo['actions'][$action] = $title->userCan( $action, $user );
533 * Get information about protections and put it in $protections
535 private function getProtectionInfo() {
537 $this->protections
= [];
538 $db = $this->getDB();
540 // Get normal protections for existing titles
541 if ( count( $this->titles
) ) {
542 $this->resetQueryParams();
543 $this->addTables( 'page_restrictions' );
544 $this->addFields( [ 'pr_page', 'pr_type', 'pr_level',
545 'pr_expiry', 'pr_cascade' ] );
546 $this->addWhereFld( 'pr_page', array_keys( $this->titles
) );
548 $res = $this->select( __METHOD__
);
549 foreach ( $res as $row ) {
550 /** @var $title Title */
551 $title = $this->titles
[$row->pr_page
];
553 'type' => $row->pr_type
,
554 'level' => $row->pr_level
,
555 'expiry' => $wgContLang->formatExpiry( $row->pr_expiry
, TS_ISO_8601
)
557 if ( $row->pr_cascade
) {
558 $a['cascade'] = true;
560 $this->protections
[$title->getNamespace()][$title->getDBkey()][] = $a;
562 // Also check old restrictions
563 foreach ( $this->titles
as $pageId => $title ) {
564 if ( $this->pageRestrictions
[$pageId] ) {
565 $namespace = $title->getNamespace();
566 $dbKey = $title->getDBkey();
567 $restrictions = explode( ':', trim( $this->pageRestrictions
[$pageId] ) );
568 foreach ( $restrictions as $restrict ) {
569 $temp = explode( '=', trim( $restrict ) );
570 if ( count( $temp ) == 1 ) {
571 // old old format should be treated as edit/move restriction
572 $restriction = trim( $temp[0] );
574 if ( $restriction == '' ) {
577 $this->protections
[$namespace][$dbKey][] = [
579 'level' => $restriction,
580 'expiry' => 'infinity',
582 $this->protections
[$namespace][$dbKey][] = [
584 'level' => $restriction,
585 'expiry' => 'infinity',
588 $restriction = trim( $temp[1] );
589 if ( $restriction == '' ) {
592 $this->protections
[$namespace][$dbKey][] = [
594 'level' => $restriction,
595 'expiry' => 'infinity',
603 // Get protections for missing titles
604 if ( count( $this->missing
) ) {
605 $this->resetQueryParams();
606 $lb = new LinkBatch( $this->missing
);
607 $this->addTables( 'protected_titles' );
608 $this->addFields( [ 'pt_title', 'pt_namespace', 'pt_create_perm', 'pt_expiry' ] );
609 $this->addWhere( $lb->constructSet( 'pt', $db ) );
610 $res = $this->select( __METHOD__
);
611 foreach ( $res as $row ) {
612 $this->protections
[$row->pt_namespace
][$row->pt_title
][] = [
614 'level' => $row->pt_create_perm
,
615 'expiry' => $wgContLang->formatExpiry( $row->pt_expiry
, TS_ISO_8601
)
620 // Separate good and missing titles into files and other pages
621 // and populate $this->restrictionTypes
622 $images = $others = [];
623 foreach ( $this->everything
as $title ) {
624 if ( $title->getNamespace() == NS_FILE
) {
625 $images[] = $title->getDBkey();
629 // Applicable protection types
630 $this->restrictionTypes
[$title->getNamespace()][$title->getDBkey()] =
631 array_values( $title->getRestrictionTypes() );
634 if ( count( $others ) ) {
635 // Non-images: check templatelinks
636 $lb = new LinkBatch( $others );
637 $this->resetQueryParams();
638 $this->addTables( [ 'page_restrictions', 'page', 'templatelinks' ] );
639 $this->addFields( [ 'pr_type', 'pr_level', 'pr_expiry',
640 'page_title', 'page_namespace',
641 'tl_title', 'tl_namespace' ] );
642 $this->addWhere( $lb->constructSet( 'tl', $db ) );
643 $this->addWhere( 'pr_page = page_id' );
644 $this->addWhere( 'pr_page = tl_from' );
645 $this->addWhereFld( 'pr_cascade', 1 );
647 $res = $this->select( __METHOD__
);
648 foreach ( $res as $row ) {
649 $source = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
650 $this->protections
[$row->tl_namespace
][$row->tl_title
][] = [
651 'type' => $row->pr_type
,
652 'level' => $row->pr_level
,
653 'expiry' => $wgContLang->formatExpiry( $row->pr_expiry
, TS_ISO_8601
),
654 'source' => $source->getPrefixedText()
659 if ( count( $images ) ) {
660 // Images: check imagelinks
661 $this->resetQueryParams();
662 $this->addTables( [ 'page_restrictions', 'page', 'imagelinks' ] );
663 $this->addFields( [ 'pr_type', 'pr_level', 'pr_expiry',
664 'page_title', 'page_namespace', 'il_to' ] );
665 $this->addWhere( 'pr_page = page_id' );
666 $this->addWhere( 'pr_page = il_from' );
667 $this->addWhereFld( 'pr_cascade', 1 );
668 $this->addWhereFld( 'il_to', $images );
670 $res = $this->select( __METHOD__
);
671 foreach ( $res as $row ) {
672 $source = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
673 $this->protections
[NS_FILE
][$row->il_to
][] = [
674 'type' => $row->pr_type
,
675 'level' => $row->pr_level
,
676 'expiry' => $wgContLang->formatExpiry( $row->pr_expiry
, TS_ISO_8601
),
677 'source' => $source->getPrefixedText()
684 * Get talk page IDs (if requested) and subject page IDs (if requested)
685 * and put them in $talkids and $subjectids
687 private function getTSIDs() {
688 $getTitles = $this->talkids
= $this->subjectids
= [];
691 foreach ( $this->everything
as $t ) {
692 if ( MWNamespace
::isTalk( $t->getNamespace() ) ) {
693 if ( $this->fld_subjectid
) {
694 $getTitles[] = $t->getSubjectPage();
696 } elseif ( $this->fld_talkid
) {
697 $getTitles[] = $t->getTalkPage();
700 if ( !count( $getTitles ) ) {
704 $db = $this->getDB();
706 // Construct a custom WHERE clause that matches
707 // all titles in $getTitles
708 $lb = new LinkBatch( $getTitles );
709 $this->resetQueryParams();
710 $this->addTables( 'page' );
711 $this->addFields( [ 'page_title', 'page_namespace', 'page_id' ] );
712 $this->addWhere( $lb->constructSet( 'page', $db ) );
713 $res = $this->select( __METHOD__
);
714 foreach ( $res as $row ) {
715 if ( MWNamespace
::isTalk( $row->page_namespace
) ) {
716 $this->talkids
[MWNamespace
::getSubject( $row->page_namespace
)][$row->page_title
] =
717 intval( $row->page_id
);
719 $this->subjectids
[MWNamespace
::getTalk( $row->page_namespace
)][$row->page_title
] =
720 intval( $row->page_id
);
725 private function getDisplayTitle() {
726 $this->displaytitles
= [];
728 $pageIds = array_keys( $this->titles
);
730 if ( !count( $pageIds ) ) {
734 $this->resetQueryParams();
735 $this->addTables( 'page_props' );
736 $this->addFields( [ 'pp_page', 'pp_value' ] );
737 $this->addWhereFld( 'pp_page', $pageIds );
738 $this->addWhereFld( 'pp_propname', 'displaytitle' );
739 $res = $this->select( __METHOD__
);
741 foreach ( $res as $row ) {
742 $this->displaytitles
[$row->pp_page
] = $row->pp_value
;
747 * Get information about watched status and put it in $this->watched
748 * and $this->notificationtimestamps
750 private function getWatchedInfo() {
751 $user = $this->getUser();
753 if ( $user->isAnon() ||
count( $this->everything
) == 0
754 ||
!$user->isAllowed( 'viewmywatchlist' )
760 $this->notificationtimestamps
= [];
761 $db = $this->getDB();
763 $lb = new LinkBatch( $this->everything
);
765 $this->resetQueryParams();
766 $this->addTables( [ 'watchlist' ] );
767 $this->addFields( [ 'wl_title', 'wl_namespace' ] );
768 $this->addFieldsIf( 'wl_notificationtimestamp', $this->fld_notificationtimestamp
);
770 $lb->constructSet( 'wl', $db ),
771 'wl_user' => $user->getId()
774 $res = $this->select( __METHOD__
);
776 foreach ( $res as $row ) {
777 if ( $this->fld_watched
) {
778 $this->watched
[$row->wl_namespace
][$row->wl_title
] = true;
780 if ( $this->fld_notificationtimestamp
) {
781 $this->notificationtimestamps
[$row->wl_namespace
][$row->wl_title
] =
782 $row->wl_notificationtimestamp
;
788 * Get the count of watchers and put it in $this->watchers
790 private function getWatcherInfo() {
791 if ( count( $this->everything
) == 0 ) {
795 $user = $this->getUser();
796 $canUnwatchedpages = $user->isAllowed( 'unwatchedpages' );
797 $unwatchedPageThreshold = $this->getConfig()->get( 'UnwatchedPageThreshold' );
798 if ( !$canUnwatchedpages && !is_int( $unwatchedPageThreshold ) ) {
802 $this->watchers
= [];
803 $this->showZeroWatchers
= $canUnwatchedpages;
804 $db = $this->getDB();
806 $lb = new LinkBatch( $this->everything
);
808 $this->resetQueryParams();
809 $this->addTables( [ 'watchlist' ] );
810 $this->addFields( [ 'wl_title', 'wl_namespace', 'count' => 'COUNT(*)' ] );
812 $lb->constructSet( 'wl', $db )
814 $this->addOption( 'GROUP BY', [ 'wl_namespace', 'wl_title' ] );
815 if ( !$canUnwatchedpages ) {
816 $this->addOption( 'HAVING', "COUNT(*) >= $unwatchedPageThreshold" );
819 $res = $this->select( __METHOD__
);
821 foreach ( $res as $row ) {
822 $this->watchers
[$row->wl_namespace
][$row->wl_title
] = (int)$row->count
;
827 * Get the count of watchers who have visited recent edits and put it in
828 * $this->visitingwatchers
830 * Based on InfoAction::pageCounts
832 private function getVisitingWatcherInfo() {
833 $config = $this->getConfig();
834 $user = $this->getUser();
835 $db = $this->getDB();
837 $canUnwatchedpages = $user->isAllowed( 'unwatchedpages' );
838 $unwatchedPageThreshold = $this->getConfig()->get( 'UnwatchedPageThreshold' );
839 if ( !$canUnwatchedpages && !is_int( $unwatchedPageThreshold ) ) {
843 $this->showZeroWatchers
= $canUnwatchedpages;
845 // Assemble a WHERE condition to find:
846 // * if the page exists, number of users watching who have
847 // visited the page recently
848 // * if the page doesn't exist, number of users that have
849 // the page on their watchlist
852 // For pages that exist
853 if ( $this->titles
) {
854 $lb = new LinkBatch( $this->titles
);
856 // Fetch last edit timestamps for pages
857 $this->resetQueryParams();
858 $this->addTables( [ 'page', 'revision' ] );
859 $this->addFields( [ 'page_namespace', 'page_title', 'rev_timestamp' ] );
861 'page_latest = rev_id',
862 $lb->constructSet( 'page', $db ),
864 $this->addOption( 'GROUP BY', [ 'page_namespace', 'page_title' ] );
865 $timestampRes = $this->select( __METHOD__
);
867 // Assemble SQL WHERE condition to find number of page watchers who also
868 // visited a "recent" edit (last visited about 26 weeks before latest edit)
869 $age = $config->get( 'WatchersMaxAge' );
871 foreach ( $timestampRes as $row ) {
872 $revTimestamp = wfTimestamp( TS_UNIX
, (int)$row->rev_timestamp
);
873 $threshold = $db->timestamp( $revTimestamp - $age );
874 $timestamps[$row->page_namespace
][$row->page_title
] = $threshold;
877 foreach ( $timestamps as $ns_key => $namespace ) {
879 foreach ( $namespace as $pg_key => $threshold ) {
880 $pageStrings[] = "wl_title = '$pg_key' AND" .
881 ' (wl_notificationtimestamp >= ' .
882 $db->addQuotes( $threshold ) .
883 ' OR wl_notificationtimestamp IS NULL)';
885 $whereStrings[] = "wl_namespace = '$ns_key' AND (" .
886 $db->makeList( $pageStrings, LIST_OR
) . ')';
890 // For nonexistant pages
891 if ( $this->missing
) {
892 $lb = new LinkBatch( $this->missing
);
893 $whereStrings[] = $lb->constructSet( 'wl', $db );
896 // Make the actual string and do the query
897 $whereString = $db->makeList( $whereStrings, LIST_OR
);
899 $this->resetQueryParams();
900 $this->addTables( [ 'watchlist' ] );
904 'count' => 'COUNT(*)'
906 $this->addWhere( [ $whereString ] );
907 $this->addOption( 'GROUP BY', [ 'wl_namespace', 'wl_title' ] );
908 if ( !$canUnwatchedpages ) {
909 $this->addOption( 'HAVING', "COUNT(*) >= $unwatchedPageThreshold" );
912 $res = $this->select( __METHOD__
);
913 foreach ( $res as $row ) {
914 $this->visitingwatchers
[$row->wl_namespace
][$row->wl_title
] = (int)$row->count
;
918 public function getCacheMode( $params ) {
919 // Other props depend on something about the current user
928 if ( array_diff( (array)$params['prop'], $publicProps ) ) {
932 // testactions also depends on the current user
933 if ( $params['testactions'] ) {
937 if ( !is_null( $params['token'] ) ) {
944 public function getAllowedParams() {
947 ApiBase
::PARAM_ISMULTI
=> true,
948 ApiBase
::PARAM_TYPE
=> [
952 'watchers', # private
953 'visitingwatchers', # private
954 'notificationtimestamp', # private
957 'readable', # private
960 // If you add more properties here, please consider whether they
961 // need to be added to getCacheMode()
963 ApiBase
::PARAM_HELP_MSG_PER_VALUE
=> [],
966 ApiBase
::PARAM_TYPE
=> 'string',
967 ApiBase
::PARAM_ISMULTI
=> true,
970 ApiBase
::PARAM_DEPRECATED
=> true,
971 ApiBase
::PARAM_ISMULTI
=> true,
972 ApiBase
::PARAM_TYPE
=> array_keys( $this->getTokenFunctions() )
975 ApiBase
::PARAM_HELP_MSG
=> 'api-help-param-continue',
980 protected function getExamplesMessages() {
982 'action=query&prop=info&titles=Main%20Page'
983 => 'apihelp-query+info-example-simple',
984 'action=query&prop=info&inprop=protection&titles=Main%20Page'
985 => 'apihelp-query+info-example-protection',
989 public function getHelpUrls() {
990 return 'https://www.mediawiki.org/wiki/API:Info';