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