Provide some info on which case value was not handled in ApiLogin
[lhc/web/wiklou.git] / includes / api / ApiQueryInfo.php
1 <?php
2
3 /*
4 * Created on Sep 25, 2006
5 *
6 * API for MediaWiki 1.8+
7 *
8 * Copyright (C) 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
9 *
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.
14 *
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.
19 *
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
24 */
25
26 if (!defined('MEDIAWIKI')) {
27 // Eclipse helper - will be ignored in production
28 require_once ('ApiQueryBase.php');
29 }
30
31 /**
32 * A query module to show basic page information.
33 *
34 * @ingroup API
35 */
36 class ApiQueryInfo extends ApiQueryBase {
37
38 public function __construct($query, $moduleName) {
39 parent :: __construct($query, $moduleName, 'in');
40 }
41
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');
50 }
51
52 protected function getTokenFunctions() {
53 // tokenname => function
54 // function prototype is func($pageid, $title)
55 // should return token or false
56
57 // Don't call the hooks twice
58 if(isset($this->tokenFunctions))
59 return $this->tokenFunctions;
60
61 // If we're in JSON callback mode, no tokens can be obtained
62 if(!is_null($this->getMain()->getRequest()->getVal('callback')))
63 return array();
64
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' )
72 );
73 wfRunHooks('APIQueryInfoTokens', array(&$this->tokenFunctions));
74 return $this->tokenFunctions;
75 }
76
77 public static function getEditToken($pageid, $title)
78 {
79 // We could check for $title->userCan('edit') here,
80 // but that's too expensive for this purpose
81 global $wgUser;
82 if(!$wgUser->isAllowed('edit'))
83 return false;
84
85 // The edit token is always the same, let's exploit that
86 static $cachedEditToken = null;
87 if(!is_null($cachedEditToken))
88 return $cachedEditToken;
89
90 $cachedEditToken = $wgUser->editToken();
91 return $cachedEditToken;
92 }
93
94 public static function getDeleteToken($pageid, $title)
95 {
96 global $wgUser;
97 if(!$wgUser->isAllowed('delete'))
98 return false;
99
100 static $cachedDeleteToken = null;
101 if(!is_null($cachedDeleteToken))
102 return $cachedDeleteToken;
103
104 $cachedDeleteToken = $wgUser->editToken();
105 return $cachedDeleteToken;
106 }
107
108 public static function getProtectToken($pageid, $title)
109 {
110 global $wgUser;
111 if(!$wgUser->isAllowed('protect'))
112 return false;
113
114 static $cachedProtectToken = null;
115 if(!is_null($cachedProtectToken))
116 return $cachedProtectToken;
117
118 $cachedProtectToken = $wgUser->editToken();
119 return $cachedProtectToken;
120 }
121
122 public static function getMoveToken($pageid, $title)
123 {
124 global $wgUser;
125 if(!$wgUser->isAllowed('move'))
126 return false;
127
128 static $cachedMoveToken = null;
129 if(!is_null($cachedMoveToken))
130 return $cachedMoveToken;
131
132 $cachedMoveToken = $wgUser->editToken();
133 return $cachedMoveToken;
134 }
135
136 public static function getBlockToken($pageid, $title)
137 {
138 global $wgUser;
139 if(!$wgUser->isAllowed('block'))
140 return false;
141
142 static $cachedBlockToken = null;
143 if(!is_null($cachedBlockToken))
144 return $cachedBlockToken;
145
146 $cachedBlockToken = $wgUser->editToken();
147 return $cachedBlockToken;
148 }
149
150 public static function getUnblockToken($pageid, $title)
151 {
152 // Currently, this is exactly the same as the block token
153 return self::getBlockToken($pageid, $title);
154 }
155
156 public function execute() {
157
158 global $wgUser;
159
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']);
167 }
168
169 $pageSet = $this->getPageSet();
170 $titles = $pageSet->getGoodTitles();
171 $missing = $pageSet->getMissingTitles();
172 $result = $this->getResult();
173
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');
181
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));
187
188 $res = $this->select(__METHOD__);
189 while($row = $db->fetchObject($res)) {
190 $a = array(
191 'type' => $row->pr_type,
192 'level' => $row->pr_level,
193 'expiry' => Block::decodeExpiry( $row->pr_expiry, TS_ISO_8601 )
194 );
195 if($row->pr_cascade)
196 $a['cascade'] = '';
197 $protections[$row->pr_page][] = $a;
198 }
199 $db->freeResult($res);
200
201 $imageIds = array();
202 foreach ($titles as $id => $title)
203 if ($title->getNamespace() == NS_IMAGE)
204 $imageIds[] = $id;
205 // To avoid code duplication
206 $cascadeTypes = array(
207 array(
208 'prefix' => 'tl',
209 'table' => 'templatelinks',
210 'ns' => 'tl_namespace',
211 'title' => 'tl_title',
212 'ids' => array_diff(array_keys($titles), $imageIds)
213 ),
214 array(
215 'prefix' => 'il',
216 'table' => 'imagelinks',
217 'ns' => NS_IMAGE,
218 'title' => 'il_to',
219 'ids' => $imageIds
220 )
221 );
222
223 foreach ($cascadeTypes as $type)
224 {
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'
238 ));
239 $this->addWhereFld('pr_cascade', 1);
240 $this->addWhereFld('page_target.page_id', $type['ids']);
241
242 $res = $this->select(__METHOD__);
243 while($row = $db->fetchObject($res)) {
244 $source = Title::makeTitle($row->page_source_namespace, $row->page_source_title);
245 $a = array(
246 'type' => $row->pr_type,
247 'level' => $row->pr_level,
248 'expiry' => Block::decodeExpiry( $row->pr_expiry, TS_ISO_8601 ),
249 'source' => $source->getPrefixedText()
250 );
251 $protections[$row->page_target_id][] = $a;
252 }
253 $db->freeResult($res);
254 }
255 }
256 }
257
258 // We don't need to check for pt stuff if there are no nonexistent titles
259 if($fld_protection && !empty($missing))
260 {
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(
271 'type' => 'create',
272 'level' => $row->pt_create_perm,
273 'expiry' => Block::decodeExpiry($row->pt_expiry, TS_ISO_8601)
274 );
275 }
276 $db->freeResult($res);
277
278 $images = array();
279 $others = array();
280 foreach ($missing as $title)
281 if ($title->getNamespace() == NS_IMAGE)
282 $images[] = $title->getDbKey();
283 else
284 $others[] = $title;
285
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);
297
298 $res = $this->select(__METHOD__);
299 while($row = $db->fetchObject($res)) {
300 $source = Title::makeTitle($row->page_namespace, $row->page_title);
301 $a = array(
302 'type' => $row->pr_type,
303 'level' => $row->pr_level,
304 'expiry' => Block::decodeExpiry( $row->pr_expiry, TS_ISO_8601 ),
305 'source' => $source->getPrefixedText()
306 );
307 $prottitles[$row->tl_namespace][$row->tl_title][] = $a;
308 }
309 $db->freeResult($res);
310 }
311
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);
321
322 $res = $this->select(__METHOD__);
323 while($row = $db->fetchObject($res)) {
324 $source = Title::makeTitle($row->page_namespace, $row->page_title);
325 $a = array(
326 'type' => $row->pr_type,
327 'level' => $row->pr_level,
328 'expiry' => Block::decodeExpiry( $row->pr_expiry, TS_ISO_8601 ),
329 'source' => $source->getPrefixedText()
330 );
331 $prottitles[NS_IMAGE][$row->il_to][] = $a;
332 }
333 $db->freeResult($res);
334 }
335 }
336
337 // Run the talkid/subjectid query
338 if($fld_talkid || $fld_subjectid)
339 {
340 $talktitles = $subjecttitles =
341 $talkids = $subjectids = array();
342 $everything = array_merge($titles, $missing);
343 foreach($everything as $t)
344 {
345 if(MWNamespace::isTalk($t->getNamespace()))
346 {
347 if($fld_subjectid)
348 $subjecttitles[] = $t->getSubjectPage();
349 }
350 else if($fld_talkid)
351 $talktitles[] = $t->getTalkPage();
352 }
353 if(!empty($talktitles) || !empty($subjecttitles))
354 {
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))
364 {
365 if(MWNamespace::isTalk($row->page_namespace))
366 $talkids[MWNamespace::getSubject($row->page_namespace)][$row->page_title] = $row->page_id;
367 else
368 $subjectids[MWNamespace::getTalk($row->page_namespace)][$row->page_title] = $row->page_id;
369 }
370 }
371 }
372
373 foreach ( $titles as $pageid => $title ) {
374 $pageInfo = array (
375 'touched' => wfTimestamp(TS_ISO_8601, $pageTouched[$pageid]),
376 'lastrevid' => intval($pageLatest[$pageid]),
377 'counter' => intval($pageCounter[$pageid]),
378 'length' => intval($pageLength[$pageid]),
379 );
380
381 if ($pageIsRedir[$pageid])
382 $pageInfo['redirect'] = '';
383
384 if ($pageIsNew[$pageid])
385 $pageInfo['new'] = '';
386
387 if (!is_null($params['token'])) {
388 $tokenFunctions = $this->getTokenFunctions();
389 foreach($params['token'] as $t)
390 {
391 $val = call_user_func($tokenFunctions[$t], $pageid, $title);
392 if($val === false)
393 $this->setWarning("Action '$t' is not allowed for the current user");
394 else
395 $pageInfo[$t . 'token'] = $val;
396 }
397 }
398
399 if($fld_protection) {
400 if (isset($protections[$pageid])) {
401 $pageInfo['protection'] = $protections[$pageid];
402 $result->setIndexedTagName($pageInfo['protection'], 'pr');
403 } else {
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(
412 'type' => 'edit',
413 'level' => $restriction,
414 'expiry' => 'infinity',
415 );
416 $pageInfo['protection'][] = array(
417 'type' => 'move',
418 'level' => $restriction,
419 'expiry' => 'infinity',
420 );
421 } else {
422 $restriction = trim( $temp[1] );
423 $pageInfo['protection'][] = array(
424 'type' => $temp[0],
425 'level' => $restriction,
426 'expiry' => 'infinity',
427 );
428 }
429 }
430 $result->setIndexedTagName($pageInfo['protection'], 'pr');
431 } else {
432 $pageInfo['protection'] = array();
433 }
434 }
435 }
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()];
440
441 $result->addValue(array (
442 'query',
443 'pages'
444 ), $pageid, $pageInfo);
445 }
446
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)
450 {
451 $res = &$result->getData();
452 foreach($missing as $pageid => $title) {
453 if(!is_null($params['token']))
454 {
455 $tokenFunctions = $this->getTokenFunctions();
456 foreach($params['token'] as $t)
457 {
458 $val = call_user_func($tokenFunctions[$t], $pageid, $title);
459 if($val !== false)
460 $res['query']['pages'][$pageid][$t . 'token'] = $val;
461 }
462 }
463 if($fld_protection)
464 {
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()];
469 else
470 $res['query']['pages'][$pageid]['protection'] = array();
471 $result->setIndexedTagName($res['query']['pages'][$pageid]['protection'], 'pr');
472 }
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()];
477 }
478 }
479 }
480
481 public function getAllowedParams() {
482 return array (
483 'prop' => array (
484 ApiBase :: PARAM_DFLT => NULL,
485 ApiBase :: PARAM_ISMULTI => true,
486 ApiBase :: PARAM_TYPE => array (
487 'protection',
488 'talkid',
489 'subjectid'
490 )),
491 'token' => array (
492 ApiBase :: PARAM_DFLT => NULL,
493 ApiBase :: PARAM_ISMULTI => true,
494 ApiBase :: PARAM_TYPE => array_keys($this->getTokenFunctions())
495 )
496 );
497 }
498
499 public function getParamDescription() {
500 return array (
501 'prop' => array (
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'
506 ),
507 'token' => 'Request a token to perform a data-modifying action on a page',
508 );
509 }
510
511
512 public function getDescription() {
513 return 'Get basic page information such as namespace, title, last touched date, ...';
514 }
515
516 protected function getExamples() {
517 return array (
518 'api.php?action=query&prop=info&titles=Main%20Page',
519 'api.php?action=query&prop=info&inprop=protection&titles=Main%20Page'
520 );
521 }
522
523 public function getVersion() {
524 return __CLASS__ . ': $Id$';
525 }
526 }