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
25 * Representation of a pair of user and title for watchlist entries.
31 private $loaded = false, $watched, $timestamp;
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
;
51 protected function getTitle() {
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();
70 * Return an array of conditions to select or update the appropriate database
75 private function dbCond() {
77 'wl_user' => $this->getUserId(),
78 'wl_namespace' => $this->getTitleNs(),
79 'wl_title' => $this->getTitleDBkey(),
84 * Load the object from the database
86 private function load() {
87 if ( $this->loaded
) {
92 // Only loggedin user can have a watchlist
93 if ( $this->mUser
->isAnon() ) {
94 $this->watched
= false;
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;
108 $this->watched
= true;
109 $this->timestamp
= $row->wl_notificationtimestamp
;
114 * Is mTitle being watched by mUser?
117 public function isWatched() {
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() {
130 if ( $this->watched
) {
131 return $this->timestamp
;
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() ) {
149 if ( $force != 'force' ) {
151 if ( !$this->watched ||
$this->timestamp
=== null ) {
156 // If the page is watched by the user (or may be watched), update the timestamp on any
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
169 public function addWatch() {
170 wfProfileIn( __METHOD__
);
172 // Only loggedin user can have a watchlist
173 if ( wfReadOnly() ||
$this->mUser
->isAnon() ) {
174 wfProfileOut( __METHOD__
);
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',
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',
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__
);
206 * Same as addWatch, only the opposite.
209 public function removeWatch() {
210 wfProfileIn( __METHOD__
);
212 // Only loggedin user can have a watchlist
213 if ( wfReadOnly() ||
$this->mUser
->isAnon() ) {
214 wfProfileOut( __METHOD__
);
219 $dbw = wfGetDB( DB_MASTER
);
220 $dbw->delete( 'watchlist',
222 'wl_user' => $this->getUserId(),
223 'wl_namespace' => MWNamespace
::getSubject( $this->getTitleNs() ),
224 'wl_title' => $this->getTitleDBkey(),
227 if ( $dbw->affectedRows() ) {
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',
237 'wl_user' => $this->getUserId(),
238 'wl_namespace' => MWNamespace
::getTalk( $this->getTitleNs() ),
239 'wl_title' => $this->getTitleDBkey(),
243 if ( $dbw->affectedRows() ) {
247 $this->watched
= false;
249 wfProfileOut( __METHOD__
);
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().
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
286 foreach ( $res as $s ) {
288 'wl_user' => $s->wl_user
,
289 'wl_namespace' => $newnamespace,
290 'wl_title' => $newtitle
294 if ( empty( $values ) ) {
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__
);