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