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