]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - includes/api/ApiQueryRevisions.php
MediaWiki 1.15.3
[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  * @ingroup 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         protected function getTokenFunctions() {
48                 // tokenname => function
49                 // function prototype is func($pageid, $title, $rev)
50                 // should return token or false
51
52                 // Don't call the hooks twice
53                 if(isset($this->tokenFunctions))
54                         return $this->tokenFunctions;
55
56                 // If we're in JSON callback mode, no tokens can be obtained
57                 if(!is_null($this->getMain()->getRequest()->getVal('callback')))
58                         return array();
59
60                 $this->tokenFunctions = array(
61                         'rollback' => array( 'ApiQueryRevisions', 'getRollbackToken' )
62                 );
63                 wfRunHooks('APIQueryRevisionsTokens', array(&$this->tokenFunctions));
64                 return $this->tokenFunctions;
65         }
66
67         public static function getRollbackToken($pageid, $title, $rev)
68         {
69                 global $wgUser;
70                 if(!$wgUser->isAllowed('rollback'))
71                         return false;
72                 return $wgUser->editToken(array($title->getPrefixedText(),
73                                                 $rev->getUserText()));
74         }
75
76         public function execute() {
77                 $params = $this->extractRequestParams(false);
78
79                 // If any of those parameters are used, work in 'enumeration' mode.
80                 // Enum mode can only be used when exactly one page is provided.
81                 // Enumerating revisions on multiple pages make it extremely
82                 // difficult to manage continuations and require additional SQL indexes
83                 $enumRevMode = (!is_null($params['user']) || !is_null($params['excludeuser']) ||
84                                 !is_null($params['limit']) || !is_null($params['startid']) ||
85                                 !is_null($params['endid']) || $params['dir'] === 'newer' ||
86                                 !is_null($params['start']) || !is_null($params['end']));
87
88
89                 $pageSet = $this->getPageSet();
90                 $pageCount = $pageSet->getGoodTitleCount();
91                 $revCount = $pageSet->getRevisionCount();
92
93                 // Optimization -- nothing to do
94                 if ($revCount === 0 && $pageCount === 0)
95                         return;
96
97                 if ($revCount > 0 && $enumRevMode)
98                         $this->dieUsage('The revids= parameter may not be used with the list options (limit, startid, endid, dirNewer, start, end).', 'revids');
99
100                 if ($pageCount > 1 && $enumRevMode)
101                         $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');
102
103                 if (!is_null($params['diffto'])) {
104                         if ($params['diffto'] == 'cur')
105                                 $params['diffto'] = 0;
106                         if ((!ctype_digit($params['diffto']) || $params['diffto'] < 0) 
107                                         && $params['diffto'] != 'prev' && $params['diffto'] != 'next')
108                                 $this->dieUsage('rvdiffto must be set to a non-negative number, "prev", "next" or "cur"', 'diffto');
109                         // Check whether the revision exists and is readable,
110                         // DifferenceEngine returns a rather ambiguous empty
111                         // string if that's not the case
112                         if ($params['diffto'] != 0) {
113                                 $difftoRev = Revision::newFromID($params['diffto']);
114                                 if (!$difftoRev)
115                                         $this->dieUsageMsg(array('nosuchrevid', $params['diffto']));
116                                 if (!$difftoRev->userCan(Revision::DELETED_TEXT)) {
117                                         $this->setWarning("Couldn't diff to r{$difftoRev->getID()}: content is hidden");
118                                         $params['diffto'] = null;
119                                 }
120                         }
121                 }
122
123                 $this->addTables('revision');
124                 $this->addFields(Revision::selectFields());
125                 $this->addTables('page');
126                 $this->addWhere('page_id = rev_page');
127
128                 $prop = array_flip($params['prop']);
129
130                 // Optional fields
131                 $this->fld_ids = isset ($prop['ids']);
132                 // $this->addFieldsIf('rev_text_id', $this->fld_ids); // should this be exposed?
133                 $this->fld_flags = isset ($prop['flags']);
134                 $this->fld_timestamp = isset ($prop['timestamp']);
135                 $this->fld_comment = isset ($prop['comment']);
136                 $this->fld_size = isset ($prop['size']);
137                 $this->fld_user = isset ($prop['user']);
138                 $this->token = $params['token'];
139                 $this->diffto = $params['diffto'];
140
141                 if ( !is_null($this->token) || $pageCount > 0) {
142                         $this->addFields( Revision::selectPageFields() );
143                 }
144
145                 if (isset ($prop['content'])) {
146
147                         // For each page we will request, the user must have read rights for that page
148                         foreach ($pageSet->getGoodTitles() as $title) {
149                                 if( !$title->userCanRead() )
150                                         $this->dieUsage(
151                                                 'The current user is not allowed to read ' . $title->getPrefixedText(),
152                                                 'accessdenied');
153                         }
154
155                         $this->addTables('text');
156                         $this->addWhere('rev_text_id=old_id');
157                         $this->addFields('old_id');
158                         $this->addFields(Revision::selectTextFields());
159
160                         $this->fld_content = true;
161
162                         $this->expandTemplates = $params['expandtemplates'];
163                         $this->generateXML = $params['generatexml'];
164                         if(isset($params['section']))
165                                 $this->section = $params['section'];
166                         else
167                                 $this->section = false;
168                 }
169
170                 $userMax = ( $this->fld_content ? ApiBase::LIMIT_SML1 : ApiBase::LIMIT_BIG1 );
171                 $botMax  = ( $this->fld_content ? ApiBase::LIMIT_SML2 : ApiBase::LIMIT_BIG2 );
172                 $limit = $params['limit'];
173                 if( $limit == 'max' ) {
174                         $limit = $this->getMain()->canApiHighLimits() ? $botMax : $userMax;
175                         $this->getResult()->addValue( 'limits', $this->getModuleName(), $limit );
176                 }
177
178                 if ($enumRevMode) {
179
180                         // This is mostly to prevent parameter errors (and optimize SQL?)
181                         if (!is_null($params['startid']) && !is_null($params['start']))
182                                 $this->dieUsage('start and startid cannot be used together', 'badparams');
183
184                         if (!is_null($params['endid']) && !is_null($params['end']))
185                                 $this->dieUsage('end and endid cannot be used together', 'badparams');
186
187                         if(!is_null($params['user']) && !is_null($params['excludeuser']))
188                                 $this->dieUsage('user and excludeuser cannot be used together', 'badparams');
189
190                         // This code makes an assumption that sorting by rev_id and rev_timestamp produces
191                         // the same result. This way users may request revisions starting at a given time,
192                         // but to page through results use the rev_id returned after each page.
193                         // Switching to rev_id removes the potential problem of having more than
194                         // one row with the same timestamp for the same page.
195                         // The order needs to be the same as start parameter to avoid SQL filesort.
196
197                         if (is_null($params['startid']) && is_null($params['endid']))
198                                 $this->addWhereRange('rev_timestamp', $params['dir'],
199                                         $params['start'], $params['end']);
200                         else {
201                                 $this->addWhereRange('rev_id', $params['dir'],
202                                         $params['startid'], $params['endid']);
203                                 // One of start and end can be set
204                                 // If neither is set, this does nothing
205                                 $this->addWhereRange('rev_timestamp', $params['dir'],
206                                         $params['start'], $params['end'], false);
207                         }
208
209                         // must manually initialize unset limit
210                         if (is_null($limit))
211                                 $limit = 10;
212                         $this->validateLimit('limit', $limit, 1, $userMax, $botMax);
213
214                         // There is only one ID, use it
215                         $this->addWhereFld('rev_page', reset(array_keys($pageSet->getGoodTitles())));
216
217                         if(!is_null($params['user'])) {
218                                 $this->addWhereFld('rev_user_text', $params['user']);
219                         } elseif (!is_null($params['excludeuser'])) {
220                                 $this->addWhere('rev_user_text != ' .
221                                         $this->getDB()->addQuotes($params['excludeuser']));
222                         }
223                         if(!is_null($params['user']) || !is_null($params['excludeuser'])) {
224                                 // Paranoia: avoid brute force searches (bug 17342)
225                                 $this->addWhere('rev_deleted & ' . Revision::DELETED_USER . ' = 0');
226                         }
227                 }
228                 elseif ($revCount > 0) {
229                         $max = $this->getMain()->canApiHighLimits() ? $botMax : $userMax;
230                         $revs = $pageSet->getRevisionIDs();
231                         if(self::truncateArray($revs, $max))
232                                 $this->setWarning("Too many values supplied for parameter 'revids': the limit is $max"); 
233
234                         // Get all revision IDs
235                         $this->addWhereFld('rev_id', array_keys($revs));
236
237                         if(!is_null($params['continue']))
238                                 $this->addWhere("rev_id >= '" . intval($params['continue']) . "'");
239                         $this->addOption('ORDER BY', 'rev_id');
240
241                         // assumption testing -- we should never get more then $revCount rows.
242                         $limit = $revCount;
243                 }
244                 elseif ($pageCount > 0) {
245                         $max = $this->getMain()->canApiHighLimits() ? $botMax : $userMax;
246                         $titles = $pageSet->getGoodTitles();
247                         if(self::truncateArray($titles, $max))
248                                 $this->setWarning("Too many values supplied for parameter 'titles': the limit is $max");
249                         
250                         // When working in multi-page non-enumeration mode,
251                         // limit to the latest revision only
252                         $this->addWhere('page_id=rev_page');
253                         $this->addWhere('page_latest=rev_id');
254                         
255                         // Get all page IDs
256                         $this->addWhereFld('page_id', array_keys($titles));
257                         // Every time someone relies on equality propagation, god kills a kitten :)
258                         $this->addWhereFld('rev_page', array_keys($titles));
259                         
260                         if(!is_null($params['continue']))
261                         {
262                                 $cont = explode('|', $params['continue']);
263                                 if(count($cont) != 2)
264                                         $this->dieUsage("Invalid continue param. You should pass the original " .
265                                                         "value returned by the previous query", "_badcontinue");
266                                 $pageid = intval($cont[0]);
267                                 $revid = intval($cont[1]);
268                                 $this->addWhere("rev_page > '$pageid' OR " .
269                                                 "(rev_page = '$pageid' AND " .
270                                                 "rev_id >= '$revid')");
271                         }
272                         $this->addOption('ORDER BY', 'rev_page, rev_id');
273
274                         // assumption testing -- we should never get more then $pageCount rows.
275                         $limit = $pageCount;
276                 } else
277                         ApiBase :: dieDebug(__METHOD__, 'param validation?');
278
279                 $this->addOption('LIMIT', $limit +1);
280
281                 $data = array ();
282                 $count = 0;
283                 $res = $this->select(__METHOD__);
284
285                 $db = $this->getDB();
286                 while ($row = $db->fetchObject($res)) {
287
288                         if (++ $count > $limit) {
289                                 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
290                                 if (!$enumRevMode)
291                                         ApiBase :: dieDebug(__METHOD__, 'Got more rows then expected'); // bug report
292                                 $this->setContinueEnumParameter('startid', intval($row->rev_id));
293                                 break;
294                         }
295                         $revision = new Revision( $row );
296                         //
297                         $fit = $this->addPageSubItem($revision->getPage(), $this->extractRowInfo($revision), 'rev');
298                         if(!$fit)
299                         {
300                                 if($enumRevMode)
301                                         $this->setContinueEnumParameter('startid', intval($row->rev_id));
302                                 else if($revCount > 0)
303                                         $this->setContinueEnumParameter('continue', intval($row->rev_id));
304                                 else
305                                         $this->setContinueEnumParameter('continue', intval($row->rev_page) .
306                                                 '|' . intval($row->rev_id));
307                                 break;
308                         }
309                 }
310                 $db->freeResult($res);
311         }
312
313         private function extractRowInfo( $revision ) {
314                 $title = $revision->getTitle();
315                 $vals = array ();
316
317                 if ($this->fld_ids) {
318                         $vals['revid'] = intval($revision->getId());
319                         // $vals['oldid'] = intval($row->rev_text_id);  // todo: should this be exposed?
320                 }
321
322                 if ($this->fld_flags && $revision->isMinor())
323                         $vals['minor'] = '';
324
325                 if ($this->fld_user) {
326                         if ($revision->isDeleted(Revision::DELETED_USER)) {
327                                 $vals['userhidden'] = '';
328                         } else {
329                                 $vals['user'] = $revision->getUserText();
330                                 if (!$revision->getUser())
331                                         $vals['anon'] = '';
332                         }
333                 }
334
335                 if ($this->fld_timestamp) {
336                         $vals['timestamp'] = wfTimestamp(TS_ISO_8601, $revision->getTimestamp());
337                 }
338
339                 if ($this->fld_size && !is_null($revision->getSize())) {
340                         $vals['size'] = intval($revision->getSize());
341                 }
342
343                 if ($this->fld_comment) {
344                         if ($revision->isDeleted(Revision::DELETED_COMMENT)) {
345                                 $vals['commenthidden'] = '';
346                         } else {
347                                 $comment = $revision->getComment();
348                                 if (strval($comment) !== '')
349                                         $vals['comment'] = $comment;
350                         }
351                 }       
352
353                 if(!is_null($this->token))
354                 {
355                         $tokenFunctions = $this->getTokenFunctions();
356                         foreach($this->token as $t)
357                         {
358                                 $val = call_user_func($tokenFunctions[$t], $title->getArticleID(), $title, $revision);
359                                 if($val === false)
360                                         $this->setWarning("Action '$t' is not allowed for the current user");
361                                 else
362                                         $vals[$t . 'token'] = $val;
363                         }
364                 }
365                 
366                 if ($this->fld_content && !$revision->isDeleted(Revision::DELETED_TEXT)) {
367                         global $wgParser;
368                         $text = $revision->getText();
369                         # Expand templates after getting section content because
370                         # template-added sections don't count and Parser::preprocess()
371                         # will have less input
372                         if ($this->section !== false) {
373                                 $text = $wgParser->getSection( $text, $this->section, false);
374                                 if($text === false)
375                                         $this->dieUsage("There is no section {$this->section} in r".$revision->getId(), 'nosuchsection');
376                         }
377                         if ($this->generateXML) {
378                                 $wgParser->startExternalParse( $title, new ParserOptions(), OT_PREPROCESS );
379                                 $dom = $wgParser->preprocessToDom( $text );
380                                 if ( is_callable( array( $dom, 'saveXML' ) ) ) {
381                                         $xml = $dom->saveXML();
382                                 } else {
383                                         $xml = $dom->__toString();
384                                 }
385                                 $vals['parsetree'] = $xml;
386                                 
387                         }
388                         if ($this->expandTemplates) {
389                                 $text = $wgParser->preprocess( $text, $title, new ParserOptions() );
390                         }
391                         ApiResult :: setContent($vals, $text);
392                 } else if ($this->fld_content) {
393                         $vals['texthidden'] = '';
394                 }
395
396                 if (!is_null($this->diffto)) {
397                         global $wgAPIMaxUncachedDiffs;
398                         static $n = 0; // Numer of uncached diffs we've had
399                         if($n< $wgAPIMaxUncachedDiffs) {
400                                 $engine = new DifferenceEngine($title, $revision->getID(), $this->diffto);
401                                 $difftext = $engine->getDiffBody();
402                                 $vals['diff']['from'] = $engine->getOldid();
403                                 $vals['diff']['to'] = $engine->getNewid();
404                                 ApiResult::setContent($vals['diff'], $difftext);
405                                 if(!$engine->wasCacheHit())
406                                         $n++;
407                         } else {
408                                 $vals['diff']['notcached'] = '';
409                         }
410                 }
411                 return $vals;
412         }
413
414         public function getAllowedParams() {
415                 return array (
416                         'prop' => array (
417                                 ApiBase :: PARAM_ISMULTI => true,
418                                 ApiBase :: PARAM_DFLT => 'ids|timestamp|flags|comment|user',
419                                 ApiBase :: PARAM_TYPE => array (
420                                         'ids',
421                                         'flags',
422                                         'timestamp',
423                                         'user',
424                                         'size',
425                                         'comment',
426                                         'content',
427                                 )
428                         ),
429                         'limit' => array (
430                                 ApiBase :: PARAM_TYPE => 'limit',
431                                 ApiBase :: PARAM_MIN => 1,
432                                 ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1,
433                                 ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
434                         ),
435                         'startid' => array (
436                                 ApiBase :: PARAM_TYPE => 'integer'
437                         ),
438                         'endid' => array (
439                                 ApiBase :: PARAM_TYPE => 'integer'
440                         ),
441                         'start' => array (
442                                 ApiBase :: PARAM_TYPE => 'timestamp'
443                         ),
444                         'end' => array (
445                                 ApiBase :: PARAM_TYPE => 'timestamp'
446                         ),
447                         'dir' => array (
448                                 ApiBase :: PARAM_DFLT => 'older',
449                                 ApiBase :: PARAM_TYPE => array (
450                                         'newer',
451                                         'older'
452                                 )
453                         ),
454                         'user' => array(
455                                 ApiBase :: PARAM_TYPE => 'user'
456                         ),
457                         'excludeuser' => array(
458                                 ApiBase :: PARAM_TYPE => 'user'
459                         ),
460                         'expandtemplates' => false,
461                         'generatexml' => false,
462                         'section' => null,
463                         'token' => array(
464                                 ApiBase :: PARAM_TYPE => array_keys($this->getTokenFunctions()),
465                                 ApiBase :: PARAM_ISMULTI => true
466                         ),
467                         'continue' => null,
468                         'diffto' => null,
469                 );
470         }
471
472         public function getParamDescription() {
473                 return array (
474                         'prop' => 'Which properties to get for each revision.',
475                         'limit' => 'limit how many revisions will be returned (enum)',
476                         'startid' => 'from which revision id to start enumeration (enum)',
477                         'endid' => 'stop revision enumeration on this revid (enum)',
478                         'start' => 'from which revision timestamp to start enumeration (enum)',
479                         'end' => 'enumerate up to this timestamp (enum)',
480                         'dir' => 'direction of enumeration - towards "newer" or "older" revisions (enum)',
481                         'user' => 'only include revisions made by user',
482                         'excludeuser' => 'exclude revisions made by user',
483                         'expandtemplates' => 'expand templates in revision content',
484                         'generatexml' => 'generate XML parse tree for revision content',
485                         'section' => 'only retrieve the content of this section',
486                         'token' => 'Which tokens to obtain for each revision',
487                         'continue' => 'When more results are available, use this to continue',
488                         'diffto' => array('Revision ID to diff each revision to.',
489                                 'Use "prev", "next" and "cur" for the previous, next and current revision respectively.'),
490                 );
491         }
492
493         public function getDescription() {
494                 return array (
495                         'Get revision information.',
496                         'This module may be used in several ways:',
497                         ' 1) Get data about a set of pages (last revision), by setting titles or pageids parameter.',
498                         ' 2) Get revisions for one given page, by using titles/pageids with start/end/limit params.',
499                         ' 3) Get data about a set of revisions by setting their IDs with revids parameter.',
500                         'All parameters marked as (enum) may only be used with a single page (#2).'
501                 );
502         }
503
504         protected function getExamples() {
505                 return array (
506                         'Get data with content for the last revision of titles "API" and "Main Page":',
507                         '  api.php?action=query&prop=revisions&titles=API|Main%20Page&rvprop=timestamp|user|comment|content',
508                         'Get last 5 revisions of the "Main Page":',
509                         '  api.php?action=query&prop=revisions&titles=Main%20Page&rvlimit=5&rvprop=timestamp|user|comment',
510                         'Get first 5 revisions of the "Main Page":',
511                         '  api.php?action=query&prop=revisions&titles=Main%20Page&rvlimit=5&rvprop=timestamp|user|comment&rvdir=newer',
512                         'Get first 5 revisions of the "Main Page" made after 2006-05-01:',
513                         '  api.php?action=query&prop=revisions&titles=Main%20Page&rvlimit=5&rvprop=timestamp|user|comment&rvdir=newer&rvstart=20060501000000',
514                         'Get first 5 revisions of the "Main Page" that were not made made by anonymous user "127.0.0.1"',
515                         '  api.php?action=query&prop=revisions&titles=Main%20Page&rvlimit=5&rvprop=timestamp|user|comment&rvexcludeuser=127.0.0.1',
516                         'Get first 5 revisions of the "Main Page" that were made by the user "MediaWiki default"',
517                         '  api.php?action=query&prop=revisions&titles=Main%20Page&rvlimit=5&rvprop=timestamp|user|comment&rvuser=MediaWiki%20default',
518                 );
519         }
520
521         public function getVersion() {
522                 return __CLASS__ . ': $Id: ApiQueryRevisions.php 48642 2009-03-20 20:21:38Z midom $';
523         }
524 }