API: Expose cascading protection via inprop=protection for non-existent pages.
[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 * @addtogroup 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 public function execute() {
53
54 global $wgUser;
55
56 $params = $this->extractRequestParams();
57 $fld_protection = $fld_talkid = $fld_subjectid = false;
58 if(!is_null($params['prop'])) {
59 $prop = array_flip($params['prop']);
60 $fld_protection = isset($prop['protection']);
61 $fld_talkid = isset($prop['talkid']);
62 $fld_subjectid = isset($prop['subjectid']);
63 }
64 if(!is_null($params['token'])) {
65 $token = $params['token'];
66 $tok_edit = $this->getTokenFlag($token, 'edit');
67 $tok_delete = $this->getTokenFlag($token, 'delete');
68 $tok_protect = $this->getTokenFlag($token, 'protect');
69 $tok_move = $this->getTokenFlag($token, 'move');
70 }
71 else
72 // Fix E_NOTICEs about unset variables
73 $token = $tok_edit = $tok_delete = $tok_protect = $tok_move = null;
74
75 $pageSet = $this->getPageSet();
76 $titles = $pageSet->getGoodTitles();
77 $missing = $pageSet->getMissingTitles();
78 $result = $this->getResult();
79
80 $pageRestrictions = $pageSet->getCustomField('page_restrictions');
81 $pageIsRedir = $pageSet->getCustomField('page_is_redirect');
82 $pageIsNew = $pageSet->getCustomField('page_is_new');
83 $pageCounter = $pageSet->getCustomField('page_counter');
84 $pageTouched = $pageSet->getCustomField('page_touched');
85 $pageLatest = $pageSet->getCustomField('page_latest');
86 $pageLength = $pageSet->getCustomField('page_len');
87
88 $db = $this->getDB();
89 if ($fld_protection && !empty($titles)) {
90 $this->addTables('page_restrictions');
91 $this->addFields(array('pr_page', 'pr_type', 'pr_level', 'pr_expiry', 'pr_cascade'));
92 $this->addWhereFld('pr_page', array_keys($titles));
93
94 $res = $this->select(__METHOD__);
95 while($row = $db->fetchObject($res)) {
96 $a = array(
97 'type' => $row->pr_type,
98 'level' => $row->pr_level,
99 'expiry' => Block::decodeExpiry( $row->pr_expiry, TS_ISO_8601 )
100 );
101 if($row->pr_cascade)
102 $a['cascade'] = '';
103 $protections[$row->pr_page][] = $a;
104 }
105 $db->freeResult($res);
106
107 $imageIds = array();
108 foreach ($titles as $id => $title)
109 if ($title->getNamespace() == NS_IMAGE)
110 $imageIds[] = $id;
111 // To avoid code duplication
112 $cascadeTypes = array(
113 array(
114 'prefix' => 'tl',
115 'table' => 'templatelinks',
116 'ns' => 'tl_namespace',
117 'title' => 'tl_title',
118 'ids' => array_diff(array_keys($titles), $imageIds)
119 ),
120 array(
121 'prefix' => 'il',
122 'table' => 'imagelinks',
123 'ns' => NS_IMAGE,
124 'title' => 'il_to',
125 'ids' => $imageIds
126 )
127 );
128
129 foreach ($cascadeTypes as $type)
130 {
131 if (count($type['ids']) != 0) {
132 $this->resetQueryParams();
133 $this->addTables(array('page_restrictions', $type['table']));
134 $this->addTables('page', 'page_source');
135 $this->addTables('page', 'page_target');
136 $this->addFields(array('pr_type', 'pr_level', 'pr_expiry',
137 'page_target.page_id AS page_target_id',
138 'page_source.page_namespace AS page_source_namespace',
139 'page_source.page_title AS page_source_title'));
140 $this->addWhere(array("{$type['prefix']}_from = pr_page",
141 'page_target.page_namespace = '.$type['ns'],
142 'page_target.page_title = '.$type['title'],
143 'page_source.page_id = pr_page'
144 ));
145 $this->addWhereFld('pr_cascade', 1);
146 $this->addWhereFld('page_target.page_id', $type['ids']);
147
148 $res = $this->select(__METHOD__);
149 while($row = $db->fetchObject($res)) {
150 $source = Title::makeTitle($row->page_source_namespace, $row->page_source_title);
151 $a = array(
152 'type' => $row->pr_type,
153 'level' => $row->pr_level,
154 'expiry' => Block::decodeExpiry( $row->pr_expiry, TS_ISO_8601 ),
155 'source' => $source->getPrefixedText()
156 );
157 $protections[$row->page_target_id][] = $a;
158 }
159 $db->freeResult($res);
160 }
161 }
162 }
163
164 // We don't need to check for pt stuff if there are no nonexistent titles
165 if($fld_protection && !empty($missing))
166 {
167 $this->resetQueryParams();
168 // Construct a custom WHERE clause that matches all titles in $missing
169 $lb = new LinkBatch($missing);
170 $this->addTables('protected_titles');
171 $this->addFields(array('pt_title', 'pt_namespace', 'pt_create_perm', 'pt_expiry'));
172 $this->addWhere($lb->constructSet('pt', $db));
173 $res = $this->select(__METHOD__);
174 $prottitles = array();
175 while($row = $db->fetchObject($res)) {
176 $prottitles[$row->pt_namespace][$row->pt_title][] = array(
177 'type' => 'create',
178 'level' => $row->pt_create_perm,
179 'expiry' => Block::decodeExpiry($row->pt_expiry, TS_ISO_8601)
180 );
181 }
182 $db->freeResult($res);
183
184 $images = array();
185 $others = array();
186 foreach ($missing as $title)
187 if ($title->getNamespace() == NS_IMAGE)
188 $images[] = $title->getDbKey();
189 else
190 $others[] = $title;
191
192 if (count($others) != 0) {
193 $lb = new LinkBatch($others);
194 $this->resetQueryParams();
195 $this->addTables(array('page_restrictions', 'page', 'templatelinks'));
196 $this->addFields(array('pr_type', 'pr_level', 'pr_expiry',
197 'page_title', 'page_namespace',
198 'tl_title', 'tl_namespace'));
199 $this->addWhere($lb->constructSet('tl', $db));
200 $this->addWhere('pr_page = page_id');
201 $this->addWhere('pr_page = tl_from');
202 $this->addWhereFld('pr_cascade', 1);
203
204 $res = $this->select(__METHOD__);
205 while($row = $db->fetchObject($res)) {
206 $source = Title::makeTitle($row->page_namespace, $row->page_title);
207 $a = array(
208 'type' => $row->pr_type,
209 'level' => $row->pr_level,
210 'expiry' => Block::decodeExpiry( $row->pr_expiry, TS_ISO_8601 ),
211 'source' => $source->getPrefixedText()
212 );
213 $prottitles[$row->tl_namespace][$row->tl_title][] = $a;
214 }
215 $db->freeResult($res);
216 }
217
218 if (count($images) != 0) {
219 $this->resetQueryParams();
220 $this->addTables(array('page_restrictions', 'page', 'imagelinks'));
221 $this->addFields(array('pr_type', 'pr_level', 'pr_expiry',
222 'page_title', 'page_namespace', 'il_to'));
223 $this->addWhere('pr_page = page_id');
224 $this->addWhere('pr_page = il_from');
225 $this->addWhereFld('pr_cascade', 1);
226 $this->addWhereFld('il_to', $images);
227
228 $res = $this->select(__METHOD__);
229 while($row = $db->fetchObject($res)) {
230 $source = Title::makeTitle($row->page_namespace, $row->page_title);
231 $a = array(
232 'type' => $row->pr_type,
233 'level' => $row->pr_level,
234 'expiry' => Block::decodeExpiry( $row->pr_expiry, TS_ISO_8601 ),
235 'source' => $source->getPrefixedText()
236 );
237 $prottitles[NS_IMAGE][$row->il_to][] = $a;
238 }
239 $db->freeResult($res);
240 }
241 }
242
243 // Run the talkid/subjectid query
244 if($fld_talkid || $fld_subjectid)
245 {
246 $talktitles = $subjecttitles =
247 $talkids = $subjectids = array();
248 $everything = array_merge($titles, $missing);
249 foreach($everything as $t)
250 {
251 if(MWNamespace::isTalk($t->getNamespace()))
252 {
253 if($fld_subjectid)
254 $subjecttitles[] = $t->getSubjectPage();
255 }
256 else if($fld_talkid)
257 $talktitles[] = $t->getTalkPage();
258 }
259 if(!empty($talktitles) || !empty($subjecttitles))
260 {
261 // Construct a custom WHERE clause that matches
262 // all titles in $talktitles and $subjecttitles
263 $lb = new LinkBatch(array_merge($talktitles, $subjecttitles));
264 $this->resetQueryParams();
265 $this->addTables('page');
266 $this->addFields(array('page_title', 'page_namespace', 'page_id'));
267 $this->addWhere($lb->constructSet('page', $db));
268 $res = $this->select(__METHOD__);
269 while($row = $db->fetchObject($res))
270 {
271 if(MWNamespace::isTalk($row->page_namespace))
272 $talkids[MWNamespace::getSubject($row->page_namespace)][$row->page_title] = $row->page_id;
273 else
274 $subjectids[MWNamespace::getTalk($row->page_namespace)][$row->page_title] = $row->page_id;
275 }
276 }
277 }
278
279 foreach ( $titles as $pageid => $title ) {
280 $pageInfo = array (
281 'touched' => wfTimestamp(TS_ISO_8601, $pageTouched[$pageid]),
282 'lastrevid' => intval($pageLatest[$pageid]),
283 'counter' => intval($pageCounter[$pageid]),
284 'length' => intval($pageLength[$pageid]),
285 );
286
287 if ($pageIsRedir[$pageid])
288 $pageInfo['redirect'] = '';
289
290 if ($pageIsNew[$pageid])
291 $pageInfo['new'] = '';
292
293 if (!is_null($token)) {
294 // Currently all tokens are generated the same way, but it might change
295 if ($tok_edit)
296 $pageInfo['edittoken'] = $wgUser->editToken();
297 if ($tok_delete)
298 $pageInfo['deletetoken'] = $wgUser->editToken();
299 if ($tok_protect)
300 $pageInfo['protecttoken'] = $wgUser->editToken();
301 if ($tok_move)
302 $pageInfo['movetoken'] = $wgUser->editToken();
303 }
304
305 if($fld_protection) {
306 if (isset($protections[$pageid])) {
307 $pageInfo['protection'] = $protections[$pageid];
308 $result->setIndexedTagName($pageInfo['protection'], 'pr');
309 } else {
310 # Also check old restrictions
311 if( $pageRestrictions[$pageid] ) {
312 foreach( explode( ':', trim( $pageRestrictions[$pageid] ) ) as $restrict ) {
313 $temp = explode( '=', trim( $restrict ) );
314 if(count($temp) == 1) {
315 // old old format should be treated as edit/move restriction
316 $restriction = trim( $temp[0] );
317 $pageInfo['protection'][] = array(
318 'type' => 'edit',
319 'level' => $restriction,
320 'expiry' => 'infinity',
321 );
322 $pageInfo['protection'][] = array(
323 'type' => 'move',
324 'level' => $restriction,
325 'expiry' => 'infinity',
326 );
327 } else {
328 $restriction = trim( $temp[1] );
329 $pageInfo['protection'][] = array(
330 'type' => $temp[0],
331 'level' => $restriction,
332 'expiry' => 'infinity',
333 );
334 }
335 }
336 $result->setIndexedTagName($pageInfo['protection'], 'pr');
337 } else {
338 $pageInfo['protection'] = array();
339 }
340 }
341 }
342 if($fld_talkid && isset($talkids[$title->getNamespace()][$title->getDbKey()]))
343 $pageInfo['talkid'] = $talkids[$title->getNamespace()][$title->getDbKey()];
344 if($fld_subjectid && isset($subjectids[$title->getNamespace()][$title->getDbKey()]))
345 $pageInfo['subjectid'] = $subjectids[$title->getNamespace()][$title->getDbKey()];
346
347 $result->addValue(array (
348 'query',
349 'pages'
350 ), $pageid, $pageInfo);
351 }
352
353 // Get edit/protect tokens and protection data for missing titles if requested
354 // Delete and move tokens are N/A for missing titles anyway
355 if($tok_edit || $tok_protect || $fld_protection || $fld_talkid || $fld_subjectid)
356 {
357 $res = &$result->getData();
358 foreach($missing as $pageid => $title) {
359 if($tok_edit)
360 $res['query']['pages'][$pageid]['edittoken'] = $wgUser->editToken();
361 if($tok_protect)
362 $res['query']['pages'][$pageid]['protecttoken'] = $wgUser->editToken();
363 if($fld_protection)
364 {
365 // Apparently the XML formatting code doesn't like array(null)
366 // This is painful to fix, so we'll just work around it
367 if(isset($prottitles[$title->getNamespace()][$title->getDBkey()]))
368 $res['query']['pages'][$pageid]['protection'] = $prottitles[$title->getNamespace()][$title->getDBkey()];
369 else
370 $res['query']['pages'][$pageid]['protection'] = array();
371 $result->setIndexedTagName($res['query']['pages'][$pageid]['protection'], 'pr');
372 }
373 if($fld_talkid && isset($talkids[$title->getNamespace()][$title->getDbKey()]))
374 $res['query']['pages'][$pageid]['talkid'] = $talkids[$title->getNamespace()][$title->getDbKey()];
375 if($fld_subjectid && isset($subjectids[$title->getNamespace()][$title->getDbKey()]))
376 $res['query']['pages'][$pageid]['subjectid'] = $subjectids[$title->getNamespace()][$title->getDbKey()];
377 }
378 }
379 }
380
381 public function getAllowedParams() {
382 return array (
383 'prop' => array (
384 ApiBase :: PARAM_DFLT => NULL,
385 ApiBase :: PARAM_ISMULTI => true,
386 ApiBase :: PARAM_TYPE => array (
387 'protection',
388 'talkid',
389 'subjectid'
390 )),
391 'token' => array (
392 ApiBase :: PARAM_DFLT => NULL,
393 ApiBase :: PARAM_ISMULTI => true,
394 ApiBase :: PARAM_TYPE => array (
395 'edit',
396 'delete',
397 'protect',
398 'move',
399 )),
400 );
401 }
402
403 public function getParamDescription() {
404 return array (
405 'prop' => array (
406 'Which additional properties to get:',
407 ' "protection" - List the protection level of each page',
408 ' "talkid" - The page ID of the talk page for each non-talk page',
409 ' "subjectid" - The page ID of the parent page for each talk page'
410 ),
411 'token' => 'Request a token to perform a data-modifying action on a page',
412 );
413 }
414
415
416 public function getDescription() {
417 return 'Get basic page information such as namespace, title, last touched date, ...';
418 }
419
420 protected function getExamples() {
421 return array (
422 'api.php?action=query&prop=info&titles=Main%20Page',
423 'api.php?action=query&prop=info&inprop=protection&titles=Main%20Page'
424 );
425 }
426
427 public function getVersion() {
428 return __CLASS__ . ': $Id$';
429 }
430 }