* New properties: links, templates, images, langlinks
[lhc/web/wiklou.git] / includes / api / ApiQueryWatchlist.php
1 <?php
2
3 /*
4 * Created on Sep 25, 2006
5 *
6 * API for MediaWiki 1.8+
7 *
8 * Copyright (C) 2006 Yuri Astrakhan <FirstnameLastname@gmail.com>
9 *
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.
14 *
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.
19 *
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
24 */
25
26 if (!defined('MEDIAWIKI')) {
27 // Eclipse helper - will be ignored in production
28 require_once ('ApiQueryBase.php');
29 }
30
31 /**
32 * @addtogroup API
33 */
34 class ApiQueryWatchlist extends ApiQueryGeneratorBase {
35
36 public function __construct($query, $moduleName) {
37 parent :: __construct($query, $moduleName, 'wl');
38 }
39
40 public function execute() {
41 $this->run();
42 }
43
44 public function executeGenerator($resultPageSet) {
45 $this->run($resultPageSet);
46 }
47
48 private function run($resultPageSet = null) {
49 global $wgUser;
50
51 if (!$wgUser->isLoggedIn())
52 $this->dieUsage('You must be logged-in to have a watchlist', 'notloggedin');
53
54 $allrev = $start = $end = $namespace = $dir = $limit = $prop = null;
55 extract($this->extractRequestParams());
56
57 $patrol = $timestamp = $user = $comment = false;
58 if (!is_null($prop)) {
59 if (!is_null($resultPageSet))
60 $this->dieUsage('prop parameter may not be used in a generator', 'params');
61
62 $user = (false !== array_search('user', $prop));
63 $comment = (false !== array_search('comment', $prop));
64 $timestamp = (false !== array_search('timestamp', $prop)); // TODO: $timestamp not currently being used.
65 $patrol = (false !== array_search('patrol', $prop));
66
67 if ($patrol) {
68 global $wgUseRCPatrol, $wgUser;
69 if (!$wgUseRCPatrol || !$wgUser->isAllowed('patrol'))
70 $this->dieUsage('patrol property is not available', 'patrol');
71 }
72 }
73
74 if (is_null($resultPageSet)) {
75 $this->addFields(array (
76 'rc_cur_id',
77 'rc_this_oldid',
78 'rc_namespace',
79 'rc_title',
80 'rc_new',
81 'rc_minor',
82 'rc_timestamp'
83 ));
84
85 $this->addFieldsIf('rc_user', $user);
86 $this->addFieldsIf('rc_user_text', $user);
87 $this->addFieldsIf('rc_comment', $comment);
88 $this->addFieldsIf('rc_patrolled', $patrol);
89 }
90 elseif ($allrev) {
91 $this->addFields(array (
92 'rc_this_oldid',
93 'rc_namespace',
94 'rc_title',
95 'rc_timestamp'
96 ));
97 } else {
98 $this->addFields(array (
99 'rc_cur_id',
100 'rc_namespace',
101 'rc_title',
102 'rc_timestamp'
103 ));
104 }
105
106 $this->addTables(array (
107 'watchlist',
108 'page',
109 'recentchanges'
110 ));
111
112 $userId = $wgUser->getID();
113 $this->addWhere(array (
114 'wl_namespace = rc_namespace',
115 'wl_title = rc_title',
116 'rc_cur_id = page_id',
117 'wl_user' => $userId
118 ));
119 $this->addWhereRange('rc_timestamp', $dir, $start, $end);
120 $this->addWhereFld('wl_namespace', $namespace);
121 $this->addWhereIf('rc_this_oldid=page_latest', !$allrev);
122 $this->addWhereIf("rc_timestamp > ''", !isset ($start) && !isset ($end));
123
124 $this->addOption('LIMIT', $limit +1);
125
126 $data = array ();
127 $count = 0;
128 $res = $this->select(__METHOD__);
129
130 $db = $this->getDB();
131 while ($row = $db->fetchObject($res)) {
132 if (++ $count > $limit) {
133 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
134 $this->setContinueEnumParameter('start', $row->rc_timestamp);
135 break;
136 }
137
138 if (is_null($resultPageSet)) {
139 $vals = $this->addRowInfo('rc', $row);
140 if($vals)
141 $data[] = $vals;
142 } else {
143 $title = Title :: makeTitle($row->rc_namespace, $row->rc_title);
144 // skip any pages that user has no rights to read
145 if ($title->userCanRead()) {
146 if ($allrev) {
147 $data[] = intval($row->rc_this_oldid);
148 } else {
149 $data[] = intval($row->rc_cur_id);
150 }
151 }
152 }
153 }
154
155 $db->freeResult($res);
156
157 if (is_null($resultPageSet)) {
158 $this->getResult()->setIndexedTagName($data, 'item');
159 $this->getResult()->addValue('query', $this->getModuleName(), $data);
160 }
161 elseif ($allrev) {
162 $resultPageSet->populateFromRevisionIDs($data);
163 } else {
164 $resultPageSet->populateFromPageIDs($data);
165 }
166 }
167
168 protected function getAllowedParams() {
169 return array (
170 'allrev' => false,
171 'start' => array (
172 ApiBase :: PARAM_TYPE => 'timestamp'
173 ),
174 'end' => array (
175 ApiBase :: PARAM_TYPE => 'timestamp'
176 ),
177 'namespace' => array (
178 ApiBase :: PARAM_ISMULTI => true,
179 ApiBase :: PARAM_TYPE => 'namespace'
180 ),
181 'dir' => array (
182 ApiBase :: PARAM_DFLT => 'older',
183 ApiBase :: PARAM_TYPE => array (
184 'newer',
185 'older'
186 )
187 ),
188 'limit' => array (
189 ApiBase :: PARAM_DFLT => 10,
190 ApiBase :: PARAM_TYPE => 'limit',
191 ApiBase :: PARAM_MIN => 1,
192 ApiBase :: PARAM_MAX1 => ApiBase :: LIMIT_BIG1,
193 ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
194 ),
195 'prop' => array (
196 APIBase :: PARAM_ISMULTI => true,
197 APIBase :: PARAM_TYPE => array (
198 'user',
199 'comment',
200 'timestamp',
201 'patrol'
202 )
203 )
204 );
205 }
206
207 protected function getParamDescription() {
208 return array (
209 'allrev' => 'Include multiple revisions of the same page within given timeframe.',
210 'start' => 'The timestamp to start enumerating from.',
211 'end' => 'The timestamp to end enumerating.',
212 'namespace' => 'Filter changes to only the given namespace(s).',
213 'dir' => 'In which direction to enumerate pages.',
214 'limit' => 'How many total pages to return per request.',
215 'prop' => 'Which additional items to get (non-generator mode only).'
216 );
217 }
218
219 protected function getDescription() {
220 return '';
221 }
222
223 protected function getExamples() {
224 return array (
225 'api.php?action=query&list=watchlist',
226 'api.php?action=query&list=watchlist&wlallrev',
227 'api.php?action=query&generator=watchlist&prop=info',
228 'api.php?action=query&generator=watchlist&gwlallrev&prop=revisions&rvprop=timestamp|user'
229 );
230 }
231
232 public function getVersion() {
233 return __CLASS__ . ': $Id$';
234 }
235 }
236 ?>