4 * Created on Oct 19, 2006
6 * API for MediaWiki 1.8+
8 * Copyright (C) 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
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.
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.
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
26 if (!defined('MEDIAWIKI')) {
27 // Eclipse helper - will be ignored in production
28 require_once ('ApiQueryBase.php');
32 * A query action to enumerate the recent changes that were done to the wiki.
33 * Various filters are supported.
37 class ApiQueryRecentChanges
extends ApiQueryBase
{
39 public function __construct($query, $moduleName) {
40 parent
:: __construct($query, $moduleName, 'rc');
43 private $fld_comment = false, $fld_user = false, $fld_flags = false,
44 $fld_timestamp = false, $fld_title = false, $fld_ids = false,
48 * Generates and outputs the result of this query based upon the provided parameters.
50 public function execute() {
52 $limit = $prop = $namespace = $show = $type = $dir = $start = $end = null;
54 /* Get the parameters of the request. */
55 extract($this->extractRequestParams());
57 /* Build our basic query. Namely, something along the lines of:
58 * SELECT * from recentchanges WHERE rc_timestamp > $start
59 * AND rc_timestamp < $end AND rc_namespace = $namespace
60 * AND rc_deleted = '0'
62 $this->addTables('recentchanges');
63 $this->addWhereRange('rc_timestamp', $dir, $start, $end);
64 $this->addWhereFld('rc_namespace', $namespace);
65 $this->addWhereFld('rc_deleted', 0);
67 $this->addWhereFld('rc_type', $this->parseRCType($type));
69 if (!is_null($show)) {
70 $show = array_flip($show);
72 /* Check for conflicting parameters. */
73 if ((isset ($show['minor']) && isset ($show['!minor']))
74 ||
(isset ($show['bot']) && isset ($show['!bot']))
75 ||
(isset ($show['anon']) && isset ($show['!anon']))) {
77 $this->dieUsage("Incorrect parameter - mutually exclusive values may not be supplied", 'show');
80 /* Add additional conditions to query depending upon parameters. */
81 $this->addWhereIf('rc_minor = 0', isset ($show['!minor']));
82 $this->addWhereIf('rc_minor != 0', isset ($show['minor']));
83 $this->addWhereIf('rc_bot = 0', isset ($show['!bot']));
84 $this->addWhereIf('rc_bot != 0', isset ($show['bot']));
85 $this->addWhereIf('rc_user = 0', isset ($show['anon']));
86 $this->addWhereIf('rc_user != 0', isset ($show['!anon']));
89 /* Add the fields we're concerned with to out query. */
90 $this->addFields(array (
99 /* Determine what properties we need to display. */
100 if (!is_null($prop)) {
101 $prop = array_flip($prop);
103 /* Set up internal members based upon params. */
104 $this->fld_comment
= isset ($prop['comment']);
105 $this->fld_user
= isset ($prop['user']);
106 $this->fld_flags
= isset ($prop['flags']);
107 $this->fld_timestamp
= isset ($prop['timestamp']);
108 $this->fld_title
= isset ($prop['title']);
109 $this->fld_ids
= isset ($prop['ids']);
110 $this->fld_sizes
= isset ($prop['sizes']);
112 /* Add fields to our query if they are specified as a needed parameter. */
113 $this->addFieldsIf('rc_id', $this->fld_ids
);
114 $this->addFieldsIf('rc_cur_id', $this->fld_ids
);
115 $this->addFieldsIf('rc_this_oldid', $this->fld_ids
);
116 $this->addFieldsIf('rc_last_oldid', $this->fld_ids
);
117 $this->addFieldsIf('rc_comment', $this->fld_comment
);
118 $this->addFieldsIf('rc_user', $this->fld_user
);
119 $this->addFieldsIf('rc_user_text', $this->fld_user
);
120 $this->addFieldsIf('rc_minor', $this->fld_flags
);
121 $this->addFieldsIf('rc_bot', $this->fld_flags
);
122 $this->addFieldsIf('rc_new', $this->fld_flags
);
123 $this->addFieldsIf('rc_old_len', $this->fld_sizes
);
124 $this->addFieldsIf('rc_new_len', $this->fld_sizes
);
127 /* Specify the limit for our query. It's $limit+1 because we (possibly) need to
128 * generate a "continue" parameter, to allow paging. */
129 $this->addOption('LIMIT', $limit +
1);
131 /* Specify the index to use in the query as rc_timestamp, instead of rc_revid (default). */
132 $this->addOption('USE INDEX', 'rc_timestamp');
137 /* Perform the actual query. */
138 $db = $this->getDB();
139 $res = $this->select(__METHOD__
);
141 /* Iterate through the rows, adding data extracted from them to our query result. */
142 while ($row = $db->fetchObject($res)) {
143 if (++
$count > $limit) {
144 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
145 $this->setContinueEnumParameter('start', wfTimestamp(TS_ISO_8601
, $row->rc_timestamp
));
149 /* Extract the data from a single row. */
150 $vals = $this->extractRowInfo($row);
152 /* Add that row's data to our final output. */
157 $db->freeResult($res);
159 /* Format the result */
160 $result = $this->getResult();
161 $result->setIndexedTagName($data, 'rc');
162 $result->addValue('query', $this->getModuleName(), $data);
166 * Extracts from a single sql row the data needed to describe one recent change.
168 * @param $row The row from which to extract the data.
169 * @return An array mapping strings (descriptors) to their respective string values.
172 private function extractRowInfo($row) {
173 /* If page was moved somewhere, get the title of the move target. */
174 $movedToTitle = false;
175 if (!empty($row->rc_moved_to_title
))
176 $movedToTitle = Title
:: makeTitle($row->rc_moved_to_ns
, $row->rc_moved_to_title
);
178 /* Determine the title of the page that has been changed. */
179 $title = Title
:: makeTitle($row->rc_namespace
, $row->rc_title
);
181 /* Our output data. */
184 $type = intval ( $row->rc_type
);
186 /* Determine what kind of change this was. */
188 case RC_EDIT
: $vals['type'] = 'edit'; break;
189 case RC_NEW
: $vals['type'] = 'new'; break;
190 case RC_MOVE
: $vals['type'] = 'move'; break;
191 case RC_LOG
: $vals['type'] = 'log'; break;
192 case RC_MOVE_OVER_REDIRECT
: $vals['type'] = 'move over redirect'; break;
193 default: $vals['type'] = $type;
196 /* Create a new entry in the result for the title. */
197 if ($this->fld_title
) {
198 ApiQueryBase
:: addTitleInfo($vals, $title);
200 ApiQueryBase
:: addTitleInfo($vals, $movedToTitle, "new_");
203 /* Add ids, such as rcid, pageid, revid, and oldid to the change's info. */
204 if ($this->fld_ids
) {
205 $vals['rcid'] = intval($row->rc_id
);
206 $vals['pageid'] = intval($row->rc_cur_id
);
207 $vals['revid'] = intval($row->rc_this_oldid
);
208 $vals['old_revid'] = intval( $row->rc_last_oldid
);
211 /* Add user data and 'anon' flag, if use is anonymous. */
212 if ($this->fld_user
) {
213 $vals['user'] = $row->rc_user_text
;
218 /* Add flags, such as new, minor, bot. */
219 if ($this->fld_flags
) {
228 /* Add sizes of each revision. (Only available on 1.10+) */
229 if ($this->fld_sizes
) {
230 $vals['oldlen'] = intval($row->rc_old_len
);
231 $vals['newlen'] = intval($row->rc_new_len
);
234 /* Add the timestamp. */
235 if ($this->fld_timestamp
)
236 $vals['timestamp'] = wfTimestamp(TS_ISO_8601
, $row->rc_timestamp
);
238 /* Add edit summary / log summary. */
239 if ($this->fld_comment
&& !empty ($row->rc_comment
)) {
240 $vals['comment'] = $row->rc_comment
;
246 private function parseRCType($type)
252 $retval[] = $this->parseRCType($t);
257 case 'edit': return RC_EDIT
;
258 case 'new': return RC_NEW
;
259 case 'log': return RC_LOG
;
263 protected function getAllowedParams() {
266 ApiBase
:: PARAM_TYPE
=> 'timestamp'
269 ApiBase
:: PARAM_TYPE
=> 'timestamp'
272 ApiBase
:: PARAM_DFLT
=> 'older',
273 ApiBase
:: PARAM_TYPE
=> array (
278 'namespace' => array (
279 ApiBase
:: PARAM_ISMULTI
=> true,
280 ApiBase
:: PARAM_TYPE
=> 'namespace'
283 ApiBase
:: PARAM_ISMULTI
=> true,
284 ApiBase
:: PARAM_DFLT
=> 'title|timestamp|ids',
285 ApiBase
:: PARAM_TYPE
=> array (
296 ApiBase
:: PARAM_ISMULTI
=> true,
297 ApiBase
:: PARAM_TYPE
=> array (
307 ApiBase
:: PARAM_DFLT
=> 10,
308 ApiBase
:: PARAM_TYPE
=> 'limit',
309 ApiBase
:: PARAM_MIN
=> 1,
310 ApiBase
:: PARAM_MAX
=> ApiBase
:: LIMIT_BIG1
,
311 ApiBase
:: PARAM_MAX2
=> ApiBase
:: LIMIT_BIG2
314 ApiBase
:: PARAM_ISMULTI
=> true,
315 ApiBase
:: PARAM_TYPE
=> array (
324 protected function getParamDescription() {
326 'start' => 'The timestamp to start enumerating from.',
327 'end' => 'The timestamp to end enumerating.',
328 'dir' => 'In which direction to enumerate.',
329 'namespace' => 'Filter log entries to only this namespace(s)',
330 'prop' => 'Include additional pieces of information',
332 'Show only items that meet this criteria.',
333 'For example, to see only minor edits done by logged-in users, set show=minor|!anon'
335 'type' => 'Which types of changes to show.',
336 'limit' => 'How many total pages to return.'
340 protected function getDescription() {
341 return 'Enumerate recent changes';
344 protected function getExamples() {
346 'api.php?action=query&list=recentchanges'
350 public function getVersion() {
351 return __CLASS__
. ': $Id$';