4 * Created on Sep 25, 2006
6 * API for MediaWiki 1.8+
8 * Copyright © 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 * http://www.gnu.org/copyleft/gpl.html
26 if ( !defined( 'MEDIAWIKI' ) ) {
27 // Eclipse helper - will be ignored in production
28 require_once( 'ApiQueryBase.php' );
32 * A query module to show basic page information.
36 class ApiQueryInfo
extends ApiQueryBase
{
38 private $fld_protection = false, $fld_talkid = false,
39 $fld_subjectid = false, $fld_url = false,
40 $fld_readable = false, $fld_watched = false,
43 public function __construct( $query, $moduleName ) {
44 parent
::__construct( $query, $moduleName, 'in' );
47 public function requestExtraData( $pageSet ) {
48 $pageSet->requestField( 'page_restrictions' );
49 $pageSet->requestField( 'page_is_redirect' );
50 $pageSet->requestField( 'page_is_new' );
51 $pageSet->requestField( 'page_counter' );
52 $pageSet->requestField( 'page_touched' );
53 $pageSet->requestField( 'page_latest' );
54 $pageSet->requestField( 'page_len' );
58 * Get an array mapping token names to their handler functions.
59 * The prototype for a token function is func($pageid, $title)
60 * it should return a token or false (permission denied)
61 * @return array(tokenname => function)
63 protected function getTokenFunctions() {
64 // Don't call the hooks twice
65 if ( isset( $this->tokenFunctions
) ) {
66 return $this->tokenFunctions
;
69 // If we're in JSON callback mode, no tokens can be obtained
70 if ( !is_null( $this->getMain()->getRequest()->getVal( 'callback' ) ) ) {
74 $this->tokenFunctions
= array(
75 'edit' => array( 'ApiQueryInfo', 'getEditToken' ),
76 'delete' => array( 'ApiQueryInfo', 'getDeleteToken' ),
77 'protect' => array( 'ApiQueryInfo', 'getProtectToken' ),
78 'move' => array( 'ApiQueryInfo', 'getMoveToken' ),
79 'block' => array( 'ApiQueryInfo', 'getBlockToken' ),
80 'unblock' => array( 'ApiQueryInfo', 'getUnblockToken' ),
81 'email' => array( 'ApiQueryInfo', 'getEmailToken' ),
82 'import' => array( 'ApiQueryInfo', 'getImportToken' ),
84 wfRunHooks( 'APIQueryInfoTokens', array( &$this->tokenFunctions
) );
85 return $this->tokenFunctions
;
88 public static function getEditToken( $pageid, $title ) {
89 // We could check for $title->userCan('edit') here,
90 // but that's too expensive for this purpose
91 // and would break caching
93 if ( !$wgUser->isAllowed( 'edit' ) ) {
97 // The edit token is always the same, let's exploit that
98 static $cachedEditToken = null;
99 if ( !is_null( $cachedEditToken ) ) {
100 return $cachedEditToken;
103 $cachedEditToken = $wgUser->editToken();
104 return $cachedEditToken;
107 public static function getDeleteToken( $pageid, $title ) {
109 if ( !$wgUser->isAllowed( 'delete' ) ) {
113 static $cachedDeleteToken = null;
114 if ( !is_null( $cachedDeleteToken ) ) {
115 return $cachedDeleteToken;
118 $cachedDeleteToken = $wgUser->editToken();
119 return $cachedDeleteToken;
122 public static function getProtectToken( $pageid, $title ) {
124 if ( !$wgUser->isAllowed( 'protect' ) ) {
128 static $cachedProtectToken = null;
129 if ( !is_null( $cachedProtectToken ) ) {
130 return $cachedProtectToken;
133 $cachedProtectToken = $wgUser->editToken();
134 return $cachedProtectToken;
137 public static function getMoveToken( $pageid, $title ) {
139 if ( !$wgUser->isAllowed( 'move' ) ) {
143 static $cachedMoveToken = null;
144 if ( !is_null( $cachedMoveToken ) ) {
145 return $cachedMoveToken;
148 $cachedMoveToken = $wgUser->editToken();
149 return $cachedMoveToken;
152 public static function getBlockToken( $pageid, $title ) {
154 if ( !$wgUser->isAllowed( 'block' ) ) {
158 static $cachedBlockToken = null;
159 if ( !is_null( $cachedBlockToken ) ) {
160 return $cachedBlockToken;
163 $cachedBlockToken = $wgUser->editToken();
164 return $cachedBlockToken;
167 public static function getUnblockToken( $pageid, $title ) {
168 // Currently, this is exactly the same as the block token
169 return self
::getBlockToken( $pageid, $title );
172 public static function getEmailToken( $pageid, $title ) {
174 if ( !$wgUser->canSendEmail() ||
$wgUser->isBlockedFromEmailUser() ) {
178 static $cachedEmailToken = null;
179 if ( !is_null( $cachedEmailToken ) ) {
180 return $cachedEmailToken;
183 $cachedEmailToken = $wgUser->editToken();
184 return $cachedEmailToken;
187 public static function getImportToken( $pageid, $title ) {
189 if ( !$wgUser->isAllowed( 'import' ) ) {
193 static $cachedImportToken = null;
194 if ( !is_null( $cachedImportToken ) ) {
195 return $cachedImportToken;
198 $cachedImportToken = $wgUser->editToken();
199 return $cachedImportToken;
202 public function execute() {
203 $this->params
= $this->extractRequestParams();
204 if ( !is_null( $this->params
['prop'] ) ) {
205 $prop = array_flip( $this->params
['prop'] );
206 $this->fld_protection
= isset( $prop['protection'] );
207 $this->fld_watched
= isset( $prop['watched'] );
208 $this->fld_talkid
= isset( $prop['talkid'] );
209 $this->fld_subjectid
= isset( $prop['subjectid'] );
210 $this->fld_url
= isset( $prop['url'] );
211 $this->fld_readable
= isset( $prop['readable'] );
212 $this->fld_preload
= isset( $prop['preload'] );
215 $pageSet = $this->getPageSet();
216 $this->titles
= $pageSet->getGoodTitles();
217 $this->missing
= $pageSet->getMissingTitles();
218 $this->everything
= $this->titles +
$this->missing
;
219 $result = $this->getResult();
221 uasort( $this->everything
, array( 'Title', 'compare' ) );
222 if ( !is_null( $this->params
['continue'] ) ) {
223 // Throw away any titles we're gonna skip so they don't
225 $cont = explode( '|', $this->params
['continue'] );
226 if ( count( $cont ) != 2 ) {
227 $this->dieUsage( 'Invalid continue param. You should pass the original ' .
228 'value returned by the previous query', '_badcontinue' );
230 $conttitle = Title
::makeTitleSafe( $cont[0], $cont[1] );
231 foreach ( $this->everything
as $pageid => $title ) {
232 if ( Title
::compare( $title, $conttitle ) >= 0 ) {
235 unset( $this->titles
[$pageid] );
236 unset( $this->missing
[$pageid] );
237 unset( $this->everything
[$pageid] );
241 $this->pageRestrictions
= $pageSet->getCustomField( 'page_restrictions' );
242 $this->pageIsRedir
= $pageSet->getCustomField( 'page_is_redirect' );
243 $this->pageIsNew
= $pageSet->getCustomField( 'page_is_new' );
244 $this->pageCounter
= $pageSet->getCustomField( 'page_counter' );
245 $this->pageTouched
= $pageSet->getCustomField( 'page_touched' );
246 $this->pageLatest
= $pageSet->getCustomField( 'page_latest' );
247 $this->pageLength
= $pageSet->getCustomField( 'page_len' );
249 $db = $this->getDB();
250 // Get protection info if requested
251 if ( $this->fld_protection
) {
252 $this->getProtectionInfo();
255 if ( $this->fld_watched
) {
256 $this->getWatchedInfo();
259 // Run the talkid/subjectid query if requested
260 if ( $this->fld_talkid ||
$this->fld_subjectid
) {
264 foreach ( $this->everything
as $pageid => $title ) {
265 $pageInfo = $this->extractPageInfo( $pageid, $title );
266 $fit = $result->addValue( array(
269 ), $pageid, $pageInfo );
271 $this->setContinueEnumParameter( 'continue',
272 $title->getNamespace() . '|' .
280 * Get a result array with information about a title
281 * @param $pageid int Page ID (negative for missing titles)
282 * @param $title Title object
285 private function extractPageInfo( $pageid, $title ) {
287 if ( $title->exists() ) {
288 $pageInfo['touched'] = wfTimestamp( TS_ISO_8601
, $this->pageTouched
[$pageid] );
289 $pageInfo['lastrevid'] = intval( $this->pageLatest
[$pageid] );
290 $pageInfo['counter'] = intval( $this->pageCounter
[$pageid] );
291 $pageInfo['length'] = intval( $this->pageLength
[$pageid] );
292 if ( $this->pageIsRedir
[$pageid] ) {
293 $pageInfo['redirect'] = '';
295 if ( $this->pageIsNew
[$pageid] ) {
296 $pageInfo['new'] = '';
300 if ( !is_null( $this->params
['token'] ) ) {
301 $tokenFunctions = $this->getTokenFunctions();
302 $pageInfo['starttimestamp'] = wfTimestamp( TS_ISO_8601
, time() );
303 foreach ( $this->params
['token'] as $t ) {
304 $val = call_user_func( $tokenFunctions[$t], $pageid, $title );
305 if ( $val === false ) {
306 $this->setWarning( "Action '$t' is not allowed for the current user" );
308 $pageInfo[$t . 'token'] = $val;
313 if ( $this->fld_protection
) {
314 $pageInfo['protection'] = array();
315 if ( isset( $this->protections
[$title->getNamespace()][$title->getDBkey()] ) ) {
316 $pageInfo['protection'] =
317 $this->protections
[$title->getNamespace()][$title->getDBkey()];
319 $this->getResult()->setIndexedTagName( $pageInfo['protection'], 'pr' );
322 if ( $this->fld_watched
&& isset( $this->watched
[$title->getNamespace()][$title->getDBkey()] ) )
324 $pageInfo['watched'] = '';
327 if ( $this->fld_talkid
&& isset( $this->talkids
[$title->getNamespace()][$title->getDBkey()] ) )
329 $pageInfo['talkid'] = $this->talkids
[$title->getNamespace()][$title->getDBkey()];
332 if ( $this->fld_subjectid
&& isset( $this->subjectids
[$title->getNamespace()][$title->getDBkey()] ) )
334 $pageInfo['subjectid'] = $this->subjectids
[$title->getNamespace()][$title->getDBkey()];
337 if ( $this->fld_url
) {
338 $pageInfo['fullurl'] = $title->getFullURL();
339 $pageInfo['editurl'] = $title->getFullURL( 'action=edit' );
341 if ( $this->fld_readable
&& $title->userCanRead() ) {
342 $pageInfo['readable'] = '';
345 if ( $this->fld_preload
) {
346 if ( $title->exists() ) {
347 $pageInfo['preload'] = '';
349 wfRunHooks( 'EditFormPreloadText', array( &$text, &$title ) );
351 $pageInfo['preload'] = $text;
358 * Get information about protections and put it in $protections
360 private function getProtectionInfo() {
361 $this->protections
= array();
362 $db = $this->getDB();
364 // Get normal protections for existing titles
365 if ( count( $this->titles
) ) {
366 $this->resetQueryParams();
367 $this->addTables( array( 'page_restrictions', 'page' ) );
368 $this->addWhere( 'page_id=pr_page' );
369 $this->addFields( array( 'pr_page', 'pr_type', 'pr_level',
370 'pr_expiry', 'pr_cascade', 'page_namespace',
372 $this->addWhereFld( 'pr_page', array_keys( $this->titles
) );
374 $res = $this->select( __METHOD__
);
375 while ( $row = $db->fetchObject( $res ) ) {
377 'type' => $row->pr_type
,
378 'level' => $row->pr_level
,
379 'expiry' => Block
::decodeExpiry( $row->pr_expiry
, TS_ISO_8601
)
381 if ( $row->pr_cascade
) {
384 $this->protections
[$row->page_namespace
][$row->page_title
][] = $a;
386 // Also check old restrictions
387 if ( $this->pageRestrictions
[$row->pr_page
] ) {
388 $restrictions = explode( ':', trim( $this->pageRestrictions
[$row->pr_page
] ) );
389 foreach ( $restrictions as $restrict ) {
390 $temp = explode( '=', trim( $restrict ) );
391 if ( count( $temp ) == 1 ) {
392 // old old format should be treated as edit/move restriction
393 $restriction = trim( $temp[0] );
395 if ( $restriction == '' ) {
398 $this->protections
[$row->page_namespace
][$row->page_title
][] = array(
400 'level' => $restriction,
401 'expiry' => 'infinity',
403 $this->protections
[$row->page_namespace
][$row->page_title
][] = array(
405 'level' => $restriction,
406 'expiry' => 'infinity',
409 $restriction = trim( $temp[1] );
410 if ( $restriction == '' ) {
413 $this->protections
[$row->page_namespace
][$row->page_title
][] = array(
415 'level' => $restriction,
416 'expiry' => 'infinity',
422 $db->freeResult( $res );
425 // Get protections for missing titles
426 if ( count( $this->missing
) ) {
427 $this->resetQueryParams();
428 $lb = new LinkBatch( $this->missing
);
429 $this->addTables( 'protected_titles' );
430 $this->addFields( array( 'pt_title', 'pt_namespace', 'pt_create_perm', 'pt_expiry' ) );
431 $this->addWhere( $lb->constructSet( 'pt', $db ) );
432 $res = $this->select( __METHOD__
);
433 while ( $row = $db->fetchObject( $res ) ) {
434 $this->protections
[$row->pt_namespace
][$row->pt_title
][] = array(
436 'level' => $row->pt_create_perm
,
437 'expiry' => Block
::decodeExpiry( $row->pt_expiry
, TS_ISO_8601
)
440 $db->freeResult( $res );
443 // Cascading protections
444 $images = $others = array();
445 foreach ( $this->everything
as $title )
446 if ( $title->getNamespace() == NS_FILE
) {
447 $images[] = $title->getDBkey();
452 if ( count( $others ) ) {
453 // Non-images: check templatelinks
454 $lb = new LinkBatch( $others );
455 $this->resetQueryParams();
456 $this->addTables( array( 'page_restrictions', 'page', 'templatelinks' ) );
457 $this->addFields( array( 'pr_type', 'pr_level', 'pr_expiry',
458 'page_title', 'page_namespace',
459 'tl_title', 'tl_namespace' ) );
460 $this->addWhere( $lb->constructSet( 'tl', $db ) );
461 $this->addWhere( 'pr_page = page_id' );
462 $this->addWhere( 'pr_page = tl_from' );
463 $this->addWhereFld( 'pr_cascade', 1 );
465 $res = $this->select( __METHOD__
);
466 while ( $row = $db->fetchObject( $res ) ) {
467 $source = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
468 $this->protections
[$row->tl_namespace
][$row->tl_title
][] = array(
469 'type' => $row->pr_type
,
470 'level' => $row->pr_level
,
471 'expiry' => Block
::decodeExpiry( $row->pr_expiry
, TS_ISO_8601
),
472 'source' => $source->getPrefixedText()
475 $db->freeResult( $res );
478 if ( count( $images ) ) {
479 // Images: check imagelinks
480 $this->resetQueryParams();
481 $this->addTables( array( 'page_restrictions', 'page', 'imagelinks' ) );
482 $this->addFields( array( 'pr_type', 'pr_level', 'pr_expiry',
483 'page_title', 'page_namespace', 'il_to' ) );
484 $this->addWhere( 'pr_page = page_id' );
485 $this->addWhere( 'pr_page = il_from' );
486 $this->addWhereFld( 'pr_cascade', 1 );
487 $this->addWhereFld( 'il_to', $images );
489 $res = $this->select( __METHOD__
);
490 while ( $row = $db->fetchObject( $res ) ) {
491 $source = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
492 $this->protections
[NS_FILE
][$row->il_to
][] = array(
493 'type' => $row->pr_type
,
494 'level' => $row->pr_level
,
495 'expiry' => Block
::decodeExpiry( $row->pr_expiry
, TS_ISO_8601
),
496 'source' => $source->getPrefixedText()
499 $db->freeResult( $res );
504 * Get talk page IDs (if requested) and subject page IDs (if requested)
505 * and put them in $talkids and $subjectids
507 private function getTSIDs() {
508 $getTitles = $this->talkids
= $this->subjectids
= array();
509 $db = $this->getDB();
510 foreach ( $this->everything
as $t ) {
511 if ( MWNamespace
::isTalk( $t->getNamespace() ) ) {
512 if ( $this->fld_subjectid
) {
513 $getTitles[] = $t->getSubjectPage();
515 } elseif ( $this->fld_talkid
) {
516 $getTitles[] = $t->getTalkPage();
519 if ( !count( $getTitles ) ) {
523 // Construct a custom WHERE clause that matches
524 // all titles in $getTitles
525 $lb = new LinkBatch( $getTitles );
526 $this->resetQueryParams();
527 $this->addTables( 'page' );
528 $this->addFields( array( 'page_title', 'page_namespace', 'page_id' ) );
529 $this->addWhere( $lb->constructSet( 'page', $db ) );
530 $res = $this->select( __METHOD__
);
531 while ( $row = $db->fetchObject( $res ) ) {
532 if ( MWNamespace
::isTalk( $row->page_namespace
) ) {
533 $this->talkids
[MWNamespace
::getSubject( $row->page_namespace
)][$row->page_title
] =
534 intval( $row->page_id
);
536 $this->subjectids
[MWNamespace
::getTalk( $row->page_namespace
)][$row->page_title
] =
537 intval( $row->page_id
);
543 * Get information about watched status and put it in $watched
545 private function getWatchedInfo() {
548 if ( $wgUser->isAnon() ||
count( $this->titles
) == 0 ) {
552 $this->watched
= array();
553 $db = $this->getDB();
555 $lb = new LinkBatch( $this->titles
);
557 $this->resetQueryParams();
558 $this->addTables( array( 'page', 'watchlist' ) );
559 $this->addFields( array( 'page_title', 'page_namespace' ) );
560 $this->addWhere( array(
561 $lb->constructSet( 'page', $db ),
562 'wl_namespace=page_namespace',
563 'wl_title=page_title',
564 'wl_user' => $wgUser->getID()
567 $res = $this->select( __METHOD__
);
569 while ( $row = $db->fetchObject( $res ) ) {
570 $this->watched
[$row->page_namespace
][$row->page_title
] = true;
574 public function getAllowedParams() {
577 ApiBase
::PARAM_DFLT
=> null,
578 ApiBase
::PARAM_ISMULTI
=> true,
579 ApiBase
::PARAM_TYPE
=> array(
589 ApiBase
::PARAM_DFLT
=> null,
590 ApiBase
::PARAM_ISMULTI
=> true,
591 ApiBase
::PARAM_TYPE
=> array_keys( $this->getTokenFunctions() )
597 public function getParamDescription() {
600 'Which additional properties to get:',
601 ' protection - List the protection level of each page',
602 ' talkid - The page ID of the talk page for each non-talk page',
603 ' watched - List the watched status of each page',
604 ' subjectid - The page ID of the parent page for each talk page',
605 ' url - Gives a full URL to the page, and also an edit URL',
606 ' readable - Whether the user can read this page',
607 ' preload - Gives the text returned by EditFormPreloadText'
609 'token' => 'Request a token to perform a data-modifying action on a page',
610 'continue' => 'When more results are available, use this to continue',
614 public function getDescription() {
615 return 'Get basic page information such as namespace, title, last touched date, ...';
618 public function getPossibleErrors() {
619 return array_merge( parent
::getPossibleErrors(), array(
620 array( 'code' => '_badcontinue', 'info' => 'Invalid continue param. You should pass the original value returned by the previous query' ),
624 protected function getExamples() {
626 'api.php?action=query&prop=info&titles=Main%20Page',
627 'api.php?action=query&prop=info&inprop=protection&titles=Main%20Page'
631 public function getVersion() {
632 return __CLASS__
. ': $Id$';