]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - includes/api/ApiQueryDeletedrevs.php
MediaWiki 1.15.0
[autoinstallsdev/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_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                 {
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                 {
121                         $lb = new LinkBatch($titles);
122                         $where = $lb->constructSet('ar', $db);
123                         $this->addWhere($where);
124                 }
125                 elseif($mode == 'all')
126                 {
127                         $this->addWhereFld('ar_namespace', $params['namespace']);
128                         if(!is_null($params['from']))
129                         {
130                                 $from = $this->getDB()->strencode($this->titleToKey($params['from']));
131                                 $this->addWhere("ar_title >= '$from'");
132                         }
133                 }
134                 
135                 if(!is_null($params['user'])) {
136                         $this->addWhereFld('ar_user_text', $params['user']);
137                 } elseif(!is_null($params['excludeuser'])) {
138                         $this->addWhere('ar_user_text != ' .
139                                 $this->getDB()->addQuotes($params['excludeuser']));
140                 }
141                 
142                 if(!is_null($params['continue']) && ($mode == 'all' || $mode == 'revs'))
143                 {
144                         $cont = explode('|', $params['continue']);
145                         if(count($cont) != 3)
146                                 $this->dieUsage("Invalid continue param. You should pass the original value returned by the previous query", "badcontinue");
147                         $ns = intval($cont[0]);
148                         $title = $this->getDB()->strencode($this->titleToKey($cont[1]));
149                         $ts = $this->getDB()->strencode($cont[2]);
150                         $op = ($params['dir'] == 'newer' ? '>' : '<');
151                         $this->addWhere("ar_namespace $op $ns OR " .
152                                         "(ar_namespace = $ns AND " .
153                                         "(ar_title $op '$title' OR " .
154                                         "(ar_title = '$title' AND " .
155                                         "ar_timestamp = '$ts')))");
156                 }
157
158                 $this->addOption('LIMIT', $limit + 1);
159                 $this->addOption('USE INDEX', array('archive' => ($mode == 'user' ? 'usertext_timestamp' : 'name_title_timestamp')));
160                 if($mode == 'all')
161                 {
162                         if($params['unique'])
163                         {
164                                 $this->addOption('GROUP BY', 'ar_title');
165                                 $this->addOption('ORDER BY', 'ar_title');
166                         }
167                         else
168                                 $this->addOption('ORDER BY', 'ar_title, ar_timestamp');
169                 }
170                 else
171                 {
172                         if($mode == 'revs')
173                         {
174                                 // Sort by ns and title in the same order as timestamp for efficiency
175                                 $this->addWhereRange('ar_namespace', $params['dir'], null, null);
176                                 $this->addWhereRange('ar_title', $params['dir'], null, null);
177                         }
178                         $this->addWhereRange('ar_timestamp', $params['dir'], $params['start'], $params['end']);
179                 }
180                 $res = $this->select(__METHOD__);
181                 $pageMap = array(); // Maps ns&title to (fake) pageid
182                 $count = 0;
183                 $newPageID = 0;
184                 while($row = $db->fetchObject($res))
185                 {
186                         if(++$count > $limit)
187                         {
188                                 // We've had enough
189                                 if($mode == 'all' || $mode == 'revs')
190                                         $this->setContinueEnumParameter('continue', intval($row->ar_namespace) . '|' .
191                                                 $this->keyToTitle($row->ar_title) . '|' . $row->ar_timestamp);
192                                 else
193                                         $this->setContinueEnumParameter('start', wfTimestamp(TS_ISO_8601, $row->ar_timestamp));
194                                 break;
195                         }
196
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);
201                         if($fld_user)
202                                 $rev['user'] = $row->ar_user_text;
203                         if($fld_comment)
204                                 $rev['comment'] = $row->ar_comment;
205                         if($fld_minor)
206                                 if($row->ar_minor_edit == 1)
207                                         $rev['minor'] = '';
208                         if($fld_len)
209                                 $rev['len'] = $row->ar_len;
210                         if($fld_content)
211                                 ApiResult::setContent($rev, Revision::getRevisionText($row));
212
213                         if(!isset($pageMap[$row->ar_namespace][$row->ar_title]))
214                         {
215                                 $pageID = $newPageID++;
216                                 $pageMap[$row->ar_namespace][$row->ar_title] = $pageID;
217                                 $t = Title::makeTitle($row->ar_namespace, $row->ar_title);
218                                 $a['revisions'] = array($rev);
219                                 $result->setIndexedTagName($a['revisions'], 'rev');
220                                 ApiQueryBase::addTitleInfo($a, $t);
221                                 if($fld_token)
222                                         $a['token'] = $token;
223                                 $fit = $result->addValue(array('query', $this->getModuleName()), $pageID, $a);
224                         }
225                         else
226                         {
227                                 $pageID = $pageMap[$row->ar_namespace][$row->ar_title];
228                                 $fit = $result->addValue(
229                                         array('query', $this->getModuleName(), $pageID, 'revisions'),
230                                         null, $rev);
231                         }
232                         if(!$fit)
233                         {
234                                 if($mode == 'all' || $mode == 'revs')
235                                         $this->setContinueEnumParameter('continue', intval($row->ar_namespace) . '|' .
236                                                 $this->keyToTitle($row->ar_title) . '|' . $row->ar_timestamp);
237                                 else
238                                         $this->setContinueEnumParameter('start', wfTimestamp(TS_ISO_8601, $row->ar_timestamp));
239                                 break;
240                         }
241                 }
242                 $db->freeResult($res);
243                 $result->setIndexedTagName_internal(array('query', $this->getModuleName()), 'page');
244         }
245
246         public function getAllowedParams() {
247                 return array (
248                         'start' => array(
249                                 ApiBase :: PARAM_TYPE => 'timestamp'
250                         ),
251                         'end' => array(
252                                 ApiBase :: PARAM_TYPE => 'timestamp',
253                         ),
254                         'dir' => array(
255                                 ApiBase :: PARAM_TYPE => array(
256                                         'newer',
257                                         'older'
258                                 ),
259                                 ApiBase :: PARAM_DFLT => 'older'
260                         ),
261                         'from' => null,
262                         'continue' => null,
263                         'unique' => false,
264                         'user' => array(
265                                 ApiBase :: PARAM_TYPE => 'user'
266                         ),
267                         'excludeuser' => array(
268                                 ApiBase :: PARAM_TYPE => 'user'
269                         ),
270                         'namespace' => array(
271                                 ApiBase :: PARAM_TYPE => 'namespace',
272                                 ApiBase :: PARAM_DFLT => 0,
273                         ),
274                         'limit' => array(
275                                 ApiBase :: PARAM_DFLT => 10,
276                                 ApiBase :: PARAM_TYPE => 'limit',
277                                 ApiBase :: PARAM_MIN => 1,
278                                 ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1,
279                                 ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
280                         ),
281                         'prop' => array(
282                                 ApiBase :: PARAM_DFLT => 'user|comment',
283                                 ApiBase :: PARAM_TYPE => array(
284                                         'revid',
285                                         'user',
286                                         'comment',
287                                         'minor',
288                                         'len',
289                                         'content',
290                                         'token'
291                                 ),
292                                 ApiBase :: PARAM_ISMULTI => true
293                         ),
294                 );
295         }
296
297         public function getParamDescription() {
298                 return array (
299                         'start' => 'The timestamp to start enumerating from. (1,2)',
300                         'end' => 'The timestamp to stop enumerating at. (1,2)',
301                         'dir' => 'The direction in which to enumerate. (1,2)',
302                         'limit' => 'The maximum amount of revisions to list',
303                         'prop' => 'Which properties to get',
304                         'namespace' => 'Only list pages in this namespace (3)',
305                         'user' => 'Only list revisions by this user',
306                         'excludeuser' => 'Don\'t list revisions by this user',
307                         'from' => 'Start listing at this title (3)',
308                         'continue' => 'When more results are available, use this to continue (3)',
309                         'unique' => 'List only one revision for each page (3)',
310                 );
311         }
312
313         public function getDescription() {
314                 return array(   'List deleted revisions.',
315                                 'This module operates in three modes:',
316                                 '1) List deleted revisions for the given title(s), sorted by timestamp',
317                                 '2) List deleted contributions for the given user, sorted by timestamp (no titles specified)',
318                                 '3) List all deleted revisions in the given namespace, sorted by title and timestamp (no titles specified, druser not set)',
319                                 'Certain parameters only apply to some modes and are ignored in others.',
320                                 'For instance, a parameter marked (1) only applies to mode 1 and is ignored in modes 2 and 3.',
321                 );
322         }
323
324         protected function getExamples() {
325                 return array (
326                         'List the last deleted revisions of Main Page and Talk:Main Page, with content (mode 1):',
327                         '  api.php?action=query&list=deletedrevs&titles=Main%20Page|Talk:Main%20Page&drprop=user|comment|content',
328                         'List the last 50 deleted contributions by Bob (mode 2):',
329                         '  api.php?action=query&list=deletedrevs&druser=Bob&drlimit=50',
330                         'List the first 50 deleted revisions in the main namespace (mode 3):',
331                         '  api.php?action=query&list=deletedrevs&drdir=newer&drlimit=50',
332                         'List the first 50 deleted pages in the Talk namespace (mode 3):',
333                         '  api.php?action=query&list=deletedrevs&drdir=newer&drlimit=50&drnamespace=1&drunique',
334                 );
335         }
336
337         public function getVersion() {
338                 return __CLASS__ . ': $Id: ApiQueryDeletedrevs.php 50220 2009-05-05 14:07:59Z tstarling $';
339         }
340 }