d6d00496af457bed94c3d085a8c5dbd2ae6bb255
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 public function __construct( $query, $moduleName ) {
45 parent
::__construct( $query, $moduleName, 'in' );
48 public function requestExtraData( $pageSet ) {
49 $pageSet->requestField( 'page_restrictions' );
50 $pageSet->requestField( 'page_is_redirect' );
51 $pageSet->requestField( 'page_is_new' );
52 $pageSet->requestField( 'page_counter' );
53 $pageSet->requestField( 'page_touched' );
54 $pageSet->requestField( 'page_latest' );
55 $pageSet->requestField( 'page_len' );
59 * Get an array mapping token names to their handler functions.
60 * The prototype for a token function is func($pageid, $title)
61 * it should return a token or false (permission denied)
62 * @return array(tokenname => function)
64 protected function getTokenFunctions() {
65 // Don't call the hooks twice
66 if ( isset( $this->tokenFunctions
) ) {
67 return $this->tokenFunctions
;
70 // If we're in JSON callback mode, no tokens can be obtained
71 if ( !is_null( $this->getMain()->getRequest()->getVal( 'callback' ) ) ) {
75 $this->tokenFunctions
= array(
76 'edit' => array( 'ApiQueryInfo', 'getEditToken' ),
77 'delete' => array( 'ApiQueryInfo', 'getDeleteToken' ),
78 'protect' => array( 'ApiQueryInfo', 'getProtectToken' ),
79 'move' => array( 'ApiQueryInfo', 'getMoveToken' ),
80 'block' => array( 'ApiQueryInfo', 'getBlockToken' ),
81 'unblock' => array( 'ApiQueryInfo', 'getUnblockToken' ),
82 'email' => array( 'ApiQueryInfo', 'getEmailToken' ),
83 'import' => array( 'ApiQueryInfo', 'getImportToken' ),
85 wfRunHooks( 'APIQueryInfoTokens', array( &$this->tokenFunctions
) );
86 return $this->tokenFunctions
;
89 public static function getEditToken( $pageid, $title ) {
90 // We could check for $title->userCan('edit') here,
91 // but that's too expensive for this purpose
92 // and would break caching
94 if ( !$wgUser->isAllowed( 'edit' ) ) {
98 // The edit token is always the same, let's exploit that
99 static $cachedEditToken = null;
100 if ( !is_null( $cachedEditToken ) ) {
101 return $cachedEditToken;
104 $cachedEditToken = $wgUser->editToken();
105 return $cachedEditToken;
108 public static function getDeleteToken( $pageid, $title ) {
110 if ( !$wgUser->isAllowed( 'delete' ) ) {
114 static $cachedDeleteToken = null;
115 if ( !is_null( $cachedDeleteToken ) ) {
116 return $cachedDeleteToken;
119 $cachedDeleteToken = $wgUser->editToken();
120 return $cachedDeleteToken;
123 public static function getProtectToken( $pageid, $title ) {
125 if ( !$wgUser->isAllowed( 'protect' ) ) {
129 static $cachedProtectToken = null;
130 if ( !is_null( $cachedProtectToken ) ) {
131 return $cachedProtectToken;
134 $cachedProtectToken = $wgUser->editToken();
135 return $cachedProtectToken;
138 public static function getMoveToken( $pageid, $title ) {
140 if ( !$wgUser->isAllowed( 'move' ) ) {
144 static $cachedMoveToken = null;
145 if ( !is_null( $cachedMoveToken ) ) {
146 return $cachedMoveToken;
149 $cachedMoveToken = $wgUser->editToken();
150 return $cachedMoveToken;
153 public static function getBlockToken( $pageid, $title ) {
155 if ( !$wgUser->isAllowed( 'block' ) ) {
159 static $cachedBlockToken = null;
160 if ( !is_null( $cachedBlockToken ) ) {
161 return $cachedBlockToken;
164 $cachedBlockToken = $wgUser->editToken();
165 return $cachedBlockToken;
168 public static function getUnblockToken( $pageid, $title ) {
169 // Currently, this is exactly the same as the block token
170 return self
::getBlockToken( $pageid, $title );
173 public static function getEmailToken( $pageid, $title ) {
175 if ( !$wgUser->canSendEmail() ||
$wgUser->isBlockedFromEmailUser() ) {
179 static $cachedEmailToken = null;
180 if ( !is_null( $cachedEmailToken ) ) {
181 return $cachedEmailToken;
184 $cachedEmailToken = $wgUser->editToken();
185 return $cachedEmailToken;
188 public static function getImportToken( $pageid, $title ) {
190 if ( !$wgUser->isAllowed( 'import' ) ) {
194 static $cachedImportToken = null;
195 if ( !is_null( $cachedImportToken ) ) {
196 return $cachedImportToken;
199 $cachedImportToken = $wgUser->editToken();
200 return $cachedImportToken;
203 public function execute() {
204 $this->params
= $this->extractRequestParams();
205 if ( !is_null( $this->params
['prop'] ) ) {
206 $prop = array_flip( $this->params
['prop'] );
207 $this->fld_protection
= isset( $prop['protection'] );
208 $this->fld_watched
= isset( $prop['watched'] );
209 $this->fld_talkid
= isset( $prop['talkid'] );
210 $this->fld_subjectid
= isset( $prop['subjectid'] );
211 $this->fld_url
= isset( $prop['url'] );
212 $this->fld_readable
= isset( $prop['readable'] );
213 $this->fld_preload
= isset( $prop['preload'] );
214 $this->fld_displaytitle
= isset( $prop['displaytitle'] );
217 $pageSet = $this->getPageSet();
218 $this->titles
= $pageSet->getGoodTitles();
219 $this->missing
= $pageSet->getMissingTitles();
220 $this->everything
= $this->titles +
$this->missing
;
221 $result = $this->getResult();
223 uasort( $this->everything
, array( 'Title', 'compare' ) );
224 if ( !is_null( $this->params
['continue'] ) ) {
225 // Throw away any titles we're gonna skip so they don't
227 $cont = explode( '|', $this->params
['continue'] );
228 if ( count( $cont ) != 2 ) {
229 $this->dieUsage( 'Invalid continue param. You should pass the original ' .
230 'value returned by the previous query', '_badcontinue' );
232 $conttitle = Title
::makeTitleSafe( $cont[0], $cont[1] );
233 foreach ( $this->everything
as $pageid => $title ) {
234 if ( Title
::compare( $title, $conttitle ) >= 0 ) {
237 unset( $this->titles
[$pageid] );
238 unset( $this->missing
[$pageid] );
239 unset( $this->everything
[$pageid] );
243 $this->pageRestrictions
= $pageSet->getCustomField( 'page_restrictions' );
244 $this->pageIsRedir
= $pageSet->getCustomField( 'page_is_redirect' );
245 $this->pageIsNew
= $pageSet->getCustomField( 'page_is_new' );
246 $this->pageCounter
= $pageSet->getCustomField( 'page_counter' );
247 $this->pageTouched
= $pageSet->getCustomField( 'page_touched' );
248 $this->pageLatest
= $pageSet->getCustomField( 'page_latest' );
249 $this->pageLength
= $pageSet->getCustomField( 'page_len' );
251 // Get protection info if requested
252 if ( $this->fld_protection
) {
253 $this->getProtectionInfo();
256 if ( $this->fld_watched
) {
257 $this->getWatchedInfo();
260 // Run the talkid/subjectid query if requested
261 if ( $this->fld_talkid ||
$this->fld_subjectid
) {
265 if ( $this->fld_displaytitle
) {
266 $this->getDisplayTitle();
269 foreach ( $this->everything
as $pageid => $title ) {
270 $pageInfo = $this->extractPageInfo( $pageid, $title );
271 $fit = $result->addValue( array(
274 ), $pageid, $pageInfo );
276 $this->setContinueEnumParameter( 'continue',
277 $title->getNamespace() . '|' .
285 * Get a result array with information about a title
286 * @param $pageid int Page ID (negative for missing titles)
287 * @param $title Title object
290 private function extractPageInfo( $pageid, $title ) {
292 if ( $title->exists() ) {
293 $pageInfo['touched'] = wfTimestamp( TS_ISO_8601
, $this->pageTouched
[$pageid] );
294 $pageInfo['lastrevid'] = intval( $this->pageLatest
[$pageid] );
295 $pageInfo['counter'] = intval( $this->pageCounter
[$pageid] );
296 $pageInfo['length'] = intval( $this->pageLength
[$pageid] );
298 if ( $this->pageIsRedir
[$pageid] ) {
299 $pageInfo['redirect'] = '';
301 if ( $this->pageIsNew
[$pageid] ) {
302 $pageInfo['new'] = '';
306 if ( !is_null( $this->params
['token'] ) ) {
307 $tokenFunctions = $this->getTokenFunctions();
308 $pageInfo['starttimestamp'] = wfTimestamp( TS_ISO_8601
, time() );
309 foreach ( $this->params
['token'] as $t ) {
310 $val = call_user_func( $tokenFunctions[$t], $pageid, $title );
311 if ( $val === false ) {
312 $this->setWarning( "Action '$t' is not allowed for the current user" );
314 $pageInfo[$t . 'token'] = $val;
319 if ( $this->fld_protection
) {
320 $pageInfo['protection'] = array();
321 if ( isset( $this->protections
[$title->getNamespace()][$title->getDBkey()] ) ) {
322 $pageInfo['protection'] =
323 $this->protections
[$title->getNamespace()][$title->getDBkey()];
325 $this->getResult()->setIndexedTagName( $pageInfo['protection'], 'pr' );
328 if ( $this->fld_watched
&& isset( $this->watched
[$title->getNamespace()][$title->getDBkey()] ) ) {
329 $pageInfo['watched'] = '';
332 if ( $this->fld_talkid
&& isset( $this->talkids
[$title->getNamespace()][$title->getDBkey()] ) ) {
333 $pageInfo['talkid'] = $this->talkids
[$title->getNamespace()][$title->getDBkey()];
336 if ( $this->fld_subjectid
&& isset( $this->subjectids
[$title->getNamespace()][$title->getDBkey()] ) ) {
337 $pageInfo['subjectid'] = $this->subjectids
[$title->getNamespace()][$title->getDBkey()];
340 if ( $this->fld_url
) {
341 $pageInfo['fullurl'] = $title->getFullURL();
342 $pageInfo['editurl'] = $title->getFullURL( 'action=edit' );
344 if ( $this->fld_readable
&& $title->userCanRead() ) {
345 $pageInfo['readable'] = '';
348 if ( $this->fld_preload
) {
349 if ( $title->exists() ) {
350 $pageInfo['preload'] = '';
353 wfRunHooks( 'EditFormPreloadText', array( &$text, &$title ) );
355 $pageInfo['preload'] = $text;
359 if ( $this->fld_displaytitle
) {
360 if ( isset( $this->displaytitles
[$title->getArticleId()] ) ) {
361 $pageInfo['displaytitle'] = $this->displaytitles
[$title->getArticleId()];
363 $pageInfo['displaytitle'] = $title->getPrefixedText();
371 * Get information about protections and put it in $protections
373 private function getProtectionInfo() {
374 $this->protections
= array();
375 $db = $this->getDB();
377 // Get normal protections for existing titles
378 if ( count( $this->titles
) ) {
379 $this->resetQueryParams();
380 $this->addTables( array( 'page_restrictions', 'page' ) );
381 $this->addWhere( 'page_id=pr_page' );
382 $this->addFields( array( 'pr_page', 'pr_type', 'pr_level',
383 'pr_expiry', 'pr_cascade', 'page_namespace',
385 $this->addWhereFld( 'pr_page', array_keys( $this->titles
) );
387 $res = $this->select( __METHOD__
);
388 foreach ( $res as $row ) {
390 'type' => $row->pr_type
,
391 'level' => $row->pr_level
,
392 'expiry' => Block
::decodeExpiry( $row->pr_expiry
, TS_ISO_8601
)
394 if ( $row->pr_cascade
) {
397 $this->protections
[$row->page_namespace
][$row->page_title
][] = $a;
399 // Also check old restrictions
400 if ( $this->pageRestrictions
[$row->pr_page
] ) {
401 $restrictions = explode( ':', trim( $this->pageRestrictions
[$row->pr_page
] ) );
402 foreach ( $restrictions as $restrict ) {
403 $temp = explode( '=', trim( $restrict ) );
404 if ( count( $temp ) == 1 ) {
405 // old old format should be treated as edit/move restriction
406 $restriction = trim( $temp[0] );
408 if ( $restriction == '' ) {
411 $this->protections
[$row->page_namespace
][$row->page_title
][] = array(
413 'level' => $restriction,
414 'expiry' => 'infinity',
416 $this->protections
[$row->page_namespace
][$row->page_title
][] = array(
418 'level' => $restriction,
419 'expiry' => 'infinity',
422 $restriction = trim( $temp[1] );
423 if ( $restriction == '' ) {
426 $this->protections
[$row->page_namespace
][$row->page_title
][] = array(
428 'level' => $restriction,
429 'expiry' => 'infinity',
437 // Get protections for missing titles
438 if ( count( $this->missing
) ) {
439 $this->resetQueryParams();
440 $lb = new LinkBatch( $this->missing
);
441 $this->addTables( 'protected_titles' );
442 $this->addFields( array( 'pt_title', 'pt_namespace', 'pt_create_perm', 'pt_expiry' ) );
443 $this->addWhere( $lb->constructSet( 'pt', $db ) );
444 $res = $this->select( __METHOD__
);
445 foreach ( $res as $row ) {
446 $this->protections
[$row->pt_namespace
][$row->pt_title
][] = array(
448 'level' => $row->pt_create_perm
,
449 'expiry' => Block
::decodeExpiry( $row->pt_expiry
, TS_ISO_8601
)
454 // Cascading protections
455 $images = $others = array();
456 foreach ( $this->everything
as $title ) {
457 if ( $title->getNamespace() == NS_FILE
) {
458 $images[] = $title->getDBkey();
464 if ( count( $others ) ) {
465 // Non-images: check templatelinks
466 $lb = new LinkBatch( $others );
467 $this->resetQueryParams();
468 $this->addTables( array( 'page_restrictions', 'page', 'templatelinks' ) );
469 $this->addFields( array( 'pr_type', 'pr_level', 'pr_expiry',
470 'page_title', 'page_namespace',
471 'tl_title', 'tl_namespace' ) );
472 $this->addWhere( $lb->constructSet( 'tl', $db ) );
473 $this->addWhere( 'pr_page = page_id' );
474 $this->addWhere( 'pr_page = tl_from' );
475 $this->addWhereFld( 'pr_cascade', 1 );
477 $res = $this->select( __METHOD__
);
478 foreach ( $res as $row ) {
479 $source = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
480 $this->protections
[$row->tl_namespace
][$row->tl_title
][] = array(
481 'type' => $row->pr_type
,
482 'level' => $row->pr_level
,
483 'expiry' => Block
::decodeExpiry( $row->pr_expiry
, TS_ISO_8601
),
484 'source' => $source->getPrefixedText()
489 if ( count( $images ) ) {
490 // Images: check imagelinks
491 $this->resetQueryParams();
492 $this->addTables( array( 'page_restrictions', 'page', 'imagelinks' ) );
493 $this->addFields( array( 'pr_type', 'pr_level', 'pr_expiry',
494 'page_title', 'page_namespace', 'il_to' ) );
495 $this->addWhere( 'pr_page = page_id' );
496 $this->addWhere( 'pr_page = il_from' );
497 $this->addWhereFld( 'pr_cascade', 1 );
498 $this->addWhereFld( 'il_to', $images );
500 $res = $this->select( __METHOD__
);
501 foreach ( $res as $row ) {
502 $source = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
503 $this->protections
[NS_FILE
][$row->il_to
][] = array(
504 'type' => $row->pr_type
,
505 'level' => $row->pr_level
,
506 'expiry' => Block
::decodeExpiry( $row->pr_expiry
, TS_ISO_8601
),
507 'source' => $source->getPrefixedText()
514 * Get talk page IDs (if requested) and subject page IDs (if requested)
515 * and put them in $talkids and $subjectids
517 private function getTSIDs() {
518 $getTitles = $this->talkids
= $this->subjectids
= array();
520 foreach ( $this->everything
as $t ) {
521 if ( MWNamespace
::isTalk( $t->getNamespace() ) ) {
522 if ( $this->fld_subjectid
) {
523 $getTitles[] = $t->getSubjectPage();
525 } elseif ( $this->fld_talkid
) {
526 $getTitles[] = $t->getTalkPage();
529 if ( !count( $getTitles ) ) {
533 $db = $this->getDB();
535 // Construct a custom WHERE clause that matches
536 // all titles in $getTitles
537 $lb = new LinkBatch( $getTitles );
538 $this->resetQueryParams();
539 $this->addTables( 'page' );
540 $this->addFields( array( 'page_title', 'page_namespace', 'page_id' ) );
541 $this->addWhere( $lb->constructSet( 'page', $db ) );
542 $res = $this->select( __METHOD__
);
543 foreach ( $res as $row ) {
544 if ( MWNamespace
::isTalk( $row->page_namespace
) ) {
545 $this->talkids
[MWNamespace
::getSubject( $row->page_namespace
)][$row->page_title
] =
546 intval( $row->page_id
);
548 $this->subjectids
[MWNamespace
::getTalk( $row->page_namespace
)][$row->page_title
] =
549 intval( $row->page_id
);
554 private function getDisplayTitle() {
555 $this->displaytitles
= array();
557 $pageIds = array_keys( $this->titles
);
559 if ( !count( $pageIds ) ) {
563 $this->resetQueryParams();
564 $this->addTables( 'page_props' );
565 $this->addFields( array( 'pp_page', 'pp_value' ) );
566 $this->addWhereFld( 'pp_page', $pageIds );
567 $this->addWhereFld( 'pp_propname', 'displaytitle' );
568 $res = $this->select( __METHOD__
);
570 foreach ( $res as $row ) {
571 $this->displaytitles
[$row->pp_page
] = $row->pp_value
;
576 * Get information about watched status and put it in $this->watched
578 private function getWatchedInfo() {
581 if ( $wgUser->isAnon() ||
count( $this->titles
) == 0 ) {
585 $this->watched
= array();
586 $db = $this->getDB();
588 $lb = new LinkBatch( $this->titles
);
590 $this->resetQueryParams();
591 $this->addTables( array( 'page', 'watchlist' ) );
592 $this->addFields( array( 'page_title', 'page_namespace' ) );
593 $this->addWhere( array(
594 $lb->constructSet( 'page', $db ),
595 'wl_namespace=page_namespace',
596 'wl_title=page_title',
597 'wl_user' => $wgUser->getID()
600 $res = $this->select( __METHOD__
);
602 foreach ( $res as $row ) {
603 $this->watched
[$row->page_namespace
][$row->page_title
] = true;
607 public function getCacheMode( $params ) {
608 $publicProps = array(
616 if ( !is_null( $params['prop'] ) ) {
617 foreach ( $params['prop'] as $prop ) {
618 if ( !in_array( $prop, $publicProps ) ) {
623 if ( !is_null( $params['token'] ) ) {
629 public function getAllowedParams() {
632 ApiBase
::PARAM_DFLT
=> null,
633 ApiBase
::PARAM_ISMULTI
=> true,
634 ApiBase
::PARAM_TYPE
=> array(
640 'readable', # private
643 // If you add more properties here, please consider whether they
644 // need to be added to getCacheMode()
647 ApiBase
::PARAM_DFLT
=> null,
648 ApiBase
::PARAM_ISMULTI
=> true,
649 ApiBase
::PARAM_TYPE
=> array_keys( $this->getTokenFunctions() )
655 public function getParamDescription() {
658 'Which additional properties to get:',
659 ' protection - List the protection level of each page',
660 ' talkid - The page ID of the talk page for each non-talk page',
661 ' watched - List the watched status of each page',
662 ' subjectid - The page ID of the parent page for each talk page',
663 ' url - Gives a full URL to the page, and also an edit URL',
664 ' readable - Whether the user can read this page',
665 ' preload - Gives the text returned by EditFormPreloadText',
666 ' displaytitle - Gives the way the page title is actually displayed',
668 'token' => 'Request a token to perform a data-modifying action on a page',
669 'continue' => 'When more results are available, use this to continue',
673 public function getDescription() {
674 return 'Get basic page information such as namespace, title, last touched date, ...';
677 public function getPossibleErrors() {
678 return array_merge( parent
::getPossibleErrors(), array(
679 array( 'code' => '_badcontinue', 'info' => 'Invalid continue param. You should pass the original value returned by the previous query' ),
683 protected function getExamples() {
685 'api.php?action=query&prop=info&titles=Main%20Page',
686 'api.php?action=query&prop=info&inprop=protection&titles=Main%20Page'
690 public function getVersion() {
691 return __CLASS__
. ': $Id$';