(bug 28070) Fix watchlist RSS for databases that store timestamps in a real timestamp...
[lhc/web/wiklou.git] / includes / api / ApiQueryDeletedrevs.php
1 <?php
2 /**
3 *
4 *
5 * Created on Jul 2, 2007
6 *
7 * Copyright © 2007 Roan Kattouw <Firstname>.<Lastname>@gmail.com
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * http://www.gnu.org/copyleft/gpl.html
23 *
24 * @file
25 */
26
27 if ( !defined( 'MEDIAWIKI' ) ) {
28 // Eclipse helper - will be ignored in production
29 require_once( 'ApiQueryBase.php' );
30 }
31
32 /**
33 * Query module to enumerate all deleted revisions.
34 *
35 * @ingroup API
36 */
37 class ApiQueryDeletedrevs extends ApiQueryBase {
38
39 public function __construct( $query, $moduleName ) {
40 parent::__construct( $query, $moduleName, 'dr' );
41 }
42
43 public function execute() {
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
50 $db = $this->getDB();
51 $params = $this->extractRequestParams( false );
52 $prop = array_flip( $params['prop'] );
53 $fld_revid = isset( $prop['revid'] );
54 $fld_user = isset( $prop['user'] );
55 $fld_userid = isset( $prop['userid'] );
56 $fld_comment = isset( $prop['comment'] );
57 $fld_parsedcomment = isset ( $prop['parsedcomment'] );
58 $fld_minor = isset( $prop['minor'] );
59 $fld_len = isset( $prop['len'] );
60 $fld_content = isset( $prop['content'] );
61 $fld_token = isset( $prop['token'] );
62
63 $result = $this->getResult();
64 $pageSet = $this->getPageSet();
65 $titles = $pageSet->getTitles();
66
67 // This module operates in three modes:
68 // 'revs': List deleted revs for certain titles
69 // 'user': List deleted revs by a certain user
70 // 'all': List all deleted revs
71 $mode = 'all';
72 if ( count( $titles ) > 0 ) {
73 $mode = 'revs';
74 } elseif ( !is_null( $params['user'] ) ) {
75 $mode = 'user';
76 }
77
78 if ( !is_null( $params['user'] ) && !is_null( $params['excludeuser'] ) ) {
79 $this->dieUsage( 'user and excludeuser cannot be used together', 'badparams' );
80 }
81
82 $this->addTables( 'archive' );
83 $this->addWhere( 'ar_deleted = 0' );
84 $this->addFields( array( 'ar_title', 'ar_namespace', 'ar_timestamp' ) );
85 if ( $fld_revid ) {
86 $this->addFields( 'ar_rev_id' );
87 }
88 if ( $fld_user ) {
89 $this->addFields( 'ar_user_text' );
90 }
91 if ( $fld_userid ) {
92 $this->addFields( 'ar_user' );
93 }
94 if ( $fld_comment || $fld_parsedcomment ) {
95 $this->addFields( 'ar_comment' );
96 }
97 if ( $fld_minor ) {
98 $this->addFields( 'ar_minor_edit' );
99 }
100 if ( $fld_len ) {
101 $this->addFields( 'ar_len' );
102 }
103 if ( $fld_content ) {
104 $this->addTables( 'text' );
105 $this->addFields( array( 'ar_text', 'ar_text_id', 'old_text', 'old_flags' ) );
106 $this->addWhere( 'ar_text_id = old_id' );
107
108 // This also means stricter restrictions
109 if ( !$wgUser->isAllowed( 'undelete' ) ) {
110 $this->dieUsage( 'You don\'t have permission to view deleted revision content', 'permissiondenied' );
111 }
112 }
113 // Check limits
114 $userMax = $fld_content ? ApiBase::LIMIT_SML1 : ApiBase::LIMIT_BIG1;
115 $botMax = $fld_content ? ApiBase::LIMIT_SML2 : ApiBase::LIMIT_BIG2;
116
117 $limit = $params['limit'];
118
119 if ( $limit == 'max' ) {
120 $limit = $this->getMain()->canApiHighLimits() ? $botMax : $userMax;
121 $this->getResult()->setParsedLimit( $this->getModuleName(), $limit );
122 }
123
124 $this->validateLimit( 'limit', $limit, 1, $userMax, $botMax );
125
126 if ( $fld_token ) {
127 // Undelete tokens are identical for all pages, so we cache one here
128 $token = $wgUser->editToken( '', $this->getMain()->getRequest() );
129 }
130
131 $dir = $params['dir'];
132
133 // We need a custom WHERE clause that matches all titles.
134 if ( $mode == 'revs' ) {
135 $lb = new LinkBatch( $titles );
136 $where = $lb->constructSet( 'ar', $db );
137 $this->addWhere( $where );
138 } elseif ( $mode == 'all' ) {
139 $this->addWhereFld( 'ar_namespace', $params['namespace'] );
140
141 $from = is_null( $params['from'] ) ? null : $this->titleToKey( $params['from'] );
142 $to = is_null( $params['to'] ) ? null : $this->titleToKey( $params['to'] );
143 $this->addWhereRange( 'ar_title', $dir, $from, $to );
144 }
145
146 if ( !is_null( $params['user'] ) ) {
147 $this->addWhereFld( 'ar_user_text', $params['user'] );
148 } elseif ( !is_null( $params['excludeuser'] ) ) {
149 $this->addWhere( 'ar_user_text != ' .
150 $this->getDB()->addQuotes( $params['excludeuser'] ) );
151 }
152
153 if ( !is_null( $params['continue'] ) && ( $mode == 'all' || $mode == 'revs' ) ) {
154 $cont = explode( '|', $params['continue'] );
155 if ( count( $cont ) != 3 ) {
156 $this->dieUsage( 'Invalid continue param. You should pass the original value returned by the previous query', 'badcontinue' );
157 }
158 $ns = intval( $cont[0] );
159 $title = $this->getDB()->strencode( $this->titleToKey( $cont[1] ) );
160 $ts = $this->getDB()->strencode( $cont[2] );
161 $op = ( $dir == 'newer' ? '>' : '<' );
162 $this->addWhere( "ar_namespace $op $ns OR " .
163 "(ar_namespace = $ns AND " .
164 "(ar_title $op '$title' OR " .
165 "(ar_title = '$title' AND " .
166 "ar_timestamp $op= '$ts')))" );
167 }
168
169 $this->addOption( 'LIMIT', $limit + 1 );
170 $this->addOption( 'USE INDEX', array( 'archive' => ( $mode == 'user' ? 'usertext_timestamp' : 'name_title_timestamp' ) ) );
171 if ( $mode == 'all' ) {
172 if ( $params['unique'] ) {
173 $this->addOption( 'GROUP BY', 'ar_title' );
174 } else {
175 $this->addOption( 'ORDER BY', 'ar_title, ar_timestamp' );
176 }
177 } else {
178 if ( $mode == 'revs' ) {
179 // Sort by ns and title in the same order as timestamp for efficiency
180 $this->addWhereRange( 'ar_namespace', $dir, null, null );
181 $this->addWhereRange( 'ar_title', $dir, null, null );
182 }
183 $this->addWhereRange( 'ar_timestamp', $dir, $params['start'], $params['end'] );
184 }
185 $res = $this->select( __METHOD__ );
186 $pageMap = array(); // Maps ns&title to (fake) pageid
187 $count = 0;
188 $newPageID = 0;
189 foreach ( $res as $row ) {
190 if ( ++$count > $limit ) {
191 // We've had enough
192 if ( $mode == 'all' || $mode == 'revs' ) {
193 $this->setContinueEnumParameter( 'continue', intval( $row->ar_namespace ) . '|' .
194 $this->keyToTitle( $row->ar_title ) . '|' . $row->ar_timestamp );
195 } else {
196 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601, $row->ar_timestamp ) );
197 }
198 break;
199 }
200
201 $rev = array();
202 $rev['timestamp'] = wfTimestamp( TS_ISO_8601, $row->ar_timestamp );
203 if ( $fld_revid ) {
204 $rev['revid'] = intval( $row->ar_rev_id );
205 }
206 if ( $fld_user ) {
207 $rev['user'] = $row->ar_user_text;
208 }
209 if ( $fld_userid ) {
210 $rev['userid'] = $row->ar_user;
211 }
212 if ( $fld_comment ) {
213 $rev['comment'] = $row->ar_comment;
214 }
215
216 $title = Title::makeTitle( $row->ar_namespace, $row->ar_title );
217
218 if ( $fld_parsedcomment ) {
219 $rev['parsedcomment'] = $wgUser->getSkin()->formatComment( $row->ar_comment, $title );
220 }
221 if ( $fld_minor && $row->ar_minor_edit == 1 ) {
222 $rev['minor'] = '';
223 }
224 if ( $fld_len ) {
225 $rev['len'] = $row->ar_len;
226 }
227 if ( $fld_content ) {
228 ApiResult::setContent( $rev, Revision::getRevisionText( $row ) );
229 }
230
231 if ( !isset( $pageMap[$row->ar_namespace][$row->ar_title] ) ) {
232 $pageID = $newPageID++;
233 $pageMap[$row->ar_namespace][$row->ar_title] = $pageID;
234 $a['revisions'] = array( $rev );
235 $result->setIndexedTagName( $a['revisions'], 'rev' );
236 ApiQueryBase::addTitleInfo( $a, $title );
237 if ( $fld_token ) {
238 $a['token'] = $token;
239 }
240 $fit = $result->addValue( array( 'query', $this->getModuleName() ), $pageID, $a );
241 } else {
242 $pageID = $pageMap[$row->ar_namespace][$row->ar_title];
243 $fit = $result->addValue(
244 array( 'query', $this->getModuleName(), $pageID, 'revisions' ),
245 null, $rev );
246 }
247 if ( !$fit ) {
248 if ( $mode == 'all' || $mode == 'revs' ) {
249 $this->setContinueEnumParameter( 'continue', intval( $row->ar_namespace ) . '|' .
250 $this->keyToTitle( $row->ar_title ) . '|' . $row->ar_timestamp );
251 } else {
252 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601, $row->ar_timestamp ) );
253 }
254 break;
255 }
256 }
257 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'page' );
258 }
259
260 public function getAllowedParams() {
261 return array(
262 'start' => array(
263 ApiBase::PARAM_TYPE => 'timestamp'
264 ),
265 'end' => array(
266 ApiBase::PARAM_TYPE => 'timestamp',
267 ),
268 'dir' => array(
269 ApiBase::PARAM_TYPE => array(
270 'newer',
271 'older'
272 ),
273 ApiBase::PARAM_DFLT => 'older'
274 ),
275 'from' => null,
276 'to' => null,
277 'continue' => null,
278 'unique' => false,
279 'user' => array(
280 ApiBase::PARAM_TYPE => 'user'
281 ),
282 'excludeuser' => array(
283 ApiBase::PARAM_TYPE => 'user'
284 ),
285 'namespace' => array(
286 ApiBase::PARAM_TYPE => 'namespace',
287 ApiBase::PARAM_DFLT => 0,
288 ),
289 'limit' => array(
290 ApiBase::PARAM_DFLT => 10,
291 ApiBase::PARAM_TYPE => 'limit',
292 ApiBase::PARAM_MIN => 1,
293 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
294 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
295 ),
296 'prop' => array(
297 ApiBase::PARAM_DFLT => 'user|comment',
298 ApiBase::PARAM_TYPE => array(
299 'revid',
300 'user',
301 'userid',
302 'comment',
303 'parsedcomment',
304 'minor',
305 'len',
306 'content',
307 'token'
308 ),
309 ApiBase::PARAM_ISMULTI => true
310 ),
311 );
312 }
313
314 public function getParamDescription() {
315 return array(
316 'start' => 'The timestamp to start enumerating from (1,2)',
317 'end' => 'The timestamp to stop enumerating at (1,2)',
318 'dir' => $this->getDirectionDescription( $this->getModulePrefix(), ' (1,2)' ),
319 'from' => 'Start listing at this title (3)',
320 'to' => 'Stop listing at this title (3)',
321 'limit' => 'The maximum amount of revisions to list',
322 'prop' => array(
323 'Which properties to get',
324 ' revid - Adds the revision ID of the deleted revision',
325 ' user - Adds the user who made the revision',
326 ' userid - Adds the user ID whom made the revision',
327 ' comment - Adds the comment of the revision',
328 ' parsedcomment - Adds the parsed comment of the revision',
329 ' minor - Tags if the revision is minor',
330 ' len - Adds the length of the revision',
331 ' content - Adds the content of the revision',
332 ' token - Gives the edit token',
333 ),
334 'namespace' => 'Only list pages in this namespace (3)',
335 'user' => 'Only list revisions by this user',
336 'excludeuser' => 'Don\'t list revisions by this user',
337 'continue' => 'When more results are available, use this to continue (3)',
338 'unique' => 'List only one revision for each page (3)',
339 );
340 }
341
342 public function getDescription() {
343 return array(
344 'List deleted revisions.',
345 'This module operates in three modes:',
346 '1) List deleted revisions for the given title(s), sorted by timestamp',
347 '2) List deleted contributions for the given user, sorted by timestamp (no titles specified)',
348 '3) List all deleted revisions in the given namespace, sorted by title and timestamp (no titles specified, druser not set)',
349 'Certain parameters only apply to some modes and are ignored in others.',
350 'For instance, a parameter marked (1) only applies to mode 1 and is ignored in modes 2 and 3',
351 );
352 }
353
354 public function getPossibleErrors() {
355 return array_merge( parent::getPossibleErrors(), array(
356 array( 'code' => 'permissiondenied', 'info' => 'You don\'t have permission to view deleted revision information' ),
357 array( 'code' => 'badparams', 'info' => 'user and excludeuser cannot be used together' ),
358 array( 'code' => 'permissiondenied', 'info' => 'You don\'t have permission to view deleted revision content' ),
359 array( 'code' => 'badcontinue', 'info' => 'Invalid continue param. You should pass the original value returned by the previous query' ),
360 ) );
361 }
362
363 protected function getExamples() {
364 return array(
365 'List the last deleted revisions of Main Page and Talk:Main Page, with content (mode 1):',
366 ' api.php?action=query&list=deletedrevs&titles=Main%20Page|Talk:Main%20Page&drprop=user|comment|content',
367 'List the last 50 deleted contributions by Bob (mode 2):',
368 ' api.php?action=query&list=deletedrevs&druser=Bob&drlimit=50',
369 'List the first 50 deleted revisions in the main namespace (mode 3):',
370 ' api.php?action=query&list=deletedrevs&drdir=newer&drlimit=50',
371 'List the first 50 deleted pages in the Talk namespace (mode 3):',
372 ' api.php?action=query&list=deletedrevs&drdir=newer&drlimit=50&drnamespace=1&drunique=',
373 );
374 }
375
376 public function getVersion() {
377 return __CLASS__ . ': $Id$';
378 }
379 }