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