* Don't issue a write query to the database if the wl_notificationtimestamp is alread...
[lhc/web/wiklou.git] / includes / WatchedItem.php
1 <?php
2 /**
3 * @file
4 * @ingroup Watchlist
5 */
6
7 /**
8 * @ingroup Watchlist
9 */
10 class WatchedItem {
11 var $mTitle, $mUser, $id, $ns, $ti;
12 private $loaded = false, $watched, $timestamp;
13
14 /**
15 * Create a WatchedItem object with the given user and title
16 * @param $user User: the user to use for (un)watching
17 * @param $title Title: the title we're going to (un)watch
18 * @return WatchedItem object
19 */
20 public static function fromUserTitle( $user, $title ) {
21 $wl = new WatchedItem;
22 $wl->mUser = $user;
23 $wl->mTitle = $title;
24 $wl->id = $user->getId();
25 # Patch (also) for email notification on page changes T.Gries/M.Arndt 11.09.2004
26 # TG patch: here we do not consider pages and their talk pages equivalent - why should we ?
27 # The change results in talk-pages not automatically included in watchlists, when their parent page is included
28 # $wl->ns = $title->getNamespace() & ~1;
29 $wl->ns = $title->getNamespace();
30
31 $wl->ti = $title->getDBkey();
32 return $wl;
33 }
34
35 /**
36 * Return an array of conditions to select or update the appropriate database
37 * row.
38 *
39 * @return array
40 */
41 private function dbCond() {
42 return array( 'wl_user' => $this->id, 'wl_namespace' => $this->ns, 'wl_title' => $this->ti );
43 }
44
45 /**
46 * Load the object from the database
47 */
48 private function load() {
49 if ( $this->loaded ) {
50 return;
51 }
52 $this->loaded = true;
53
54 # Pages and their talk pages are considered equivalent for watching;
55 # remember that talk namespaces are numbered as page namespace+1.
56
57 $dbr = wfGetDB( DB_SLAVE );
58 $row = $dbr->selectRow( 'watchlist', 'wl_notificationtimestamp',
59 $this->dbCond(), __METHOD__ );
60
61 if ( $row === false ) {
62 $this->watched = false;
63 } else {
64 $this->watched = true;
65 $this->timestamp = $row->wl_notificationtimestamp;
66 }
67 }
68
69 /**
70 * Is mTitle being watched by mUser?
71 * @return bool
72 */
73 public function isWatched() {
74 $this->load();
75 return $this->watched;
76 }
77
78 /**
79 * Get the notification timestamp of this entry.
80 *
81 * @return false|null|string: false if the page is not watched, the value of
82 * the wl_notificationtimestamp field otherwise
83 */
84 public function getNotificationTimestamp() {
85 $this->load();
86 if ( $this->watched ) {
87 return $this->timestamp;
88 } else {
89 return false;
90 }
91 }
92
93 /**
94 * Reset the notification timestamp of this entry
95 *
96 * @param $force Whether to force the write query to be executed even if the
97 * page is not watched or the notification timestamp is already NULL.
98 */
99 public function resetNotificationTimestamp( $force = '' ) {
100 if ( $force != 'force' ) {
101 $this->load();
102 if ( !$this->watched || $this->timestamp === null ) {
103 return;
104 }
105 }
106
107 // If the page is watched by the user (or may be watched), update the timestamp on any
108 // any matching rows
109 $dbw = wfGetDB( DB_MASTER );
110 $dbw->update( 'watchlist', array( 'wl_notificationtimestamp' => null ),
111 $this->dbCond(), __METHOD__ );
112 $this->timestamp = null;
113 }
114
115 /**
116 * Given a title and user (assumes the object is setup), add the watch to the
117 * database.
118 * @return bool (always true)
119 */
120 public function addWatch() {
121 wfProfileIn( __METHOD__ );
122
123 // Use INSERT IGNORE to avoid overwriting the notification timestamp
124 // if there's already an entry for this page
125 $dbw = wfGetDB( DB_MASTER );
126 $dbw->insert( 'watchlist',
127 array(
128 'wl_user' => $this->id,
129 'wl_namespace' => MWNamespace::getSubject($this->ns),
130 'wl_title' => $this->ti,
131 'wl_notificationtimestamp' => null
132 ), __METHOD__, 'IGNORE' );
133
134 // Every single watched page needs now to be listed in watchlist;
135 // namespace:page and namespace_talk:page need separate entries:
136 $dbw->insert( 'watchlist',
137 array(
138 'wl_user' => $this->id,
139 'wl_namespace' => MWNamespace::getTalk($this->ns),
140 'wl_title' => $this->ti,
141 'wl_notificationtimestamp' => null
142 ), __METHOD__, 'IGNORE' );
143
144 $this->watched = true;
145
146 wfProfileOut( __METHOD__ );
147 return true;
148 }
149
150 /**
151 * Same as addWatch, only the opposite.
152 * @return bool
153 */
154 public function removeWatch() {
155 wfProfileIn( __METHOD__ );
156
157 $success = false;
158 $dbw = wfGetDB( DB_MASTER );
159 $dbw->delete( 'watchlist',
160 array(
161 'wl_user' => $this->id,
162 'wl_namespace' => MWNamespace::getSubject($this->ns),
163 'wl_title' => $this->ti
164 ), __METHOD__
165 );
166 if ( $dbw->affectedRows() ) {
167 $success = true;
168 }
169
170 # the following code compensates the new behaviour, introduced by the
171 # enotif patch, that every single watched page needs now to be listed
172 # in watchlist namespace:page and namespace_talk:page had separate
173 # entries: clear them
174 $dbw->delete( 'watchlist',
175 array(
176 'wl_user' => $this->id,
177 'wl_namespace' => MWNamespace::getTalk($this->ns),
178 'wl_title' => $this->ti
179 ), __METHOD__
180 );
181
182 if ( $dbw->affectedRows() ) {
183 $success = true;
184 }
185
186 $this->watched = false;
187
188 wfProfileOut( __METHOD__ );
189 return $success;
190 }
191
192 /**
193 * Check if the given title already is watched by the user, and if so
194 * add watches on a new title. To be used for page renames and such.
195 *
196 * @param $ot Title: page title to duplicate entries from, if present
197 * @param $nt Title: page title to add watches on
198 */
199 public static function duplicateEntries( $ot, $nt ) {
200 WatchedItem::doDuplicateEntries( $ot->getSubjectPage(), $nt->getSubjectPage() );
201 WatchedItem::doDuplicateEntries( $ot->getTalkPage(), $nt->getTalkPage() );
202 }
203
204 /**
205 * Handle duplicate entries. Backend for duplicateEntries().
206 *
207 * @param $ot Title
208 * @param $nt Title
209 *
210 * @return bool
211 */
212 private static function doDuplicateEntries( $ot, $nt ) {
213 $oldnamespace = $ot->getNamespace();
214 $newnamespace = $nt->getNamespace();
215 $oldtitle = $ot->getDBkey();
216 $newtitle = $nt->getDBkey();
217
218 $dbw = wfGetDB( DB_MASTER );
219 $res = $dbw->select( 'watchlist', 'wl_user',
220 array( 'wl_namespace' => $oldnamespace, 'wl_title' => $oldtitle ),
221 __METHOD__, 'FOR UPDATE'
222 );
223 # Construct array to replace into the watchlist
224 $values = array();
225 foreach ( $res as $s ) {
226 $values[] = array(
227 'wl_user' => $s->wl_user,
228 'wl_namespace' => $newnamespace,
229 'wl_title' => $newtitle
230 );
231 }
232
233 if( empty( $values ) ) {
234 // Nothing to do
235 return true;
236 }
237
238 # Perform replace
239 # Note that multi-row replace is very efficient for MySQL but may be inefficient for
240 # some other DBMSes, mostly due to poor simulation by us
241 $dbw->replace( 'watchlist', array( array( 'wl_user', 'wl_namespace', 'wl_title' ) ), $values, __METHOD__ );
242 return true;
243 }
244 }