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.
41 $fname = 'WatchedItem::isWatched';
43 $dbr = wfGetDB( DB_SLAVE );
44 $res = $dbr->select( 'watchlist', 1, array( 'wl_user' => $this->id, 'wl_namespace' => $this->ns,
45 'wl_title' => $this->ti ), $fname );
46 $iswatched = ($dbr->numRows( $res ) > 0) ? 1 : 0;
51 * Given a title and user (assumes the object is setup), add the watch to the
53 * @return bool (always true)
55 public function addWatch() {
56 $fname = 'WatchedItem::addWatch';
57 wfProfileIn( $fname );
59 // Use INSERT IGNORE to avoid overwriting the notification timestamp
60 // if there's already an entry for this page
61 $dbw = wfGetDB( DB_MASTER );
62 $dbw->insert( 'watchlist',
64 'wl_user' => $this->id,
65 'wl_namespace' => ($this->ns & ~1),
66 'wl_title' => $this->ti,
67 'wl_notificationtimestamp' => NULL
68 ), $fname, 'IGNORE' );
70 // Every single watched page needs now to be listed in watchlist;
71 // namespace:page and namespace_talk:page need separate entries:
72 $dbw->insert( 'watchlist',
74 'wl_user' => $this->id,
75 'wl_namespace' => ($this->ns | 1 ),
76 'wl_title' => $this->ti,
77 'wl_notificationtimestamp' => NULL
78 ), $fname, 'IGNORE' );
80 wfProfileOut( $fname );
85 * Same as addWatch, only the opposite.
88 public function removeWatch() {
89 $fname = 'WatchedItem::removeWatch';
92 $dbw = wfGetDB( DB_MASTER );
93 $dbw->delete( 'watchlist',
95 'wl_user' => $this->id,
96 'wl_namespace' => ($this->ns & ~1),
97 'wl_title' => $this->ti
100 if ( $dbw->affectedRows() ) {
104 # the following code compensates the new behaviour, introduced by the
105 # enotif patch, that every single watched page needs now to be listed
106 # in watchlist namespace:page and namespace_talk:page had separate
107 # entries: clear them
108 $dbw->delete( 'watchlist',
110 'wl_user' => $this->id,
111 'wl_namespace' => ($this->ns | 1),
112 'wl_title' => $this->ti
116 if ( $dbw->affectedRows() ) {
123 * Check if the given title already is watched by the user, and if so
124 * add watches on a new title. To be used for page renames and such.
126 * @param $ot Title: page title to duplicate entries from, if present
127 * @param $nt Title: page title to add watches on
129 public static function duplicateEntries( $ot, $nt ) {
130 WatchedItem::doDuplicateEntries( $ot->getSubjectPage(), $nt->getSubjectPage() );
131 WatchedItem::doDuplicateEntries( $ot->getTalkPage(), $nt->getTalkPage() );
135 * Handle duplicate entries. Backend for duplicateEntries().
137 private static function doDuplicateEntries( $ot, $nt ) {
138 $fname = "WatchedItem::duplicateEntries";
139 $oldnamespace = $ot->getNamespace();
140 $newnamespace = $nt->getNamespace();
141 $oldtitle = $ot->getDBkey();
142 $newtitle = $nt->getDBkey();
144 $dbw = wfGetDB( DB_MASTER );
145 $res = $dbw->select( 'watchlist', 'wl_user',
146 array( 'wl_namespace' => $oldnamespace, 'wl_title' => $oldtitle ),
149 # Construct array to replace into the watchlist
151 while ( $s = $dbw->fetchObject( $res ) ) {
153 'wl_user' => $s->wl_user,
154 'wl_namespace' => $newnamespace,
155 'wl_title' => $newtitle
158 $dbw->freeResult( $res );
160 if( empty( $values ) ) {
166 # Note that multi-row replace is very efficient for MySQL but may be inefficient for
167 # some other DBMSes, mostly due to poor simulation by us
168 $dbw->replace( 'watchlist', array(array( 'wl_user', 'wl_namespace', 'wl_title')), $values, $fname );