3 * Copyright © 2006 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
22 use MediaWiki\MediaWikiServices
;
23 use MediaWiki\Linker\LinkTarget
;
26 * A query module to show basic page information.
30 class ApiQueryInfo
extends ApiQueryBase
{
32 private $fld_protection = false, $fld_talkid = false,
33 $fld_subjectid = false, $fld_url = false,
34 $fld_readable = false, $fld_watched = false,
35 $fld_watchers = false, $fld_visitingwatchers = false,
36 $fld_notificationtimestamp = false,
37 $fld_preload = false, $fld_displaytitle = false, $fld_varianttitles = false;
48 private $pageRestrictions, $pageIsRedir, $pageIsNew, $pageTouched,
49 $pageLatest, $pageLength;
51 private $protections, $restrictionTypes, $watched, $watchers, $visitingwatchers,
52 $notificationtimestamps, $talkids, $subjectids, $displaytitles, $variantTitles;
53 private $showZeroWatchers = false;
55 private $tokenFunctions;
57 private $countTestedActions = 0;
59 public function __construct( ApiQuery
$query, $moduleName ) {
60 parent
::__construct( $query, $moduleName, 'in' );
64 * @param ApiPageSet $pageSet
67 public function requestExtraData( $pageSet ) {
68 $pageSet->requestField( 'page_restrictions' );
69 // If the pageset is resolving redirects we won't get page_is_redirect.
70 // But we can't know for sure until the pageset is executed (revids may
71 // turn it off), so request it unconditionally.
72 $pageSet->requestField( 'page_is_redirect' );
73 $pageSet->requestField( 'page_is_new' );
74 $config = $this->getConfig();
75 $pageSet->requestField( 'page_touched' );
76 $pageSet->requestField( 'page_latest' );
77 $pageSet->requestField( 'page_len' );
78 if ( $config->get( 'ContentHandlerUseDB' ) ) {
79 $pageSet->requestField( 'page_content_model' );
81 if ( $config->get( 'PageLanguageUseDB' ) ) {
82 $pageSet->requestField( 'page_lang' );
87 * Get an array mapping token names to their handler functions.
88 * The prototype for a token function is func($pageid, $title)
89 * it should return a token or false (permission denied)
90 * @deprecated since 1.24
91 * @return array [ tokenname => function ]
93 protected function getTokenFunctions() {
94 // Don't call the hooks twice
95 if ( isset( $this->tokenFunctions
) ) {
96 return $this->tokenFunctions
;
99 // If we're in a mode that breaks the same-origin policy, no tokens can
101 if ( $this->lacksSameOriginSecurity() ) {
105 $this->tokenFunctions
= [
106 'edit' => [ self
::class, 'getEditToken' ],
107 'delete' => [ self
::class, 'getDeleteToken' ],
108 'protect' => [ self
::class, 'getProtectToken' ],
109 'move' => [ self
::class, 'getMoveToken' ],
110 'block' => [ self
::class, 'getBlockToken' ],
111 'unblock' => [ self
::class, 'getUnblockToken' ],
112 'email' => [ self
::class, 'getEmailToken' ],
113 'import' => [ self
::class, 'getImportToken' ],
114 'watch' => [ self
::class, 'getWatchToken' ],
116 Hooks
::run( 'APIQueryInfoTokens', [ &$this->tokenFunctions
] );
118 return $this->tokenFunctions
;
121 protected static $cachedTokens = [];
124 * @deprecated since 1.24
126 public static function resetTokenCache() {
127 self
::$cachedTokens = [];
131 * @deprecated since 1.24
133 public static function getEditToken( $pageid, $title ) {
134 // We could check for $title->userCan('edit') here,
135 // but that's too expensive for this purpose
136 // and would break caching
138 if ( !MediaWikiServices
::getInstance()->getPermissionManager()
139 ->userHasRight( $wgUser, 'edit' ) ) {
143 // The token is always the same, let's exploit that
144 if ( !isset( self
::$cachedTokens['edit'] ) ) {
145 self
::$cachedTokens['edit'] = $wgUser->getEditToken();
148 return self
::$cachedTokens['edit'];
152 * @deprecated since 1.24
154 public static function getDeleteToken( $pageid, $title ) {
156 if ( !MediaWikiServices
::getInstance()->getPermissionManager()
157 ->userHasRight( $wgUser, 'delete' ) ) {
161 // The token is always the same, let's exploit that
162 if ( !isset( self
::$cachedTokens['delete'] ) ) {
163 self
::$cachedTokens['delete'] = $wgUser->getEditToken();
166 return self
::$cachedTokens['delete'];
170 * @deprecated since 1.24
172 public static function getProtectToken( $pageid, $title ) {
174 if ( !MediaWikiServices
::getInstance()->getPermissionManager()
175 ->userHasRight( $wgUser, 'protect' ) ) {
179 // The token is always the same, let's exploit that
180 if ( !isset( self
::$cachedTokens['protect'] ) ) {
181 self
::$cachedTokens['protect'] = $wgUser->getEditToken();
184 return self
::$cachedTokens['protect'];
188 * @deprecated since 1.24
190 public static function getMoveToken( $pageid, $title ) {
192 if ( !MediaWikiServices
::getInstance()->getPermissionManager()
193 ->userHasRight( $wgUser, 'move' ) ) {
197 // The token is always the same, let's exploit that
198 if ( !isset( self
::$cachedTokens['move'] ) ) {
199 self
::$cachedTokens['move'] = $wgUser->getEditToken();
202 return self
::$cachedTokens['move'];
206 * @deprecated since 1.24
208 public static function getBlockToken( $pageid, $title ) {
210 if ( !MediaWikiServices
::getInstance()->getPermissionManager()
211 ->userHasRight( $wgUser, 'block' ) ) {
215 // The token is always the same, let's exploit that
216 if ( !isset( self
::$cachedTokens['block'] ) ) {
217 self
::$cachedTokens['block'] = $wgUser->getEditToken();
220 return self
::$cachedTokens['block'];
224 * @deprecated since 1.24
226 public static function getUnblockToken( $pageid, $title ) {
227 // Currently, this is exactly the same as the block token
228 return self
::getBlockToken( $pageid, $title );
232 * @deprecated since 1.24
234 public static function getEmailToken( $pageid, $title ) {
236 if ( !$wgUser->canSendEmail() ||
$wgUser->isBlockedFromEmailuser() ) {
240 // The token is always the same, let's exploit that
241 if ( !isset( self
::$cachedTokens['email'] ) ) {
242 self
::$cachedTokens['email'] = $wgUser->getEditToken();
245 return self
::$cachedTokens['email'];
249 * @deprecated since 1.24
251 public static function getImportToken( $pageid, $title ) {
253 if ( !MediaWikiServices
::getInstance()
254 ->getPermissionManager()
255 ->userHasAnyRight( $wgUser, 'import', 'importupload' ) ) {
259 // The token is always the same, let's exploit that
260 if ( !isset( self
::$cachedTokens['import'] ) ) {
261 self
::$cachedTokens['import'] = $wgUser->getEditToken();
264 return self
::$cachedTokens['import'];
268 * @deprecated since 1.24
270 public static function getWatchToken( $pageid, $title ) {
272 if ( !$wgUser->isLoggedIn() ) {
276 // The token is always the same, let's exploit that
277 if ( !isset( self
::$cachedTokens['watch'] ) ) {
278 self
::$cachedTokens['watch'] = $wgUser->getEditToken( 'watch' );
281 return self
::$cachedTokens['watch'];
285 * @deprecated since 1.24
287 public static function getOptionsToken( $pageid, $title ) {
289 if ( !$wgUser->isLoggedIn() ) {
293 // The token is always the same, let's exploit that
294 if ( !isset( self
::$cachedTokens['options'] ) ) {
295 self
::$cachedTokens['options'] = $wgUser->getEditToken();
298 return self
::$cachedTokens['options'];
301 public function execute() {
302 $this->params
= $this->extractRequestParams();
303 if ( !is_null( $this->params
['prop'] ) ) {
304 $prop = array_flip( $this->params
['prop'] );
305 $this->fld_protection
= isset( $prop['protection'] );
306 $this->fld_watched
= isset( $prop['watched'] );
307 $this->fld_watchers
= isset( $prop['watchers'] );
308 $this->fld_visitingwatchers
= isset( $prop['visitingwatchers'] );
309 $this->fld_notificationtimestamp
= isset( $prop['notificationtimestamp'] );
310 $this->fld_talkid
= isset( $prop['talkid'] );
311 $this->fld_subjectid
= isset( $prop['subjectid'] );
312 $this->fld_url
= isset( $prop['url'] );
313 $this->fld_readable
= isset( $prop['readable'] );
314 $this->fld_preload
= isset( $prop['preload'] );
315 $this->fld_displaytitle
= isset( $prop['displaytitle'] );
316 $this->fld_varianttitles
= isset( $prop['varianttitles'] );
319 $pageSet = $this->getPageSet();
320 $this->titles
= $pageSet->getGoodTitles();
321 $this->missing
= $pageSet->getMissingTitles();
322 $this->everything
= $this->titles +
$this->missing
;
323 $result = $this->getResult();
325 uasort( $this->everything
, [ Title
::class, 'compare' ] );
326 if ( !is_null( $this->params
['continue'] ) ) {
327 // Throw away any titles we're gonna skip so they don't
329 $cont = explode( '|', $this->params
['continue'] );
330 $this->dieContinueUsageIf( count( $cont ) != 2 );
331 $conttitle = Title
::makeTitleSafe( $cont[0], $cont[1] );
332 foreach ( $this->everything
as $pageid => $title ) {
333 if ( Title
::compare( $title, $conttitle ) >= 0 ) {
336 unset( $this->titles
[$pageid] );
337 unset( $this->missing
[$pageid] );
338 unset( $this->everything
[$pageid] );
342 $this->pageRestrictions
= $pageSet->getCustomField( 'page_restrictions' );
343 // when resolving redirects, no page will have this field
344 $this->pageIsRedir
= !$pageSet->isResolvingRedirects()
345 ?
$pageSet->getCustomField( 'page_is_redirect' )
347 $this->pageIsNew
= $pageSet->getCustomField( 'page_is_new' );
349 $this->pageTouched
= $pageSet->getCustomField( 'page_touched' );
350 $this->pageLatest
= $pageSet->getCustomField( 'page_latest' );
351 $this->pageLength
= $pageSet->getCustomField( 'page_len' );
353 // Get protection info if requested
354 if ( $this->fld_protection
) {
355 $this->getProtectionInfo();
358 if ( $this->fld_watched ||
$this->fld_notificationtimestamp
) {
359 $this->getWatchedInfo();
362 if ( $this->fld_watchers
) {
363 $this->getWatcherInfo();
366 if ( $this->fld_visitingwatchers
) {
367 $this->getVisitingWatcherInfo();
370 // Run the talkid/subjectid query if requested
371 if ( $this->fld_talkid ||
$this->fld_subjectid
) {
375 if ( $this->fld_displaytitle
) {
376 $this->getDisplayTitle();
379 if ( $this->fld_varianttitles
) {
380 $this->getVariantTitles();
383 /** @var Title $title */
384 foreach ( $this->everything
as $pageid => $title ) {
385 $pageInfo = $this->extractPageInfo( $pageid, $title );
386 $fit = $pageInfo !== null && $result->addValue( [
389 ], $pageid, $pageInfo );
391 $this->setContinueEnumParameter( 'continue',
392 $title->getNamespace() . '|' .
400 * Get a result array with information about a title
401 * @param int $pageid Page ID (negative for missing titles)
402 * @param Title $title
405 private function extractPageInfo( $pageid, $title ) {
407 // $title->exists() needs pageid, which is not set for all title objects
408 $titleExists = $pageid > 0;
409 $ns = $title->getNamespace();
410 $dbkey = $title->getDBkey();
412 $pageInfo['contentmodel'] = $title->getContentModel();
414 $pageLanguage = $title->getPageLanguage();
415 $pageInfo['pagelanguage'] = $pageLanguage->getCode();
416 $pageInfo['pagelanguagehtmlcode'] = $pageLanguage->getHtmlCode();
417 $pageInfo['pagelanguagedir'] = $pageLanguage->getDir();
419 $user = $this->getUser();
421 if ( $titleExists ) {
422 $pageInfo['touched'] = wfTimestamp( TS_ISO_8601
, $this->pageTouched
[$pageid] );
423 $pageInfo['lastrevid'] = (int)$this->pageLatest
[$pageid];
424 $pageInfo['length'] = (int)$this->pageLength
[$pageid];
426 if ( isset( $this->pageIsRedir
[$pageid] ) && $this->pageIsRedir
[$pageid] ) {
427 $pageInfo['redirect'] = true;
429 if ( $this->pageIsNew
[$pageid] ) {
430 $pageInfo['new'] = true;
434 if ( !is_null( $this->params
['token'] ) ) {
435 $tokenFunctions = $this->getTokenFunctions();
436 $pageInfo['starttimestamp'] = wfTimestamp( TS_ISO_8601
, time() );
437 foreach ( $this->params
['token'] as $t ) {
438 $val = call_user_func( $tokenFunctions[$t], $pageid, $title );
439 if ( $val === false ) {
440 $this->addWarning( [ 'apiwarn-tokennotallowed', $t ] );
442 $pageInfo[$t . 'token'] = $val;
447 if ( $this->fld_protection
) {
448 $pageInfo['protection'] = [];
449 if ( isset( $this->protections
[$ns][$dbkey] ) ) {
450 $pageInfo['protection'] =
451 $this->protections
[$ns][$dbkey];
453 ApiResult
::setIndexedTagName( $pageInfo['protection'], 'pr' );
455 $pageInfo['restrictiontypes'] = [];
456 if ( isset( $this->restrictionTypes
[$ns][$dbkey] ) ) {
457 $pageInfo['restrictiontypes'] =
458 $this->restrictionTypes
[$ns][$dbkey];
460 ApiResult
::setIndexedTagName( $pageInfo['restrictiontypes'], 'rt' );
463 if ( $this->fld_watched
&& $this->watched
!== null ) {
464 $pageInfo['watched'] = $this->watched
[$ns][$dbkey];
467 if ( $this->fld_watchers
) {
468 if ( $this->watchers
!== null && $this->watchers
[$ns][$dbkey] !== 0 ) {
469 $pageInfo['watchers'] = $this->watchers
[$ns][$dbkey];
470 } elseif ( $this->showZeroWatchers
) {
471 $pageInfo['watchers'] = 0;
475 if ( $this->fld_visitingwatchers
) {
476 if ( $this->visitingwatchers
!== null && $this->visitingwatchers
[$ns][$dbkey] !== 0 ) {
477 $pageInfo['visitingwatchers'] = $this->visitingwatchers
[$ns][$dbkey];
478 } elseif ( $this->showZeroWatchers
) {
479 $pageInfo['visitingwatchers'] = 0;
483 if ( $this->fld_notificationtimestamp
) {
484 $pageInfo['notificationtimestamp'] = '';
485 if ( $this->notificationtimestamps
[$ns][$dbkey] ) {
486 $pageInfo['notificationtimestamp'] =
487 wfTimestamp( TS_ISO_8601
, $this->notificationtimestamps
[$ns][$dbkey] );
491 if ( $this->fld_talkid
&& isset( $this->talkids
[$ns][$dbkey] ) ) {
492 $pageInfo['talkid'] = $this->talkids
[$ns][$dbkey];
495 if ( $this->fld_subjectid
&& isset( $this->subjectids
[$ns][$dbkey] ) ) {
496 $pageInfo['subjectid'] = $this->subjectids
[$ns][$dbkey];
499 if ( $this->fld_url
) {
500 $pageInfo['fullurl'] = wfExpandUrl( $title->getFullURL(), PROTO_CURRENT
);
501 $pageInfo['editurl'] = wfExpandUrl( $title->getFullURL( 'action=edit' ), PROTO_CURRENT
);
502 $pageInfo['canonicalurl'] = wfExpandUrl( $title->getFullURL(), PROTO_CANONICAL
);
504 if ( $this->fld_readable
) {
505 $pageInfo['readable'] = $this->getPermissionManager()->userCan(
506 'read', $user, $title
510 if ( $this->fld_preload
) {
511 if ( $titleExists ) {
512 $pageInfo['preload'] = '';
515 Hooks
::run( 'EditFormPreloadText', [ &$text, &$title ] );
517 $pageInfo['preload'] = $text;
521 if ( $this->fld_displaytitle
) {
522 if ( isset( $this->displaytitles
[$pageid] ) ) {
523 $pageInfo['displaytitle'] = $this->displaytitles
[$pageid];
525 $pageInfo['displaytitle'] = $title->getPrefixedText();
529 if ( $this->fld_varianttitles
&& isset( $this->variantTitles
[$pageid] ) ) {
530 $pageInfo['varianttitles'] = $this->variantTitles
[$pageid];
533 if ( $this->params
['testactions'] ) {
534 $limit = $this->getMain()->canApiHighLimits() ? self
::LIMIT_SML2
: self
::LIMIT_SML1
;
535 if ( $this->countTestedActions
>= $limit ) {
536 return null; // force a continuation
539 $detailLevel = $this->params
['testactionsdetail'];
540 $rigor = $detailLevel === 'quick' ?
'quick' : 'secure';
541 $errorFormatter = $this->getErrorFormatter();
542 if ( $errorFormatter->getFormat() === 'bc' ) {
543 // Eew, no. Use a more modern format here.
544 $errorFormatter = $errorFormatter->newWithFormat( 'plaintext' );
547 $user = $this->getUser();
548 $pageInfo['actions'] = [];
549 foreach ( $this->params
['testactions'] as $action ) {
550 $this->countTestedActions++
;
552 if ( $detailLevel === 'boolean' ) {
553 $pageInfo['actions'][$action] = $this->getPermissionManager()->userCan(
554 $action, $user, $title
557 $pageInfo['actions'][$action] = $errorFormatter->arrayFromStatus( $this->errorArrayToStatus(
558 $this->getPermissionManager()->getPermissionErrors(
559 $action, $user, $title, $rigor
571 * Get information about protections and put it in $protections
573 private function getProtectionInfo() {
574 $this->protections
= [];
575 $db = $this->getDB();
577 // Get normal protections for existing titles
578 if ( count( $this->titles
) ) {
579 $this->resetQueryParams();
580 $this->addTables( 'page_restrictions' );
581 $this->addFields( [ 'pr_page', 'pr_type', 'pr_level',
582 'pr_expiry', 'pr_cascade' ] );
583 $this->addWhereFld( 'pr_page', array_keys( $this->titles
) );
585 $res = $this->select( __METHOD__
);
586 foreach ( $res as $row ) {
587 /** @var Title $title */
588 $title = $this->titles
[$row->pr_page
];
590 'type' => $row->pr_type
,
591 'level' => $row->pr_level
,
592 'expiry' => ApiResult
::formatExpiry( $row->pr_expiry
)
594 if ( $row->pr_cascade
) {
595 $a['cascade'] = true;
597 $this->protections
[$title->getNamespace()][$title->getDBkey()][] = $a;
599 // Also check old restrictions
600 foreach ( $this->titles
as $pageId => $title ) {
601 if ( $this->pageRestrictions
[$pageId] ) {
602 $namespace = $title->getNamespace();
603 $dbKey = $title->getDBkey();
604 $restrictions = explode( ':', trim( $this->pageRestrictions
[$pageId] ) );
605 foreach ( $restrictions as $restrict ) {
606 $temp = explode( '=', trim( $restrict ) );
607 if ( count( $temp ) == 1 ) {
608 // old old format should be treated as edit/move restriction
609 $restriction = trim( $temp[0] );
611 if ( $restriction == '' ) {
614 $this->protections
[$namespace][$dbKey][] = [
616 'level' => $restriction,
617 'expiry' => 'infinity',
619 $this->protections
[$namespace][$dbKey][] = [
621 'level' => $restriction,
622 'expiry' => 'infinity',
625 $restriction = trim( $temp[1] );
626 if ( $restriction == '' ) {
629 $this->protections
[$namespace][$dbKey][] = [
631 'level' => $restriction,
632 'expiry' => 'infinity',
640 // Get protections for missing titles
641 if ( count( $this->missing
) ) {
642 $this->resetQueryParams();
643 $lb = new LinkBatch( $this->missing
);
644 $this->addTables( 'protected_titles' );
645 $this->addFields( [ 'pt_title', 'pt_namespace', 'pt_create_perm', 'pt_expiry' ] );
646 $this->addWhere( $lb->constructSet( 'pt', $db ) );
647 $res = $this->select( __METHOD__
);
648 foreach ( $res as $row ) {
649 $this->protections
[$row->pt_namespace
][$row->pt_title
][] = [
651 'level' => $row->pt_create_perm
,
652 'expiry' => ApiResult
::formatExpiry( $row->pt_expiry
)
657 // Separate good and missing titles into files and other pages
658 // and populate $this->restrictionTypes
659 $images = $others = [];
660 foreach ( $this->everything
as $title ) {
661 if ( $title->getNamespace() == NS_FILE
) {
662 $images[] = $title->getDBkey();
666 // Applicable protection types
667 $this->restrictionTypes
[$title->getNamespace()][$title->getDBkey()] =
668 array_values( $title->getRestrictionTypes() );
671 if ( count( $others ) ) {
672 // Non-images: check templatelinks
673 $lb = new LinkBatch( $others );
674 $this->resetQueryParams();
675 $this->addTables( [ 'page_restrictions', 'page', 'templatelinks' ] );
676 $this->addFields( [ 'pr_type', 'pr_level', 'pr_expiry',
677 'page_title', 'page_namespace',
678 'tl_title', 'tl_namespace' ] );
679 $this->addWhere( $lb->constructSet( 'tl', $db ) );
680 $this->addWhere( 'pr_page = page_id' );
681 $this->addWhere( 'pr_page = tl_from' );
682 $this->addWhereFld( 'pr_cascade', 1 );
684 $res = $this->select( __METHOD__
);
685 foreach ( $res as $row ) {
686 $source = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
687 $this->protections
[$row->tl_namespace
][$row->tl_title
][] = [
688 'type' => $row->pr_type
,
689 'level' => $row->pr_level
,
690 'expiry' => ApiResult
::formatExpiry( $row->pr_expiry
),
691 'source' => $source->getPrefixedText()
696 if ( count( $images ) ) {
697 // Images: check imagelinks
698 $this->resetQueryParams();
699 $this->addTables( [ 'page_restrictions', 'page', 'imagelinks' ] );
700 $this->addFields( [ 'pr_type', 'pr_level', 'pr_expiry',
701 'page_title', 'page_namespace', 'il_to' ] );
702 $this->addWhere( 'pr_page = page_id' );
703 $this->addWhere( 'pr_page = il_from' );
704 $this->addWhereFld( 'pr_cascade', 1 );
705 $this->addWhereFld( 'il_to', $images );
707 $res = $this->select( __METHOD__
);
708 foreach ( $res as $row ) {
709 $source = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
710 $this->protections
[NS_FILE
][$row->il_to
][] = [
711 'type' => $row->pr_type
,
712 'level' => $row->pr_level
,
713 'expiry' => ApiResult
::formatExpiry( $row->pr_expiry
),
714 'source' => $source->getPrefixedText()
721 * Get talk page IDs (if requested) and subject page IDs (if requested)
722 * and put them in $talkids and $subjectids
724 private function getTSIDs() {
725 $getTitles = $this->talkids
= $this->subjectids
= [];
726 $nsInfo = MediaWikiServices
::getInstance()->getNamespaceInfo();
729 foreach ( $this->everything
as $t ) {
730 if ( $nsInfo->isTalk( $t->getNamespace() ) ) {
731 if ( $this->fld_subjectid
) {
732 $getTitles[] = $t->getSubjectPage();
734 } elseif ( $this->fld_talkid
) {
735 $getTitles[] = $t->getTalkPage();
738 if ( $getTitles === [] ) {
742 $db = $this->getDB();
744 // Construct a custom WHERE clause that matches
745 // all titles in $getTitles
746 $lb = new LinkBatch( $getTitles );
747 $this->resetQueryParams();
748 $this->addTables( 'page' );
749 $this->addFields( [ 'page_title', 'page_namespace', 'page_id' ] );
750 $this->addWhere( $lb->constructSet( 'page', $db ) );
751 $res = $this->select( __METHOD__
);
752 foreach ( $res as $row ) {
753 if ( $nsInfo->isTalk( $row->page_namespace
) ) {
754 $this->talkids
[$nsInfo->getSubject( $row->page_namespace
)][$row->page_title
] =
755 (int)( $row->page_id
);
757 $this->subjectids
[$nsInfo->getTalk( $row->page_namespace
)][$row->page_title
] =
758 (int)( $row->page_id
);
763 private function getDisplayTitle() {
764 $this->displaytitles
= [];
766 $pageIds = array_keys( $this->titles
);
768 if ( $pageIds === [] ) {
772 $this->resetQueryParams();
773 $this->addTables( 'page_props' );
774 $this->addFields( [ 'pp_page', 'pp_value' ] );
775 $this->addWhereFld( 'pp_page', $pageIds );
776 $this->addWhereFld( 'pp_propname', 'displaytitle' );
777 $res = $this->select( __METHOD__
);
779 foreach ( $res as $row ) {
780 $this->displaytitles
[$row->pp_page
] = $row->pp_value
;
784 private function getVariantTitles() {
785 if ( $this->titles
=== [] ) {
788 $this->variantTitles
= [];
789 foreach ( $this->titles
as $pageId => $t ) {
790 $this->variantTitles
[$pageId] = isset( $this->displaytitles
[$pageId] )
791 ?
$this->getAllVariants( $this->displaytitles
[$pageId] )
792 : $this->getAllVariants( $t->getText(), $t->getNamespace() );
796 private function getAllVariants( $text, $ns = NS_MAIN
) {
798 $contLang = MediaWikiServices
::getInstance()->getContentLanguage();
799 foreach ( $contLang->getVariants() as $variant ) {
800 $convertTitle = $contLang->autoConvert( $text, $variant );
801 if ( $ns !== NS_MAIN
) {
802 $convertNs = $contLang->convertNamespace( $ns, $variant );
803 $convertTitle = $convertNs . ':' . $convertTitle;
805 $result[$variant] = $convertTitle;
811 * Get information about watched status and put it in $this->watched
812 * and $this->notificationtimestamps
814 private function getWatchedInfo() {
815 $user = $this->getUser();
817 if ( $user->isAnon() ||
count( $this->everything
) == 0
818 ||
!$this->getPermissionManager()->userHasRight( $user, 'viewmywatchlist' )
824 $this->notificationtimestamps
= [];
826 $store = MediaWikiServices
::getInstance()->getWatchedItemStore();
827 $timestamps = $store->getNotificationTimestampsBatch( $user, $this->everything
);
829 if ( $this->fld_watched
) {
830 foreach ( $timestamps as $namespaceId => $dbKeys ) {
831 $this->watched
[$namespaceId] = array_map(
839 if ( $this->fld_notificationtimestamp
) {
840 $this->notificationtimestamps
= $timestamps;
845 * Get the count of watchers and put it in $this->watchers
847 private function getWatcherInfo() {
848 if ( count( $this->everything
) == 0 ) {
852 $user = $this->getUser();
853 $canUnwatchedpages = $this->getPermissionManager()->userHasRight( $user, 'unwatchedpages' );
854 $unwatchedPageThreshold = $this->getConfig()->get( 'UnwatchedPageThreshold' );
855 if ( !$canUnwatchedpages && !is_int( $unwatchedPageThreshold ) ) {
859 $this->showZeroWatchers
= $canUnwatchedpages;
862 if ( !$canUnwatchedpages ) {
863 $countOptions['minimumWatchers'] = $unwatchedPageThreshold;
866 $this->watchers
= MediaWikiServices
::getInstance()->getWatchedItemStore()->countWatchersMultiple(
873 * Get the count of watchers who have visited recent edits and put it in
874 * $this->visitingwatchers
876 * Based on InfoAction::pageCounts
878 private function getVisitingWatcherInfo() {
879 $config = $this->getConfig();
880 $user = $this->getUser();
881 $db = $this->getDB();
883 $canUnwatchedpages = $this->getPermissionManager()->userHasRight( $user, 'unwatchedpages' );
884 $unwatchedPageThreshold = $this->getConfig()->get( 'UnwatchedPageThreshold' );
885 if ( !$canUnwatchedpages && !is_int( $unwatchedPageThreshold ) ) {
889 $this->showZeroWatchers
= $canUnwatchedpages;
891 $titlesWithThresholds = [];
892 if ( $this->titles
) {
893 $lb = new LinkBatch( $this->titles
);
895 // Fetch last edit timestamps for pages
896 $this->resetQueryParams();
897 $this->addTables( [ 'page', 'revision' ] );
898 $this->addFields( [ 'page_namespace', 'page_title', 'rev_timestamp' ] );
900 'page_latest = rev_id',
901 $lb->constructSet( 'page', $db ),
903 $this->addOption( 'GROUP BY', [ 'page_namespace', 'page_title' ] );
904 $timestampRes = $this->select( __METHOD__
);
906 $age = $config->get( 'WatchersMaxAge' );
908 foreach ( $timestampRes as $row ) {
909 $revTimestamp = wfTimestamp( TS_UNIX
, (int)$row->rev_timestamp
);
910 $timestamps[$row->page_namespace
][$row->page_title
] = $revTimestamp - $age;
912 $titlesWithThresholds = array_map(
913 function ( LinkTarget
$target ) use ( $timestamps ) {
915 $target, $timestamps[$target->getNamespace()][$target->getDBkey()]
922 if ( $this->missing
) {
923 $titlesWithThresholds = array_merge(
924 $titlesWithThresholds,
926 function ( LinkTarget
$target ) {
927 return [ $target, null ];
933 $store = MediaWikiServices
::getInstance()->getWatchedItemStore();
934 $this->visitingwatchers
= $store->countVisitingWatchersMultiple(
935 $titlesWithThresholds,
936 !$canUnwatchedpages ?
$unwatchedPageThreshold : null
940 public function getCacheMode( $params ) {
941 // Other props depend on something about the current user
951 if ( array_diff( (array)$params['prop'], $publicProps ) ) {
955 // testactions also depends on the current user
956 if ( $params['testactions'] ) {
960 if ( !is_null( $params['token'] ) ) {
967 public function getAllowedParams() {
970 ApiBase
::PARAM_ISMULTI
=> true,
971 ApiBase
::PARAM_TYPE
=> [
975 'watchers', # private
976 'visitingwatchers', # private
977 'notificationtimestamp', # private
980 'readable', # private
984 // If you add more properties here, please consider whether they
985 // need to be added to getCacheMode()
987 ApiBase
::PARAM_HELP_MSG_PER_VALUE
=> [],
988 ApiBase
::PARAM_DEPRECATED_VALUES
=> [
989 'readable' => true, // Since 1.32
993 ApiBase
::PARAM_TYPE
=> 'string',
994 ApiBase
::PARAM_ISMULTI
=> true,
996 'testactionsdetail' => [
997 ApiBase
::PARAM_TYPE
=> [ 'boolean', 'full', 'quick' ],
998 ApiBase
::PARAM_DFLT
=> 'boolean',
999 ApiBase
::PARAM_HELP_MSG_PER_VALUE
=> [],
1002 ApiBase
::PARAM_DEPRECATED
=> true,
1003 ApiBase
::PARAM_ISMULTI
=> true,
1004 ApiBase
::PARAM_TYPE
=> array_keys( $this->getTokenFunctions() )
1007 ApiBase
::PARAM_HELP_MSG
=> 'api-help-param-continue',
1012 protected function getExamplesMessages() {
1014 'action=query&prop=info&titles=Main%20Page'
1015 => 'apihelp-query+info-example-simple',
1016 'action=query&prop=info&inprop=protection&titles=Main%20Page'
1017 => 'apihelp-query+info-example-protection',
1021 public function getHelpUrls() {
1022 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Info';