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