ab4a24088617ef7f86a60e4a15ebddea1d19264c
[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 ));
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_new AS page_is_new'
86 // 'rc_patrolled'
87
88 );
89 }
90 elseif ($allrev) {
91 $fields = array (
92 'rc_this_oldid AS rev_id',
93 'rc_namespace AS page_namespace',
94 'rc_title AS page_title',
95 'rc_timestamp AS rev_timestamp'
96 );
97 } else {
98 $fields = array (
99 'rc_cur_id AS page_id',
100 'rc_namespace AS page_namespace',
101 'rc_title AS page_title',
102 'rc_timestamp AS rev_timestamp'
103 );
104 }
105
106 $where = array (
107 'wl_namespace = rc_namespace',
108 'wl_title = rc_title',
109 'rc_cur_id = page_id',
110 'wl_user' => $wgUser->getID());
111
112 if (!$allrev)
113 $where[] = 'rc_this_oldid=page_latest';
114 if (isset ($namespace))
115 $where['wl_namespace'] = $namespace;
116
117 if (isset ($start))
118 $where[] = 'rc_timestamp' . $after . $db->addQuotes($start);
119
120 if (isset ($end))
121 $where[] = 'rc_timestamp' . $before . $db->addQuotes($end);
122
123 if (!isset ($start) && !isset ($end))
124 $where[] = "rc_timestamp > ''";
125
126 $this->profileDBIn();
127 $res = $db->select($tables, $fields, $where, __METHOD__, $options);
128 $this->profileDBOut();
129
130 $data = array ();
131 $count = 0;
132 while ($row = $db->fetchObject($res)) {
133 if (++ $count > $limit) {
134 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
135 $this->setContinueEnumParameter('from', $row->rev_timestamp);
136 break;
137 }
138
139 $title = Title :: makeTitle($row->page_namespace, $row->page_title);
140 // skip any pages that user has no rights to read
141 if ($title->userCanRead()) {
142
143 if (is_null($resultPageSet)) {
144 $id = intval($row->page_id);
145
146 $data[] = array (
147 'ns' => $title->getNamespace(), 'title' => $title->getPrefixedText(), 'id' => intval($row->page_id), 'comment' => $row->rev_comment, 'isuser' => $row->rev_user, 'user' => $row->rev_user_text, 'timestamp' => $row->rev_timestamp, 'minor' => $row->rev_minor_edit, 'rev_id' => $row->rev_id, 'rc_last_oldid' => $row->rc_last_oldid, 'rc_id' => $row->rc_id,
148 // 'rc_patrolled' => $row->rc_patrolled,
149 'isnew' => $row->page_is_new);
150 }
151 elseif ($allrev) {
152 $data[] = intval($row->rev_id);
153 } else {
154 $data[] = intval($row->page_id);
155 }
156 }
157 }
158 $db->freeResult($res);
159
160 if (is_null($resultPageSet)) {
161 ApiResult :: setIndexedTagName($data, 'p');
162 $this->getResult()->addValue('query', 'watchlist', $data);
163 }
164 elseif ($allrev) {
165 $resultPageSet->populateFromRevisionIDs($data);
166 } else {
167 $resultPageSet->populateFromPageIDs($data);
168 }
169 }
170
171 protected function getAllowedParams() {
172 $namespaces = $this->getQuery()->getValidNamespaces();
173 return array (
174 'allrev' => false,
175 'start' => array (
176 ApiBase :: PARAM_TYPE => 'timestamp'
177 ),
178 'end' => array (
179 ApiBase :: PARAM_TYPE => 'timestamp'
180 ),
181 'namespace' => array (
182 ApiBase :: PARAM_ISMULTI => true,
183 ApiBase :: PARAM_TYPE => $namespaces
184 ),
185 'dir' => array (
186 ApiBase :: PARAM_DFLT => 'older',
187 ApiBase :: PARAM_TYPE => array (
188 'newer',
189 'older'
190 )
191 ),
192 'limit' => array (
193 ApiBase :: PARAM_DFLT => 10,
194 ApiBase :: PARAM_TYPE => 'limit',
195 ApiBase :: PARAM_MIN => 1,
196 ApiBase :: PARAM_MAX1 => 500,
197 ApiBase :: PARAM_MAX2 => 5000
198 )
199 );
200 }
201
202 protected function getParamDescription() {
203 return array (
204 'allrev' => 'Include multiple revisions of the same page within given timeframe.',
205 'start' => 'The timestamp to start enumerating from.',
206 'end' => 'The timestamp to end enumerating.',
207 'namespace' => 'Filter changes to only the given namespace(s).',
208 'dir' => 'In which direction to enumerate pages.',
209 'limit' => 'How many total pages to return per request.'
210 );
211 }
212
213 protected function getDescription() {
214 return '';
215 }
216
217 protected function getExamples() {
218 return array (
219 'api.php?action=query&list=watchlist',
220 'api.php?action=query&list=watchlist&wlallrev',
221 'api.php?action=query&generator=watchlist&prop=info',
222 'api.php?action=query&generator=watchlist&gwlallrev&prop=revisions&rvprop=timestamp|user'
223 );
224 }
225
226 public function getVersion() {
227 return __CLASS__ . ': $Id$';
228 }
229 }
230 ?>