11 var $mTitle, $mUser, $id, $ns, $ti;
14 * Create a WatchedItem object with the given user and title
15 * @param $user User: the user to use for (un)watching
16 * @param $title Title: the title we're going to (un)watch
17 * @return WatchedItem object
19 public static function fromUserTitle( $user, $title ) {
20 $wl = new WatchedItem
;
23 $wl->id
= $user->getId();
24 # Patch (also) for email notification on page changes T.Gries/M.Arndt 11.09.2004
25 # TG patch: here we do not consider pages and their talk pages equivalent - why should we ?
26 # The change results in talk-pages not automatically included in watchlists, when their parent page is included
27 # $wl->ns = $title->getNamespace() & ~1;
28 $wl->ns
= $title->getNamespace();
30 $wl->ti
= $title->getDBkey();
35 * Is mTitle being watched by mUser?
38 public function isWatched() {
39 # Pages and their talk pages are considered equivalent for watching;
40 # remember that talk namespaces are numbered as page namespace+1.
42 $dbr = wfGetDB( DB_SLAVE
);
43 $res = $dbr->select( 'watchlist', 1, array( 'wl_user' => $this->id
, 'wl_namespace' => $this->ns
,
44 'wl_title' => $this->ti
), __METHOD__
);
45 $iswatched = ($dbr->numRows( $res ) > 0) ?
1 : 0;
50 * Given a title and user (assumes the object is setup), add the watch to the
52 * @return bool (always true)
54 public function addWatch() {
55 wfProfileIn( __METHOD__
);
57 // Use INSERT IGNORE to avoid overwriting the notification timestamp
58 // if there's already an entry for this page
59 $dbw = wfGetDB( DB_MASTER
);
60 $dbw->insert( 'watchlist',
62 'wl_user' => $this->id
,
63 'wl_namespace' => MWNamespace
::getSubject($this->ns
),
64 'wl_title' => $this->ti
,
65 'wl_notificationtimestamp' => NULL
66 ), __METHOD__
, 'IGNORE' );
68 // Every single watched page needs now to be listed in watchlist;
69 // namespace:page and namespace_talk:page need separate entries:
70 $dbw->insert( 'watchlist',
72 'wl_user' => $this->id
,
73 'wl_namespace' => MWNamespace
::getTalk($this->ns
),
74 'wl_title' => $this->ti
,
75 'wl_notificationtimestamp' => NULL
76 ), __METHOD__
, 'IGNORE' );
78 wfProfileOut( __METHOD__
);
83 * Same as addWatch, only the opposite.
86 public function removeWatch() {
88 $dbw = wfGetDB( DB_MASTER
);
89 $dbw->delete( 'watchlist',
91 'wl_user' => $this->id
,
92 'wl_namespace' => MWNamespace
::getSubject($this->ns
),
93 'wl_title' => $this->ti
96 if ( $dbw->affectedRows() ) {
100 # the following code compensates the new behaviour, introduced by the
101 # enotif patch, that every single watched page needs now to be listed
102 # in watchlist namespace:page and namespace_talk:page had separate
103 # entries: clear them
104 $dbw->delete( 'watchlist',
106 'wl_user' => $this->id
,
107 'wl_namespace' => MWNamespace
::getTalk($this->ns
),
108 'wl_title' => $this->ti
112 if ( $dbw->affectedRows() ) {
119 * Check if the given title already is watched by the user, and if so
120 * add watches on a new title. To be used for page renames and such.
122 * @param $ot Title: page title to duplicate entries from, if present
123 * @param $nt Title: page title to add watches on
125 public static function duplicateEntries( $ot, $nt ) {
126 WatchedItem
::doDuplicateEntries( $ot->getSubjectPage(), $nt->getSubjectPage() );
127 WatchedItem
::doDuplicateEntries( $ot->getTalkPage(), $nt->getTalkPage() );
131 * Handle duplicate entries. Backend for duplicateEntries().
133 private static function doDuplicateEntries( $ot, $nt ) {
134 $oldnamespace = $ot->getNamespace();
135 $newnamespace = $nt->getNamespace();
136 $oldtitle = $ot->getDBkey();
137 $newtitle = $nt->getDBkey();
139 $dbw = wfGetDB( DB_MASTER
);
140 $res = $dbw->select( 'watchlist', 'wl_user',
141 array( 'wl_namespace' => $oldnamespace, 'wl_title' => $oldtitle ),
142 __METHOD__
, 'FOR UPDATE'
144 # Construct array to replace into the watchlist
146 while ( $s = $dbw->fetchObject( $res ) ) {
148 'wl_user' => $s->wl_user
,
149 'wl_namespace' => $newnamespace,
150 'wl_title' => $newtitle
153 $dbw->freeResult( $res );
155 if( empty( $values ) ) {
161 # Note that multi-row replace is very efficient for MySQL but may be inefficient for
162 # some other DBMSes, mostly due to poor simulation by us
163 $dbw->replace( 'watchlist', array( array( 'wl_user', 'wl_namespace', 'wl_title' ) ), $values, __METHOD__
);