ApiQueryRevisions.php 19 KB

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