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 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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,
41 $fld_preload = false, $fld_displaytitle = 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'] );
213 $this->fld_displaytitle
= isset( $prop['displaytitle'] );
216 $pageSet = $this->getPageSet();
217 $this->titles
= $pageSet->getGoodTitles();
218 $this->missing
= $pageSet->getMissingTitles();
219 $this->everything
= $this->titles +
$this->missing
;
220 $result = $this->getResult();
222 uasort( $this->everything
, array( 'Title', 'compare' ) );
223 if ( !is_null( $this->params
['continue'] ) ) {
224 // Throw away any titles we're gonna skip so they don't
226 $cont = explode( '|', $this->params
['continue'] );
227 if ( count( $cont ) != 2 ) {
228 $this->dieUsage( 'Invalid continue param. You should pass the original ' .
229 'value returned by the previous query', '_badcontinue' );
231 $conttitle = Title
::makeTitleSafe( $cont[0], $cont[1] );
232 foreach ( $this->everything
as $pageid => $title ) {
233 if ( Title
::compare( $title, $conttitle ) >= 0 ) {
236 unset( $this->titles
[$pageid] );
237 unset( $this->missing
[$pageid] );
238 unset( $this->everything
[$pageid] );
242 $this->pageRestrictions
= $pageSet->getCustomField( 'page_restrictions' );
243 $this->pageIsRedir
= $pageSet->getCustomField( 'page_is_redirect' );
244 $this->pageIsNew
= $pageSet->getCustomField( 'page_is_new' );
245 $this->pageCounter
= $pageSet->getCustomField( 'page_counter' );
246 $this->pageTouched
= $pageSet->getCustomField( 'page_touched' );
247 $this->pageLatest
= $pageSet->getCustomField( 'page_latest' );
248 $this->pageLength
= $pageSet->getCustomField( 'page_len' );
250 // Get protection info if requested
251 if ( $this->fld_protection
) {
252 $this->getProtectionInfo();
255 if ( $this->fld_watched
) {
256 $this->getMain()->setVaryCookie();
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 // Don't cache tokens
308 $this->getMain()->setCachePrivate();
310 $tokenFunctions = $this->getTokenFunctions();
311 $pageInfo['starttimestamp'] = wfTimestamp( TS_ISO_8601
, time() );
312 foreach ( $this->params
['token'] as $t ) {
313 $val = call_user_func( $tokenFunctions[$t], $pageid, $title );
314 if ( $val === false ) {
315 $this->setWarning( "Action '$t' is not allowed for the current user" );
317 $pageInfo[$t . 'token'] = $val;
322 if ( $this->fld_protection
) {
323 $pageInfo['protection'] = array();
324 if ( isset( $this->protections
[$title->getNamespace()][$title->getDBkey()] ) ) {
325 $pageInfo['protection'] =
326 $this->protections
[$title->getNamespace()][$title->getDBkey()];
328 $this->getResult()->setIndexedTagName( $pageInfo['protection'], 'pr' );
331 if ( $this->fld_watched
&& isset( $this->watched
[$title->getNamespace()][$title->getDBkey()] ) ) {
332 $pageInfo['watched'] = '';
335 if ( $this->fld_talkid
&& isset( $this->talkids
[$title->getNamespace()][$title->getDBkey()] ) ) {
336 $pageInfo['talkid'] = $this->talkids
[$title->getNamespace()][$title->getDBkey()];
339 if ( $this->fld_subjectid
&& isset( $this->subjectids
[$title->getNamespace()][$title->getDBkey()] ) ) {
340 $pageInfo['subjectid'] = $this->subjectids
[$title->getNamespace()][$title->getDBkey()];
343 if ( $this->fld_url
) {
344 $pageInfo['fullurl'] = $title->getFullURL();
345 $pageInfo['editurl'] = $title->getFullURL( 'action=edit' );
347 if ( $this->fld_readable
&& $title->userCanRead() ) {
348 $pageInfo['readable'] = '';
351 if ( $this->fld_preload
) {
352 if ( $title->exists() ) {
353 $pageInfo['preload'] = '';
356 wfRunHooks( 'EditFormPreloadText', array( &$text, &$title ) );
358 $pageInfo['preload'] = $text;
362 if ( $this->fld_displaytitle
) {
363 if ( isset( $this->displaytitles
[$title->getArticleId()] ) ) {
364 $pageInfo['displaytitle'] = $this->displaytitles
[$title->getArticleId()];
366 $pageInfo['displaytitle'] = $title->getPrefixedText();
374 * Get information about protections and put it in $protections
376 private function getProtectionInfo() {
377 $this->protections
= array();
378 $db = $this->getDB();
380 // Get normal protections for existing titles
381 if ( count( $this->titles
) ) {
382 $this->resetQueryParams();
383 $this->addTables( array( 'page_restrictions', 'page' ) );
384 $this->addWhere( 'page_id=pr_page' );
385 $this->addFields( array( 'pr_page', 'pr_type', 'pr_level',
386 'pr_expiry', 'pr_cascade', 'page_namespace',
388 $this->addWhereFld( 'pr_page', array_keys( $this->titles
) );
390 $res = $this->select( __METHOD__
);
391 foreach ( $res as $row ) {
393 'type' => $row->pr_type
,
394 'level' => $row->pr_level
,
395 'expiry' => Block
::decodeExpiry( $row->pr_expiry
, TS_ISO_8601
)
397 if ( $row->pr_cascade
) {
400 $this->protections
[$row->page_namespace
][$row->page_title
][] = $a;
402 // Also check old restrictions
403 if ( $this->pageRestrictions
[$row->pr_page
] ) {
404 $restrictions = explode( ':', trim( $this->pageRestrictions
[$row->pr_page
] ) );
405 foreach ( $restrictions as $restrict ) {
406 $temp = explode( '=', trim( $restrict ) );
407 if ( count( $temp ) == 1 ) {
408 // old old format should be treated as edit/move restriction
409 $restriction = trim( $temp[0] );
411 if ( $restriction == '' ) {
414 $this->protections
[$row->page_namespace
][$row->page_title
][] = array(
416 'level' => $restriction,
417 'expiry' => 'infinity',
419 $this->protections
[$row->page_namespace
][$row->page_title
][] = array(
421 'level' => $restriction,
422 'expiry' => 'infinity',
425 $restriction = trim( $temp[1] );
426 if ( $restriction == '' ) {
429 $this->protections
[$row->page_namespace
][$row->page_title
][] = array(
431 'level' => $restriction,
432 'expiry' => 'infinity',
440 // Get protections for missing titles
441 if ( count( $this->missing
) ) {
442 $this->resetQueryParams();
443 $lb = new LinkBatch( $this->missing
);
444 $this->addTables( 'protected_titles' );
445 $this->addFields( array( 'pt_title', 'pt_namespace', 'pt_create_perm', 'pt_expiry' ) );
446 $this->addWhere( $lb->constructSet( 'pt', $db ) );
447 $res = $this->select( __METHOD__
);
448 foreach ( $res as $row ) {
449 $this->protections
[$row->pt_namespace
][$row->pt_title
][] = array(
451 'level' => $row->pt_create_perm
,
452 'expiry' => Block
::decodeExpiry( $row->pt_expiry
, TS_ISO_8601
)
457 // Cascading protections
458 $images = $others = array();
459 foreach ( $this->everything
as $title ) {
460 if ( $title->getNamespace() == NS_FILE
) {
461 $images[] = $title->getDBkey();
467 if ( count( $others ) ) {
468 // Non-images: check templatelinks
469 $lb = new LinkBatch( $others );
470 $this->resetQueryParams();
471 $this->addTables( array( 'page_restrictions', 'page', 'templatelinks' ) );
472 $this->addFields( array( 'pr_type', 'pr_level', 'pr_expiry',
473 'page_title', 'page_namespace',
474 'tl_title', 'tl_namespace' ) );
475 $this->addWhere( $lb->constructSet( 'tl', $db ) );
476 $this->addWhere( 'pr_page = page_id' );
477 $this->addWhere( 'pr_page = tl_from' );
478 $this->addWhereFld( 'pr_cascade', 1 );
480 $res = $this->select( __METHOD__
);
481 foreach ( $res as $row ) {
482 $source = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
483 $this->protections
[$row->tl_namespace
][$row->tl_title
][] = array(
484 'type' => $row->pr_type
,
485 'level' => $row->pr_level
,
486 'expiry' => Block
::decodeExpiry( $row->pr_expiry
, TS_ISO_8601
),
487 'source' => $source->getPrefixedText()
492 if ( count( $images ) ) {
493 // Images: check imagelinks
494 $this->resetQueryParams();
495 $this->addTables( array( 'page_restrictions', 'page', 'imagelinks' ) );
496 $this->addFields( array( 'pr_type', 'pr_level', 'pr_expiry',
497 'page_title', 'page_namespace', 'il_to' ) );
498 $this->addWhere( 'pr_page = page_id' );
499 $this->addWhere( 'pr_page = il_from' );
500 $this->addWhereFld( 'pr_cascade', 1 );
501 $this->addWhereFld( 'il_to', $images );
503 $res = $this->select( __METHOD__
);
504 foreach ( $res as $row ) {
505 $source = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
506 $this->protections
[NS_FILE
][$row->il_to
][] = array(
507 'type' => $row->pr_type
,
508 'level' => $row->pr_level
,
509 'expiry' => Block
::decodeExpiry( $row->pr_expiry
, TS_ISO_8601
),
510 'source' => $source->getPrefixedText()
517 * Get talk page IDs (if requested) and subject page IDs (if requested)
518 * and put them in $talkids and $subjectids
520 private function getTSIDs() {
521 $getTitles = $this->talkids
= $this->subjectids
= array();
523 foreach ( $this->everything
as $t ) {
524 if ( MWNamespace
::isTalk( $t->getNamespace() ) ) {
525 if ( $this->fld_subjectid
) {
526 $getTitles[] = $t->getSubjectPage();
528 } elseif ( $this->fld_talkid
) {
529 $getTitles[] = $t->getTalkPage();
532 if ( !count( $getTitles ) ) {
536 $db = $this->getDB();
538 // Construct a custom WHERE clause that matches
539 // all titles in $getTitles
540 $lb = new LinkBatch( $getTitles );
541 $this->resetQueryParams();
542 $this->addTables( 'page' );
543 $this->addFields( array( 'page_title', 'page_namespace', 'page_id' ) );
544 $this->addWhere( $lb->constructSet( 'page', $db ) );
545 $res = $this->select( __METHOD__
);
546 foreach ( $res as $row ) {
547 if ( MWNamespace
::isTalk( $row->page_namespace
) ) {
548 $this->talkids
[MWNamespace
::getSubject( $row->page_namespace
)][$row->page_title
] =
549 intval( $row->page_id
);
551 $this->subjectids
[MWNamespace
::getTalk( $row->page_namespace
)][$row->page_title
] =
552 intval( $row->page_id
);
557 private function getDisplayTitle() {
558 $this->displaytitles
= array();
560 $pageIds = array_keys( $this->titles
);
562 if ( !count( $pageIds ) ) {
566 $this->resetQueryParams();
567 $this->addTables( 'page_props' );
568 $this->addFields( array( 'pp_page', 'pp_value' ) );
569 $this->addWhereFld( 'pp_page', $pageIds );
570 $this->addWhereFld( 'pp_propname', 'displaytitle' );
571 $res = $this->select( __METHOD__
);
573 foreach ( $res as $row ) {
574 $this->displaytitles
[$row->pp_page
] = $row->pp_value
;
579 * Get information about watched status and put it in $this->watched
581 private function getWatchedInfo() {
584 if ( $wgUser->isAnon() ||
count( $this->titles
) == 0 ) {
588 $this->watched
= array();
589 $db = $this->getDB();
591 $lb = new LinkBatch( $this->titles
);
593 $this->resetQueryParams();
594 $this->addTables( array( 'page', 'watchlist' ) );
595 $this->addFields( array( 'page_title', 'page_namespace' ) );
596 $this->addWhere( array(
597 $lb->constructSet( 'page', $db ),
598 'wl_namespace=page_namespace',
599 'wl_title=page_title',
600 'wl_user' => $wgUser->getID()
603 $res = $this->select( __METHOD__
);
605 foreach ( $res as $row ) {
606 $this->watched
[$row->page_namespace
][$row->page_title
] = true;
610 public function getAllowedParams() {
613 ApiBase
::PARAM_DFLT
=> null,
614 ApiBase
::PARAM_ISMULTI
=> true,
615 ApiBase
::PARAM_TYPE
=> array(
626 ApiBase
::PARAM_DFLT
=> null,
627 ApiBase
::PARAM_ISMULTI
=> true,
628 ApiBase
::PARAM_TYPE
=> array_keys( $this->getTokenFunctions() )
634 public function getParamDescription() {
637 'Which additional properties to get:',
638 ' protection - List the protection level of each page',
639 ' talkid - The page ID of the talk page for each non-talk page',
640 ' watched - List the watched status of each page',
641 ' subjectid - The page ID of the parent page for each talk page',
642 ' url - Gives a full URL to the page, and also an edit URL',
643 ' readable - Whether the user can read this page',
644 ' preload - Gives the text returned by EditFormPreloadText',
645 ' displaytitle - Gives the way the page title is actually displayed',
647 'token' => 'Request a token to perform a data-modifying action on a page',
648 'continue' => 'When more results are available, use this to continue',
652 public function getDescription() {
653 return 'Get basic page information such as namespace, title, last touched date, ...';
656 public function getPossibleErrors() {
657 return array_merge( parent
::getPossibleErrors(), array(
658 array( 'code' => '_badcontinue', 'info' => 'Invalid continue param. You should pass the original value returned by the previous query' ),
662 protected function getExamples() {
664 'api.php?action=query&prop=info&titles=Main%20Page',
665 'api.php?action=query&prop=info&inprop=protection&titles=Main%20Page'
669 public function getVersion() {
670 return __CLASS__
. ': $Id$';