Users without the delete permission but with the deletedhistory one should not be...
[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 * @addtogroup 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 $this->run();
44 }
45
46 private function run() {
47
48 global $wgUser;
49 // Before doing anything at all, let's check permissions
50 if(!$wgUser->isAllowed('deletedhistory'))
51 $this->dieUsage('You don\'t have permission to view deleted revisions information', 'permissiondenied');
52
53 $db = $this->getDB();
54 $params = $this->extractRequestParams();
55 $prop = array_flip($params['prop']);
56 $fld_revid = isset($prop['revid']);
57 $fld_user = isset($prop['user']);
58 $fld_comment = isset($prop['comment']);
59 $fld_minor = isset($prop['minor']);
60 $fld_len = isset($prop['len']);
61 $fld_content = isset($prop['content']);
62 $fld_token = isset($prop['token']);
63
64 $result = $this->getResult();
65 $pageSet = $this->getPageSet();
66 $titles = $pageSet->getTitles();
67 $data = array();
68
69 $this->addTables('archive');
70 $this->addFields(array('ar_title', 'ar_namespace', 'ar_timestamp'));
71 if($fld_revid)
72 $this->addFields('ar_rev_id');
73 if($fld_user)
74 $this->addFields('ar_user_text');
75 if($fld_comment)
76 $this->addFields('ar_comment');
77 if($fld_minor)
78 $this->addFields('ar_minor_edit');
79 if($fld_len)
80 $this->addFields('ar_len');
81 if($fld_content)
82 {
83 $this->addTables('text');
84 $this->addFields(array('ar_text', 'ar_text_id', 'old_text', 'old_flags'));
85 $this->addWhere('ar_text_id = old_id');
86
87 // This also means stricter limits
88 $userMax = 50;
89 $botMax = 200;
90 $this->validateLimit('limit', $params['limit'], 1, $userMax, $botMax);
91
92 // And also stricter restrictions
93 if(!$wgUser->isAllowed('delete')) {
94 $this->dieUsage('You don\'t have permission to view deleted revisions content', 'permissiondeniedcontent');
95 }
96 }
97 if($fld_token)
98 // Undelete tokens are identical for all pages, so we cache one here
99 $token = $wgUser->editToken();
100
101 // We need a custom WHERE clause that matches all titles.
102 if(count($titles) > 0)
103 {
104 $lb = new LinkBatch($titles);
105 $where = $lb->constructSet('ar', $db);
106 $this->addWhere($where);
107 }
108
109 $this->addOption('LIMIT', $params['limit'] + 1);
110 $this->addWhereRange('ar_timestamp', $params['dir'], $params['start'], $params['end']);
111 if(isset($params['namespace']))
112 $this->addWhereFld('ar_namespace', $params['namespace']);
113 $res = $this->select(__METHOD__);
114 $pages = array();
115 $count = 0;
116 // First populate the $pages array
117 while($row = $db->fetchObject($res))
118 {
119 if($count++ == $params['limit'])
120 {
121 // We've had enough
122 $this->setContinueEnumParameter('start', wfTimestamp(TS_ISO_8601, $row->ar_timestamp));
123 break;
124 }
125
126 $rev = array();
127 $rev['timestamp'] = wfTimestamp(TS_ISO_8601, $row->ar_timestamp);
128 if($fld_revid)
129 $rev['revid'] = $row->ar_rev_id;
130 if($fld_user)
131 $rev['user'] = $row->ar_user_text;
132 if($fld_comment)
133 $rev['comment'] = $row->ar_comment;
134 if($fld_minor)
135 if($row->ar_minor_edit == 1)
136 $rev['minor'] = '';
137 if($fld_len)
138 $rev['len'] = $row->ar_len;
139 if($fld_content)
140 ApiResult::setContent($rev, Revision::getRevisionText($row));
141
142 $t = Title::makeTitle($row->ar_namespace, $row->ar_title);
143 if(!isset($pages[$t->getPrefixedText()]))
144 {
145 $pages[$t->getPrefixedText()] = array(
146 'title' => $t->getPrefixedText(),
147 'ns' => intval($row->ar_namespace),
148 'revisions' => array($rev)
149 );
150 if($fld_token)
151 $pages[$t->getPrefixedText()]['token'] = $token;
152 }
153 else
154 $pages[$t->getPrefixedText()]['revisions'][] = $rev;
155 }
156 $db->freeResult($res);
157
158 // We don't want entire pagenames as keys, so let's make this array indexed
159 foreach($pages as $page)
160 {
161 $result->setIndexedTagName($page['revisions'], 'rev');
162 $data[] = $page;
163 }
164 $result->setIndexedTagName($data, 'page');
165 $result->addValue('query', $this->getModuleName(), $data);
166 }
167
168 protected function getAllowedParams() {
169 return array (
170 'start' => array(
171 ApiBase :: PARAM_TYPE => 'timestamp'
172 ),
173 'end' => array(
174 ApiBase :: PARAM_TYPE => 'timestamp',
175 ),
176 'dir' => array(
177 ApiBase :: PARAM_TYPE => array(
178 'newer',
179 'older'
180 ),
181 ApiBase :: PARAM_DFLT => 'older'
182 ),
183 'namespace' => array(
184 ApiBase :: PARAM_ISMULTI => true,
185 ApiBase :: PARAM_TYPE => 'namespace'
186 ),
187 'limit' => array(
188 ApiBase :: PARAM_DFLT => 10,
189 ApiBase :: PARAM_TYPE => 'limit',
190 ApiBase :: PARAM_MIN => 1,
191 ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1,
192 ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
193 ),
194 'prop' => array(
195 ApiBase :: PARAM_DFLT => 'user|comment',
196 ApiBase :: PARAM_TYPE => array(
197 'revid',
198 'user',
199 'comment',
200 'minor',
201 'len',
202 'content',
203 'token'
204 ),
205 ApiBase :: PARAM_ISMULTI => true
206 )
207 );
208 }
209
210 protected function getParamDescription() {
211 return array (
212 'start' => 'The timestamp to start enumerating from',
213 'end' => 'The timestamp to stop enumerating at',
214 'dir' => 'The direction in which to enumerate',
215 'namespace' => 'The namespaces to search in',
216 'limit' => 'The maximum amount of revisions to list',
217 'prop' => 'Which properties to get'
218 );
219 }
220
221 protected function getDescription() {
222 return 'List deleted revisions.';
223 }
224
225 protected function getExamples() {
226 return array (
227 'List the first 50 deleted revisions in the Category and Category talk namespaces',
228 ' api.php?action=query&list=deletedrevs&drdir=newer&drlimit=50&drnamespace=14|15',
229 'List the last deleted revisions of Main Page and Talk:Main Page, with content:',
230 ' api.php?action=query&list=deletedrevs&titles=Main%20Page|Talk:Main%20Page&drprop=user|comment|content'
231 );
232 }
233
234 public function getVersion() {
235 return __CLASS__ . ': $Id: ApiQueryDeletedrevs.php 23531 2007-06-30 01:19:14Z simetrical $';
236 }
237 }