4 * Created on Sep 25, 2006
6 * API for MediaWiki 1.8+
8 * Copyright (C) 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 * http://www.gnu.org/copyleft/gpl.html
26 if (!defined('MEDIAWIKI')) {
27 // Eclipse helper - will be ignored in production
28 require_once ('ApiQueryBase.php');
32 * A query module to show basic page information.
36 class ApiQueryInfo
extends ApiQueryBase
{
38 public function __construct($query, $moduleName) {
39 parent
:: __construct($query, $moduleName, 'in');
42 public function requestExtraData($pageSet) {
43 $pageSet->requestField('page_restrictions');
44 $pageSet->requestField('page_is_redirect');
45 $pageSet->requestField('page_is_new');
46 $pageSet->requestField('page_counter');
47 $pageSet->requestField('page_touched');
48 $pageSet->requestField('page_latest');
49 $pageSet->requestField('page_len');
52 protected function getTokenFunctions() {
53 // tokenname => function
54 // function prototype is func($pageid, $title)
55 // should return token or false
57 // Don't call the hooks twice
58 if(isset($this->tokenFunctions
))
59 return $this->tokenFunctions
;
61 // If we're in JSON callback mode, no tokens can be obtained
62 if(!is_null($this->getMain()->getRequest()->getVal('callback')))
65 $this->tokenFunctions
= array(
66 'edit' => array( 'ApiQueryInfo', 'getEditToken' ),
67 'delete' => array( 'ApiQueryInfo', 'getDeleteToken' ),
68 'protect' => array( 'ApiQueryInfo', 'getProtectToken' ),
69 'move' => array( 'ApiQueryInfo', 'getMoveToken' ),
70 'block' => array( 'ApiQueryInfo', 'getBlockToken' ),
71 'unblock' => array( 'ApiQueryInfo', 'getUnblockToken' )
73 wfRunHooks('APIQueryInfoTokens', array(&$this->tokenFunctions
));
74 return $this->tokenFunctions
;
77 public static function getEditToken($pageid, $title)
79 // We could check for $title->userCan('edit') here,
80 // but that's too expensive for this purpose
82 if(!$wgUser->isAllowed('edit'))
85 // The edit token is always the same, let's exploit that
86 static $cachedEditToken = null;
87 if(!is_null($cachedEditToken))
88 return $cachedEditToken;
90 $cachedEditToken = $wgUser->editToken();
91 return $cachedEditToken;
94 public static function getDeleteToken($pageid, $title)
97 if(!$wgUser->isAllowed('delete'))
100 static $cachedDeleteToken = null;
101 if(!is_null($cachedDeleteToken))
102 return $cachedDeleteToken;
104 $cachedDeleteToken = $wgUser->editToken();
105 return $cachedDeleteToken;
108 public static function getProtectToken($pageid, $title)
111 if(!$wgUser->isAllowed('protect'))
114 static $cachedProtectToken = null;
115 if(!is_null($cachedProtectToken))
116 return $cachedProtectToken;
118 $cachedProtectToken = $wgUser->editToken();
119 return $cachedProtectToken;
122 public static function getMoveToken($pageid, $title)
125 if(!$wgUser->isAllowed('move'))
128 static $cachedMoveToken = null;
129 if(!is_null($cachedMoveToken))
130 return $cachedMoveToken;
132 $cachedMoveToken = $wgUser->editToken();
133 return $cachedMoveToken;
136 public static function getBlockToken($pageid, $title)
139 if(!$wgUser->isAllowed('block'))
142 static $cachedBlockToken = null;
143 if(!is_null($cachedBlockToken))
144 return $cachedBlockToken;
146 $cachedBlockToken = $wgUser->editToken();
147 return $cachedBlockToken;
150 public static function getUnblockToken($pageid, $title)
152 // Currently, this is exactly the same as the block token
153 return self
::getBlockToken($pageid, $title);
156 public function execute() {
160 $params = $this->extractRequestParams();
161 $fld_protection = $fld_talkid = $fld_subjectid = false;
162 if(!is_null($params['prop'])) {
163 $prop = array_flip($params['prop']);
164 $fld_protection = isset($prop['protection']);
165 $fld_talkid = isset($prop['talkid']);
166 $fld_subjectid = isset($prop['subjectid']);
169 $pageSet = $this->getPageSet();
170 $titles = $pageSet->getGoodTitles();
171 $missing = $pageSet->getMissingTitles();
172 $result = $this->getResult();
174 $pageRestrictions = $pageSet->getCustomField('page_restrictions');
175 $pageIsRedir = $pageSet->getCustomField('page_is_redirect');
176 $pageIsNew = $pageSet->getCustomField('page_is_new');
177 $pageCounter = $pageSet->getCustomField('page_counter');
178 $pageTouched = $pageSet->getCustomField('page_touched');
179 $pageLatest = $pageSet->getCustomField('page_latest');
180 $pageLength = $pageSet->getCustomField('page_len');
182 $db = $this->getDB();
183 if ($fld_protection && !empty($titles)) {
184 $this->addTables('page_restrictions');
185 $this->addFields(array('pr_page', 'pr_type', 'pr_level', 'pr_expiry', 'pr_cascade'));
186 $this->addWhereFld('pr_page', array_keys($titles));
188 $res = $this->select(__METHOD__
);
189 while($row = $db->fetchObject($res)) {
191 'type' => $row->pr_type
,
192 'level' => $row->pr_level
,
193 'expiry' => Block
::decodeExpiry( $row->pr_expiry
, TS_ISO_8601
)
197 $protections[$row->pr_page
][] = $a;
199 $db->freeResult($res);
202 foreach ($titles as $id => $title)
203 if ($title->getNamespace() == NS_IMAGE
)
205 // To avoid code duplication
206 $cascadeTypes = array(
209 'table' => 'templatelinks',
210 'ns' => 'tl_namespace',
211 'title' => 'tl_title',
212 'ids' => array_diff(array_keys($titles), $imageIds)
216 'table' => 'imagelinks',
223 foreach ($cascadeTypes as $type)
225 if (count($type['ids']) != 0) {
226 $this->resetQueryParams();
227 $this->addTables(array('page_restrictions', $type['table']));
228 $this->addTables('page', 'page_source');
229 $this->addTables('page', 'page_target');
230 $this->addFields(array('pr_type', 'pr_level', 'pr_expiry',
231 'page_target.page_id AS page_target_id',
232 'page_source.page_namespace AS page_source_namespace',
233 'page_source.page_title AS page_source_title'));
234 $this->addWhere(array("{$type['prefix']}_from = pr_page",
235 'page_target.page_namespace = '.$type['ns'],
236 'page_target.page_title = '.$type['title'],
237 'page_source.page_id = pr_page'
239 $this->addWhereFld('pr_cascade', 1);
240 $this->addWhereFld('page_target.page_id', $type['ids']);
242 $res = $this->select(__METHOD__
);
243 while($row = $db->fetchObject($res)) {
244 $source = Title
::makeTitle($row->page_source_namespace
, $row->page_source_title
);
246 'type' => $row->pr_type
,
247 'level' => $row->pr_level
,
248 'expiry' => Block
::decodeExpiry( $row->pr_expiry
, TS_ISO_8601
),
249 'source' => $source->getPrefixedText()
251 $protections[$row->page_target_id
][] = $a;
253 $db->freeResult($res);
258 // We don't need to check for pt stuff if there are no nonexistent titles
259 if($fld_protection && !empty($missing))
261 $this->resetQueryParams();
262 // Construct a custom WHERE clause that matches all titles in $missing
263 $lb = new LinkBatch($missing);
264 $this->addTables('protected_titles');
265 $this->addFields(array('pt_title', 'pt_namespace', 'pt_create_perm', 'pt_expiry'));
266 $this->addWhere($lb->constructSet('pt', $db));
267 $res = $this->select(__METHOD__
);
268 $prottitles = array();
269 while($row = $db->fetchObject($res)) {
270 $prottitles[$row->pt_namespace
][$row->pt_title
][] = array(
272 'level' => $row->pt_create_perm
,
273 'expiry' => Block
::decodeExpiry($row->pt_expiry
, TS_ISO_8601
)
276 $db->freeResult($res);
280 foreach ($missing as $title)
281 if ($title->getNamespace() == NS_IMAGE
)
282 $images[] = $title->getDbKey();
286 if (count($others) != 0) {
287 $lb = new LinkBatch($others);
288 $this->resetQueryParams();
289 $this->addTables(array('page_restrictions', 'page', 'templatelinks'));
290 $this->addFields(array('pr_type', 'pr_level', 'pr_expiry',
291 'page_title', 'page_namespace',
292 'tl_title', 'tl_namespace'));
293 $this->addWhere($lb->constructSet('tl', $db));
294 $this->addWhere('pr_page = page_id');
295 $this->addWhere('pr_page = tl_from');
296 $this->addWhereFld('pr_cascade', 1);
298 $res = $this->select(__METHOD__
);
299 while($row = $db->fetchObject($res)) {
300 $source = Title
::makeTitle($row->page_namespace
, $row->page_title
);
302 'type' => $row->pr_type
,
303 'level' => $row->pr_level
,
304 'expiry' => Block
::decodeExpiry( $row->pr_expiry
, TS_ISO_8601
),
305 'source' => $source->getPrefixedText()
307 $prottitles[$row->tl_namespace
][$row->tl_title
][] = $a;
309 $db->freeResult($res);
312 if (count($images) != 0) {
313 $this->resetQueryParams();
314 $this->addTables(array('page_restrictions', 'page', 'imagelinks'));
315 $this->addFields(array('pr_type', 'pr_level', 'pr_expiry',
316 'page_title', 'page_namespace', 'il_to'));
317 $this->addWhere('pr_page = page_id');
318 $this->addWhere('pr_page = il_from');
319 $this->addWhereFld('pr_cascade', 1);
320 $this->addWhereFld('il_to', $images);
322 $res = $this->select(__METHOD__
);
323 while($row = $db->fetchObject($res)) {
324 $source = Title
::makeTitle($row->page_namespace
, $row->page_title
);
326 'type' => $row->pr_type
,
327 'level' => $row->pr_level
,
328 'expiry' => Block
::decodeExpiry( $row->pr_expiry
, TS_ISO_8601
),
329 'source' => $source->getPrefixedText()
331 $prottitles[NS_IMAGE
][$row->il_to
][] = $a;
333 $db->freeResult($res);
337 // Run the talkid/subjectid query
338 if($fld_talkid ||
$fld_subjectid)
340 $talktitles = $subjecttitles =
341 $talkids = $subjectids = array();
342 $everything = array_merge($titles, $missing);
343 foreach($everything as $t)
345 if(MWNamespace
::isTalk($t->getNamespace()))
348 $subjecttitles[] = $t->getSubjectPage();
351 $talktitles[] = $t->getTalkPage();
353 if(!empty($talktitles) ||
!empty($subjecttitles))
355 // Construct a custom WHERE clause that matches
356 // all titles in $talktitles and $subjecttitles
357 $lb = new LinkBatch(array_merge($talktitles, $subjecttitles));
358 $this->resetQueryParams();
359 $this->addTables('page');
360 $this->addFields(array('page_title', 'page_namespace', 'page_id'));
361 $this->addWhere($lb->constructSet('page', $db));
362 $res = $this->select(__METHOD__
);
363 while($row = $db->fetchObject($res))
365 if(MWNamespace
::isTalk($row->page_namespace
))
366 $talkids[MWNamespace
::getSubject($row->page_namespace
)][$row->page_title
] = $row->page_id
;
368 $subjectids[MWNamespace
::getTalk($row->page_namespace
)][$row->page_title
] = $row->page_id
;
373 foreach ( $titles as $pageid => $title ) {
375 'touched' => wfTimestamp(TS_ISO_8601
, $pageTouched[$pageid]),
376 'lastrevid' => intval($pageLatest[$pageid]),
377 'counter' => intval($pageCounter[$pageid]),
378 'length' => intval($pageLength[$pageid]),
381 if ($pageIsRedir[$pageid])
382 $pageInfo['redirect'] = '';
384 if ($pageIsNew[$pageid])
385 $pageInfo['new'] = '';
387 if (!is_null($params['token'])) {
388 $tokenFunctions = $this->getTokenFunctions();
389 foreach($params['token'] as $t)
391 $val = call_user_func($tokenFunctions[$t], $pageid, $title);
393 $this->setWarning("Action '$t' is not allowed for the current user");
395 $pageInfo[$t . 'token'] = $val;
399 if($fld_protection) {
400 if (isset($protections[$pageid])) {
401 $pageInfo['protection'] = $protections[$pageid];
402 $result->setIndexedTagName($pageInfo['protection'], 'pr');
404 # Also check old restrictions
405 if( $pageRestrictions[$pageid] ) {
406 foreach( explode( ':', trim( $pageRestrictions[$pageid] ) ) as $restrict ) {
407 $temp = explode( '=', trim( $restrict ) );
408 if(count($temp) == 1) {
409 // old old format should be treated as edit/move restriction
410 $restriction = trim( $temp[0] );
411 $pageInfo['protection'][] = array(
413 'level' => $restriction,
414 'expiry' => 'infinity',
416 $pageInfo['protection'][] = array(
418 'level' => $restriction,
419 'expiry' => 'infinity',
422 $restriction = trim( $temp[1] );
423 $pageInfo['protection'][] = array(
425 'level' => $restriction,
426 'expiry' => 'infinity',
430 $result->setIndexedTagName($pageInfo['protection'], 'pr');
432 $pageInfo['protection'] = array();
436 if($fld_talkid && isset($talkids[$title->getNamespace()][$title->getDbKey()]))
437 $pageInfo['talkid'] = $talkids[$title->getNamespace()][$title->getDbKey()];
438 if($fld_subjectid && isset($subjectids[$title->getNamespace()][$title->getDbKey()]))
439 $pageInfo['subjectid'] = $subjectids[$title->getNamespace()][$title->getDbKey()];
441 $result->addValue(array (
444 ), $pageid, $pageInfo);
447 // Get edit/protect tokens and protection data for missing titles if requested
448 // Delete and move tokens are N/A for missing titles anyway
449 if(!is_null($params['token']) ||
$fld_protection ||
$fld_talkid ||
$fld_subjectid)
451 $res = &$result->getData();
452 foreach($missing as $pageid => $title) {
453 if(!is_null($params['token']))
455 $tokenFunctions = $this->getTokenFunctions();
456 foreach($params['token'] as $t)
458 $val = call_user_func($tokenFunctions[$t], $pageid, $title);
460 $res['query']['pages'][$pageid][$t . 'token'] = $val;
465 // Apparently the XML formatting code doesn't like array(null)
466 // This is painful to fix, so we'll just work around it
467 if(isset($prottitles[$title->getNamespace()][$title->getDBkey()]))
468 $res['query']['pages'][$pageid]['protection'] = $prottitles[$title->getNamespace()][$title->getDBkey()];
470 $res['query']['pages'][$pageid]['protection'] = array();
471 $result->setIndexedTagName($res['query']['pages'][$pageid]['protection'], 'pr');
473 if($fld_talkid && isset($talkids[$title->getNamespace()][$title->getDbKey()]))
474 $res['query']['pages'][$pageid]['talkid'] = $talkids[$title->getNamespace()][$title->getDbKey()];
475 if($fld_subjectid && isset($subjectids[$title->getNamespace()][$title->getDbKey()]))
476 $res['query']['pages'][$pageid]['subjectid'] = $subjectids[$title->getNamespace()][$title->getDbKey()];
481 public function getAllowedParams() {
484 ApiBase
:: PARAM_DFLT
=> NULL,
485 ApiBase
:: PARAM_ISMULTI
=> true,
486 ApiBase
:: PARAM_TYPE
=> array (
492 ApiBase
:: PARAM_DFLT
=> NULL,
493 ApiBase
:: PARAM_ISMULTI
=> true,
494 ApiBase
:: PARAM_TYPE
=> array_keys($this->getTokenFunctions())
499 public function getParamDescription() {
502 'Which additional properties to get:',
503 ' "protection" - List the protection level of each page',
504 ' "talkid" - The page ID of the talk page for each non-talk page',
505 ' "subjectid" - The page ID of the parent page for each talk page'
507 'token' => 'Request a token to perform a data-modifying action on a page',
512 public function getDescription() {
513 return 'Get basic page information such as namespace, title, last touched date, ...';
516 protected function getExamples() {
518 'api.php?action=query&prop=info&titles=Main%20Page',
519 'api.php?action=query&prop=info&inprop=protection&titles=Main%20Page'
523 public function getVersion() {
524 return __CLASS__
. ': $Id$';