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