Tidy up the class
[mediawiki.git] / includes / api / ApiQueryDeletedrevs.php
blob14e4f59d2828bf9b7509d901fefc6a95ce371e8c
1 <?php
3 /**
4 * Created on Jul 2, 2007
6 * API for MediaWiki 1.8+
8 * Copyright © 2007 Roan Kattouw <Firstname>.<Lastname>@home.nl
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.
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.
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
26 if ( !defined( 'MEDIAWIKI' ) ) {
27 // Eclipse helper - will be ignored in production
28 require_once( 'ApiQueryBase.php' );
31 /**
32 * Query module to enumerate all available pages.
34 * @ingroup API
36 class ApiQueryDeletedrevs extends ApiQueryBase {
38 public function __construct( $query, $moduleName ) {
39 parent::__construct( $query, $moduleName, 'dr' );
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' );
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'] );
61 $result = $this->getResult();
62 $pageSet = $this->getPageSet();
63 $titles = $pageSet->getTitles();
64 $data = array();
66 // This module operates in three modes:
67 // 'revs': List deleted revs for certain titles
68 // 'user': List deleted revs by a certain user
69 // 'all': List all deleted revs
70 $mode = 'all';
71 if ( count( $titles ) > 0 ) {
72 $mode = 'revs';
73 } elseif ( !is_null( $params['user'] ) ) {
74 $mode = 'user';
77 if ( !is_null( $params['user'] ) && !is_null( $params['excludeuser'] ) ) {
78 $this->dieUsage( 'user and excludeuser cannot be used together', 'badparams' );
81 $this->addTables( 'archive' );
82 $this->addWhere( 'ar_deleted = 0' );
83 $this->addFields( array( 'ar_title', 'ar_namespace', 'ar_timestamp' ) );
84 if ( $fld_revid ) {
85 $this->addFields( 'ar_rev_id' );
87 if ( $fld_user ) {
88 $this->addFields( 'ar_user_text' );
90 if ( $fld_comment || $fld_parsedcomment ) {
91 $this->addFields( 'ar_comment' );
93 if ( $fld_minor ) {
94 $this->addFields( 'ar_minor_edit' );
96 if ( $fld_len ) {
97 $this->addFields( 'ar_len' );
99 if ( $fld_content ) {
100 $this->addTables( 'text' );
101 $this->addFields( array( 'ar_text', 'ar_text_id', 'old_text', 'old_flags' ) );
102 $this->addWhere( 'ar_text_id = old_id' );
104 // This also means stricter restrictions
105 if ( !$wgUser->isAllowed( 'undelete' ) ) {
106 $this->dieUsage( 'You don\'t have permission to view deleted revision content', 'permissiondenied' );
109 // Check limits
110 $userMax = $fld_content ? ApiBase::LIMIT_SML1 : ApiBase::LIMIT_BIG1;
111 $botMax = $fld_content ? ApiBase::LIMIT_SML2 : ApiBase::LIMIT_BIG2;
113 $limit = $params['limit'];
115 if ( $limit == 'max' ) {
116 $limit = $this->getMain()->canApiHighLimits() ? $botMax : $userMax;
117 $this->getResult()->addValue( 'limits', $this->getModuleName(), $limit );
120 $this->validateLimit( 'limit', $limit, 1, $userMax, $botMax );
122 if ( $fld_token ) {
123 // Undelete tokens are identical for all pages, so we cache one here
124 $token = $wgUser->editToken();
127 // We need a custom WHERE clause that matches all titles.
128 if ( $mode == 'revs' ) {
129 $lb = new LinkBatch( $titles );
130 $where = $lb->constructSet( 'ar', $db );
131 $this->addWhere( $where );
132 } elseif ( $mode == 'all' ) {
133 $this->addWhereFld( 'ar_namespace', $params['namespace'] );
134 if ( !is_null( $params['from'] ) ) {
135 $from = $this->getDB()->strencode( $this->titleToKey( $params['from'] ) );
136 $this->addWhere( "ar_title >= '$from'" );
140 if ( !is_null( $params['user'] ) ) {
141 $this->addWhereFld( 'ar_user_text', $params['user'] );
142 } elseif ( !is_null( $params['excludeuser'] ) ) {
143 $this->addWhere( 'ar_user_text != ' .
144 $this->getDB()->addQuotes( $params['excludeuser'] ) );
147 if ( !is_null( $params['continue'] ) && ( $mode == 'all' || $mode == 'revs' ) )
149 $cont = explode( '|', $params['continue'] );
150 if ( count( $cont ) != 3 ) {
151 $this->dieUsage( 'Invalid continue param. You should pass the original value returned by the previous query', 'badcontinue' );
153 $ns = intval( $cont[0] );
154 $title = $this->getDB()->strencode( $this->titleToKey( $cont[1] ) );
155 $ts = $this->getDB()->strencode( $cont[2] );
156 $op = ( $params['dir'] == 'newer' ? '>' : '<' );
157 $this->addWhere( "ar_namespace $op $ns OR " .
158 "(ar_namespace = $ns AND " .
159 "(ar_title $op '$title' OR " .
160 "(ar_title = '$title' AND " .
161 "ar_timestamp $op= '$ts')))" );
164 $this->addOption( 'LIMIT', $limit + 1 );
165 $this->addOption( 'USE INDEX', array( 'archive' => ( $mode == 'user' ? 'usertext_timestamp' : 'name_title_timestamp' ) ) );
166 if ( $mode == 'all' ) {
167 if ( $params['unique'] ) {
168 $this->addOption( 'GROUP BY', 'ar_title' );
169 $this->addOption( 'ORDER BY', 'ar_title' );
170 } else {
171 $this->addOption( 'ORDER BY', 'ar_title, ar_timestamp' );
173 } else {
174 if ( $mode == 'revs' ) {
175 // Sort by ns and title in the same order as timestamp for efficiency
176 $this->addWhereRange( 'ar_namespace', $params['dir'], null, null );
177 $this->addWhereRange( 'ar_title', $params['dir'], null, null );
179 $this->addWhereRange( 'ar_timestamp', $params['dir'], $params['start'], $params['end'] );
181 $res = $this->select( __METHOD__ );
182 $pageMap = array(); // Maps ns&title to (fake) pageid
183 $count = 0;
184 $newPageID = 0;
185 while ( $row = $db->fetchObject( $res ) ) {
186 if ( ++$count > $limit ) {
187 // We've had enough
188 if ( $mode == 'all' || $mode == 'revs' ) {
189 $this->setContinueEnumParameter( 'continue', intval( $row->ar_namespace ) . '|' .
190 $this->keyToTitle( $row->ar_title ) . '|' . $row->ar_timestamp );
191 } else {
192 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601, $row->ar_timestamp ) );
194 break;
197 $rev = array();
198 $rev['timestamp'] = wfTimestamp( TS_ISO_8601, $row->ar_timestamp );
199 if ( $fld_revid ) {
200 $rev['revid'] = intval( $row->ar_rev_id );
202 if ( $fld_user ) {
203 $rev['user'] = $row->ar_user_text;
205 if ( $fld_comment ) {
206 $rev['comment'] = $row->ar_comment;
209 $title = Title::makeTitle( $row->ar_namespace, $row->ar_title );
211 if ( $fld_parsedcomment ) {
212 global $wgUser;
213 $rev['parsedcomment'] = $wgUser->getSkin()->formatComment( $row->ar_comment, $title );
215 if ( $fld_minor && $row->ar_minor_edit == 1 ) {
216 $rev['minor'] = '';
218 if ( $fld_len ) {
219 $rev['len'] = $row->ar_len;
221 if ( $fld_content ) {
222 ApiResult::setContent( $rev, Revision::getRevisionText( $row ) );
225 if ( !isset( $pageMap[$row->ar_namespace][$row->ar_title] ) ) {
226 $pageID = $newPageID++;
227 $pageMap[$row->ar_namespace][$row->ar_title] = $pageID;
228 $a['revisions'] = array( $rev );
229 $result->setIndexedTagName( $a['revisions'], 'rev' );
230 ApiQueryBase::addTitleInfo( $a, $title );
231 if ( $fld_token ) {
232 $a['token'] = $token;
234 $fit = $result->addValue( array( 'query', $this->getModuleName() ), $pageID, $a );
235 } else {
236 $pageID = $pageMap[$row->ar_namespace][$row->ar_title];
237 $fit = $result->addValue(
238 array( 'query', $this->getModuleName(), $pageID, 'revisions' ),
239 null, $rev );
241 if ( !$fit ) {
242 if ( $mode == 'all' || $mode == 'revs' ) {
243 $this->setContinueEnumParameter( 'continue', intval( $row->ar_namespace ) . '|' .
244 $this->keyToTitle( $row->ar_title ) . '|' . $row->ar_timestamp );
245 } else {
246 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601, $row->ar_timestamp ) );
248 break;
251 $db->freeResult( $res );
252 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'page' );
255 public function getAllowedParams() {
256 return array(
257 'start' => array(
258 ApiBase::PARAM_TYPE => 'timestamp'
260 'end' => array(
261 ApiBase::PARAM_TYPE => 'timestamp',
263 'dir' => array(
264 ApiBase::PARAM_TYPE => array(
265 'newer',
266 'older'
268 ApiBase::PARAM_DFLT => 'older'
270 'from' => null,
271 'continue' => null,
272 'unique' => false,
273 'user' => array(
274 ApiBase::PARAM_TYPE => 'user'
276 'excludeuser' => array(
277 ApiBase::PARAM_TYPE => 'user'
279 'namespace' => array(
280 ApiBase::PARAM_TYPE => 'namespace',
281 ApiBase::PARAM_DFLT => 0,
283 'limit' => array(
284 ApiBase::PARAM_DFLT => 10,
285 ApiBase::PARAM_TYPE => 'limit',
286 ApiBase::PARAM_MIN => 1,
287 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
288 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
290 'prop' => array(
291 ApiBase::PARAM_DFLT => 'user|comment',
292 ApiBase::PARAM_TYPE => array(
293 'revid',
294 'user',
295 'comment',
296 'parsedcomment',
297 'minor',
298 'len',
299 'content',
300 'token'
302 ApiBase::PARAM_ISMULTI => true
307 public function getParamDescription() {
308 return array(
309 'start' => 'The timestamp to start enumerating from. (1,2)',
310 'end' => 'The timestamp to stop enumerating at. (1,2)',
311 'dir' => 'The direction in which to enumerate. (1,2)',
312 'limit' => 'The maximum amount of revisions to list',
313 'prop' => 'Which properties to get',
314 'namespace' => 'Only list pages in this namespace (3)',
315 'user' => 'Only list revisions by this user',
316 'excludeuser' => 'Don\'t list revisions by this user',
317 'from' => 'Start listing at this title (3)',
318 'continue' => 'When more results are available, use this to continue (3)',
319 'unique' => 'List only one revision for each page (3)',
323 public function getDescription() {
324 return array(
325 'List deleted revisions.',
326 'This module operates in three modes:',
327 '1) List deleted revisions for the given title(s), sorted by timestamp',
328 '2) List deleted contributions for the given user, sorted by timestamp (no titles specified)',
329 '3) List all deleted revisions in the given namespace, sorted by title and timestamp (no titles specified, druser not set)',
330 'Certain parameters only apply to some modes and are ignored in others.',
331 'For instance, a parameter marked (1) only applies to mode 1 and is ignored in modes 2 and 3.',
335 public function getPossibleErrors() {
336 return array_merge( parent::getPossibleErrors(), array(
337 array( 'code' => 'permissiondenied', 'info' => 'You don\'t have permission to view deleted revision information' ),
338 array( 'code' => 'badparams', 'info' => 'user and excludeuser cannot be used together' ),
339 array( 'code' => 'permissiondenied', 'info' => 'You don\'t have permission to view deleted revision content' ),
340 array( 'code' => 'badcontinue', 'info' => 'Invalid continue param. You should pass the original value returned by the previous query' ),
341 ) );
344 protected function getExamples() {
345 return array(
346 'List the last deleted revisions of Main Page and Talk:Main Page, with content (mode 1):',
347 ' api.php?action=query&list=deletedrevs&titles=Main%20Page|Talk:Main%20Page&drprop=user|comment|content',
348 'List the last 50 deleted contributions by Bob (mode 2):',
349 ' api.php?action=query&list=deletedrevs&druser=Bob&drlimit=50',
350 'List the first 50 deleted revisions in the main namespace (mode 3):',
351 ' api.php?action=query&list=deletedrevs&drdir=newer&drlimit=50',
352 'List the first 50 deleted pages in the Talk namespace (mode 3):',
353 ' api.php?action=query&list=deletedrevs&drdir=newer&drlimit=50&drnamespace=1&drunique',
357 public function getVersion() {
358 return __CLASS__ . ': $Id$';