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 $db = $this->getDB();
251 // Get protection info if requested
252 if ( $this->fld_protection
) {
253 $this->getProtectionInfo();
256 if ( $this->fld_watched
) {
257 $this->getMain()->setVaryCookie();
258 $this->getWatchedInfo();
261 // Run the talkid/subjectid query if requested
262 if ( $this->fld_talkid ||
$this->fld_subjectid
) {
266 if ( $this->fld_displaytitle
) {
267 $this->getDisplayTitle();
270 foreach ( $this->everything
as $pageid => $title ) {
271 $pageInfo = $this->extractPageInfo( $pageid, $title );
272 $fit = $result->addValue( array(
275 ), $pageid, $pageInfo );
277 $this->setContinueEnumParameter( 'continue',
278 $title->getNamespace() . '|' .
286 * Get a result array with information about a title
287 * @param $pageid int Page ID (negative for missing titles)
288 * @param $title Title object
291 private function extractPageInfo( $pageid, $title ) {
293 if ( $title->exists() ) {
294 $pageInfo['touched'] = wfTimestamp( TS_ISO_8601
, $this->pageTouched
[$pageid] );
295 $pageInfo['lastrevid'] = intval( $this->pageLatest
[$pageid] );
296 $pageInfo['counter'] = intval( $this->pageCounter
[$pageid] );
297 $pageInfo['length'] = intval( $this->pageLength
[$pageid] );
299 if ( $this->pageIsRedir
[$pageid] ) {
300 $pageInfo['redirect'] = '';
302 if ( $this->pageIsNew
[$pageid] ) {
303 $pageInfo['new'] = '';
307 if ( !is_null( $this->params
['token'] ) ) {
308 // Don't cache tokens
309 $this->getMain()->setCachePrivate();
311 $tokenFunctions = $this->getTokenFunctions();
312 $pageInfo['starttimestamp'] = wfTimestamp( TS_ISO_8601
, time() );
313 foreach ( $this->params
['token'] as $t ) {
314 $val = call_user_func( $tokenFunctions[$t], $pageid, $title );
315 if ( $val === false ) {
316 $this->setWarning( "Action '$t' is not allowed for the current user" );
318 $pageInfo[$t . 'token'] = $val;
323 if ( $this->fld_protection
) {
324 $pageInfo['protection'] = array();
325 if ( isset( $this->protections
[$title->getNamespace()][$title->getDBkey()] ) ) {
326 $pageInfo['protection'] =
327 $this->protections
[$title->getNamespace()][$title->getDBkey()];
329 $this->getResult()->setIndexedTagName( $pageInfo['protection'], 'pr' );
332 if ( $this->fld_watched
&& isset( $this->watched
[$title->getNamespace()][$title->getDBkey()] ) ) {
333 $pageInfo['watched'] = '';
336 if ( $this->fld_talkid
&& isset( $this->talkids
[$title->getNamespace()][$title->getDBkey()] ) ) {
337 $pageInfo['talkid'] = $this->talkids
[$title->getNamespace()][$title->getDBkey()];
340 if ( $this->fld_subjectid
&& isset( $this->subjectids
[$title->getNamespace()][$title->getDBkey()] ) ) {
341 $pageInfo['subjectid'] = $this->subjectids
[$title->getNamespace()][$title->getDBkey()];
344 if ( $this->fld_url
) {
345 $pageInfo['fullurl'] = $title->getFullURL();
346 $pageInfo['editurl'] = $title->getFullURL( 'action=edit' );
348 if ( $this->fld_readable
&& $title->userCanRead() ) {
349 $pageInfo['readable'] = '';
352 if ( $this->fld_preload
) {
353 if ( $title->exists() ) {
354 $pageInfo['preload'] = '';
357 wfRunHooks( 'EditFormPreloadText', array( &$text, &$title ) );
359 $pageInfo['preload'] = $text;
363 if ( $this->fld_displaytitle
) {
364 if ( isset( $this->displaytitles
[$title->getArticleId()] ) ) {
365 $pageInfo['displaytitle'] = $this->displaytitles
[$title->getArticleId()];
367 $pageInfo['displaytitle'] = $title->getPrefixedText();
375 * Get information about protections and put it in $protections
377 private function getProtectionInfo() {
378 $this->protections
= array();
379 $db = $this->getDB();
381 // Get normal protections for existing titles
382 if ( count( $this->titles
) ) {
383 $this->resetQueryParams();
384 $this->addTables( array( 'page_restrictions', 'page' ) );
385 $this->addWhere( 'page_id=pr_page' );
386 $this->addFields( array( 'pr_page', 'pr_type', 'pr_level',
387 'pr_expiry', 'pr_cascade', 'page_namespace',
389 $this->addWhereFld( 'pr_page', array_keys( $this->titles
) );
391 $res = $this->select( __METHOD__
);
392 foreach ( $res as $row ) {
394 'type' => $row->pr_type
,
395 'level' => $row->pr_level
,
396 'expiry' => Block
::decodeExpiry( $row->pr_expiry
, TS_ISO_8601
)
398 if ( $row->pr_cascade
) {
401 $this->protections
[$row->page_namespace
][$row->page_title
][] = $a;
403 // Also check old restrictions
404 if ( $this->pageRestrictions
[$row->pr_page
] ) {
405 $restrictions = explode( ':', trim( $this->pageRestrictions
[$row->pr_page
] ) );
406 foreach ( $restrictions as $restrict ) {
407 $temp = explode( '=', trim( $restrict ) );
408 if ( count( $temp ) == 1 ) {
409 // old old format should be treated as edit/move restriction
410 $restriction = trim( $temp[0] );
412 if ( $restriction == '' ) {
415 $this->protections
[$row->page_namespace
][$row->page_title
][] = array(
417 'level' => $restriction,
418 'expiry' => 'infinity',
420 $this->protections
[$row->page_namespace
][$row->page_title
][] = array(
422 'level' => $restriction,
423 'expiry' => 'infinity',
426 $restriction = trim( $temp[1] );
427 if ( $restriction == '' ) {
430 $this->protections
[$row->page_namespace
][$row->page_title
][] = array(
432 'level' => $restriction,
433 'expiry' => 'infinity',
441 // Get protections for missing titles
442 if ( count( $this->missing
) ) {
443 $this->resetQueryParams();
444 $lb = new LinkBatch( $this->missing
);
445 $this->addTables( 'protected_titles' );
446 $this->addFields( array( 'pt_title', 'pt_namespace', 'pt_create_perm', 'pt_expiry' ) );
447 $this->addWhere( $lb->constructSet( 'pt', $db ) );
448 $res = $this->select( __METHOD__
);
449 foreach ( $res as $row ) {
450 $this->protections
[$row->pt_namespace
][$row->pt_title
][] = array(
452 'level' => $row->pt_create_perm
,
453 'expiry' => Block
::decodeExpiry( $row->pt_expiry
, TS_ISO_8601
)
458 // Cascading protections
459 $images = $others = array();
460 foreach ( $this->everything
as $title ) {
461 if ( $title->getNamespace() == NS_FILE
) {
462 $images[] = $title->getDBkey();
468 if ( count( $others ) ) {
469 // Non-images: check templatelinks
470 $lb = new LinkBatch( $others );
471 $this->resetQueryParams();
472 $this->addTables( array( 'page_restrictions', 'page', 'templatelinks' ) );
473 $this->addFields( array( 'pr_type', 'pr_level', 'pr_expiry',
474 'page_title', 'page_namespace',
475 'tl_title', 'tl_namespace' ) );
476 $this->addWhere( $lb->constructSet( 'tl', $db ) );
477 $this->addWhere( 'pr_page = page_id' );
478 $this->addWhere( 'pr_page = tl_from' );
479 $this->addWhereFld( 'pr_cascade', 1 );
481 $res = $this->select( __METHOD__
);
482 foreach ( $res as $row ) {
483 $source = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
484 $this->protections
[$row->tl_namespace
][$row->tl_title
][] = array(
485 'type' => $row->pr_type
,
486 'level' => $row->pr_level
,
487 'expiry' => Block
::decodeExpiry( $row->pr_expiry
, TS_ISO_8601
),
488 'source' => $source->getPrefixedText()
493 if ( count( $images ) ) {
494 // Images: check imagelinks
495 $this->resetQueryParams();
496 $this->addTables( array( 'page_restrictions', 'page', 'imagelinks' ) );
497 $this->addFields( array( 'pr_type', 'pr_level', 'pr_expiry',
498 'page_title', 'page_namespace', 'il_to' ) );
499 $this->addWhere( 'pr_page = page_id' );
500 $this->addWhere( 'pr_page = il_from' );
501 $this->addWhereFld( 'pr_cascade', 1 );
502 $this->addWhereFld( 'il_to', $images );
504 $res = $this->select( __METHOD__
);
505 foreach ( $res as $row ) {
506 $source = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
507 $this->protections
[NS_FILE
][$row->il_to
][] = array(
508 'type' => $row->pr_type
,
509 'level' => $row->pr_level
,
510 'expiry' => Block
::decodeExpiry( $row->pr_expiry
, TS_ISO_8601
),
511 'source' => $source->getPrefixedText()
518 * Get talk page IDs (if requested) and subject page IDs (if requested)
519 * and put them in $talkids and $subjectids
521 private function getTSIDs() {
522 $getTitles = $this->talkids
= $this->subjectids
= array();
524 foreach ( $this->everything
as $t ) {
525 if ( MWNamespace
::isTalk( $t->getNamespace() ) ) {
526 if ( $this->fld_subjectid
) {
527 $getTitles[] = $t->getSubjectPage();
529 } elseif ( $this->fld_talkid
) {
530 $getTitles[] = $t->getTalkPage();
533 if ( !count( $getTitles ) ) {
537 $db = $this->getDB();
539 // Construct a custom WHERE clause that matches
540 // all titles in $getTitles
541 $lb = new LinkBatch( $getTitles );
542 $this->resetQueryParams();
543 $this->addTables( 'page' );
544 $this->addFields( array( 'page_title', 'page_namespace', 'page_id' ) );
545 $this->addWhere( $lb->constructSet( 'page', $db ) );
546 $res = $this->select( __METHOD__
);
547 foreach ( $res as $row ) {
548 if ( MWNamespace
::isTalk( $row->page_namespace
) ) {
549 $this->talkids
[MWNamespace
::getSubject( $row->page_namespace
)][$row->page_title
] =
550 intval( $row->page_id
);
552 $this->subjectids
[MWNamespace
::getTalk( $row->page_namespace
)][$row->page_title
] =
553 intval( $row->page_id
);
558 private function getDisplayTitle() {
559 $this->displaytitles
= array();
561 $pageIds = array_keys( $this->titles
);
563 if ( !count( $pageIds ) ) {
567 $db = $this->getDB();
569 $this->resetQueryParams();
570 $this->addTables( 'page_props' );
571 $this->addFields( array( 'pp_page', 'pp_value' ) );
572 $this->addWhereFld( 'pp_page', $pageIds );
573 $this->addWhereFld( 'pp_propname', 'displaytitle' );
574 $res = $this->select( __METHOD__
);
576 foreach ( $res as $row ) {
577 $this->displaytitles
[$row->pp_page
] = $row->pp_value
;
582 * Get information about watched status and put it in $this->watched
584 private function getWatchedInfo() {
587 if ( $wgUser->isAnon() ||
count( $this->titles
) == 0 ) {
591 $this->watched
= array();
592 $db = $this->getDB();
594 $lb = new LinkBatch( $this->titles
);
596 $this->resetQueryParams();
597 $this->addTables( array( 'page', 'watchlist' ) );
598 $this->addFields( array( 'page_title', 'page_namespace' ) );
599 $this->addWhere( array(
600 $lb->constructSet( 'page', $db ),
601 'wl_namespace=page_namespace',
602 'wl_title=page_title',
603 'wl_user' => $wgUser->getID()
606 $res = $this->select( __METHOD__
);
608 foreach ( $res as $row ) {
609 $this->watched
[$row->page_namespace
][$row->page_title
] = true;
613 public function getAllowedParams() {
616 ApiBase
::PARAM_DFLT
=> null,
617 ApiBase
::PARAM_ISMULTI
=> true,
618 ApiBase
::PARAM_TYPE
=> array(
629 ApiBase
::PARAM_DFLT
=> null,
630 ApiBase
::PARAM_ISMULTI
=> true,
631 ApiBase
::PARAM_TYPE
=> array_keys( $this->getTokenFunctions() )
637 public function getParamDescription() {
640 'Which additional properties to get:',
641 ' protection - List the protection level of each page',
642 ' talkid - The page ID of the talk page for each non-talk page',
643 ' watched - List the watched status of each page',
644 ' subjectid - The page ID of the parent page for each talk page',
645 ' url - Gives a full URL to the page, and also an edit URL',
646 ' readable - Whether the user can read this page',
647 ' preload - Gives the text returned by EditFormPreloadText',
648 ' displaytitle - Gives the way the page title is actually displayed',
650 'token' => 'Request a token to perform a data-modifying action on a page',
651 'continue' => 'When more results are available, use this to continue',
655 public function getDescription() {
656 return 'Get basic page information such as namespace, title, last touched date, ...';
659 public function getPossibleErrors() {
660 return array_merge( parent
::getPossibleErrors(), array(
661 array( 'code' => '_badcontinue', 'info' => 'Invalid continue param. You should pass the original value returned by the previous query' ),
665 protected function getExamples() {
667 'api.php?action=query&prop=info&titles=Main%20Page',
668 'api.php?action=query&prop=info&inprop=protection&titles=Main%20Page'
672 public function getVersion() {
673 return __CLASS__
. ': $Id$';