4 * Created on Sep 25, 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 * This query action allows clients to retrieve a list of recently modified pages
33 * that are part of the logged-in user's watchlist.
37 class ApiQueryWatchlist
extends ApiQueryGeneratorBase
{
39 public function __construct($query, $moduleName) {
40 parent
:: __construct($query, $moduleName, 'wl');
43 public function execute() {
47 public function executeGenerator($resultPageSet) {
48 $this->run($resultPageSet);
51 private $fld_ids = false, $fld_title = false, $fld_patrol = false, $fld_flags = false,
52 $fld_timestamp = false, $fld_user = false, $fld_comment = false, $fld_sizes = false;
54 private function run($resultPageSet = null) {
55 global $wgUser, $wgDBtype;
57 $this->selectNamedDB('watchlist', DB_SLAVE
, 'watchlist');
59 if (!$wgUser->isLoggedIn())
60 $this->dieUsage('You must be logged-in to have a watchlist', 'notloggedin');
62 $params = $this->extractRequestParams();
64 if (!is_null($params['prop']) && is_null($resultPageSet)) {
66 $prop = array_flip($params['prop']);
68 $this->fld_ids
= isset($prop['ids']);
69 $this->fld_title
= isset($prop['title']);
70 $this->fld_flags
= isset($prop['flags']);
71 $this->fld_user
= isset($prop['user']);
72 $this->fld_comment
= isset($prop['comment']);
73 $this->fld_timestamp
= isset($prop['timestamp']);
74 $this->fld_sizes
= isset($prop['sizes']);
75 $this->fld_patrol
= isset($prop['patrol']);
77 if ($this->fld_patrol
) {
79 if (!$wgUser->useRCPatrol() && !$wgUser->useNPPatrol())
80 $this->dieUsage('patrol property is not available', 'patrol');
84 if (is_null($resultPageSet)) {
85 $this->addFields(array (
93 $this->addFieldsIf('rc_new', $this->fld_flags
);
94 $this->addFieldsIf('rc_minor', $this->fld_flags
);
95 $this->addFieldsIf('rc_bot', $this->fld_flags
);
96 $this->addFieldsIf('rc_user', $this->fld_user
);
97 $this->addFieldsIf('rc_user_text', $this->fld_user
);
98 $this->addFieldsIf('rc_comment', $this->fld_comment
);
99 $this->addFieldsIf('rc_patrolled', $this->fld_patrol
);
100 $this->addFieldsIf('rc_old_len', $this->fld_sizes
);
101 $this->addFieldsIf('rc_new_len', $this->fld_sizes
);
103 elseif ($params['allrev']) {
104 $this->addFields(array (
111 $this->addFields(array (
119 $this->addTables(array (
125 $userId = $wgUser->getId();
126 $this->addWhere(array (
127 'wl_namespace = rc_namespace',
128 'wl_title = rc_title',
129 'rc_cur_id = page_id',
130 'wl_user' => $userId,
134 $this->addWhereRange('rc_timestamp', $params['dir'], $params['start'], $params['end']);
135 $this->addWhereFld('wl_namespace', $params['namespace']);
136 $this->addWhereIf('rc_this_oldid=page_latest', !$params['allrev']);
138 if (!is_null($params['show'])) {
139 $show = array_flip($params['show']);
141 /* Check for conflicting parameters. */
142 if ((isset ($show['minor']) && isset ($show['!minor']))
143 ||
(isset ($show['bot']) && isset ($show['!bot']))
144 ||
(isset ($show['anon']) && isset ($show['!anon']))
145 ||
(isset ($show['patrolled']) && isset ($show['!patrolled']))) {
147 $this->dieUsage("Incorrect parameter - mutually exclusive values may not be supplied", 'show');
152 if((isset($show['patrolled']) ||
isset($show['!patrolled'])) && !$wgUser->useRCPatrol() && !$wgUser->useNPPatrol())
153 $this->dieUsage("You need the patrol right to request the patrolled flag", 'permissiondenied');
155 /* Add additional conditions to query depending upon parameters. */
156 $this->addWhereIf('rc_minor = 0', isset ($show['!minor']));
157 $this->addWhereIf('rc_minor != 0', isset ($show['minor']));
158 $this->addWhereIf('rc_bot = 0', isset ($show['!bot']));
159 $this->addWhereIf('rc_bot != 0', isset ($show['bot']));
160 $this->addWhereIf('rc_user = 0', isset ($show['anon']));
161 $this->addWhereIf('rc_user != 0', isset ($show['!anon']));
162 $this->addWhereIf('rc_patrolled = 0', isset($show['!patrolled']));
163 $this->addWhereIf('rc_patrolled != 0', isset($show['patrolled']));
166 if(!is_null($params['user']) && !is_null($params['excludeuser']))
167 $this->dieUsage('user and excludeuser cannot be used together', 'user-excludeuser');
168 if(!is_null($params['user']))
169 $this->addWhereFld('rc_user_text', $params['user']);
170 if(!is_null($params['excludeuser']))
171 $this->addWhere('rc_user_text != ' . $this->getDB()->addQuotes($params['excludeuser']));
174 # This is an index optimization for mysql, as done in the Special:Watchlist page
175 $this->addWhereIf("rc_timestamp > ''", !isset ($params['start']) && !isset ($params['end']) && $wgDBtype == 'mysql');
177 $this->addOption('LIMIT', $params['limit'] +
1);
181 $res = $this->select(__METHOD__
);
183 $db = $this->getDB();
184 while ($row = $db->fetchObject($res)) {
185 if (++
$count > $params['limit']) {
186 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
187 $this->setContinueEnumParameter('start', wfTimestamp(TS_ISO_8601
, $row->rc_timestamp
));
191 if (is_null($resultPageSet)) {
192 $vals = $this->extractRowInfo($row);
193 $fit = $this->getResult()->addValue(array('query', $this->getModuleName()), null, $vals);
196 $this->setContinueEnumParameter('start',
197 wfTimestamp(TS_ISO_8601
, $row->rc_timestamp
));
201 if ($params['allrev']) {
202 $ids[] = intval($row->rc_this_oldid
);
204 $ids[] = intval($row->rc_cur_id
);
209 $db->freeResult($res);
211 if (is_null($resultPageSet)) {
212 $this->getResult()->setIndexedTagName_internal(array('query', $this->getModuleName()), 'item');
214 elseif ($params['allrev']) {
215 $resultPageSet->populateFromRevisionIDs($ids);
217 $resultPageSet->populateFromPageIDs($ids);
221 private function extractRowInfo($row) {
225 if ($this->fld_ids
) {
226 $vals['pageid'] = intval($row->rc_cur_id
);
227 $vals['revid'] = intval($row->rc_this_oldid
);
230 if ($this->fld_title
)
231 ApiQueryBase
:: addTitleInfo($vals, Title
:: makeTitle($row->rc_namespace
, $row->rc_title
));
233 if ($this->fld_user
) {
234 $vals['user'] = $row->rc_user_text
;
239 if ($this->fld_flags
) {
248 if ($this->fld_patrol
&& isset($row->rc_patrolled
))
249 $vals['patrolled'] = '';
251 if ($this->fld_timestamp
)
252 $vals['timestamp'] = wfTimestamp(TS_ISO_8601
, $row->rc_timestamp
);
254 if ($this->fld_sizes
) {
255 $vals['oldlen'] = intval($row->rc_old_len
);
256 $vals['newlen'] = intval($row->rc_new_len
);
259 if ($this->fld_comment
&& isset( $row->rc_comment
))
260 $vals['comment'] = $row->rc_comment
;
265 public function getAllowedParams() {
269 ApiBase
:: PARAM_TYPE
=> 'timestamp'
272 ApiBase
:: PARAM_TYPE
=> 'timestamp'
274 'namespace' => array (
275 ApiBase
:: PARAM_ISMULTI
=> true,
276 ApiBase
:: PARAM_TYPE
=> 'namespace'
279 ApiBase
:: PARAM_TYPE
=> 'user',
281 'excludeuser' => array(
282 ApiBase
:: PARAM_TYPE
=> 'user',
285 ApiBase
:: PARAM_DFLT
=> 'older',
286 ApiBase
:: PARAM_TYPE
=> array (
292 ApiBase
:: PARAM_DFLT
=> 10,
293 ApiBase
:: PARAM_TYPE
=> 'limit',
294 ApiBase
:: PARAM_MIN
=> 1,
295 ApiBase
:: PARAM_MAX
=> ApiBase
:: LIMIT_BIG1
,
296 ApiBase
:: PARAM_MAX2
=> ApiBase
:: LIMIT_BIG2
299 APIBase
:: PARAM_ISMULTI
=> true,
300 APIBase
:: PARAM_DFLT
=> 'ids|title|flags',
301 APIBase
:: PARAM_TYPE
=> array (
313 ApiBase
:: PARAM_ISMULTI
=> true,
314 ApiBase
:: PARAM_TYPE
=> array (
328 public function getParamDescription() {
330 'allrev' => 'Include multiple revisions of the same page within given timeframe.',
331 'start' => 'The timestamp to start enumerating from.',
332 'end' => 'The timestamp to end enumerating.',
333 'namespace' => 'Filter changes to only the given namespace(s).',
334 'user' => 'Only list changes by this user',
335 'excludeuser' => 'Don\'t list changes by this user',
336 'dir' => 'In which direction to enumerate pages.',
337 'limit' => 'How many total results to return per request.',
338 'prop' => 'Which additional items to get (non-generator mode only).',
340 'Show only items that meet this criteria.',
341 'For example, to see only minor edits done by logged-in users, set show=minor|!anon'
346 public function getDescription() {
347 return "Get all recent changes to pages in the logged in user's watchlist";
350 protected function getExamples() {
352 'api.php?action=query&list=watchlist',
353 'api.php?action=query&list=watchlist&wlprop=ids|title|timestamp|user|comment',
354 'api.php?action=query&list=watchlist&wlallrev&wlprop=ids|title|timestamp|user|comment',
355 'api.php?action=query&generator=watchlist&prop=info',
356 'api.php?action=query&generator=watchlist&gwlallrev&prop=revisions&rvprop=timestamp|user'
360 public function getVersion() {
361 return __CLASS__
. ': $Id$';