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;
41 private $params, $titles, $missing, $everything;
43 private $pageRestrictions, $pageIsRedir, $pageIsNew, $pageTouched,
44 $pageLatest, $pageLength;
46 private $protections, $restrictionTypes, $watched, $watchers, $visitingwatchers,
47 $notificationtimestamps, $talkids, $subjectids, $displaytitles;
48 private $showZeroWatchers = false;
50 private $tokenFunctions;
52 private $countTestedActions = 0;
54 public function __construct( ApiQuery
$query, $moduleName ) {
55 parent
::__construct( $query, $moduleName, 'in' );
59 * @param ApiPageSet $pageSet
62 public function requestExtraData( $pageSet ) {
63 $pageSet->requestField( 'page_restrictions' );
64 // If the pageset is resolving redirects we won't get page_is_redirect.
65 // But we can't know for sure until the pageset is executed (revids may
66 // turn it off), so request it unconditionally.
67 $pageSet->requestField( 'page_is_redirect' );
68 $pageSet->requestField( 'page_is_new' );
69 $config = $this->getConfig();
70 $pageSet->requestField( 'page_touched' );
71 $pageSet->requestField( 'page_latest' );
72 $pageSet->requestField( 'page_len' );
73 if ( $config->get( 'ContentHandlerUseDB' ) ) {
74 $pageSet->requestField( 'page_content_model' );
76 if ( $config->get( 'PageLanguageUseDB' ) ) {
77 $pageSet->requestField( 'page_lang' );
82 * Get an array mapping token names to their handler functions.
83 * The prototype for a token function is func($pageid, $title)
84 * it should return a token or false (permission denied)
85 * @deprecated since 1.24
86 * @return array Array(tokenname => function)
88 protected function getTokenFunctions() {
89 // Don't call the hooks twice
90 if ( isset( $this->tokenFunctions
) ) {
91 return $this->tokenFunctions
;
94 // If we're in a mode that breaks the same-origin policy, no tokens can
96 if ( $this->lacksSameOriginSecurity() ) {
100 $this->tokenFunctions
= array(
101 'edit' => array( 'ApiQueryInfo', 'getEditToken' ),
102 'delete' => array( 'ApiQueryInfo', 'getDeleteToken' ),
103 'protect' => array( 'ApiQueryInfo', 'getProtectToken' ),
104 'move' => array( 'ApiQueryInfo', 'getMoveToken' ),
105 'block' => array( 'ApiQueryInfo', 'getBlockToken' ),
106 'unblock' => array( 'ApiQueryInfo', 'getUnblockToken' ),
107 'email' => array( 'ApiQueryInfo', 'getEmailToken' ),
108 'import' => array( 'ApiQueryInfo', 'getImportToken' ),
109 'watch' => array( 'ApiQueryInfo', 'getWatchToken' ),
111 Hooks
::run( 'APIQueryInfoTokens', array( &$this->tokenFunctions
) );
113 return $this->tokenFunctions
;
116 static protected $cachedTokens = array();
119 * @deprecated since 1.24
121 public static function resetTokenCache() {
122 ApiQueryInfo
::$cachedTokens = array();
126 * @deprecated since 1.24
128 public static function getEditToken( $pageid, $title ) {
129 // We could check for $title->userCan('edit') here,
130 // but that's too expensive for this purpose
131 // and would break caching
133 if ( !$wgUser->isAllowed( 'edit' ) ) {
137 // The token is always the same, let's exploit that
138 if ( !isset( ApiQueryInfo
::$cachedTokens['edit'] ) ) {
139 ApiQueryInfo
::$cachedTokens['edit'] = $wgUser->getEditToken();
142 return ApiQueryInfo
::$cachedTokens['edit'];
146 * @deprecated since 1.24
148 public static function getDeleteToken( $pageid, $title ) {
150 if ( !$wgUser->isAllowed( 'delete' ) ) {
154 // The token is always the same, let's exploit that
155 if ( !isset( ApiQueryInfo
::$cachedTokens['delete'] ) ) {
156 ApiQueryInfo
::$cachedTokens['delete'] = $wgUser->getEditToken();
159 return ApiQueryInfo
::$cachedTokens['delete'];
163 * @deprecated since 1.24
165 public static function getProtectToken( $pageid, $title ) {
167 if ( !$wgUser->isAllowed( 'protect' ) ) {
171 // The token is always the same, let's exploit that
172 if ( !isset( ApiQueryInfo
::$cachedTokens['protect'] ) ) {
173 ApiQueryInfo
::$cachedTokens['protect'] = $wgUser->getEditToken();
176 return ApiQueryInfo
::$cachedTokens['protect'];
180 * @deprecated since 1.24
182 public static function getMoveToken( $pageid, $title ) {
184 if ( !$wgUser->isAllowed( 'move' ) ) {
188 // The token is always the same, let's exploit that
189 if ( !isset( ApiQueryInfo
::$cachedTokens['move'] ) ) {
190 ApiQueryInfo
::$cachedTokens['move'] = $wgUser->getEditToken();
193 return ApiQueryInfo
::$cachedTokens['move'];
197 * @deprecated since 1.24
199 public static function getBlockToken( $pageid, $title ) {
201 if ( !$wgUser->isAllowed( 'block' ) ) {
205 // The token is always the same, let's exploit that
206 if ( !isset( ApiQueryInfo
::$cachedTokens['block'] ) ) {
207 ApiQueryInfo
::$cachedTokens['block'] = $wgUser->getEditToken();
210 return ApiQueryInfo
::$cachedTokens['block'];
214 * @deprecated since 1.24
216 public static function getUnblockToken( $pageid, $title ) {
217 // Currently, this is exactly the same as the block token
218 return self
::getBlockToken( $pageid, $title );
222 * @deprecated since 1.24
224 public static function getEmailToken( $pageid, $title ) {
226 if ( !$wgUser->canSendEmail() ||
$wgUser->isBlockedFromEmailuser() ) {
230 // The token is always the same, let's exploit that
231 if ( !isset( ApiQueryInfo
::$cachedTokens['email'] ) ) {
232 ApiQueryInfo
::$cachedTokens['email'] = $wgUser->getEditToken();
235 return ApiQueryInfo
::$cachedTokens['email'];
239 * @deprecated since 1.24
241 public static function getImportToken( $pageid, $title ) {
243 if ( !$wgUser->isAllowedAny( 'import', 'importupload' ) ) {
247 // The token is always the same, let's exploit that
248 if ( !isset( ApiQueryInfo
::$cachedTokens['import'] ) ) {
249 ApiQueryInfo
::$cachedTokens['import'] = $wgUser->getEditToken();
252 return ApiQueryInfo
::$cachedTokens['import'];
256 * @deprecated since 1.24
258 public static function getWatchToken( $pageid, $title ) {
260 if ( !$wgUser->isLoggedIn() ) {
264 // The token is always the same, let's exploit that
265 if ( !isset( ApiQueryInfo
::$cachedTokens['watch'] ) ) {
266 ApiQueryInfo
::$cachedTokens['watch'] = $wgUser->getEditToken( 'watch' );
269 return ApiQueryInfo
::$cachedTokens['watch'];
273 * @deprecated since 1.24
275 public static function getOptionsToken( $pageid, $title ) {
277 if ( !$wgUser->isLoggedIn() ) {
281 // The token is always the same, let's exploit that
282 if ( !isset( ApiQueryInfo
::$cachedTokens['options'] ) ) {
283 ApiQueryInfo
::$cachedTokens['options'] = $wgUser->getEditToken();
286 return ApiQueryInfo
::$cachedTokens['options'];
289 public function execute() {
290 $this->params
= $this->extractRequestParams();
291 if ( !is_null( $this->params
['prop'] ) ) {
292 $prop = array_flip( $this->params
['prop'] );
293 $this->fld_protection
= isset( $prop['protection'] );
294 $this->fld_watched
= isset( $prop['watched'] );
295 $this->fld_watchers
= isset( $prop['watchers'] );
296 $this->fld_visitingwatchers
= isset( $prop['visitingwatchers'] );
297 $this->fld_notificationtimestamp
= isset( $prop['notificationtimestamp'] );
298 $this->fld_talkid
= isset( $prop['talkid'] );
299 $this->fld_subjectid
= isset( $prop['subjectid'] );
300 $this->fld_url
= isset( $prop['url'] );
301 $this->fld_readable
= isset( $prop['readable'] );
302 $this->fld_preload
= isset( $prop['preload'] );
303 $this->fld_displaytitle
= isset( $prop['displaytitle'] );
306 $pageSet = $this->getPageSet();
307 $this->titles
= $pageSet->getGoodTitles();
308 $this->missing
= $pageSet->getMissingTitles();
309 $this->everything
= $this->titles +
$this->missing
;
310 $result = $this->getResult();
312 uasort( $this->everything
, array( 'Title', 'compare' ) );
313 if ( !is_null( $this->params
['continue'] ) ) {
314 // Throw away any titles we're gonna skip so they don't
316 $cont = explode( '|', $this->params
['continue'] );
317 $this->dieContinueUsageIf( count( $cont ) != 2 );
318 $conttitle = Title
::makeTitleSafe( $cont[0], $cont[1] );
319 foreach ( $this->everything
as $pageid => $title ) {
320 if ( Title
::compare( $title, $conttitle ) >= 0 ) {
323 unset( $this->titles
[$pageid] );
324 unset( $this->missing
[$pageid] );
325 unset( $this->everything
[$pageid] );
329 $this->pageRestrictions
= $pageSet->getCustomField( 'page_restrictions' );
330 // when resolving redirects, no page will have this field
331 $this->pageIsRedir
= !$pageSet->isResolvingRedirects()
332 ?
$pageSet->getCustomField( 'page_is_redirect' )
334 $this->pageIsNew
= $pageSet->getCustomField( 'page_is_new' );
336 $this->pageTouched
= $pageSet->getCustomField( 'page_touched' );
337 $this->pageLatest
= $pageSet->getCustomField( 'page_latest' );
338 $this->pageLength
= $pageSet->getCustomField( 'page_len' );
340 // Get protection info if requested
341 if ( $this->fld_protection
) {
342 $this->getProtectionInfo();
345 if ( $this->fld_watched ||
$this->fld_notificationtimestamp
) {
346 $this->getWatchedInfo();
349 if ( $this->fld_watchers
) {
350 $this->getWatcherInfo();
353 if ( $this->fld_visitingwatchers
) {
354 $this->getVisitingWatcherInfo();
357 // Run the talkid/subjectid query if requested
358 if ( $this->fld_talkid ||
$this->fld_subjectid
) {
362 if ( $this->fld_displaytitle
) {
363 $this->getDisplayTitle();
366 /** @var $title Title */
367 foreach ( $this->everything
as $pageid => $title ) {
368 $pageInfo = $this->extractPageInfo( $pageid, $title );
369 $fit = $pageInfo !== null && $result->addValue( array(
372 ), $pageid, $pageInfo );
374 $this->setContinueEnumParameter( 'continue',
375 $title->getNamespace() . '|' .
383 * Get a result array with information about a title
384 * @param int $pageid Page ID (negative for missing titles)
385 * @param Title $title
388 private function extractPageInfo( $pageid, $title ) {
390 // $title->exists() needs pageid, which is not set for all title objects
391 $titleExists = $pageid > 0;
392 $ns = $title->getNamespace();
393 $dbkey = $title->getDBkey();
395 $pageInfo['contentmodel'] = $title->getContentModel();
397 $pageLanguage = $title->getPageLanguage();
398 $pageInfo['pagelanguage'] = $pageLanguage->getCode();
399 $pageInfo['pagelanguagehtmlcode'] = $pageLanguage->getHtmlCode();
400 $pageInfo['pagelanguagedir'] = $pageLanguage->getDir();
402 if ( $titleExists ) {
403 $pageInfo['touched'] = wfTimestamp( TS_ISO_8601
, $this->pageTouched
[$pageid] );
404 $pageInfo['lastrevid'] = intval( $this->pageLatest
[$pageid] );
405 $pageInfo['length'] = intval( $this->pageLength
[$pageid] );
407 if ( isset( $this->pageIsRedir
[$pageid] ) && $this->pageIsRedir
[$pageid] ) {
408 $pageInfo['redirect'] = true;
410 if ( $this->pageIsNew
[$pageid] ) {
411 $pageInfo['new'] = true;
415 if ( !is_null( $this->params
['token'] ) ) {
416 $tokenFunctions = $this->getTokenFunctions();
417 $pageInfo['starttimestamp'] = wfTimestamp( TS_ISO_8601
, time() );
418 foreach ( $this->params
['token'] as $t ) {
419 $val = call_user_func( $tokenFunctions[$t], $pageid, $title );
420 if ( $val === false ) {
421 $this->setWarning( "Action '$t' is not allowed for the current user" );
423 $pageInfo[$t . 'token'] = $val;
428 if ( $this->fld_protection
) {
429 $pageInfo['protection'] = array();
430 if ( isset( $this->protections
[$ns][$dbkey] ) ) {
431 $pageInfo['protection'] =
432 $this->protections
[$ns][$dbkey];
434 ApiResult
::setIndexedTagName( $pageInfo['protection'], 'pr' );
436 $pageInfo['restrictiontypes'] = array();
437 if ( isset( $this->restrictionTypes
[$ns][$dbkey] ) ) {
438 $pageInfo['restrictiontypes'] =
439 $this->restrictionTypes
[$ns][$dbkey];
441 ApiResult
::setIndexedTagName( $pageInfo['restrictiontypes'], 'rt' );
444 if ( $this->fld_watched
) {
445 $pageInfo['watched'] = isset( $this->watched
[$ns][$dbkey] );
448 if ( $this->fld_watchers
) {
449 if ( isset( $this->watchers
[$ns][$dbkey] ) ) {
450 $pageInfo['watchers'] = $this->watchers
[$ns][$dbkey];
451 } elseif ( $this->showZeroWatchers
) {
452 $pageInfo['watchers'] = 0;
456 if ( $this->fld_visitingwatchers
) {
457 if ( isset( $this->visitingwatchers
[$ns][$dbkey] ) ) {
458 $pageInfo['visitingwatchers'] = $this->visitingwatchers
[$ns][$dbkey];
459 } elseif ( $this->showZeroWatchers
) {
460 $pageInfo['visitingwatchers'] = 0;
464 if ( $this->fld_notificationtimestamp
) {
465 $pageInfo['notificationtimestamp'] = '';
466 if ( isset( $this->notificationtimestamps
[$ns][$dbkey] ) ) {
467 $pageInfo['notificationtimestamp'] =
468 wfTimestamp( TS_ISO_8601
, $this->notificationtimestamps
[$ns][$dbkey] );
472 if ( $this->fld_talkid
&& isset( $this->talkids
[$ns][$dbkey] ) ) {
473 $pageInfo['talkid'] = $this->talkids
[$ns][$dbkey];
476 if ( $this->fld_subjectid
&& isset( $this->subjectids
[$ns][$dbkey] ) ) {
477 $pageInfo['subjectid'] = $this->subjectids
[$ns][$dbkey];
480 if ( $this->fld_url
) {
481 $pageInfo['fullurl'] = wfExpandUrl( $title->getFullURL(), PROTO_CURRENT
);
482 $pageInfo['editurl'] = wfExpandUrl( $title->getFullURL( 'action=edit' ), PROTO_CURRENT
);
483 $pageInfo['canonicalurl'] = wfExpandUrl( $title->getFullURL(), PROTO_CANONICAL
);
485 if ( $this->fld_readable
) {
486 $pageInfo['readable'] = $title->userCan( 'read', $this->getUser() );
489 if ( $this->fld_preload
) {
490 if ( $titleExists ) {
491 $pageInfo['preload'] = '';
494 Hooks
::run( 'EditFormPreloadText', array( &$text, &$title ) );
496 $pageInfo['preload'] = $text;
500 if ( $this->fld_displaytitle
) {
501 if ( isset( $this->displaytitles
[$pageid] ) ) {
502 $pageInfo['displaytitle'] = $this->displaytitles
[$pageid];
504 $pageInfo['displaytitle'] = $title->getPrefixedText();
508 if ( $this->params
['testactions'] ) {
509 $limit = $this->getMain()->canApiHighLimits() ? self
::LIMIT_SML1
: self
::LIMIT_SML2
;
510 if ( $this->countTestedActions
>= $limit ) {
511 return null; // force a continuation
514 $user = $this->getUser();
515 $pageInfo['actions'] = array();
516 foreach ( $this->params
['testactions'] as $action ) {
517 $this->countTestedActions++
;
518 $pageInfo['actions'][$action] = $title->userCan( $action, $user );
526 * Get information about protections and put it in $protections
528 private function getProtectionInfo() {
530 $this->protections
= array();
531 $db = $this->getDB();
533 // Get normal protections for existing titles
534 if ( count( $this->titles
) ) {
535 $this->resetQueryParams();
536 $this->addTables( 'page_restrictions' );
537 $this->addFields( array( 'pr_page', 'pr_type', 'pr_level',
538 'pr_expiry', 'pr_cascade' ) );
539 $this->addWhereFld( 'pr_page', array_keys( $this->titles
) );
541 $res = $this->select( __METHOD__
);
542 foreach ( $res as $row ) {
543 /** @var $title Title */
544 $title = $this->titles
[$row->pr_page
];
546 'type' => $row->pr_type
,
547 'level' => $row->pr_level
,
548 'expiry' => $wgContLang->formatExpiry( $row->pr_expiry
, TS_ISO_8601
)
550 if ( $row->pr_cascade
) {
551 $a['cascade'] = true;
553 $this->protections
[$title->getNamespace()][$title->getDBkey()][] = $a;
555 // Also check old restrictions
556 foreach ( $this->titles
as $pageId => $title ) {
557 if ( $this->pageRestrictions
[$pageId] ) {
558 $namespace = $title->getNamespace();
559 $dbKey = $title->getDBkey();
560 $restrictions = explode( ':', trim( $this->pageRestrictions
[$pageId] ) );
561 foreach ( $restrictions as $restrict ) {
562 $temp = explode( '=', trim( $restrict ) );
563 if ( count( $temp ) == 1 ) {
564 // old old format should be treated as edit/move restriction
565 $restriction = trim( $temp[0] );
567 if ( $restriction == '' ) {
570 $this->protections
[$namespace][$dbKey][] = array(
572 'level' => $restriction,
573 'expiry' => 'infinity',
575 $this->protections
[$namespace][$dbKey][] = array(
577 'level' => $restriction,
578 'expiry' => 'infinity',
581 $restriction = trim( $temp[1] );
582 if ( $restriction == '' ) {
585 $this->protections
[$namespace][$dbKey][] = array(
587 'level' => $restriction,
588 'expiry' => 'infinity',
596 // Get protections for missing titles
597 if ( count( $this->missing
) ) {
598 $this->resetQueryParams();
599 $lb = new LinkBatch( $this->missing
);
600 $this->addTables( 'protected_titles' );
601 $this->addFields( array( 'pt_title', 'pt_namespace', 'pt_create_perm', 'pt_expiry' ) );
602 $this->addWhere( $lb->constructSet( 'pt', $db ) );
603 $res = $this->select( __METHOD__
);
604 foreach ( $res as $row ) {
605 $this->protections
[$row->pt_namespace
][$row->pt_title
][] = array(
607 'level' => $row->pt_create_perm
,
608 'expiry' => $wgContLang->formatExpiry( $row->pt_expiry
, TS_ISO_8601
)
613 // Separate good and missing titles into files and other pages
614 // and populate $this->restrictionTypes
615 $images = $others = array();
616 foreach ( $this->everything
as $title ) {
617 if ( $title->getNamespace() == NS_FILE
) {
618 $images[] = $title->getDBkey();
622 // Applicable protection types
623 $this->restrictionTypes
[$title->getNamespace()][$title->getDBkey()] =
624 array_values( $title->getRestrictionTypes() );
627 if ( count( $others ) ) {
628 // Non-images: check templatelinks
629 $lb = new LinkBatch( $others );
630 $this->resetQueryParams();
631 $this->addTables( array( 'page_restrictions', 'page', 'templatelinks' ) );
632 $this->addFields( array( 'pr_type', 'pr_level', 'pr_expiry',
633 'page_title', 'page_namespace',
634 'tl_title', 'tl_namespace' ) );
635 $this->addWhere( $lb->constructSet( 'tl', $db ) );
636 $this->addWhere( 'pr_page = page_id' );
637 $this->addWhere( 'pr_page = tl_from' );
638 $this->addWhereFld( 'pr_cascade', 1 );
640 $res = $this->select( __METHOD__
);
641 foreach ( $res as $row ) {
642 $source = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
643 $this->protections
[$row->tl_namespace
][$row->tl_title
][] = array(
644 'type' => $row->pr_type
,
645 'level' => $row->pr_level
,
646 'expiry' => $wgContLang->formatExpiry( $row->pr_expiry
, TS_ISO_8601
),
647 'source' => $source->getPrefixedText()
652 if ( count( $images ) ) {
653 // Images: check imagelinks
654 $this->resetQueryParams();
655 $this->addTables( array( 'page_restrictions', 'page', 'imagelinks' ) );
656 $this->addFields( array( 'pr_type', 'pr_level', 'pr_expiry',
657 'page_title', 'page_namespace', 'il_to' ) );
658 $this->addWhere( 'pr_page = page_id' );
659 $this->addWhere( 'pr_page = il_from' );
660 $this->addWhereFld( 'pr_cascade', 1 );
661 $this->addWhereFld( 'il_to', $images );
663 $res = $this->select( __METHOD__
);
664 foreach ( $res as $row ) {
665 $source = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
666 $this->protections
[NS_FILE
][$row->il_to
][] = array(
667 'type' => $row->pr_type
,
668 'level' => $row->pr_level
,
669 'expiry' => $wgContLang->formatExpiry( $row->pr_expiry
, TS_ISO_8601
),
670 'source' => $source->getPrefixedText()
677 * Get talk page IDs (if requested) and subject page IDs (if requested)
678 * and put them in $talkids and $subjectids
680 private function getTSIDs() {
681 $getTitles = $this->talkids
= $this->subjectids
= array();
684 foreach ( $this->everything
as $t ) {
685 if ( MWNamespace
::isTalk( $t->getNamespace() ) ) {
686 if ( $this->fld_subjectid
) {
687 $getTitles[] = $t->getSubjectPage();
689 } elseif ( $this->fld_talkid
) {
690 $getTitles[] = $t->getTalkPage();
693 if ( !count( $getTitles ) ) {
697 $db = $this->getDB();
699 // Construct a custom WHERE clause that matches
700 // all titles in $getTitles
701 $lb = new LinkBatch( $getTitles );
702 $this->resetQueryParams();
703 $this->addTables( 'page' );
704 $this->addFields( array( 'page_title', 'page_namespace', 'page_id' ) );
705 $this->addWhere( $lb->constructSet( 'page', $db ) );
706 $res = $this->select( __METHOD__
);
707 foreach ( $res as $row ) {
708 if ( MWNamespace
::isTalk( $row->page_namespace
) ) {
709 $this->talkids
[MWNamespace
::getSubject( $row->page_namespace
)][$row->page_title
] =
710 intval( $row->page_id
);
712 $this->subjectids
[MWNamespace
::getTalk( $row->page_namespace
)][$row->page_title
] =
713 intval( $row->page_id
);
718 private function getDisplayTitle() {
719 $this->displaytitles
= array();
721 $pageIds = array_keys( $this->titles
);
723 if ( !count( $pageIds ) ) {
727 $this->resetQueryParams();
728 $this->addTables( 'page_props' );
729 $this->addFields( array( 'pp_page', 'pp_value' ) );
730 $this->addWhereFld( 'pp_page', $pageIds );
731 $this->addWhereFld( 'pp_propname', 'displaytitle' );
732 $res = $this->select( __METHOD__
);
734 foreach ( $res as $row ) {
735 $this->displaytitles
[$row->pp_page
] = $row->pp_value
;
740 * Get information about watched status and put it in $this->watched
741 * and $this->notificationtimestamps
743 private function getWatchedInfo() {
744 $user = $this->getUser();
746 if ( $user->isAnon() ||
count( $this->everything
) == 0
747 ||
!$user->isAllowed( 'viewmywatchlist' )
752 $this->watched
= array();
753 $this->notificationtimestamps
= array();
754 $db = $this->getDB();
756 $lb = new LinkBatch( $this->everything
);
758 $this->resetQueryParams();
759 $this->addTables( array( 'watchlist' ) );
760 $this->addFields( array( 'wl_title', 'wl_namespace' ) );
761 $this->addFieldsIf( 'wl_notificationtimestamp', $this->fld_notificationtimestamp
);
762 $this->addWhere( array(
763 $lb->constructSet( 'wl', $db ),
764 'wl_user' => $user->getId()
767 $res = $this->select( __METHOD__
);
769 foreach ( $res as $row ) {
770 if ( $this->fld_watched
) {
771 $this->watched
[$row->wl_namespace
][$row->wl_title
] = true;
773 if ( $this->fld_notificationtimestamp
) {
774 $this->notificationtimestamps
[$row->wl_namespace
][$row->wl_title
] =
775 $row->wl_notificationtimestamp
;
781 * Get the count of watchers and put it in $this->watchers
783 private function getWatcherInfo() {
784 if ( count( $this->everything
) == 0 ) {
788 $user = $this->getUser();
789 $canUnwatchedpages = $user->isAllowed( 'unwatchedpages' );
790 $unwatchedPageThreshold = $this->getConfig()->get( 'UnwatchedPageThreshold' );
791 if ( !$canUnwatchedpages && !is_int( $unwatchedPageThreshold ) ) {
795 $this->watchers
= array();
796 $this->showZeroWatchers
= $canUnwatchedpages;
797 $db = $this->getDB();
799 $lb = new LinkBatch( $this->everything
);
801 $this->resetQueryParams();
802 $this->addTables( array( 'watchlist' ) );
803 $this->addFields( array( 'wl_title', 'wl_namespace', 'count' => 'COUNT(*)' ) );
804 $this->addWhere( array(
805 $lb->constructSet( 'wl', $db )
807 $this->addOption( 'GROUP BY', array( 'wl_namespace', 'wl_title' ) );
808 if ( !$canUnwatchedpages ) {
809 $this->addOption( 'HAVING', "COUNT(*) >= $unwatchedPageThreshold" );
812 $res = $this->select( __METHOD__
);
814 foreach ( $res as $row ) {
815 $this->watchers
[$row->wl_namespace
][$row->wl_title
] = (int)$row->count
;
820 * Get the count of watchers who have visited recent edits and put it in
821 * $this->visitingwatchers
823 * Based on InfoAction::pageCounts
825 private function getVisitingWatcherInfo() {
826 $config = $this->getConfig();
827 $user = $this->getUser();
828 $db = $this->getDB();
830 $canUnwatchedpages = $user->isAllowed( 'unwatchedpages' );
831 $unwatchedPageThreshold = $this->getConfig()->get( 'UnwatchedPageThreshold' );
832 if ( !$canUnwatchedpages && !is_int( $unwatchedPageThreshold ) ) {
836 $this->showZeroWatchers
= $canUnwatchedpages;
838 // Assemble a WHERE condition to find:
839 // * if the page exists, number of users watching who have
840 // visited the page recently
841 // * if the page doesn't exist, number of users that have
842 // the page on their watchlist
843 $whereStrings = array();
845 // For pages that exist
846 if ( $this->titles
) {
847 $lb = new LinkBatch( $this->titles
);
849 // Fetch last edit timestamps for pages
850 $this->resetQueryParams();
851 $this->addTables( array( 'page', 'revision' ) );
852 $this->addFields( array( 'page_namespace', 'page_title', 'rev_timestamp' ) );
853 $this->addWhere( array(
854 'page_latest = rev_id',
855 $lb->constructSet( 'page', $db ),
857 $this->addOption( 'GROUP BY', array( 'page_namespace', 'page_title' ) );
858 $timestampRes = $this->select( __METHOD__
);
860 // Assemble SQL WHERE condition to find number of page watchers who also
861 // visited a "recent" edit (last visited about 26 weeks before latest edit)
862 $age = $config->get( 'WatchersMaxAge' );
863 $timestamps = array();
864 foreach ( $timestampRes as $row ) {
865 $revTimestamp = wfTimestamp( TS_UNIX
, (int)$row->rev_timestamp
);
866 $threshold = $db->timestamp( $revTimestamp - $age );
867 $timestamps[$row->page_namespace
][$row->page_title
] = $threshold;
870 foreach ( $timestamps as $ns_key => $namespace ) {
871 $pageStrings = array();
872 foreach ( $namespace as $pg_key => $threshold ) {
873 $pageStrings[] = "wl_title = '$pg_key' AND" .
874 ' (wl_notificationtimestamp >= ' .
875 $db->addQuotes( $threshold ) .
876 ' OR wl_notificationtimestamp IS NULL)';
878 $whereStrings[] = "wl_namespace = '$ns_key' AND (" .
879 $db->makeList( $pageStrings, LIST_OR
) . ')';
883 // For nonexistant pages
884 if ( $this->missing
) {
885 $lb = new LinkBatch( $this->missing
);
886 $whereStrings[] = $lb->constructSet( 'wl', $db );
889 // Make the actual string and do the query
890 $whereString = $db->makeList( $whereStrings, LIST_OR
);
892 $this->resetQueryParams();
893 $this->addTables( array( 'watchlist' ) );
894 $this->addFields( array(
897 'count' => 'COUNT(*)'
899 $this->addWhere( array( $whereString ) );
900 $this->addOption( 'GROUP BY', array( 'wl_namespace', 'wl_title' ) );
901 if ( !$canUnwatchedpages ) {
902 $this->addOption( 'HAVING', "COUNT(*) >= $unwatchedPageThreshold" );
905 $res = $this->select( __METHOD__
);
906 foreach ( $res as $row ) {
907 $this->visitingwatchers
[$row->wl_namespace
][$row->wl_title
] = (int)$row->count
;
911 public function getCacheMode( $params ) {
912 // Other props depend on something about the current user
913 $publicProps = array(
921 if ( array_diff( (array)$params['prop'], $publicProps ) ) {
925 // testactions also depends on the current user
926 if ( $params['testactions'] ) {
930 if ( !is_null( $params['token'] ) ) {
937 public function getAllowedParams() {
940 ApiBase
::PARAM_ISMULTI
=> true,
941 ApiBase
::PARAM_TYPE
=> array(
945 'watchers', # private
946 'visitingwatchers', # private
947 'notificationtimestamp', # private
950 'readable', # private
953 // If you add more properties here, please consider whether they
954 // need to be added to getCacheMode()
956 ApiBase
::PARAM_HELP_MSG_PER_VALUE
=> array(),
958 'testactions' => array(
959 ApiBase
::PARAM_TYPE
=> 'string',
960 ApiBase
::PARAM_ISMULTI
=> true,
963 ApiBase
::PARAM_DEPRECATED
=> true,
964 ApiBase
::PARAM_ISMULTI
=> true,
965 ApiBase
::PARAM_TYPE
=> array_keys( $this->getTokenFunctions() )
968 ApiBase
::PARAM_HELP_MSG
=> 'api-help-param-continue',
973 protected function getExamplesMessages() {
975 'action=query&prop=info&titles=Main%20Page'
976 => 'apihelp-query+info-example-simple',
977 'action=query&prop=info&inprop=protection&titles=Main%20Page'
978 => 'apihelp-query+info-example-protection',
982 public function getHelpUrls() {
983 return 'https://www.mediawiki.org/wiki/API:Info';