Fix some more regressions from r46845 reported by Brad Jorsch on the mediawiki-api...
[lhc/web/wiklou.git] / includes / api / ApiQueryDeletedrevs.php
1 <?php
2
3 /*
4 * Created on Jul 2, 2007
5 *
6 * API for MediaWiki 1.8+
7 *
8 * Copyright (C) 2007 Roan Kattouw <Firstname>.<Lastname>@home.nl
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 * Query module to enumerate all available pages.
33 *
34 * @ingroup API
35 */
36 class ApiQueryDeletedrevs extends ApiQueryBase {
37
38 public function __construct($query, $moduleName) {
39 parent :: __construct($query, $moduleName, 'dr');
40 }
41
42 public function execute() {
43
44 global $wgUser;
45 // Before doing anything at all, let's check permissions
46 if(!$wgUser->isAllowed('deletedhistory'))
47 $this->dieUsage('You don\'t have permission to view deleted revision information', 'permissiondenied');
48
49 $db = $this->getDB();
50 $params = $this->extractRequestParams(false);
51 $prop = array_flip($params['prop']);
52 $fld_revid = isset($prop['revid']);
53 $fld_user = isset($prop['user']);
54 $fld_comment = isset($prop['comment']);
55 $fld_minor = isset($prop['minor']);
56 $fld_len = isset($prop['len']);
57 $fld_content = isset($prop['content']);
58 $fld_token = isset($prop['token']);
59
60 $result = $this->getResult();
61 $pageSet = $this->getPageSet();
62 $titles = $pageSet->getTitles();
63 $data = array();
64
65 // This module operates in three modes:
66 // 'revs': List deleted revs for certain titles
67 // 'user': List deleted revs by a certain user
68 // 'all': List all deleted revs
69 $mode = 'all';
70 if(count($titles) > 0)
71 $mode = 'revs';
72 else if(!is_null($params['user']))
73 $mode = 'user';
74
75 if(!is_null($params['user']) && !is_null($params['excludeuser']))
76 $this->dieUsage('user and excludeuser cannot be used together', 'badparams');
77
78 $this->addTables('archive');
79 $this->addFields(array('ar_title', 'ar_namespace', 'ar_timestamp'));
80 if($fld_revid)
81 $this->addFields('ar_rev_id');
82 if($fld_user)
83 $this->addFields('ar_user_text');
84 if($fld_comment)
85 $this->addFields('ar_comment');
86 if($fld_minor)
87 $this->addFields('ar_minor_edit');
88 if($fld_len)
89 $this->addFields('ar_len');
90 if($fld_content)
91 {
92 $this->addTables('text');
93 $this->addFields(array('ar_text', 'ar_text_id', 'old_text', 'old_flags'));
94 $this->addWhere('ar_text_id = old_id');
95
96 // This also means stricter restrictions
97 if(!$wgUser->isAllowed('undelete'))
98 $this->dieUsage('You don\'t have permission to view deleted revision content', 'permissiondenied');
99 }
100 // Check limits
101 $userMax = $fld_content ? ApiBase :: LIMIT_SML1 : ApiBase :: LIMIT_BIG1;
102 $botMax = $fld_content ? ApiBase :: LIMIT_SML2 : ApiBase :: LIMIT_BIG2;
103
104 $limit = $params['limit'];
105
106 if( $limit == 'max' ) {
107 $limit = $this->getMain()->canApiHighLimits() ? $botMax : $userMax;
108 $this->getResult()->addValue( 'limits', $this->getModuleName(), $limit );
109 }
110
111 $this->validateLimit('limit', $limit, 1, $userMax, $botMax);
112
113 if($fld_token)
114 // Undelete tokens are identical for all pages, so we cache one here
115 $token = $wgUser->editToken();
116
117 // We need a custom WHERE clause that matches all titles.
118 if($mode == 'revs')
119 {
120 $lb = new LinkBatch($titles);
121 $where = $lb->constructSet('ar', $db);
122 $this->addWhere($where);
123 }
124 elseif($mode == 'all')
125 {
126 $this->addWhereFld('ar_namespace', $params['namespace']);
127 if(!is_null($params['from']))
128 {
129 $from = $this->getDB()->strencode($this->titleToKey($params['from']));
130 $this->addWhere("ar_title >= '$from'");
131 }
132 }
133
134 if(!is_null($params['user'])) {
135 $this->addWhereFld('ar_user_text', $params['user']);
136 } elseif(!is_null($params['excludeuser'])) {
137 $this->addWhere('ar_user_text != ' .
138 $this->getDB()->addQuotes($params['excludeuser']));
139 }
140
141 if(!is_null($params['continue']) && $mode == 'all')
142 {
143 $cont = explode('|', $params['continue']);
144 if(count($cont) != 2)
145 $this->dieUsage("Invalid continue param. You should pass the original value returned by the previous query", "badcontinue");
146 $title = $this->getDB()->strencode($this->titleToKey($cont[0]));
147 $ts = $this->getDB()->strencode($cont[1]);
148 $this->addWhere("ar_title > '$title' OR " .
149 "(ar_title = '$title' AND " .
150 "ar_timestamp >= '$ts')");
151 }
152
153 $this->addOption('LIMIT', $limit + 1);
154 $this->addOption('USE INDEX', array('archive' => ($mode == 'user' ? 'usertext_timestamp' : 'name_title_timestamp')));
155 if($mode == 'all')
156 {
157 if($params['unique'])
158 {
159 $this->addOption('GROUP BY', 'ar_title');
160 $this->addOption('ORDER BY', 'ar_title');
161 }
162 else
163 $this->addOption('ORDER BY', 'ar_title, ar_timestamp');
164 }
165 else
166 {
167 if($mode == 'revs')
168 {
169 // Sort by ns and title in the same order as timestamp for efficiency
170 $this->addWhereRange('ar_namespace', $params['dir'], null, null);
171 $this->addWhereRange('ar_title', $params['dir'], null, null);
172 }
173 $this->addWhereRange('ar_timestamp', $params['dir'], $params['start'], $params['end']);
174 }
175 $res = $this->select(__METHOD__);
176 $pageMap = array(); // Maps ns&title to (fake) pageid
177 $count = 0;
178 $newPageID = 0;
179 while($row = $db->fetchObject($res))
180 {
181 if(++$count > $limit)
182 {
183 // We've had enough
184 if($mode == 'all')
185 $this->setContinueEnumParameter('continue', $this->keyToTitle($row->ar_title) . '|' .
186 $row->ar_timestamp);
187 else
188 $this->setContinueEnumParameter('start', wfTimestamp(TS_ISO_8601, $row->ar_timestamp));
189 break;
190 }
191
192 $rev = array();
193 $rev['timestamp'] = wfTimestamp(TS_ISO_8601, $row->ar_timestamp);
194 if($fld_revid)
195 $rev['revid'] = $row->ar_rev_id;
196 if($fld_user)
197 $rev['user'] = $row->ar_user_text;
198 if($fld_comment)
199 $rev['comment'] = $row->ar_comment;
200 if($fld_minor)
201 if($row->ar_minor_edit == 1)
202 $rev['minor'] = '';
203 if($fld_len)
204 $rev['len'] = $row->ar_len;
205 if($fld_content)
206 ApiResult::setContent($rev, Revision::getRevisionText($row));
207
208 if(!isset($pageMap[$row->ar_namespace][$row->ar_title]))
209 {
210 $pageID = $newPageID++;
211 $pageMap[$row->ar_namespace][$row->ar_title] = $pageID;
212 $t = Title::makeTitle($row->ar_namespace, $row->ar_title);
213 $a = array(
214 'title' => $t->getPrefixedText(),
215 'ns' => intval($row->ar_namespace),
216 'revisions' => array($rev)
217 );
218 $result->setIndexedTagName($a['revisions'], 'rev');
219 if($fld_token)
220 $a['token'] = $token;
221 $fit = $result->addValue(array('query', $this->getModuleName()), $pageID, $a);
222 }
223 else
224 {
225 $pageID = $pageMap[$row->ar_namespace][$row->ar_title];
226 $fit = $result->addValue(
227 array('query', $this->getModuleName(), $pageID, 'revisions'),
228 null, $rev);
229 }
230 if(!$fit)
231 {
232 if($mode == 'all')
233 $this->setContinueEnumParameter('continue', $this->keyToTitle($row->ar_title) . '|' .
234 $row->ar_timestamp);
235 else
236 $this->setContinueEnumParameter('start', wfTimestamp(TS_ISO_8601, $row->ar_timestamp));
237 break;
238 }
239 }
240 $db->freeResult($res);
241 $result->setIndexedTagName_internal(array('query', $this->getModuleName()), 'page');
242 }
243
244 public function getAllowedParams() {
245 return array (
246 'start' => array(
247 ApiBase :: PARAM_TYPE => 'timestamp'
248 ),
249 'end' => array(
250 ApiBase :: PARAM_TYPE => 'timestamp',
251 ),
252 'dir' => array(
253 ApiBase :: PARAM_TYPE => array(
254 'newer',
255 'older'
256 ),
257 ApiBase :: PARAM_DFLT => 'older'
258 ),
259 'from' => null,
260 'continue' => null,
261 'unique' => false,
262 'user' => array(
263 ApiBase :: PARAM_TYPE => 'user'
264 ),
265 'excludeuser' => array(
266 ApiBase :: PARAM_TYPE => 'user'
267 ),
268 'namespace' => array(
269 ApiBase :: PARAM_TYPE => 'namespace',
270 ApiBase :: PARAM_DFLT => 0,
271 ),
272 'limit' => array(
273 ApiBase :: PARAM_DFLT => 10,
274 ApiBase :: PARAM_TYPE => 'limit',
275 ApiBase :: PARAM_MIN => 1,
276 ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1,
277 ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
278 ),
279 'prop' => array(
280 ApiBase :: PARAM_DFLT => 'user|comment',
281 ApiBase :: PARAM_TYPE => array(
282 'revid',
283 'user',
284 'comment',
285 'minor',
286 'len',
287 'content',
288 'token'
289 ),
290 ApiBase :: PARAM_ISMULTI => true
291 ),
292 );
293 }
294
295 public function getParamDescription() {
296 return array (
297 'start' => 'The timestamp to start enumerating from. (1,2)',
298 'end' => 'The timestamp to stop enumerating at. (1,2)',
299 'dir' => 'The direction in which to enumerate. (1,2)',
300 'limit' => 'The maximum amount of revisions to list',
301 'prop' => 'Which properties to get',
302 'namespace' => 'Only list pages in this namespace (3)',
303 'user' => 'Only list revisions by this user',
304 'excludeuser' => 'Don\'t list revisions by this user',
305 'from' => 'Start listing at this title (3)',
306 'continue' => 'When more results are available, use this to continue (3)',
307 'unique' => 'List only one revision for each page (3)',
308 );
309 }
310
311 public function getDescription() {
312 return array( 'List deleted revisions.',
313 'This module operates in three modes:',
314 '1) List deleted revisions for the given title(s), sorted by timestamp',
315 '2) List deleted contributions for the given user, sorted by timestamp (no titles specified)',
316 '3) List all deleted revisions in the given namespace, sorted by title and timestamp (no titles specified, druser not set)',
317 'Certain parameters only apply to some modes and are ignored in others.',
318 'For instance, a parameter marked (1) only applies to mode 1 and is ignored in modes 2 and 3.',
319 );
320 }
321
322 protected function getExamples() {
323 return array (
324 'List the last deleted revisions of Main Page and Talk:Main Page, with content (mode 1):',
325 ' api.php?action=query&list=deletedrevs&titles=Main%20Page|Talk:Main%20Page&drprop=user|comment|content',
326 'List the last 50 deleted contributions by Bob (mode 2):',
327 ' api.php?action=query&list=deletedrevs&druser=Bob&drlimit=50',
328 'List the first 50 deleted revisions in the main namespace (mode 3):',
329 ' api.php?action=query&list=deletedrevs&drdir=newer&drlimit=50',
330 'List the first 50 deleted pages in the Talk namespace (mode 3):',
331 ' api.php?action=query&list=deletedrevs&drdir=newer&drlimit=50&drnamespace=1&drunique',
332 );
333 }
334
335 public function getVersion() {
336 return __CLASS__ . ': $Id$';
337 }
338 }