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 ( !$wgUser->isAllowed( 'edit' ) ) {
142 // The token is always the same, let's exploit that
143 if ( !isset( self
::$cachedTokens['edit'] ) ) {
144 self
::$cachedTokens['edit'] = $wgUser->getEditToken();
147 return self
::$cachedTokens['edit'];
151 * @deprecated since 1.24
153 public static function getDeleteToken( $pageid, $title ) {
155 if ( !$wgUser->isAllowed( 'delete' ) ) {
159 // The token is always the same, let's exploit that
160 if ( !isset( self
::$cachedTokens['delete'] ) ) {
161 self
::$cachedTokens['delete'] = $wgUser->getEditToken();
164 return self
::$cachedTokens['delete'];
168 * @deprecated since 1.24
170 public static function getProtectToken( $pageid, $title ) {
172 if ( !$wgUser->isAllowed( 'protect' ) ) {
176 // The token is always the same, let's exploit that
177 if ( !isset( self
::$cachedTokens['protect'] ) ) {
178 self
::$cachedTokens['protect'] = $wgUser->getEditToken();
181 return self
::$cachedTokens['protect'];
185 * @deprecated since 1.24
187 public static function getMoveToken( $pageid, $title ) {
189 if ( !$wgUser->isAllowed( 'move' ) ) {
193 // The token is always the same, let's exploit that
194 if ( !isset( self
::$cachedTokens['move'] ) ) {
195 self
::$cachedTokens['move'] = $wgUser->getEditToken();
198 return self
::$cachedTokens['move'];
202 * @deprecated since 1.24
204 public static function getBlockToken( $pageid, $title ) {
206 if ( !$wgUser->isAllowed( 'block' ) ) {
210 // The token is always the same, let's exploit that
211 if ( !isset( self
::$cachedTokens['block'] ) ) {
212 self
::$cachedTokens['block'] = $wgUser->getEditToken();
215 return self
::$cachedTokens['block'];
219 * @deprecated since 1.24
221 public static function getUnblockToken( $pageid, $title ) {
222 // Currently, this is exactly the same as the block token
223 return self
::getBlockToken( $pageid, $title );
227 * @deprecated since 1.24
229 public static function getEmailToken( $pageid, $title ) {
231 if ( !$wgUser->canSendEmail() ||
$wgUser->isBlockedFromEmailuser() ) {
235 // The token is always the same, let's exploit that
236 if ( !isset( self
::$cachedTokens['email'] ) ) {
237 self
::$cachedTokens['email'] = $wgUser->getEditToken();
240 return self
::$cachedTokens['email'];
244 * @deprecated since 1.24
246 public static function getImportToken( $pageid, $title ) {
248 if ( !$wgUser->isAllowedAny( 'import', 'importupload' ) ) {
252 // The token is always the same, let's exploit that
253 if ( !isset( self
::$cachedTokens['import'] ) ) {
254 self
::$cachedTokens['import'] = $wgUser->getEditToken();
257 return self
::$cachedTokens['import'];
261 * @deprecated since 1.24
263 public static function getWatchToken( $pageid, $title ) {
265 if ( !$wgUser->isLoggedIn() ) {
269 // The token is always the same, let's exploit that
270 if ( !isset( self
::$cachedTokens['watch'] ) ) {
271 self
::$cachedTokens['watch'] = $wgUser->getEditToken( 'watch' );
274 return self
::$cachedTokens['watch'];
278 * @deprecated since 1.24
280 public static function getOptionsToken( $pageid, $title ) {
282 if ( !$wgUser->isLoggedIn() ) {
286 // The token is always the same, let's exploit that
287 if ( !isset( self
::$cachedTokens['options'] ) ) {
288 self
::$cachedTokens['options'] = $wgUser->getEditToken();
291 return self
::$cachedTokens['options'];
294 public function execute() {
295 $this->params
= $this->extractRequestParams();
296 if ( !is_null( $this->params
['prop'] ) ) {
297 $prop = array_flip( $this->params
['prop'] );
298 $this->fld_protection
= isset( $prop['protection'] );
299 $this->fld_watched
= isset( $prop['watched'] );
300 $this->fld_watchers
= isset( $prop['watchers'] );
301 $this->fld_visitingwatchers
= isset( $prop['visitingwatchers'] );
302 $this->fld_notificationtimestamp
= isset( $prop['notificationtimestamp'] );
303 $this->fld_talkid
= isset( $prop['talkid'] );
304 $this->fld_subjectid
= isset( $prop['subjectid'] );
305 $this->fld_url
= isset( $prop['url'] );
306 $this->fld_readable
= isset( $prop['readable'] );
307 $this->fld_preload
= isset( $prop['preload'] );
308 $this->fld_displaytitle
= isset( $prop['displaytitle'] );
309 $this->fld_varianttitles
= isset( $prop['varianttitles'] );
312 $pageSet = $this->getPageSet();
313 $this->titles
= $pageSet->getGoodTitles();
314 $this->missing
= $pageSet->getMissingTitles();
315 $this->everything
= $this->titles +
$this->missing
;
316 $result = $this->getResult();
318 uasort( $this->everything
, [ Title
::class, 'compare' ] );
319 if ( !is_null( $this->params
['continue'] ) ) {
320 // Throw away any titles we're gonna skip so they don't
322 $cont = explode( '|', $this->params
['continue'] );
323 $this->dieContinueUsageIf( count( $cont ) != 2 );
324 $conttitle = Title
::makeTitleSafe( $cont[0], $cont[1] );
325 foreach ( $this->everything
as $pageid => $title ) {
326 if ( Title
::compare( $title, $conttitle ) >= 0 ) {
329 unset( $this->titles
[$pageid] );
330 unset( $this->missing
[$pageid] );
331 unset( $this->everything
[$pageid] );
335 $this->pageRestrictions
= $pageSet->getCustomField( 'page_restrictions' );
336 // when resolving redirects, no page will have this field
337 $this->pageIsRedir
= !$pageSet->isResolvingRedirects()
338 ?
$pageSet->getCustomField( 'page_is_redirect' )
340 $this->pageIsNew
= $pageSet->getCustomField( 'page_is_new' );
342 $this->pageTouched
= $pageSet->getCustomField( 'page_touched' );
343 $this->pageLatest
= $pageSet->getCustomField( 'page_latest' );
344 $this->pageLength
= $pageSet->getCustomField( 'page_len' );
346 // Get protection info if requested
347 if ( $this->fld_protection
) {
348 $this->getProtectionInfo();
351 if ( $this->fld_watched ||
$this->fld_notificationtimestamp
) {
352 $this->getWatchedInfo();
355 if ( $this->fld_watchers
) {
356 $this->getWatcherInfo();
359 if ( $this->fld_visitingwatchers
) {
360 $this->getVisitingWatcherInfo();
363 // Run the talkid/subjectid query if requested
364 if ( $this->fld_talkid ||
$this->fld_subjectid
) {
368 if ( $this->fld_displaytitle
) {
369 $this->getDisplayTitle();
372 if ( $this->fld_varianttitles
) {
373 $this->getVariantTitles();
376 /** @var Title $title */
377 foreach ( $this->everything
as $pageid => $title ) {
378 $pageInfo = $this->extractPageInfo( $pageid, $title );
379 $fit = $pageInfo !== null && $result->addValue( [
382 ], $pageid, $pageInfo );
384 $this->setContinueEnumParameter( 'continue',
385 $title->getNamespace() . '|' .
393 * Get a result array with information about a title
394 * @param int $pageid Page ID (negative for missing titles)
395 * @param Title $title
398 private function extractPageInfo( $pageid, $title ) {
400 // $title->exists() needs pageid, which is not set for all title objects
401 $titleExists = $pageid > 0;
402 $ns = $title->getNamespace();
403 $dbkey = $title->getDBkey();
405 $pageInfo['contentmodel'] = $title->getContentModel();
407 $pageLanguage = $title->getPageLanguage();
408 $pageInfo['pagelanguage'] = $pageLanguage->getCode();
409 $pageInfo['pagelanguagehtmlcode'] = $pageLanguage->getHtmlCode();
410 $pageInfo['pagelanguagedir'] = $pageLanguage->getDir();
412 if ( $titleExists ) {
413 $pageInfo['touched'] = wfTimestamp( TS_ISO_8601
, $this->pageTouched
[$pageid] );
414 $pageInfo['lastrevid'] = (int)$this->pageLatest
[$pageid];
415 $pageInfo['length'] = (int)$this->pageLength
[$pageid];
417 if ( isset( $this->pageIsRedir
[$pageid] ) && $this->pageIsRedir
[$pageid] ) {
418 $pageInfo['redirect'] = true;
420 if ( $this->pageIsNew
[$pageid] ) {
421 $pageInfo['new'] = true;
425 if ( !is_null( $this->params
['token'] ) ) {
426 $tokenFunctions = $this->getTokenFunctions();
427 $pageInfo['starttimestamp'] = wfTimestamp( TS_ISO_8601
, time() );
428 foreach ( $this->params
['token'] as $t ) {
429 $val = call_user_func( $tokenFunctions[$t], $pageid, $title );
430 if ( $val === false ) {
431 $this->addWarning( [ 'apiwarn-tokennotallowed', $t ] );
433 $pageInfo[$t . 'token'] = $val;
438 if ( $this->fld_protection
) {
439 $pageInfo['protection'] = [];
440 if ( isset( $this->protections
[$ns][$dbkey] ) ) {
441 $pageInfo['protection'] =
442 $this->protections
[$ns][$dbkey];
444 ApiResult
::setIndexedTagName( $pageInfo['protection'], 'pr' );
446 $pageInfo['restrictiontypes'] = [];
447 if ( isset( $this->restrictionTypes
[$ns][$dbkey] ) ) {
448 $pageInfo['restrictiontypes'] =
449 $this->restrictionTypes
[$ns][$dbkey];
451 ApiResult
::setIndexedTagName( $pageInfo['restrictiontypes'], 'rt' );
454 if ( $this->fld_watched
&& $this->watched
!== null ) {
455 $pageInfo['watched'] = $this->watched
[$ns][$dbkey];
458 if ( $this->fld_watchers
) {
459 if ( $this->watchers
!== null && $this->watchers
[$ns][$dbkey] !== 0 ) {
460 $pageInfo['watchers'] = $this->watchers
[$ns][$dbkey];
461 } elseif ( $this->showZeroWatchers
) {
462 $pageInfo['watchers'] = 0;
466 if ( $this->fld_visitingwatchers
) {
467 if ( $this->visitingwatchers
!== null && $this->visitingwatchers
[$ns][$dbkey] !== 0 ) {
468 $pageInfo['visitingwatchers'] = $this->visitingwatchers
[$ns][$dbkey];
469 } elseif ( $this->showZeroWatchers
) {
470 $pageInfo['visitingwatchers'] = 0;
474 if ( $this->fld_notificationtimestamp
) {
475 $pageInfo['notificationtimestamp'] = '';
476 if ( $this->notificationtimestamps
[$ns][$dbkey] ) {
477 $pageInfo['notificationtimestamp'] =
478 wfTimestamp( TS_ISO_8601
, $this->notificationtimestamps
[$ns][$dbkey] );
482 if ( $this->fld_talkid
&& isset( $this->talkids
[$ns][$dbkey] ) ) {
483 $pageInfo['talkid'] = $this->talkids
[$ns][$dbkey];
486 if ( $this->fld_subjectid
&& isset( $this->subjectids
[$ns][$dbkey] ) ) {
487 $pageInfo['subjectid'] = $this->subjectids
[$ns][$dbkey];
490 if ( $this->fld_url
) {
491 $pageInfo['fullurl'] = wfExpandUrl( $title->getFullURL(), PROTO_CURRENT
);
492 $pageInfo['editurl'] = wfExpandUrl( $title->getFullURL( 'action=edit' ), PROTO_CURRENT
);
493 $pageInfo['canonicalurl'] = wfExpandUrl( $title->getFullURL(), PROTO_CANONICAL
);
495 if ( $this->fld_readable
) {
496 $pageInfo['readable'] = $title->userCan( 'read', $this->getUser() );
499 if ( $this->fld_preload
) {
500 if ( $titleExists ) {
501 $pageInfo['preload'] = '';
504 Hooks
::run( 'EditFormPreloadText', [ &$text, &$title ] );
506 $pageInfo['preload'] = $text;
510 if ( $this->fld_displaytitle
) {
511 if ( isset( $this->displaytitles
[$pageid] ) ) {
512 $pageInfo['displaytitle'] = $this->displaytitles
[$pageid];
514 $pageInfo['displaytitle'] = $title->getPrefixedText();
518 if ( $this->fld_varianttitles
&& isset( $this->variantTitles
[$pageid] ) ) {
519 $pageInfo['varianttitles'] = $this->variantTitles
[$pageid];
522 if ( $this->params
['testactions'] ) {
523 $limit = $this->getMain()->canApiHighLimits() ? self
::LIMIT_SML2
: self
::LIMIT_SML1
;
524 if ( $this->countTestedActions
>= $limit ) {
525 return null; // force a continuation
528 $detailLevel = $this->params
['testactionsdetail'];
529 $rigor = $detailLevel === 'quick' ?
'quick' : 'secure';
530 $errorFormatter = $this->getErrorFormatter();
531 if ( $errorFormatter->getFormat() === 'bc' ) {
532 // Eew, no. Use a more modern format here.
533 $errorFormatter = $errorFormatter->newWithFormat( 'plaintext' );
536 $user = $this->getUser();
537 $pageInfo['actions'] = [];
538 foreach ( $this->params
['testactions'] as $action ) {
539 $this->countTestedActions++
;
541 if ( $detailLevel === 'boolean' ) {
542 $pageInfo['actions'][$action] = $title->userCan( $action, $user );
544 $pageInfo['actions'][$action] = $errorFormatter->arrayFromStatus( $this->errorArrayToStatus(
545 $title->getUserPermissionsErrors( $action, $user, $rigor ),
556 * Get information about protections and put it in $protections
558 private function getProtectionInfo() {
559 $this->protections
= [];
560 $db = $this->getDB();
562 // Get normal protections for existing titles
563 if ( count( $this->titles
) ) {
564 $this->resetQueryParams();
565 $this->addTables( 'page_restrictions' );
566 $this->addFields( [ 'pr_page', 'pr_type', 'pr_level',
567 'pr_expiry', 'pr_cascade' ] );
568 $this->addWhereFld( 'pr_page', array_keys( $this->titles
) );
570 $res = $this->select( __METHOD__
);
571 foreach ( $res as $row ) {
572 /** @var Title $title */
573 $title = $this->titles
[$row->pr_page
];
575 'type' => $row->pr_type
,
576 'level' => $row->pr_level
,
577 'expiry' => ApiResult
::formatExpiry( $row->pr_expiry
)
579 if ( $row->pr_cascade
) {
580 $a['cascade'] = true;
582 $this->protections
[$title->getNamespace()][$title->getDBkey()][] = $a;
584 // Also check old restrictions
585 foreach ( $this->titles
as $pageId => $title ) {
586 if ( $this->pageRestrictions
[$pageId] ) {
587 $namespace = $title->getNamespace();
588 $dbKey = $title->getDBkey();
589 $restrictions = explode( ':', trim( $this->pageRestrictions
[$pageId] ) );
590 foreach ( $restrictions as $restrict ) {
591 $temp = explode( '=', trim( $restrict ) );
592 if ( count( $temp ) == 1 ) {
593 // old old format should be treated as edit/move restriction
594 $restriction = trim( $temp[0] );
596 if ( $restriction == '' ) {
599 $this->protections
[$namespace][$dbKey][] = [
601 'level' => $restriction,
602 'expiry' => 'infinity',
604 $this->protections
[$namespace][$dbKey][] = [
606 'level' => $restriction,
607 'expiry' => 'infinity',
610 $restriction = trim( $temp[1] );
611 if ( $restriction == '' ) {
614 $this->protections
[$namespace][$dbKey][] = [
616 'level' => $restriction,
617 'expiry' => 'infinity',
625 // Get protections for missing titles
626 if ( count( $this->missing
) ) {
627 $this->resetQueryParams();
628 $lb = new LinkBatch( $this->missing
);
629 $this->addTables( 'protected_titles' );
630 $this->addFields( [ 'pt_title', 'pt_namespace', 'pt_create_perm', 'pt_expiry' ] );
631 $this->addWhere( $lb->constructSet( 'pt', $db ) );
632 $res = $this->select( __METHOD__
);
633 foreach ( $res as $row ) {
634 $this->protections
[$row->pt_namespace
][$row->pt_title
][] = [
636 'level' => $row->pt_create_perm
,
637 'expiry' => ApiResult
::formatExpiry( $row->pt_expiry
)
642 // Separate good and missing titles into files and other pages
643 // and populate $this->restrictionTypes
644 $images = $others = [];
645 foreach ( $this->everything
as $title ) {
646 if ( $title->getNamespace() == NS_FILE
) {
647 $images[] = $title->getDBkey();
651 // Applicable protection types
652 $this->restrictionTypes
[$title->getNamespace()][$title->getDBkey()] =
653 array_values( $title->getRestrictionTypes() );
656 if ( count( $others ) ) {
657 // Non-images: check templatelinks
658 $lb = new LinkBatch( $others );
659 $this->resetQueryParams();
660 $this->addTables( [ 'page_restrictions', 'page', 'templatelinks' ] );
661 $this->addFields( [ 'pr_type', 'pr_level', 'pr_expiry',
662 'page_title', 'page_namespace',
663 'tl_title', 'tl_namespace' ] );
664 $this->addWhere( $lb->constructSet( 'tl', $db ) );
665 $this->addWhere( 'pr_page = page_id' );
666 $this->addWhere( 'pr_page = tl_from' );
667 $this->addWhereFld( 'pr_cascade', 1 );
669 $res = $this->select( __METHOD__
);
670 foreach ( $res as $row ) {
671 $source = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
672 $this->protections
[$row->tl_namespace
][$row->tl_title
][] = [
673 'type' => $row->pr_type
,
674 'level' => $row->pr_level
,
675 'expiry' => ApiResult
::formatExpiry( $row->pr_expiry
),
676 'source' => $source->getPrefixedText()
681 if ( count( $images ) ) {
682 // Images: check imagelinks
683 $this->resetQueryParams();
684 $this->addTables( [ 'page_restrictions', 'page', 'imagelinks' ] );
685 $this->addFields( [ 'pr_type', 'pr_level', 'pr_expiry',
686 'page_title', 'page_namespace', 'il_to' ] );
687 $this->addWhere( 'pr_page = page_id' );
688 $this->addWhere( 'pr_page = il_from' );
689 $this->addWhereFld( 'pr_cascade', 1 );
690 $this->addWhereFld( 'il_to', $images );
692 $res = $this->select( __METHOD__
);
693 foreach ( $res as $row ) {
694 $source = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
695 $this->protections
[NS_FILE
][$row->il_to
][] = [
696 'type' => $row->pr_type
,
697 'level' => $row->pr_level
,
698 'expiry' => ApiResult
::formatExpiry( $row->pr_expiry
),
699 'source' => $source->getPrefixedText()
706 * Get talk page IDs (if requested) and subject page IDs (if requested)
707 * and put them in $talkids and $subjectids
709 private function getTSIDs() {
710 $getTitles = $this->talkids
= $this->subjectids
= [];
711 $nsInfo = MediaWikiServices
::getInstance()->getNamespaceInfo();
714 foreach ( $this->everything
as $t ) {
715 if ( $nsInfo->isTalk( $t->getNamespace() ) ) {
716 if ( $this->fld_subjectid
) {
717 $getTitles[] = $t->getSubjectPage();
719 } elseif ( $this->fld_talkid
) {
720 $getTitles[] = $t->getTalkPage();
723 if ( $getTitles === [] ) {
727 $db = $this->getDB();
729 // Construct a custom WHERE clause that matches
730 // all titles in $getTitles
731 $lb = new LinkBatch( $getTitles );
732 $this->resetQueryParams();
733 $this->addTables( 'page' );
734 $this->addFields( [ 'page_title', 'page_namespace', 'page_id' ] );
735 $this->addWhere( $lb->constructSet( 'page', $db ) );
736 $res = $this->select( __METHOD__
);
737 foreach ( $res as $row ) {
738 if ( $nsInfo->isTalk( $row->page_namespace
) ) {
739 $this->talkids
[$nsInfo->getSubject( $row->page_namespace
)][$row->page_title
] =
740 (int)( $row->page_id
);
742 $this->subjectids
[$nsInfo->getTalk( $row->page_namespace
)][$row->page_title
] =
743 (int)( $row->page_id
);
748 private function getDisplayTitle() {
749 $this->displaytitles
= [];
751 $pageIds = array_keys( $this->titles
);
753 if ( $pageIds === [] ) {
757 $this->resetQueryParams();
758 $this->addTables( 'page_props' );
759 $this->addFields( [ 'pp_page', 'pp_value' ] );
760 $this->addWhereFld( 'pp_page', $pageIds );
761 $this->addWhereFld( 'pp_propname', 'displaytitle' );
762 $res = $this->select( __METHOD__
);
764 foreach ( $res as $row ) {
765 $this->displaytitles
[$row->pp_page
] = $row->pp_value
;
769 private function getVariantTitles() {
770 if ( $this->titles
=== [] ) {
773 $this->variantTitles
= [];
774 foreach ( $this->titles
as $pageId => $t ) {
775 $this->variantTitles
[$pageId] = isset( $this->displaytitles
[$pageId] )
776 ?
$this->getAllVariants( $this->displaytitles
[$pageId] )
777 : $this->getAllVariants( $t->getText(), $t->getNamespace() );
781 private function getAllVariants( $text, $ns = NS_MAIN
) {
783 $contLang = MediaWikiServices
::getInstance()->getContentLanguage();
784 foreach ( $contLang->getVariants() as $variant ) {
785 $convertTitle = $contLang->autoConvert( $text, $variant );
786 if ( $ns !== NS_MAIN
) {
787 $convertNs = $contLang->convertNamespace( $ns, $variant );
788 $convertTitle = $convertNs . ':' . $convertTitle;
790 $result[$variant] = $convertTitle;
796 * Get information about watched status and put it in $this->watched
797 * and $this->notificationtimestamps
799 private function getWatchedInfo() {
800 $user = $this->getUser();
802 if ( $user->isAnon() ||
count( $this->everything
) == 0
803 ||
!$user->isAllowed( 'viewmywatchlist' )
809 $this->notificationtimestamps
= [];
811 $store = MediaWikiServices
::getInstance()->getWatchedItemStore();
812 $timestamps = $store->getNotificationTimestampsBatch( $user, $this->everything
);
814 if ( $this->fld_watched
) {
815 foreach ( $timestamps as $namespaceId => $dbKeys ) {
816 $this->watched
[$namespaceId] = array_map(
824 if ( $this->fld_notificationtimestamp
) {
825 $this->notificationtimestamps
= $timestamps;
830 * Get the count of watchers and put it in $this->watchers
832 private function getWatcherInfo() {
833 if ( count( $this->everything
) == 0 ) {
837 $user = $this->getUser();
838 $canUnwatchedpages = $user->isAllowed( 'unwatchedpages' );
839 $unwatchedPageThreshold = $this->getConfig()->get( 'UnwatchedPageThreshold' );
840 if ( !$canUnwatchedpages && !is_int( $unwatchedPageThreshold ) ) {
844 $this->showZeroWatchers
= $canUnwatchedpages;
847 if ( !$canUnwatchedpages ) {
848 $countOptions['minimumWatchers'] = $unwatchedPageThreshold;
851 $this->watchers
= MediaWikiServices
::getInstance()->getWatchedItemStore()->countWatchersMultiple(
858 * Get the count of watchers who have visited recent edits and put it in
859 * $this->visitingwatchers
861 * Based on InfoAction::pageCounts
863 private function getVisitingWatcherInfo() {
864 $config = $this->getConfig();
865 $user = $this->getUser();
866 $db = $this->getDB();
868 $canUnwatchedpages = $user->isAllowed( 'unwatchedpages' );
869 $unwatchedPageThreshold = $this->getConfig()->get( 'UnwatchedPageThreshold' );
870 if ( !$canUnwatchedpages && !is_int( $unwatchedPageThreshold ) ) {
874 $this->showZeroWatchers
= $canUnwatchedpages;
876 $titlesWithThresholds = [];
877 if ( $this->titles
) {
878 $lb = new LinkBatch( $this->titles
);
880 // Fetch last edit timestamps for pages
881 $this->resetQueryParams();
882 $this->addTables( [ 'page', 'revision' ] );
883 $this->addFields( [ 'page_namespace', 'page_title', 'rev_timestamp' ] );
885 'page_latest = rev_id',
886 $lb->constructSet( 'page', $db ),
888 $this->addOption( 'GROUP BY', [ 'page_namespace', 'page_title' ] );
889 $timestampRes = $this->select( __METHOD__
);
891 $age = $config->get( 'WatchersMaxAge' );
893 foreach ( $timestampRes as $row ) {
894 $revTimestamp = wfTimestamp( TS_UNIX
, (int)$row->rev_timestamp
);
895 $timestamps[$row->page_namespace
][$row->page_title
] = $revTimestamp - $age;
897 $titlesWithThresholds = array_map(
898 function ( LinkTarget
$target ) use ( $timestamps ) {
900 $target, $timestamps[$target->getNamespace()][$target->getDBkey()]
907 if ( $this->missing
) {
908 $titlesWithThresholds = array_merge(
909 $titlesWithThresholds,
911 function ( LinkTarget
$target ) {
912 return [ $target, null ];
918 $store = MediaWikiServices
::getInstance()->getWatchedItemStore();
919 $this->visitingwatchers
= $store->countVisitingWatchersMultiple(
920 $titlesWithThresholds,
921 !$canUnwatchedpages ?
$unwatchedPageThreshold : null
925 public function getCacheMode( $params ) {
926 // Other props depend on something about the current user
936 if ( array_diff( (array)$params['prop'], $publicProps ) ) {
940 // testactions also depends on the current user
941 if ( $params['testactions'] ) {
945 if ( !is_null( $params['token'] ) ) {
952 public function getAllowedParams() {
955 ApiBase
::PARAM_ISMULTI
=> true,
956 ApiBase
::PARAM_TYPE
=> [
960 'watchers', # private
961 'visitingwatchers', # private
962 'notificationtimestamp', # private
965 'readable', # private
969 // If you add more properties here, please consider whether they
970 // need to be added to getCacheMode()
972 ApiBase
::PARAM_HELP_MSG_PER_VALUE
=> [],
973 ApiBase
::PARAM_DEPRECATED_VALUES
=> [
974 'readable' => true, // Since 1.32
978 ApiBase
::PARAM_TYPE
=> 'string',
979 ApiBase
::PARAM_ISMULTI
=> true,
981 'testactionsdetail' => [
982 ApiBase
::PARAM_TYPE
=> [ 'boolean', 'full', 'quick' ],
983 ApiBase
::PARAM_DFLT
=> 'boolean',
984 ApiBase
::PARAM_HELP_MSG_PER_VALUE
=> [],
987 ApiBase
::PARAM_DEPRECATED
=> true,
988 ApiBase
::PARAM_ISMULTI
=> true,
989 ApiBase
::PARAM_TYPE
=> array_keys( $this->getTokenFunctions() )
992 ApiBase
::PARAM_HELP_MSG
=> 'api-help-param-continue',
997 protected function getExamplesMessages() {
999 'action=query&prop=info&titles=Main%20Page'
1000 => 'apihelp-query+info-example-simple',
1001 'action=query&prop=info&inprop=protection&titles=Main%20Page'
1002 => 'apihelp-query+info-example-protection',
1006 public function getHelpUrls() {
1007 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Info';