3156b7ee90766fd3bdbbb15966cf03f8afe7b0b6
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 global $wgDisableCounters;
53 $pageSet->requestField( 'page_restrictions' );
54 $pageSet->requestField( 'page_is_redirect' );
55 $pageSet->requestField( 'page_is_new' );
56 if ( !$wgDisableCounters ) {
57 $pageSet->requestField( 'page_counter' );
59 $pageSet->requestField( 'page_touched' );
60 $pageSet->requestField( 'page_latest' );
61 $pageSet->requestField( 'page_len' );
65 * Get an array mapping token names to their handler functions.
66 * The prototype for a token function is func($pageid, $title)
67 * it should return a token or false (permission denied)
68 * @return array(tokenname => function)
70 protected function getTokenFunctions() {
71 // Don't call the hooks twice
72 if ( isset( $this->tokenFunctions
) ) {
73 return $this->tokenFunctions
;
76 // If we're in JSON callback mode, no tokens can be obtained
77 if ( !is_null( $this->getMain()->getRequest()->getVal( 'callback' ) ) ) {
81 $this->tokenFunctions
= array(
82 'edit' => array( 'ApiQueryInfo', 'getEditToken' ),
83 'delete' => array( 'ApiQueryInfo', 'getDeleteToken' ),
84 'protect' => array( 'ApiQueryInfo', 'getProtectToken' ),
85 'move' => array( 'ApiQueryInfo', 'getMoveToken' ),
86 'block' => array( 'ApiQueryInfo', 'getBlockToken' ),
87 'unblock' => array( 'ApiQueryInfo', 'getUnblockToken' ),
88 'email' => array( 'ApiQueryInfo', 'getEmailToken' ),
89 'import' => array( 'ApiQueryInfo', 'getImportToken' ),
90 'patrol' => array( 'ApiQueryRecentChanges', 'getPatrolToken' ),
92 wfRunHooks( 'APIQueryInfoTokens', array( &$this->tokenFunctions
) );
93 return $this->tokenFunctions
;
96 public static function getEditToken( $pageid, $title ) {
97 // We could check for $title->userCan('edit') here,
98 // but that's too expensive for this purpose
99 // and would break caching
101 if ( !$wgUser->isAllowed( 'edit' ) ) {
105 // The edit token is always the same, let's exploit that
106 static $cachedEditToken = null;
107 if ( !is_null( $cachedEditToken ) ) {
108 return $cachedEditToken;
111 $cachedEditToken = $wgUser->editToken();
112 return $cachedEditToken;
115 public static function getDeleteToken( $pageid, $title ) {
117 if ( !$wgUser->isAllowed( 'delete' ) ) {
121 static $cachedDeleteToken = null;
122 if ( !is_null( $cachedDeleteToken ) ) {
123 return $cachedDeleteToken;
126 $cachedDeleteToken = $wgUser->editToken();
127 return $cachedDeleteToken;
130 public static function getProtectToken( $pageid, $title ) {
132 if ( !$wgUser->isAllowed( 'protect' ) ) {
136 static $cachedProtectToken = null;
137 if ( !is_null( $cachedProtectToken ) ) {
138 return $cachedProtectToken;
141 $cachedProtectToken = $wgUser->editToken();
142 return $cachedProtectToken;
145 public static function getMoveToken( $pageid, $title ) {
147 if ( !$wgUser->isAllowed( 'move' ) ) {
151 static $cachedMoveToken = null;
152 if ( !is_null( $cachedMoveToken ) ) {
153 return $cachedMoveToken;
156 $cachedMoveToken = $wgUser->editToken();
157 return $cachedMoveToken;
160 public static function getBlockToken( $pageid, $title ) {
162 if ( !$wgUser->isAllowed( 'block' ) ) {
166 static $cachedBlockToken = null;
167 if ( !is_null( $cachedBlockToken ) ) {
168 return $cachedBlockToken;
171 $cachedBlockToken = $wgUser->editToken();
172 return $cachedBlockToken;
175 public static function getUnblockToken( $pageid, $title ) {
176 // Currently, this is exactly the same as the block token
177 return self
::getBlockToken( $pageid, $title );
180 public static function getEmailToken( $pageid, $title ) {
182 if ( !$wgUser->canSendEmail() ||
$wgUser->isBlockedFromEmailUser() ) {
186 static $cachedEmailToken = null;
187 if ( !is_null( $cachedEmailToken ) ) {
188 return $cachedEmailToken;
191 $cachedEmailToken = $wgUser->editToken();
192 return $cachedEmailToken;
195 public static function getImportToken( $pageid, $title ) {
197 if ( !$wgUser->isAllowed( 'import' ) ) {
201 static $cachedImportToken = null;
202 if ( !is_null( $cachedImportToken ) ) {
203 return $cachedImportToken;
206 $cachedImportToken = $wgUser->editToken();
207 return $cachedImportToken;
210 public function execute() {
211 $this->params
= $this->extractRequestParams();
212 if ( !is_null( $this->params
['prop'] ) ) {
213 $prop = array_flip( $this->params
['prop'] );
214 $this->fld_protection
= isset( $prop['protection'] );
215 $this->fld_watched
= isset( $prop['watched'] );
216 $this->fld_talkid
= isset( $prop['talkid'] );
217 $this->fld_subjectid
= isset( $prop['subjectid'] );
218 $this->fld_url
= isset( $prop['url'] );
219 $this->fld_readable
= isset( $prop['readable'] );
220 $this->fld_preload
= isset( $prop['preload'] );
221 $this->fld_displaytitle
= isset( $prop['displaytitle'] );
224 $pageSet = $this->getPageSet();
225 $this->titles
= $pageSet->getGoodTitles();
226 $this->missing
= $pageSet->getMissingTitles();
227 $this->everything
= $this->titles +
$this->missing
;
228 $result = $this->getResult();
230 uasort( $this->everything
, array( 'Title', 'compare' ) );
231 if ( !is_null( $this->params
['continue'] ) ) {
232 // Throw away any titles we're gonna skip so they don't
234 $cont = explode( '|', $this->params
['continue'] );
235 if ( count( $cont ) != 2 ) {
236 $this->dieUsage( 'Invalid continue param. You should pass the original ' .
237 'value returned by the previous query', '_badcontinue' );
239 $conttitle = Title
::makeTitleSafe( $cont[0], $cont[1] );
240 foreach ( $this->everything
as $pageid => $title ) {
241 if ( Title
::compare( $title, $conttitle ) >= 0 ) {
244 unset( $this->titles
[$pageid] );
245 unset( $this->missing
[$pageid] );
246 unset( $this->everything
[$pageid] );
250 $this->pageRestrictions
= $pageSet->getCustomField( 'page_restrictions' );
251 $this->pageIsRedir
= $pageSet->getCustomField( 'page_is_redirect' );
252 $this->pageIsNew
= $pageSet->getCustomField( 'page_is_new' );
254 global $wgDisableCounters;
256 if ( !$wgDisableCounters ) {
257 $this->pageCounter
= $pageSet->getCustomField( 'page_counter' );
259 $this->pageTouched
= $pageSet->getCustomField( 'page_touched' );
260 $this->pageLatest
= $pageSet->getCustomField( 'page_latest' );
261 $this->pageLength
= $pageSet->getCustomField( 'page_len' );
263 // Get protection info if requested
264 if ( $this->fld_protection
) {
265 $this->getProtectionInfo();
268 if ( $this->fld_watched
) {
269 $this->getWatchedInfo();
272 // Run the talkid/subjectid query if requested
273 if ( $this->fld_talkid ||
$this->fld_subjectid
) {
277 if ( $this->fld_displaytitle
) {
278 $this->getDisplayTitle();
281 foreach ( $this->everything
as $pageid => $title ) {
282 $pageInfo = $this->extractPageInfo( $pageid, $title );
283 $fit = $result->addValue( array(
286 ), $pageid, $pageInfo );
288 $this->setContinueEnumParameter( 'continue',
289 $title->getNamespace() . '|' .
297 * Get a result array with information about a title
298 * @param $pageid int Page ID (negative for missing titles)
299 * @param $title Title object
302 private function extractPageInfo( $pageid, $title ) {
304 if ( $title->exists() ) {
305 global $wgDisableCounters;
307 $pageInfo['touched'] = wfTimestamp( TS_ISO_8601
, $this->pageTouched
[$pageid] );
308 $pageInfo['lastrevid'] = intval( $this->pageLatest
[$pageid] );
309 $pageInfo['counter'] = $wgDisableCounters
311 : intval( $this->pageCounter
[$pageid] );
312 $pageInfo['length'] = intval( $this->pageLength
[$pageid] );
314 if ( $this->pageIsRedir
[$pageid] ) {
315 $pageInfo['redirect'] = '';
317 if ( $this->pageIsNew
[$pageid] ) {
318 $pageInfo['new'] = '';
322 if ( !is_null( $this->params
['token'] ) ) {
323 $tokenFunctions = $this->getTokenFunctions();
324 $pageInfo['starttimestamp'] = wfTimestamp( TS_ISO_8601
, time() );
325 foreach ( $this->params
['token'] as $t ) {
326 $val = call_user_func( $tokenFunctions[$t], $pageid, $title );
327 if ( $val === false ) {
328 $this->setWarning( "Action '$t' is not allowed for the current user" );
330 $pageInfo[$t . 'token'] = $val;
335 if ( $this->fld_protection
) {
336 $pageInfo['protection'] = array();
337 if ( isset( $this->protections
[$title->getNamespace()][$title->getDBkey()] ) ) {
338 $pageInfo['protection'] =
339 $this->protections
[$title->getNamespace()][$title->getDBkey()];
341 $this->getResult()->setIndexedTagName( $pageInfo['protection'], 'pr' );
344 if ( $this->fld_watched
&& isset( $this->watched
[$title->getNamespace()][$title->getDBkey()] ) ) {
345 $pageInfo['watched'] = '';
348 if ( $this->fld_talkid
&& isset( $this->talkids
[$title->getNamespace()][$title->getDBkey()] ) ) {
349 $pageInfo['talkid'] = $this->talkids
[$title->getNamespace()][$title->getDBkey()];
352 if ( $this->fld_subjectid
&& isset( $this->subjectids
[$title->getNamespace()][$title->getDBkey()] ) ) {
353 $pageInfo['subjectid'] = $this->subjectids
[$title->getNamespace()][$title->getDBkey()];
356 if ( $this->fld_url
) {
357 $pageInfo['fullurl'] = $title->getFullURL();
358 $pageInfo['editurl'] = $title->getFullURL( 'action=edit' );
360 if ( $this->fld_readable
&& $title->userCanRead() ) {
361 $pageInfo['readable'] = '';
364 if ( $this->fld_preload
) {
365 if ( $title->exists() ) {
366 $pageInfo['preload'] = '';
369 wfRunHooks( 'EditFormPreloadText', array( &$text, &$title ) );
371 $pageInfo['preload'] = $text;
375 if ( $this->fld_displaytitle
) {
376 if ( isset( $this->displaytitles
[$title->getArticleId()] ) ) {
377 $pageInfo['displaytitle'] = $this->displaytitles
[$title->getArticleId()];
379 $pageInfo['displaytitle'] = $title->getPrefixedText();
387 * Get information about protections and put it in $protections
389 private function getProtectionInfo() {
390 $this->protections
= array();
391 $db = $this->getDB();
393 // Get normal protections for existing titles
394 if ( count( $this->titles
) ) {
395 $this->resetQueryParams();
396 $this->addTables( array( 'page_restrictions', 'page' ) );
397 $this->addWhere( 'page_id=pr_page' );
398 $this->addFields( array( 'pr_page', 'pr_type', 'pr_level',
399 'pr_expiry', 'pr_cascade', 'page_namespace',
401 $this->addWhereFld( 'pr_page', array_keys( $this->titles
) );
403 $res = $this->select( __METHOD__
);
404 foreach ( $res as $row ) {
406 'type' => $row->pr_type
,
407 'level' => $row->pr_level
,
408 'expiry' => Block
::decodeExpiry( $row->pr_expiry
, TS_ISO_8601
)
410 if ( $row->pr_cascade
) {
413 $this->protections
[$row->page_namespace
][$row->page_title
][] = $a;
415 // Also check old restrictions
416 if ( $this->pageRestrictions
[$row->pr_page
] ) {
417 $restrictions = explode( ':', trim( $this->pageRestrictions
[$row->pr_page
] ) );
418 foreach ( $restrictions as $restrict ) {
419 $temp = explode( '=', trim( $restrict ) );
420 if ( count( $temp ) == 1 ) {
421 // old old format should be treated as edit/move restriction
422 $restriction = trim( $temp[0] );
424 if ( $restriction == '' ) {
427 $this->protections
[$row->page_namespace
][$row->page_title
][] = array(
429 'level' => $restriction,
430 'expiry' => 'infinity',
432 $this->protections
[$row->page_namespace
][$row->page_title
][] = array(
434 'level' => $restriction,
435 'expiry' => 'infinity',
438 $restriction = trim( $temp[1] );
439 if ( $restriction == '' ) {
442 $this->protections
[$row->page_namespace
][$row->page_title
][] = array(
444 'level' => $restriction,
445 'expiry' => 'infinity',
453 // Get protections for missing titles
454 if ( count( $this->missing
) ) {
455 $this->resetQueryParams();
456 $lb = new LinkBatch( $this->missing
);
457 $this->addTables( 'protected_titles' );
458 $this->addFields( array( 'pt_title', 'pt_namespace', 'pt_create_perm', 'pt_expiry' ) );
459 $this->addWhere( $lb->constructSet( 'pt', $db ) );
460 $res = $this->select( __METHOD__
);
461 foreach ( $res as $row ) {
462 $this->protections
[$row->pt_namespace
][$row->pt_title
][] = array(
464 'level' => $row->pt_create_perm
,
465 'expiry' => Block
::decodeExpiry( $row->pt_expiry
, TS_ISO_8601
)
470 // Cascading protections
471 $images = $others = array();
472 foreach ( $this->everything
as $title ) {
473 if ( $title->getNamespace() == NS_FILE
) {
474 $images[] = $title->getDBkey();
480 if ( count( $others ) ) {
481 // Non-images: check templatelinks
482 $lb = new LinkBatch( $others );
483 $this->resetQueryParams();
484 $this->addTables( array( 'page_restrictions', 'page', 'templatelinks' ) );
485 $this->addFields( array( 'pr_type', 'pr_level', 'pr_expiry',
486 'page_title', 'page_namespace',
487 'tl_title', 'tl_namespace' ) );
488 $this->addWhere( $lb->constructSet( 'tl', $db ) );
489 $this->addWhere( 'pr_page = page_id' );
490 $this->addWhere( 'pr_page = tl_from' );
491 $this->addWhereFld( 'pr_cascade', 1 );
493 $res = $this->select( __METHOD__
);
494 foreach ( $res as $row ) {
495 $source = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
496 $this->protections
[$row->tl_namespace
][$row->tl_title
][] = array(
497 'type' => $row->pr_type
,
498 'level' => $row->pr_level
,
499 'expiry' => Block
::decodeExpiry( $row->pr_expiry
, TS_ISO_8601
),
500 'source' => $source->getPrefixedText()
505 if ( count( $images ) ) {
506 // Images: check imagelinks
507 $this->resetQueryParams();
508 $this->addTables( array( 'page_restrictions', 'page', 'imagelinks' ) );
509 $this->addFields( array( 'pr_type', 'pr_level', 'pr_expiry',
510 'page_title', 'page_namespace', 'il_to' ) );
511 $this->addWhere( 'pr_page = page_id' );
512 $this->addWhere( 'pr_page = il_from' );
513 $this->addWhereFld( 'pr_cascade', 1 );
514 $this->addWhereFld( 'il_to', $images );
516 $res = $this->select( __METHOD__
);
517 foreach ( $res as $row ) {
518 $source = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
519 $this->protections
[NS_FILE
][$row->il_to
][] = array(
520 'type' => $row->pr_type
,
521 'level' => $row->pr_level
,
522 'expiry' => Block
::decodeExpiry( $row->pr_expiry
, TS_ISO_8601
),
523 'source' => $source->getPrefixedText()
530 * Get talk page IDs (if requested) and subject page IDs (if requested)
531 * and put them in $talkids and $subjectids
533 private function getTSIDs() {
534 $getTitles = $this->talkids
= $this->subjectids
= array();
536 foreach ( $this->everything
as $t ) {
537 if ( MWNamespace
::isTalk( $t->getNamespace() ) ) {
538 if ( $this->fld_subjectid
) {
539 $getTitles[] = $t->getSubjectPage();
541 } elseif ( $this->fld_talkid
) {
542 $getTitles[] = $t->getTalkPage();
545 if ( !count( $getTitles ) ) {
549 $db = $this->getDB();
551 // Construct a custom WHERE clause that matches
552 // all titles in $getTitles
553 $lb = new LinkBatch( $getTitles );
554 $this->resetQueryParams();
555 $this->addTables( 'page' );
556 $this->addFields( array( 'page_title', 'page_namespace', 'page_id' ) );
557 $this->addWhere( $lb->constructSet( 'page', $db ) );
558 $res = $this->select( __METHOD__
);
559 foreach ( $res as $row ) {
560 if ( MWNamespace
::isTalk( $row->page_namespace
) ) {
561 $this->talkids
[MWNamespace
::getSubject( $row->page_namespace
)][$row->page_title
] =
562 intval( $row->page_id
);
564 $this->subjectids
[MWNamespace
::getTalk( $row->page_namespace
)][$row->page_title
] =
565 intval( $row->page_id
);
570 private function getDisplayTitle() {
571 $this->displaytitles
= array();
573 $pageIds = array_keys( $this->titles
);
575 if ( !count( $pageIds ) ) {
579 $this->resetQueryParams();
580 $this->addTables( 'page_props' );
581 $this->addFields( array( 'pp_page', 'pp_value' ) );
582 $this->addWhereFld( 'pp_page', $pageIds );
583 $this->addWhereFld( 'pp_propname', 'displaytitle' );
584 $res = $this->select( __METHOD__
);
586 foreach ( $res as $row ) {
587 $this->displaytitles
[$row->pp_page
] = $row->pp_value
;
592 * Get information about watched status and put it in $this->watched
594 private function getWatchedInfo() {
597 if ( $wgUser->isAnon() ||
count( $this->everything
) == 0 ) {
601 $this->watched
= array();
602 $db = $this->getDB();
604 $lb = new LinkBatch( $this->everything
);
606 $this->resetQueryParams();
607 $this->addTables( array( 'watchlist' ) );
608 $this->addFields( array( 'wl_title', 'wl_namespace' ) );
609 $this->addWhere( array(
610 $lb->constructSet( 'wl', $db ),
611 'wl_user' => $wgUser->getID()
614 $res = $this->select( __METHOD__
);
616 foreach ( $res as $row ) {
617 $this->watched
[$row->wl_namespace
][$row->wl_title
] = true;
621 public function getCacheMode( $params ) {
622 $publicProps = array(
630 if ( !is_null( $params['prop'] ) ) {
631 foreach ( $params['prop'] as $prop ) {
632 if ( !in_array( $prop, $publicProps ) ) {
637 if ( !is_null( $params['token'] ) ) {
643 public function getAllowedParams() {
646 ApiBase
::PARAM_DFLT
=> null,
647 ApiBase
::PARAM_ISMULTI
=> true,
648 ApiBase
::PARAM_TYPE
=> array(
654 'readable', # private
657 // If you add more properties here, please consider whether they
658 // need to be added to getCacheMode()
661 ApiBase
::PARAM_DFLT
=> null,
662 ApiBase
::PARAM_ISMULTI
=> true,
663 ApiBase
::PARAM_TYPE
=> array_keys( $this->getTokenFunctions() )
669 public function getParamDescription() {
672 'Which additional properties to get:',
673 ' protection - List the protection level of each page',
674 ' talkid - The page ID of the talk page for each non-talk page',
675 ' watched - List the watched status of each page',
676 ' subjectid - The page ID of the parent page for each talk page',
677 ' url - Gives a full URL to the page, and also an edit URL',
678 ' readable - Whether the user can read this page',
679 ' preload - Gives the text returned by EditFormPreloadText',
680 ' displaytitle - Gives the way the page title is actually displayed',
682 'token' => 'Request a token to perform a data-modifying action on a page',
683 'continue' => 'When more results are available, use this to continue',
687 public function getDescription() {
688 return 'Get basic page information such as namespace, title, last touched date, ...';
691 public function getPossibleErrors() {
692 return array_merge( parent
::getPossibleErrors(), array(
693 array( 'code' => '_badcontinue', 'info' => 'Invalid continue param. You should pass the original value returned by the previous query' ),
697 protected function getExamples() {
699 'api.php?action=query&prop=info&titles=Main%20Page',
700 'api.php?action=query&prop=info&inprop=protection&titles=Main%20Page'
704 public function getVersion() {
705 return __CLASS__
. ': $Id$';