API * Common field output function to simplify result generation
[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_id', $dir, $startid, $endid);
112 else
113 $this->addWhereRange('rev_timestamp', $dir, $start, $end);
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', array_pop(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 $db = $this->getDB();
151 $res = $this->select(__METHOD__);
152 while ($row = $db->fetchObject($res)) {
153
154 if (++ $count > $limit) {
155 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
156 if (!$enumRevMode)
157 ApiBase :: dieDebug(__METHOD__, 'Got more rows then expected'); // bug report
158 $this->setContinueEnumParameter('startid', $row->rev_id);
159 break;
160 }
161
162 $vals = $this->addRowInfo('rev', $row);
163 if ($vals) {
164 if ($showContent)
165 ApiResult :: setContent($vals, Revision :: getRevisionText($row));
166
167 $this->getResult()->addValue(array (
168 'query',
169 'pages',
170 intval($row->rev_page
171 ), 'revisions'), intval($row->rev_id), $vals);
172 }
173 }
174 $db->freeResult($res);
175
176 // Ensure that all revisions are shown as '<rev>' elements
177 $result = $this->getResult();
178 if ($result->getIsRawMode()) {
179 $data = & $result->getData();
180 foreach ($data['query']['pages'] as & $page) {
181 if (is_array($page) && array_key_exists('revisions', $page)) {
182 $result->setIndexedTagName($page['revisions'], 'rev');
183 }
184 }
185 }
186 }
187
188 protected function getAllowedParams() {
189 return array (
190 'prop' => array (
191 ApiBase :: PARAM_ISMULTI => true,
192 ApiBase :: PARAM_TYPE => array (
193 'timestamp',
194 'user',
195 'comment',
196 'content'
197 )
198 ),
199 'limit' => array (
200 ApiBase :: PARAM_TYPE => 'limit',
201 ApiBase :: PARAM_MIN => 1,
202 ApiBase :: PARAM_MAX1 => ApiBase :: LIMIT_SML1,
203 ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_SML2
204 ),
205 'startid' => array (
206 ApiBase :: PARAM_TYPE => 'integer'
207 ),
208 'endid' => array (
209 ApiBase :: PARAM_TYPE => 'integer'
210 ),
211 'start' => array (
212 ApiBase :: PARAM_TYPE => 'timestamp'
213 ),
214 'end' => array (
215 ApiBase :: PARAM_TYPE => 'timestamp'
216 ),
217 'dir' => array (
218 ApiBase :: PARAM_DFLT => 'older',
219 ApiBase :: PARAM_TYPE => array (
220 'newer',
221 'older'
222 )
223 )
224 );
225 }
226
227 protected function getParamDescription() {
228 return array (
229 'prop' => 'Which properties to get for each revision.',
230 'limit' => 'limit how many revisions will be returned (enum)',
231 'startid' => 'from which revision id to start enumeration (enum)',
232 'endid' => 'stop revision enumeration on this revid (enum)',
233 'start' => 'from which revision timestamp to start enumeration (enum)',
234 'end' => 'enumerate up to this timestamp (enum)',
235 'dir' => 'direction of enumeration - towards "newer" or "older" revisions (enum)'
236 );
237 }
238
239 protected function getDescription() {
240 return array (
241 'Get revision information.',
242 'This module may be used in several ways:',
243 ' 1) Get data about a set of pages (last revision), by setting titles or pageids parameter.',
244 ' 2) Get revisions for one given page, by using titles/pageids with start/end/limit params.',
245 ' 3) Get data about a set of revisions by setting their IDs with revids parameter.',
246 'All parameters marked as (enum) may only be used with a single page (#2).'
247 );
248 }
249
250 protected function getExamples() {
251 return array (
252 'Get data with content for the last revision of titles "API" and "Main Page":',
253 ' api.php?action=query&prop=revisions&titles=API|Main%20Page&rvprop=timestamp|user|comment|content',
254 'Get last 5 revisions of the "Main Page":',
255 ' api.php?action=query&prop=revisions&titles=Main%20Page&rvlimit=5&rvprop=timestamp|user|comment',
256 'Get first 5 revisions of the "Main Page":',
257 ' api.php?action=query&prop=revisions&titles=Main%20Page&rvlimit=5&rvprop=timestamp|user|comment&rvdir=newer',
258 'Get first 5 revisions of the "Main Page" made after 2006-05-01:',
259 ' api.php?action=query&prop=revisions&titles=Main%20Page&rvlimit=5&rvprop=timestamp|user|comment&rvdir=newer&rvstart=20060501000000'
260 );
261 }
262
263 public function getVersion() {
264 return __CLASS__ . ': $Id$';
265 }
266 }
267 ?>