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_preload = false, $fld_displaytitle = false;
39 private $params, $titles, $missing, $everything, $pageCounter;
41 private $pageRestrictions, $pageIsRedir, $pageIsNew, $pageTouched,
42 $pageLatest, $pageLength;
44 private $protections, $watched, $talkids, $subjectids, $displaytitles;
46 private $tokenFunctions;
48 public function __construct( $query, $moduleName ) {
49 parent
::__construct( $query, $moduleName, 'in' );
53 * @param $pageSet ApiPageSet
55 public function requestExtraData( $pageSet ) {
56 global $wgDisableCounters;
58 $pageSet->requestField( 'page_restrictions' );
59 $pageSet->requestField( 'page_is_redirect' );
60 $pageSet->requestField( 'page_is_new' );
61 if ( !$wgDisableCounters ) {
62 $pageSet->requestField( 'page_counter' );
64 $pageSet->requestField( 'page_touched' );
65 $pageSet->requestField( 'page_latest' );
66 $pageSet->requestField( 'page_len' );
70 * Get an array mapping token names to their handler functions.
71 * The prototype for a token function is func($pageid, $title)
72 * it should return a token or false (permission denied)
73 * @return array array(tokenname => function)
75 protected function getTokenFunctions() {
76 // Don't call the hooks twice
77 if ( isset( $this->tokenFunctions
) ) {
78 return $this->tokenFunctions
;
81 // If we're in JSON callback mode, no tokens can be obtained
82 if ( !is_null( $this->getMain()->getRequest()->getVal( 'callback' ) ) ) {
86 $this->tokenFunctions
= array(
87 'edit' => array( 'ApiQueryInfo', 'getEditToken' ),
88 'delete' => array( 'ApiQueryInfo', 'getDeleteToken' ),
89 'protect' => array( 'ApiQueryInfo', 'getProtectToken' ),
90 'move' => array( 'ApiQueryInfo', 'getMoveToken' ),
91 'block' => array( 'ApiQueryInfo', 'getBlockToken' ),
92 'unblock' => array( 'ApiQueryInfo', 'getUnblockToken' ),
93 'email' => array( 'ApiQueryInfo', 'getEmailToken' ),
94 'import' => array( 'ApiQueryInfo', 'getImportToken' ),
95 'watch' => array( 'ApiQueryInfo', 'getWatchToken'),
97 wfRunHooks( 'APIQueryInfoTokens', array( &$this->tokenFunctions
) );
98 return $this->tokenFunctions
;
101 public static function getEditToken( $pageid, $title ) {
102 // We could check for $title->userCan('edit') here,
103 // but that's too expensive for this purpose
104 // and would break caching
106 if ( !$wgUser->isAllowed( 'edit' ) ) {
110 // The edit token is always the same, let's exploit that
111 static $cachedEditToken = null;
112 if ( !is_null( $cachedEditToken ) ) {
113 return $cachedEditToken;
116 $cachedEditToken = $wgUser->getEditToken();
117 return $cachedEditToken;
120 public static function getDeleteToken( $pageid, $title ) {
122 if ( !$wgUser->isAllowed( 'delete' ) ) {
126 static $cachedDeleteToken = null;
127 if ( !is_null( $cachedDeleteToken ) ) {
128 return $cachedDeleteToken;
131 $cachedDeleteToken = $wgUser->getEditToken();
132 return $cachedDeleteToken;
135 public static function getProtectToken( $pageid, $title ) {
137 if ( !$wgUser->isAllowed( 'protect' ) ) {
141 static $cachedProtectToken = null;
142 if ( !is_null( $cachedProtectToken ) ) {
143 return $cachedProtectToken;
146 $cachedProtectToken = $wgUser->getEditToken();
147 return $cachedProtectToken;
150 public static function getMoveToken( $pageid, $title ) {
152 if ( !$wgUser->isAllowed( 'move' ) ) {
156 static $cachedMoveToken = null;
157 if ( !is_null( $cachedMoveToken ) ) {
158 return $cachedMoveToken;
161 $cachedMoveToken = $wgUser->getEditToken();
162 return $cachedMoveToken;
165 public static function getBlockToken( $pageid, $title ) {
167 if ( !$wgUser->isAllowed( 'block' ) ) {
171 static $cachedBlockToken = null;
172 if ( !is_null( $cachedBlockToken ) ) {
173 return $cachedBlockToken;
176 $cachedBlockToken = $wgUser->getEditToken();
177 return $cachedBlockToken;
180 public static function getUnblockToken( $pageid, $title ) {
181 // Currently, this is exactly the same as the block token
182 return self
::getBlockToken( $pageid, $title );
185 public static function getEmailToken( $pageid, $title ) {
187 if ( !$wgUser->canSendEmail() ||
$wgUser->isBlockedFromEmailUser() ) {
191 static $cachedEmailToken = null;
192 if ( !is_null( $cachedEmailToken ) ) {
193 return $cachedEmailToken;
196 $cachedEmailToken = $wgUser->getEditToken();
197 return $cachedEmailToken;
200 public static function getImportToken( $pageid, $title ) {
202 if ( !$wgUser->isAllowedAny( 'import', 'importupload' ) ) {
206 static $cachedImportToken = null;
207 if ( !is_null( $cachedImportToken ) ) {
208 return $cachedImportToken;
211 $cachedImportToken = $wgUser->getEditToken();
212 return $cachedImportToken;
215 public static function getWatchToken( $pageid, $title ) {
217 if ( !$wgUser->isLoggedIn() ) {
221 static $cachedWatchToken = null;
222 if ( !is_null( $cachedWatchToken ) ) {
223 return $cachedWatchToken;
226 $cachedWatchToken = $wgUser->getEditToken( 'watch' );
227 return $cachedWatchToken;
230 public function execute() {
231 $this->params
= $this->extractRequestParams();
232 if ( !is_null( $this->params
['prop'] ) ) {
233 $prop = array_flip( $this->params
['prop'] );
234 $this->fld_protection
= isset( $prop['protection'] );
235 $this->fld_watched
= isset( $prop['watched'] );
236 $this->fld_talkid
= isset( $prop['talkid'] );
237 $this->fld_subjectid
= isset( $prop['subjectid'] );
238 $this->fld_url
= isset( $prop['url'] );
239 $this->fld_readable
= isset( $prop['readable'] );
240 $this->fld_preload
= isset( $prop['preload'] );
241 $this->fld_displaytitle
= isset( $prop['displaytitle'] );
244 $pageSet = $this->getPageSet();
245 $this->titles
= $pageSet->getGoodTitles();
246 $this->missing
= $pageSet->getMissingTitles();
247 $this->everything
= $this->titles +
$this->missing
;
248 $result = $this->getResult();
250 uasort( $this->everything
, array( 'Title', 'compare' ) );
251 if ( !is_null( $this->params
['continue'] ) ) {
252 // Throw away any titles we're gonna skip so they don't
254 $cont = explode( '|', $this->params
['continue'] );
255 if ( count( $cont ) != 2 ) {
256 $this->dieUsage( 'Invalid continue param. You should pass the original ' .
257 'value returned by the previous query', '_badcontinue' );
259 $conttitle = Title
::makeTitleSafe( $cont[0], $cont[1] );
260 foreach ( $this->everything
as $pageid => $title ) {
261 if ( Title
::compare( $title, $conttitle ) >= 0 ) {
264 unset( $this->titles
[$pageid] );
265 unset( $this->missing
[$pageid] );
266 unset( $this->everything
[$pageid] );
270 $this->pageRestrictions
= $pageSet->getCustomField( 'page_restrictions' );
271 $this->pageIsRedir
= $pageSet->getCustomField( 'page_is_redirect' );
272 $this->pageIsNew
= $pageSet->getCustomField( 'page_is_new' );
274 global $wgDisableCounters;
276 if ( !$wgDisableCounters ) {
277 $this->pageCounter
= $pageSet->getCustomField( 'page_counter' );
279 $this->pageTouched
= $pageSet->getCustomField( 'page_touched' );
280 $this->pageLatest
= $pageSet->getCustomField( 'page_latest' );
281 $this->pageLength
= $pageSet->getCustomField( 'page_len' );
283 // Get protection info if requested
284 if ( $this->fld_protection
) {
285 $this->getProtectionInfo();
288 if ( $this->fld_watched
) {
289 $this->getWatchedInfo();
292 // Run the talkid/subjectid query if requested
293 if ( $this->fld_talkid ||
$this->fld_subjectid
) {
297 if ( $this->fld_displaytitle
) {
298 $this->getDisplayTitle();
301 foreach ( $this->everything
as $pageid => $title ) {
302 $pageInfo = $this->extractPageInfo( $pageid, $title );
303 $fit = $result->addValue( array(
306 ), $pageid, $pageInfo );
308 $this->setContinueEnumParameter( 'continue',
309 $title->getNamespace() . '|' .
317 * Get a result array with information about a title
318 * @param $pageid int Page ID (negative for missing titles)
319 * @param $title Title object
322 private function extractPageInfo( $pageid, $title ) {
324 if ( $title->exists() ) {
325 global $wgDisableCounters;
327 $pageInfo['touched'] = wfTimestamp( TS_ISO_8601
, $this->pageTouched
[$pageid] );
328 $pageInfo['lastrevid'] = intval( $this->pageLatest
[$pageid] );
329 $pageInfo['counter'] = $wgDisableCounters
331 : intval( $this->pageCounter
[$pageid] );
332 $pageInfo['length'] = intval( $this->pageLength
[$pageid] );
334 if ( $this->pageIsRedir
[$pageid] ) {
335 $pageInfo['redirect'] = '';
337 if ( $this->pageIsNew
[$pageid] ) {
338 $pageInfo['new'] = '';
342 if ( !is_null( $this->params
['token'] ) ) {
343 $tokenFunctions = $this->getTokenFunctions();
344 $pageInfo['starttimestamp'] = wfTimestamp( TS_ISO_8601
, time() );
345 foreach ( $this->params
['token'] as $t ) {
346 $val = call_user_func( $tokenFunctions[$t], $pageid, $title );
347 if ( $val === false ) {
348 $this->setWarning( "Action '$t' is not allowed for the current user" );
350 $pageInfo[$t . 'token'] = $val;
355 if ( $this->fld_protection
) {
356 $pageInfo['protection'] = array();
357 if ( isset( $this->protections
[$title->getNamespace()][$title->getDBkey()] ) ) {
358 $pageInfo['protection'] =
359 $this->protections
[$title->getNamespace()][$title->getDBkey()];
361 $this->getResult()->setIndexedTagName( $pageInfo['protection'], 'pr' );
364 if ( $this->fld_watched
&& isset( $this->watched
[$title->getNamespace()][$title->getDBkey()] ) ) {
365 $pageInfo['watched'] = '';
368 if ( $this->fld_talkid
&& isset( $this->talkids
[$title->getNamespace()][$title->getDBkey()] ) ) {
369 $pageInfo['talkid'] = $this->talkids
[$title->getNamespace()][$title->getDBkey()];
372 if ( $this->fld_subjectid
&& isset( $this->subjectids
[$title->getNamespace()][$title->getDBkey()] ) ) {
373 $pageInfo['subjectid'] = $this->subjectids
[$title->getNamespace()][$title->getDBkey()];
376 if ( $this->fld_url
) {
377 $pageInfo['fullurl'] = wfExpandUrl( $title->getFullURL(), PROTO_CURRENT
);
378 $pageInfo['editurl'] = wfExpandUrl( $title->getFullURL( 'action=edit' ), PROTO_CURRENT
);
380 if ( $this->fld_readable
&& $title->userCan( 'read' ) ) {
381 $pageInfo['readable'] = '';
384 if ( $this->fld_preload
) {
385 if ( $title->exists() ) {
386 $pageInfo['preload'] = '';
389 wfRunHooks( 'EditFormPreloadText', array( &$text, &$title ) );
391 $pageInfo['preload'] = $text;
395 if ( $this->fld_displaytitle
) {
396 if ( isset( $this->displaytitles
[$title->getArticleId()] ) ) {
397 $pageInfo['displaytitle'] = $this->displaytitles
[$title->getArticleId()];
399 $pageInfo['displaytitle'] = $title->getPrefixedText();
407 * Get information about protections and put it in $protections
409 private function getProtectionInfo() {
411 $this->protections
= array();
412 $db = $this->getDB();
414 // Get normal protections for existing titles
415 if ( count( $this->titles
) ) {
416 $this->resetQueryParams();
417 $this->addTables( array( 'page_restrictions', 'page' ) );
418 $this->addWhere( 'page_id=pr_page' );
419 $this->addFields( array( 'pr_page', 'pr_type', 'pr_level',
420 'pr_expiry', 'pr_cascade', 'page_namespace',
422 $this->addWhereFld( 'pr_page', array_keys( $this->titles
) );
424 $res = $this->select( __METHOD__
);
425 foreach ( $res as $row ) {
427 'type' => $row->pr_type
,
428 'level' => $row->pr_level
,
429 'expiry' => $wgContLang->formatExpiry( $row->pr_expiry
, TS_ISO_8601
)
431 if ( $row->pr_cascade
) {
434 $this->protections
[$row->page_namespace
][$row->page_title
][] = $a;
436 // Also check old restrictions
437 if ( $this->pageRestrictions
[$row->pr_page
] ) {
438 $restrictions = explode( ':', trim( $this->pageRestrictions
[$row->pr_page
] ) );
439 foreach ( $restrictions as $restrict ) {
440 $temp = explode( '=', trim( $restrict ) );
441 if ( count( $temp ) == 1 ) {
442 // old old format should be treated as edit/move restriction
443 $restriction = trim( $temp[0] );
445 if ( $restriction == '' ) {
448 $this->protections
[$row->page_namespace
][$row->page_title
][] = array(
450 'level' => $restriction,
451 'expiry' => 'infinity',
453 $this->protections
[$row->page_namespace
][$row->page_title
][] = array(
455 'level' => $restriction,
456 'expiry' => 'infinity',
459 $restriction = trim( $temp[1] );
460 if ( $restriction == '' ) {
463 $this->protections
[$row->page_namespace
][$row->page_title
][] = array(
465 'level' => $restriction,
466 'expiry' => 'infinity',
474 // Get protections for missing titles
475 if ( count( $this->missing
) ) {
476 $this->resetQueryParams();
477 $lb = new LinkBatch( $this->missing
);
478 $this->addTables( 'protected_titles' );
479 $this->addFields( array( 'pt_title', 'pt_namespace', 'pt_create_perm', 'pt_expiry' ) );
480 $this->addWhere( $lb->constructSet( 'pt', $db ) );
481 $res = $this->select( __METHOD__
);
482 foreach ( $res as $row ) {
483 $this->protections
[$row->pt_namespace
][$row->pt_title
][] = array(
485 'level' => $row->pt_create_perm
,
486 'expiry' => $wgContLang->formatExpiry( $row->pt_expiry
, TS_ISO_8601
)
491 // Cascading protections
492 $images = $others = array();
493 foreach ( $this->everything
as $title ) {
494 if ( $title->getNamespace() == NS_FILE
) {
495 $images[] = $title->getDBkey();
501 if ( count( $others ) ) {
502 // Non-images: check templatelinks
503 $lb = new LinkBatch( $others );
504 $this->resetQueryParams();
505 $this->addTables( array( 'page_restrictions', 'page', 'templatelinks' ) );
506 $this->addFields( array( 'pr_type', 'pr_level', 'pr_expiry',
507 'page_title', 'page_namespace',
508 'tl_title', 'tl_namespace' ) );
509 $this->addWhere( $lb->constructSet( 'tl', $db ) );
510 $this->addWhere( 'pr_page = page_id' );
511 $this->addWhere( 'pr_page = tl_from' );
512 $this->addWhereFld( 'pr_cascade', 1 );
514 $res = $this->select( __METHOD__
);
515 foreach ( $res as $row ) {
516 $source = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
517 $this->protections
[$row->tl_namespace
][$row->tl_title
][] = array(
518 'type' => $row->pr_type
,
519 'level' => $row->pr_level
,
520 'expiry' => $wgContLang->formatExpiry( $row->pr_expiry
, TS_ISO_8601
),
521 'source' => $source->getPrefixedText()
526 if ( count( $images ) ) {
527 // Images: check imagelinks
528 $this->resetQueryParams();
529 $this->addTables( array( 'page_restrictions', 'page', 'imagelinks' ) );
530 $this->addFields( array( 'pr_type', 'pr_level', 'pr_expiry',
531 'page_title', 'page_namespace', 'il_to' ) );
532 $this->addWhere( 'pr_page = page_id' );
533 $this->addWhere( 'pr_page = il_from' );
534 $this->addWhereFld( 'pr_cascade', 1 );
535 $this->addWhereFld( 'il_to', $images );
537 $res = $this->select( __METHOD__
);
538 foreach ( $res as $row ) {
539 $source = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
540 $this->protections
[NS_FILE
][$row->il_to
][] = array(
541 'type' => $row->pr_type
,
542 'level' => $row->pr_level
,
543 'expiry' => $wgContLang->formatExpiry( $row->pr_expiry
, TS_ISO_8601
),
544 'source' => $source->getPrefixedText()
551 * Get talk page IDs (if requested) and subject page IDs (if requested)
552 * and put them in $talkids and $subjectids
554 private function getTSIDs() {
555 $getTitles = $this->talkids
= $this->subjectids
= array();
557 foreach ( $this->everything
as $t ) {
558 if ( MWNamespace
::isTalk( $t->getNamespace() ) ) {
559 if ( $this->fld_subjectid
) {
560 $getTitles[] = $t->getSubjectPage();
562 } elseif ( $this->fld_talkid
) {
563 $getTitles[] = $t->getTalkPage();
566 if ( !count( $getTitles ) ) {
570 $db = $this->getDB();
572 // Construct a custom WHERE clause that matches
573 // all titles in $getTitles
574 $lb = new LinkBatch( $getTitles );
575 $this->resetQueryParams();
576 $this->addTables( 'page' );
577 $this->addFields( array( 'page_title', 'page_namespace', 'page_id' ) );
578 $this->addWhere( $lb->constructSet( 'page', $db ) );
579 $res = $this->select( __METHOD__
);
580 foreach ( $res as $row ) {
581 if ( MWNamespace
::isTalk( $row->page_namespace
) ) {
582 $this->talkids
[MWNamespace
::getSubject( $row->page_namespace
)][$row->page_title
] =
583 intval( $row->page_id
);
585 $this->subjectids
[MWNamespace
::getTalk( $row->page_namespace
)][$row->page_title
] =
586 intval( $row->page_id
);
591 private function getDisplayTitle() {
592 $this->displaytitles
= array();
594 $pageIds = array_keys( $this->titles
);
596 if ( !count( $pageIds ) ) {
600 $this->resetQueryParams();
601 $this->addTables( 'page_props' );
602 $this->addFields( array( 'pp_page', 'pp_value' ) );
603 $this->addWhereFld( 'pp_page', $pageIds );
604 $this->addWhereFld( 'pp_propname', 'displaytitle' );
605 $res = $this->select( __METHOD__
);
607 foreach ( $res as $row ) {
608 $this->displaytitles
[$row->pp_page
] = $row->pp_value
;
613 * Get information about watched status and put it in $this->watched
615 private function getWatchedInfo() {
616 $user = $this->getUser();
618 if ( $user->isAnon() ||
count( $this->everything
) == 0 ) {
622 $this->watched
= array();
623 $db = $this->getDB();
625 $lb = new LinkBatch( $this->everything
);
627 $this->resetQueryParams();
628 $this->addTables( array( 'watchlist' ) );
629 $this->addFields( array( 'wl_title', 'wl_namespace' ) );
630 $this->addWhere( array(
631 $lb->constructSet( 'wl', $db ),
632 'wl_user' => $user->getID()
635 $res = $this->select( __METHOD__
);
637 foreach ( $res as $row ) {
638 $this->watched
[$row->wl_namespace
][$row->wl_title
] = true;
642 public function getCacheMode( $params ) {
643 $publicProps = array(
651 if ( !is_null( $params['prop'] ) ) {
652 foreach ( $params['prop'] as $prop ) {
653 if ( !in_array( $prop, $publicProps ) ) {
658 if ( !is_null( $params['token'] ) ) {
664 public function getAllowedParams() {
667 ApiBase
::PARAM_DFLT
=> null,
668 ApiBase
::PARAM_ISMULTI
=> true,
669 ApiBase
::PARAM_TYPE
=> array(
675 'readable', # private
678 // If you add more properties here, please consider whether they
679 // need to be added to getCacheMode()
682 ApiBase
::PARAM_DFLT
=> null,
683 ApiBase
::PARAM_ISMULTI
=> true,
684 ApiBase
::PARAM_TYPE
=> array_keys( $this->getTokenFunctions() )
690 public function getParamDescription() {
693 'Which additional properties to get:',
694 ' protection - List the protection level of each page',
695 ' talkid - The page ID of the talk page for each non-talk page',
696 ' watched - List the watched status of each page',
697 ' subjectid - The page ID of the parent page for each talk page',
698 ' url - Gives a full URL to the page, and also an edit URL',
699 ' readable - Whether the user can read this page',
700 ' preload - Gives the text returned by EditFormPreloadText',
701 ' displaytitle - Gives the way the page title is actually displayed',
703 'token' => 'Request a token to perform a data-modifying action on a page',
704 'continue' => 'When more results are available, use this to continue',
708 public function getDescription() {
709 return 'Get basic page information such as namespace, title, last touched date, ...';
712 public function getPossibleErrors() {
713 return array_merge( parent
::getPossibleErrors(), array(
714 array( 'code' => '_badcontinue', 'info' => 'Invalid continue param. You should pass the original value returned by the previous query' ),
718 public function getExamples() {
720 'api.php?action=query&prop=info&titles=Main%20Page',
721 'api.php?action=query&prop=info&inprop=protection&titles=Main%20Page'
725 public function getHelpUrls() {
726 return 'https://www.mediawiki.org/wiki/API:Properties#info_.2F_in';
729 public function getVersion() {
730 return __CLASS__
. ': $Id$';