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 $params, $titles, $missing, $everything, $pageCounter;
46 private $pageRestrictions, $pageIsRedir, $pageIsNew, $pageTouched,
47 $pageLatest, $pageLength;
49 private $protections, $watched, $talkids, $subjectids, $displaytitles;
51 private $tokenFunctions;
53 public function __construct( $query, $moduleName ) {
54 parent
::__construct( $query, $moduleName, 'in' );
58 * @param $pageSet ApiPageSet
61 public function requestExtraData( $pageSet ) {
62 global $wgDisableCounters;
64 $pageSet->requestField( 'page_restrictions' );
65 $pageSet->requestField( 'page_is_redirect' );
66 $pageSet->requestField( 'page_is_new' );
67 if ( !$wgDisableCounters ) {
68 $pageSet->requestField( 'page_counter' );
70 $pageSet->requestField( 'page_touched' );
71 $pageSet->requestField( 'page_latest' );
72 $pageSet->requestField( 'page_len' );
76 * Get an array mapping token names to their handler functions.
77 * The prototype for a token function is func($pageid, $title)
78 * it should return a token or false (permission denied)
79 * @return array(tokenname => function)
81 protected function getTokenFunctions() {
82 // Don't call the hooks twice
83 if ( isset( $this->tokenFunctions
) ) {
84 return $this->tokenFunctions
;
87 // If we're in JSON callback mode, no tokens can be obtained
88 if ( !is_null( $this->getMain()->getRequest()->getVal( 'callback' ) ) ) {
92 $this->tokenFunctions
= array(
93 'edit' => array( 'ApiQueryInfo', 'getEditToken' ),
94 'delete' => array( 'ApiQueryInfo', 'getDeleteToken' ),
95 'protect' => array( 'ApiQueryInfo', 'getProtectToken' ),
96 'move' => array( 'ApiQueryInfo', 'getMoveToken' ),
97 'block' => array( 'ApiQueryInfo', 'getBlockToken' ),
98 'unblock' => array( 'ApiQueryInfo', 'getUnblockToken' ),
99 'email' => array( 'ApiQueryInfo', 'getEmailToken' ),
100 'import' => array( 'ApiQueryInfo', 'getImportToken' ),
101 'watch' => array( 'ApiQueryInfo', 'getWatchToken'),
103 wfRunHooks( 'APIQueryInfoTokens', array( &$this->tokenFunctions
) );
104 return $this->tokenFunctions
;
107 public static function getEditToken( $pageid, $title ) {
108 // We could check for $title->userCan('edit') here,
109 // but that's too expensive for this purpose
110 // and would break caching
112 if ( !$wgUser->isAllowed( 'edit' ) ) {
116 // The edit token is always the same, let's exploit that
117 static $cachedEditToken = null;
118 if ( !is_null( $cachedEditToken ) ) {
119 return $cachedEditToken;
122 $cachedEditToken = $wgUser->editToken();
123 return $cachedEditToken;
126 public static function getDeleteToken( $pageid, $title ) {
128 if ( !$wgUser->isAllowed( 'delete' ) ) {
132 static $cachedDeleteToken = null;
133 if ( !is_null( $cachedDeleteToken ) ) {
134 return $cachedDeleteToken;
137 $cachedDeleteToken = $wgUser->editToken();
138 return $cachedDeleteToken;
141 public static function getProtectToken( $pageid, $title ) {
143 if ( !$wgUser->isAllowed( 'protect' ) ) {
147 static $cachedProtectToken = null;
148 if ( !is_null( $cachedProtectToken ) ) {
149 return $cachedProtectToken;
152 $cachedProtectToken = $wgUser->editToken();
153 return $cachedProtectToken;
156 public static function getMoveToken( $pageid, $title ) {
158 if ( !$wgUser->isAllowed( 'move' ) ) {
162 static $cachedMoveToken = null;
163 if ( !is_null( $cachedMoveToken ) ) {
164 return $cachedMoveToken;
167 $cachedMoveToken = $wgUser->editToken();
168 return $cachedMoveToken;
171 public static function getBlockToken( $pageid, $title ) {
173 if ( !$wgUser->isAllowed( 'block' ) ) {
177 static $cachedBlockToken = null;
178 if ( !is_null( $cachedBlockToken ) ) {
179 return $cachedBlockToken;
182 $cachedBlockToken = $wgUser->editToken();
183 return $cachedBlockToken;
186 public static function getUnblockToken( $pageid, $title ) {
187 // Currently, this is exactly the same as the block token
188 return self
::getBlockToken( $pageid, $title );
191 public static function getEmailToken( $pageid, $title ) {
193 if ( !$wgUser->canSendEmail() ||
$wgUser->isBlockedFromEmailUser() ) {
197 static $cachedEmailToken = null;
198 if ( !is_null( $cachedEmailToken ) ) {
199 return $cachedEmailToken;
202 $cachedEmailToken = $wgUser->editToken();
203 return $cachedEmailToken;
206 public static function getImportToken( $pageid, $title ) {
208 if ( !$wgUser->isAllowedAny( 'import', 'importupload' ) ) {
212 static $cachedImportToken = null;
213 if ( !is_null( $cachedImportToken ) ) {
214 return $cachedImportToken;
217 $cachedImportToken = $wgUser->editToken();
218 return $cachedImportToken;
221 public static function getWatchToken( $pageid, $title ) {
223 if ( !$wgUser->isLoggedIn() ) {
227 static $cachedWatchToken = null;
228 if ( !is_null( $cachedWatchToken ) ) {
229 return $cachedWatchToken;
232 $cachedWatchToken = $wgUser->editToken( 'watch' );
233 return $cachedWatchToken;
236 public function execute() {
237 $this->params
= $this->extractRequestParams();
238 if ( !is_null( $this->params
['prop'] ) ) {
239 $prop = array_flip( $this->params
['prop'] );
240 $this->fld_protection
= isset( $prop['protection'] );
241 $this->fld_watched
= isset( $prop['watched'] );
242 $this->fld_talkid
= isset( $prop['talkid'] );
243 $this->fld_subjectid
= isset( $prop['subjectid'] );
244 $this->fld_url
= isset( $prop['url'] );
245 $this->fld_readable
= isset( $prop['readable'] );
246 $this->fld_preload
= isset( $prop['preload'] );
247 $this->fld_displaytitle
= isset( $prop['displaytitle'] );
250 $pageSet = $this->getPageSet();
251 $this->titles
= $pageSet->getGoodTitles();
252 $this->missing
= $pageSet->getMissingTitles();
253 $this->everything
= $this->titles +
$this->missing
;
254 $result = $this->getResult();
256 uasort( $this->everything
, array( 'Title', 'compare' ) );
257 if ( !is_null( $this->params
['continue'] ) ) {
258 // Throw away any titles we're gonna skip so they don't
260 $cont = explode( '|', $this->params
['continue'] );
261 if ( count( $cont ) != 2 ) {
262 $this->dieUsage( 'Invalid continue param. You should pass the original ' .
263 'value returned by the previous query', '_badcontinue' );
265 $conttitle = Title
::makeTitleSafe( $cont[0], $cont[1] );
266 foreach ( $this->everything
as $pageid => $title ) {
267 if ( Title
::compare( $title, $conttitle ) >= 0 ) {
270 unset( $this->titles
[$pageid] );
271 unset( $this->missing
[$pageid] );
272 unset( $this->everything
[$pageid] );
276 $this->pageRestrictions
= $pageSet->getCustomField( 'page_restrictions' );
277 $this->pageIsRedir
= $pageSet->getCustomField( 'page_is_redirect' );
278 $this->pageIsNew
= $pageSet->getCustomField( 'page_is_new' );
280 global $wgDisableCounters;
282 if ( !$wgDisableCounters ) {
283 $this->pageCounter
= $pageSet->getCustomField( 'page_counter' );
285 $this->pageTouched
= $pageSet->getCustomField( 'page_touched' );
286 $this->pageLatest
= $pageSet->getCustomField( 'page_latest' );
287 $this->pageLength
= $pageSet->getCustomField( 'page_len' );
289 // Get protection info if requested
290 if ( $this->fld_protection
) {
291 $this->getProtectionInfo();
294 if ( $this->fld_watched
) {
295 $this->getWatchedInfo();
298 // Run the talkid/subjectid query if requested
299 if ( $this->fld_talkid ||
$this->fld_subjectid
) {
303 if ( $this->fld_displaytitle
) {
304 $this->getDisplayTitle();
307 foreach ( $this->everything
as $pageid => $title ) {
308 $pageInfo = $this->extractPageInfo( $pageid, $title );
309 $fit = $result->addValue( array(
312 ), $pageid, $pageInfo );
314 $this->setContinueEnumParameter( 'continue',
315 $title->getNamespace() . '|' .
323 * Get a result array with information about a title
324 * @param $pageid int Page ID (negative for missing titles)
325 * @param $title Title object
328 private function extractPageInfo( $pageid, $title ) {
330 if ( $title->exists() ) {
331 global $wgDisableCounters;
333 $pageInfo['touched'] = wfTimestamp( TS_ISO_8601
, $this->pageTouched
[$pageid] );
334 $pageInfo['lastrevid'] = intval( $this->pageLatest
[$pageid] );
335 $pageInfo['counter'] = $wgDisableCounters
337 : intval( $this->pageCounter
[$pageid] );
338 $pageInfo['length'] = intval( $this->pageLength
[$pageid] );
340 if ( $this->pageIsRedir
[$pageid] ) {
341 $pageInfo['redirect'] = '';
343 if ( $this->pageIsNew
[$pageid] ) {
344 $pageInfo['new'] = '';
348 if ( !is_null( $this->params
['token'] ) ) {
349 $tokenFunctions = $this->getTokenFunctions();
350 $pageInfo['starttimestamp'] = wfTimestamp( TS_ISO_8601
, time() );
351 foreach ( $this->params
['token'] as $t ) {
352 $val = call_user_func( $tokenFunctions[$t], $pageid, $title );
353 if ( $val === false ) {
354 $this->setWarning( "Action '$t' is not allowed for the current user" );
356 $pageInfo[$t . 'token'] = $val;
361 if ( $this->fld_protection
) {
362 $pageInfo['protection'] = array();
363 if ( isset( $this->protections
[$title->getNamespace()][$title->getDBkey()] ) ) {
364 $pageInfo['protection'] =
365 $this->protections
[$title->getNamespace()][$title->getDBkey()];
367 $this->getResult()->setIndexedTagName( $pageInfo['protection'], 'pr' );
370 if ( $this->fld_watched
&& isset( $this->watched
[$title->getNamespace()][$title->getDBkey()] ) ) {
371 $pageInfo['watched'] = '';
374 if ( $this->fld_talkid
&& isset( $this->talkids
[$title->getNamespace()][$title->getDBkey()] ) ) {
375 $pageInfo['talkid'] = $this->talkids
[$title->getNamespace()][$title->getDBkey()];
378 if ( $this->fld_subjectid
&& isset( $this->subjectids
[$title->getNamespace()][$title->getDBkey()] ) ) {
379 $pageInfo['subjectid'] = $this->subjectids
[$title->getNamespace()][$title->getDBkey()];
382 if ( $this->fld_url
) {
383 $pageInfo['fullurl'] = $title->getFullURL();
384 $pageInfo['editurl'] = $title->getFullURL( 'action=edit' );
386 if ( $this->fld_readable
&& $title->userCanRead() ) {
387 $pageInfo['readable'] = '';
390 if ( $this->fld_preload
) {
391 if ( $title->exists() ) {
392 $pageInfo['preload'] = '';
395 wfRunHooks( 'EditFormPreloadText', array( &$text, &$title ) );
397 $pageInfo['preload'] = $text;
401 if ( $this->fld_displaytitle
) {
402 if ( isset( $this->displaytitles
[$title->getArticleId()] ) ) {
403 $pageInfo['displaytitle'] = $this->displaytitles
[$title->getArticleId()];
405 $pageInfo['displaytitle'] = $title->getPrefixedText();
413 * Get information about protections and put it in $protections
415 private function getProtectionInfo() {
417 $this->protections
= array();
418 $db = $this->getDB();
420 // Get normal protections for existing titles
421 if ( count( $this->titles
) ) {
422 $this->resetQueryParams();
423 $this->addTables( array( 'page_restrictions', 'page' ) );
424 $this->addWhere( 'page_id=pr_page' );
425 $this->addFields( array( 'pr_page', 'pr_type', 'pr_level',
426 'pr_expiry', 'pr_cascade', 'page_namespace',
428 $this->addWhereFld( 'pr_page', array_keys( $this->titles
) );
430 $res = $this->select( __METHOD__
);
431 foreach ( $res as $row ) {
433 'type' => $row->pr_type
,
434 'level' => $row->pr_level
,
435 'expiry' => $wgContLang->formatExpiry( $row->pr_expiry
, TS_ISO_8601
)
437 if ( $row->pr_cascade
) {
440 $this->protections
[$row->page_namespace
][$row->page_title
][] = $a;
442 // Also check old restrictions
443 if ( $this->pageRestrictions
[$row->pr_page
] ) {
444 $restrictions = explode( ':', trim( $this->pageRestrictions
[$row->pr_page
] ) );
445 foreach ( $restrictions as $restrict ) {
446 $temp = explode( '=', trim( $restrict ) );
447 if ( count( $temp ) == 1 ) {
448 // old old format should be treated as edit/move restriction
449 $restriction = trim( $temp[0] );
451 if ( $restriction == '' ) {
454 $this->protections
[$row->page_namespace
][$row->page_title
][] = array(
456 'level' => $restriction,
457 'expiry' => 'infinity',
459 $this->protections
[$row->page_namespace
][$row->page_title
][] = array(
461 'level' => $restriction,
462 'expiry' => 'infinity',
465 $restriction = trim( $temp[1] );
466 if ( $restriction == '' ) {
469 $this->protections
[$row->page_namespace
][$row->page_title
][] = array(
471 'level' => $restriction,
472 'expiry' => 'infinity',
480 // Get protections for missing titles
481 if ( count( $this->missing
) ) {
482 $this->resetQueryParams();
483 $lb = new LinkBatch( $this->missing
);
484 $this->addTables( 'protected_titles' );
485 $this->addFields( array( 'pt_title', 'pt_namespace', 'pt_create_perm', 'pt_expiry' ) );
486 $this->addWhere( $lb->constructSet( 'pt', $db ) );
487 $res = $this->select( __METHOD__
);
488 foreach ( $res as $row ) {
489 $this->protections
[$row->pt_namespace
][$row->pt_title
][] = array(
491 'level' => $row->pt_create_perm
,
492 'expiry' => $wgContLang->formatExpiry( $row->pt_expiry
, TS_ISO_8601
)
497 // Cascading protections
498 $images = $others = array();
499 foreach ( $this->everything
as $title ) {
500 if ( $title->getNamespace() == NS_FILE
) {
501 $images[] = $title->getDBkey();
507 if ( count( $others ) ) {
508 // Non-images: check templatelinks
509 $lb = new LinkBatch( $others );
510 $this->resetQueryParams();
511 $this->addTables( array( 'page_restrictions', 'page', 'templatelinks' ) );
512 $this->addFields( array( 'pr_type', 'pr_level', 'pr_expiry',
513 'page_title', 'page_namespace',
514 'tl_title', 'tl_namespace' ) );
515 $this->addWhere( $lb->constructSet( 'tl', $db ) );
516 $this->addWhere( 'pr_page = page_id' );
517 $this->addWhere( 'pr_page = tl_from' );
518 $this->addWhereFld( 'pr_cascade', 1 );
520 $res = $this->select( __METHOD__
);
521 foreach ( $res as $row ) {
522 $source = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
523 $this->protections
[$row->tl_namespace
][$row->tl_title
][] = array(
524 'type' => $row->pr_type
,
525 'level' => $row->pr_level
,
526 'expiry' => $wgContLang->formatExpiry( $row->pr_expiry
, TS_ISO_8601
),
527 'source' => $source->getPrefixedText()
532 if ( count( $images ) ) {
533 // Images: check imagelinks
534 $this->resetQueryParams();
535 $this->addTables( array( 'page_restrictions', 'page', 'imagelinks' ) );
536 $this->addFields( array( 'pr_type', 'pr_level', 'pr_expiry',
537 'page_title', 'page_namespace', 'il_to' ) );
538 $this->addWhere( 'pr_page = page_id' );
539 $this->addWhere( 'pr_page = il_from' );
540 $this->addWhereFld( 'pr_cascade', 1 );
541 $this->addWhereFld( 'il_to', $images );
543 $res = $this->select( __METHOD__
);
544 foreach ( $res as $row ) {
545 $source = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
546 $this->protections
[NS_FILE
][$row->il_to
][] = array(
547 'type' => $row->pr_type
,
548 'level' => $row->pr_level
,
549 'expiry' => $wgContLang->formatExpiry( $row->pr_expiry
, TS_ISO_8601
),
550 'source' => $source->getPrefixedText()
557 * Get talk page IDs (if requested) and subject page IDs (if requested)
558 * and put them in $talkids and $subjectids
560 private function getTSIDs() {
561 $getTitles = $this->talkids
= $this->subjectids
= array();
563 foreach ( $this->everything
as $t ) {
564 if ( MWNamespace
::isTalk( $t->getNamespace() ) ) {
565 if ( $this->fld_subjectid
) {
566 $getTitles[] = $t->getSubjectPage();
568 } elseif ( $this->fld_talkid
) {
569 $getTitles[] = $t->getTalkPage();
572 if ( !count( $getTitles ) ) {
576 $db = $this->getDB();
578 // Construct a custom WHERE clause that matches
579 // all titles in $getTitles
580 $lb = new LinkBatch( $getTitles );
581 $this->resetQueryParams();
582 $this->addTables( 'page' );
583 $this->addFields( array( 'page_title', 'page_namespace', 'page_id' ) );
584 $this->addWhere( $lb->constructSet( 'page', $db ) );
585 $res = $this->select( __METHOD__
);
586 foreach ( $res as $row ) {
587 if ( MWNamespace
::isTalk( $row->page_namespace
) ) {
588 $this->talkids
[MWNamespace
::getSubject( $row->page_namespace
)][$row->page_title
] =
589 intval( $row->page_id
);
591 $this->subjectids
[MWNamespace
::getTalk( $row->page_namespace
)][$row->page_title
] =
592 intval( $row->page_id
);
597 private function getDisplayTitle() {
598 $this->displaytitles
= array();
600 $pageIds = array_keys( $this->titles
);
602 if ( !count( $pageIds ) ) {
606 $this->resetQueryParams();
607 $this->addTables( 'page_props' );
608 $this->addFields( array( 'pp_page', 'pp_value' ) );
609 $this->addWhereFld( 'pp_page', $pageIds );
610 $this->addWhereFld( 'pp_propname', 'displaytitle' );
611 $res = $this->select( __METHOD__
);
613 foreach ( $res as $row ) {
614 $this->displaytitles
[$row->pp_page
] = $row->pp_value
;
619 * Get information about watched status and put it in $this->watched
621 private function getWatchedInfo() {
624 if ( $wgUser->isAnon() ||
count( $this->everything
) == 0 ) {
628 $this->watched
= array();
629 $db = $this->getDB();
631 $lb = new LinkBatch( $this->everything
);
633 $this->resetQueryParams();
634 $this->addTables( array( 'watchlist' ) );
635 $this->addFields( array( 'wl_title', 'wl_namespace' ) );
636 $this->addWhere( array(
637 $lb->constructSet( 'wl', $db ),
638 'wl_user' => $wgUser->getID()
641 $res = $this->select( __METHOD__
);
643 foreach ( $res as $row ) {
644 $this->watched
[$row->wl_namespace
][$row->wl_title
] = true;
648 public function getCacheMode( $params ) {
649 $publicProps = array(
657 if ( !is_null( $params['prop'] ) ) {
658 foreach ( $params['prop'] as $prop ) {
659 if ( !in_array( $prop, $publicProps ) ) {
664 if ( !is_null( $params['token'] ) ) {
670 public function getAllowedParams() {
673 ApiBase
::PARAM_DFLT
=> null,
674 ApiBase
::PARAM_ISMULTI
=> true,
675 ApiBase
::PARAM_TYPE
=> array(
681 'readable', # private
684 // If you add more properties here, please consider whether they
685 // need to be added to getCacheMode()
688 ApiBase
::PARAM_DFLT
=> null,
689 ApiBase
::PARAM_ISMULTI
=> true,
690 ApiBase
::PARAM_TYPE
=> array_keys( $this->getTokenFunctions() )
696 public function getParamDescription() {
699 'Which additional properties to get:',
700 ' protection - List the protection level of each page',
701 ' talkid - The page ID of the talk page for each non-talk page',
702 ' watched - List the watched status of each page',
703 ' subjectid - The page ID of the parent page for each talk page',
704 ' url - Gives a full URL to the page, and also an edit URL',
705 ' readable - Whether the user can read this page',
706 ' preload - Gives the text returned by EditFormPreloadText',
707 ' displaytitle - Gives the way the page title is actually displayed',
709 'token' => 'Request a token to perform a data-modifying action on a page',
710 'continue' => 'When more results are available, use this to continue',
714 public function getDescription() {
715 return 'Get basic page information such as namespace, title, last touched date, ...';
718 public function getPossibleErrors() {
719 return array_merge( parent
::getPossibleErrors(), array(
720 array( 'code' => '_badcontinue', 'info' => 'Invalid continue param. You should pass the original value returned by the previous query' ),
724 protected function getExamples() {
726 'api.php?action=query&prop=info&titles=Main%20Page',
727 'api.php?action=query&prop=info&inprop=protection&titles=Main%20Page'
731 public function getVersion() {
732 return __CLASS__
. ': $Id$';