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