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