4ce7b6bbb43672f8b169105e83d3706a42ace671
4 * Created on Sep 25, 2006
6 * API for MediaWiki 1.8+
8 * Copyright (C) 2006 Yuri Astrakhan <FirstnameLastname@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');
34 class ApiQueryWatchlist
extends ApiQueryGeneratorBase
{
36 public function __construct($query, $moduleName) {
37 parent
:: __construct($query, $moduleName, 'wl');
40 public function execute() {
44 public function executeGenerator($resultPageSet) {
45 $this->run($resultPageSet);
48 private $fld_patrol = false, $fld_flags = false, $fld_timestamp = false, $fld_user = false, $fld_comment = false;
50 private function run($resultPageSet = null) {
53 $this->selectNamedDB('watchlist', DB_SLAVE
, 'watchlist');
55 if (!$wgUser->isLoggedIn())
56 $this->dieUsage('You must be logged-in to have a watchlist', 'notloggedin');
58 $allrev = $start = $end = $namespace = $dir = $limit = $prop = null;
59 extract($this->extractRequestParams());
61 if (!is_null($prop)) {
62 if (!is_null($resultPageSet))
63 $this->dieUsage('prop parameter may not be used in a generator', 'params');
65 $prop = array_flip($prop);
67 $this->fld_flags
= isset($prop['flags']);
68 $this->fld_user
= isset($prop['user']);
69 $this->fld_comment
= isset($prop['comment']);
70 $this->fld_timestamp
= isset($prop['timestamp']);
71 $this->fld_patrol
= isset($prop['patrol']);
73 if ($this->fld_patrol
) {
74 global $wgUseRCPatrol, $wgUser;
75 if (!$wgUseRCPatrol ||
!$wgUser->isAllowed('patrol'))
76 $this->dieUsage('patrol property is not available', 'patrol');
80 if (is_null($resultPageSet)) {
81 $this->addFields(array (
83 // 'rc_this_oldid', // Should this field be exposed?
89 $this->addFieldsIf('rc_new', $this->fld_flags
);
90 $this->addFieldsIf('rc_minor', $this->fld_flags
);
91 $this->addFieldsIf('rc_user', $this->fld_user
);
92 $this->addFieldsIf('rc_user_text', $this->fld_user
);
93 $this->addFieldsIf('rc_comment', $this->fld_comment
);
94 $this->addFieldsIf('rc_patrolled', $this->fld_patrol
);
97 $this->addFields(array (
104 $this->addFields(array (
112 $this->addTables(array (
118 $userId = $wgUser->getID();
119 $this->addWhere(array (
120 'wl_namespace = rc_namespace',
121 'wl_title = rc_title',
122 'rc_cur_id = page_id',
125 $this->addWhereRange('rc_timestamp', $dir, $start, $end);
126 $this->addWhereFld('wl_namespace', $namespace);
127 $this->addWhereIf('rc_this_oldid=page_latest', !$allrev);
128 $this->addWhereIf("rc_timestamp > ''", !isset ($start) && !isset ($end));
130 $this->addOption('LIMIT', $limit +
1);
134 $res = $this->select(__METHOD__
);
136 $db = $this->getDB();
137 while ($row = $db->fetchObject($res)) {
138 if (++
$count > $limit) {
139 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
140 $this->setContinueEnumParameter('start', $row->rc_timestamp
);
144 if (is_null($resultPageSet)) {
145 $vals = $this->extractRowInfo($row);
149 $title = Title
:: makeTitle($row->rc_namespace
, $row->rc_title
);
150 // skip any pages that user has no rights to read
151 if ($title->userCanRead()) {
153 $data[] = intval($row->rc_this_oldid
);
155 $data[] = intval($row->rc_cur_id
);
161 $db->freeResult($res);
163 if (is_null($resultPageSet)) {
164 $this->getResult()->setIndexedTagName($data, 'item');
165 $this->getResult()->addValue('query', $this->getModuleName(), $data);
168 $resultPageSet->populateFromRevisionIDs($data);
170 $resultPageSet->populateFromPageIDs($data);
174 private function extractRowInfo($row) {
176 $title = Title
:: makeTitle($row->rc_namespace
, $row->rc_title
);
177 if (!$title->userCanRead())
182 $vals['pageid'] = intval($row->rc_cur_id
);
183 // $vals['textid'] = intval($row->rc_this_oldid); // Should this field be exposed?
185 ApiQueryBase
:: addTitleInfo($vals, $title);
187 if ($this->fld_user
) {
188 $vals['user'] = $row->rc_user_text
;
193 if ($this->fld_flags
) {
200 if ($this->fld_patrol
&& isset($row->rc_patrolled
))
201 $vals['patrolled'] = '';
203 if ($this->fld_timestamp
)
204 $vals['timestamp'] = wfTimestamp(TS_ISO_8601
, $row->rc_timestamp
);
206 if ($this->fld_comment
&& !empty ($row->rc_comment
))
207 $vals['comment'] = $row->rc_comment
;
212 protected function getAllowedParams() {
216 ApiBase
:: PARAM_TYPE
=> 'timestamp'
219 ApiBase
:: PARAM_TYPE
=> 'timestamp'
221 'namespace' => array (
222 ApiBase
:: PARAM_ISMULTI
=> true,
223 ApiBase
:: PARAM_TYPE
=> 'namespace'
226 ApiBase
:: PARAM_DFLT
=> 'older',
227 ApiBase
:: PARAM_TYPE
=> array (
233 ApiBase
:: PARAM_DFLT
=> 10,
234 ApiBase
:: PARAM_TYPE
=> 'limit',
235 ApiBase
:: PARAM_MIN
=> 1,
236 ApiBase
:: PARAM_MAX
=> ApiBase
:: LIMIT_BIG1
,
237 ApiBase
:: PARAM_MAX2
=> ApiBase
:: LIMIT_BIG2
240 APIBase
:: PARAM_ISMULTI
=> true,
241 APIBase
:: PARAM_TYPE
=> array (
252 protected function getParamDescription() {
254 'allrev' => 'Include multiple revisions of the same page within given timeframe.',
255 'start' => 'The timestamp to start enumerating from.',
256 'end' => 'The timestamp to end enumerating.',
257 'namespace' => 'Filter changes to only the given namespace(s).',
258 'dir' => 'In which direction to enumerate pages.',
259 'limit' => 'How many total pages to return per request.',
260 'prop' => 'Which additional items to get (non-generator mode only).'
264 protected function getDescription() {
268 protected function getExamples() {
270 'api.php?action=query&list=watchlist&wlprop=timestamp|user|comment',
271 'api.php?action=query&list=watchlist&wlallrev&wlprop=timestamp|user|comment',
272 'api.php?action=query&generator=watchlist&prop=info',
273 'api.php?action=query&generator=watchlist&gwlallrev&prop=revisions&rvprop=timestamp|user'
277 public function getVersion() {
278 return __CLASS__
. ': $Id$';