511276aa97dfa15f6a12d78fe3c5f2a53383a8a8
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']);
111 $this->fld_patrolled
= isset($prop['patrolled']);
114 if($this->fld_patrolled
&& !$wgUser->isAllowed('patrol'))
115 $this->dieUsage("You need the patrol right to request the patrolled flag");
117 /* Add fields to our query if they are specified as a needed parameter. */
118 $this->addFieldsIf('rc_id', $this->fld_ids
);
119 $this->addFieldsIf('rc_cur_id', $this->fld_ids
);
120 $this->addFieldsIf('rc_this_oldid', $this->fld_ids
);
121 $this->addFieldsIf('rc_last_oldid', $this->fld_ids
);
122 $this->addFieldsIf('rc_comment', $this->fld_comment
);
123 $this->addFieldsIf('rc_user', $this->fld_user
);
124 $this->addFieldsIf('rc_user_text', $this->fld_user
);
125 $this->addFieldsIf('rc_minor', $this->fld_flags
);
126 $this->addFieldsIf('rc_bot', $this->fld_flags
);
127 $this->addFieldsIf('rc_new', $this->fld_flags
);
128 $this->addFieldsIf('rc_old_len', $this->fld_sizes
);
129 $this->addFieldsIf('rc_new_len', $this->fld_sizes
);
130 $this->addFieldsIf('rc_patrolled', $this->fld_patrolled
);
133 /* Specify the limit for our query. It's $limit+1 because we (possibly) need to
134 * generate a "continue" parameter, to allow paging. */
135 $this->addOption('LIMIT', $limit +
1);
137 /* Specify the index to use in the query as rc_timestamp, instead of rc_revid (default). */
138 $this->addOption('USE INDEX', 'rc_timestamp');
143 /* Perform the actual query. */
144 $db = $this->getDB();
145 $res = $this->select(__METHOD__
);
147 /* Iterate through the rows, adding data extracted from them to our query result. */
148 while ($row = $db->fetchObject($res)) {
149 if (++
$count > $limit) {
150 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
151 $this->setContinueEnumParameter('start', wfTimestamp(TS_ISO_8601
, $row->rc_timestamp
));
155 /* Extract the data from a single row. */
156 $vals = $this->extractRowInfo($row);
158 /* Add that row's data to our final output. */
163 $db->freeResult($res);
165 /* Format the result */
166 $result = $this->getResult();
167 $result->setIndexedTagName($data, 'rc');
168 $result->addValue('query', $this->getModuleName(), $data);
172 * Extracts from a single sql row the data needed to describe one recent change.
174 * @param $row The row from which to extract the data.
175 * @return An array mapping strings (descriptors) to their respective string values.
178 private function extractRowInfo($row) {
179 /* If page was moved somewhere, get the title of the move target. */
180 $movedToTitle = false;
181 if (!empty($row->rc_moved_to_title
))
182 $movedToTitle = Title
:: makeTitle($row->rc_moved_to_ns
, $row->rc_moved_to_title
);
184 /* Determine the title of the page that has been changed. */
185 $title = Title
:: makeTitle($row->rc_namespace
, $row->rc_title
);
187 /* Our output data. */
190 $type = intval ( $row->rc_type
);
192 /* Determine what kind of change this was. */
194 case RC_EDIT
: $vals['type'] = 'edit'; break;
195 case RC_NEW
: $vals['type'] = 'new'; break;
196 case RC_MOVE
: $vals['type'] = 'move'; break;
197 case RC_LOG
: $vals['type'] = 'log'; break;
198 case RC_MOVE_OVER_REDIRECT
: $vals['type'] = 'move over redirect'; break;
199 default: $vals['type'] = $type;
202 /* Create a new entry in the result for the title. */
203 if ($this->fld_title
) {
204 ApiQueryBase
:: addTitleInfo($vals, $title);
206 ApiQueryBase
:: addTitleInfo($vals, $movedToTitle, "new_");
209 /* Add ids, such as rcid, pageid, revid, and oldid to the change's info. */
210 if ($this->fld_ids
) {
211 $vals['rcid'] = intval($row->rc_id
);
212 $vals['pageid'] = intval($row->rc_cur_id
);
213 $vals['revid'] = intval($row->rc_this_oldid
);
214 $vals['old_revid'] = intval( $row->rc_last_oldid
);
217 /* Add user data and 'anon' flag, if use is anonymous. */
218 if ($this->fld_user
) {
219 $vals['user'] = $row->rc_user_text
;
224 /* Add flags, such as new, minor, bot. */
225 if ($this->fld_flags
) {
234 /* Add sizes of each revision. (Only available on 1.10+) */
235 if ($this->fld_sizes
) {
236 $vals['oldlen'] = intval($row->rc_old_len
);
237 $vals['newlen'] = intval($row->rc_new_len
);
240 /* Add the timestamp. */
241 if ($this->fld_timestamp
)
242 $vals['timestamp'] = wfTimestamp(TS_ISO_8601
, $row->rc_timestamp
);
244 /* Add edit summary / log summary. */
245 if ($this->fld_comment
&& !empty ($row->rc_comment
)) {
246 $vals['comment'] = $row->rc_comment
;
249 /* Add the patrolled flag */
250 if ($this->fld_patrolled
&& $row->rc_patrolled
== 1)
251 $vals['patrolled'] = '';
256 private function parseRCType($type)
262 $retval[] = $this->parseRCType($t);
267 case 'edit': return RC_EDIT
;
268 case 'new': return RC_NEW
;
269 case 'log': return RC_LOG
;
273 public function getAllowedParams() {
276 ApiBase
:: PARAM_TYPE
=> 'timestamp'
279 ApiBase
:: PARAM_TYPE
=> 'timestamp'
282 ApiBase
:: PARAM_DFLT
=> 'older',
283 ApiBase
:: PARAM_TYPE
=> array (
288 'namespace' => array (
289 ApiBase
:: PARAM_ISMULTI
=> true,
290 ApiBase
:: PARAM_TYPE
=> 'namespace'
293 ApiBase
:: PARAM_ISMULTI
=> true,
294 ApiBase
:: PARAM_DFLT
=> 'title|timestamp|ids',
295 ApiBase
:: PARAM_TYPE
=> array (
307 ApiBase
:: PARAM_ISMULTI
=> true,
308 ApiBase
:: PARAM_TYPE
=> array (
318 ApiBase
:: PARAM_DFLT
=> 10,
319 ApiBase
:: PARAM_TYPE
=> 'limit',
320 ApiBase
:: PARAM_MIN
=> 1,
321 ApiBase
:: PARAM_MAX
=> ApiBase
:: LIMIT_BIG1
,
322 ApiBase
:: PARAM_MAX2
=> ApiBase
:: LIMIT_BIG2
325 ApiBase
:: PARAM_ISMULTI
=> true,
326 ApiBase
:: PARAM_TYPE
=> array (
335 public function getParamDescription() {
337 'start' => 'The timestamp to start enumerating from.',
338 'end' => 'The timestamp to end enumerating.',
339 'dir' => 'In which direction to enumerate.',
340 'namespace' => 'Filter log entries to only this namespace(s)',
341 'prop' => 'Include additional pieces of information',
343 'Show only items that meet this criteria.',
344 'For example, to see only minor edits done by logged-in users, set show=minor|!anon'
346 'type' => 'Which types of changes to show.',
347 'limit' => 'How many total pages to return.'
351 public function getDescription() {
352 return 'Enumerate recent changes';
355 protected function getExamples() {
357 'api.php?action=query&list=recentchanges'
361 public function getVersion() {
362 return __CLASS__
. ': $Id$';