729b6237b67c43f1ff0d818845ff8b2d74fdeb5a
[lhc/web/wiklou.git] / includes / api / ApiQueryRevisions.php
1 <?php
2
3
4 /*
5 * Created on Sep 7, 2006
6 *
7 * API for MediaWiki 1.8+
8 *
9 * Copyright (C) 2006 Yuri Astrakhan <FirstnameLastname@gmail.com>
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 * http://www.gnu.org/copyleft/gpl.html
25 */
26
27 if (!defined('MEDIAWIKI')) {
28 // Eclipse helper - will be ignored in production
29 require_once ('ApiQueryBase.php');
30 }
31
32 class ApiQueryRevisions extends ApiQueryBase {
33
34 public function __construct($query, $moduleName) {
35 parent :: __construct($query, $moduleName, 'rv');
36 }
37
38 public function execute() {
39 $limit = $startid = $endid = $start = $end = $dir = $prop = null;
40 extract($this->extractRequestParams());
41
42 $db = $this->getDB();
43
44 // true when ordered by timestamp from older to newer, false otherwise
45 $dirNewer = ($dir === 'newer');
46
47 // If any of those parameters are used, work in 'enumeration' mode.
48 // Enum mode can only be used when exactly one page is provided.
49 // Enumerating revisions on multiple pages make it extremelly
50 // difficult to manage continuations and require additional sql indexes
51 $enumRevMode = (!is_null($limit) || !is_null($startid) || !is_null($endid) || $dirNewer || !is_null($start) || !is_null($end));
52
53 $pageSet = $this->getPageSet();
54 $pageCount = $pageSet->getGoodTitleCount();
55 $revCount = $pageSet->getRevisionCount();
56
57 // Optimization -- nothing to do
58 if ($revCount === 0 && $pageCount === 0)
59 return;
60
61 if ($revCount > 0 && $enumRevMode)
62 $this->dieUsage('The revids= parameter may not be used with the list options (limit, startid, endid, dirNewer, start, end).', 'revids');
63
64 if ($pageCount > 1 && $enumRevMode)
65 $this->dieUsage('titles, pageids or a generator was used to supply multiple pages, but the limit, startid, endid, dirNewer, start, and end parameters may only be used on a single page.', 'multpages');
66
67 $tables = array (
68 'revision'
69 );
70 $fields = array (
71 'rev_id',
72 'rev_page',
73 'rev_text_id',
74 'rev_minor_edit'
75 );
76 $where = array (
77 'rev_deleted' => 0
78 );
79 $options = array ();
80
81 $showTimestamp = $showUser = $showComment = $showContent = false;
82 if (!is_null($prop)) {
83 foreach ($prop as $p) {
84 switch ($p) {
85 case 'timestamp' :
86 $fields[] = 'rev_timestamp';
87 $showTimestamp = true;
88 break;
89 case 'user' :
90 $fields[] = 'rev_user';
91 $fields[] = 'rev_user_text';
92 $showUser = true;
93 break;
94 case 'comment' :
95 $fields[] = 'rev_comment';
96 $showComment = true;
97 break;
98 case 'content' :
99 $tables[] = 'text';
100 $where[] = 'rev_text_id=old_id';
101 $fields[] = 'old_id';
102 $fields[] = 'old_text';
103 $fields[] = 'old_flags';
104 $showContent = true;
105 break;
106 default :
107 ApiBase :: dieDebug(__METHOD__, "unknown prop $p");
108 }
109 }
110 }
111
112 $userMax = ($showContent ? 50 : 500);
113 $botMax = ($showContent ? 200 : 10000);
114
115 if ($enumRevMode) {
116
117 // This is mostly to prevent parameter errors (and optimize sql?)
118 if (!is_null($startid) && !is_null($start))
119 $this->dieUsage('start and startid cannot be used together', 'badparams');
120
121 if (!is_null($endid) && !is_null($end))
122 $this->dieUsage('end and endid cannot be used together', 'badparams');
123
124 // This code makes an assumption that sorting by rev_id and rev_timestamp produces
125 // the same result. This way users may request revisions starting at a given time,
126 // but to page through results use the rev_id returned after each page.
127 // Switching to rev_id removes the potential problem of having more than
128 // one row with the same timestamp for the same page.
129 // The order needs to be the same as start parameter to avoid SQL filesort.
130 $options['ORDER BY'] = (!is_null($startid) ? 'rev_id' : 'rev_timestamp') . ($dirNewer ? '' : ' DESC');
131
132 $before = ($dirNewer ? '<=' : '>=');
133 $after = ($dirNewer ? '>=' : '<=');
134
135 if (!is_null($startid))
136 $where[] = 'rev_id' . $after . intval($startid);
137 if (!is_null($endid))
138 $where[] = 'rev_id' . $before . intval($endid);
139 if (!is_null($start))
140 $where[] = 'rev_timestamp' . $after . $db->addQuotes($start);
141 if (!is_null($end))
142 $where[] = 'rev_timestamp' . $before . $db->addQuotes($end);
143
144 // must manually initialize unset limit
145 if (!!is_null($limit))
146 $limit = 10;
147
148 $this->validateLimit($this->encodeParamName('limit'), $limit, 1, $userMax, $botMax);
149
150 // There is only one ID, use it
151 $where['rev_page'] = array_pop(array_keys($pageSet->getGoodTitles()));
152
153 }
154 elseif ($pageCount > 0) {
155 // When working in multi-page non-enumeration mode,
156 // limit to the latest revision only
157 $tables[] = 'page';
158 $where[] = 'page_id=rev_page';
159 $where[] = 'page_latest=rev_id';
160 $this->validateLimit('page_count', $pageCount, 1, $userMax, $botMax);
161
162 // Get all page IDs
163 $where['page_id'] = array_keys($pageSet->getGoodTitles());
164
165 $limit = $pageCount; // assumption testing -- we should never get more then $pageCount rows.
166 }
167 elseif ($revCount > 0) {
168 $this->validateLimit('rev_count', $revCount, 1, $userMax, $botMax);
169
170 // Get all revision IDs
171 $where['rev_id'] = array_keys($pageSet->getRevisionIDs());
172
173 $limit = $revCount; // assumption testing -- we should never get more then $revCount rows.
174 } else
175 ApiBase :: dieDebug(__METHOD__, 'param validation?');
176
177 $options['LIMIT'] = $limit +1;
178
179 $this->profileDBIn();
180 $res = $db->select($tables, $fields, $where, __METHOD__, $options);
181 $this->profileDBOut();
182
183 $data = array ();
184 $count = 0;
185 while ($row = $db->fetchObject($res)) {
186
187 if (++ $count > $limit) {
188 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
189 if (!$enumRevMode)
190 ApiBase :: dieDebug(__METHOD__, 'Got more rows then expected'); // bug report
191 $this->setContinueEnumParameter('startid', $row->rev_id);
192 break;
193 }
194
195 $vals = array (
196 'revid' => intval($row->rev_id
197 ), 'oldid' => intval($row->rev_text_id));
198
199 if ($row->rev_minor_edit) {
200 $vals['minor'] = '';
201 }
202
203 if ($showTimestamp)
204 $vals['timestamp'] = wfTimestamp(TS_ISO_8601, $row->rev_timestamp);
205
206 if ($showUser) {
207 $vals['user'] = $row->rev_user_text;
208 if (!$row->rev_user)
209 $vals['anon'] = '';
210 }
211
212 if ($showComment)
213 $vals['comment'] = $row->rev_comment;
214
215 if ($showContent) {
216 ApiResult :: setContent($vals, Revision :: getRevisionText($row));
217 }
218
219 $this->getResult()->addValue(array (
220 'query',
221 'pages',
222 intval($row->rev_page
223 ), 'revisions'), intval($row->rev_id), $vals);
224 }
225 $db->freeResult($res);
226
227 // Ensure that all revisions are shown as '<r>' elements
228 $data = & $this->getResultData();
229 foreach ($data['query']['pages'] as & $page) {
230 if (is_array($page) && array_key_exists('revisions', $page)) {
231 ApiResult :: setIndexedTagName($page['revisions'], 'rev');
232 }
233 }
234 }
235
236 protected function getAllowedParams() {
237 return array (
238 'prop' => array (
239 ApiBase :: PARAM_ISMULTI => true,
240 ApiBase :: PARAM_TYPE => array (
241 'timestamp',
242 'user',
243 'comment',
244 'content'
245 )
246 ),
247 'limit' => array (
248 ApiBase :: PARAM_TYPE => 'limit',
249 ApiBase :: PARAM_MIN => 1,
250 ApiBase :: PARAM_MAX1 => ApiBase :: LIMIT_SML1,
251 ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_SML2
252 ),
253 'startid' => array (
254 ApiBase :: PARAM_TYPE => 'integer'
255 ),
256 'endid' => array (
257 ApiBase :: PARAM_TYPE => 'integer'
258 ),
259 'start' => array (
260 ApiBase :: PARAM_TYPE => 'timestamp'
261 ),
262 'end' => array (
263 ApiBase :: PARAM_TYPE => 'timestamp'
264 ),
265 'dir' => array (
266 ApiBase :: PARAM_DFLT => 'older',
267 ApiBase :: PARAM_TYPE => array (
268 'newer',
269 'older'
270 )
271 )
272 );
273 }
274
275 protected function getParamDescription() {
276 return array (
277 'prop' => 'Which properties to get for each revision.',
278 'limit' => 'limit how many revisions will be returned (enum)',
279 'startid' => 'from which revision id to start enumeration (enum)',
280 'endid' => 'stop revision enumeration on this revid (enum)',
281 'start' => 'from which revision timestamp to start enumeration (enum)',
282 'end' => 'enumerate up to this timestamp (enum)',
283 'dir' => 'direction of enumeration - towards "newer" or "older" revisions (enum)'
284 );
285 }
286
287 protected function getDescription() {
288 return array (
289 'Get revision information.',
290 'This module may be used in several ways:',
291 ' 1) Get data about a set of pages (last revision), by setting titles or pageids parameter.',
292 ' 2) Get revisions for one given page, by using titles/pageids with start/end/limit params.',
293 ' 3) Get data about a set of revisions by setting their IDs with revids parameter.',
294 'All parameters marked as (enum) may only be used with a single page (#2).'
295 );
296 }
297
298 protected function getExamples() {
299 return array (
300 'Get data with content for the last revision of titles "API" and "Main Page":',
301 ' api.php?action=query&prop=revisions&titles=API|Main%20Page&rvprop=timestamp|user|comment|content',
302 'Get last 5 revisions of the "Main Page":',
303 ' api.php?action=query&prop=revisions&titles=Main%20Page&rvlimit=5&rvprop=timestamp|user|comment',
304 'Get first 5 revisions of the "Main Page":',
305 ' api.php?action=query&prop=revisions&titles=Main%20Page&rvlimit=5&rvprop=timestamp|user|comment&rvdir=newer',
306 'Get first 5 revisions of the "Main Page" made after 2006-05-01:',
307 ' api.php?action=query&prop=revisions&titles=Main%20Page&rvlimit=5&rvprop=timestamp|user|comment&rvdir=newer&rvstart=20060501000000'
308 );
309 }
310
311 public function getVersion() {
312 return __CLASS__ . ': $Id$';
313 }
314 }
315 ?>