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
28 * A query module to show basic page information.
32 class ApiQueryInfo
extends ApiQueryBase
{
34 private $fld_protection = false, $fld_talkid = false,
35 $fld_subjectid = false, $fld_url = false,
36 $fld_readable = false, $fld_watched = false,
37 $fld_preload = false, $fld_displaytitle = false;
39 private $params, $titles, $missing, $everything, $pageCounter;
41 private $pageRestrictions, $pageIsRedir, $pageIsNew, $pageTouched,
42 $pageLatest, $pageLength;
44 private $protections, $watched, $talkids, $subjectids, $displaytitles;
46 private $tokenFunctions;
48 public function __construct( $query, $moduleName ) {
49 parent
::__construct( $query, $moduleName, 'in' );
53 * @param $pageSet ApiPageSet
56 public function requestExtraData( $pageSet ) {
57 global $wgDisableCounters;
59 $pageSet->requestField( 'page_restrictions' );
60 $pageSet->requestField( 'page_is_redirect' );
61 $pageSet->requestField( 'page_is_new' );
62 if ( !$wgDisableCounters ) {
63 $pageSet->requestField( 'page_counter' );
65 $pageSet->requestField( 'page_touched' );
66 $pageSet->requestField( 'page_latest' );
67 $pageSet->requestField( 'page_len' );
71 * Get an array mapping token names to their handler functions.
72 * The prototype for a token function is func($pageid, $title)
73 * it should return a token or false (permission denied)
74 * @return array array(tokenname => function)
76 protected function getTokenFunctions() {
77 // Don't call the hooks twice
78 if ( isset( $this->tokenFunctions
) ) {
79 return $this->tokenFunctions
;
82 // If we're in JSON callback mode, no tokens can be obtained
83 if ( !is_null( $this->getMain()->getRequest()->getVal( 'callback' ) ) ) {
87 $this->tokenFunctions
= array(
88 'edit' => array( 'ApiQueryInfo', 'getEditToken' ),
89 'delete' => array( 'ApiQueryInfo', 'getDeleteToken' ),
90 'protect' => array( 'ApiQueryInfo', 'getProtectToken' ),
91 'move' => array( 'ApiQueryInfo', 'getMoveToken' ),
92 'block' => array( 'ApiQueryInfo', 'getBlockToken' ),
93 'unblock' => array( 'ApiQueryInfo', 'getUnblockToken' ),
94 'email' => array( 'ApiQueryInfo', 'getEmailToken' ),
95 'import' => array( 'ApiQueryInfo', 'getImportToken' ),
96 'watch' => array( 'ApiQueryInfo', 'getWatchToken'),
98 wfRunHooks( 'APIQueryInfoTokens', array( &$this->tokenFunctions
) );
99 return $this->tokenFunctions
;
102 public static function getEditToken( $pageid, $title ) {
103 // We could check for $title->userCan('edit') here,
104 // but that's too expensive for this purpose
105 // and would break caching
107 if ( !$wgUser->isAllowed( 'edit' ) ) {
111 // The edit token is always the same, let's exploit that
112 static $cachedEditToken = null;
113 if ( !is_null( $cachedEditToken ) ) {
114 return $cachedEditToken;
117 $cachedEditToken = $wgUser->getEditToken();
118 return $cachedEditToken;
121 public static function getDeleteToken( $pageid, $title ) {
123 if ( !$wgUser->isAllowed( 'delete' ) ) {
127 static $cachedDeleteToken = null;
128 if ( !is_null( $cachedDeleteToken ) ) {
129 return $cachedDeleteToken;
132 $cachedDeleteToken = $wgUser->getEditToken();
133 return $cachedDeleteToken;
136 public static function getProtectToken( $pageid, $title ) {
138 if ( !$wgUser->isAllowed( 'protect' ) ) {
142 static $cachedProtectToken = null;
143 if ( !is_null( $cachedProtectToken ) ) {
144 return $cachedProtectToken;
147 $cachedProtectToken = $wgUser->getEditToken();
148 return $cachedProtectToken;
151 public static function getMoveToken( $pageid, $title ) {
153 if ( !$wgUser->isAllowed( 'move' ) ) {
157 static $cachedMoveToken = null;
158 if ( !is_null( $cachedMoveToken ) ) {
159 return $cachedMoveToken;
162 $cachedMoveToken = $wgUser->getEditToken();
163 return $cachedMoveToken;
166 public static function getBlockToken( $pageid, $title ) {
168 if ( !$wgUser->isAllowed( 'block' ) ) {
172 static $cachedBlockToken = null;
173 if ( !is_null( $cachedBlockToken ) ) {
174 return $cachedBlockToken;
177 $cachedBlockToken = $wgUser->getEditToken();
178 return $cachedBlockToken;
181 public static function getUnblockToken( $pageid, $title ) {
182 // Currently, this is exactly the same as the block token
183 return self
::getBlockToken( $pageid, $title );
186 public static function getEmailToken( $pageid, $title ) {
188 if ( !$wgUser->canSendEmail() ||
$wgUser->isBlockedFromEmailUser() ) {
192 static $cachedEmailToken = null;
193 if ( !is_null( $cachedEmailToken ) ) {
194 return $cachedEmailToken;
197 $cachedEmailToken = $wgUser->getEditToken();
198 return $cachedEmailToken;
201 public static function getImportToken( $pageid, $title ) {
203 if ( !$wgUser->isAllowedAny( 'import', 'importupload' ) ) {
207 static $cachedImportToken = null;
208 if ( !is_null( $cachedImportToken ) ) {
209 return $cachedImportToken;
212 $cachedImportToken = $wgUser->getEditToken();
213 return $cachedImportToken;
216 public static function getWatchToken( $pageid, $title ) {
218 if ( !$wgUser->isLoggedIn() ) {
222 static $cachedWatchToken = null;
223 if ( !is_null( $cachedWatchToken ) ) {
224 return $cachedWatchToken;
227 $cachedWatchToken = $wgUser->getEditToken( 'watch' );
228 return $cachedWatchToken;
231 public static function getOptionsToken( $pageid, $title ) {
233 if ( !$wgUser->isLoggedIn() ) {
237 static $cachedOptionsToken = null;
238 if ( !is_null( $cachedOptionsToken ) ) {
239 return $cachedOptionsToken;
242 $cachedOptionsToken = $wgUser->getEditToken();
243 return $cachedOptionsToken;
246 public function execute() {
247 $this->params
= $this->extractRequestParams();
248 if ( !is_null( $this->params
['prop'] ) ) {
249 $prop = array_flip( $this->params
['prop'] );
250 $this->fld_protection
= isset( $prop['protection'] );
251 $this->fld_watched
= isset( $prop['watched'] );
252 $this->fld_talkid
= isset( $prop['talkid'] );
253 $this->fld_subjectid
= isset( $prop['subjectid'] );
254 $this->fld_url
= isset( $prop['url'] );
255 $this->fld_readable
= isset( $prop['readable'] );
256 $this->fld_preload
= isset( $prop['preload'] );
257 $this->fld_displaytitle
= isset( $prop['displaytitle'] );
260 $pageSet = $this->getPageSet();
261 $this->titles
= $pageSet->getGoodTitles();
262 $this->missing
= $pageSet->getMissingTitles();
263 $this->everything
= $this->titles +
$this->missing
;
264 $result = $this->getResult();
266 uasort( $this->everything
, array( 'Title', 'compare' ) );
267 if ( !is_null( $this->params
['continue'] ) ) {
268 // Throw away any titles we're gonna skip so they don't
270 $cont = explode( '|', $this->params
['continue'] );
271 if ( count( $cont ) != 2 ) {
272 $this->dieUsage( 'Invalid continue param. You should pass the original ' .
273 'value returned by the previous query', '_badcontinue' );
275 $conttitle = Title
::makeTitleSafe( $cont[0], $cont[1] );
276 foreach ( $this->everything
as $pageid => $title ) {
277 if ( Title
::compare( $title, $conttitle ) >= 0 ) {
280 unset( $this->titles
[$pageid] );
281 unset( $this->missing
[$pageid] );
282 unset( $this->everything
[$pageid] );
286 $this->pageRestrictions
= $pageSet->getCustomField( 'page_restrictions' );
287 $this->pageIsRedir
= $pageSet->getCustomField( 'page_is_redirect' );
288 $this->pageIsNew
= $pageSet->getCustomField( 'page_is_new' );
290 global $wgDisableCounters;
292 if ( !$wgDisableCounters ) {
293 $this->pageCounter
= $pageSet->getCustomField( 'page_counter' );
295 $this->pageTouched
= $pageSet->getCustomField( 'page_touched' );
296 $this->pageLatest
= $pageSet->getCustomField( 'page_latest' );
297 $this->pageLength
= $pageSet->getCustomField( 'page_len' );
299 // Get protection info if requested
300 if ( $this->fld_protection
) {
301 $this->getProtectionInfo();
304 if ( $this->fld_watched
) {
305 $this->getWatchedInfo();
308 // Run the talkid/subjectid query if requested
309 if ( $this->fld_talkid ||
$this->fld_subjectid
) {
313 if ( $this->fld_displaytitle
) {
314 $this->getDisplayTitle();
317 foreach ( $this->everything
as $pageid => $title ) {
318 $pageInfo = $this->extractPageInfo( $pageid, $title );
319 $fit = $result->addValue( array(
322 ), $pageid, $pageInfo );
324 $this->setContinueEnumParameter( 'continue',
325 $title->getNamespace() . '|' .
333 * Get a result array with information about a title
334 * @param $pageid int Page ID (negative for missing titles)
335 * @param $title Title object
338 private function extractPageInfo( $pageid, $title ) {
340 $titleExists = $pageid > 0; //$title->exists() needs pageid, which is not set for all title objects
341 $ns = $title->getNamespace();
342 $dbkey = $title->getDBkey();
343 if ( $titleExists ) {
344 global $wgDisableCounters;
346 $pageInfo['touched'] = wfTimestamp( TS_ISO_8601
, $this->pageTouched
[$pageid] );
347 $pageInfo['lastrevid'] = intval( $this->pageLatest
[$pageid] );
348 $pageInfo['counter'] = $wgDisableCounters
350 : intval( $this->pageCounter
[$pageid] );
351 $pageInfo['length'] = intval( $this->pageLength
[$pageid] );
353 if ( $this->pageIsRedir
[$pageid] ) {
354 $pageInfo['redirect'] = '';
356 if ( $this->pageIsNew
[$pageid] ) {
357 $pageInfo['new'] = '';
361 if ( !is_null( $this->params
['token'] ) ) {
362 $tokenFunctions = $this->getTokenFunctions();
363 $pageInfo['starttimestamp'] = wfTimestamp( TS_ISO_8601
, time() );
364 foreach ( $this->params
['token'] as $t ) {
365 $val = call_user_func( $tokenFunctions[$t], $pageid, $title );
366 if ( $val === false ) {
367 $this->setWarning( "Action '$t' is not allowed for the current user" );
369 $pageInfo[$t . 'token'] = $val;
374 if ( $this->fld_protection
) {
375 $pageInfo['protection'] = array();
376 if ( isset( $this->protections
[$ns][$dbkey] ) ) {
377 $pageInfo['protection'] =
378 $this->protections
[$ns][$dbkey];
380 $this->getResult()->setIndexedTagName( $pageInfo['protection'], 'pr' );
383 if ( $this->fld_watched
&& isset( $this->watched
[$ns][$dbkey] ) ) {
384 $pageInfo['watched'] = '';
387 if ( $this->fld_talkid
&& isset( $this->talkids
[$ns][$dbkey] ) ) {
388 $pageInfo['talkid'] = $this->talkids
[$ns][$dbkey];
391 if ( $this->fld_subjectid
&& isset( $this->subjectids
[$ns][$dbkey] ) ) {
392 $pageInfo['subjectid'] = $this->subjectids
[$ns][$dbkey];
395 if ( $this->fld_url
) {
396 $pageInfo['fullurl'] = wfExpandUrl( $title->getFullURL(), PROTO_CURRENT
);
397 $pageInfo['editurl'] = wfExpandUrl( $title->getFullURL( 'action=edit' ), PROTO_CURRENT
);
399 if ( $this->fld_readable
&& $title->userCan( 'read' ) ) {
400 $pageInfo['readable'] = '';
403 if ( $this->fld_preload
) {
404 if ( $titleExists ) {
405 $pageInfo['preload'] = '';
408 wfRunHooks( 'EditFormPreloadText', array( &$text, &$title ) );
410 $pageInfo['preload'] = $text;
414 if ( $this->fld_displaytitle
) {
415 if ( isset( $this->displaytitles
[$pageid] ) ) {
416 $pageInfo['displaytitle'] = $this->displaytitles
[$pageid];
418 $pageInfo['displaytitle'] = $title->getPrefixedText();
426 * Get information about protections and put it in $protections
428 private function getProtectionInfo() {
430 $this->protections
= array();
431 $db = $this->getDB();
433 // Get normal protections for existing titles
434 if ( count( $this->titles
) ) {
435 $this->resetQueryParams();
436 $this->addTables( array( 'page_restrictions', 'page' ) );
437 $this->addWhere( 'page_id=pr_page' );
438 $this->addFields( array( 'pr_page', 'pr_type', 'pr_level',
439 'pr_expiry', 'pr_cascade', 'page_namespace',
441 $this->addWhereFld( 'pr_page', array_keys( $this->titles
) );
443 $res = $this->select( __METHOD__
);
444 foreach ( $res as $row ) {
446 'type' => $row->pr_type
,
447 'level' => $row->pr_level
,
448 'expiry' => $wgContLang->formatExpiry( $row->pr_expiry
, TS_ISO_8601
)
450 if ( $row->pr_cascade
) {
453 $this->protections
[$row->page_namespace
][$row->page_title
][] = $a;
455 // Also check old restrictions
456 if ( $this->pageRestrictions
[$row->pr_page
] ) {
457 $restrictions = explode( ':', trim( $this->pageRestrictions
[$row->pr_page
] ) );
458 foreach ( $restrictions as $restrict ) {
459 $temp = explode( '=', trim( $restrict ) );
460 if ( count( $temp ) == 1 ) {
461 // old old format should be treated as edit/move restriction
462 $restriction = trim( $temp[0] );
464 if ( $restriction == '' ) {
467 $this->protections
[$row->page_namespace
][$row->page_title
][] = array(
469 'level' => $restriction,
470 'expiry' => 'infinity',
472 $this->protections
[$row->page_namespace
][$row->page_title
][] = array(
474 'level' => $restriction,
475 'expiry' => 'infinity',
478 $restriction = trim( $temp[1] );
479 if ( $restriction == '' ) {
482 $this->protections
[$row->page_namespace
][$row->page_title
][] = array(
484 'level' => $restriction,
485 'expiry' => 'infinity',
493 // Get protections for missing titles
494 if ( count( $this->missing
) ) {
495 $this->resetQueryParams();
496 $lb = new LinkBatch( $this->missing
);
497 $this->addTables( 'protected_titles' );
498 $this->addFields( array( 'pt_title', 'pt_namespace', 'pt_create_perm', 'pt_expiry' ) );
499 $this->addWhere( $lb->constructSet( 'pt', $db ) );
500 $res = $this->select( __METHOD__
);
501 foreach ( $res as $row ) {
502 $this->protections
[$row->pt_namespace
][$row->pt_title
][] = array(
504 'level' => $row->pt_create_perm
,
505 'expiry' => $wgContLang->formatExpiry( $row->pt_expiry
, TS_ISO_8601
)
510 // Cascading protections
511 $images = $others = array();
512 foreach ( $this->everything
as $title ) {
513 if ( $title->getNamespace() == NS_FILE
) {
514 $images[] = $title->getDBkey();
520 if ( count( $others ) ) {
521 // Non-images: check templatelinks
522 $lb = new LinkBatch( $others );
523 $this->resetQueryParams();
524 $this->addTables( array( 'page_restrictions', 'page', 'templatelinks' ) );
525 $this->addFields( array( 'pr_type', 'pr_level', 'pr_expiry',
526 'page_title', 'page_namespace',
527 'tl_title', 'tl_namespace' ) );
528 $this->addWhere( $lb->constructSet( 'tl', $db ) );
529 $this->addWhere( 'pr_page = page_id' );
530 $this->addWhere( 'pr_page = tl_from' );
531 $this->addWhereFld( 'pr_cascade', 1 );
533 $res = $this->select( __METHOD__
);
534 foreach ( $res as $row ) {
535 $source = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
536 $this->protections
[$row->tl_namespace
][$row->tl_title
][] = array(
537 'type' => $row->pr_type
,
538 'level' => $row->pr_level
,
539 'expiry' => $wgContLang->formatExpiry( $row->pr_expiry
, TS_ISO_8601
),
540 'source' => $source->getPrefixedText()
545 if ( count( $images ) ) {
546 // Images: check imagelinks
547 $this->resetQueryParams();
548 $this->addTables( array( 'page_restrictions', 'page', 'imagelinks' ) );
549 $this->addFields( array( 'pr_type', 'pr_level', 'pr_expiry',
550 'page_title', 'page_namespace', 'il_to' ) );
551 $this->addWhere( 'pr_page = page_id' );
552 $this->addWhere( 'pr_page = il_from' );
553 $this->addWhereFld( 'pr_cascade', 1 );
554 $this->addWhereFld( 'il_to', $images );
556 $res = $this->select( __METHOD__
);
557 foreach ( $res as $row ) {
558 $source = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
559 $this->protections
[NS_FILE
][$row->il_to
][] = array(
560 'type' => $row->pr_type
,
561 'level' => $row->pr_level
,
562 'expiry' => $wgContLang->formatExpiry( $row->pr_expiry
, TS_ISO_8601
),
563 'source' => $source->getPrefixedText()
570 * Get talk page IDs (if requested) and subject page IDs (if requested)
571 * and put them in $talkids and $subjectids
573 private function getTSIDs() {
574 $getTitles = $this->talkids
= $this->subjectids
= array();
576 foreach ( $this->everything
as $t ) {
577 if ( MWNamespace
::isTalk( $t->getNamespace() ) ) {
578 if ( $this->fld_subjectid
) {
579 $getTitles[] = $t->getSubjectPage();
581 } elseif ( $this->fld_talkid
) {
582 $getTitles[] = $t->getTalkPage();
585 if ( !count( $getTitles ) ) {
589 $db = $this->getDB();
591 // Construct a custom WHERE clause that matches
592 // all titles in $getTitles
593 $lb = new LinkBatch( $getTitles );
594 $this->resetQueryParams();
595 $this->addTables( 'page' );
596 $this->addFields( array( 'page_title', 'page_namespace', 'page_id' ) );
597 $this->addWhere( $lb->constructSet( 'page', $db ) );
598 $res = $this->select( __METHOD__
);
599 foreach ( $res as $row ) {
600 if ( MWNamespace
::isTalk( $row->page_namespace
) ) {
601 $this->talkids
[MWNamespace
::getSubject( $row->page_namespace
)][$row->page_title
] =
602 intval( $row->page_id
);
604 $this->subjectids
[MWNamespace
::getTalk( $row->page_namespace
)][$row->page_title
] =
605 intval( $row->page_id
);
610 private function getDisplayTitle() {
611 $this->displaytitles
= array();
613 $pageIds = array_keys( $this->titles
);
615 if ( !count( $pageIds ) ) {
619 $this->resetQueryParams();
620 $this->addTables( 'page_props' );
621 $this->addFields( array( 'pp_page', 'pp_value' ) );
622 $this->addWhereFld( 'pp_page', $pageIds );
623 $this->addWhereFld( 'pp_propname', 'displaytitle' );
624 $res = $this->select( __METHOD__
);
626 foreach ( $res as $row ) {
627 $this->displaytitles
[$row->pp_page
] = $row->pp_value
;
632 * Get information about watched status and put it in $this->watched
634 private function getWatchedInfo() {
635 $user = $this->getUser();
637 if ( $user->isAnon() ||
count( $this->everything
) == 0 ) {
641 $this->watched
= array();
642 $db = $this->getDB();
644 $lb = new LinkBatch( $this->everything
);
646 $this->resetQueryParams();
647 $this->addTables( array( 'watchlist' ) );
648 $this->addFields( array( 'wl_title', 'wl_namespace' ) );
649 $this->addWhere( array(
650 $lb->constructSet( 'wl', $db ),
651 'wl_user' => $user->getID()
654 $res = $this->select( __METHOD__
);
656 foreach ( $res as $row ) {
657 $this->watched
[$row->wl_namespace
][$row->wl_title
] = true;
661 public function getCacheMode( $params ) {
662 $publicProps = array(
670 if ( !is_null( $params['prop'] ) ) {
671 foreach ( $params['prop'] as $prop ) {
672 if ( !in_array( $prop, $publicProps ) ) {
677 if ( !is_null( $params['token'] ) ) {
683 public function getAllowedParams() {
686 ApiBase
::PARAM_DFLT
=> null,
687 ApiBase
::PARAM_ISMULTI
=> true,
688 ApiBase
::PARAM_TYPE
=> array(
694 'readable', # private
697 // If you add more properties here, please consider whether they
698 // need to be added to getCacheMode()
701 ApiBase
::PARAM_DFLT
=> null,
702 ApiBase
::PARAM_ISMULTI
=> true,
703 ApiBase
::PARAM_TYPE
=> array_keys( $this->getTokenFunctions() )
709 public function getParamDescription() {
712 'Which additional properties to get:',
713 ' protection - List the protection level of each page',
714 ' talkid - The page ID of the talk page for each non-talk page',
715 ' watched - List the watched status of each page',
716 ' subjectid - The page ID of the parent page for each talk page',
717 ' url - Gives a full URL to the page, and also an edit URL',
718 ' readable - Whether the user can read this page',
719 ' preload - Gives the text returned by EditFormPreloadText',
720 ' displaytitle - Gives the way the page title is actually displayed',
722 'token' => 'Request a token to perform a data-modifying action on a page',
723 'continue' => 'When more results are available, use this to continue',
727 public function getResultProperties() {
729 ApiBase
::PROP_LIST
=> false,
731 'touched' => 'timestamp',
732 'lastrevid' => 'integer',
734 ApiBase
::PROP_TYPE
=> 'integer',
735 ApiBase
::PROP_NULLABLE
=> true
737 'length' => 'integer',
738 'redirect' => 'boolean',
740 'starttimestamp' => array(
741 ApiBase
::PROP_TYPE
=> 'timestamp',
742 ApiBase
::PROP_NULLABLE
=> true
746 'watched' => 'boolean'
750 ApiBase
::PROP_TYPE
=> 'integer',
751 ApiBase
::PROP_NULLABLE
=> true
754 'subjectid' => array(
755 'subjectid' => array(
756 ApiBase
::PROP_TYPE
=> 'integer',
757 ApiBase
::PROP_NULLABLE
=> true
761 'fullurl' => 'string',
762 'editurl' => 'string'
765 'readable' => 'boolean'
768 'preload' => 'string'
770 'displaytitle' => array(
771 'displaytitle' => 'string'
775 self
::addTokenProperties( $props, $this->getTokenFunctions() );
780 public function getDescription() {
781 return 'Get basic page information such as namespace, title, last touched date, ...';
784 public function getPossibleErrors() {
785 return array_merge( parent
::getPossibleErrors(), array(
786 array( 'code' => '_badcontinue', 'info' => 'Invalid continue param. You should pass the original value returned by the previous query' ),
790 public function getExamples() {
792 'api.php?action=query&prop=info&titles=Main%20Page',
793 'api.php?action=query&prop=info&inprop=protection&titles=Main%20Page'
797 public function getHelpUrls() {
798 return 'https://www.mediawiki.org/wiki/API:Properties#info_.2F_in';
801 public function getVersion() {
802 return __CLASS__
. ': $Id$';