11 var $mTitle, $mUser, $id, $ns, $ti;
12 private $loaded = false, $watched, $timestamp;
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
20 public static function fromUserTitle( $user, $title ) {
21 $wl = new WatchedItem
;
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();
31 $wl->ti
= $title->getDBkey();
36 * Return an array of conditions to select or update the appropriate database
41 private function dbCond() {
42 return array( 'wl_user' => $this->id
, 'wl_namespace' => $this->ns
, 'wl_title' => $this->ti
);
46 * Load the object from the database
48 private function load() {
49 if ( $this->loaded
) {
54 # Pages and their talk pages are considered equivalent for watching;
55 # remember that talk namespaces are numbered as page namespace+1.
57 $dbr = wfGetDB( DB_SLAVE
);
58 $row = $dbr->selectRow( 'watchlist', 'wl_notificationtimestamp',
59 $this->dbCond(), __METHOD__
);
61 if ( $row === false ) {
62 $this->watched
= false;
64 $this->watched
= true;
65 $this->timestamp
= $row->wl_notificationtimestamp
;
70 * Is mTitle being watched by mUser?
73 public function isWatched() {
75 return $this->watched
;
79 * Get the notification timestamp of this entry.
81 * @return false|null|string: false if the page is not watched, the value of
82 * the wl_notificationtimestamp field otherwise
84 public function getNotificationTimestamp() {
86 if ( $this->watched
) {
87 return $this->timestamp
;
94 * Reset the notification timestamp of this entry
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.
99 public function resetNotificationTimestamp( $force = '' ) {
100 if ( $force != 'force' ) {
102 if ( !$this->watched ||
$this->timestamp
=== null ) {
107 // If the page is watched by the user (or may be watched), update the timestamp on any
109 $dbw = wfGetDB( DB_MASTER
);
110 $dbw->update( 'watchlist', array( 'wl_notificationtimestamp' => null ),
111 $this->dbCond(), __METHOD__
);
112 $this->timestamp
= null;
116 * Given a title and user (assumes the object is setup), add the watch to the
118 * @return bool (always true)
120 public function addWatch() {
121 wfProfileIn( __METHOD__
);
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',
128 'wl_user' => $this->id
,
129 'wl_namespace' => MWNamespace
::getSubject($this->ns
),
130 'wl_title' => $this->ti
,
131 'wl_notificationtimestamp' => null
132 ), __METHOD__
, 'IGNORE' );
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',
138 'wl_user' => $this->id
,
139 'wl_namespace' => MWNamespace
::getTalk($this->ns
),
140 'wl_title' => $this->ti
,
141 'wl_notificationtimestamp' => null
142 ), __METHOD__
, 'IGNORE' );
144 $this->watched
= true;
146 wfProfileOut( __METHOD__
);
151 * Same as addWatch, only the opposite.
154 public function removeWatch() {
155 wfProfileIn( __METHOD__
);
158 $dbw = wfGetDB( DB_MASTER
);
159 $dbw->delete( 'watchlist',
161 'wl_user' => $this->id
,
162 'wl_namespace' => MWNamespace
::getSubject($this->ns
),
163 'wl_title' => $this->ti
166 if ( $dbw->affectedRows() ) {
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',
176 'wl_user' => $this->id
,
177 'wl_namespace' => MWNamespace
::getTalk($this->ns
),
178 'wl_title' => $this->ti
182 if ( $dbw->affectedRows() ) {
186 $this->watched
= false;
188 wfProfileOut( __METHOD__
);
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.
196 * @param $ot Title: page title to duplicate entries from, if present
197 * @param $nt Title: page title to add watches on
199 public static function duplicateEntries( $ot, $nt ) {
200 WatchedItem
::doDuplicateEntries( $ot->getSubjectPage(), $nt->getSubjectPage() );
201 WatchedItem
::doDuplicateEntries( $ot->getTalkPage(), $nt->getTalkPage() );
205 * Handle duplicate entries. Backend for duplicateEntries().
212 private static function doDuplicateEntries( $ot, $nt ) {
213 $oldnamespace = $ot->getNamespace();
214 $newnamespace = $nt->getNamespace();
215 $oldtitle = $ot->getDBkey();
216 $newtitle = $nt->getDBkey();
218 $dbw = wfGetDB( DB_MASTER
);
219 $res = $dbw->select( 'watchlist', 'wl_user',
220 array( 'wl_namespace' => $oldnamespace, 'wl_title' => $oldtitle ),
221 __METHOD__
, 'FOR UPDATE'
223 # Construct array to replace into the watchlist
225 foreach ( $res as $s ) {
227 'wl_user' => $s->wl_user
,
228 'wl_namespace' => $newnamespace,
229 'wl_title' => $newtitle
233 if( empty( $values ) ) {
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__
);