thumb_handler.php doesn't seem to extract path_info correctly
[mediawiki.git] / includes / WatchedItem.php
blob45aa82266e681b7598210e3639b0b0233c287522
1 <?php
2 /**
3 * Accessor and mutator for watchlist entries.
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.
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.
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
20 * @file
21 * @ingroup Watchlist
24 /**
25 * Representation of a pair of user and title for watchlist entries.
27 * @ingroup Watchlist
29 class WatchedItem {
30 var $mTitle, $mUser;
31 private $loaded = false, $watched, $timestamp;
33 /**
34 * Create a WatchedItem object with the given user and title
35 * @param $user User: the user to use for (un)watching
36 * @param $title Title: the title we're going to (un)watch
37 * @return WatchedItem object
39 public static function fromUserTitle( $user, $title ) {
40 $wl = new WatchedItem;
41 $wl->mUser = $user;
42 $wl->mTitle = $title;
44 return $wl;
47 /**
48 * Title being watched
49 * @return Title
51 protected function getTitle() {
52 return $this->mTitle;
55 /** Helper to retrieve the title namespace */
56 protected function getTitleNs() {
57 return $this->getTitle()->getNamespace();
60 /** Helper to retrieve the title DBkey */
61 protected function getTitleDBkey() {
62 return $this->getTitle()->getDBkey();
64 /** Helper to retrieve the user id */
65 protected function getUserId() {
66 return $this->mUser->getId();
69 /**
70 * Return an array of conditions to select or update the appropriate database
71 * row.
73 * @return array
75 private function dbCond() {
76 return array(
77 'wl_user' => $this->getUserId(),
78 'wl_namespace' => $this->getTitleNs(),
79 'wl_title' => $this->getTitleDBkey(),
83 /**
84 * Load the object from the database
86 private function load() {
87 if ( $this->loaded ) {
88 return;
90 $this->loaded = true;
92 // Only loggedin user can have a watchlist
93 if ( $this->mUser->isAnon() ) {
94 $this->watched = false;
95 return;
98 # Pages and their talk pages are considered equivalent for watching;
99 # remember that talk namespaces are numbered as page namespace+1.
101 $dbr = wfGetDB( DB_SLAVE );
102 $row = $dbr->selectRow( 'watchlist', 'wl_notificationtimestamp',
103 $this->dbCond(), __METHOD__ );
105 if ( $row === false ) {
106 $this->watched = false;
107 } else {
108 $this->watched = true;
109 $this->timestamp = $row->wl_notificationtimestamp;
114 * Is mTitle being watched by mUser?
115 * @return bool
117 public function isWatched() {
118 $this->load();
119 return $this->watched;
123 * Get the notification timestamp of this entry.
125 * @return false|null|string: false if the page is not watched, the value of
126 * the wl_notificationtimestamp field otherwise
128 public function getNotificationTimestamp() {
129 $this->load();
130 if ( $this->watched ) {
131 return $this->timestamp;
132 } else {
133 return false;
138 * Reset the notification timestamp of this entry
140 * @param $force Whether to force the write query to be executed even if the
141 * page is not watched or the notification timestamp is already NULL.
143 public function resetNotificationTimestamp( $force = '' ) {
144 // Only loggedin user can have a watchlist
145 if ( wfReadOnly() || $this->mUser->isAnon() ) {
146 return;
149 if ( $force != 'force' ) {
150 $this->load();
151 if ( !$this->watched || $this->timestamp === null ) {
152 return;
156 // If the page is watched by the user (or may be watched), update the timestamp on any
157 // any matching rows
158 $dbw = wfGetDB( DB_MASTER );
159 $dbw->update( 'watchlist', array( 'wl_notificationtimestamp' => null ),
160 $this->dbCond(), __METHOD__ );
161 $this->timestamp = null;
165 * Given a title and user (assumes the object is setup), add the watch to the
166 * database.
167 * @return bool
169 public function addWatch() {
170 wfProfileIn( __METHOD__ );
172 // Only loggedin user can have a watchlist
173 if ( wfReadOnly() || $this->mUser->isAnon() ) {
174 wfProfileOut( __METHOD__ );
175 return false;
178 // Use INSERT IGNORE to avoid overwriting the notification timestamp
179 // if there's already an entry for this page
180 $dbw = wfGetDB( DB_MASTER );
181 $dbw->insert( 'watchlist',
182 array(
183 'wl_user' => $this->getUserId(),
184 'wl_namespace' => MWNamespace::getSubject( $this->getTitleNs() ),
185 'wl_title' => $this->getTitleDBkey(),
186 'wl_notificationtimestamp' => null
187 ), __METHOD__, 'IGNORE' );
189 // Every single watched page needs now to be listed in watchlist;
190 // namespace:page and namespace_talk:page need separate entries:
191 $dbw->insert( 'watchlist',
192 array(
193 'wl_user' => $this->getUserId(),
194 'wl_namespace' => MWNamespace::getTalk( $this->getTitleNs() ),
195 'wl_title' => $this->getTitleDBkey(),
196 'wl_notificationtimestamp' => null
197 ), __METHOD__, 'IGNORE' );
199 $this->watched = true;
201 wfProfileOut( __METHOD__ );
202 return true;
206 * Same as addWatch, only the opposite.
207 * @return bool
209 public function removeWatch() {
210 wfProfileIn( __METHOD__ );
212 // Only loggedin user can have a watchlist
213 if ( wfReadOnly() || $this->mUser->isAnon() ) {
214 wfProfileOut( __METHOD__ );
215 return false;
218 $success = false;
219 $dbw = wfGetDB( DB_MASTER );
220 $dbw->delete( 'watchlist',
221 array(
222 'wl_user' => $this->getUserId(),
223 'wl_namespace' => MWNamespace::getSubject( $this->getTitleNs() ),
224 'wl_title' => $this->getTitleDBkey(),
225 ), __METHOD__
227 if ( $dbw->affectedRows() ) {
228 $success = true;
231 # the following code compensates the new behavior, introduced by the
232 # enotif patch, that every single watched page needs now to be listed
233 # in watchlist namespace:page and namespace_talk:page had separate
234 # entries: clear them
235 $dbw->delete( 'watchlist',
236 array(
237 'wl_user' => $this->getUserId(),
238 'wl_namespace' => MWNamespace::getTalk( $this->getTitleNs() ),
239 'wl_title' => $this->getTitleDBkey(),
240 ), __METHOD__
243 if ( $dbw->affectedRows() ) {
244 $success = true;
247 $this->watched = false;
249 wfProfileOut( __METHOD__ );
250 return $success;
254 * Check if the given title already is watched by the user, and if so
255 * add watches on a new title. To be used for page renames and such.
257 * @param $ot Title: page title to duplicate entries from, if present
258 * @param $nt Title: page title to add watches on
260 public static function duplicateEntries( $ot, $nt ) {
261 WatchedItem::doDuplicateEntries( $ot->getSubjectPage(), $nt->getSubjectPage() );
262 WatchedItem::doDuplicateEntries( $ot->getTalkPage(), $nt->getTalkPage() );
266 * Handle duplicate entries. Backend for duplicateEntries().
268 * @param $ot Title
269 * @param $nt Title
271 * @return bool
273 private static function doDuplicateEntries( $ot, $nt ) {
274 $oldnamespace = $ot->getNamespace();
275 $newnamespace = $nt->getNamespace();
276 $oldtitle = $ot->getDBkey();
277 $newtitle = $nt->getDBkey();
279 $dbw = wfGetDB( DB_MASTER );
280 $res = $dbw->select( 'watchlist', 'wl_user',
281 array( 'wl_namespace' => $oldnamespace, 'wl_title' => $oldtitle ),
282 __METHOD__, 'FOR UPDATE'
284 # Construct array to replace into the watchlist
285 $values = array();
286 foreach ( $res as $s ) {
287 $values[] = array(
288 'wl_user' => $s->wl_user,
289 'wl_namespace' => $newnamespace,
290 'wl_title' => $newtitle
294 if ( empty( $values ) ) {
295 // Nothing to do
296 return true;
299 # Perform replace
300 # Note that multi-row replace is very efficient for MySQL but may be inefficient for
301 # some other DBMSes, mostly due to poor simulation by us
302 $dbw->replace( 'watchlist', array( array( 'wl_user', 'wl_namespace', 'wl_title' ) ), $values, __METHOD__ );
303 return true;