f35075fd7a5b04ee14f640bf29c5b997a27eb781
[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 // true when ordered by timestamp from older to newer, false otherwise
43 $dirNewer = ($rvdir === 'newer');
44
45 // If any of those parameters are used, work in 'enumeration' mode.
46 // Enum mode can only be used when exactly one page is provided.
47 // Enumerating revisions on multiple pages make it extremelly
48 // difficult to manage continuations and require additional sql indexes
49 $enumRevMode = ($rvlimit !== 0 || $rvstartid !== 0 || $rvendid !== 0 || $dirNewer || isset ($rvstart) || isset ($rvend));
50
51 $data = $this->getData();
52 $pageCount = $data->getGoodTitleCount();
53 $revCount = $data->getRevisionCount();
54
55 // Optimization -- nothing to do
56 if ($revCount === 0 && $pageCount === 0)
57 return;
58
59 if ($revCount > 0 && $pageCount > 0)
60 $this->dieUsage('The revids= parameter may not be used with titles, pageids, or generator options.', 'rv_revids');
61
62 if ($revCount > 0 && $enumRevMode)
63 $this->dieUsage('The revids= parameter may not be used with the list options (rvlimit, rvstartid, rvendid, dirNewer, rvstart, rvend).', 'rv_revids');
64
65 if ($revCount === 0 && $pageCount > 1 && $enumRevMode)
66 $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');
67
68 $tables = array (
69 'revision'
70 );
71 $fields = array (
72 'rev_id',
73 'rev_page',
74 'rev_text_id',
75 'rev_minor_edit'
76 );
77 $conds = array (
78 'rev_deleted' => 0
79 );
80 $options = array ();
81
82 $showTimestamp = $showUser = $showComment = $showContent = false;
83 if (isset ($rvprop)) {
84 foreach ($rvprop as $prop) {
85 switch ($prop) {
86 case 'timestamp' :
87 $fields[] = 'rev_timestamp';
88 $showTimestamp = true;
89 break;
90 case 'user' :
91 $fields[] = 'rev_user';
92 $fields[] = 'rev_user_text';
93 $showUser = true;
94 break;
95 case 'comment' :
96 $fields[] = 'rev_comment';
97 $showComment = true;
98 break;
99 case 'content' :
100 // todo: check the page count/limit when requesting content
101 //$this->validateLimit( 'content: (rvlimit*pages)+revids',
102 //$rvlimit * count($this->existingPageIds) + count($this->revIdsArray), 50, 200 );
103 $tables[] = 'text';
104 $conds[] = 'rev_text_id=old_id';
105 $fields[] = 'old_id';
106 $fields[] = 'old_text';
107 $fields[] = 'old_flags';
108 $showContent = true;
109 break;
110 default :
111 ApiBase :: dieDebug("unknown rvprop $prop");
112 }
113 }
114 }
115
116 $userMax = ($showContent ? 50 : 500);
117 $botMax = ($showContent ? 200 : 10000);
118
119 if ($enumRevMode) {
120
121 // This is mostly to prevent parameter errors (and optimize sql?)
122 if ($rvstartid !== 0 && isset ($rvstart))
123 $this->dieUsage('rvstart and rvstartid cannot be used together', 'rv_badparams');
124
125 if ($rvendid !== 0 && isset ($rvend))
126 $this->dieUsage('rvend and rvend cannot be used together', 'rv_badparams');
127
128 $options['ORDER BY'] = 'rev_timestamp' . ($dirNewer ? '' : ' DESC');
129 $before = ($dirNewer ? '<=' : '>=');
130 $after = ($dirNewer ? '>=' : '<=');
131
132 if ($rvstartid !== 0)
133 $conds[] = 'rev_id' . $after . intval($rvstartid);
134 if ($rvendid !== 0)
135 $conds[] = 'rev_id' . $before . intval($rvendid);
136 if (isset ($rvstart))
137 $conds[] = 'rev_timestamp' . $after . $this->prepareTimestamp($rvstart);
138 if (isset ($rvend))
139 $conds[] = 'rev_timestamp' . $before . $this->prepareTimestamp($rvend);
140
141 // must manually initialize unset rvlimit
142 if (!isset ($rvlimit))
143 $rvlimit = 10;
144
145 $this->validateLimit('rvlimit', $rvlimit, 1, $userMax, $botMax);
146
147 // There is only one ID, use it
148 $conds['rev_page'] = array_pop(array_keys($data->getGoodTitles()));
149
150 } else
151 if ($pageCount > 0) {
152 // When working in multi-page non-enumeration mode,
153 // limit to the latest revision only
154 $tables[] = 'page';
155 $conds[] = 'page_id=rev_page';
156 $conds[] = 'page_latest=rev_id';
157 $this->validateLimit('page_count', $pageCount, 1, $userMax, $botMax);
158
159 // Get all page IDs
160 $conds['page_id'] = array_keys($data->getGoodTitles());
161
162 $rvlimit = $pageCount; // assumption testing -- we should never get more then $pageCount rows.
163 } else
164 if ($revCount > 0) {
165 $this->validateLimit('rev_count', $revCount, 1, $userMax, $botMax);
166
167 // Get all revision IDs
168 $conds['rev_id'] = array_keys($data->getRevisionIDs());
169
170 $rvlimit = $revCount; // assumption testing -- we should never get more then $revCount rows.
171 } else
172 ApiBase :: dieDebug('param validation?');
173
174 $options['LIMIT'] = $rvlimit +1;
175
176 $db = $this->getDB();
177 $this->profileDBIn();
178 $res = $db->select($tables, $fields, $conds, __CLASS__ . '::' . __FUNCTION__, $options);
179 $this->profileDBOut();
180
181 $data = array ();
182 $count = 0;
183 while ($row = $db->fetchObject($res)) {
184
185 if (++ $count > $rvlimit) {
186 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
187 if (!$enumRevMode)
188 ApiBase :: dieDebug('Got more rows then expected'); // bug report
189
190 $startStr = 'rvstartid=' . $row->rev_id;
191 $msg = array (
192 'continue' => $startStr
193 );
194 $this->getResult()->addValue('query-status', 'revisions', $msg);
195 break;
196 }
197
198 $vals = array (
199 'revid' => intval($row->rev_id
200 ), 'oldid' => intval($row->rev_text_id));
201
202 if ($row->rev_minor_edit) {
203 $vals['minor'] = '';
204 }
205
206 if ($showTimestamp)
207 $vals['timestamp'] = wfTimestamp(TS_ISO_8601, $row->rev_timestamp);
208
209 if ($showUser) {
210 $vals['user'] = $row->rev_user_text;
211 if (!$row->rev_user)
212 $vals['anon'] = '';
213 }
214
215 if ($showComment)
216 $vals['comment'] = $row->rev_comment;
217
218 if ($showContent) {
219 ApiResult :: addContent($vals, Revision :: getRevisionText($row));
220 }
221
222 $this->getResult()->addValue(array (
223 'query',
224 'pages',
225 intval($row->rev_page
226 ), 'revisions'), intval($row->rev_id), $vals);
227 }
228 $db->freeResult($res);
229
230 // Ensure that all revisions are shown as '<r>' elements
231 $data = & $this->getResultData();
232 foreach ($data['query']['pages'] as & $page) {
233 if (isset ($page['revisions'])) {
234 ApiResult :: setIndexedTagName($page['revisions'], 'rev');
235 }
236 }
237 }
238
239 protected function getAllowedParams() {
240 return array (
241 'rvlimit' => array (
242 GN_ENUM_DFLT => 0,
243 GN_ENUM_TYPE => 'limit',
244 GN_ENUM_MIN => 0,
245 GN_ENUM_MAX1 => 50,
246 GN_ENUM_MAX2 => 500
247 ),
248 'rvstartid' => 0,
249 'rvendid' => 0,
250 'rvstart' => array (
251 GN_ENUM_TYPE => 'timestamp'
252 ),
253 'rvend' => array (
254 GN_ENUM_TYPE => 'timestamp'
255 ),
256 'rvdir' => array (
257 GN_ENUM_DFLT => 'older',
258 GN_ENUM_TYPE => array (
259 'newer',
260 'older'
261 )
262 ),
263 'rvprop' => array (
264 GN_ENUM_ISMULTI => true,
265 GN_ENUM_TYPE => array (
266 'timestamp',
267 'user',
268 'comment',
269 'content'
270 )
271 )
272 );
273 }
274
275 protected function getDescription() {
276 return array (
277 'Get revision information.',
278 'This module may be used in several ways:',
279 ' 1) Get data about a set of pages (last revision), by setting titles or pageids parameter.',
280 ' 2) Get revisions for one given page, by using titles/pageids with rvstart/rvend/rvlimit params.',
281 ' 3) Get data about a set of revisions by setting their IDs with revids parameter.'
282 );
283 }
284
285 protected function getExamples() {
286 return array (
287 'api.php?action=query&prop=revisions&titles=Main%20Page&rvprop=timestamp|user|comment|content'
288 );
289 }
290 }
291 ?>