*
* API for MediaWiki 1.8+
*
- * Copyright (C) 2006 Yuri Astrakhan <FirstnameLastname@gmail.com>
+ * Copyright (C) 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
}
/**
+ * A query action to enumerate revisions of a given page, or show top revisions of multiple pages.
+ * Various pieces of information may be shown - flags, comments, and the actual wiki markup of the rev.
+ * In the enumeration mode, ranges of revisions may be requested and filtered.
+ *
* @addtogroup API
*/
class ApiQueryRevisions extends ApiQueryBase {
parent :: __construct($query, $moduleName, 'rv');
}
+ private $fld_ids = false, $fld_flags = false, $fld_timestamp = false,
+ $fld_comment = false, $fld_user = false, $fld_content = false;
+
public function execute() {
$limit = $startid = $endid = $start = $end = $dir = $prop = $user = $excludeuser = null;
extract($this->extractRequestParams());
$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');
$this->addTables('revision');
- $this->addFields(array (
- 'rev_id',
- 'rev_page',
- 'rev_text_id',
- 'rev_minor_edit'
- ));
$this->addWhere('rev_deleted=0');
- $showContent = false;
+ $prop = array_flip($prop);
- if (!is_null($prop)) {
- $prop = array_flip($prop);
- $this->addFieldsIf('rev_timestamp', isset ($prop['timestamp']));
- $this->addFieldsIf('rev_comment', isset ($prop['comment']));
- if (isset ($prop['user'])) {
- $this->addFields('rev_user');
- $this->addFields('rev_user_text');
- }
- if (isset ($prop['content'])) {
- $this->addTables('text');
- $this->addWhere('rev_text_id=old_id');
- $this->addFields('old_id');
- $this->addFields('old_text');
- $this->addFields('old_flags');
- $showContent = true;
+ // These field are needed regardless of the client requesting them
+ $this->addFields('rev_id');
+ $this->addFields('rev_page');
+
+ // Optional fields
+ $this->fld_ids = isset ($prop['ids']);
+ // $this->addFieldsIf('rev_text_id', $this->fld_ids); // should this be exposed?
+ $this->fld_flags = $this->addFieldsIf('rev_minor_edit', isset ($prop['flags']));
+ $this->fld_timestamp = $this->addFieldsIf('rev_timestamp', isset ($prop['timestamp']));
+ $this->fld_comment = $this->addFieldsIf('rev_comment', isset ($prop['comment']));
+
+ if (isset ($prop['user'])) {
+ $this->addFields('rev_user');
+ $this->addFields('rev_user_text');
+ $this->fld_user = true;
+ }
+ if (isset ($prop['content'])) {
+
+ // For each page we will request, the user must have read rights for that page
+ foreach ($pageSet->getGoodTitles() as $title) {
+ if( !$title->userCanRead() )
+ $this->dieUsage(
+ 'The current user is not allowed to read ' . $title->getPrefixedText(),
+ 'accessdenied');
}
+
+ $this->addTables('text');
+ $this->addWhere('rev_text_id=old_id');
+ $this->addFields('old_id');
+ $this->addFields('old_text');
+ $this->addFields('old_flags');
+ $this->fld_content = true;
}
- $userMax = ($showContent ? 50 : 500);
- $botMax = ($showContent ? 200 : 10000);
+ $userMax = ($this->fld_content ? 50 : 500);
+ $botMax = ($this->fld_content ? 200 : 10000);
if ($enumRevMode) {
// must manually initialize unset limit
if (is_null($limit))
$limit = 10;
- $this->validateLimit($this->encodeParamName('limit'), $limit, 1, $userMax, $botMax);
+ $this->validateLimit('limit', $limit, 1, $userMax, $botMax);
// There is only one ID, use it
$this->addWhereFld('rev_page', current(array_keys($pageSet->getGoodTitles())));
-
+
if(!is_null($user)) {
- $this->addWhere('rev_user_text', $user);
+ $this->addWhereFld('rev_user_text', $user);
} elseif (!is_null( $excludeuser)) {
$this->addWhere('rev_user_text != ' . $this->getDB()->addQuotes($excludeuser));
}
// Get all revision IDs
$this->addWhereFld('rev_id', array_keys($pageSet->getRevisionIDs()));
- $limit = $revCount; // assumption testing -- we should never get more then $revCount rows.
+ // assumption testing -- we should never get more then $revCount rows.
+ $limit = $revCount;
}
elseif ($pageCount > 0) {
// When working in multi-page non-enumeration mode,
// Get all page IDs
$this->addWhereFld('page_id', array_keys($pageSet->getGoodTitles()));
- $limit = $pageCount; // assumption testing -- we should never get more then $pageCount rows.
+ // assumption testing -- we should never get more then $pageCount rows.
+ $limit = $pageCount;
} else
ApiBase :: dieDebug(__METHOD__, 'param validation?');
// We've reached the one extra which shows that there are additional pages to be had. Stop here...
if (!$enumRevMode)
ApiBase :: dieDebug(__METHOD__, 'Got more rows then expected'); // bug report
- $this->setContinueEnumParameter('startid', $row->rev_id);
+ $this->setContinueEnumParameter('startid', intval($row->rev_id));
break;
}
- $vals = $this->addRowInfo('rev', $row);
- if ($vals) {
- if ($showContent)
- ApiResult :: setContent($vals, Revision :: getRevisionText($row));
-
- $this->getResult()->addValue(array (
+ $this->getResult()->addValue(
+ array (
'query',
'pages',
- intval($row->rev_page
- ), 'revisions'), intval($row->rev_id), $vals);
- }
+ intval($row->rev_page),
+ 'revisions'),
+ null,
+ $this->extractRowInfo($row));
}
$db->freeResult($res);
}
}
+ private function extractRowInfo($row) {
+
+ $vals = array ();
+
+ if ($this->fld_ids) {
+ $vals['revid'] = intval($row->rev_id);
+ $vals['pageid'] = intval($row->rev_page);
+ // $vals['oldid'] = intval($row->rev_text_id); // todo: should this be exposed?
+ }
+
+ if ($this->fld_flags && $row->rev_minor_edit)
+ $vals['minor'] = '';
+
+ if ($this->fld_user) {
+ $vals['user'] = $row->rev_user_text;
+ if (!$row->rev_user)
+ $vals['anon'] = '';
+ }
+
+ if ($this->fld_timestamp) {
+ $vals['timestamp'] = wfTimestamp(TS_ISO_8601, $row->rev_timestamp);
+ }
+
+ if ($this->fld_comment && !empty ($row->rev_comment)) {
+ $vals['comment'] = $row->rev_comment;
+ }
+
+ if ($this->fld_content) {
+ ApiResult :: setContent($vals, Revision :: getRevisionText($row));
+ }
+
+ return $vals;
+ }
+
protected function getAllowedParams() {
return array (
'prop' => array (
ApiBase :: PARAM_ISMULTI => true,
+ ApiBase :: PARAM_DFLT => 'ids|timestamp|flags|comment|user',
ApiBase :: PARAM_TYPE => array (
+ 'ids',
+ 'flags',
'timestamp',
'user',
'comment',
return __CLASS__ . ': $Id$';
}
}
-?>
+