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