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