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