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