5 * Created on Sep 25, 2006
7 * API for MediaWiki 1.8+
9 * Copyright (C) 2006 Yuri Astrakhan <FirstnameLastname@gmail.com>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 * http://www.gnu.org/copyleft/gpl.html
27 if (!defined('MEDIAWIKI')) {
28 // Eclipse helper - will be ignored in production
29 require_once ('ApiQueryBase.php');
32 class ApiQueryWatchlist
extends ApiQueryGeneratorBase
{
34 public function __construct($query, $moduleName) {
35 parent
:: __construct($query, $moduleName, 'wl');
38 public function execute() {
42 public function executeGenerator($resultPageSet) {
43 $this->run($resultPageSet);
46 private function run($resultPageSet = null) {
49 if (!$wgUser->isLoggedIn())
50 $this->dieUsage('You must be logged-in to have a watchlist', 'notloggedin');
52 $allrev = $start = $end = $namespace = $dir = $limit = $prop = null;
53 extract($this->extractRequestParams());
57 $dirNewer = ($dir === 'newer');
58 $before = ($dirNewer ?
'<=' : '>=');
59 $after = ($dirNewer ?
'>=' : '<=');
69 'ORDER BY' => 'rc_timestamp' . ($dirNewer ?
'' : ' DESC'
72 $patrol = $timestamp = $user = $comment = false;
73 if (!is_null($prop)) {
74 if (!is_null($resultPageSet))
75 $this->dieUsage('prop parameter may not be used in a generator', 'params');
77 $user = (false !== array_search('user', $prop));
78 $comment = (false !== array_search('comment', $prop));
79 $timestamp = (false !== array_search('timestamp', $prop));
80 $patrol = (false !== array_search('patrol', $prop));
83 global $wgUseRCPatrol, $wgUser;
84 if (!$wgUseRCPatrol ||
!$wgUser->isAllowed('patrol'))
85 $this->dieUsage('patrol property is not available', 'patrol');
89 if (is_null($resultPageSet)) {
91 'rc_cur_id AS page_id',
92 'rc_this_oldid AS rev_id',
93 'rc_namespace AS page_namespace',
94 'rc_title AS page_title',
95 'rc_new AS page_is_new',
96 'rc_minor AS rev_minor_edit'
99 $fields[] = 'rc_user AS rev_user';
100 $fields[] = 'rc_user_text AS rev_user_text';
103 $fields[] = 'rc_comment AS rev_comment';
105 $fields[] = 'rc_timestamp AS rev_timestamp';
107 $fields[] = 'rc_patrolled';
111 'rc_this_oldid AS rev_id',
112 'rc_namespace AS page_namespace',
113 'rc_title AS page_title',
114 'rc_timestamp AS rev_timestamp'
118 'rc_cur_id AS page_id',
119 'rc_namespace AS page_namespace',
120 'rc_title AS page_title',
121 'rc_timestamp AS rev_timestamp'
126 'wl_namespace = rc_namespace',
127 'wl_title = rc_title',
128 'rc_cur_id = page_id',
129 'wl_user' => $wgUser->getID());
132 $where[] = 'rc_this_oldid=page_latest';
133 if (isset ($namespace))
134 $where['wl_namespace'] = $namespace;
137 $where[] = 'rc_timestamp' . $after . $db->addQuotes($start);
140 $where[] = 'rc_timestamp' . $before . $db->addQuotes($end);
142 if (!isset ($start) && !isset ($end))
143 $where[] = "rc_timestamp > ''";
145 $this->profileDBIn();
146 $res = $db->select($tables, $fields, $where, __METHOD__
, $options);
147 $this->profileDBOut();
151 while ($row = $db->fetchObject($res)) {
152 if (++
$count > $limit) {
153 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
154 $this->setContinueEnumParameter('from', $row->rev_timestamp
);
158 $title = Title
:: makeTitle($row->page_namespace
, $row->page_title
);
159 // skip any pages that user has no rights to read
160 if ($title->userCanRead()) {
162 if (is_null($resultPageSet)) {
163 $id = intval($row->page_id
);
166 $vals['pageid'] = intval($row->page_id
);
167 $vals['revid'] = intval($row->rev_id
);
168 $vals['ns'] = $title->getNamespace();
169 $vals['title'] = $title->getPrefixedText();
171 if ($row->page_is_new
)
173 if ($row->rev_minor_edit
)
179 $vals['user'] = $row->rev_user_text
;
182 $vals['comment'] = $row->rev_comment
;
184 $vals['timestamp'] = $row->rev_timestamp
;
185 if ($patrol && $row->rc_patrolled
)
186 $vals['patrolled'] = '';
191 $data[] = intval($row->rev_id
);
193 $data[] = intval($row->page_id
);
197 $db->freeResult($res);
199 if (is_null($resultPageSet)) {
200 ApiResult
:: setIndexedTagName($data, 'item');
201 $this->getResult()->addValue('query', $this->getModuleName(), $data);
204 $resultPageSet->populateFromRevisionIDs($data);
206 $resultPageSet->populateFromPageIDs($data);
210 protected function getAllowedParams() {
211 $namespaces = $this->getQuery()->getValidNamespaces();
215 ApiBase
:: PARAM_TYPE
=> 'timestamp'
218 ApiBase
:: PARAM_TYPE
=> 'timestamp'
220 'namespace' => array (
221 ApiBase
:: PARAM_ISMULTI
=> true,
222 ApiBase
:: PARAM_TYPE
=> $namespaces
225 ApiBase
:: PARAM_DFLT
=> 'older',
226 ApiBase
:: PARAM_TYPE
=> array (
232 ApiBase
:: PARAM_DFLT
=> 10,
233 ApiBase
:: PARAM_TYPE
=> 'limit',
234 ApiBase
:: PARAM_MIN
=> 1,
235 ApiBase
:: PARAM_MAX1
=> 500,
236 ApiBase
:: PARAM_MAX2
=> 5000
239 APIBase
:: PARAM_ISMULTI
=> true,
240 APIBase
:: PARAM_TYPE
=> array (
250 protected function getParamDescription() {
252 'allrev' => 'Include multiple revisions of the same page within given timeframe.',
253 'start' => 'The timestamp to start enumerating from.',
254 'end' => 'The timestamp to end enumerating.',
255 'namespace' => 'Filter changes to only the given namespace(s).',
256 'dir' => 'In which direction to enumerate pages.',
257 'limit' => 'How many total pages to return per request.',
258 'prop' => 'Which additional items to get (non-generator mode only).'
262 protected function getDescription() {
266 protected function getExamples() {
268 'api.php?action=query&list=watchlist',
269 'api.php?action=query&list=watchlist&wlallrev',
270 'api.php?action=query&generator=watchlist&prop=info',
271 'api.php?action=query&generator=watchlist&gwlallrev&prop=revisions&rvprop=timestamp|user'
275 public function getVersion() {
276 return __CLASS__
. ': $Id$';