d96808f3f9e445b59bd2a4168574621b8f83912f
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->isAllowed( 'import' ) ) {
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() {
400 $this->protections
= array();
401 $db = $this->getDB();
403 // Get normal protections for existing titles
404 if ( count( $this->titles
) ) {
405 $this->resetQueryParams();
406 $this->addTables( array( 'page_restrictions', 'page' ) );
407 $this->addWhere( 'page_id=pr_page' );
408 $this->addFields( array( 'pr_page', 'pr_type', 'pr_level',
409 'pr_expiry', 'pr_cascade', 'page_namespace',
411 $this->addWhereFld( 'pr_page', array_keys( $this->titles
) );
413 $res = $this->select( __METHOD__
);
414 foreach ( $res as $row ) {
416 'type' => $row->pr_type
,
417 'level' => $row->pr_level
,
418 'expiry' => Block
::decodeExpiry( $row->pr_expiry
, TS_ISO_8601
)
420 if ( $row->pr_cascade
) {
423 $this->protections
[$row->page_namespace
][$row->page_title
][] = $a;
425 // Also check old restrictions
426 if ( $this->pageRestrictions
[$row->pr_page
] ) {
427 $restrictions = explode( ':', trim( $this->pageRestrictions
[$row->pr_page
] ) );
428 foreach ( $restrictions as $restrict ) {
429 $temp = explode( '=', trim( $restrict ) );
430 if ( count( $temp ) == 1 ) {
431 // old old format should be treated as edit/move restriction
432 $restriction = trim( $temp[0] );
434 if ( $restriction == '' ) {
437 $this->protections
[$row->page_namespace
][$row->page_title
][] = array(
439 'level' => $restriction,
440 'expiry' => 'infinity',
442 $this->protections
[$row->page_namespace
][$row->page_title
][] = array(
444 'level' => $restriction,
445 'expiry' => 'infinity',
448 $restriction = trim( $temp[1] );
449 if ( $restriction == '' ) {
452 $this->protections
[$row->page_namespace
][$row->page_title
][] = array(
454 'level' => $restriction,
455 'expiry' => 'infinity',
463 // Get protections for missing titles
464 if ( count( $this->missing
) ) {
465 $this->resetQueryParams();
466 $lb = new LinkBatch( $this->missing
);
467 $this->addTables( 'protected_titles' );
468 $this->addFields( array( 'pt_title', 'pt_namespace', 'pt_create_perm', 'pt_expiry' ) );
469 $this->addWhere( $lb->constructSet( 'pt', $db ) );
470 $res = $this->select( __METHOD__
);
471 foreach ( $res as $row ) {
472 $this->protections
[$row->pt_namespace
][$row->pt_title
][] = array(
474 'level' => $row->pt_create_perm
,
475 'expiry' => Block
::decodeExpiry( $row->pt_expiry
, TS_ISO_8601
)
480 // Cascading protections
481 $images = $others = array();
482 foreach ( $this->everything
as $title ) {
483 if ( $title->getNamespace() == NS_FILE
) {
484 $images[] = $title->getDBkey();
490 if ( count( $others ) ) {
491 // Non-images: check templatelinks
492 $lb = new LinkBatch( $others );
493 $this->resetQueryParams();
494 $this->addTables( array( 'page_restrictions', 'page', 'templatelinks' ) );
495 $this->addFields( array( 'pr_type', 'pr_level', 'pr_expiry',
496 'page_title', 'page_namespace',
497 'tl_title', 'tl_namespace' ) );
498 $this->addWhere( $lb->constructSet( 'tl', $db ) );
499 $this->addWhere( 'pr_page = page_id' );
500 $this->addWhere( 'pr_page = tl_from' );
501 $this->addWhereFld( 'pr_cascade', 1 );
503 $res = $this->select( __METHOD__
);
504 foreach ( $res as $row ) {
505 $source = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
506 $this->protections
[$row->tl_namespace
][$row->tl_title
][] = array(
507 'type' => $row->pr_type
,
508 'level' => $row->pr_level
,
509 'expiry' => Block
::decodeExpiry( $row->pr_expiry
, TS_ISO_8601
),
510 'source' => $source->getPrefixedText()
515 if ( count( $images ) ) {
516 // Images: check imagelinks
517 $this->resetQueryParams();
518 $this->addTables( array( 'page_restrictions', 'page', 'imagelinks' ) );
519 $this->addFields( array( 'pr_type', 'pr_level', 'pr_expiry',
520 'page_title', 'page_namespace', 'il_to' ) );
521 $this->addWhere( 'pr_page = page_id' );
522 $this->addWhere( 'pr_page = il_from' );
523 $this->addWhereFld( 'pr_cascade', 1 );
524 $this->addWhereFld( 'il_to', $images );
526 $res = $this->select( __METHOD__
);
527 foreach ( $res as $row ) {
528 $source = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
529 $this->protections
[NS_FILE
][$row->il_to
][] = array(
530 'type' => $row->pr_type
,
531 'level' => $row->pr_level
,
532 'expiry' => Block
::decodeExpiry( $row->pr_expiry
, TS_ISO_8601
),
533 'source' => $source->getPrefixedText()
540 * Get talk page IDs (if requested) and subject page IDs (if requested)
541 * and put them in $talkids and $subjectids
543 private function getTSIDs() {
544 $getTitles = $this->talkids
= $this->subjectids
= array();
546 foreach ( $this->everything
as $t ) {
547 if ( MWNamespace
::isTalk( $t->getNamespace() ) ) {
548 if ( $this->fld_subjectid
) {
549 $getTitles[] = $t->getSubjectPage();
551 } elseif ( $this->fld_talkid
) {
552 $getTitles[] = $t->getTalkPage();
555 if ( !count( $getTitles ) ) {
559 $db = $this->getDB();
561 // Construct a custom WHERE clause that matches
562 // all titles in $getTitles
563 $lb = new LinkBatch( $getTitles );
564 $this->resetQueryParams();
565 $this->addTables( 'page' );
566 $this->addFields( array( 'page_title', 'page_namespace', 'page_id' ) );
567 $this->addWhere( $lb->constructSet( 'page', $db ) );
568 $res = $this->select( __METHOD__
);
569 foreach ( $res as $row ) {
570 if ( MWNamespace
::isTalk( $row->page_namespace
) ) {
571 $this->talkids
[MWNamespace
::getSubject( $row->page_namespace
)][$row->page_title
] =
572 intval( $row->page_id
);
574 $this->subjectids
[MWNamespace
::getTalk( $row->page_namespace
)][$row->page_title
] =
575 intval( $row->page_id
);
580 private function getDisplayTitle() {
581 $this->displaytitles
= array();
583 $pageIds = array_keys( $this->titles
);
585 if ( !count( $pageIds ) ) {
589 $this->resetQueryParams();
590 $this->addTables( 'page_props' );
591 $this->addFields( array( 'pp_page', 'pp_value' ) );
592 $this->addWhereFld( 'pp_page', $pageIds );
593 $this->addWhereFld( 'pp_propname', 'displaytitle' );
594 $res = $this->select( __METHOD__
);
596 foreach ( $res as $row ) {
597 $this->displaytitles
[$row->pp_page
] = $row->pp_value
;
602 * Get information about watched status and put it in $this->watched
604 private function getWatchedInfo() {
607 if ( $wgUser->isAnon() ||
count( $this->everything
) == 0 ) {
611 $this->watched
= array();
612 $db = $this->getDB();
614 $lb = new LinkBatch( $this->everything
);
616 $this->resetQueryParams();
617 $this->addTables( array( 'watchlist' ) );
618 $this->addFields( array( 'wl_title', 'wl_namespace' ) );
619 $this->addWhere( array(
620 $lb->constructSet( 'wl', $db ),
621 'wl_user' => $wgUser->getID()
624 $res = $this->select( __METHOD__
);
626 foreach ( $res as $row ) {
627 $this->watched
[$row->wl_namespace
][$row->wl_title
] = true;
631 public function getCacheMode( $params ) {
632 $publicProps = array(
640 if ( !is_null( $params['prop'] ) ) {
641 foreach ( $params['prop'] as $prop ) {
642 if ( !in_array( $prop, $publicProps ) ) {
647 if ( !is_null( $params['token'] ) ) {
653 public function getAllowedParams() {
656 ApiBase
::PARAM_DFLT
=> null,
657 ApiBase
::PARAM_ISMULTI
=> true,
658 ApiBase
::PARAM_TYPE
=> array(
664 'readable', # private
667 // If you add more properties here, please consider whether they
668 // need to be added to getCacheMode()
671 ApiBase
::PARAM_DFLT
=> null,
672 ApiBase
::PARAM_ISMULTI
=> true,
673 ApiBase
::PARAM_TYPE
=> array_keys( $this->getTokenFunctions() )
679 public function getParamDescription() {
682 'Which additional properties to get:',
683 ' protection - List the protection level of each page',
684 ' talkid - The page ID of the talk page for each non-talk page',
685 ' watched - List the watched status of each page',
686 ' subjectid - The page ID of the parent page for each talk page',
687 ' url - Gives a full URL to the page, and also an edit URL',
688 ' readable - Whether the user can read this page',
689 ' preload - Gives the text returned by EditFormPreloadText',
690 ' displaytitle - Gives the way the page title is actually displayed',
692 'token' => 'Request a token to perform a data-modifying action on a page',
693 'continue' => 'When more results are available, use this to continue',
697 public function getDescription() {
698 return 'Get basic page information such as namespace, title, last touched date, ...';
701 public function getPossibleErrors() {
702 return array_merge( parent
::getPossibleErrors(), array(
703 array( 'code' => '_badcontinue', 'info' => 'Invalid continue param. You should pass the original value returned by the previous query' ),
707 protected function getExamples() {
709 'api.php?action=query&prop=info&titles=Main%20Page',
710 'api.php?action=query&prop=info&inprop=protection&titles=Main%20Page'
714 public function getVersion() {
715 return __CLASS__
. ': $Id$';