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.
30 var $mTitle, $mUser, $id, $gr, $ns, $ti;
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, $group = 0 ) {
40 $wl = new WatchedItem
;
43 $wl->id
= $user->getId();
44 # Patch (also) for email notification on page changes T.Gries/M.Arndt 11.09.2004
45 # TG patch: here we do not consider pages and their talk pages equivalent - why should we ?
46 # The change results in talk-pages not automatically included in watchlists, when their parent page is included
47 # $wl->ns = $title->getNamespace() & ~1;
48 $wl->ns
= $title->getNamespace();
50 $wl->ti
= $title->getDBkey();
57 * Return an array of conditions to select or update the appropriate database
62 private function dbCond() {
63 return array( 'wl_user' => $this->id
, 'wl_namespace' => $this->ns
, 'wl_title' => $this->ti
);
67 * Load the object from the database
69 private function load() {
70 if ( $this->loaded
) {
75 # Pages and their talk pages are considered equivalent for watching;
76 # remember that talk namespaces are numbered as page namespace+1.
78 $dbr = wfGetDB( DB_SLAVE
);
79 $row = $dbr->selectRow( 'watchlist', 'wl_notificationtimestamp',
80 $this->dbCond(), __METHOD__
);
82 if ( $row === false ) {
83 $this->watched
= false;
85 $this->watched
= true;
86 $this->timestamp
= $row->wl_notificationtimestamp
;
91 * Is mTitle being watched by mUser?
94 public function isWatched() {
96 return $this->watched
;
100 * Get the notification timestamp of this entry.
102 * @return false|null|string: false if the page is not watched, the value of
103 * the wl_notificationtimestamp field otherwise
105 public function getNotificationTimestamp() {
107 if ( $this->watched
) {
108 return $this->timestamp
;
115 * Reset the notification timestamp of this entry
117 * @param $force Whether to force the write query to be executed even if the
118 * page is not watched or the notification timestamp is already NULL.
120 public function resetNotificationTimestamp( $force = '' ) {
121 if ( $force != 'force' ) {
123 if ( !$this->watched ||
$this->timestamp
=== null ) {
128 // If the page is watched by the user (or may be watched), update the timestamp on any
130 $dbw = wfGetDB( DB_MASTER
);
131 $dbw->update( 'watchlist', array( 'wl_notificationtimestamp' => null ),
132 $this->dbCond(), __METHOD__
);
133 $this->timestamp
= null;
137 * Given a title and user (assumes the object is setup), add the watch to the
139 * @return bool (always true)
141 public function addWatch() {
142 wfProfileIn( __METHOD__
);
144 // Use INSERT IGNORE to avoid overwriting the notification timestamp
145 // if there's already an entry for this page
146 $dbw = wfGetDB( DB_MASTER
);
147 $dbw->insert( 'watchlist',
149 'wl_user' => $this->id
,
150 'wl_group' => $this->gr
,
151 'wl_namespace' => MWNamespace
::getSubject($this->ns
),
152 'wl_title' => $this->ti
,
153 'wl_notificationtimestamp' => null
154 ), __METHOD__
, 'IGNORE' );
156 // Every single watched page needs now to be listed in watchlist;
157 // namespace:page and namespace_talk:page need separate entries:
158 $dbw->insert( 'watchlist',
160 'wl_user' => $this->id
,
161 'wl_group' => $this->gr
,
162 'wl_namespace' => MWNamespace
::getTalk($this->ns
),
163 'wl_title' => $this->ti
,
164 'wl_notificationtimestamp' => null
165 ), __METHOD__
, 'IGNORE' );
167 $this->watched
= true;
169 wfProfileOut( __METHOD__
);
174 * Same as addWatch, only the opposite.
177 public function removeWatch() {
178 wfProfileIn( __METHOD__
);
181 $dbw = wfGetDB( DB_MASTER
);
182 $dbw->delete( 'watchlist',
184 'wl_user' => $this->id
,
185 'wl_namespace' => MWNamespace
::getSubject($this->ns
),
186 'wl_title' => $this->ti
189 if ( $dbw->affectedRows() ) {
193 # the following code compensates the new behaviour, introduced by the
194 # enotif patch, that every single watched page needs now to be listed
195 # in watchlist namespace:page and namespace_talk:page had separate
196 # entries: clear them
197 $dbw->delete( 'watchlist',
199 'wl_user' => $this->id
,
200 'wl_namespace' => MWNamespace
::getTalk($this->ns
),
201 'wl_title' => $this->ti
205 if ( $dbw->affectedRows() ) {
209 $this->watched
= false;
211 wfProfileOut( __METHOD__
);
216 * Check if the given title already is watched by the user, and if so
217 * add watches on a new title. To be used for page renames and such.
219 * @param $ot Title: page title to duplicate entries from, if present
220 * @param $nt Title: page title to add watches on
222 public static function duplicateEntries( $ot, $nt ) {
223 WatchedItem
::doDuplicateEntries( $ot->getSubjectPage(), $nt->getSubjectPage() );
224 WatchedItem
::doDuplicateEntries( $ot->getTalkPage(), $nt->getTalkPage() );
228 * Handle duplicate entries. Backend for duplicateEntries().
235 private static function doDuplicateEntries( $ot, $nt ) {
236 $oldnamespace = $ot->getNamespace();
237 $newnamespace = $nt->getNamespace();
238 $oldtitle = $ot->getDBkey();
239 $newtitle = $nt->getDBkey();
241 $dbw = wfGetDB( DB_MASTER
);
242 $res = $dbw->select( 'watchlist', 'wl_user',
243 array( 'wl_namespace' => $oldnamespace, 'wl_title' => $oldtitle ),
244 __METHOD__
, 'FOR UPDATE'
246 # Construct array to replace into the watchlist
248 foreach ( $res as $s ) {
250 'wl_user' => $s->wl_user
,
251 'wl_namespace' => $newnamespace,
252 'wl_title' => $newtitle
256 if( empty( $values ) ) {
262 # Note that multi-row replace is very efficient for MySQL but may be inefficient for
263 # some other DBMSes, mostly due to poor simulation by us
264 $dbw->replace( 'watchlist', array( array( 'wl_user', 'wl_namespace', 'wl_title' ) ), $values, __METHOD__
);