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' ),
102 wfRunHooks( 'APIQueryInfoTokens', array( &$this->tokenFunctions
) );
103 return $this->tokenFunctions
;
106 public static function getEditToken( $pageid, $title ) {
107 // We could check for $title->userCan('edit') here,
108 // but that's too expensive for this purpose
109 // and would break caching
111 if ( !$wgUser->isAllowed( 'edit' ) ) {
115 // The edit token is always the same, let's exploit that
116 static $cachedEditToken = null;
117 if ( !is_null( $cachedEditToken ) ) {
118 return $cachedEditToken;
121 $cachedEditToken = $wgUser->editToken();
122 return $cachedEditToken;
125 public static function getDeleteToken( $pageid, $title ) {
127 if ( !$wgUser->isAllowed( 'delete' ) ) {
131 static $cachedDeleteToken = null;
132 if ( !is_null( $cachedDeleteToken ) ) {
133 return $cachedDeleteToken;
136 $cachedDeleteToken = $wgUser->editToken();
137 return $cachedDeleteToken;
140 public static function getProtectToken( $pageid, $title ) {
142 if ( !$wgUser->isAllowed( 'protect' ) ) {
146 static $cachedProtectToken = null;
147 if ( !is_null( $cachedProtectToken ) ) {
148 return $cachedProtectToken;
151 $cachedProtectToken = $wgUser->editToken();
152 return $cachedProtectToken;
155 public static function getMoveToken( $pageid, $title ) {
157 if ( !$wgUser->isAllowed( 'move' ) ) {
161 static $cachedMoveToken = null;
162 if ( !is_null( $cachedMoveToken ) ) {
163 return $cachedMoveToken;
166 $cachedMoveToken = $wgUser->editToken();
167 return $cachedMoveToken;
170 public static function getBlockToken( $pageid, $title ) {
172 if ( !$wgUser->isAllowed( 'block' ) ) {
176 static $cachedBlockToken = null;
177 if ( !is_null( $cachedBlockToken ) ) {
178 return $cachedBlockToken;
181 $cachedBlockToken = $wgUser->editToken();
182 return $cachedBlockToken;
185 public static function getUnblockToken( $pageid, $title ) {
186 // Currently, this is exactly the same as the block token
187 return self
::getBlockToken( $pageid, $title );
190 public static function getEmailToken( $pageid, $title ) {
192 if ( !$wgUser->canSendEmail() ||
$wgUser->isBlockedFromEmailUser() ) {
196 static $cachedEmailToken = null;
197 if ( !is_null( $cachedEmailToken ) ) {
198 return $cachedEmailToken;
201 $cachedEmailToken = $wgUser->editToken();
202 return $cachedEmailToken;
205 public static function getImportToken( $pageid, $title ) {
207 if ( !$wgUser->isAllowedAny( 'import', 'importupload' ) ) {
211 static $cachedImportToken = null;
212 if ( !is_null( $cachedImportToken ) ) {
213 return $cachedImportToken;
216 $cachedImportToken = $wgUser->editToken();
217 return $cachedImportToken;
220 public function execute() {
221 $this->params
= $this->extractRequestParams();
222 if ( !is_null( $this->params
['prop'] ) ) {
223 $prop = array_flip( $this->params
['prop'] );
224 $this->fld_protection
= isset( $prop['protection'] );
225 $this->fld_watched
= isset( $prop['watched'] );
226 $this->fld_talkid
= isset( $prop['talkid'] );
227 $this->fld_subjectid
= isset( $prop['subjectid'] );
228 $this->fld_url
= isset( $prop['url'] );
229 $this->fld_readable
= isset( $prop['readable'] );
230 $this->fld_preload
= isset( $prop['preload'] );
231 $this->fld_displaytitle
= isset( $prop['displaytitle'] );
234 $pageSet = $this->getPageSet();
235 $this->titles
= $pageSet->getGoodTitles();
236 $this->missing
= $pageSet->getMissingTitles();
237 $this->everything
= $this->titles +
$this->missing
;
238 $result = $this->getResult();
240 uasort( $this->everything
, array( 'Title', 'compare' ) );
241 if ( !is_null( $this->params
['continue'] ) ) {
242 // Throw away any titles we're gonna skip so they don't
244 $cont = explode( '|', $this->params
['continue'] );
245 if ( count( $cont ) != 2 ) {
246 $this->dieUsage( 'Invalid continue param. You should pass the original ' .
247 'value returned by the previous query', '_badcontinue' );
249 $conttitle = Title
::makeTitleSafe( $cont[0], $cont[1] );
250 foreach ( $this->everything
as $pageid => $title ) {
251 if ( Title
::compare( $title, $conttitle ) >= 0 ) {
254 unset( $this->titles
[$pageid] );
255 unset( $this->missing
[$pageid] );
256 unset( $this->everything
[$pageid] );
260 $this->pageRestrictions
= $pageSet->getCustomField( 'page_restrictions' );
261 $this->pageIsRedir
= $pageSet->getCustomField( 'page_is_redirect' );
262 $this->pageIsNew
= $pageSet->getCustomField( 'page_is_new' );
264 global $wgDisableCounters;
266 if ( !$wgDisableCounters ) {
267 $this->pageCounter
= $pageSet->getCustomField( 'page_counter' );
269 $this->pageTouched
= $pageSet->getCustomField( 'page_touched' );
270 $this->pageLatest
= $pageSet->getCustomField( 'page_latest' );
271 $this->pageLength
= $pageSet->getCustomField( 'page_len' );
273 // Get protection info if requested
274 if ( $this->fld_protection
) {
275 $this->getProtectionInfo();
278 if ( $this->fld_watched
) {
279 $this->getWatchedInfo();
282 // Run the talkid/subjectid query if requested
283 if ( $this->fld_talkid ||
$this->fld_subjectid
) {
287 if ( $this->fld_displaytitle
) {
288 $this->getDisplayTitle();
291 foreach ( $this->everything
as $pageid => $title ) {
292 $pageInfo = $this->extractPageInfo( $pageid, $title );
293 $fit = $result->addValue( array(
296 ), $pageid, $pageInfo );
298 $this->setContinueEnumParameter( 'continue',
299 $title->getNamespace() . '|' .
307 * Get a result array with information about a title
308 * @param $pageid int Page ID (negative for missing titles)
309 * @param $title Title object
312 private function extractPageInfo( $pageid, $title ) {
314 if ( $title->exists() ) {
315 global $wgDisableCounters;
317 $pageInfo['touched'] = wfTimestamp( TS_ISO_8601
, $this->pageTouched
[$pageid] );
318 $pageInfo['lastrevid'] = intval( $this->pageLatest
[$pageid] );
319 $pageInfo['counter'] = $wgDisableCounters
321 : intval( $this->pageCounter
[$pageid] );
322 $pageInfo['length'] = intval( $this->pageLength
[$pageid] );
324 if ( $this->pageIsRedir
[$pageid] ) {
325 $pageInfo['redirect'] = '';
327 if ( $this->pageIsNew
[$pageid] ) {
328 $pageInfo['new'] = '';
332 if ( !is_null( $this->params
['token'] ) ) {
333 $tokenFunctions = $this->getTokenFunctions();
334 $pageInfo['starttimestamp'] = wfTimestamp( TS_ISO_8601
, time() );
335 foreach ( $this->params
['token'] as $t ) {
336 $val = call_user_func( $tokenFunctions[$t], $pageid, $title );
337 if ( $val === false ) {
338 $this->setWarning( "Action '$t' is not allowed for the current user" );
340 $pageInfo[$t . 'token'] = $val;
345 if ( $this->fld_protection
) {
346 $pageInfo['protection'] = array();
347 if ( isset( $this->protections
[$title->getNamespace()][$title->getDBkey()] ) ) {
348 $pageInfo['protection'] =
349 $this->protections
[$title->getNamespace()][$title->getDBkey()];
351 $this->getResult()->setIndexedTagName( $pageInfo['protection'], 'pr' );
354 if ( $this->fld_watched
&& isset( $this->watched
[$title->getNamespace()][$title->getDBkey()] ) ) {
355 $pageInfo['watched'] = '';
358 if ( $this->fld_talkid
&& isset( $this->talkids
[$title->getNamespace()][$title->getDBkey()] ) ) {
359 $pageInfo['talkid'] = $this->talkids
[$title->getNamespace()][$title->getDBkey()];
362 if ( $this->fld_subjectid
&& isset( $this->subjectids
[$title->getNamespace()][$title->getDBkey()] ) ) {
363 $pageInfo['subjectid'] = $this->subjectids
[$title->getNamespace()][$title->getDBkey()];
366 if ( $this->fld_url
) {
367 $pageInfo['fullurl'] = $title->getFullURL();
368 $pageInfo['editurl'] = $title->getFullURL( 'action=edit' );
370 if ( $this->fld_readable
&& $title->userCanRead() ) {
371 $pageInfo['readable'] = '';
374 if ( $this->fld_preload
) {
375 if ( $title->exists() ) {
376 $pageInfo['preload'] = '';
379 wfRunHooks( 'EditFormPreloadText', array( &$text, &$title ) );
381 $pageInfo['preload'] = $text;
385 if ( $this->fld_displaytitle
) {
386 if ( isset( $this->displaytitles
[$title->getArticleId()] ) ) {
387 $pageInfo['displaytitle'] = $this->displaytitles
[$title->getArticleId()];
389 $pageInfo['displaytitle'] = $title->getPrefixedText();
397 * Get information about protections and put it in $protections
399 private function getProtectionInfo() {
401 $this->protections
= array();
402 $db = $this->getDB();
404 // Get normal protections for existing titles
405 if ( count( $this->titles
) ) {
406 $this->resetQueryParams();
407 $this->addTables( array( 'page_restrictions', 'page' ) );
408 $this->addWhere( 'page_id=pr_page' );
409 $this->addFields( array( 'pr_page', 'pr_type', 'pr_level',
410 'pr_expiry', 'pr_cascade', 'page_namespace',
412 $this->addWhereFld( 'pr_page', array_keys( $this->titles
) );
414 $res = $this->select( __METHOD__
);
415 foreach ( $res as $row ) {
417 'type' => $row->pr_type
,
418 'level' => $row->pr_level
,
419 'expiry' => $wgContLang->formatExpiry( $row->pr_expiry
, TS_ISO_8601
)
421 if ( $row->pr_cascade
) {
424 $this->protections
[$row->page_namespace
][$row->page_title
][] = $a;
426 // Also check old restrictions
427 if ( $this->pageRestrictions
[$row->pr_page
] ) {
428 $restrictions = explode( ':', trim( $this->pageRestrictions
[$row->pr_page
] ) );
429 foreach ( $restrictions as $restrict ) {
430 $temp = explode( '=', trim( $restrict ) );
431 if ( count( $temp ) == 1 ) {
432 // old old format should be treated as edit/move restriction
433 $restriction = trim( $temp[0] );
435 if ( $restriction == '' ) {
438 $this->protections
[$row->page_namespace
][$row->page_title
][] = array(
440 'level' => $restriction,
441 'expiry' => 'infinity',
443 $this->protections
[$row->page_namespace
][$row->page_title
][] = array(
445 'level' => $restriction,
446 'expiry' => 'infinity',
449 $restriction = trim( $temp[1] );
450 if ( $restriction == '' ) {
453 $this->protections
[$row->page_namespace
][$row->page_title
][] = array(
455 'level' => $restriction,
456 'expiry' => 'infinity',
464 // Get protections for missing titles
465 if ( count( $this->missing
) ) {
466 $this->resetQueryParams();
467 $lb = new LinkBatch( $this->missing
);
468 $this->addTables( 'protected_titles' );
469 $this->addFields( array( 'pt_title', 'pt_namespace', 'pt_create_perm', 'pt_expiry' ) );
470 $this->addWhere( $lb->constructSet( 'pt', $db ) );
471 $res = $this->select( __METHOD__
);
472 foreach ( $res as $row ) {
473 $this->protections
[$row->pt_namespace
][$row->pt_title
][] = array(
475 'level' => $row->pt_create_perm
,
476 'expiry' => $wgContLang->formatExpiry( $row->pt_expiry
, TS_ISO_8601
)
481 // Cascading protections
482 $images = $others = array();
483 foreach ( $this->everything
as $title ) {
484 if ( $title->getNamespace() == NS_FILE
) {
485 $images[] = $title->getDBkey();
491 if ( count( $others ) ) {
492 // Non-images: check templatelinks
493 $lb = new LinkBatch( $others );
494 $this->resetQueryParams();
495 $this->addTables( array( 'page_restrictions', 'page', 'templatelinks' ) );
496 $this->addFields( array( 'pr_type', 'pr_level', 'pr_expiry',
497 'page_title', 'page_namespace',
498 'tl_title', 'tl_namespace' ) );
499 $this->addWhere( $lb->constructSet( 'tl', $db ) );
500 $this->addWhere( 'pr_page = page_id' );
501 $this->addWhere( 'pr_page = tl_from' );
502 $this->addWhereFld( 'pr_cascade', 1 );
504 $res = $this->select( __METHOD__
);
505 foreach ( $res as $row ) {
506 $source = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
507 $this->protections
[$row->tl_namespace
][$row->tl_title
][] = array(
508 'type' => $row->pr_type
,
509 'level' => $row->pr_level
,
510 'expiry' => $wgContLang->formatExpiry( $row->pr_expiry
, TS_ISO_8601
),
511 'source' => $source->getPrefixedText()
516 if ( count( $images ) ) {
517 // Images: check imagelinks
518 $this->resetQueryParams();
519 $this->addTables( array( 'page_restrictions', 'page', 'imagelinks' ) );
520 $this->addFields( array( 'pr_type', 'pr_level', 'pr_expiry',
521 'page_title', 'page_namespace', 'il_to' ) );
522 $this->addWhere( 'pr_page = page_id' );
523 $this->addWhere( 'pr_page = il_from' );
524 $this->addWhereFld( 'pr_cascade', 1 );
525 $this->addWhereFld( 'il_to', $images );
527 $res = $this->select( __METHOD__
);
528 foreach ( $res as $row ) {
529 $source = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
530 $this->protections
[NS_FILE
][$row->il_to
][] = array(
531 'type' => $row->pr_type
,
532 'level' => $row->pr_level
,
533 'expiry' => $wgContLang->formatExpiry( $row->pr_expiry
, TS_ISO_8601
),
534 'source' => $source->getPrefixedText()
541 * Get talk page IDs (if requested) and subject page IDs (if requested)
542 * and put them in $talkids and $subjectids
544 private function getTSIDs() {
545 $getTitles = $this->talkids
= $this->subjectids
= array();
547 foreach ( $this->everything
as $t ) {
548 if ( MWNamespace
::isTalk( $t->getNamespace() ) ) {
549 if ( $this->fld_subjectid
) {
550 $getTitles[] = $t->getSubjectPage();
552 } elseif ( $this->fld_talkid
) {
553 $getTitles[] = $t->getTalkPage();
556 if ( !count( $getTitles ) ) {
560 $db = $this->getDB();
562 // Construct a custom WHERE clause that matches
563 // all titles in $getTitles
564 $lb = new LinkBatch( $getTitles );
565 $this->resetQueryParams();
566 $this->addTables( 'page' );
567 $this->addFields( array( 'page_title', 'page_namespace', 'page_id' ) );
568 $this->addWhere( $lb->constructSet( 'page', $db ) );
569 $res = $this->select( __METHOD__
);
570 foreach ( $res as $row ) {
571 if ( MWNamespace
::isTalk( $row->page_namespace
) ) {
572 $this->talkids
[MWNamespace
::getSubject( $row->page_namespace
)][$row->page_title
] =
573 intval( $row->page_id
);
575 $this->subjectids
[MWNamespace
::getTalk( $row->page_namespace
)][$row->page_title
] =
576 intval( $row->page_id
);
581 private function getDisplayTitle() {
582 $this->displaytitles
= array();
584 $pageIds = array_keys( $this->titles
);
586 if ( !count( $pageIds ) ) {
590 $this->resetQueryParams();
591 $this->addTables( 'page_props' );
592 $this->addFields( array( 'pp_page', 'pp_value' ) );
593 $this->addWhereFld( 'pp_page', $pageIds );
594 $this->addWhereFld( 'pp_propname', 'displaytitle' );
595 $res = $this->select( __METHOD__
);
597 foreach ( $res as $row ) {
598 $this->displaytitles
[$row->pp_page
] = $row->pp_value
;
603 * Get information about watched status and put it in $this->watched
605 private function getWatchedInfo() {
608 if ( $wgUser->isAnon() ||
count( $this->everything
) == 0 ) {
612 $this->watched
= array();
613 $db = $this->getDB();
615 $lb = new LinkBatch( $this->everything
);
617 $this->resetQueryParams();
618 $this->addTables( array( 'watchlist' ) );
619 $this->addFields( array( 'wl_title', 'wl_namespace' ) );
620 $this->addWhere( array(
621 $lb->constructSet( 'wl', $db ),
622 'wl_user' => $wgUser->getID()
625 $res = $this->select( __METHOD__
);
627 foreach ( $res as $row ) {
628 $this->watched
[$row->wl_namespace
][$row->wl_title
] = true;
632 public function getCacheMode( $params ) {
633 $publicProps = array(
641 if ( !is_null( $params['prop'] ) ) {
642 foreach ( $params['prop'] as $prop ) {
643 if ( !in_array( $prop, $publicProps ) ) {
648 if ( !is_null( $params['token'] ) ) {
654 public function getAllowedParams() {
657 ApiBase
::PARAM_DFLT
=> null,
658 ApiBase
::PARAM_ISMULTI
=> true,
659 ApiBase
::PARAM_TYPE
=> array(
665 'readable', # private
668 // If you add more properties here, please consider whether they
669 // need to be added to getCacheMode()
672 ApiBase
::PARAM_DFLT
=> null,
673 ApiBase
::PARAM_ISMULTI
=> true,
674 ApiBase
::PARAM_TYPE
=> array_keys( $this->getTokenFunctions() )
680 public function getParamDescription() {
683 'Which additional properties to get:',
684 ' protection - List the protection level of each page',
685 ' talkid - The page ID of the talk page for each non-talk page',
686 ' watched - List the watched status of each page',
687 ' subjectid - The page ID of the parent page for each talk page',
688 ' url - Gives a full URL to the page, and also an edit URL',
689 ' readable - Whether the user can read this page',
690 ' preload - Gives the text returned by EditFormPreloadText',
691 ' displaytitle - Gives the way the page title is actually displayed',
693 'token' => 'Request a token to perform a data-modifying action on a page',
694 'continue' => 'When more results are available, use this to continue',
698 public function getDescription() {
699 return 'Get basic page information such as namespace, title, last touched date, ...';
702 public function getPossibleErrors() {
703 return array_merge( parent
::getPossibleErrors(), array(
704 array( 'code' => '_badcontinue', 'info' => 'Invalid continue param. You should pass the original value returned by the previous query' ),
708 protected function getExamples() {
710 'api.php?action=query&prop=info&titles=Main%20Page',
711 'api.php?action=query&prop=info&inprop=protection&titles=Main%20Page'
715 public function getVersion() {
716 return __CLASS__
. ': $Id$';