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,
47 public function execute() {
48 $limit = $prop = $namespace = $show = $dir = $start = $end = null;
49 extract($this->extractRequestParams());
51 $this->addTables('recentchanges');
52 $this->addWhereRange('rc_timestamp', $dir, $start, $end);
53 $this->addWhereFld('rc_namespace', $namespace);
55 if (!is_null($show)) {
56 $show = array_flip($show);
57 if ((isset ($show['minor']) && isset ($show['!minor'])) ||
(isset ($show['bot']) && isset ($show['!bot'])) ||
(isset ($show['anon']) && isset ($show['!anon'])))
58 $this->dieUsage("Incorrect parameter - mutually exclusive values may not be supplied", 'show');
60 $this->addWhereIf('rc_minor = 0', isset ($show['!minor']));
61 $this->addWhereIf('rc_minor != 0', isset ($show['minor']));
62 $this->addWhereIf('rc_bot = 0', isset ($show['!bot']));
63 $this->addWhereIf('rc_bot != 0', isset ($show['bot']));
64 $this->addWhereIf('rc_user = 0', isset ($show['anon']));
65 $this->addWhereIf('rc_user != 0', isset ($show['!anon']));
68 $this->addFields(array (
77 if (!is_null($prop)) {
78 $prop = array_flip($prop);
80 $this->fld_comment
= isset ($prop['comment']);
81 $this->fld_user
= isset ($prop['user']);
82 $this->fld_flags
= isset ($prop['flags']);
83 $this->fld_timestamp
= isset ($prop['timestamp']);
84 $this->fld_title
= isset ($prop['title']);
85 $this->fld_ids
= isset ($prop['ids']);
86 $this->fld_sizes
= isset ($prop['sizes']);
88 $this->addFieldsIf('rc_cur_id', $this->fld_ids
);
89 $this->addFieldsIf('rc_this_oldid', $this->fld_ids
);
90 $this->addFieldsIf('rc_last_oldid', $this->fld_ids
);
91 $this->addFieldsIf('rc_comment', $this->fld_comment
);
92 $this->addFieldsIf('rc_user', $this->fld_user
);
93 $this->addFieldsIf('rc_user_text', $this->fld_user
);
94 $this->addFieldsIf('rc_minor', $this->fld_flags
);
95 $this->addFieldsIf('rc_bot', $this->fld_flags
);
96 $this->addFieldsIf('rc_new', $this->fld_flags
);
97 $this->addFieldsIf('rc_old_len', $this->fld_sizes
);
98 $this->addFieldsIf('rc_new_len', $this->fld_sizes
);
101 $this->addOption('LIMIT', $limit +
1);
102 $this->addOption('USE INDEX', 'rc_timestamp');
106 $db = $this->getDB();
107 $res = $this->select(__METHOD__
);
109 while ($row = $db->fetchObject($res)) {
110 if (++
$count > $limit) {
111 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
112 $this->setContinueEnumParameter('start', $row->rc_timestamp
);
116 $vals = $this->extractRowInfo($row);
120 $db->freeResult($res);
122 $result = $this->getResult();
123 $result->setIndexedTagName($data, 'rc');
124 $result->addValue('query', $this->getModuleName(), $data);
128 * Security overview: As implemented, any change to a restricted page (userCanRead() == false)
129 * is hidden from the client, except when a page is being moved to a non-restricted name,
130 * or when a non-restricted becomes restricted. When shown, all other fields are shown as well.
132 private function extractRowInfo($row) {
133 $title = Title
:: makeTitle($row->rc_namespace
, $row->rc_title
);
134 $movedToTitle = false;
135 if (!empty($row->rc_moved_to_title
))
136 $movedToTitle = Title
:: makeTitle($row->rc_moved_to_ns
, $row->rc_moved_to_title
);
138 // If either this is an edit of a restricted page,
139 // or a move where both to and from names are restricted, skip
140 if (!$title->userCanRead() && (!$movedToTitle ||
141 ($movedToTitle && !$movedToTitle->userCanRead())))
146 $vals['type'] = intval($row->rc_type
);
148 if ($this->fld_title
) {
149 ApiQueryBase
:: addTitleInfo($vals, $title);
151 ApiQueryBase
:: addTitleInfo($vals, $movedToTitle, false, "new_");
154 if ($this->fld_ids
) {
155 $vals['pageid'] = intval($row->rc_cur_id
);
156 $vals['revid'] = intval($row->rc_this_oldid
);
157 $vals['old_revid'] = intval( $row->rc_last_oldid
);
160 if ($this->fld_user
) {
161 $vals['user'] = $row->rc_user_text
;
166 if ($this->fld_flags
) {
175 if ($this->fld_sizes
) {
176 $vals['oldlen'] = intval($row->rc_old_len
);
177 $vals['newlen'] = intval($row->rc_new_len
);
180 if ($this->fld_timestamp
)
181 $vals['timestamp'] = wfTimestamp(TS_ISO_8601
, $row->rc_timestamp
);
183 if ($this->fld_comment
&& !empty ($row->rc_comment
)) {
184 $vals['comment'] = $row->rc_comment
;
190 protected function getAllowedParams() {
193 ApiBase
:: PARAM_TYPE
=> 'timestamp'
196 ApiBase
:: PARAM_TYPE
=> 'timestamp'
199 ApiBase
:: PARAM_DFLT
=> 'older',
200 ApiBase
:: PARAM_TYPE
=> array (
205 'namespace' => array (
206 ApiBase
:: PARAM_ISMULTI
=> true,
207 ApiBase
:: PARAM_TYPE
=> 'namespace'
210 ApiBase
:: PARAM_ISMULTI
=> true,
211 ApiBase
:: PARAM_DFLT
=> 'title|timestamp|ids',
212 ApiBase
:: PARAM_TYPE
=> array (
223 ApiBase
:: PARAM_ISMULTI
=> true,
224 ApiBase
:: PARAM_TYPE
=> array (
234 ApiBase
:: PARAM_DFLT
=> 10,
235 ApiBase
:: PARAM_TYPE
=> 'limit',
236 ApiBase
:: PARAM_MIN
=> 1,
237 ApiBase
:: PARAM_MAX
=> ApiBase
:: LIMIT_BIG1
,
238 ApiBase
:: PARAM_MAX2
=> ApiBase
:: LIMIT_BIG2
243 protected function getParamDescription() {
245 'start' => 'The timestamp to start enumerating from.',
246 'end' => 'The timestamp to end enumerating.',
247 'dir' => 'In which direction to enumerate.',
248 'namespace' => 'Filter log entries to only this namespace(s)',
249 'prop' => 'Include additional pieces of information',
251 'Show only items that meet this criteria.',
252 'For example, to see only minor edits done by logged-in users, set show=minor|!anon'
254 'limit' => 'How many total pages to return.'
258 protected function getDescription() {
259 return 'Enumerate recent changes';
262 protected function getExamples() {
264 'api.php?action=query&list=recentchanges'
268 public function getVersion() {
269 return __CLASS__
. ': $Id$';