Whitelist the <wbr> element.
[mediawiki.git] / includes / api / ApiQueryInfo.php
blob017684edd321db35566fc7e5223071e1e4569495
1 <?php
2 /**
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
24 * @file
27 /**
28 * A query module to show basic page information.
30 * @ingroup API
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, $fld_watchers = false,
37 $fld_notificationtimestamp = false,
38 $fld_preload = false, $fld_displaytitle = false;
40 private $params, $titles, $missing, $everything, $pageCounter;
42 private $pageRestrictions, $pageIsRedir, $pageIsNew, $pageTouched,
43 $pageLatest, $pageLength;
45 private $protections, $watched, $watchers, $notificationtimestamps, $talkids, $subjectids, $displaytitles;
46 private $showZeroWatchers = false;
48 private $tokenFunctions;
50 public function __construct( $query, $moduleName ) {
51 parent::__construct( $query, $moduleName, 'in' );
54 /**
55 * @param $pageSet ApiPageSet
56 * @return void
58 public function requestExtraData( $pageSet ) {
59 global $wgDisableCounters, $wgContentHandlerUseDB;
61 $pageSet->requestField( 'page_restrictions' );
62 // when resolving redirects, no page will have this field
63 if ( !$pageSet->isResolvingRedirects() ) {
64 $pageSet->requestField( 'page_is_redirect' );
66 $pageSet->requestField( 'page_is_new' );
67 if ( !$wgDisableCounters ) {
68 $pageSet->requestField( 'page_counter' );
70 $pageSet->requestField( 'page_touched' );
71 $pageSet->requestField( 'page_latest' );
72 $pageSet->requestField( 'page_len' );
73 if ( $wgContentHandlerUseDB ) {
74 $pageSet->requestField( 'page_content_model' );
78 /**
79 * Get an array mapping token names to their handler functions.
80 * The prototype for a token function is func($pageid, $title)
81 * it should return a token or false (permission denied)
82 * @return array array(tokenname => function)
84 protected function getTokenFunctions() {
85 // Don't call the hooks twice
86 if ( isset( $this->tokenFunctions ) ) {
87 return $this->tokenFunctions;
90 // If we're in JSON callback mode, no tokens can be obtained
91 if ( !is_null( $this->getMain()->getRequest()->getVal( 'callback' ) ) ) {
92 return array();
95 $this->tokenFunctions = array(
96 'edit' => array( 'ApiQueryInfo', 'getEditToken' ),
97 'delete' => array( 'ApiQueryInfo', 'getDeleteToken' ),
98 'protect' => array( 'ApiQueryInfo', 'getProtectToken' ),
99 'move' => array( 'ApiQueryInfo', 'getMoveToken' ),
100 'block' => array( 'ApiQueryInfo', 'getBlockToken' ),
101 'unblock' => array( 'ApiQueryInfo', 'getUnblockToken' ),
102 'email' => array( 'ApiQueryInfo', 'getEmailToken' ),
103 'import' => array( 'ApiQueryInfo', 'getImportToken' ),
104 'watch' => array( 'ApiQueryInfo', 'getWatchToken' ),
106 wfRunHooks( 'APIQueryInfoTokens', array( &$this->tokenFunctions ) );
107 return $this->tokenFunctions;
110 static $cachedTokens = array();
112 public static function resetTokenCache() {
113 ApiQueryInfo::$cachedTokens = array();
116 public static function getEditToken( $pageid, $title ) {
117 // We could check for $title->userCan('edit') here,
118 // but that's too expensive for this purpose
119 // and would break caching
120 global $wgUser;
121 if ( !$wgUser->isAllowed( 'edit' ) ) {
122 return false;
125 // The token is always the same, let's exploit that
126 if ( !isset( ApiQueryInfo::$cachedTokens['edit'] ) ) {
127 ApiQueryInfo::$cachedTokens['edit'] = $wgUser->getEditToken();
130 return ApiQueryInfo::$cachedTokens['edit'];
133 public static function getDeleteToken( $pageid, $title ) {
134 global $wgUser;
135 if ( !$wgUser->isAllowed( 'delete' ) ) {
136 return false;
139 // The token is always the same, let's exploit that
140 if ( !isset( ApiQueryInfo::$cachedTokens['delete'] ) ) {
141 ApiQueryInfo::$cachedTokens['delete'] = $wgUser->getEditToken();
144 return ApiQueryInfo::$cachedTokens['delete'];
147 public static function getProtectToken( $pageid, $title ) {
148 global $wgUser;
149 if ( !$wgUser->isAllowed( 'protect' ) ) {
150 return false;
153 // The token is always the same, let's exploit that
154 if ( !isset( ApiQueryInfo::$cachedTokens['protect'] ) ) {
155 ApiQueryInfo::$cachedTokens['protect'] = $wgUser->getEditToken();
158 return ApiQueryInfo::$cachedTokens['protect'];
161 public static function getMoveToken( $pageid, $title ) {
162 global $wgUser;
163 if ( !$wgUser->isAllowed( 'move' ) ) {
164 return false;
167 // The token is always the same, let's exploit that
168 if ( !isset( ApiQueryInfo::$cachedTokens['move'] ) ) {
169 ApiQueryInfo::$cachedTokens['move'] = $wgUser->getEditToken();
172 return ApiQueryInfo::$cachedTokens['move'];
175 public static function getBlockToken( $pageid, $title ) {
176 global $wgUser;
177 if ( !$wgUser->isAllowed( 'block' ) ) {
178 return false;
181 // The token is always the same, let's exploit that
182 if ( !isset( ApiQueryInfo::$cachedTokens['block'] ) ) {
183 ApiQueryInfo::$cachedTokens['block'] = $wgUser->getEditToken();
186 return ApiQueryInfo::$cachedTokens['block'];
189 public static function getUnblockToken( $pageid, $title ) {
190 // Currently, this is exactly the same as the block token
191 return self::getBlockToken( $pageid, $title );
194 public static function getEmailToken( $pageid, $title ) {
195 global $wgUser;
196 if ( !$wgUser->canSendEmail() || $wgUser->isBlockedFromEmailUser() ) {
197 return false;
200 // The token is always the same, let's exploit that
201 if ( !isset( ApiQueryInfo::$cachedTokens['email'] ) ) {
202 ApiQueryInfo::$cachedTokens['email'] = $wgUser->getEditToken();
205 return ApiQueryInfo::$cachedTokens['email'];
208 public static function getImportToken( $pageid, $title ) {
209 global $wgUser;
210 if ( !$wgUser->isAllowedAny( 'import', 'importupload' ) ) {
211 return false;
214 // The token is always the same, let's exploit that
215 if ( !isset( ApiQueryInfo::$cachedTokens['import'] ) ) {
216 ApiQueryInfo::$cachedTokens['import'] = $wgUser->getEditToken();
219 return ApiQueryInfo::$cachedTokens['import'];
222 public static function getWatchToken( $pageid, $title ) {
223 global $wgUser;
224 if ( !$wgUser->isLoggedIn() ) {
225 return false;
228 // The token is always the same, let's exploit that
229 if ( !isset( ApiQueryInfo::$cachedTokens['watch'] ) ) {
230 ApiQueryInfo::$cachedTokens['watch'] = $wgUser->getEditToken( 'watch' );
233 return ApiQueryInfo::$cachedTokens['watch'];
236 public static function getOptionsToken( $pageid, $title ) {
237 global $wgUser;
238 if ( !$wgUser->isLoggedIn() ) {
239 return false;
242 // The token is always the same, let's exploit that
243 if ( !isset( ApiQueryInfo::$cachedTokens['options'] ) ) {
244 ApiQueryInfo::$cachedTokens['options'] = $wgUser->getEditToken();
247 return ApiQueryInfo::$cachedTokens['options'];
250 public function execute() {
251 $this->params = $this->extractRequestParams();
252 if ( !is_null( $this->params['prop'] ) ) {
253 $prop = array_flip( $this->params['prop'] );
254 $this->fld_protection = isset( $prop['protection'] );
255 $this->fld_watched = isset( $prop['watched'] );
256 $this->fld_watchers = isset( $prop['watchers'] );
257 $this->fld_notificationtimestamp = isset( $prop['notificationtimestamp'] );
258 $this->fld_talkid = isset( $prop['talkid'] );
259 $this->fld_subjectid = isset( $prop['subjectid'] );
260 $this->fld_url = isset( $prop['url'] );
261 $this->fld_readable = isset( $prop['readable'] );
262 $this->fld_preload = isset( $prop['preload'] );
263 $this->fld_displaytitle = isset( $prop['displaytitle'] );
266 $pageSet = $this->getPageSet();
267 $this->titles = $pageSet->getGoodTitles();
268 $this->missing = $pageSet->getMissingTitles();
269 $this->everything = $this->titles + $this->missing;
270 $result = $this->getResult();
272 uasort( $this->everything, array( 'Title', 'compare' ) );
273 if ( !is_null( $this->params['continue'] ) ) {
274 // Throw away any titles we're gonna skip so they don't
275 // clutter queries
276 $cont = explode( '|', $this->params['continue'] );
277 $this->dieContinueUsageIf( count( $cont ) != 2 );
278 $conttitle = Title::makeTitleSafe( $cont[0], $cont[1] );
279 foreach ( $this->everything as $pageid => $title ) {
280 if ( Title::compare( $title, $conttitle ) >= 0 ) {
281 break;
283 unset( $this->titles[$pageid] );
284 unset( $this->missing[$pageid] );
285 unset( $this->everything[$pageid] );
289 $this->pageRestrictions = $pageSet->getCustomField( 'page_restrictions' );
290 // when resolving redirects, no page will have this field
291 $this->pageIsRedir = !$pageSet->isResolvingRedirects()
292 ? $pageSet->getCustomField( 'page_is_redirect' )
293 : array();
294 $this->pageIsNew = $pageSet->getCustomField( 'page_is_new' );
296 global $wgDisableCounters;
298 if ( !$wgDisableCounters ) {
299 $this->pageCounter = $pageSet->getCustomField( 'page_counter' );
301 $this->pageTouched = $pageSet->getCustomField( 'page_touched' );
302 $this->pageLatest = $pageSet->getCustomField( 'page_latest' );
303 $this->pageLength = $pageSet->getCustomField( 'page_len' );
305 // Get protection info if requested
306 if ( $this->fld_protection ) {
307 $this->getProtectionInfo();
310 if ( $this->fld_watched || $this->fld_notificationtimestamp ) {
311 $this->getWatchedInfo();
314 if ( $this->fld_watchers ) {
315 $this->getWatcherInfo();
318 // Run the talkid/subjectid query if requested
319 if ( $this->fld_talkid || $this->fld_subjectid ) {
320 $this->getTSIDs();
323 if ( $this->fld_displaytitle ) {
324 $this->getDisplayTitle();
327 /** @var $title Title */
328 foreach ( $this->everything as $pageid => $title ) {
329 $pageInfo = $this->extractPageInfo( $pageid, $title );
330 $fit = $result->addValue( array(
331 'query',
332 'pages'
333 ), $pageid, $pageInfo );
334 if ( !$fit ) {
335 $this->setContinueEnumParameter( 'continue',
336 $title->getNamespace() . '|' .
337 $title->getText() );
338 break;
344 * Get a result array with information about a title
345 * @param int $pageid Page ID (negative for missing titles)
346 * @param $title Title object
347 * @return array
349 private function extractPageInfo( $pageid, $title ) {
350 $pageInfo = array();
351 $titleExists = $pageid > 0; //$title->exists() needs pageid, which is not set for all title objects
352 $ns = $title->getNamespace();
353 $dbkey = $title->getDBkey();
355 $pageInfo['contentmodel'] = $title->getContentModel();
356 $pageInfo['pagelanguage'] = $title->getPageLanguage()->getCode();
358 if ( $titleExists ) {
359 global $wgDisableCounters;
361 $pageInfo['touched'] = wfTimestamp( TS_ISO_8601, $this->pageTouched[$pageid] );
362 $pageInfo['lastrevid'] = intval( $this->pageLatest[$pageid] );
363 $pageInfo['counter'] = $wgDisableCounters
364 ? ''
365 : intval( $this->pageCounter[$pageid] );
366 $pageInfo['length'] = intval( $this->pageLength[$pageid] );
368 if ( isset( $this->pageIsRedir[$pageid] ) && $this->pageIsRedir[$pageid] ) {
369 $pageInfo['redirect'] = '';
371 if ( $this->pageIsNew[$pageid] ) {
372 $pageInfo['new'] = '';
376 if ( !is_null( $this->params['token'] ) ) {
377 $tokenFunctions = $this->getTokenFunctions();
378 $pageInfo['starttimestamp'] = wfTimestamp( TS_ISO_8601, time() );
379 foreach ( $this->params['token'] as $t ) {
380 $val = call_user_func( $tokenFunctions[$t], $pageid, $title );
381 if ( $val === false ) {
382 $this->setWarning( "Action '$t' is not allowed for the current user" );
383 } else {
384 $pageInfo[$t . 'token'] = $val;
389 if ( $this->fld_protection ) {
390 $pageInfo['protection'] = array();
391 if ( isset( $this->protections[$ns][$dbkey] ) ) {
392 $pageInfo['protection'] =
393 $this->protections[$ns][$dbkey];
395 $this->getResult()->setIndexedTagName( $pageInfo['protection'], 'pr' );
398 if ( $this->fld_watched && isset( $this->watched[$ns][$dbkey] ) ) {
399 $pageInfo['watched'] = '';
402 if ( $this->fld_watchers ) {
403 if ( isset( $this->watchers[$ns][$dbkey] ) ) {
404 $pageInfo['watchers'] = $this->watchers[$ns][$dbkey];
405 } elseif ( $this->showZeroWatchers ) {
406 $pageInfo['watchers'] = 0;
410 if ( $this->fld_notificationtimestamp ) {
411 $pageInfo['notificationtimestamp'] = '';
412 if ( isset( $this->notificationtimestamps[$ns][$dbkey] ) ) {
413 $pageInfo['notificationtimestamp'] = wfTimestamp( TS_ISO_8601, $this->notificationtimestamps[$ns][$dbkey] );
417 if ( $this->fld_talkid && isset( $this->talkids[$ns][$dbkey] ) ) {
418 $pageInfo['talkid'] = $this->talkids[$ns][$dbkey];
421 if ( $this->fld_subjectid && isset( $this->subjectids[$ns][$dbkey] ) ) {
422 $pageInfo['subjectid'] = $this->subjectids[$ns][$dbkey];
425 if ( $this->fld_url ) {
426 $pageInfo['fullurl'] = wfExpandUrl( $title->getFullURL(), PROTO_CURRENT );
427 $pageInfo['editurl'] = wfExpandUrl( $title->getFullURL( 'action=edit' ), PROTO_CURRENT );
429 if ( $this->fld_readable && $title->userCan( 'read', $this->getUser() ) ) {
430 $pageInfo['readable'] = '';
433 if ( $this->fld_preload ) {
434 if ( $titleExists ) {
435 $pageInfo['preload'] = '';
436 } else {
437 $text = null;
438 wfRunHooks( 'EditFormPreloadText', array( &$text, &$title ) );
440 $pageInfo['preload'] = $text;
444 if ( $this->fld_displaytitle ) {
445 if ( isset( $this->displaytitles[$pageid] ) ) {
446 $pageInfo['displaytitle'] = $this->displaytitles[$pageid];
447 } else {
448 $pageInfo['displaytitle'] = $title->getPrefixedText();
452 return $pageInfo;
456 * Get information about protections and put it in $protections
458 private function getProtectionInfo() {
459 global $wgContLang;
460 $this->protections = array();
461 $db = $this->getDB();
463 // Get normal protections for existing titles
464 if ( count( $this->titles ) ) {
465 $this->resetQueryParams();
466 $this->addTables( 'page_restrictions' );
467 $this->addFields( array( 'pr_page', 'pr_type', 'pr_level',
468 'pr_expiry', 'pr_cascade' ) );
469 $this->addWhereFld( 'pr_page', array_keys( $this->titles ) );
471 $res = $this->select( __METHOD__ );
472 foreach ( $res as $row ) {
473 /** @var $title Title */
474 $title = $this->titles[$row->pr_page];
475 $a = array(
476 'type' => $row->pr_type,
477 'level' => $row->pr_level,
478 'expiry' => $wgContLang->formatExpiry( $row->pr_expiry, TS_ISO_8601 )
480 if ( $row->pr_cascade ) {
481 $a['cascade'] = '';
483 $this->protections[$title->getNamespace()][$title->getDBkey()][] = $a;
485 // Also check old restrictions
486 foreach ( $this->titles as $pageId => $title ) {
487 if ( $this->pageRestrictions[$pageId] ) {
488 $namespace = $title->getNamespace();
489 $dbKey = $title->getDBkey();
490 $restrictions = explode( ':', trim( $this->pageRestrictions[$pageId] ) );
491 foreach ( $restrictions as $restrict ) {
492 $temp = explode( '=', trim( $restrict ) );
493 if ( count( $temp ) == 1 ) {
494 // old old format should be treated as edit/move restriction
495 $restriction = trim( $temp[0] );
497 if ( $restriction == '' ) {
498 continue;
500 $this->protections[$namespace][$dbKey][] = array(
501 'type' => 'edit',
502 'level' => $restriction,
503 'expiry' => 'infinity',
505 $this->protections[$namespace][$dbKey][] = array(
506 'type' => 'move',
507 'level' => $restriction,
508 'expiry' => 'infinity',
510 } else {
511 $restriction = trim( $temp[1] );
512 if ( $restriction == '' ) {
513 continue;
515 $this->protections[$namespace][$dbKey][] = array(
516 'type' => $temp[0],
517 'level' => $restriction,
518 'expiry' => 'infinity',
526 // Get protections for missing titles
527 if ( count( $this->missing ) ) {
528 $this->resetQueryParams();
529 $lb = new LinkBatch( $this->missing );
530 $this->addTables( 'protected_titles' );
531 $this->addFields( array( 'pt_title', 'pt_namespace', 'pt_create_perm', 'pt_expiry' ) );
532 $this->addWhere( $lb->constructSet( 'pt', $db ) );
533 $res = $this->select( __METHOD__ );
534 foreach ( $res as $row ) {
535 $this->protections[$row->pt_namespace][$row->pt_title][] = array(
536 'type' => 'create',
537 'level' => $row->pt_create_perm,
538 'expiry' => $wgContLang->formatExpiry( $row->pt_expiry, TS_ISO_8601 )
543 // Cascading protections
544 $images = $others = array();
545 foreach ( $this->everything as $title ) {
546 if ( $title->getNamespace() == NS_FILE ) {
547 $images[] = $title->getDBkey();
548 } else {
549 $others[] = $title;
553 if ( count( $others ) ) {
554 // Non-images: check templatelinks
555 $lb = new LinkBatch( $others );
556 $this->resetQueryParams();
557 $this->addTables( array( 'page_restrictions', 'page', 'templatelinks' ) );
558 $this->addFields( array( 'pr_type', 'pr_level', 'pr_expiry',
559 'page_title', 'page_namespace',
560 'tl_title', 'tl_namespace' ) );
561 $this->addWhere( $lb->constructSet( 'tl', $db ) );
562 $this->addWhere( 'pr_page = page_id' );
563 $this->addWhere( 'pr_page = tl_from' );
564 $this->addWhereFld( 'pr_cascade', 1 );
566 $res = $this->select( __METHOD__ );
567 foreach ( $res as $row ) {
568 $source = Title::makeTitle( $row->page_namespace, $row->page_title );
569 $this->protections[$row->tl_namespace][$row->tl_title][] = array(
570 'type' => $row->pr_type,
571 'level' => $row->pr_level,
572 'expiry' => $wgContLang->formatExpiry( $row->pr_expiry, TS_ISO_8601 ),
573 'source' => $source->getPrefixedText()
578 if ( count( $images ) ) {
579 // Images: check imagelinks
580 $this->resetQueryParams();
581 $this->addTables( array( 'page_restrictions', 'page', 'imagelinks' ) );
582 $this->addFields( array( 'pr_type', 'pr_level', 'pr_expiry',
583 'page_title', 'page_namespace', 'il_to' ) );
584 $this->addWhere( 'pr_page = page_id' );
585 $this->addWhere( 'pr_page = il_from' );
586 $this->addWhereFld( 'pr_cascade', 1 );
587 $this->addWhereFld( 'il_to', $images );
589 $res = $this->select( __METHOD__ );
590 foreach ( $res as $row ) {
591 $source = Title::makeTitle( $row->page_namespace, $row->page_title );
592 $this->protections[NS_FILE][$row->il_to][] = array(
593 'type' => $row->pr_type,
594 'level' => $row->pr_level,
595 'expiry' => $wgContLang->formatExpiry( $row->pr_expiry, TS_ISO_8601 ),
596 'source' => $source->getPrefixedText()
603 * Get talk page IDs (if requested) and subject page IDs (if requested)
604 * and put them in $talkids and $subjectids
606 private function getTSIDs() {
607 $getTitles = $this->talkids = $this->subjectids = array();
609 /** @var $t Title */
610 foreach ( $this->everything as $t ) {
611 if ( MWNamespace::isTalk( $t->getNamespace() ) ) {
612 if ( $this->fld_subjectid ) {
613 $getTitles[] = $t->getSubjectPage();
615 } elseif ( $this->fld_talkid ) {
616 $getTitles[] = $t->getTalkPage();
619 if ( !count( $getTitles ) ) {
620 return;
623 $db = $this->getDB();
625 // Construct a custom WHERE clause that matches
626 // all titles in $getTitles
627 $lb = new LinkBatch( $getTitles );
628 $this->resetQueryParams();
629 $this->addTables( 'page' );
630 $this->addFields( array( 'page_title', 'page_namespace', 'page_id' ) );
631 $this->addWhere( $lb->constructSet( 'page', $db ) );
632 $res = $this->select( __METHOD__ );
633 foreach ( $res as $row ) {
634 if ( MWNamespace::isTalk( $row->page_namespace ) ) {
635 $this->talkids[MWNamespace::getSubject( $row->page_namespace )][$row->page_title] =
636 intval( $row->page_id );
637 } else {
638 $this->subjectids[MWNamespace::getTalk( $row->page_namespace )][$row->page_title] =
639 intval( $row->page_id );
644 private function getDisplayTitle() {
645 $this->displaytitles = array();
647 $pageIds = array_keys( $this->titles );
649 if ( !count( $pageIds ) ) {
650 return;
653 $this->resetQueryParams();
654 $this->addTables( 'page_props' );
655 $this->addFields( array( 'pp_page', 'pp_value' ) );
656 $this->addWhereFld( 'pp_page', $pageIds );
657 $this->addWhereFld( 'pp_propname', 'displaytitle' );
658 $res = $this->select( __METHOD__ );
660 foreach ( $res as $row ) {
661 $this->displaytitles[$row->pp_page] = $row->pp_value;
666 * Get information about watched status and put it in $this->watched
667 * and $this->notificationtimestamps
669 private function getWatchedInfo() {
670 $user = $this->getUser();
672 if ( $user->isAnon() || count( $this->everything ) == 0
673 || !$user->isAllowed( 'viewmywatchlist' )
675 return;
678 $this->watched = array();
679 $this->notificationtimestamps = array();
680 $db = $this->getDB();
682 $lb = new LinkBatch( $this->everything );
684 $this->resetQueryParams();
685 $this->addTables( array( 'watchlist' ) );
686 $this->addFields( array( 'wl_title', 'wl_namespace' ) );
687 $this->addFieldsIf( 'wl_notificationtimestamp', $this->fld_notificationtimestamp );
688 $this->addWhere( array(
689 $lb->constructSet( 'wl', $db ),
690 'wl_user' => $user->getID()
691 ) );
693 $res = $this->select( __METHOD__ );
695 foreach ( $res as $row ) {
696 if ( $this->fld_watched ) {
697 $this->watched[$row->wl_namespace][$row->wl_title] = true;
699 if ( $this->fld_notificationtimestamp ) {
700 $this->notificationtimestamps[$row->wl_namespace][$row->wl_title] = $row->wl_notificationtimestamp;
706 * Get the count of watchers and put it in $this->watchers
708 private function getWatcherInfo() {
709 global $wgUnwatchedPageThreshold;
711 if ( count( $this->everything ) == 0 ) {
712 return;
715 $user = $this->getUser();
716 $canUnwatchedpages = $user->isAllowed( 'unwatchedpages' );
717 if ( !$canUnwatchedpages && !is_int( $wgUnwatchedPageThreshold ) ) {
718 return;
721 $this->watchers = array();
722 $this->showZeroWatchers = $canUnwatchedpages;
723 $db = $this->getDB();
725 $lb = new LinkBatch( $this->everything );
727 $this->resetQueryParams();
728 $this->addTables( array( 'watchlist' ) );
729 $this->addFields( array( 'wl_title', 'wl_namespace', 'count' => 'COUNT(*)' ) );
730 $this->addWhere( array(
731 $lb->constructSet( 'wl', $db )
732 ) );
733 $this->addOption( 'GROUP BY', array( 'wl_namespace', 'wl_title' ) );
734 if ( !$canUnwatchedpages ) {
735 $this->addOption( 'HAVING', "COUNT(*) >= $wgUnwatchedPageThreshold" );
738 $res = $this->select( __METHOD__ );
740 foreach ( $res as $row ) {
741 $this->watchers[$row->wl_namespace][$row->wl_title] = (int)$row->count;
745 public function getCacheMode( $params ) {
746 $publicProps = array(
747 'protection',
748 'talkid',
749 'subjectid',
750 'url',
751 'preload',
752 'displaytitle',
754 if ( !is_null( $params['prop'] ) ) {
755 foreach ( $params['prop'] as $prop ) {
756 if ( !in_array( $prop, $publicProps ) ) {
757 return 'private';
761 if ( !is_null( $params['token'] ) ) {
762 return 'private';
764 return 'public';
767 public function getAllowedParams() {
768 return array(
769 'prop' => array(
770 ApiBase::PARAM_DFLT => null,
771 ApiBase::PARAM_ISMULTI => true,
772 ApiBase::PARAM_TYPE => array(
773 'protection',
774 'talkid',
775 'watched', # private
776 'watchers', # private
777 'notificationtimestamp', # private
778 'subjectid',
779 'url',
780 'readable', # private
781 'preload',
782 'displaytitle',
783 // If you add more properties here, please consider whether they
784 // need to be added to getCacheMode()
785 ) ),
786 'token' => array(
787 ApiBase::PARAM_DFLT => null,
788 ApiBase::PARAM_ISMULTI => true,
789 ApiBase::PARAM_TYPE => array_keys( $this->getTokenFunctions() )
791 'continue' => null,
795 public function getParamDescription() {
796 return array(
797 'prop' => array(
798 'Which additional properties to get:',
799 ' protection - List the protection level of each page',
800 ' talkid - The page ID of the talk page for each non-talk page',
801 ' watched - List the watched status of each page',
802 ' watchers - The number of watchers, if allowed',
803 ' notificationtimestamp - The watchlist notification timestamp of each page',
804 ' subjectid - The page ID of the parent page for each talk page',
805 ' url - Gives a full URL to the page, and also an edit URL',
806 ' readable - Whether the user can read this page',
807 ' preload - Gives the text returned by EditFormPreloadText',
808 ' displaytitle - Gives the way the page title is actually displayed',
810 'token' => 'Request a token to perform a data-modifying action on a page',
811 'continue' => 'When more results are available, use this to continue',
815 public function getResultProperties() {
816 $props = array(
817 ApiBase::PROP_LIST => false,
818 '' => array(
819 'touched' => 'timestamp',
820 'lastrevid' => 'integer',
821 'counter' => array(
822 ApiBase::PROP_TYPE => 'integer',
823 ApiBase::PROP_NULLABLE => true
825 'length' => 'integer',
826 'redirect' => 'boolean',
827 'new' => 'boolean',
828 'starttimestamp' => array(
829 ApiBase::PROP_TYPE => 'timestamp',
830 ApiBase::PROP_NULLABLE => true
832 'contentmodel' => 'string',
834 'watched' => array(
835 'watched' => 'boolean'
837 'watchers' => array(
838 'watchers' => array(
839 ApiBase::PROP_TYPE => 'integer',
840 ApiBase::PROP_NULLABLE => true
843 'notificationtimestamp' => array(
844 'notificationtimestamp' => array(
845 ApiBase::PROP_TYPE => 'timestamp',
846 ApiBase::PROP_NULLABLE => true
849 'talkid' => array(
850 'talkid' => array(
851 ApiBase::PROP_TYPE => 'integer',
852 ApiBase::PROP_NULLABLE => true
855 'subjectid' => array(
856 'subjectid' => array(
857 ApiBase::PROP_TYPE => 'integer',
858 ApiBase::PROP_NULLABLE => true
861 'url' => array(
862 'fullurl' => 'string',
863 'editurl' => 'string'
865 'readable' => array(
866 'readable' => 'boolean'
868 'preload' => array(
869 'preload' => 'string'
871 'displaytitle' => array(
872 'displaytitle' => 'string'
876 self::addTokenProperties( $props, $this->getTokenFunctions() );
878 return $props;
881 public function getDescription() {
882 return 'Get basic page information such as namespace, title, last touched date, ...';
885 public function getExamples() {
886 return array(
887 'api.php?action=query&prop=info&titles=Main%20Page',
888 'api.php?action=query&prop=info&inprop=protection&titles=Main%20Page'
892 public function getHelpUrls() {
893 return 'https://www.mediawiki.org/wiki/API:Properties#info_.2F_in';