Revert "Remove unsupported and mostly non-functional Mssql support"
[lhc/web/wiklou.git] / includes / WatchedItem.php
1 <?php
2 /**
3 * Accessor and mutator for watchlist entries.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup Watchlist
22 */
23
24 /**
25 * Representation of a pair of user and title for watchlist entries.
26 *
27 * @ingroup Watchlist
28 */
29 class WatchedItem {
30 /**
31 * Constant to specify that user rights 'editmywatchlist' and
32 * 'viewmywatchlist' should not be checked.
33 * @since 1.22
34 */
35 const IGNORE_USER_RIGHTS = 0;
36
37 /**
38 * Constant to specify that user rights 'editmywatchlist' and
39 * 'viewmywatchlist' should be checked.
40 * @since 1.22
41 */
42 const CHECK_USER_RIGHTS = 1;
43
44 var $mTitle, $mUser, $mCheckRights;
45 private $loaded = false, $watched, $timestamp;
46
47 /**
48 * Create a WatchedItem object with the given user and title
49 * @since 1.22 $checkRights parameter added
50 * @param $user User: the user to use for (un)watching
51 * @param $title Title: the title we're going to (un)watch
52 * @param $checkRights int: Whether to check the 'viewmywatchlist' and 'editmywatchlist' rights.
53 * Pass either WatchedItem::IGNORE_USER_RIGHTS or WatchedItem::CHECK_USER_RIGHTS.
54 * @return WatchedItem object
55 */
56 public static function fromUserTitle( $user, $title, $checkRights = WatchedItem::CHECK_USER_RIGHTS ) {
57 $wl = new WatchedItem;
58 $wl->mUser = $user;
59 $wl->mTitle = $title;
60 $wl->mCheckRights = $checkRights;
61
62 return $wl;
63 }
64
65 /**
66 * Title being watched
67 * @return Title
68 */
69 protected function getTitle() {
70 return $this->mTitle;
71 }
72
73 /** Helper to retrieve the title namespace */
74 protected function getTitleNs() {
75 return $this->getTitle()->getNamespace();
76 }
77
78 /** Helper to retrieve the title DBkey */
79 protected function getTitleDBkey() {
80 return $this->getTitle()->getDBkey();
81 }
82 /** Helper to retrieve the user id */
83 protected function getUserId() {
84 return $this->mUser->getId();
85 }
86
87 /**
88 * Return an array of conditions to select or update the appropriate database
89 * row.
90 *
91 * @return array
92 */
93 private function dbCond() {
94 return array(
95 'wl_user' => $this->getUserId(),
96 'wl_namespace' => $this->getTitleNs(),
97 'wl_title' => $this->getTitleDBkey(),
98 );
99 }
100
101 /**
102 * Load the object from the database
103 */
104 private function load() {
105 if ( $this->loaded ) {
106 return;
107 }
108 $this->loaded = true;
109
110 // Only loggedin user can have a watchlist
111 if ( $this->mUser->isAnon() ) {
112 $this->watched = false;
113 return;
114 }
115
116 # Pages and their talk pages are considered equivalent for watching;
117 # remember that talk namespaces are numbered as page namespace+1.
118
119 $dbr = wfGetDB( DB_SLAVE );
120 $row = $dbr->selectRow( 'watchlist', 'wl_notificationtimestamp',
121 $this->dbCond(), __METHOD__ );
122
123 if ( $row === false ) {
124 $this->watched = false;
125 } else {
126 $this->watched = true;
127 $this->timestamp = $row->wl_notificationtimestamp;
128 }
129 }
130
131 /**
132 * Check permissions
133 * @param $what string: 'viewmywatchlist' or 'editmywatchlist'
134 */
135 private function isAllowed( $what ) {
136 return !$this->mCheckRights || $this->mUser->isAllowed( $what );
137 }
138
139 /**
140 * Is mTitle being watched by mUser?
141 * @return bool
142 */
143 public function isWatched() {
144 if ( !$this->isAllowed( 'viewmywatchlist' ) ) {
145 return false;
146 }
147
148 $this->load();
149 return $this->watched;
150 }
151
152 /**
153 * Get the notification timestamp of this entry.
154 *
155 * @return false|null|string: false if the page is not watched, the value of
156 * the wl_notificationtimestamp field otherwise
157 */
158 public function getNotificationTimestamp() {
159 if ( !$this->isAllowed( 'viewmywatchlist' ) ) {
160 return false;
161 }
162
163 $this->load();
164 if ( $this->watched ) {
165 return $this->timestamp;
166 } else {
167 return false;
168 }
169 }
170
171 /**
172 * Reset the notification timestamp of this entry
173 *
174 * @param $force Whether to force the write query to be executed even if the
175 * page is not watched or the notification timestamp is already NULL.
176 * @param int $oldid The revision id being viewed. If not given or 0, latest revision is assumed.
177 */
178 public function resetNotificationTimestamp( $force = '', $oldid = 0 ) {
179 // Only loggedin user can have a watchlist
180 if ( wfReadOnly() || $this->mUser->isAnon() || !$this->isAllowed( 'editmywatchlist' ) ) {
181 return;
182 }
183
184 if ( $force != 'force' ) {
185 $this->load();
186 if ( !$this->watched || $this->timestamp === null ) {
187 return;
188 }
189 }
190
191 $title = $this->getTitle();
192 if ( !$oldid ) {
193 // No oldid given, assuming latest revision; clear the timestamp.
194 $notificationTimestamp = null;
195 } elseif ( !$title->getNextRevisionID( $oldid ) ) {
196 // Oldid given and is the latest revision for this title; clear the timestamp.
197 $notificationTimestamp = null;
198 } else {
199 // See if the version marked as read is more recent than the one we're viewing.
200 // Call load() if it wasn't called before due to $force.
201 $this->load();
202
203 if ( $this->timestamp === null ) {
204 // This can only happen if $force is enabled.
205 $notificationTimestamp = null;
206 } else {
207 // Oldid given and isn't the latest; update the timestamp.
208 // This will result in no further notification emails being sent!
209 $dbr = wfGetDB( DB_SLAVE );
210 $notificationTimestamp = $dbr->selectField(
211 'revision', 'rev_timestamp',
212 array( 'rev_page' => $title->getArticleID(), 'rev_id' => $oldid )
213 );
214 // We need to go one second to the future because of various strict comparisons
215 // throughout the codebase
216 $ts = new MWTimestamp( $notificationTimestamp );
217 $ts->timestamp->add( new DateInterval( 'PT1S' ) );
218 $notificationTimestamp = $ts->getTimestamp( TS_MW );
219
220 if ( $notificationTimestamp < $this->timestamp ) {
221 if ( $force != 'force' ) {
222 return;
223 } else {
224 // This is a little silly…
225 $notificationTimestamp = $this->timestamp;
226 }
227 }
228 }
229 }
230
231 // If the page is watched by the user (or may be watched), update the timestamp on any
232 // any matching rows
233 $dbw = wfGetDB( DB_MASTER );
234 $dbw->update( 'watchlist', array( 'wl_notificationtimestamp' => $notificationTimestamp ),
235 $this->dbCond(), __METHOD__ );
236 $this->timestamp = null;
237 }
238
239 /**
240 * Given a title and user (assumes the object is setup), add the watch to the
241 * database.
242 * @return bool
243 */
244 public function addWatch() {
245 wfProfileIn( __METHOD__ );
246
247 // Only loggedin user can have a watchlist
248 if ( wfReadOnly() || $this->mUser->isAnon() || !$this->isAllowed( 'editmywatchlist' ) ) {
249 wfProfileOut( __METHOD__ );
250 return false;
251 }
252
253 // Use INSERT IGNORE to avoid overwriting the notification timestamp
254 // if there's already an entry for this page
255 $dbw = wfGetDB( DB_MASTER );
256 $dbw->insert( 'watchlist',
257 array(
258 'wl_user' => $this->getUserId(),
259 'wl_namespace' => MWNamespace::getSubject( $this->getTitleNs() ),
260 'wl_title' => $this->getTitleDBkey(),
261 'wl_notificationtimestamp' => null
262 ), __METHOD__, 'IGNORE' );
263
264 // Every single watched page needs now to be listed in watchlist;
265 // namespace:page and namespace_talk:page need separate entries:
266 $dbw->insert( 'watchlist',
267 array(
268 'wl_user' => $this->getUserId(),
269 'wl_namespace' => MWNamespace::getTalk( $this->getTitleNs() ),
270 'wl_title' => $this->getTitleDBkey(),
271 'wl_notificationtimestamp' => null
272 ), __METHOD__, 'IGNORE' );
273
274 $this->watched = true;
275
276 wfProfileOut( __METHOD__ );
277 return true;
278 }
279
280 /**
281 * Same as addWatch, only the opposite.
282 * @return bool
283 */
284 public function removeWatch() {
285 wfProfileIn( __METHOD__ );
286
287 // Only loggedin user can have a watchlist
288 if ( wfReadOnly() || $this->mUser->isAnon() || !$this->isAllowed( 'editmywatchlist' ) ) {
289 wfProfileOut( __METHOD__ );
290 return false;
291 }
292
293 $success = false;
294 $dbw = wfGetDB( DB_MASTER );
295 $dbw->delete( 'watchlist',
296 array(
297 'wl_user' => $this->getUserId(),
298 'wl_namespace' => MWNamespace::getSubject( $this->getTitleNs() ),
299 'wl_title' => $this->getTitleDBkey(),
300 ), __METHOD__
301 );
302 if ( $dbw->affectedRows() ) {
303 $success = true;
304 }
305
306 # the following code compensates the new behavior, introduced by the
307 # enotif patch, that every single watched page needs now to be listed
308 # in watchlist namespace:page and namespace_talk:page had separate
309 # entries: clear them
310 $dbw->delete( 'watchlist',
311 array(
312 'wl_user' => $this->getUserId(),
313 'wl_namespace' => MWNamespace::getTalk( $this->getTitleNs() ),
314 'wl_title' => $this->getTitleDBkey(),
315 ), __METHOD__
316 );
317
318 if ( $dbw->affectedRows() ) {
319 $success = true;
320 }
321
322 $this->watched = false;
323
324 wfProfileOut( __METHOD__ );
325 return $success;
326 }
327
328 /**
329 * Check if the given title already is watched by the user, and if so
330 * add watches on a new title. To be used for page renames and such.
331 *
332 * @param $ot Title: page title to duplicate entries from, if present
333 * @param $nt Title: page title to add watches on
334 */
335 public static function duplicateEntries( $ot, $nt ) {
336 WatchedItem::doDuplicateEntries( $ot->getSubjectPage(), $nt->getSubjectPage() );
337 WatchedItem::doDuplicateEntries( $ot->getTalkPage(), $nt->getTalkPage() );
338 }
339
340 /**
341 * Handle duplicate entries. Backend for duplicateEntries().
342 *
343 * @param $ot Title
344 * @param $nt Title
345 *
346 * @return bool
347 */
348 private static function doDuplicateEntries( $ot, $nt ) {
349 $oldnamespace = $ot->getNamespace();
350 $newnamespace = $nt->getNamespace();
351 $oldtitle = $ot->getDBkey();
352 $newtitle = $nt->getDBkey();
353
354 $dbw = wfGetDB( DB_MASTER );
355 $res = $dbw->select( 'watchlist', 'wl_user',
356 array( 'wl_namespace' => $oldnamespace, 'wl_title' => $oldtitle ),
357 __METHOD__, 'FOR UPDATE'
358 );
359 # Construct array to replace into the watchlist
360 $values = array();
361 foreach ( $res as $s ) {
362 $values[] = array(
363 'wl_user' => $s->wl_user,
364 'wl_namespace' => $newnamespace,
365 'wl_title' => $newtitle
366 );
367 }
368
369 if ( empty( $values ) ) {
370 // Nothing to do
371 return true;
372 }
373
374 # Perform replace
375 # Note that multi-row replace is very efficient for MySQL but may be inefficient for
376 # some other DBMSes, mostly due to poor simulation by us
377 $dbw->replace( 'watchlist', array( array( 'wl_user', 'wl_namespace', 'wl_title' ) ), $values, __METHOD__ );
378 return true;
379 }
380 }