Merge "Use 'pagetitle' in content language"
[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 /**
28 * Query module to enumerate all deleted revisions.
29 *
30 * @ingroup API
31 */
32 class ApiQueryDeletedrevs extends ApiQueryBase {
33
34 public function __construct( $query, $moduleName ) {
35 parent::__construct( $query, $moduleName, 'dr' );
36 }
37
38 public function execute() {
39 $user = $this->getUser();
40 // Before doing anything at all, let's check permissions
41 if ( !$user->isAllowed( 'deletedhistory' ) ) {
42 $this->dieUsage(
43 'You don\'t have permission to view deleted revision information',
44 'permissiondenied'
45 );
46 }
47
48 $db = $this->getDB();
49 $params = $this->extractRequestParams( false );
50 $prop = array_flip( $params['prop'] );
51 $fld_parentid = isset( $prop['parentid'] );
52 $fld_revid = isset( $prop['revid'] );
53 $fld_user = isset( $prop['user'] );
54 $fld_userid = isset( $prop['userid'] );
55 $fld_comment = isset( $prop['comment'] );
56 $fld_parsedcomment = isset( $prop['parsedcomment'] );
57 $fld_minor = isset( $prop['minor'] );
58 $fld_len = isset( $prop['len'] );
59 $fld_sha1 = isset( $prop['sha1'] );
60 $fld_content = isset( $prop['content'] );
61 $fld_token = isset( $prop['token'] );
62
63 // If we're in JSON callback mode, no tokens can be obtained
64 if ( !is_null( $this->getMain()->getRequest()->getVal( 'callback' ) ) ) {
65 $fld_token = false;
66 }
67
68 $result = $this->getResult();
69 $pageSet = $this->getPageSet();
70 $titles = $pageSet->getTitles();
71
72 // This module operates in three modes:
73 // 'revs': List deleted revs for certain titles (1)
74 // 'user': List deleted revs by a certain user (2)
75 // 'all': List all deleted revs in NS (3)
76 $mode = 'all';
77 if ( count( $titles ) > 0 ) {
78 $mode = 'revs';
79 } elseif ( !is_null( $params['user'] ) ) {
80 $mode = 'user';
81 }
82
83 if ( $mode == 'revs' || $mode == 'user' ) {
84 // Ignore namespace and unique due to inability to know whether they were purposely set
85 foreach ( array( 'from', 'to', 'prefix', /*'namespace', 'unique'*/ ) as $p ) {
86 if ( !is_null( $params[$p] ) ) {
87 $this->dieUsage( "The '{$p}' parameter cannot be used in modes 1 or 2", 'badparams' );
88 }
89 }
90 } else {
91 foreach ( array( 'start', 'end' ) as $p ) {
92 if ( !is_null( $params[$p] ) ) {
93 $this->dieUsage( "The {$p} parameter cannot be used in mode 3", 'badparams' );
94 }
95 }
96 }
97
98 if ( !is_null( $params['user'] ) && !is_null( $params['excludeuser'] ) ) {
99 $this->dieUsage( 'user and excludeuser cannot be used together', 'badparams' );
100 }
101
102 $this->addTables( 'archive' );
103 $this->addWhere( 'ar_deleted = 0' );
104 $this->addFields( array( 'ar_title', 'ar_namespace', 'ar_timestamp' ) );
105
106 $this->addFieldsIf( 'ar_parent_id', $fld_parentid );
107 $this->addFieldsIf( 'ar_rev_id', $fld_revid );
108 $this->addFieldsIf( 'ar_user_text', $fld_user );
109 $this->addFieldsIf( 'ar_user', $fld_userid );
110 $this->addFieldsIf( 'ar_comment', $fld_comment || $fld_parsedcomment );
111 $this->addFieldsIf( 'ar_minor_edit', $fld_minor );
112 $this->addFieldsIf( 'ar_len', $fld_len );
113 $this->addFieldsIf( 'ar_sha1', $fld_sha1 );
114
115 if ( $fld_content ) {
116 $this->addTables( 'text' );
117 $this->addFields( array( 'ar_text', 'ar_text_id', 'old_text', 'old_flags' ) );
118 $this->addWhere( 'ar_text_id = old_id' );
119
120 // This also means stricter restrictions
121 if ( !$user->isAllowed( 'undelete' ) ) {
122 $this->dieUsage(
123 'You don\'t have permission to view deleted revision content',
124 'permissiondenied'
125 );
126 }
127 }
128 // Check limits
129 $userMax = $fld_content ? ApiBase::LIMIT_SML1 : ApiBase::LIMIT_BIG1;
130 $botMax = $fld_content ? ApiBase::LIMIT_SML2 : ApiBase::LIMIT_BIG2;
131
132 $limit = $params['limit'];
133
134 if ( $limit == 'max' ) {
135 $limit = $this->getMain()->canApiHighLimits() ? $botMax : $userMax;
136 $this->getResult()->setParsedLimit( $this->getModuleName(), $limit );
137 }
138
139 $this->validateLimit( 'limit', $limit, 1, $userMax, $botMax );
140
141 if ( $fld_token ) {
142 // Undelete tokens are identical for all pages, so we cache one here
143 $token = $user->getEditToken( '', $this->getMain()->getRequest() );
144 }
145
146 $dir = $params['dir'];
147
148 // We need a custom WHERE clause that matches all titles.
149 if ( $mode == 'revs' ) {
150 $lb = new LinkBatch( $titles );
151 $where = $lb->constructSet( 'ar', $db );
152 $this->addWhere( $where );
153 } elseif ( $mode == 'all' ) {
154 $this->addWhereFld( 'ar_namespace', $params['namespace'] );
155
156 $from = is_null( $params['from'] ) ? null : $this->titleToKey( $params['from'] );
157 $to = is_null( $params['to'] ) ? null : $this->titleToKey( $params['to'] );
158 $this->addWhereRange( 'ar_title', $dir, $from, $to );
159
160 if ( isset( $params['prefix'] ) ) {
161 $this->addWhere( 'ar_title' .
162 $db->buildLike( $this->titlePartToKey( $params['prefix'] ), $db->anyString() ) );
163 }
164 }
165
166 if ( !is_null( $params['user'] ) ) {
167 $this->addWhereFld( 'ar_user_text', $params['user'] );
168 } elseif ( !is_null( $params['excludeuser'] ) ) {
169 $this->addWhere( 'ar_user_text != ' .
170 $db->addQuotes( $params['excludeuser'] ) );
171 }
172
173 if ( !is_null( $params['continue'] ) && ( $mode == 'all' || $mode == 'revs' ) ) {
174 $cont = explode( '|', $params['continue'] );
175 $this->dieContinueUsageIf( count( $cont ) != 3 );
176 $ns = intval( $cont[0] );
177 $this->dieContinueUsageIf( strval( $ns ) !== $cont[0] );
178 $title = $db->addQuotes( $cont[1] );
179 $ts = $db->addQuotes( $db->timestamp( $cont[2] ) );
180 $op = ( $dir == 'newer' ? '>' : '<' );
181 $this->addWhere( "ar_namespace $op $ns OR " .
182 "(ar_namespace = $ns AND " .
183 "(ar_title $op $title OR " .
184 "(ar_title = $title AND " .
185 "ar_timestamp $op= $ts)))" );
186 }
187
188 $this->addOption( 'LIMIT', $limit + 1 );
189 $this->addOption(
190 'USE INDEX',
191 array( 'archive' => ( $mode == 'user' ? 'usertext_timestamp' : 'name_title_timestamp' ) )
192 );
193 if ( $mode == 'all' ) {
194 if ( $params['unique'] ) {
195 $this->addOption( 'GROUP BY', 'ar_title' );
196 } else {
197 $sort = ( $dir == 'newer' ? '' : ' DESC' );
198 $this->addOption( 'ORDER BY', array(
199 'ar_title' . $sort,
200 'ar_timestamp' . $sort
201 ) );
202 }
203 } else {
204 if ( $mode == 'revs' ) {
205 // Sort by ns and title in the same order as timestamp for efficiency
206 $this->addWhereRange( 'ar_namespace', $dir, null, null );
207 $this->addWhereRange( 'ar_title', $dir, null, null );
208 }
209 $this->addTimestampWhereRange( 'ar_timestamp', $dir, $params['start'], $params['end'] );
210 }
211 $res = $this->select( __METHOD__ );
212 $pageMap = array(); // Maps ns&title to (fake) pageid
213 $count = 0;
214 $newPageID = 0;
215 foreach ( $res as $row ) {
216 if ( ++$count > $limit ) {
217 // We've had enough
218 if ( $mode == 'all' || $mode == 'revs' ) {
219 $this->setContinueEnumParameter( 'continue', intval( $row->ar_namespace ) . '|' .
220 $row->ar_title . '|' . $row->ar_timestamp );
221 } else {
222 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601, $row->ar_timestamp ) );
223 }
224 break;
225 }
226
227 $rev = array();
228 $rev['timestamp'] = wfTimestamp( TS_ISO_8601, $row->ar_timestamp );
229 if ( $fld_revid ) {
230 $rev['revid'] = intval( $row->ar_rev_id );
231 }
232 if ( $fld_parentid && !is_null( $row->ar_parent_id ) ) {
233 $rev['parentid'] = intval( $row->ar_parent_id );
234 }
235 if ( $fld_user ) {
236 $rev['user'] = $row->ar_user_text;
237 }
238 if ( $fld_userid ) {
239 $rev['userid'] = $row->ar_user;
240 }
241 if ( $fld_comment ) {
242 $rev['comment'] = $row->ar_comment;
243 }
244
245 $title = Title::makeTitle( $row->ar_namespace, $row->ar_title );
246
247 if ( $fld_parsedcomment ) {
248 $rev['parsedcomment'] = Linker::formatComment( $row->ar_comment, $title );
249 }
250 if ( $fld_minor && $row->ar_minor_edit == 1 ) {
251 $rev['minor'] = '';
252 }
253 if ( $fld_len ) {
254 $rev['len'] = $row->ar_len;
255 }
256 if ( $fld_sha1 ) {
257 if ( $row->ar_sha1 != '' ) {
258 $rev['sha1'] = wfBaseConvert( $row->ar_sha1, 36, 16, 40 );
259 } else {
260 $rev['sha1'] = '';
261 }
262 }
263 if ( $fld_content ) {
264 ApiResult::setContent( $rev, Revision::getRevisionText( $row ) );
265 }
266
267 if ( !isset( $pageMap[$row->ar_namespace][$row->ar_title] ) ) {
268 $pageID = $newPageID++;
269 $pageMap[$row->ar_namespace][$row->ar_title] = $pageID;
270 $a['revisions'] = array( $rev );
271 $result->setIndexedTagName( $a['revisions'], 'rev' );
272 ApiQueryBase::addTitleInfo( $a, $title );
273 if ( $fld_token ) {
274 $a['token'] = $token;
275 }
276 $fit = $result->addValue( array( 'query', $this->getModuleName() ), $pageID, $a );
277 } else {
278 $pageID = $pageMap[$row->ar_namespace][$row->ar_title];
279 $fit = $result->addValue(
280 array( 'query', $this->getModuleName(), $pageID, 'revisions' ),
281 null, $rev );
282 }
283 if ( !$fit ) {
284 if ( $mode == 'all' || $mode == 'revs' ) {
285 $this->setContinueEnumParameter( 'continue', intval( $row->ar_namespace ) . '|' .
286 $row->ar_title . '|' . $row->ar_timestamp );
287 } else {
288 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601, $row->ar_timestamp ) );
289 }
290 break;
291 }
292 }
293 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'page' );
294 }
295
296 public function getAllowedParams() {
297 return array(
298 'start' => array(
299 ApiBase::PARAM_TYPE => 'timestamp'
300 ),
301 'end' => array(
302 ApiBase::PARAM_TYPE => 'timestamp',
303 ),
304 'dir' => array(
305 ApiBase::PARAM_TYPE => array(
306 'newer',
307 'older'
308 ),
309 ApiBase::PARAM_DFLT => 'older'
310 ),
311 'from' => null,
312 'to' => null,
313 'prefix' => null,
314 'continue' => null,
315 'unique' => false,
316 'user' => array(
317 ApiBase::PARAM_TYPE => 'user'
318 ),
319 'excludeuser' => array(
320 ApiBase::PARAM_TYPE => 'user'
321 ),
322 'namespace' => array(
323 ApiBase::PARAM_TYPE => 'namespace',
324 ApiBase::PARAM_DFLT => NS_MAIN,
325 ),
326 'limit' => array(
327 ApiBase::PARAM_DFLT => 10,
328 ApiBase::PARAM_TYPE => 'limit',
329 ApiBase::PARAM_MIN => 1,
330 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
331 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
332 ),
333 'prop' => array(
334 ApiBase::PARAM_DFLT => 'user|comment',
335 ApiBase::PARAM_TYPE => array(
336 'revid',
337 'parentid',
338 'user',
339 'userid',
340 'comment',
341 'parsedcomment',
342 'minor',
343 'len',
344 'sha1',
345 'content',
346 'token'
347 ),
348 ApiBase::PARAM_ISMULTI => true
349 ),
350 );
351 }
352
353 public function getParamDescription() {
354 return array(
355 'start' => 'The timestamp to start enumerating from (1, 2)',
356 'end' => 'The timestamp to stop enumerating at (1, 2)',
357 'dir' => $this->getDirectionDescription( $this->getModulePrefix(), ' (1, 3)' ),
358 'from' => 'Start listing at this title (3)',
359 'to' => 'Stop listing at this title (3)',
360 'prefix' => 'Search for all page titles that begin with this value (3)',
361 'limit' => 'The maximum amount of revisions to list',
362 'prop' => array(
363 'Which properties to get',
364 ' revid - Adds the revision ID of the deleted revision',
365 ' parentid - Adds the revision ID of the previous revision to the page',
366 ' user - Adds the user who made the revision',
367 ' userid - Adds the user ID whom made the revision',
368 ' comment - Adds the comment of the revision',
369 ' parsedcomment - Adds the parsed comment of the revision',
370 ' minor - Tags if the revision is minor',
371 ' len - Adds the length (bytes) of the revision',
372 ' sha1 - Adds the SHA-1 (base 16) of the revision',
373 ' content - Adds the content of the revision',
374 ' token - Gives the edit token',
375 ),
376 'namespace' => 'Only list pages in this namespace (3)',
377 'user' => 'Only list revisions by this user',
378 'excludeuser' => 'Don\'t list revisions by this user',
379 'continue' => 'When more results are available, use this to continue (1, 3)',
380 'unique' => 'List only one revision for each page (3)',
381 );
382 }
383
384 public function getResultProperties() {
385 return array(
386 '' => array(
387 'ns' => 'namespace',
388 'title' => 'string'
389 ),
390 'token' => array(
391 'token' => 'string'
392 )
393 );
394 }
395
396 public function getDescription() {
397 $p = $this->getModulePrefix();
398
399 return array(
400 'List deleted revisions.',
401 'Operates in three modes:',
402 ' 1) List deleted revisions for the given title(s), sorted by timestamp',
403 ' 2) List deleted contributions for the given user, sorted by timestamp (no titles specified)',
404 " 3) List all deleted revisions in the given namespace, sorted by title and timestamp',
405 ' (no titles specified, {$p}user not set)",
406 'Certain parameters only apply to some modes and are ignored in others.',
407 'For instance, a parameter marked (1) only applies to mode 1 and is ignored in modes 2 and 3',
408 );
409 }
410
411 public function getPossibleErrors() {
412 return array_merge( parent::getPossibleErrors(), array(
413 array(
414 'code' => 'permissiondenied',
415 'info' => 'You don\'t have permission to view deleted revision information'
416 ),
417 array( 'code' => 'badparams', 'info' => 'user and excludeuser cannot be used together'
418 ),
419 array(
420 'code' => 'permissiondenied',
421 'info' => 'You don\'t have permission to view deleted revision content'
422 ),
423 array( 'code' => 'badparams', 'info' => "The 'from' parameter cannot be used in modes 1 or 2" ),
424 array( 'code' => 'badparams', 'info' => "The 'to' parameter cannot be used in modes 1 or 2" ),
425 array(
426 'code' => 'badparams',
427 'info' => "The 'prefix' parameter cannot be used in modes 1 or 2"
428 ),
429 array( 'code' => 'badparams', 'info' => "The 'start' parameter cannot be used in mode 3" ),
430 array( 'code' => 'badparams', 'info' => "The 'end' parameter cannot be used in mode 3" ),
431 ) );
432 }
433
434 public function getExamples() {
435 return array(
436 'api.php?action=query&list=deletedrevs&titles=Main%20Page|Talk:Main%20Page&' .
437 'drprop=user|comment|content'
438 => 'List the last deleted revisions of Main Page and Talk:Main Page, with content (mode 1)',
439 'api.php?action=query&list=deletedrevs&druser=Bob&drlimit=50'
440 => 'List the last 50 deleted contributions by Bob (mode 2)',
441 'api.php?action=query&list=deletedrevs&drdir=newer&drlimit=50'
442 => 'List the first 50 deleted revisions in the main namespace (mode 3)',
443 'api.php?action=query&list=deletedrevs&drdir=newer&drlimit=50&drnamespace=1&drunique='
444 => 'List the first 50 deleted pages in the Talk namespace (mode 3):',
445 );
446 }
447
448 public function getHelpUrls() {
449 return 'https://www.mediawiki.org/wiki/API:Deletedrevs';
450 }
451 }