* API query optimizations
[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 if (is_null($resultPageSet)) {
72 $fields = array (
73 'rc_namespace AS page_namespace',
74 'rc_title AS page_title',
75 'rc_comment AS rev_comment',
76 'rc_cur_id AS page_id',
77 'rc_user AS rev_user',
78 'rc_user_text AS rev_user_text',
79 'rc_timestamp AS rev_timestamp',
80 'rc_minor AS rev_minor_edit',
81 'rc_this_oldid AS rev_id',
82 'rc_last_oldid',
83 'rc_id',
84 // 'rc_patrolled',
85 'rc_new AS page_is_new'
86 );
87 } elseif ($allrev) {
88 $fields = array (
89 'rc_this_oldid AS rev_id',
90 'rc_namespace AS page_namespace',
91 'rc_title AS page_title',
92 'rc_timestamp AS rev_timestamp'
93 );
94 } else {
95 $fields = array (
96 'rc_cur_id AS page_id',
97 'rc_namespace AS page_namespace',
98 'rc_title AS page_title',
99 'rc_timestamp AS rev_timestamp'
100 );
101 }
102
103 $where = array (
104 'wl_namespace = rc_namespace',
105 'wl_title = rc_title',
106 'rc_cur_id = page_id',
107 'wl_user' => $wgUser->getID());
108
109 if (!$allrev)
110 $where[] = 'rc_this_oldid=page_latest';
111 if (isset ($namespace))
112 $where['wl_namespace'] = $namespace;
113
114 if (isset ($start))
115 $where[] = 'rc_timestamp' . $after . $db->addQuotes($start);
116
117 if (isset ($end))
118 $where[] = 'rc_timestamp' . $before . $db->addQuotes($end);
119
120 if (!isset ($start) && !isset ($end))
121 $where[] = "rc_timestamp > ''";
122
123 $this->profileDBIn();
124 $res = $db->select($tables, $fields, $where, __METHOD__, $options);
125 $this->profileDBOut();
126
127 $data = array ();
128 $count = 0;
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 $msg = array (
133 'continue' => $this->encodeParamName('from'
134 ) . '=' . $row->rev_timestamp);
135 $this->getResult()->addValue('query-status', 'watchlist', $msg);
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(),
148 'title' => $title->getPrefixedText(),
149 'id' => intval($row->page_id),
150 'comment' => $row->rev_comment,
151 'isuser' => $row->rev_user,
152 'user' => $row->rev_user_text,
153 'timestamp' => $row->rev_timestamp,
154 'minor' => $row->rev_minor_edit,
155 'rev_id' => $row->rev_id,
156 'rc_last_oldid' => $row->rc_last_oldid,
157 'rc_id' => $row->rc_id,
158 // 'rc_patrolled' => $row->rc_patrolled,
159 'isnew' => $row->page_is_new
160 );
161 } elseif ($allrev) {
162 $data[] = intval($row->rev_id);
163 } else {
164 $data[] = intval($row->page_id);
165 }
166 }
167 }
168 $db->freeResult($res);
169
170 if (is_null($resultPageSet)) {
171 ApiResult :: setIndexedTagName($data, 'p');
172 $this->getResult()->addValue('query', 'watchlist', $data);
173 } elseif ($allrev) {
174 $resultPageSet->populateFromRevisionIDs($data);
175 } else {
176 $resultPageSet->populateFromPageIDs($data);
177 }
178 }
179
180 protected function getAllowedParams() {
181 $namespaces = $this->getQuery()->getValidNamespaces();
182 return array (
183 'allrev' => false,
184 'start' => array (
185 ApiBase :: PARAM_TYPE => 'timestamp'
186 ),
187 'end' => array (
188 ApiBase :: PARAM_TYPE => 'timestamp'
189 ),
190 'namespace' => array (
191 ApiBase :: PARAM_ISMULTI => true,
192 ApiBase :: PARAM_TYPE => $namespaces
193 ),
194 'dir' => array (
195 ApiBase :: PARAM_DFLT => 'older',
196 ApiBase :: PARAM_TYPE => array (
197 'newer',
198 'older'
199 )
200 ),
201 'limit' => array (
202 ApiBase :: PARAM_DFLT => 10,
203 ApiBase :: PARAM_TYPE => 'limit',
204 ApiBase :: PARAM_MIN => 1,
205 ApiBase :: PARAM_MAX1 => 500,
206 ApiBase :: PARAM_MAX2 => 5000
207 )
208 );
209 }
210
211 protected function getParamDescription() {
212 return array (
213 'allrev' => 'Include multiple revisions of the same page within given timeframe',
214 'start' => 'The timestamp to start enumerating from.',
215 'end' => 'The timestamp to end enumerating.',
216 'namespace' => 'Filter changes to only the given namespace(s).',
217 'dir' => 'In which direction to enumerate pages "older" (default), or "newer")',
218 'limit' => 'How many total pages to return per request'
219 );
220 }
221
222 protected function getDescription() {
223 return '';
224 }
225
226 protected function getExamples() {
227 return array (
228 'api.php?action=query&list=watchlist',
229 'api.php?action=query&list=watchlist&wlallrev',
230 'api.php?action=query&generator=watchlist&prop=info',
231 'api.php?action=query&generator=watchlist&gwlallrev&prop=revisions&rvprop=timestamp|user'
232 );
233 }
234
235 public function getVersion() {
236 return __CLASS__ . ': $Id$';
237 }
238 }
239 ?>