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 = $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 if (!is_null($show)) {
68 $show = array_flip($show);
70 /* Check for conflicting parameters. */
71 if ((isset ($show['minor']) && isset ($show['!minor']))
72 ||
(isset ($show['bot']) && isset ($show['!bot']))
73 ||
(isset ($show['anon']) && isset ($show['!anon']))) {
75 $this->dieUsage("Incorrect parameter - mutually exclusive values may not be supplied", 'show');
78 /* Add additional conditions to query depending upon parameters. */
79 $this->addWhereIf('rc_minor = 0', isset ($show['!minor']));
80 $this->addWhereIf('rc_minor != 0', isset ($show['minor']));
81 $this->addWhereIf('rc_bot = 0', isset ($show['!bot']));
82 $this->addWhereIf('rc_bot != 0', isset ($show['bot']));
83 $this->addWhereIf('rc_user = 0', isset ($show['anon']));
84 $this->addWhereIf('rc_user != 0', isset ($show['!anon']));
87 /* Add the fields we're concerned with to out query. */
88 $this->addFields(array (
97 /* Determine what properties we need to display. */
98 if (!is_null($prop)) {
99 $prop = array_flip($prop);
101 /* Set up internal members based upon params. */
102 $this->fld_comment
= isset ($prop['comment']);
103 $this->fld_user
= isset ($prop['user']);
104 $this->fld_flags
= isset ($prop['flags']);
105 $this->fld_timestamp
= isset ($prop['timestamp']);
106 $this->fld_title
= isset ($prop['title']);
107 $this->fld_ids
= isset ($prop['ids']);
108 $this->fld_sizes
= isset ($prop['sizes']);
110 /* Add fields to our query if they are specified as a needed parameter. */
111 $this->addFieldsIf('rc_id', $this->fld_ids
);
112 $this->addFieldsIf('rc_cur_id', $this->fld_ids
);
113 $this->addFieldsIf('rc_this_oldid', $this->fld_ids
);
114 $this->addFieldsIf('rc_last_oldid', $this->fld_ids
);
115 $this->addFieldsIf('rc_comment', $this->fld_comment
);
116 $this->addFieldsIf('rc_user', $this->fld_user
);
117 $this->addFieldsIf('rc_user_text', $this->fld_user
);
118 $this->addFieldsIf('rc_minor', $this->fld_flags
);
119 $this->addFieldsIf('rc_bot', $this->fld_flags
);
120 $this->addFieldsIf('rc_new', $this->fld_flags
);
121 $this->addFieldsIf('rc_old_len', $this->fld_sizes
);
122 $this->addFieldsIf('rc_new_len', $this->fld_sizes
);
125 /* Specify the limit for our query. It's $limit+1 because we (possibly) need to
126 * generate a "continue" parameter, to allow paging. */
127 $this->addOption('LIMIT', $limit +
1);
129 /* Specify the index to use in the query as rc_timestamp, instead of rc_revid (default). */
130 $this->addOption('USE INDEX', 'rc_timestamp');
135 /* Perform the actual query. */
136 $db = $this->getDB();
137 $res = $this->select(__METHOD__
);
139 /* Iterate through the rows, adding data extracted from them to our query result. */
140 while ($row = $db->fetchObject($res)) {
141 if (++
$count > $limit) {
142 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
143 $this->setContinueEnumParameter('start', wfTimestamp(TS_ISO_8601
, $row->rc_timestamp
));
147 /* Extract the data from a single row. */
148 $vals = $this->extractRowInfo($row);
150 /* Add that row's data to our final output. */
155 $db->freeResult($res);
157 /* Format the result */
158 $result = $this->getResult();
159 $result->setIndexedTagName($data, 'rc');
160 $result->addValue('query', $this->getModuleName(), $data);
164 * Extracts from a single sql row the data needed to describe one recent change.
166 * @param $row The row from which to extract the data.
167 * @return An array mapping strings (descriptors) to their respective string values.
170 private function extractRowInfo($row) {
171 /* If page was moved somewhere, get the title of the move target. */
172 $movedToTitle = false;
173 if (!empty($row->rc_moved_to_title
))
174 $movedToTitle = Title
:: makeTitle($row->rc_moved_to_ns
, $row->rc_moved_to_title
);
176 /* Determine the title of the page that has been changed. */
177 $title = Title
:: makeTitle($row->rc_namespace
, $row->rc_title
);
179 /* Our output data. */
182 $type = intval ( $row->rc_type
);
184 /* Determine what kind of change this was. */
186 case RC_EDIT
: $vals['type'] = 'edit'; break;
187 case RC_NEW
: $vals['type'] = 'new'; break;
188 case RC_MOVE
: $vals['type'] = 'move'; break;
189 case RC_LOG
: $vals['type'] = 'log'; break;
190 case RC_MOVE_OVER_REDIRECT
: $vals['type'] = 'move over redirect'; break;
191 default: $vals['type'] = $type;
194 /* Create a new entry in the result for the title. */
195 if ($this->fld_title
) {
196 ApiQueryBase
:: addTitleInfo($vals, $title);
198 ApiQueryBase
:: addTitleInfo($vals, $movedToTitle, "new_");
201 /* Add ids, such as rcid, pageid, revid, and oldid to the change's info. */
202 if ($this->fld_ids
) {
203 $vals['rcid'] = intval($row->rc_id
);
204 $vals['pageid'] = intval($row->rc_cur_id
);
205 $vals['revid'] = intval($row->rc_this_oldid
);
206 $vals['old_revid'] = intval( $row->rc_last_oldid
);
209 /* Add user data and 'anon' flag, if use is anonymous. */
210 if ($this->fld_user
) {
211 $vals['user'] = $row->rc_user_text
;
216 /* Add flags, such as new, minor, bot. */
217 if ($this->fld_flags
) {
226 /* Add sizes of each revision. (Only available on 1.10+) */
227 if ($this->fld_sizes
) {
228 $vals['oldlen'] = intval($row->rc_old_len
);
229 $vals['newlen'] = intval($row->rc_new_len
);
232 /* Add the timestamp. */
233 if ($this->fld_timestamp
)
234 $vals['timestamp'] = wfTimestamp(TS_ISO_8601
, $row->rc_timestamp
);
236 /* Add edit summary / log summary. */
237 if ($this->fld_comment
&& !empty ($row->rc_comment
)) {
238 $vals['comment'] = $row->rc_comment
;
244 protected function getAllowedParams() {
247 ApiBase
:: PARAM_TYPE
=> 'timestamp'
250 ApiBase
:: PARAM_TYPE
=> 'timestamp'
253 ApiBase
:: PARAM_DFLT
=> 'older',
254 ApiBase
:: PARAM_TYPE
=> array (
259 'namespace' => array (
260 ApiBase
:: PARAM_ISMULTI
=> true,
261 ApiBase
:: PARAM_TYPE
=> 'namespace'
264 ApiBase
:: PARAM_ISMULTI
=> true,
265 ApiBase
:: PARAM_DFLT
=> 'title|timestamp|ids',
266 ApiBase
:: PARAM_TYPE
=> array (
277 ApiBase
:: PARAM_ISMULTI
=> true,
278 ApiBase
:: PARAM_TYPE
=> array (
288 ApiBase
:: PARAM_DFLT
=> 10,
289 ApiBase
:: PARAM_TYPE
=> 'limit',
290 ApiBase
:: PARAM_MIN
=> 1,
291 ApiBase
:: PARAM_MAX
=> ApiBase
:: LIMIT_BIG1
,
292 ApiBase
:: PARAM_MAX2
=> ApiBase
:: LIMIT_BIG2
297 protected function getParamDescription() {
299 'start' => 'The timestamp to start enumerating from.',
300 'end' => 'The timestamp to end enumerating.',
301 'dir' => 'In which direction to enumerate.',
302 'namespace' => 'Filter log entries to only this namespace(s)',
303 'prop' => 'Include additional pieces of information',
305 'Show only items that meet this criteria.',
306 'For example, to see only minor edits done by logged-in users, set show=minor|!anon'
308 'limit' => 'How many total pages to return.'
312 protected function getDescription() {
313 return 'Enumerate recent changes';
316 protected function getExamples() {
318 'api.php?action=query&list=recentchanges'
322 public function getVersion() {
323 return __CLASS__
. ': $Id$';