17 * Create a WatchedItem object with the given user and title
21 function &fromUserTitle( &$user, &$title ) {
22 $wl = new WatchedItem
;
24 $wl->mTitle
=& $title;
25 $wl->id
= $user->getId();
26 # Patch (also) for email notification on page changes T.Gries/M.Arndt 11.09.2004
27 # TG patch: here we do not consider pages and their talk pages equivalent - why should we ?
28 # The change results in talk-pages not automatically included in watchlists, when their parent page is included
29 # $wl->ns = $title->getNamespace() & ~1;
30 $wl->ns
= $title->getNamespace();
32 $wl->ti
= $title->getDBkey();
37 * Returns the memcached key for this item
41 return "$wgDBname:watchlist:user:$this->id:page:$this->ns:$this->ti";
45 * Is mTitle being watched by mUser?
47 function isWatched() {
48 # Pages and their talk pages are considered equivalent for watching;
49 # remember that talk namespaces are numbered as page namespace+1.
51 $fname = 'WatchedItem::isWatched';
53 $key = $this->watchKey();
54 $iswatched = $wgMemc->get( $key );
55 if( is_integer( $iswatched ) ) return $iswatched;
57 $dbr =& wfGetDB( DB_SLAVE
);
58 $res = $dbr->select( 'watchlist', 1, array( 'wl_user' => $this->id
, 'wl_namespace' => $this->ns
,
59 'wl_title' => $this->ti
), $fname );
60 $iswatched = ($dbr->numRows( $res ) > 0) ?
1 : 0;
61 $wgMemc->set( $key, $iswatched );
69 $fname = 'WatchedItem::addWatch';
70 wfProfileIn( $fname );
72 // Use INSERT IGNORE to avoid overwriting the notification timestamp
73 // if there's already an entry for this page
74 $dbw =& wfGetDB( DB_MASTER
);
75 $dbw->insert( 'watchlist',
77 'wl_user' => $this->id
,
78 'wl_namespace' => ($this->ns
& ~
1),
79 'wl_title' => $this->ti
,
80 'wl_notificationtimestamp' => NULL
81 ), $fname, 'IGNORE' );
83 // Every single watched page needs now to be listed in watchlist;
84 // namespace:page and namespace_talk:page need separate entries:
85 $dbw->insert( 'watchlist',
87 'wl_user' => $this->id
,
88 'wl_namespace' => ($this->ns |
1 ),
89 'wl_title' => $this->ti
,
90 'wl_notificationtimestamp' => NULL
91 ), $fname, 'IGNORE' );
94 $wgMemc->set( $this->watchkey(), 1 );
95 wfProfileOut( $fname );
99 function removeWatch() {
101 $fname = 'WatchedItem::removeWatch';
104 $dbw =& wfGetDB( DB_MASTER
);
105 $dbw->delete( 'watchlist',
107 'wl_user' => $this->id
,
108 'wl_namespace' => ($this->ns
& ~
1),
109 'wl_title' => $this->ti
112 if ( $dbw->affectedRows() ) {
116 # the following code compensates the new behaviour, introduced by the
117 # enotif patch, that every single watched page needs now to be listed
118 # in watchlist namespace:page and namespace_talk:page had separate
119 # entries: clear them
120 $dbw->delete( 'watchlist',
122 'wl_user' => $this->id
,
123 'wl_namespace' => ($this->ns |
1),
124 'wl_title' => $this->ti
128 if ( $dbw->affectedRows() ) {
132 $wgMemc->set( $this->watchkey(), 0 );
138 * Check if the given title already is watched by the user, and if so
139 * add watches on a new title. To be used for page renames and such.
141 * @param Title $ot Page title to duplicate entries from, if present
142 * @param Title $nt Page title to add watches on
145 function duplicateEntries( $ot, $nt ) {
146 WatchedItem
::doDuplicateEntries( $ot->getSubjectPage(), $nt->getSubjectPage() );
147 WatchedItem
::doDuplicateEntries( $ot->getTalkPage(), $nt->getTalkPage() );
154 function doDuplicateEntries( $ot, $nt ) {
155 $fname = "WatchedItem::duplicateEntries";
156 $oldnamespace = $ot->getNamespace();
157 $newnamespace = $nt->getNamespace();
158 $oldtitle = $ot->getDBkey();
159 $newtitle = $nt->getDBkey();
161 $dbw =& wfGetDB( DB_MASTER
);
162 $res = $dbw->select( 'watchlist', 'wl_user',
163 array( 'wl_namespace' => $oldnamespace, 'wl_title' => $oldtitle ),
166 # Construct array to replace into the watchlist
168 while ( $s = $dbw->fetchObject( $res ) ) {
170 'wl_user' => $s->wl_user
,
171 'wl_namespace' => $newnamespace,
172 'wl_title' => $newtitle
175 $dbw->freeResult( $res );
177 if( empty( $values ) ) {
183 # Note that multi-row replace is very efficient for MySQL but may be inefficient for
184 # some other DBMSes, mostly due to poor simulation by us
185 $dbw->replace( 'watchlist', array(array( 'wl_user', 'wl_namespace', 'wl_title')), $values, $fname );