* API: revisions & pageset cleanup
[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 if ($revCount > 0 && $pageCount > 0)
56 $this->dieUsage('The rvrevids= parameter may not be used with titles, pageids, and generator options.', 'rv_rvrevids');
57
58 if ($revCount > 0 && $enumRevMode)
59 $this->dieUsage('The rvrevids= parameter may not be used with the list options (rvlimit, rvstartid, rvendid, dirNewer, rvstart, rvend).', 'rv_rvrevids');
60
61 if ($revCount === 0 && $pageCount === 0)
62 $this->dieUsage('No pages were given. Please use titles, pageids or a generator to provide page(s) to work on.', 'rv_no_pages');
63
64 if ($revCount === 0 && $pageCount > 1 && $enumRevMode)
65 $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');
66
67 $tables = array (
68 'revision'
69 );
70 $fields = array (
71 'rev_id',
72 'rev_page',
73 'rev_text_id',
74 'rev_minor_edit'
75 );
76 $conds = array (
77 'rev_deleted' => 0
78 );
79 $options = array ();
80
81 $showTimestamp = $showUser = $showComment = $showContent = false;
82 if (isset ($rvprop)) {
83 foreach ($rvprop as $prop) {
84 switch ($prop) {
85 case 'timestamp' :
86 $fields[] = 'rev_timestamp';
87 $showTimestamp = true;
88 break;
89 case 'user' :
90 $fields[] = 'rev_user';
91 $fields[] = 'rev_user_text';
92 $showUser = true;
93 break;
94 case 'comment' :
95 $fields[] = 'rev_comment';
96 $showComment = true;
97 break;
98 case 'content' :
99 // todo: check the page count/limit when requesting content
100 //$this->validateLimit( 'content: (rvlimit*pages)+revids',
101 //$rvlimit * count($this->existingPageIds) + count($this->revIdsArray), 50, 200 );
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 $this->dieDebug("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 $options['ORDER BY'] = 'rev_timestamp' . ($dirNewer ? '' : ' DESC');
128 $before = ($dirNewer ? '<=' : '>=');
129 $after = ($dirNewer ? '>=' : '<=');
130
131 if ($rvstartid !== 0)
132 $conds[] = 'rev_id' . $after . intval($rvstartid);
133 if ($rvendid !== 0)
134 $conds[] = 'rev_id' . $before . intval($rvendid);
135 if (isset ($rvstart))
136 $conds[] = 'rev_timestamp' . $after . $this->prepareTimestamp($rvstart);
137 if (isset ($rvend))
138 $conds[] = 'rev_timestamp' . $before . $this->prepareTimestamp($rvend);
139
140 // must manually initialize unset rvlimit
141 if (!isset ($rvlimit))
142 $rvlimit = 10;
143
144 $this->validateLimit('rvlimit', $rvlimit, 1, $userMax, $botMax);
145
146 // There is only one ID, use it
147 $conds['rev_page'] = array_pop(array_keys($data->getGoodTitles()));
148
149 } else
150 if ($pageCount > 0) {
151 // When working in multi-page non-enumeration mode,
152 // limit to the latest revision only
153 $tables[] = 'page';
154 $conds[] = 'page_id=rev_page';
155 $conds[] = 'page_latest=rev_id';
156 $this->validateLimit('page_count', $pageCount, 1, $userMax, $botMax);
157
158 // Get all page IDs
159 $conds['page_id'] = array_keys($data->getGoodTitles());
160
161 $rvlimit = $pageCount; // assumption testing -- we should never get more then $pageCount rows.
162 } else
163 if ($revCount > 0) {
164 $this->validateLimit('rev_count', $revCount, 1, $userMax, $botMax);
165
166 // Get all revision IDs
167 $conds['rev_id'] = array_keys($data->getRevisionIDs());
168
169 $rvlimit = $revCount; // assumption testing -- we should never get more then $revCount rows.
170 } else
171 $this->dieDebug('param validation?');
172
173 $options['LIMIT'] = $rvlimit +1;
174
175 $db = $this->getDB();
176 $this->profileDBIn();
177 $res = $db->select($tables, $fields, $conds, __CLASS__ . '::' . __FUNCTION__, $options);
178 $this->profileDBOut();
179
180 $data = array ();
181 $count = 0;
182 while ($row = $db->fetchObject($res)) {
183
184 if (++ $count > $rvlimit) {
185 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
186 if (!$enumRevMode)
187 $this->dieDebug('Got more rows then expected'); // bug report
188
189 $startStr = 'rvstartid=' . $row->rev_id;
190 $msg = array (
191 'continue' => $startStr
192 );
193 $this->getResult()->addMessage('query-status', 'revisions', $msg);
194 break;
195 }
196
197 $vals = array (
198 'revid' => intval($row->rev_id
199 ), 'oldid' => intval($row->rev_text_id));
200
201 if ($row->rev_minor_edit) {
202 $vals['minor'] = '';
203 }
204
205 if ($showTimestamp)
206 $vals['timestamp'] = wfTimestamp(TS_ISO_8601, $row->rev_timestamp);
207
208 if ($showUser) {
209 $vals['user'] = $row->rev_user_text;
210 if (!$row->rev_user)
211 $vals['anon'] = '';
212 }
213
214 if ($showComment)
215 $vals['comment'] = $row->rev_comment;
216
217 if ($showContent) {
218 $vals['xml:space'] = 'preserve';
219 $vals['*'] = Revision :: getRevisionText($row);
220 } else {
221 $vals['*'] = ''; // Force all elements to be attributes
222 }
223
224 $data[$row->rev_page]['revisions']['_element'] = 'rv';
225 $data[$row->rev_page]['revisions'][$row->rev_id] = $vals;
226 }
227 $db->freeResult($res);
228
229 $this->getResult()->addMessage('query', 'allpages', $data);
230 }
231
232 protected function getAllowedParams() {
233 return array (
234 'rvrevids' => array (
235 GN_ENUM_ISMULTI => true,
236 GN_ENUM_TYPE => 'integer'
237 ),
238 'rvlimit' => array (
239 GN_ENUM_DFLT => 0,
240 GN_ENUM_TYPE => 'limit',
241 GN_ENUM_MIN => 0,
242 GN_ENUM_MAX1 => 50,
243 GN_ENUM_MAX2 => 500
244 ),
245 'rvstartid' => 0,
246 'rvendid' => 0,
247 'rvstart' => array (
248 GN_ENUM_TYPE => 'timestamp'
249 ),
250 'rvend' => array (
251 GN_ENUM_TYPE => 'timestamp'
252 ),
253 'rvdir' => array (
254 GN_ENUM_DFLT => 'older',
255 GN_ENUM_TYPE => array (
256 'newer',
257 'older'
258 )
259 ),
260 'rvprop' => array (
261 GN_ENUM_ISMULTI => true,
262 GN_ENUM_TYPE => array (
263 'timestamp',
264 'user',
265 'comment',
266 'content'
267 )
268 )
269 );
270 }
271
272 protected function getDescription() {
273 return array (
274 'Get revision information.',
275 'This module may be used in several ways:',
276 ' 1) Get data about a set of pages (last revision), by setting titles or pageids parameter.',
277 ' 2) Get revisions for one given page, by using titles/pageids with rvstart*/rvend*/rvlimit params.',
278 ' 3) Get data about a set of revisions by setting their IDs with revids parameter.'
279 );
280 }
281
282 protected function getExamples() {
283 return array (
284 'api.php?action=query&prop=revisions&titles=ArticleA&rvprop=timestamp|user|comment|content'
285 );
286 }
287 }
288 ?>