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