]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - includes/api/ApiQueryWatchlist.php
MediaWiki 1.15.0
[autoinstallsdev/mediawiki.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 <Firstname><Lastname>@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  * This query action allows clients to retrieve a list of recently modified pages
33  * that are part of the logged-in user's watchlist.
34  *
35  * @ingroup API
36  */
37 class ApiQueryWatchlist extends ApiQueryGeneratorBase {
38
39         public function __construct($query, $moduleName) {
40                 parent :: __construct($query, $moduleName, 'wl');
41         }
42
43         public function execute() {
44                 $this->run();
45         }
46
47         public function executeGenerator($resultPageSet) {
48                 $this->run($resultPageSet);
49         }
50
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;
53
54         private function run($resultPageSet = null) {
55                 global $wgUser, $wgDBtype;
56
57                 $this->selectNamedDB('watchlist', DB_SLAVE, 'watchlist');
58
59                 if (!$wgUser->isLoggedIn())
60                         $this->dieUsage('You must be logged-in to have a watchlist', 'notloggedin');
61
62                 $params = $this->extractRequestParams();
63
64                 if (!is_null($params['prop']) && is_null($resultPageSet)) {
65
66                         $prop = array_flip($params['prop']);
67
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']);
76
77                         if ($this->fld_patrol) {
78                                 global $wgUser;
79                                 if (!$wgUser->useRCPatrol() && !$wgUser->useNPPatrol())
80                                         $this->dieUsage('patrol property is not available', 'patrol');
81                         }
82                 }
83
84                 if (is_null($resultPageSet)) {
85                         $this->addFields(array (
86                                 'rc_cur_id',
87                                 'rc_this_oldid',
88                                 'rc_namespace',
89                                 'rc_title',
90                                 'rc_timestamp'
91                         ));
92
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);
102                 }
103                 elseif ($params['allrev']) {
104                         $this->addFields(array (
105                                 'rc_this_oldid',
106                                 'rc_namespace',
107                                 'rc_title',
108                                 'rc_timestamp'
109                         ));
110                 } else {
111                         $this->addFields(array (
112                                 'rc_cur_id',
113                                 'rc_namespace',
114                                 'rc_title',
115                                 'rc_timestamp'
116                         ));
117                 }
118
119                 $this->addTables(array (
120                         'watchlist',
121                         'page',
122                         'recentchanges'
123                 ));
124
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,
131                         'rc_deleted' => 0,
132                 ));
133
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']);
137
138                 if (!is_null($params['show'])) {
139                         $show = array_flip($params['show']);
140
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']))) {
146
147                                 $this->dieUsage("Incorrect parameter - mutually exclusive values may not be supplied", 'show');
148                         }
149                         
150                         // Check permissions
151                         global $wgUser;
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');
154
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']));                      
164                 }
165
166
167                 # This is an index optimization for mysql, as done in the Special:Watchlist page
168                 $this->addWhereIf("rc_timestamp > ''", !isset ($params['start']) && !isset ($params['end']) && $wgDBtype == 'mysql');
169
170                 $this->addOption('LIMIT', $params['limit'] +1);
171
172                 $ids = array ();
173                 $count = 0;
174                 $res = $this->select(__METHOD__);
175
176                 $db = $this->getDB();
177                 while ($row = $db->fetchObject($res)) {
178                         if (++ $count > $params['limit']) {
179                                 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
180                                 $this->setContinueEnumParameter('start', wfTimestamp(TS_ISO_8601, $row->rc_timestamp));
181                                 break;
182                         }
183
184                         if (is_null($resultPageSet)) {
185                                 $vals = $this->extractRowInfo($row);
186                                 $fit = $this->getResult()->addValue(array('query', $this->getModuleName()), null, $vals);
187                                 if(!$fit)
188                                 {
189                                         $this->setContinueEnumParameter('start',
190                                                         wfTimestamp(TS_ISO_8601, $row->rc_timestamp));
191                                         break;
192                                 }
193                         } else {
194                                 if ($params['allrev']) {
195                                         $ids[] = intval($row->rc_this_oldid);
196                                 } else {
197                                         $ids[] = intval($row->rc_cur_id);
198                                 }
199                         }
200                 }
201
202                 $db->freeResult($res);
203
204                 if (is_null($resultPageSet)) {
205                         $this->getResult()->setIndexedTagName_internal(array('query', $this->getModuleName()), 'item');
206                 }
207                 elseif ($params['allrev']) {
208                         $resultPageSet->populateFromRevisionIDs($ids);
209                 } else {
210                         $resultPageSet->populateFromPageIDs($ids);
211                 }
212         }
213
214         private function extractRowInfo($row) {
215
216                 $vals = array ();
217
218                 if ($this->fld_ids) {
219                         $vals['pageid'] = intval($row->rc_cur_id);
220                         $vals['revid'] = intval($row->rc_this_oldid);
221                 }
222
223                 if ($this->fld_title)
224                         ApiQueryBase :: addTitleInfo($vals, Title :: makeTitle($row->rc_namespace, $row->rc_title));
225
226                 if ($this->fld_user) {
227                         $vals['user'] = $row->rc_user_text;
228                         if (!$row->rc_user)
229                                 $vals['anon'] = '';
230                 }
231
232                 if ($this->fld_flags) {
233                         if ($row->rc_new)
234                                 $vals['new'] = '';
235                         if ($row->rc_minor)
236                                 $vals['minor'] = '';
237                         if ($row->rc_bot)
238                                 $vals['bot'] = '';
239                 }
240
241                 if ($this->fld_patrol && isset($row->rc_patrolled))
242                         $vals['patrolled'] = '';
243
244                 if ($this->fld_timestamp)
245                         $vals['timestamp'] = wfTimestamp(TS_ISO_8601, $row->rc_timestamp);
246
247                 if ($this->fld_sizes) {
248                         $vals['oldlen'] = intval($row->rc_old_len);
249                         $vals['newlen'] = intval($row->rc_new_len);
250                 }
251
252                 if ($this->fld_comment && isset( $row->rc_comment ))
253                         $vals['comment'] = $row->rc_comment;
254
255                 return $vals;
256         }
257
258         public function getAllowedParams() {
259                 return array (
260                         'allrev' => false,
261                         'start' => array (
262                                 ApiBase :: PARAM_TYPE => 'timestamp'
263                         ),
264                         'end' => array (
265                                 ApiBase :: PARAM_TYPE => 'timestamp'
266                         ),
267                         'namespace' => array (
268                                 ApiBase :: PARAM_ISMULTI => true,
269                                 ApiBase :: PARAM_TYPE => 'namespace'
270                         ),
271                         'dir' => array (
272                                 ApiBase :: PARAM_DFLT => 'older',
273                                 ApiBase :: PARAM_TYPE => array (
274                                         'newer',
275                                         'older'
276                                 )
277                         ),
278                         'limit' => array (
279                                 ApiBase :: PARAM_DFLT => 10,
280                                 ApiBase :: PARAM_TYPE => 'limit',
281                                 ApiBase :: PARAM_MIN => 1,
282                                 ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1,
283                                 ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
284                         ),
285                         'prop' => array (
286                                 APIBase :: PARAM_ISMULTI => true,
287                                 APIBase :: PARAM_DFLT => 'ids|title|flags',
288                                 APIBase :: PARAM_TYPE => array (
289                                         'ids',
290                                         'title',
291                                         'flags',
292                                         'user',
293                                         'comment',
294                                         'timestamp',
295                                         'patrol',
296                                         'sizes',
297                                 )
298                         ),
299                         'show' => array (
300                                 ApiBase :: PARAM_ISMULTI => true,
301                                 ApiBase :: PARAM_TYPE => array (
302                                         'minor',
303                                         '!minor',
304                                         'bot',
305                                         '!bot',
306                                         'anon',
307                                         '!anon',
308                                         'patrolled',
309                                         '!patrolled',
310                                 )
311                         )
312                 );
313         }
314
315         public function getParamDescription() {
316                 return array (
317                         'allrev' => 'Include multiple revisions of the same page within given timeframe.',
318                         'start' => 'The timestamp to start enumerating from.',
319                         'end' => 'The timestamp to end enumerating.',
320                         'namespace' => 'Filter changes to only the given namespace(s).',
321                         'dir' => 'In which direction to enumerate pages.',
322                         'limit' => 'How many total results to return per request.',
323                         'prop' => 'Which additional items to get (non-generator mode only).',
324                         'show' => array (
325                                 'Show only items that meet this criteria.',
326                                 'For example, to see only minor edits done by logged-in users, set show=minor|!anon'
327                         )
328                 );
329         }
330
331         public function getDescription() {
332                 return "Get all recent changes to pages in the logged in user's watchlist";
333         }
334
335         protected function getExamples() {
336                 return array (
337                         'api.php?action=query&list=watchlist',
338                         'api.php?action=query&list=watchlist&wlprop=ids|title|timestamp|user|comment',
339                         'api.php?action=query&list=watchlist&wlallrev&wlprop=ids|title|timestamp|user|comment',
340                         'api.php?action=query&generator=watchlist&prop=info',
341                         'api.php?action=query&generator=watchlist&gwlallrev&prop=revisions&rvprop=timestamp|user'
342                 );
343         }
344
345         public function getVersion() {
346                 return __CLASS__ . ': $Id: ApiQueryWatchlist.php 47865 2009-02-27 16:03:01Z catrope $';
347         }
348 }