Fixed spacing in actions/cache/filebackend/filerepo/job folder
[mediawiki.git] / includes / WatchedItem.php
blob5ac92f73562c41d04ca72598958cc8474c5f7eac
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 # Pages and their talk pages are considered equivalent for watching;
93 # remember that talk namespaces are numbered as page namespace+1.
95 $dbr = wfGetDB( DB_SLAVE );
96 $row = $dbr->selectRow( 'watchlist', 'wl_notificationtimestamp',
97 $this->dbCond(), __METHOD__ );
99 if ( $row === false ) {
100 $this->watched = false;
101 } else {
102 $this->watched = true;
103 $this->timestamp = $row->wl_notificationtimestamp;
108 * Is mTitle being watched by mUser?
109 * @return bool
111 public function isWatched() {
112 $this->load();
113 return $this->watched;
117 * Get the notification timestamp of this entry.
119 * @return false|null|string: false if the page is not watched, the value of
120 * the wl_notificationtimestamp field otherwise
122 public function getNotificationTimestamp() {
123 $this->load();
124 if ( $this->watched ) {
125 return $this->timestamp;
126 } else {
127 return false;
132 * Reset the notification timestamp of this entry
134 * @param $force Whether to force the write query to be executed even if the
135 * page is not watched or the notification timestamp is already NULL.
137 public function resetNotificationTimestamp( $force = '' ) {
138 if ( $force != 'force' ) {
139 $this->load();
140 if ( !$this->watched || $this->timestamp === null ) {
141 return;
145 // If the page is watched by the user (or may be watched), update the timestamp on any
146 // any matching rows
147 $dbw = wfGetDB( DB_MASTER );
148 $dbw->update( 'watchlist', array( 'wl_notificationtimestamp' => null ),
149 $this->dbCond(), __METHOD__ );
150 $this->timestamp = null;
154 * Given a title and user (assumes the object is setup), add the watch to the
155 * database.
156 * @return bool (always true)
158 public function addWatch() {
159 wfProfileIn( __METHOD__ );
161 // Use INSERT IGNORE to avoid overwriting the notification timestamp
162 // if there's already an entry for this page
163 $dbw = wfGetDB( DB_MASTER );
164 $dbw->insert( 'watchlist',
165 array(
166 'wl_user' => $this->getUserId(),
167 'wl_namespace' => MWNamespace::getSubject( $this->getTitleNs() ),
168 'wl_title' => $this->getTitleDBkey(),
169 'wl_notificationtimestamp' => null
170 ), __METHOD__, 'IGNORE' );
172 // Every single watched page needs now to be listed in watchlist;
173 // namespace:page and namespace_talk:page need separate entries:
174 $dbw->insert( 'watchlist',
175 array(
176 'wl_user' => $this->getUserId(),
177 'wl_namespace' => MWNamespace::getTalk( $this->getTitleNs() ),
178 'wl_title' => $this->getTitleDBkey(),
179 'wl_notificationtimestamp' => null
180 ), __METHOD__, 'IGNORE' );
182 $this->watched = true;
184 wfProfileOut( __METHOD__ );
185 return true;
189 * Same as addWatch, only the opposite.
190 * @return bool
192 public function removeWatch() {
193 wfProfileIn( __METHOD__ );
195 $success = false;
196 $dbw = wfGetDB( DB_MASTER );
197 $dbw->delete( 'watchlist',
198 array(
199 'wl_user' => $this->getUserId(),
200 'wl_namespace' => MWNamespace::getSubject( $this->getTitleNs() ),
201 'wl_title' => $this->getTitleDBkey(),
202 ), __METHOD__
204 if ( $dbw->affectedRows() ) {
205 $success = true;
208 # the following code compensates the new behavior, introduced by the
209 # enotif patch, that every single watched page needs now to be listed
210 # in watchlist namespace:page and namespace_talk:page had separate
211 # entries: clear them
212 $dbw->delete( 'watchlist',
213 array(
214 'wl_user' => $this->getUserId(),
215 'wl_namespace' => MWNamespace::getTalk( $this->getTitleNs() ),
216 'wl_title' => $this->getTitleDBkey(),
217 ), __METHOD__
220 if ( $dbw->affectedRows() ) {
221 $success = true;
224 $this->watched = false;
226 wfProfileOut( __METHOD__ );
227 return $success;
231 * Check if the given title already is watched by the user, and if so
232 * add watches on a new title. To be used for page renames and such.
234 * @param $ot Title: page title to duplicate entries from, if present
235 * @param $nt Title: page title to add watches on
237 public static function duplicateEntries( $ot, $nt ) {
238 WatchedItem::doDuplicateEntries( $ot->getSubjectPage(), $nt->getSubjectPage() );
239 WatchedItem::doDuplicateEntries( $ot->getTalkPage(), $nt->getTalkPage() );
243 * Handle duplicate entries. Backend for duplicateEntries().
245 * @param $ot Title
246 * @param $nt Title
248 * @return bool
250 private static function doDuplicateEntries( $ot, $nt ) {
251 $oldnamespace = $ot->getNamespace();
252 $newnamespace = $nt->getNamespace();
253 $oldtitle = $ot->getDBkey();
254 $newtitle = $nt->getDBkey();
256 $dbw = wfGetDB( DB_MASTER );
257 $res = $dbw->select( 'watchlist', 'wl_user',
258 array( 'wl_namespace' => $oldnamespace, 'wl_title' => $oldtitle ),
259 __METHOD__, 'FOR UPDATE'
261 # Construct array to replace into the watchlist
262 $values = array();
263 foreach ( $res as $s ) {
264 $values[] = array(
265 'wl_user' => $s->wl_user,
266 'wl_namespace' => $newnamespace,
267 'wl_title' => $newtitle
271 if( empty( $values ) ) {
272 // Nothing to do
273 return true;
276 # Perform replace
277 # Note that multi-row replace is very efficient for MySQL but may be inefficient for
278 # some other DBMSes, mostly due to poor simulation by us
279 $dbw->replace( 'watchlist', array( array( 'wl_user', 'wl_namespace', 'wl_title' ) ), $values, __METHOD__ );
280 return true;