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 function execute() {
232 $this->params
= $this->extractRequestParams();
233 if ( !is_null( $this->params
['prop'] ) ) {
234 $prop = array_flip( $this->params
['prop'] );
235 $this->fld_protection
= isset( $prop['protection'] );
236 $this->fld_watched
= isset( $prop['watched'] );
237 $this->fld_talkid
= isset( $prop['talkid'] );
238 $this->fld_subjectid
= isset( $prop['subjectid'] );
239 $this->fld_url
= isset( $prop['url'] );
240 $this->fld_readable
= isset( $prop['readable'] );
241 $this->fld_preload
= isset( $prop['preload'] );
242 $this->fld_displaytitle
= isset( $prop['displaytitle'] );
245 $pageSet = $this->getPageSet();
246 $this->titles
= $pageSet->getGoodTitles();
247 $this->missing
= $pageSet->getMissingTitles();
248 $this->everything
= $this->titles +
$this->missing
;
249 $result = $this->getResult();
251 uasort( $this->everything
, array( 'Title', 'compare' ) );
252 if ( !is_null( $this->params
['continue'] ) ) {
253 // Throw away any titles we're gonna skip so they don't
255 $cont = explode( '|', $this->params
['continue'] );
256 if ( count( $cont ) != 2 ) {
257 $this->dieUsage( 'Invalid continue param. You should pass the original ' .
258 'value returned by the previous query', '_badcontinue' );
260 $conttitle = Title
::makeTitleSafe( $cont[0], $cont[1] );
261 foreach ( $this->everything
as $pageid => $title ) {
262 if ( Title
::compare( $title, $conttitle ) >= 0 ) {
265 unset( $this->titles
[$pageid] );
266 unset( $this->missing
[$pageid] );
267 unset( $this->everything
[$pageid] );
271 $this->pageRestrictions
= $pageSet->getCustomField( 'page_restrictions' );
272 $this->pageIsRedir
= $pageSet->getCustomField( 'page_is_redirect' );
273 $this->pageIsNew
= $pageSet->getCustomField( 'page_is_new' );
275 global $wgDisableCounters;
277 if ( !$wgDisableCounters ) {
278 $this->pageCounter
= $pageSet->getCustomField( 'page_counter' );
280 $this->pageTouched
= $pageSet->getCustomField( 'page_touched' );
281 $this->pageLatest
= $pageSet->getCustomField( 'page_latest' );
282 $this->pageLength
= $pageSet->getCustomField( 'page_len' );
284 // Get protection info if requested
285 if ( $this->fld_protection
) {
286 $this->getProtectionInfo();
289 if ( $this->fld_watched
) {
290 $this->getWatchedInfo();
293 // Run the talkid/subjectid query if requested
294 if ( $this->fld_talkid ||
$this->fld_subjectid
) {
298 if ( $this->fld_displaytitle
) {
299 $this->getDisplayTitle();
302 foreach ( $this->everything
as $pageid => $title ) {
303 $pageInfo = $this->extractPageInfo( $pageid, $title );
304 $fit = $result->addValue( array(
307 ), $pageid, $pageInfo );
309 $this->setContinueEnumParameter( 'continue',
310 $title->getNamespace() . '|' .
318 * Get a result array with information about a title
319 * @param $pageid int Page ID (negative for missing titles)
320 * @param $title Title object
323 private function extractPageInfo( $pageid, $title ) {
325 if ( $title->exists() ) {
326 global $wgDisableCounters;
328 $pageInfo['touched'] = wfTimestamp( TS_ISO_8601
, $this->pageTouched
[$pageid] );
329 $pageInfo['lastrevid'] = intval( $this->pageLatest
[$pageid] );
330 $pageInfo['counter'] = $wgDisableCounters
332 : intval( $this->pageCounter
[$pageid] );
333 $pageInfo['length'] = intval( $this->pageLength
[$pageid] );
335 if ( $this->pageIsRedir
[$pageid] ) {
336 $pageInfo['redirect'] = '';
338 if ( $this->pageIsNew
[$pageid] ) {
339 $pageInfo['new'] = '';
343 if ( !is_null( $this->params
['token'] ) ) {
344 $tokenFunctions = $this->getTokenFunctions();
345 $pageInfo['starttimestamp'] = wfTimestamp( TS_ISO_8601
, time() );
346 foreach ( $this->params
['token'] as $t ) {
347 $val = call_user_func( $tokenFunctions[$t], $pageid, $title );
348 if ( $val === false ) {
349 $this->setWarning( "Action '$t' is not allowed for the current user" );
351 $pageInfo[$t . 'token'] = $val;
356 if ( $this->fld_protection
) {
357 $pageInfo['protection'] = array();
358 if ( isset( $this->protections
[$title->getNamespace()][$title->getDBkey()] ) ) {
359 $pageInfo['protection'] =
360 $this->protections
[$title->getNamespace()][$title->getDBkey()];
362 $this->getResult()->setIndexedTagName( $pageInfo['protection'], 'pr' );
365 if ( $this->fld_watched
&& isset( $this->watched
[$title->getNamespace()][$title->getDBkey()] ) ) {
366 $pageInfo['watched'] = '';
369 if ( $this->fld_talkid
&& isset( $this->talkids
[$title->getNamespace()][$title->getDBkey()] ) ) {
370 $pageInfo['talkid'] = $this->talkids
[$title->getNamespace()][$title->getDBkey()];
373 if ( $this->fld_subjectid
&& isset( $this->subjectids
[$title->getNamespace()][$title->getDBkey()] ) ) {
374 $pageInfo['subjectid'] = $this->subjectids
[$title->getNamespace()][$title->getDBkey()];
377 if ( $this->fld_url
) {
378 $pageInfo['fullurl'] = wfExpandUrl( $title->getFullURL(), PROTO_CURRENT
);
379 $pageInfo['editurl'] = wfExpandUrl( $title->getFullURL( 'action=edit' ), PROTO_CURRENT
);
381 if ( $this->fld_readable
&& $title->userCan( 'read' ) ) {
382 $pageInfo['readable'] = '';
385 if ( $this->fld_preload
) {
386 if ( $title->exists() ) {
387 $pageInfo['preload'] = '';
390 wfRunHooks( 'EditFormPreloadText', array( &$text, &$title ) );
392 $pageInfo['preload'] = $text;
396 if ( $this->fld_displaytitle
) {
397 if ( isset( $this->displaytitles
[$title->getArticleID()] ) ) {
398 $pageInfo['displaytitle'] = $this->displaytitles
[$title->getArticleID()];
400 $pageInfo['displaytitle'] = $title->getPrefixedText();
408 * Get information about protections and put it in $protections
410 private function getProtectionInfo() {
412 $this->protections
= array();
413 $db = $this->getDB();
415 // Get normal protections for existing titles
416 if ( count( $this->titles
) ) {
417 $this->resetQueryParams();
418 $this->addTables( array( 'page_restrictions', 'page' ) );
419 $this->addWhere( 'page_id=pr_page' );
420 $this->addFields( array( 'pr_page', 'pr_type', 'pr_level',
421 'pr_expiry', 'pr_cascade', 'page_namespace',
423 $this->addWhereFld( 'pr_page', array_keys( $this->titles
) );
425 $res = $this->select( __METHOD__
);
426 foreach ( $res as $row ) {
428 'type' => $row->pr_type
,
429 'level' => $row->pr_level
,
430 'expiry' => $wgContLang->formatExpiry( $row->pr_expiry
, TS_ISO_8601
)
432 if ( $row->pr_cascade
) {
435 $this->protections
[$row->page_namespace
][$row->page_title
][] = $a;
437 // Also check old restrictions
438 if ( $this->pageRestrictions
[$row->pr_page
] ) {
439 $restrictions = explode( ':', trim( $this->pageRestrictions
[$row->pr_page
] ) );
440 foreach ( $restrictions as $restrict ) {
441 $temp = explode( '=', trim( $restrict ) );
442 if ( count( $temp ) == 1 ) {
443 // old old format should be treated as edit/move restriction
444 $restriction = trim( $temp[0] );
446 if ( $restriction == '' ) {
449 $this->protections
[$row->page_namespace
][$row->page_title
][] = array(
451 'level' => $restriction,
452 'expiry' => 'infinity',
454 $this->protections
[$row->page_namespace
][$row->page_title
][] = array(
456 'level' => $restriction,
457 'expiry' => 'infinity',
460 $restriction = trim( $temp[1] );
461 if ( $restriction == '' ) {
464 $this->protections
[$row->page_namespace
][$row->page_title
][] = array(
466 'level' => $restriction,
467 'expiry' => 'infinity',
475 // Get protections for missing titles
476 if ( count( $this->missing
) ) {
477 $this->resetQueryParams();
478 $lb = new LinkBatch( $this->missing
);
479 $this->addTables( 'protected_titles' );
480 $this->addFields( array( 'pt_title', 'pt_namespace', 'pt_create_perm', 'pt_expiry' ) );
481 $this->addWhere( $lb->constructSet( 'pt', $db ) );
482 $res = $this->select( __METHOD__
);
483 foreach ( $res as $row ) {
484 $this->protections
[$row->pt_namespace
][$row->pt_title
][] = array(
486 'level' => $row->pt_create_perm
,
487 'expiry' => $wgContLang->formatExpiry( $row->pt_expiry
, TS_ISO_8601
)
492 // Cascading protections
493 $images = $others = array();
494 foreach ( $this->everything
as $title ) {
495 if ( $title->getNamespace() == NS_FILE
) {
496 $images[] = $title->getDBkey();
502 if ( count( $others ) ) {
503 // Non-images: check templatelinks
504 $lb = new LinkBatch( $others );
505 $this->resetQueryParams();
506 $this->addTables( array( 'page_restrictions', 'page', 'templatelinks' ) );
507 $this->addFields( array( 'pr_type', 'pr_level', 'pr_expiry',
508 'page_title', 'page_namespace',
509 'tl_title', 'tl_namespace' ) );
510 $this->addWhere( $lb->constructSet( 'tl', $db ) );
511 $this->addWhere( 'pr_page = page_id' );
512 $this->addWhere( 'pr_page = tl_from' );
513 $this->addWhereFld( 'pr_cascade', 1 );
515 $res = $this->select( __METHOD__
);
516 foreach ( $res as $row ) {
517 $source = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
518 $this->protections
[$row->tl_namespace
][$row->tl_title
][] = array(
519 'type' => $row->pr_type
,
520 'level' => $row->pr_level
,
521 'expiry' => $wgContLang->formatExpiry( $row->pr_expiry
, TS_ISO_8601
),
522 'source' => $source->getPrefixedText()
527 if ( count( $images ) ) {
528 // Images: check imagelinks
529 $this->resetQueryParams();
530 $this->addTables( array( 'page_restrictions', 'page', 'imagelinks' ) );
531 $this->addFields( array( 'pr_type', 'pr_level', 'pr_expiry',
532 'page_title', 'page_namespace', 'il_to' ) );
533 $this->addWhere( 'pr_page = page_id' );
534 $this->addWhere( 'pr_page = il_from' );
535 $this->addWhereFld( 'pr_cascade', 1 );
536 $this->addWhereFld( 'il_to', $images );
538 $res = $this->select( __METHOD__
);
539 foreach ( $res as $row ) {
540 $source = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
541 $this->protections
[NS_FILE
][$row->il_to
][] = array(
542 'type' => $row->pr_type
,
543 'level' => $row->pr_level
,
544 'expiry' => $wgContLang->formatExpiry( $row->pr_expiry
, TS_ISO_8601
),
545 'source' => $source->getPrefixedText()
552 * Get talk page IDs (if requested) and subject page IDs (if requested)
553 * and put them in $talkids and $subjectids
555 private function getTSIDs() {
556 $getTitles = $this->talkids
= $this->subjectids
= array();
558 foreach ( $this->everything
as $t ) {
559 if ( MWNamespace
::isTalk( $t->getNamespace() ) ) {
560 if ( $this->fld_subjectid
) {
561 $getTitles[] = $t->getSubjectPage();
563 } elseif ( $this->fld_talkid
) {
564 $getTitles[] = $t->getTalkPage();
567 if ( !count( $getTitles ) ) {
571 $db = $this->getDB();
573 // Construct a custom WHERE clause that matches
574 // all titles in $getTitles
575 $lb = new LinkBatch( $getTitles );
576 $this->resetQueryParams();
577 $this->addTables( 'page' );
578 $this->addFields( array( 'page_title', 'page_namespace', 'page_id' ) );
579 $this->addWhere( $lb->constructSet( 'page', $db ) );
580 $res = $this->select( __METHOD__
);
581 foreach ( $res as $row ) {
582 if ( MWNamespace
::isTalk( $row->page_namespace
) ) {
583 $this->talkids
[MWNamespace
::getSubject( $row->page_namespace
)][$row->page_title
] =
584 intval( $row->page_id
);
586 $this->subjectids
[MWNamespace
::getTalk( $row->page_namespace
)][$row->page_title
] =
587 intval( $row->page_id
);
592 private function getDisplayTitle() {
593 $this->displaytitles
= array();
595 $pageIds = array_keys( $this->titles
);
597 if ( !count( $pageIds ) ) {
601 $this->resetQueryParams();
602 $this->addTables( 'page_props' );
603 $this->addFields( array( 'pp_page', 'pp_value' ) );
604 $this->addWhereFld( 'pp_page', $pageIds );
605 $this->addWhereFld( 'pp_propname', 'displaytitle' );
606 $res = $this->select( __METHOD__
);
608 foreach ( $res as $row ) {
609 $this->displaytitles
[$row->pp_page
] = $row->pp_value
;
614 * Get information about watched status and put it in $this->watched
616 private function getWatchedInfo() {
617 $user = $this->getUser();
619 if ( $user->isAnon() ||
count( $this->everything
) == 0 ) {
623 $this->watched
= array();
624 $db = $this->getDB();
626 $lb = new LinkBatch( $this->everything
);
628 $this->resetQueryParams();
629 $this->addTables( array( 'watchlist' ) );
630 $this->addFields( array( 'wl_title', 'wl_namespace' ) );
631 $this->addWhere( array(
632 $lb->constructSet( 'wl', $db ),
633 'wl_user' => $user->getID()
636 $res = $this->select( __METHOD__
);
638 foreach ( $res as $row ) {
639 $this->watched
[$row->wl_namespace
][$row->wl_title
] = true;
643 public function getCacheMode( $params ) {
644 $publicProps = array(
652 if ( !is_null( $params['prop'] ) ) {
653 foreach ( $params['prop'] as $prop ) {
654 if ( !in_array( $prop, $publicProps ) ) {
659 if ( !is_null( $params['token'] ) ) {
665 public function getAllowedParams() {
668 ApiBase
::PARAM_DFLT
=> null,
669 ApiBase
::PARAM_ISMULTI
=> true,
670 ApiBase
::PARAM_TYPE
=> array(
676 'readable', # private
679 // If you add more properties here, please consider whether they
680 // need to be added to getCacheMode()
683 ApiBase
::PARAM_DFLT
=> null,
684 ApiBase
::PARAM_ISMULTI
=> true,
685 ApiBase
::PARAM_TYPE
=> array_keys( $this->getTokenFunctions() )
691 public function getParamDescription() {
694 'Which additional properties to get:',
695 ' protection - List the protection level of each page',
696 ' talkid - The page ID of the talk page for each non-talk page',
697 ' watched - List the watched status of each page',
698 ' subjectid - The page ID of the parent page for each talk page',
699 ' url - Gives a full URL to the page, and also an edit URL',
700 ' readable - Whether the user can read this page',
701 ' preload - Gives the text returned by EditFormPreloadText',
702 ' displaytitle - Gives the way the page title is actually displayed',
704 'token' => 'Request a token to perform a data-modifying action on a page',
705 'continue' => 'When more results are available, use this to continue',
709 public function getDescription() {
710 return 'Get basic page information such as namespace, title, last touched date, ...';
713 public function getPossibleErrors() {
714 return array_merge( parent
::getPossibleErrors(), array(
715 array( 'code' => '_badcontinue', 'info' => 'Invalid continue param. You should pass the original value returned by the previous query' ),
719 public function getExamples() {
721 'api.php?action=query&prop=info&titles=Main%20Page',
722 'api.php?action=query&prop=info&inprop=protection&titles=Main%20Page'
726 public function getHelpUrls() {
727 return 'https://www.mediawiki.org/wiki/API:Properties#info_.2F_in';
730 public function getVersion() {
731 return __CLASS__
. ': $Id$';