aaaand one more
[lhc/web/wiklou.git] / includes / api / ApiQueryRevisions.php
1 <?php
2
3 /*
4 * Created on Sep 7, 2006
5 *
6 * API for MediaWiki 1.8+
7 *
8 * Copyright (C) 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
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 * A query action to enumerate revisions of a given page, or show top revisions of multiple pages.
33 * Various pieces of information may be shown - flags, comments, and the actual wiki markup of the rev.
34 * In the enumeration mode, ranges of revisions may be requested and filtered.
35 *
36 * @addtogroup API
37 */
38 class ApiQueryRevisions extends ApiQueryBase {
39
40 public function __construct($query, $moduleName) {
41 parent :: __construct($query, $moduleName, 'rv');
42 }
43
44 private $fld_ids = false, $fld_flags = false, $fld_timestamp = false,
45 $fld_comment = false, $fld_user = false, $fld_content = false;
46
47 public function execute() {
48 $limit = $startid = $endid = $start = $end = $dir = $prop = $user = $excludeuser = null;
49 extract($this->extractRequestParams());
50
51 // If any of those parameters are used, work in 'enumeration' mode.
52 // Enum mode can only be used when exactly one page is provided.
53 // Enumerating revisions on multiple pages make it extremelly
54 // difficult to manage continuations and require additional sql indexes
55 $enumRevMode = (!is_null($user) || !is_null($excludeuser) || !is_null($limit) || !is_null($startid) || !is_null($endid) || $dir === 'newer' || !is_null($start) || !is_null($end));
56
57 $pageSet = $this->getPageSet();
58 $pageCount = $pageSet->getGoodTitleCount();
59 $revCount = $pageSet->getRevisionCount();
60
61 // Optimization -- nothing to do
62 if ($revCount === 0 && $pageCount === 0)
63 return;
64
65 if ($revCount > 0 && $enumRevMode)
66 $this->dieUsage('The revids= parameter may not be used with the list options (limit, startid, endid, dirNewer, start, end).', 'revids');
67
68 if ($pageCount > 1 && $enumRevMode)
69 $this->dieUsage('titles, pageids or a generator was used to supply multiple pages, but the limit, startid, endid, dirNewer, user, excludeuser, start, and end parameters may only be used on a single page.', 'multpages');
70
71 $this->addTables('revision');
72 $this->addWhere('rev_deleted=0');
73
74 $prop = array_flip($prop);
75
76 // These field are needed regardless of the client requesting them
77 $this->addFields('rev_id');
78 $this->addFields('rev_page');
79
80 // Optional fields
81 $this->fld_ids = isset ($prop['ids']);
82 // $this->addFieldsIf('rev_text_id', $this->fld_ids); // should this be exposed?
83 $this->fld_flags = $this->addFieldsIf('rev_minor_edit', isset ($prop['flags']));
84 $this->fld_timestamp = $this->addFieldsIf('rev_timestamp', isset ($prop['timestamp']));
85 $this->fld_comment = $this->addFieldsIf('rev_comment', isset ($prop['comment']));
86
87 if (isset ($prop['user'])) {
88 $this->addFields('rev_user');
89 $this->addFields('rev_user_text');
90 $this->fld_user = true;
91 }
92 if (isset ($prop['content'])) {
93 $this->addTables('text');
94 $this->addWhere('rev_text_id=old_id');
95 $this->addFields('old_id');
96 $this->addFields('old_text');
97 $this->addFields('old_flags');
98 $this->fld_content = true;
99 }
100
101 $userMax = ($this->fld_content ? 50 : 500);
102 $botMax = ($this->fld_content ? 200 : 10000);
103
104 if ($enumRevMode) {
105
106 // This is mostly to prevent parameter errors (and optimize sql?)
107 if (!is_null($startid) && !is_null($start))
108 $this->dieUsage('start and startid cannot be used together', 'badparams');
109
110 if (!is_null($endid) && !is_null($end))
111 $this->dieUsage('end and endid cannot be used together', 'badparams');
112
113 if(!is_null($user) && !is_null( $excludeuser))
114 $this->dieUsage('user and excludeuser cannot be used together', 'badparams');
115
116 // This code makes an assumption that sorting by rev_id and rev_timestamp produces
117 // the same result. This way users may request revisions starting at a given time,
118 // but to page through results use the rev_id returned after each page.
119 // Switching to rev_id removes the potential problem of having more than
120 // one row with the same timestamp for the same page.
121 // The order needs to be the same as start parameter to avoid SQL filesort.
122
123 if (is_null($startid))
124 $this->addWhereRange('rev_timestamp', $dir, $start, $end);
125 else
126 $this->addWhereRange('rev_id', $dir, $startid, $endid);
127
128 // must manually initialize unset limit
129 if (is_null($limit))
130 $limit = 10;
131 $this->validateLimit($this->encodeParamName('limit'), $limit, 1, $userMax, $botMax);
132
133 // There is only one ID, use it
134 $this->addWhereFld('rev_page', current(array_keys($pageSet->getGoodTitles())));
135
136 if(!is_null($user)) {
137 $this->addWhereFld('rev_user_text', $user);
138 } elseif (!is_null( $excludeuser)) {
139 $this->addWhere('rev_user_text != ' . $this->getDB()->addQuotes($excludeuser));
140 }
141 }
142 elseif ($revCount > 0) {
143 $this->validateLimit('rev_count', $revCount, 1, $userMax, $botMax);
144
145 // Get all revision IDs
146 $this->addWhereFld('rev_id', array_keys($pageSet->getRevisionIDs()));
147
148 // assumption testing -- we should never get more then $revCount rows.
149 $limit = $revCount;
150 }
151 elseif ($pageCount > 0) {
152 // When working in multi-page non-enumeration mode,
153 // limit to the latest revision only
154 $this->addTables('page');
155 $this->addWhere('page_id=rev_page');
156 $this->addWhere('page_latest=rev_id');
157 $this->validateLimit('page_count', $pageCount, 1, $userMax, $botMax);
158
159 // Get all page IDs
160 $this->addWhereFld('page_id', array_keys($pageSet->getGoodTitles()));
161
162 // assumption testing -- we should never get more then $pageCount rows.
163 $limit = $pageCount;
164 } else
165 ApiBase :: dieDebug(__METHOD__, 'param validation?');
166
167 $this->addOption('LIMIT', $limit +1);
168
169 $data = array ();
170 $count = 0;
171 $res = $this->select(__METHOD__);
172
173 $db = $this->getDB();
174 while ($row = $db->fetchObject($res)) {
175
176 if (++ $count > $limit) {
177 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
178 if (!$enumRevMode)
179 ApiBase :: dieDebug(__METHOD__, 'Got more rows then expected'); // bug report
180 $this->setContinueEnumParameter('startid', intval($row->rev_id));
181 break;
182 }
183
184 $this->getResult()->addValue(
185 array (
186 'query',
187 'pages',
188 intval($row->rev_page),
189 'revisions'),
190 null,
191 $this->extractRowInfo($row));
192 }
193 $db->freeResult($res);
194
195 // Ensure that all revisions are shown as '<rev>' elements
196 $result = $this->getResult();
197 if ($result->getIsRawMode()) {
198 $data =& $result->getData();
199 foreach ($data['query']['pages'] as & $page) {
200 if (is_array($page) && array_key_exists('revisions', $page)) {
201 $result->setIndexedTagName($page['revisions'], 'rev');
202 }
203 }
204 }
205 }
206
207 private function extractRowInfo($row) {
208
209 $vals = array ();
210
211 if ($this->fld_ids) {
212 $vals['revid'] = intval($row->rev_id);
213 $vals['pageid'] = intval($row->rev_page);
214 // $vals['oldid'] = intval($row->rev_text_id); // todo: should this be exposed?
215 }
216
217 if ($this->fld_flags && $row->rev_minor_edit)
218 $vals['minor'] = '';
219
220 if ($this->fld_user) {
221 $vals['user'] = $row->rev_user_text;
222 if (!$row->rev_user)
223 $vals['anon'] = '';
224 }
225
226 if ($this->fld_timestamp) {
227 $vals['timestamp'] = wfTimestamp(TS_ISO_8601, $row->rev_timestamp);
228 }
229
230 if ($this->fld_comment && !empty ($row->rev_comment)) {
231 $vals['comment'] = $row->rev_comment;
232 }
233
234 if ($this->fld_content) {
235 ApiResult :: setContent($vals, Revision :: getRevisionText($row));
236 }
237
238 return $vals;
239 }
240
241 protected function getAllowedParams() {
242 return array (
243 'prop' => array (
244 ApiBase :: PARAM_ISMULTI => true,
245 ApiBase :: PARAM_DFLT => 'ids|timestamp|flags|comment|user',
246 ApiBase :: PARAM_TYPE => array (
247 'ids',
248 'flags',
249 'timestamp',
250 'user',
251 'comment',
252 'content'
253 )
254 ),
255 'limit' => array (
256 ApiBase :: PARAM_TYPE => 'limit',
257 ApiBase :: PARAM_MIN => 1,
258 ApiBase :: PARAM_MAX => ApiBase :: LIMIT_SML1,
259 ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_SML2
260 ),
261 'startid' => array (
262 ApiBase :: PARAM_TYPE => 'integer'
263 ),
264 'endid' => array (
265 ApiBase :: PARAM_TYPE => 'integer'
266 ),
267 'start' => array (
268 ApiBase :: PARAM_TYPE => 'timestamp'
269 ),
270 'end' => array (
271 ApiBase :: PARAM_TYPE => 'timestamp'
272 ),
273 'dir' => array (
274 ApiBase :: PARAM_DFLT => 'older',
275 ApiBase :: PARAM_TYPE => array (
276 'newer',
277 'older'
278 )
279 ),
280 'user' => array(
281 ApiBase :: PARAM_TYPE => 'user'
282 ),
283 'excludeuser' => array(
284 ApiBase :: PARAM_TYPE => 'user'
285 )
286 );
287 }
288
289 protected function getParamDescription() {
290 return array (
291 'prop' => 'Which properties to get for each revision.',
292 'limit' => 'limit how many revisions will be returned (enum)',
293 'startid' => 'from which revision id to start enumeration (enum)',
294 'endid' => 'stop revision enumeration on this revid (enum)',
295 'start' => 'from which revision timestamp to start enumeration (enum)',
296 'end' => 'enumerate up to this timestamp (enum)',
297 'dir' => 'direction of enumeration - towards "newer" or "older" revisions (enum)',
298 'user' => 'only include revisions made by user',
299 'excludeuser' => 'exclude revisions made by user',
300 );
301 }
302
303 protected function getDescription() {
304 return array (
305 'Get revision information.',
306 'This module may be used in several ways:',
307 ' 1) Get data about a set of pages (last revision), by setting titles or pageids parameter.',
308 ' 2) Get revisions for one given page, by using titles/pageids with start/end/limit params.',
309 ' 3) Get data about a set of revisions by setting their IDs with revids parameter.',
310 'All parameters marked as (enum) may only be used with a single page (#2).'
311 );
312 }
313
314 protected function getExamples() {
315 return array (
316 'Get data with content for the last revision of titles "API" and "Main Page":',
317 ' api.php?action=query&prop=revisions&titles=API|Main%20Page&rvprop=timestamp|user|comment|content',
318 'Get last 5 revisions of the "Main Page":',
319 ' api.php?action=query&prop=revisions&titles=Main%20Page&rvlimit=5&rvprop=timestamp|user|comment',
320 'Get first 5 revisions of the "Main Page":',
321 ' api.php?action=query&prop=revisions&titles=Main%20Page&rvlimit=5&rvprop=timestamp|user|comment&rvdir=newer',
322 'Get first 5 revisions of the "Main Page" made after 2006-05-01:',
323 ' api.php?action=query&prop=revisions&titles=Main%20Page&rvlimit=5&rvprop=timestamp|user|comment&rvdir=newer&rvstart=20060501000000',
324 'Get first 5 revisions of the "Main Page" that were not made made by anonymous user "127.0.0.1"',
325 ' api.php?action=query&prop=revisions&titles=Main%20Page&rvlimit=5&rvprop=timestamp|user|comment&rvexcludeuser=127.0.0.1',
326 'Get first 5 revisions of the "Main Page" that were made by the user "MediaWiki default"',
327 ' api.php?action=query&prop=revisions&titles=Main%20Page&rvlimit=5&rvprop=timestamp|user|comment&rvuser=MediaWiki%20default',
328 );
329 }
330
331 public function getVersion() {
332 return __CLASS__ . ': $Id$';
333 }
334 }
335 ?>