API: (bug 11405) Expand templates implementation in the API
[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, $fld_size = 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 $this->fld_size = $this->addFieldsIf('rev_len', isset ($prop['size']));
87
88 if (isset ($prop['user'])) {
89 $this->addFields('rev_user');
90 $this->addFields('rev_user_text');
91 $this->fld_user = true;
92 }
93 if (isset ($prop['content'])) {
94
95 // For each page we will request, the user must have read rights for that page
96 foreach ($pageSet->getGoodTitles() as $title) {
97 if( !$title->userCanRead() )
98 $this->dieUsage(
99 'The current user is not allowed to read ' . $title->getPrefixedText(),
100 'accessdenied');
101 }
102
103 $this->addTables('text');
104 $this->addWhere('rev_text_id=old_id');
105 $this->addFields('old_id');
106 $this->addFields('old_text');
107 $this->addFields('old_flags');
108
109 $this->fld_content = true;
110
111 $this->expandTemplates = $expandtemplates;
112 }
113
114 $userMax = ($this->fld_content ? 50 : 500);
115 $botMax = ($this->fld_content ? 200 : 10000);
116
117 if ($enumRevMode) {
118
119 // This is mostly to prevent parameter errors (and optimize sql?)
120 if (!is_null($startid) && !is_null($start))
121 $this->dieUsage('start and startid cannot be used together', 'badparams');
122
123 if (!is_null($endid) && !is_null($end))
124 $this->dieUsage('end and endid cannot be used together', 'badparams');
125
126 if(!is_null($user) && !is_null( $excludeuser))
127 $this->dieUsage('user and excludeuser cannot be used together', 'badparams');
128
129 // This code makes an assumption that sorting by rev_id and rev_timestamp produces
130 // the same result. This way users may request revisions starting at a given time,
131 // but to page through results use the rev_id returned after each page.
132 // Switching to rev_id removes the potential problem of having more than
133 // one row with the same timestamp for the same page.
134 // The order needs to be the same as start parameter to avoid SQL filesort.
135
136 if (is_null($startid))
137 $this->addWhereRange('rev_timestamp', $dir, $start, $end);
138 else
139 $this->addWhereRange('rev_id', $dir, $startid, $endid);
140
141 // must manually initialize unset limit
142 if (is_null($limit))
143 $limit = 10;
144 $this->validateLimit('limit', $limit, 1, $userMax, $botMax);
145
146 // There is only one ID, use it
147 $this->addWhereFld('rev_page', current(array_keys($pageSet->getGoodTitles())));
148
149 if(!is_null($user)) {
150 $this->addWhereFld('rev_user_text', $user);
151 } elseif (!is_null( $excludeuser)) {
152 $this->addWhere('rev_user_text != ' . $this->getDB()->addQuotes($excludeuser));
153 }
154 }
155 elseif ($revCount > 0) {
156 $this->validateLimit('rev_count', $revCount, 1, $userMax, $botMax);
157
158 // Get all revision IDs
159 $this->addWhereFld('rev_id', array_keys($pageSet->getRevisionIDs()));
160
161 // assumption testing -- we should never get more then $revCount rows.
162 $limit = $revCount;
163 }
164 elseif ($pageCount > 0) {
165 // When working in multi-page non-enumeration mode,
166 // limit to the latest revision only
167 $this->addTables('page');
168 $this->addWhere('page_id=rev_page');
169 $this->addWhere('page_latest=rev_id');
170 $this->validateLimit('page_count', $pageCount, 1, $userMax, $botMax);
171
172 // Get all page IDs
173 $this->addWhereFld('page_id', array_keys($pageSet->getGoodTitles()));
174
175 // assumption testing -- we should never get more then $pageCount rows.
176 $limit = $pageCount;
177 } else
178 ApiBase :: dieDebug(__METHOD__, 'param validation?');
179
180 $this->addOption('LIMIT', $limit +1);
181
182 $data = array ();
183 $count = 0;
184 $res = $this->select(__METHOD__);
185
186 $db = $this->getDB();
187 while ($row = $db->fetchObject($res)) {
188
189 if (++ $count > $limit) {
190 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
191 if (!$enumRevMode)
192 ApiBase :: dieDebug(__METHOD__, 'Got more rows then expected'); // bug report
193 $this->setContinueEnumParameter('startid', intval($row->rev_id));
194 break;
195 }
196
197 $this->getResult()->addValue(
198 array (
199 'query',
200 'pages',
201 intval($row->rev_page),
202 'revisions'),
203 null,
204 $this->extractRowInfo($row));
205 }
206 $db->freeResult($res);
207
208 // Ensure that all revisions are shown as '<rev>' elements
209 $result = $this->getResult();
210 if ($result->getIsRawMode()) {
211 $data =& $result->getData();
212 foreach ($data['query']['pages'] as & $page) {
213 if (is_array($page) && array_key_exists('revisions', $page)) {
214 $result->setIndexedTagName($page['revisions'], 'rev');
215 }
216 }
217 }
218 }
219
220 private function extractRowInfo($row) {
221
222 $vals = array ();
223
224 if ($this->fld_ids) {
225 $vals['revid'] = intval($row->rev_id);
226 // $vals['oldid'] = intval($row->rev_text_id); // todo: should this be exposed?
227 }
228
229 if ($this->fld_flags && $row->rev_minor_edit)
230 $vals['minor'] = '';
231
232 if ($this->fld_user) {
233 $vals['user'] = $row->rev_user_text;
234 if (!$row->rev_user)
235 $vals['anon'] = '';
236 }
237
238 if ($this->fld_timestamp) {
239 $vals['timestamp'] = wfTimestamp(TS_ISO_8601, $row->rev_timestamp);
240 }
241
242 if ($this->fld_size && !is_null($row->rev_len)) {
243 $vals['size'] = intval($row->rev_len);
244 }
245
246 if ($this->fld_comment && !empty ($row->rev_comment)) {
247 $vals['comment'] = $row->rev_comment;
248 }
249
250 if ($this->fld_content) {
251 $text = Revision :: getRevisionText($row);
252 if ($this->expandTemplates) {
253 global $wgParser;
254 $text = $wgParser->preprocess( $text, Title::newFromID($row->rev_page), new ParserOptions() );
255 }
256 ApiResult :: setContent($vals, $text);
257 }
258
259 return $vals;
260 }
261
262 protected function getAllowedParams() {
263 return array (
264 'prop' => array (
265 ApiBase :: PARAM_ISMULTI => true,
266 ApiBase :: PARAM_DFLT => 'ids|timestamp|flags|comment|user',
267 ApiBase :: PARAM_TYPE => array (
268 'ids',
269 'flags',
270 'timestamp',
271 'user',
272 'size',
273 'comment',
274 'content',
275 )
276 ),
277 'limit' => array (
278 ApiBase :: PARAM_TYPE => 'limit',
279 ApiBase :: PARAM_MIN => 1,
280 ApiBase :: PARAM_MAX => ApiBase :: LIMIT_SML1,
281 ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_SML2
282 ),
283 'startid' => array (
284 ApiBase :: PARAM_TYPE => 'integer'
285 ),
286 'endid' => array (
287 ApiBase :: PARAM_TYPE => 'integer'
288 ),
289 'start' => array (
290 ApiBase :: PARAM_TYPE => 'timestamp'
291 ),
292 'end' => array (
293 ApiBase :: PARAM_TYPE => 'timestamp'
294 ),
295 'dir' => array (
296 ApiBase :: PARAM_DFLT => 'older',
297 ApiBase :: PARAM_TYPE => array (
298 'newer',
299 'older'
300 )
301 ),
302 'user' => array(
303 ApiBase :: PARAM_TYPE => 'user'
304 ),
305 'excludeuser' => array(
306 ApiBase :: PARAM_TYPE => 'user'
307 ),
308
309 'expandtemplates' => false,
310 );
311 }
312
313 protected function getParamDescription() {
314 return array (
315 'prop' => 'Which properties to get for each revision.',
316 'limit' => 'limit how many revisions will be returned (enum)',
317 'startid' => 'from which revision id to start enumeration (enum)',
318 'endid' => 'stop revision enumeration on this revid (enum)',
319 'start' => 'from which revision timestamp to start enumeration (enum)',
320 'end' => 'enumerate up to this timestamp (enum)',
321 'dir' => 'direction of enumeration - towards "newer" or "older" revisions (enum)',
322 'user' => 'only include revisions made by user',
323 'excludeuser' => 'exclude revisions made by user',
324 'expandtemplates' => 'expand templates in revision content'
325 );
326 }
327
328 protected function getDescription() {
329 return array (
330 'Get revision information.',
331 'This module may be used in several ways:',
332 ' 1) Get data about a set of pages (last revision), by setting titles or pageids parameter.',
333 ' 2) Get revisions for one given page, by using titles/pageids with start/end/limit params.',
334 ' 3) Get data about a set of revisions by setting their IDs with revids parameter.',
335 'All parameters marked as (enum) may only be used with a single page (#2).'
336 );
337 }
338
339 protected function getExamples() {
340 return array (
341 'Get data with content for the last revision of titles "API" and "Main Page":',
342 ' api.php?action=query&prop=revisions&titles=API|Main%20Page&rvprop=timestamp|user|comment|content',
343 'Get last 5 revisions of the "Main Page":',
344 ' api.php?action=query&prop=revisions&titles=Main%20Page&rvlimit=5&rvprop=timestamp|user|comment',
345 'Get first 5 revisions of the "Main Page":',
346 ' api.php?action=query&prop=revisions&titles=Main%20Page&rvlimit=5&rvprop=timestamp|user|comment&rvdir=newer',
347 'Get first 5 revisions of the "Main Page" made after 2006-05-01:',
348 ' api.php?action=query&prop=revisions&titles=Main%20Page&rvlimit=5&rvprop=timestamp|user|comment&rvdir=newer&rvstart=20060501000000',
349 'Get first 5 revisions of the "Main Page" that were not made made by anonymous user "127.0.0.1"',
350 ' api.php?action=query&prop=revisions&titles=Main%20Page&rvlimit=5&rvprop=timestamp|user|comment&rvexcludeuser=127.0.0.1',
351 'Get first 5 revisions of the "Main Page" that were made by the user "MediaWiki default"',
352 ' api.php?action=query&prop=revisions&titles=Main%20Page&rvlimit=5&rvprop=timestamp|user|comment&rvuser=MediaWiki%20default',
353 );
354 }
355
356 public function getVersion() {
357 return __CLASS__ . ': $Id$';
358 }
359 }
360