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 = $titles = $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'
63 $rc = $db->tableName('recentchanges');
64 $this->addWhereRange('rc_timestamp', $dir, $start, $end);
65 $this->addWhereFld('rc_namespace', $namespace);
66 $this->addWhereFld('rc_deleted', 0);
70 foreach($titles as $t)
72 $obj = Title
::newFromText($t);
74 if($obj->getNamespace() < 0)
76 // LinkBatch refuses these, but we need them anyway
77 if(!array_key_exists($obj->getNamespace(), $lb->data
))
78 $lb->data
[$obj->getNamespace()] = array();
79 $lb->data
[$obj->getNamespace()][$obj->getDbKey()] = 1;
82 $where = $lb->constructSet('rc', $this->getDb());
84 $this->addWhere($where);
88 $this->addWhereFld('rc_type', $this->parseRCType($type));
90 if (!is_null($show)) {
91 $show = array_flip($show);
93 /* Check for conflicting parameters. */
94 if ((isset ($show['minor']) && isset ($show['!minor']))
95 ||
(isset ($show['bot']) && isset ($show['!bot']))
96 ||
(isset ($show['anon']) && isset ($show['!anon']))
97 ||
(isset ($show['redirect']) && isset ($show['!redirect']))) {
99 $this->dieUsage("Incorrect parameter - mutually exclusive values may not be supplied", 'show');
102 /* Add additional conditions to query depending upon parameters. */
103 $this->addWhereIf('rc_minor = 0', isset ($show['!minor']));
104 $this->addWhereIf('rc_minor != 0', isset ($show['minor']));
105 $this->addWhereIf('rc_bot = 0', isset ($show['!bot']));
106 $this->addWhereIf('rc_bot != 0', isset ($show['bot']));
107 $this->addWhereIf('rc_user = 0', isset ($show['anon']));
108 $this->addWhereIf('rc_user != 0', isset ($show['!anon']));
109 $this->addWhereIf('page_is_redirect = 1', isset ($show['redirect']));
110 // Don't throw log entries out the window here
111 $this->addWhereIf('page_is_redirect = 0 OR page_is_redirect IS NULL', isset ($show['!redirect']));
114 /* Add the fields we're concerned with to out query. */
115 $this->addFields(array (
124 /* Determine what properties we need to display. */
125 if (!is_null($prop)) {
126 $prop = array_flip($prop);
128 /* Set up internal members based upon params. */
129 $this->fld_comment
= isset ($prop['comment']);
130 $this->fld_user
= isset ($prop['user']);
131 $this->fld_flags
= isset ($prop['flags']);
132 $this->fld_timestamp
= isset ($prop['timestamp']);
133 $this->fld_title
= isset ($prop['title']);
134 $this->fld_ids
= isset ($prop['ids']);
135 $this->fld_sizes
= isset ($prop['sizes']);
136 $this->fld_redirect
= isset($prop['redirect']);
137 $this->fld_patrolled
= isset($prop['patrolled']);
140 if($this->fld_patrolled
&& !$wgUser->isAllowed('patrol'))
141 $this->dieUsage("You need the patrol right to request the patrolled flag", 'permissiondenied');
143 /* Add fields to our query if they are specified as a needed parameter. */
144 $this->addFieldsIf('rc_id', $this->fld_ids
);
145 $this->addFieldsIf('rc_cur_id', $this->fld_ids
);
146 $this->addFieldsIf('rc_this_oldid', $this->fld_ids
);
147 $this->addFieldsIf('rc_last_oldid', $this->fld_ids
);
148 $this->addFieldsIf('rc_comment', $this->fld_comment
);
149 $this->addFieldsIf('rc_user', $this->fld_user
);
150 $this->addFieldsIf('rc_user_text', $this->fld_user
);
151 $this->addFieldsIf('rc_minor', $this->fld_flags
);
152 $this->addFieldsIf('rc_bot', $this->fld_flags
);
153 $this->addFieldsIf('rc_new', $this->fld_flags
);
154 $this->addFieldsIf('rc_old_len', $this->fld_sizes
);
155 $this->addFieldsIf('rc_new_len', $this->fld_sizes
);
156 $this->addFieldsIf('rc_patrolled', $this->fld_patrolled
);
157 if($this->fld_redirect ||
isset($show['redirect']) ||
isset($show['!redirect']))
159 $page = $db->tableName('page');
160 $tables = "$page RIGHT JOIN $rc FORCE INDEX(rc_timestamp) ON page_namespace=rc_namespace AND page_title=rc_title";
161 $this->addFields('page_is_redirect');
165 $tables = "$rc FORCE INDEX(rc_timestamp)";
166 $this->addTables($tables);
167 /* Specify the limit for our query. It's $limit+1 because we (possibly) need to
168 * generate a "continue" parameter, to allow paging. */
169 $this->addOption('LIMIT', $limit +
1);
174 /* Perform the actual query. */
175 $db = $this->getDB();
176 $res = $this->select(__METHOD__
);
178 /* Iterate through the rows, adding data extracted from them to our query result. */
179 while ($row = $db->fetchObject($res)) {
180 if (++
$count > $limit) {
181 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
182 $this->setContinueEnumParameter('start', wfTimestamp(TS_ISO_8601
, $row->rc_timestamp
));
186 /* Extract the data from a single row. */
187 $vals = $this->extractRowInfo($row);
189 /* Add that row's data to our final output. */
194 $db->freeResult($res);
196 /* Format the result */
197 $result = $this->getResult();
198 $result->setIndexedTagName($data, 'rc');
199 $result->addValue('query', $this->getModuleName(), $data);
203 * Extracts from a single sql row the data needed to describe one recent change.
205 * @param $row The row from which to extract the data.
206 * @return An array mapping strings (descriptors) to their respective string values.
209 private function extractRowInfo($row) {
210 /* If page was moved somewhere, get the title of the move target. */
211 $movedToTitle = false;
212 if (!empty($row->rc_moved_to_title
))
213 $movedToTitle = Title
:: makeTitle($row->rc_moved_to_ns
, $row->rc_moved_to_title
);
215 /* Determine the title of the page that has been changed. */
216 $title = Title
:: makeTitle($row->rc_namespace
, $row->rc_title
);
218 /* Our output data. */
221 $type = intval ( $row->rc_type
);
223 /* Determine what kind of change this was. */
225 case RC_EDIT
: $vals['type'] = 'edit'; break;
226 case RC_NEW
: $vals['type'] = 'new'; break;
227 case RC_MOVE
: $vals['type'] = 'move'; break;
228 case RC_LOG
: $vals['type'] = 'log'; break;
229 case RC_MOVE_OVER_REDIRECT
: $vals['type'] = 'move over redirect'; break;
230 default: $vals['type'] = $type;
233 /* Create a new entry in the result for the title. */
234 if ($this->fld_title
) {
235 ApiQueryBase
:: addTitleInfo($vals, $title);
237 ApiQueryBase
:: addTitleInfo($vals, $movedToTitle, "new_");
240 /* Add ids, such as rcid, pageid, revid, and oldid to the change's info. */
241 if ($this->fld_ids
) {
242 $vals['rcid'] = intval($row->rc_id
);
243 $vals['pageid'] = intval($row->rc_cur_id
);
244 $vals['revid'] = intval($row->rc_this_oldid
);
245 $vals['old_revid'] = intval( $row->rc_last_oldid
);
248 /* Add user data and 'anon' flag, if use is anonymous. */
249 if ($this->fld_user
) {
250 $vals['user'] = $row->rc_user_text
;
255 /* Add flags, such as new, minor, bot. */
256 if ($this->fld_flags
) {
265 /* Add sizes of each revision. (Only available on 1.10+) */
266 if ($this->fld_sizes
) {
267 $vals['oldlen'] = intval($row->rc_old_len
);
268 $vals['newlen'] = intval($row->rc_new_len
);
271 /* Add the timestamp. */
272 if ($this->fld_timestamp
)
273 $vals['timestamp'] = wfTimestamp(TS_ISO_8601
, $row->rc_timestamp
);
275 /* Add edit summary / log summary. */
276 if ($this->fld_comment
&& !empty ($row->rc_comment
)) {
277 $vals['comment'] = $row->rc_comment
;
280 if ($this->fld_redirect
)
281 if($row->page_is_redirect
)
282 $vals['redirect'] = '';
284 /* Add the patrolled flag */
285 if ($this->fld_patrolled
&& $row->rc_patrolled
== 1)
286 $vals['patrolled'] = '';
291 private function parseRCType($type)
297 $retval[] = $this->parseRCType($t);
302 case 'edit': return RC_EDIT
;
303 case 'new': return RC_NEW
;
304 case 'log': return RC_LOG
;
308 public function getAllowedParams() {
311 ApiBase
:: PARAM_TYPE
=> 'timestamp'
314 ApiBase
:: PARAM_TYPE
=> 'timestamp'
317 ApiBase
:: PARAM_DFLT
=> 'older',
318 ApiBase
:: PARAM_TYPE
=> array (
323 'namespace' => array (
324 ApiBase
:: PARAM_ISMULTI
=> true,
325 ApiBase
:: PARAM_TYPE
=> 'namespace'
328 ApiBase
:: PARAM_ISMULTI
=> true
331 ApiBase
:: PARAM_ISMULTI
=> true,
332 ApiBase
:: PARAM_DFLT
=> 'title|timestamp|ids',
333 ApiBase
:: PARAM_TYPE
=> array (
346 ApiBase
:: PARAM_ISMULTI
=> true,
347 ApiBase
:: PARAM_TYPE
=> array (
359 ApiBase
:: PARAM_DFLT
=> 10,
360 ApiBase
:: PARAM_TYPE
=> 'limit',
361 ApiBase
:: PARAM_MIN
=> 1,
362 ApiBase
:: PARAM_MAX
=> ApiBase
:: LIMIT_BIG1
,
363 ApiBase
:: PARAM_MAX2
=> ApiBase
:: LIMIT_BIG2
366 ApiBase
:: PARAM_ISMULTI
=> true,
367 ApiBase
:: PARAM_TYPE
=> array (
376 public function getParamDescription() {
378 'start' => 'The timestamp to start enumerating from.',
379 'end' => 'The timestamp to end enumerating.',
380 'dir' => 'In which direction to enumerate.',
381 'namespace' => 'Filter log entries to only this namespace(s)',
382 'titles' => 'Filter log entries to only these page titles',
383 'prop' => 'Include additional pieces of information',
385 'Show only items that meet this criteria.',
386 'For example, to see only minor edits done by logged-in users, set show=minor|!anon'
388 'type' => 'Which types of changes to show.',
389 'limit' => 'How many total pages to return.'
393 public function getDescription() {
394 return 'Enumerate recent changes';
397 protected function getExamples() {
399 'api.php?action=query&list=recentchanges'
403 public function getVersion() {
404 return __CLASS__
. ': $Id$';