3 * API for MediaWiki 1.8+
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
27 if ( !defined( 'MEDIAWIKI' ) ) {
28 // Eclipse helper - will be ignored in production
29 require_once( 'ApiQueryBase.php' );
33 * A query module to show basic page information.
37 class ApiQueryInfo
extends ApiQueryBase
{
39 private $fld_protection = false, $fld_talkid = false,
40 $fld_subjectid = false, $fld_url = false,
41 $fld_readable = false, $fld_watched = false,
42 $fld_preload = false, $fld_displaytitle = false;
44 private $tokenFunctions;
46 public function __construct( $query, $moduleName ) {
47 parent
::__construct( $query, $moduleName, 'in' );
50 public function requestExtraData( $pageSet ) {
51 $pageSet->requestField( 'page_restrictions' );
52 $pageSet->requestField( 'page_is_redirect' );
53 $pageSet->requestField( 'page_is_new' );
54 $pageSet->requestField( 'page_counter' );
55 $pageSet->requestField( 'page_touched' );
56 $pageSet->requestField( 'page_latest' );
57 $pageSet->requestField( 'page_len' );
61 * Get an array mapping token names to their handler functions.
62 * The prototype for a token function is func($pageid, $title)
63 * it should return a token or false (permission denied)
64 * @return array(tokenname => function)
66 protected function getTokenFunctions() {
67 // Don't call the hooks twice
68 if ( isset( $this->tokenFunctions
) ) {
69 return $this->tokenFunctions
;
72 // If we're in JSON callback mode, no tokens can be obtained
73 if ( !is_null( $this->getMain()->getRequest()->getVal( 'callback' ) ) ) {
77 $this->tokenFunctions
= array(
78 'edit' => array( 'ApiQueryInfo', 'getEditToken' ),
79 'delete' => array( 'ApiQueryInfo', 'getDeleteToken' ),
80 'protect' => array( 'ApiQueryInfo', 'getProtectToken' ),
81 'move' => array( 'ApiQueryInfo', 'getMoveToken' ),
82 'block' => array( 'ApiQueryInfo', 'getBlockToken' ),
83 'unblock' => array( 'ApiQueryInfo', 'getUnblockToken' ),
84 'email' => array( 'ApiQueryInfo', 'getEmailToken' ),
85 'import' => array( 'ApiQueryInfo', 'getImportToken' ),
87 wfRunHooks( 'APIQueryInfoTokens', array( &$this->tokenFunctions
) );
88 return $this->tokenFunctions
;
91 public static function getEditToken( $pageid, $title ) {
92 // We could check for $title->userCan('edit') here,
93 // but that's too expensive for this purpose
94 // and would break caching
96 if ( !$wgUser->isAllowed( 'edit' ) ) {
100 // The edit token is always the same, let's exploit that
101 static $cachedEditToken = null;
102 if ( !is_null( $cachedEditToken ) ) {
103 return $cachedEditToken;
106 $cachedEditToken = $wgUser->editToken();
107 return $cachedEditToken;
110 public static function getDeleteToken( $pageid, $title ) {
112 if ( !$wgUser->isAllowed( 'delete' ) ) {
116 static $cachedDeleteToken = null;
117 if ( !is_null( $cachedDeleteToken ) ) {
118 return $cachedDeleteToken;
121 $cachedDeleteToken = $wgUser->editToken();
122 return $cachedDeleteToken;
125 public static function getProtectToken( $pageid, $title ) {
127 if ( !$wgUser->isAllowed( 'protect' ) ) {
131 static $cachedProtectToken = null;
132 if ( !is_null( $cachedProtectToken ) ) {
133 return $cachedProtectToken;
136 $cachedProtectToken = $wgUser->editToken();
137 return $cachedProtectToken;
140 public static function getMoveToken( $pageid, $title ) {
142 if ( !$wgUser->isAllowed( 'move' ) ) {
146 static $cachedMoveToken = null;
147 if ( !is_null( $cachedMoveToken ) ) {
148 return $cachedMoveToken;
151 $cachedMoveToken = $wgUser->editToken();
152 return $cachedMoveToken;
155 public static function getBlockToken( $pageid, $title ) {
157 if ( !$wgUser->isAllowed( 'block' ) ) {
161 static $cachedBlockToken = null;
162 if ( !is_null( $cachedBlockToken ) ) {
163 return $cachedBlockToken;
166 $cachedBlockToken = $wgUser->editToken();
167 return $cachedBlockToken;
170 public static function getUnblockToken( $pageid, $title ) {
171 // Currently, this is exactly the same as the block token
172 return self
::getBlockToken( $pageid, $title );
175 public static function getEmailToken( $pageid, $title ) {
177 if ( !$wgUser->canSendEmail() ||
$wgUser->isBlockedFromEmailUser() ) {
181 static $cachedEmailToken = null;
182 if ( !is_null( $cachedEmailToken ) ) {
183 return $cachedEmailToken;
186 $cachedEmailToken = $wgUser->editToken();
187 return $cachedEmailToken;
190 public static function getImportToken( $pageid, $title ) {
192 if ( !$wgUser->isAllowed( 'import' ) ) {
196 static $cachedImportToken = null;
197 if ( !is_null( $cachedImportToken ) ) {
198 return $cachedImportToken;
201 $cachedImportToken = $wgUser->editToken();
202 return $cachedImportToken;
205 public function execute() {
206 $this->params
= $this->extractRequestParams();
207 if ( !is_null( $this->params
['prop'] ) ) {
208 $prop = array_flip( $this->params
['prop'] );
209 $this->fld_protection
= isset( $prop['protection'] );
210 $this->fld_watched
= isset( $prop['watched'] );
211 $this->fld_talkid
= isset( $prop['talkid'] );
212 $this->fld_subjectid
= isset( $prop['subjectid'] );
213 $this->fld_url
= isset( $prop['url'] );
214 $this->fld_readable
= isset( $prop['readable'] );
215 $this->fld_preload
= isset( $prop['preload'] );
216 $this->fld_displaytitle
= isset( $prop['displaytitle'] );
219 $pageSet = $this->getPageSet();
220 $this->titles
= $pageSet->getGoodTitles();
221 $this->missing
= $pageSet->getMissingTitles();
222 $this->everything
= $this->titles +
$this->missing
;
223 $result = $this->getResult();
225 uasort( $this->everything
, array( 'Title', 'compare' ) );
226 if ( !is_null( $this->params
['continue'] ) ) {
227 // Throw away any titles we're gonna skip so they don't
229 $cont = explode( '|', $this->params
['continue'] );
230 if ( count( $cont ) != 2 ) {
231 $this->dieUsage( 'Invalid continue param. You should pass the original ' .
232 'value returned by the previous query', '_badcontinue' );
234 $conttitle = Title
::makeTitleSafe( $cont[0], $cont[1] );
235 foreach ( $this->everything
as $pageid => $title ) {
236 if ( Title
::compare( $title, $conttitle ) >= 0 ) {
239 unset( $this->titles
[$pageid] );
240 unset( $this->missing
[$pageid] );
241 unset( $this->everything
[$pageid] );
245 $this->pageRestrictions
= $pageSet->getCustomField( 'page_restrictions' );
246 $this->pageIsRedir
= $pageSet->getCustomField( 'page_is_redirect' );
247 $this->pageIsNew
= $pageSet->getCustomField( 'page_is_new' );
248 $this->pageCounter
= $pageSet->getCustomField( 'page_counter' );
249 $this->pageTouched
= $pageSet->getCustomField( 'page_touched' );
250 $this->pageLatest
= $pageSet->getCustomField( 'page_latest' );
251 $this->pageLength
= $pageSet->getCustomField( 'page_len' );
253 // Get protection info if requested
254 if ( $this->fld_protection
) {
255 $this->getProtectionInfo();
258 if ( $this->fld_watched
) {
259 $this->getWatchedInfo();
262 // Run the talkid/subjectid query if requested
263 if ( $this->fld_talkid ||
$this->fld_subjectid
) {
267 if ( $this->fld_displaytitle
) {
268 $this->getDisplayTitle();
271 foreach ( $this->everything
as $pageid => $title ) {
272 $pageInfo = $this->extractPageInfo( $pageid, $title );
273 $fit = $result->addValue( array(
276 ), $pageid, $pageInfo );
278 $this->setContinueEnumParameter( 'continue',
279 $title->getNamespace() . '|' .
287 * Get a result array with information about a title
288 * @param $pageid int Page ID (negative for missing titles)
289 * @param $title Title object
292 private function extractPageInfo( $pageid, $title ) {
294 if ( $title->exists() ) {
295 $pageInfo['touched'] = wfTimestamp( TS_ISO_8601
, $this->pageTouched
[$pageid] );
296 $pageInfo['lastrevid'] = intval( $this->pageLatest
[$pageid] );
297 $pageInfo['counter'] = intval( $this->pageCounter
[$pageid] );
298 $pageInfo['length'] = intval( $this->pageLength
[$pageid] );
300 if ( $this->pageIsRedir
[$pageid] ) {
301 $pageInfo['redirect'] = '';
303 if ( $this->pageIsNew
[$pageid] ) {
304 $pageInfo['new'] = '';
308 if ( !is_null( $this->params
['token'] ) ) {
309 $tokenFunctions = $this->getTokenFunctions();
310 $pageInfo['starttimestamp'] = wfTimestamp( TS_ISO_8601
, time() );
311 foreach ( $this->params
['token'] as $t ) {
312 $val = call_user_func( $tokenFunctions[$t], $pageid, $title );
313 if ( $val === false ) {
314 $this->setWarning( "Action '$t' is not allowed for the current user" );
316 $pageInfo[$t . 'token'] = $val;
321 if ( $this->fld_protection
) {
322 $pageInfo['protection'] = array();
323 if ( isset( $this->protections
[$title->getNamespace()][$title->getDBkey()] ) ) {
324 $pageInfo['protection'] =
325 $this->protections
[$title->getNamespace()][$title->getDBkey()];
327 $this->getResult()->setIndexedTagName( $pageInfo['protection'], 'pr' );
330 if ( $this->fld_watched
&& isset( $this->watched
[$title->getNamespace()][$title->getDBkey()] ) ) {
331 $pageInfo['watched'] = '';
334 if ( $this->fld_talkid
&& isset( $this->talkids
[$title->getNamespace()][$title->getDBkey()] ) ) {
335 $pageInfo['talkid'] = $this->talkids
[$title->getNamespace()][$title->getDBkey()];
338 if ( $this->fld_subjectid
&& isset( $this->subjectids
[$title->getNamespace()][$title->getDBkey()] ) ) {
339 $pageInfo['subjectid'] = $this->subjectids
[$title->getNamespace()][$title->getDBkey()];
342 if ( $this->fld_url
) {
343 $pageInfo['fullurl'] = $title->getFullURL();
344 $pageInfo['editurl'] = $title->getFullURL( 'action=edit' );
346 if ( $this->fld_readable
&& $title->userCanRead() ) {
347 $pageInfo['readable'] = '';
350 if ( $this->fld_preload
) {
351 if ( $title->exists() ) {
352 $pageInfo['preload'] = '';
355 wfRunHooks( 'EditFormPreloadText', array( &$text, &$title ) );
357 $pageInfo['preload'] = $text;
361 if ( $this->fld_displaytitle
) {
362 if ( isset( $this->displaytitles
[$title->getArticleId()] ) ) {
363 $pageInfo['displaytitle'] = $this->displaytitles
[$title->getArticleId()];
365 $pageInfo['displaytitle'] = $title->getPrefixedText();
373 * Get information about protections and put it in $protections
375 private function getProtectionInfo() {
376 $this->protections
= array();
377 $db = $this->getDB();
379 // Get normal protections for existing titles
380 if ( count( $this->titles
) ) {
381 $this->resetQueryParams();
382 $this->addTables( array( 'page_restrictions', 'page' ) );
383 $this->addWhere( 'page_id=pr_page' );
384 $this->addFields( array( 'pr_page', 'pr_type', 'pr_level',
385 'pr_expiry', 'pr_cascade', 'page_namespace',
387 $this->addWhereFld( 'pr_page', array_keys( $this->titles
) );
389 $res = $this->select( __METHOD__
);
390 foreach ( $res as $row ) {
392 'type' => $row->pr_type
,
393 'level' => $row->pr_level
,
394 'expiry' => Block
::decodeExpiry( $row->pr_expiry
, TS_ISO_8601
)
396 if ( $row->pr_cascade
) {
399 $this->protections
[$row->page_namespace
][$row->page_title
][] = $a;
401 // Also check old restrictions
402 if ( $this->pageRestrictions
[$row->pr_page
] ) {
403 $restrictions = explode( ':', trim( $this->pageRestrictions
[$row->pr_page
] ) );
404 foreach ( $restrictions as $restrict ) {
405 $temp = explode( '=', trim( $restrict ) );
406 if ( count( $temp ) == 1 ) {
407 // old old format should be treated as edit/move restriction
408 $restriction = trim( $temp[0] );
410 if ( $restriction == '' ) {
413 $this->protections
[$row->page_namespace
][$row->page_title
][] = array(
415 'level' => $restriction,
416 'expiry' => 'infinity',
418 $this->protections
[$row->page_namespace
][$row->page_title
][] = array(
420 'level' => $restriction,
421 'expiry' => 'infinity',
424 $restriction = trim( $temp[1] );
425 if ( $restriction == '' ) {
428 $this->protections
[$row->page_namespace
][$row->page_title
][] = array(
430 'level' => $restriction,
431 'expiry' => 'infinity',
439 // Get protections for missing titles
440 if ( count( $this->missing
) ) {
441 $this->resetQueryParams();
442 $lb = new LinkBatch( $this->missing
);
443 $this->addTables( 'protected_titles' );
444 $this->addFields( array( 'pt_title', 'pt_namespace', 'pt_create_perm', 'pt_expiry' ) );
445 $this->addWhere( $lb->constructSet( 'pt', $db ) );
446 $res = $this->select( __METHOD__
);
447 foreach ( $res as $row ) {
448 $this->protections
[$row->pt_namespace
][$row->pt_title
][] = array(
450 'level' => $row->pt_create_perm
,
451 'expiry' => Block
::decodeExpiry( $row->pt_expiry
, TS_ISO_8601
)
456 // Cascading protections
457 $images = $others = array();
458 foreach ( $this->everything
as $title ) {
459 if ( $title->getNamespace() == NS_FILE
) {
460 $images[] = $title->getDBkey();
466 if ( count( $others ) ) {
467 // Non-images: check templatelinks
468 $lb = new LinkBatch( $others );
469 $this->resetQueryParams();
470 $this->addTables( array( 'page_restrictions', 'page', 'templatelinks' ) );
471 $this->addFields( array( 'pr_type', 'pr_level', 'pr_expiry',
472 'page_title', 'page_namespace',
473 'tl_title', 'tl_namespace' ) );
474 $this->addWhere( $lb->constructSet( 'tl', $db ) );
475 $this->addWhere( 'pr_page = page_id' );
476 $this->addWhere( 'pr_page = tl_from' );
477 $this->addWhereFld( 'pr_cascade', 1 );
479 $res = $this->select( __METHOD__
);
480 foreach ( $res as $row ) {
481 $source = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
482 $this->protections
[$row->tl_namespace
][$row->tl_title
][] = array(
483 'type' => $row->pr_type
,
484 'level' => $row->pr_level
,
485 'expiry' => Block
::decodeExpiry( $row->pr_expiry
, TS_ISO_8601
),
486 'source' => $source->getPrefixedText()
491 if ( count( $images ) ) {
492 // Images: check imagelinks
493 $this->resetQueryParams();
494 $this->addTables( array( 'page_restrictions', 'page', 'imagelinks' ) );
495 $this->addFields( array( 'pr_type', 'pr_level', 'pr_expiry',
496 'page_title', 'page_namespace', 'il_to' ) );
497 $this->addWhere( 'pr_page = page_id' );
498 $this->addWhere( 'pr_page = il_from' );
499 $this->addWhereFld( 'pr_cascade', 1 );
500 $this->addWhereFld( 'il_to', $images );
502 $res = $this->select( __METHOD__
);
503 foreach ( $res as $row ) {
504 $source = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
505 $this->protections
[NS_FILE
][$row->il_to
][] = array(
506 'type' => $row->pr_type
,
507 'level' => $row->pr_level
,
508 'expiry' => Block
::decodeExpiry( $row->pr_expiry
, TS_ISO_8601
),
509 'source' => $source->getPrefixedText()
516 * Get talk page IDs (if requested) and subject page IDs (if requested)
517 * and put them in $talkids and $subjectids
519 private function getTSIDs() {
520 $getTitles = $this->talkids
= $this->subjectids
= array();
522 foreach ( $this->everything
as $t ) {
523 if ( MWNamespace
::isTalk( $t->getNamespace() ) ) {
524 if ( $this->fld_subjectid
) {
525 $getTitles[] = $t->getSubjectPage();
527 } elseif ( $this->fld_talkid
) {
528 $getTitles[] = $t->getTalkPage();
531 if ( !count( $getTitles ) ) {
535 $db = $this->getDB();
537 // Construct a custom WHERE clause that matches
538 // all titles in $getTitles
539 $lb = new LinkBatch( $getTitles );
540 $this->resetQueryParams();
541 $this->addTables( 'page' );
542 $this->addFields( array( 'page_title', 'page_namespace', 'page_id' ) );
543 $this->addWhere( $lb->constructSet( 'page', $db ) );
544 $res = $this->select( __METHOD__
);
545 foreach ( $res as $row ) {
546 if ( MWNamespace
::isTalk( $row->page_namespace
) ) {
547 $this->talkids
[MWNamespace
::getSubject( $row->page_namespace
)][$row->page_title
] =
548 intval( $row->page_id
);
550 $this->subjectids
[MWNamespace
::getTalk( $row->page_namespace
)][$row->page_title
] =
551 intval( $row->page_id
);
556 private function getDisplayTitle() {
557 $this->displaytitles
= array();
559 $pageIds = array_keys( $this->titles
);
561 if ( !count( $pageIds ) ) {
565 $this->resetQueryParams();
566 $this->addTables( 'page_props' );
567 $this->addFields( array( 'pp_page', 'pp_value' ) );
568 $this->addWhereFld( 'pp_page', $pageIds );
569 $this->addWhereFld( 'pp_propname', 'displaytitle' );
570 $res = $this->select( __METHOD__
);
572 foreach ( $res as $row ) {
573 $this->displaytitles
[$row->pp_page
] = $row->pp_value
;
578 * Get information about watched status and put it in $this->watched
580 private function getWatchedInfo() {
583 if ( $wgUser->isAnon() ||
count( $this->titles
) == 0 ) {
587 $this->watched
= array();
588 $db = $this->getDB();
590 $lb = new LinkBatch( $this->titles
);
592 $this->resetQueryParams();
593 $this->addTables( array( 'page', 'watchlist' ) );
594 $this->addFields( array( 'page_title', 'page_namespace' ) );
595 $this->addWhere( array(
596 $lb->constructSet( 'page', $db ),
597 'wl_namespace=page_namespace',
598 'wl_title=page_title',
599 'wl_user' => $wgUser->getID()
602 $res = $this->select( __METHOD__
);
604 foreach ( $res as $row ) {
605 $this->watched
[$row->page_namespace
][$row->page_title
] = true;
609 public function getCacheMode( $params ) {
610 $publicProps = array(
618 if ( !is_null( $params['prop'] ) ) {
619 foreach ( $params['prop'] as $prop ) {
620 if ( !in_array( $prop, $publicProps ) ) {
625 if ( !is_null( $params['token'] ) ) {
631 public function getAllowedParams() {
634 ApiBase
::PARAM_DFLT
=> null,
635 ApiBase
::PARAM_ISMULTI
=> true,
636 ApiBase
::PARAM_TYPE
=> array(
642 'readable', # private
645 // If you add more properties here, please consider whether they
646 // need to be added to getCacheMode()
649 ApiBase
::PARAM_DFLT
=> null,
650 ApiBase
::PARAM_ISMULTI
=> true,
651 ApiBase
::PARAM_TYPE
=> array_keys( $this->getTokenFunctions() )
657 public function getParamDescription() {
660 'Which additional properties to get:',
661 ' protection - List the protection level of each page',
662 ' talkid - The page ID of the talk page for each non-talk page',
663 ' watched - List the watched status of each page',
664 ' subjectid - The page ID of the parent page for each talk page',
665 ' url - Gives a full URL to the page, and also an edit URL',
666 ' readable - Whether the user can read this page',
667 ' preload - Gives the text returned by EditFormPreloadText',
668 ' displaytitle - Gives the way the page title is actually displayed',
670 'token' => 'Request a token to perform a data-modifying action on a page',
671 'continue' => 'When more results are available, use this to continue',
675 public function getDescription() {
676 return 'Get basic page information such as namespace, title, last touched date, ...';
679 public function getPossibleErrors() {
680 return array_merge( parent
::getPossibleErrors(), array(
681 array( 'code' => '_badcontinue', 'info' => 'Invalid continue param. You should pass the original value returned by the previous query' ),
685 protected function getExamples() {
687 'api.php?action=query&prop=info&titles=Main%20Page',
688 'api.php?action=query&prop=info&inprop=protection&titles=Main%20Page'
692 public function getVersion() {
693 return __CLASS__
. ': $Id$';