* API: added watchlist module (incomplete)
[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 = null;
53 extract($this->extractRequestParams());
54
55 $db = $this->getDB();
56
57 $tables = array (
58 'watchlist',
59 'recentchanges',
60 'page'
61 );
62 $fields = array (
63 'rc_namespace AS page_namespace',
64 'rc_title AS page_title',
65 'rc_comment AS rev_comment',
66 'rc_cur_id AS page_id',
67 'rc_user AS rev_user',
68 'rc_user_text AS rev_user_text',
69 'rc_timestamp AS rev_timestamp',
70 'rc_minor AS rev_minor_edit',
71 'rc_this_oldid AS rev_id',
72 'rc_last_oldid',
73 'rc_id',
74 // 'rc_patrolled',
75 'rc_new AS page_is_new'
76 );
77
78 $dirNewer = ($dir === 'newer');
79 $before = ($dirNewer ? '<=' : '>=');
80 $after = ($dirNewer ? '>=' : '<=');
81
82 $where = array (
83 'wl_namespace = rc_namespace',
84 'wl_title = rc_title',
85 'rc_cur_id = page_id',
86 'wl_user' => $wgUser->getID());
87
88 if (!$allrev)
89 $where[] = 'rc_this_oldid=page_latest';
90 if (isset ($namespace))
91 $where['wl_namespace'] = $namespace;
92 if (isset ($start))
93 $where[] = 'rev_timestamp' . $after . $db->addQuotes($start);
94 if (isset ($end))
95 $where[] = 'rev_timestamp' . $before . $db->addQuotes($end);
96
97 // if (is_null($resultPageSet)) {
98 // $fields = array (
99 // 'page_id',
100 // 'page_namespace',
101 // 'page_title'
102 // );
103 // } else {
104 // $fields = $resultPageSet->getPageTableFields();
105 // }
106
107 $options = array (
108 'LIMIT' => $limit +1,
109 'ORDER BY' => 'rc_timestamp' . ($dirNewer ? '' : ' DESC'
110 ));
111
112 $this->profileDBIn();
113 $res = $db->select($tables, $fields, $where, __METHOD__, $options);
114 $this->profileDBOut();
115
116 $data = array ();
117 $count = 0;
118 while ($row = $db->fetchObject($res)) {
119 if (++ $count > $limit) {
120 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
121 $msg = array (
122 'continue' => $this->encodeParamName('from'
123 ) . '=' . $row->rev_timestamp);
124 $this->getResult()->addValue('query-status', 'watchlist', $msg);
125 break;
126 }
127
128 $title = Title :: makeTitle($row->page_namespace, $row->page_title);
129 // skip any pages that user has no rights to read
130 if ($title->userCanRead()) {
131
132 if (is_null($resultPageSet)) {
133 $id = intval($row->page_id);
134
135 $data[] = array (
136 'ns' => $title->getNamespace(),
137 'title' => $title->getPrefixedText(),
138 'id' => intval($row->page_id),
139 'comment' => $row->rev_comment,
140 'isuser' => $row->rev_user,
141 'user' => $row->rev_user_text,
142 'timestamp' => $row->rev_timestamp,
143 'minor' => $row->rev_minor_edit,
144 'rev_id' => $row->rev_id,
145 'rc_last_oldid' => $row->rc_last_oldid,
146 'rc_id' => $row->rc_id,
147 // 'rc_patrolled' => $row->rc_patrolled,
148 'isnew' => $row->page_is_new
149 );
150 } else {
151 // $resultPageSet->processDbRow($row);
152 }
153 }
154 }
155 $db->freeResult($res);
156
157 if (is_null($resultPageSet)) {
158 ApiResult :: setIndexedTagName($data, 'p');
159 $this->getResult()->addValue('query', 'watchlist', $data);
160 }
161 }
162
163 protected function getAllowedParams() {
164 $namespaces = $this->getQuery()->getValidNamespaces();
165 return array (
166 'allrev' => false,
167 'start' => array (
168 ApiBase :: PARAM_TYPE => 'timestamp'
169 ),
170 'end' => array (
171 ApiBase :: PARAM_TYPE => 'timestamp'
172 ),
173 'namespace' => array (
174 ApiBase :: PARAM_ISMULTI => true,
175 ApiBase :: PARAM_TYPE => $namespaces
176 ),
177 'dir' => array (
178 ApiBase :: PARAM_DFLT => 'older',
179 ApiBase :: PARAM_TYPE => array (
180 'newer',
181 'older'
182 )
183 ),
184 'limit' => array (
185 ApiBase :: PARAM_DFLT => 10,
186 ApiBase :: PARAM_TYPE => 'limit',
187 ApiBase :: PARAM_MIN => 1,
188 ApiBase :: PARAM_MAX1 => 500,
189 ApiBase :: PARAM_MAX2 => 5000
190 )
191 );
192 }
193
194 protected function getParamDescription() {
195 return array (
196 'allrev' => 'Include multiple revisions of the same page within given timeframe',
197 'start' => 'The timestamp to start enumerating from.',
198 'end' => 'The timestamp to end enumerating.',
199 'namespace' => 'Filter changes to only the given namespace(s).',
200 'dir' => 'In which direction to enumerate pages "older" (default), or "newer")',
201 'limit' => 'How many total pages to return per request'
202 );
203 }
204
205 protected function getDescription() {
206 return '';
207 }
208
209 protected function getExamples() {
210 return array (
211 'api.php?action=query&list=watchlist',
212 'api.php?action=query&list=watchlist&wlallrev'
213 );
214 }
215
216 public function getVersion() {
217 return __CLASS__ . ': $Id$';
218 }
219 }
220 ?>