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
;
122 protected static $cachedTokens = [];
125 * @deprecated since 1.24
127 public static function resetTokenCache() {
128 self
::$cachedTokens = [];
132 * @deprecated since 1.24
134 public static function getEditToken( $pageid, $title ) {
135 // We could check for $title->userCan('edit') here,
136 // but that's too expensive for this purpose
137 // and would break caching
139 if ( !MediaWikiServices
::getInstance()->getPermissionManager()
140 ->userHasRight( $wgUser, 'edit' ) ) {
144 // The token is always the same, let's exploit that
145 if ( !isset( self
::$cachedTokens['edit'] ) ) {
146 self
::$cachedTokens['edit'] = $wgUser->getEditToken();
149 return self
::$cachedTokens['edit'];
153 * @deprecated since 1.24
155 public static function getDeleteToken( $pageid, $title ) {
157 if ( !MediaWikiServices
::getInstance()->getPermissionManager()
158 ->userHasRight( $wgUser, 'delete' ) ) {
162 // The token is always the same, let's exploit that
163 if ( !isset( self
::$cachedTokens['delete'] ) ) {
164 self
::$cachedTokens['delete'] = $wgUser->getEditToken();
167 return self
::$cachedTokens['delete'];
171 * @deprecated since 1.24
173 public static function getProtectToken( $pageid, $title ) {
175 if ( !MediaWikiServices
::getInstance()->getPermissionManager()
176 ->userHasRight( $wgUser, 'protect' ) ) {
180 // The token is always the same, let's exploit that
181 if ( !isset( self
::$cachedTokens['protect'] ) ) {
182 self
::$cachedTokens['protect'] = $wgUser->getEditToken();
185 return self
::$cachedTokens['protect'];
189 * @deprecated since 1.24
191 public static function getMoveToken( $pageid, $title ) {
193 if ( !MediaWikiServices
::getInstance()->getPermissionManager()
194 ->userHasRight( $wgUser, 'move' ) ) {
198 // The token is always the same, let's exploit that
199 if ( !isset( self
::$cachedTokens['move'] ) ) {
200 self
::$cachedTokens['move'] = $wgUser->getEditToken();
203 return self
::$cachedTokens['move'];
207 * @deprecated since 1.24
209 public static function getBlockToken( $pageid, $title ) {
211 if ( !MediaWikiServices
::getInstance()->getPermissionManager()
212 ->userHasRight( $wgUser, 'block' ) ) {
216 // The token is always the same, let's exploit that
217 if ( !isset( self
::$cachedTokens['block'] ) ) {
218 self
::$cachedTokens['block'] = $wgUser->getEditToken();
221 return self
::$cachedTokens['block'];
225 * @deprecated since 1.24
227 public static function getUnblockToken( $pageid, $title ) {
228 // Currently, this is exactly the same as the block token
229 return self
::getBlockToken( $pageid, $title );
233 * @deprecated since 1.24
235 public static function getEmailToken( $pageid, $title ) {
237 if ( !$wgUser->canSendEmail() ||
$wgUser->isBlockedFromEmailuser() ) {
241 // The token is always the same, let's exploit that
242 if ( !isset( self
::$cachedTokens['email'] ) ) {
243 self
::$cachedTokens['email'] = $wgUser->getEditToken();
246 return self
::$cachedTokens['email'];
250 * @deprecated since 1.24
252 public static function getImportToken( $pageid, $title ) {
254 if ( !MediaWikiServices
::getInstance()
255 ->getPermissionManager()
256 ->userHasAnyRight( $wgUser, 'import', 'importupload' ) ) {
260 // The token is always the same, let's exploit that
261 if ( !isset( self
::$cachedTokens['import'] ) ) {
262 self
::$cachedTokens['import'] = $wgUser->getEditToken();
265 return self
::$cachedTokens['import'];
269 * @deprecated since 1.24
271 public static function getWatchToken( $pageid, $title ) {
273 if ( !$wgUser->isLoggedIn() ) {
277 // The token is always the same, let's exploit that
278 if ( !isset( self
::$cachedTokens['watch'] ) ) {
279 self
::$cachedTokens['watch'] = $wgUser->getEditToken( 'watch' );
282 return self
::$cachedTokens['watch'];
286 * @deprecated since 1.24
288 public static function getOptionsToken( $pageid, $title ) {
290 if ( !$wgUser->isLoggedIn() ) {
294 // The token is always the same, let's exploit that
295 if ( !isset( self
::$cachedTokens['options'] ) ) {
296 self
::$cachedTokens['options'] = $wgUser->getEditToken();
299 return self
::$cachedTokens['options'];
302 public function execute() {
303 $this->params
= $this->extractRequestParams();
304 if ( !is_null( $this->params
['prop'] ) ) {
305 $prop = array_flip( $this->params
['prop'] );
306 $this->fld_protection
= isset( $prop['protection'] );
307 $this->fld_watched
= isset( $prop['watched'] );
308 $this->fld_watchers
= isset( $prop['watchers'] );
309 $this->fld_visitingwatchers
= isset( $prop['visitingwatchers'] );
310 $this->fld_notificationtimestamp
= isset( $prop['notificationtimestamp'] );
311 $this->fld_talkid
= isset( $prop['talkid'] );
312 $this->fld_subjectid
= isset( $prop['subjectid'] );
313 $this->fld_url
= isset( $prop['url'] );
314 $this->fld_readable
= isset( $prop['readable'] );
315 $this->fld_preload
= isset( $prop['preload'] );
316 $this->fld_displaytitle
= isset( $prop['displaytitle'] );
317 $this->fld_varianttitles
= isset( $prop['varianttitles'] );
320 $pageSet = $this->getPageSet();
321 $this->titles
= $pageSet->getGoodTitles();
322 $this->missing
= $pageSet->getMissingTitles();
323 $this->everything
= $this->titles +
$this->missing
;
324 $result = $this->getResult();
326 uasort( $this->everything
, [ Title
::class, 'compare' ] );
327 if ( !is_null( $this->params
['continue'] ) ) {
328 // Throw away any titles we're gonna skip so they don't
330 $cont = explode( '|', $this->params
['continue'] );
331 $this->dieContinueUsageIf( count( $cont ) != 2 );
332 $conttitle = Title
::makeTitleSafe( $cont[0], $cont[1] );
333 foreach ( $this->everything
as $pageid => $title ) {
334 if ( Title
::compare( $title, $conttitle ) >= 0 ) {
337 unset( $this->titles
[$pageid] );
338 unset( $this->missing
[$pageid] );
339 unset( $this->everything
[$pageid] );
343 $this->pageRestrictions
= $pageSet->getCustomField( 'page_restrictions' );
344 // when resolving redirects, no page will have this field
345 $this->pageIsRedir
= !$pageSet->isResolvingRedirects()
346 ?
$pageSet->getCustomField( 'page_is_redirect' )
348 $this->pageIsNew
= $pageSet->getCustomField( 'page_is_new' );
350 $this->pageTouched
= $pageSet->getCustomField( 'page_touched' );
351 $this->pageLatest
= $pageSet->getCustomField( 'page_latest' );
352 $this->pageLength
= $pageSet->getCustomField( 'page_len' );
354 // Get protection info if requested
355 if ( $this->fld_protection
) {
356 $this->getProtectionInfo();
359 if ( $this->fld_watched ||
$this->fld_notificationtimestamp
) {
360 $this->getWatchedInfo();
363 if ( $this->fld_watchers
) {
364 $this->getWatcherInfo();
367 if ( $this->fld_visitingwatchers
) {
368 $this->getVisitingWatcherInfo();
371 // Run the talkid/subjectid query if requested
372 if ( $this->fld_talkid ||
$this->fld_subjectid
) {
376 if ( $this->fld_displaytitle
) {
377 $this->getDisplayTitle();
380 if ( $this->fld_varianttitles
) {
381 $this->getVariantTitles();
384 /** @var Title $title */
385 foreach ( $this->everything
as $pageid => $title ) {
386 $pageInfo = $this->extractPageInfo( $pageid, $title );
387 $fit = $pageInfo !== null && $result->addValue( [
390 ], $pageid, $pageInfo );
392 $this->setContinueEnumParameter( 'continue',
393 $title->getNamespace() . '|' .
401 * Get a result array with information about a title
402 * @param int $pageid Page ID (negative for missing titles)
403 * @param Title $title
406 private function extractPageInfo( $pageid, $title ) {
408 // $title->exists() needs pageid, which is not set for all title objects
409 $titleExists = $pageid > 0;
410 $ns = $title->getNamespace();
411 $dbkey = $title->getDBkey();
413 $pageInfo['contentmodel'] = $title->getContentModel();
415 $pageLanguage = $title->getPageLanguage();
416 $pageInfo['pagelanguage'] = $pageLanguage->getCode();
417 $pageInfo['pagelanguagehtmlcode'] = $pageLanguage->getHtmlCode();
418 $pageInfo['pagelanguagedir'] = $pageLanguage->getDir();
420 $user = $this->getUser();
422 if ( $titleExists ) {
423 $pageInfo['touched'] = wfTimestamp( TS_ISO_8601
, $this->pageTouched
[$pageid] );
424 $pageInfo['lastrevid'] = (int)$this->pageLatest
[$pageid];
425 $pageInfo['length'] = (int)$this->pageLength
[$pageid];
427 if ( isset( $this->pageIsRedir
[$pageid] ) && $this->pageIsRedir
[$pageid] ) {
428 $pageInfo['redirect'] = true;
430 if ( $this->pageIsNew
[$pageid] ) {
431 $pageInfo['new'] = true;
435 if ( !is_null( $this->params
['token'] ) ) {
436 $tokenFunctions = $this->getTokenFunctions();
437 $pageInfo['starttimestamp'] = wfTimestamp( TS_ISO_8601
, time() );
438 foreach ( $this->params
['token'] as $t ) {
439 $val = call_user_func( $tokenFunctions[$t], $pageid, $title );
440 if ( $val === false ) {
441 $this->addWarning( [ 'apiwarn-tokennotallowed', $t ] );
443 $pageInfo[$t . 'token'] = $val;
448 if ( $this->fld_protection
) {
449 $pageInfo['protection'] = [];
450 if ( isset( $this->protections
[$ns][$dbkey] ) ) {
451 $pageInfo['protection'] =
452 $this->protections
[$ns][$dbkey];
454 ApiResult
::setIndexedTagName( $pageInfo['protection'], 'pr' );
456 $pageInfo['restrictiontypes'] = [];
457 if ( isset( $this->restrictionTypes
[$ns][$dbkey] ) ) {
458 $pageInfo['restrictiontypes'] =
459 $this->restrictionTypes
[$ns][$dbkey];
461 ApiResult
::setIndexedTagName( $pageInfo['restrictiontypes'], 'rt' );
464 if ( $this->fld_watched
&& $this->watched
!== null ) {
465 $pageInfo['watched'] = $this->watched
[$ns][$dbkey];
468 if ( $this->fld_watchers
) {
469 if ( $this->watchers
!== null && $this->watchers
[$ns][$dbkey] !== 0 ) {
470 $pageInfo['watchers'] = $this->watchers
[$ns][$dbkey];
471 } elseif ( $this->showZeroWatchers
) {
472 $pageInfo['watchers'] = 0;
476 if ( $this->fld_visitingwatchers
) {
477 if ( $this->visitingwatchers
!== null && $this->visitingwatchers
[$ns][$dbkey] !== 0 ) {
478 $pageInfo['visitingwatchers'] = $this->visitingwatchers
[$ns][$dbkey];
479 } elseif ( $this->showZeroWatchers
) {
480 $pageInfo['visitingwatchers'] = 0;
484 if ( $this->fld_notificationtimestamp
) {
485 $pageInfo['notificationtimestamp'] = '';
486 if ( $this->notificationtimestamps
[$ns][$dbkey] ) {
487 $pageInfo['notificationtimestamp'] =
488 wfTimestamp( TS_ISO_8601
, $this->notificationtimestamps
[$ns][$dbkey] );
492 if ( $this->fld_talkid
&& isset( $this->talkids
[$ns][$dbkey] ) ) {
493 $pageInfo['talkid'] = $this->talkids
[$ns][$dbkey];
496 if ( $this->fld_subjectid
&& isset( $this->subjectids
[$ns][$dbkey] ) ) {
497 $pageInfo['subjectid'] = $this->subjectids
[$ns][$dbkey];
500 if ( $this->fld_url
) {
501 $pageInfo['fullurl'] = wfExpandUrl( $title->getFullURL(), PROTO_CURRENT
);
502 $pageInfo['editurl'] = wfExpandUrl( $title->getFullURL( 'action=edit' ), PROTO_CURRENT
);
503 $pageInfo['canonicalurl'] = wfExpandUrl( $title->getFullURL(), PROTO_CANONICAL
);
505 if ( $this->fld_readable
) {
506 $pageInfo['readable'] = $this->getPermissionManager()->userCan(
507 'read', $user, $title
511 if ( $this->fld_preload
) {
512 if ( $titleExists ) {
513 $pageInfo['preload'] = '';
516 Hooks
::run( 'EditFormPreloadText', [ &$text, &$title ] );
518 $pageInfo['preload'] = $text;
522 if ( $this->fld_displaytitle
) {
523 if ( isset( $this->displaytitles
[$pageid] ) ) {
524 $pageInfo['displaytitle'] = $this->displaytitles
[$pageid];
526 $pageInfo['displaytitle'] = $title->getPrefixedText();
530 if ( $this->fld_varianttitles
&& isset( $this->variantTitles
[$pageid] ) ) {
531 $pageInfo['varianttitles'] = $this->variantTitles
[$pageid];
534 if ( $this->params
['testactions'] ) {
535 $limit = $this->getMain()->canApiHighLimits() ? self
::LIMIT_SML2
: self
::LIMIT_SML1
;
536 if ( $this->countTestedActions
>= $limit ) {
537 return null; // force a continuation
540 $detailLevel = $this->params
['testactionsdetail'];
541 $rigor = $detailLevel === 'quick' ?
'quick' : 'secure';
542 $errorFormatter = $this->getErrorFormatter();
543 if ( $errorFormatter->getFormat() === 'bc' ) {
544 // Eew, no. Use a more modern format here.
545 $errorFormatter = $errorFormatter->newWithFormat( 'plaintext' );
548 $user = $this->getUser();
549 $pageInfo['actions'] = [];
550 foreach ( $this->params
['testactions'] as $action ) {
551 $this->countTestedActions++
;
553 if ( $detailLevel === 'boolean' ) {
554 $pageInfo['actions'][$action] = $this->getPermissionManager()->userCan(
555 $action, $user, $title
558 $pageInfo['actions'][$action] = $errorFormatter->arrayFromStatus( $this->errorArrayToStatus(
559 $this->getPermissionManager()->getPermissionErrors(
560 $action, $user, $title, $rigor
572 * Get information about protections and put it in $protections
574 private function getProtectionInfo() {
575 $this->protections
= [];
576 $db = $this->getDB();
578 // Get normal protections for existing titles
579 if ( count( $this->titles
) ) {
580 $this->resetQueryParams();
581 $this->addTables( 'page_restrictions' );
582 $this->addFields( [ 'pr_page', 'pr_type', 'pr_level',
583 'pr_expiry', 'pr_cascade' ] );
584 $this->addWhereFld( 'pr_page', array_keys( $this->titles
) );
586 $res = $this->select( __METHOD__
);
587 foreach ( $res as $row ) {
588 /** @var Title $title */
589 $title = $this->titles
[$row->pr_page
];
591 'type' => $row->pr_type
,
592 'level' => $row->pr_level
,
593 'expiry' => ApiResult
::formatExpiry( $row->pr_expiry
)
595 if ( $row->pr_cascade
) {
596 $a['cascade'] = true;
598 $this->protections
[$title->getNamespace()][$title->getDBkey()][] = $a;
600 // Also check old restrictions
601 foreach ( $this->titles
as $pageId => $title ) {
602 if ( $this->pageRestrictions
[$pageId] ) {
603 $namespace = $title->getNamespace();
604 $dbKey = $title->getDBkey();
605 $restrictions = explode( ':', trim( $this->pageRestrictions
[$pageId] ) );
606 foreach ( $restrictions as $restrict ) {
607 $temp = explode( '=', trim( $restrict ) );
608 if ( count( $temp ) == 1 ) {
609 // old old format should be treated as edit/move restriction
610 $restriction = trim( $temp[0] );
612 if ( $restriction == '' ) {
615 $this->protections
[$namespace][$dbKey][] = [
617 'level' => $restriction,
618 'expiry' => 'infinity',
620 $this->protections
[$namespace][$dbKey][] = [
622 'level' => $restriction,
623 'expiry' => 'infinity',
626 $restriction = trim( $temp[1] );
627 if ( $restriction == '' ) {
630 $this->protections
[$namespace][$dbKey][] = [
632 'level' => $restriction,
633 'expiry' => 'infinity',
641 // Get protections for missing titles
642 if ( count( $this->missing
) ) {
643 $this->resetQueryParams();
644 $lb = new LinkBatch( $this->missing
);
645 $this->addTables( 'protected_titles' );
646 $this->addFields( [ 'pt_title', 'pt_namespace', 'pt_create_perm', 'pt_expiry' ] );
647 $this->addWhere( $lb->constructSet( 'pt', $db ) );
648 $res = $this->select( __METHOD__
);
649 foreach ( $res as $row ) {
650 $this->protections
[$row->pt_namespace
][$row->pt_title
][] = [
652 'level' => $row->pt_create_perm
,
653 'expiry' => ApiResult
::formatExpiry( $row->pt_expiry
)
658 // Separate good and missing titles into files and other pages
659 // and populate $this->restrictionTypes
660 $images = $others = [];
661 foreach ( $this->everything
as $title ) {
662 if ( $title->getNamespace() == NS_FILE
) {
663 $images[] = $title->getDBkey();
667 // Applicable protection types
668 $this->restrictionTypes
[$title->getNamespace()][$title->getDBkey()] =
669 array_values( $title->getRestrictionTypes() );
672 if ( count( $others ) ) {
673 // Non-images: check templatelinks
674 $lb = new LinkBatch( $others );
675 $this->resetQueryParams();
676 $this->addTables( [ 'page_restrictions', 'page', 'templatelinks' ] );
677 $this->addFields( [ 'pr_type', 'pr_level', 'pr_expiry',
678 'page_title', 'page_namespace',
679 'tl_title', 'tl_namespace' ] );
680 $this->addWhere( $lb->constructSet( 'tl', $db ) );
681 $this->addWhere( 'pr_page = page_id' );
682 $this->addWhere( 'pr_page = tl_from' );
683 $this->addWhereFld( 'pr_cascade', 1 );
685 $res = $this->select( __METHOD__
);
686 foreach ( $res as $row ) {
687 $source = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
688 $this->protections
[$row->tl_namespace
][$row->tl_title
][] = [
689 'type' => $row->pr_type
,
690 'level' => $row->pr_level
,
691 'expiry' => ApiResult
::formatExpiry( $row->pr_expiry
),
692 'source' => $source->getPrefixedText()
697 if ( count( $images ) ) {
698 // Images: check imagelinks
699 $this->resetQueryParams();
700 $this->addTables( [ 'page_restrictions', 'page', 'imagelinks' ] );
701 $this->addFields( [ 'pr_type', 'pr_level', 'pr_expiry',
702 'page_title', 'page_namespace', 'il_to' ] );
703 $this->addWhere( 'pr_page = page_id' );
704 $this->addWhere( 'pr_page = il_from' );
705 $this->addWhereFld( 'pr_cascade', 1 );
706 $this->addWhereFld( 'il_to', $images );
708 $res = $this->select( __METHOD__
);
709 foreach ( $res as $row ) {
710 $source = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
711 $this->protections
[NS_FILE
][$row->il_to
][] = [
712 'type' => $row->pr_type
,
713 'level' => $row->pr_level
,
714 'expiry' => ApiResult
::formatExpiry( $row->pr_expiry
),
715 'source' => $source->getPrefixedText()
722 * Get talk page IDs (if requested) and subject page IDs (if requested)
723 * and put them in $talkids and $subjectids
725 private function getTSIDs() {
726 $getTitles = $this->talkids
= $this->subjectids
= [];
727 $nsInfo = MediaWikiServices
::getInstance()->getNamespaceInfo();
730 foreach ( $this->everything
as $t ) {
731 if ( $nsInfo->isTalk( $t->getNamespace() ) ) {
732 if ( $this->fld_subjectid
) {
733 $getTitles[] = $t->getSubjectPage();
735 } elseif ( $this->fld_talkid
) {
736 $getTitles[] = $t->getTalkPage();
739 if ( $getTitles === [] ) {
743 $db = $this->getDB();
745 // Construct a custom WHERE clause that matches
746 // all titles in $getTitles
747 $lb = new LinkBatch( $getTitles );
748 $this->resetQueryParams();
749 $this->addTables( 'page' );
750 $this->addFields( [ 'page_title', 'page_namespace', 'page_id' ] );
751 $this->addWhere( $lb->constructSet( 'page', $db ) );
752 $res = $this->select( __METHOD__
);
753 foreach ( $res as $row ) {
754 if ( $nsInfo->isTalk( $row->page_namespace
) ) {
755 $this->talkids
[$nsInfo->getSubject( $row->page_namespace
)][$row->page_title
] =
756 (int)( $row->page_id
);
758 $this->subjectids
[$nsInfo->getTalk( $row->page_namespace
)][$row->page_title
] =
759 (int)( $row->page_id
);
764 private function getDisplayTitle() {
765 $this->displaytitles
= [];
767 $pageIds = array_keys( $this->titles
);
769 if ( $pageIds === [] ) {
773 $this->resetQueryParams();
774 $this->addTables( 'page_props' );
775 $this->addFields( [ 'pp_page', 'pp_value' ] );
776 $this->addWhereFld( 'pp_page', $pageIds );
777 $this->addWhereFld( 'pp_propname', 'displaytitle' );
778 $res = $this->select( __METHOD__
);
780 foreach ( $res as $row ) {
781 $this->displaytitles
[$row->pp_page
] = $row->pp_value
;
785 private function getVariantTitles() {
786 if ( $this->titles
=== [] ) {
789 $this->variantTitles
= [];
790 foreach ( $this->titles
as $pageId => $t ) {
791 $this->variantTitles
[$pageId] = isset( $this->displaytitles
[$pageId] )
792 ?
$this->getAllVariants( $this->displaytitles
[$pageId] )
793 : $this->getAllVariants( $t->getText(), $t->getNamespace() );
797 private function getAllVariants( $text, $ns = NS_MAIN
) {
799 $contLang = MediaWikiServices
::getInstance()->getContentLanguage();
800 foreach ( $contLang->getVariants() as $variant ) {
801 $convertTitle = $contLang->autoConvert( $text, $variant );
802 if ( $ns !== NS_MAIN
) {
803 $convertNs = $contLang->convertNamespace( $ns, $variant );
804 $convertTitle = $convertNs . ':' . $convertTitle;
806 $result[$variant] = $convertTitle;
812 * Get information about watched status and put it in $this->watched
813 * and $this->notificationtimestamps
815 private function getWatchedInfo() {
816 $user = $this->getUser();
818 if ( $user->isAnon() ||
count( $this->everything
) == 0
819 ||
!$this->getPermissionManager()->userHasRight( $user, 'viewmywatchlist' )
825 $this->notificationtimestamps
= [];
827 $store = MediaWikiServices
::getInstance()->getWatchedItemStore();
828 $timestamps = $store->getNotificationTimestampsBatch( $user, $this->everything
);
830 if ( $this->fld_watched
) {
831 foreach ( $timestamps as $namespaceId => $dbKeys ) {
832 $this->watched
[$namespaceId] = array_map(
840 if ( $this->fld_notificationtimestamp
) {
841 $this->notificationtimestamps
= $timestamps;
846 * Get the count of watchers and put it in $this->watchers
848 private function getWatcherInfo() {
849 if ( count( $this->everything
) == 0 ) {
853 $user = $this->getUser();
854 $canUnwatchedpages = $this->getPermissionManager()->userHasRight( $user, 'unwatchedpages' );
855 $unwatchedPageThreshold = $this->getConfig()->get( 'UnwatchedPageThreshold' );
856 if ( !$canUnwatchedpages && !is_int( $unwatchedPageThreshold ) ) {
860 $this->showZeroWatchers
= $canUnwatchedpages;
863 if ( !$canUnwatchedpages ) {
864 $countOptions['minimumWatchers'] = $unwatchedPageThreshold;
867 $this->watchers
= MediaWikiServices
::getInstance()->getWatchedItemStore()->countWatchersMultiple(
874 * Get the count of watchers who have visited recent edits and put it in
875 * $this->visitingwatchers
877 * Based on InfoAction::pageCounts
879 private function getVisitingWatcherInfo() {
880 $config = $this->getConfig();
881 $user = $this->getUser();
882 $db = $this->getDB();
884 $canUnwatchedpages = $this->getPermissionManager()->userHasRight( $user, 'unwatchedpages' );
885 $unwatchedPageThreshold = $this->getConfig()->get( 'UnwatchedPageThreshold' );
886 if ( !$canUnwatchedpages && !is_int( $unwatchedPageThreshold ) ) {
890 $this->showZeroWatchers
= $canUnwatchedpages;
892 $titlesWithThresholds = [];
893 if ( $this->titles
) {
894 $lb = new LinkBatch( $this->titles
);
896 // Fetch last edit timestamps for pages
897 $this->resetQueryParams();
898 $this->addTables( [ 'page', 'revision' ] );
899 $this->addFields( [ 'page_namespace', 'page_title', 'rev_timestamp' ] );
901 'page_latest = rev_id',
902 $lb->constructSet( 'page', $db ),
904 $this->addOption( 'GROUP BY', [ 'page_namespace', 'page_title' ] );
905 $timestampRes = $this->select( __METHOD__
);
907 $age = $config->get( 'WatchersMaxAge' );
909 foreach ( $timestampRes as $row ) {
910 $revTimestamp = wfTimestamp( TS_UNIX
, (int)$row->rev_timestamp
);
911 $timestamps[$row->page_namespace
][$row->page_title
] = $revTimestamp - $age;
913 $titlesWithThresholds = array_map(
914 function ( LinkTarget
$target ) use ( $timestamps ) {
916 $target, $timestamps[$target->getNamespace()][$target->getDBkey()]
923 if ( $this->missing
) {
924 $titlesWithThresholds = array_merge(
925 $titlesWithThresholds,
927 function ( LinkTarget
$target ) {
928 return [ $target, null ];
934 $store = MediaWikiServices
::getInstance()->getWatchedItemStore();
935 $this->visitingwatchers
= $store->countVisitingWatchersMultiple(
936 $titlesWithThresholds,
937 !$canUnwatchedpages ?
$unwatchedPageThreshold : null
941 public function getCacheMode( $params ) {
942 // Other props depend on something about the current user
952 if ( array_diff( (array)$params['prop'], $publicProps ) ) {
956 // testactions also depends on the current user
957 if ( $params['testactions'] ) {
961 if ( !is_null( $params['token'] ) ) {
968 public function getAllowedParams() {
971 ApiBase
::PARAM_ISMULTI
=> true,
972 ApiBase
::PARAM_TYPE
=> [
976 'watchers', # private
977 'visitingwatchers', # private
978 'notificationtimestamp', # private
981 'readable', # private
985 // If you add more properties here, please consider whether they
986 // need to be added to getCacheMode()
988 ApiBase
::PARAM_HELP_MSG_PER_VALUE
=> [],
989 ApiBase
::PARAM_DEPRECATED_VALUES
=> [
990 'readable' => true, // Since 1.32
994 ApiBase
::PARAM_TYPE
=> 'string',
995 ApiBase
::PARAM_ISMULTI
=> true,
997 'testactionsdetail' => [
998 ApiBase
::PARAM_TYPE
=> [ 'boolean', 'full', 'quick' ],
999 ApiBase
::PARAM_DFLT
=> 'boolean',
1000 ApiBase
::PARAM_HELP_MSG_PER_VALUE
=> [],
1003 ApiBase
::PARAM_DEPRECATED
=> true,
1004 ApiBase
::PARAM_ISMULTI
=> true,
1005 ApiBase
::PARAM_TYPE
=> array_keys( $this->getTokenFunctions() )
1008 ApiBase
::PARAM_HELP_MSG
=> 'api-help-param-continue',
1013 protected function getExamplesMessages() {
1015 'action=query&prop=info&titles=Main%20Page'
1016 => 'apihelp-query+info-example-simple',
1017 'action=query&prop=info&inprop=protection&titles=Main%20Page'
1018 => 'apihelp-query+info-example-protection',
1022 public function getHelpUrls() {
1023 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Info';