Abolished $wgDBname as a unique wiki identifier, it doesn't work with the new-fangled...
[lhc/web/wiklou.git] / includes / WatchedItem.php
1 <?php
2 /**
3 *
4 * @package MediaWiki
5 */
6
7 /**
8 *
9 * @package MediaWiki
10 */
11 class WatchedItem {
12 var $mTitle, $mUser;
13
14 /**
15 * Create a WatchedItem object with the given user and title
16 * @todo document
17 * @access private
18 */
19 function &fromUserTitle( &$user, &$title ) {
20 $wl = new WatchedItem;
21 $wl->mUser =& $user;
22 $wl->mTitle =& $title;
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();
29
30 $wl->ti = $title->getDBkey();
31 return $wl;
32 }
33
34 /**
35 * Returns the memcached key for this item
36 */
37 function watchKey() {
38 return wfMemcKey( 'watchlist', 'user', $this->id, 'page', $this->ns, $this->ti );
39 }
40
41 /**
42 * Is mTitle being watched by mUser?
43 */
44 function isWatched() {
45 # Pages and their talk pages are considered equivalent for watching;
46 # remember that talk namespaces are numbered as page namespace+1.
47 global $wgMemc;
48 $fname = 'WatchedItem::isWatched';
49
50 $key = $this->watchKey();
51 $iswatched = $wgMemc->get( $key );
52 if( is_integer( $iswatched ) ) return $iswatched;
53
54 $dbr =& wfGetDB( DB_SLAVE );
55 $res = $dbr->select( 'watchlist', 1, array( 'wl_user' => $this->id, 'wl_namespace' => $this->ns,
56 'wl_title' => $this->ti ), $fname );
57 $iswatched = ($dbr->numRows( $res ) > 0) ? 1 : 0;
58 $wgMemc->set( $key, $iswatched );
59 return $iswatched;
60 }
61
62 /**
63 * @todo document
64 */
65 function addWatch() {
66 $fname = 'WatchedItem::addWatch';
67 wfProfileIn( $fname );
68
69 // Use INSERT IGNORE to avoid overwriting the notification timestamp
70 // if there's already an entry for this page
71 $dbw =& wfGetDB( DB_MASTER );
72 $dbw->insert( 'watchlist',
73 array(
74 'wl_user' => $this->id,
75 'wl_namespace' => ($this->ns & ~1),
76 'wl_title' => $this->ti,
77 'wl_notificationtimestamp' => NULL
78 ), $fname, 'IGNORE' );
79
80 // Every single watched page needs now to be listed in watchlist;
81 // namespace:page and namespace_talk:page need separate entries:
82 $dbw->insert( 'watchlist',
83 array(
84 'wl_user' => $this->id,
85 'wl_namespace' => ($this->ns | 1 ),
86 'wl_title' => $this->ti,
87 'wl_notificationtimestamp' => NULL
88 ), $fname, 'IGNORE' );
89
90 global $wgMemc;
91 $wgMemc->set( $this->watchkey(), 1 );
92 wfProfileOut( $fname );
93 return true;
94 }
95
96 function removeWatch() {
97 global $wgMemc;
98 $fname = 'WatchedItem::removeWatch';
99
100 $success = false;
101 $dbw =& wfGetDB( DB_MASTER );
102 $dbw->delete( 'watchlist',
103 array(
104 'wl_user' => $this->id,
105 'wl_namespace' => ($this->ns & ~1),
106 'wl_title' => $this->ti
107 ), $fname
108 );
109 if ( $dbw->affectedRows() ) {
110 $success = true;
111 }
112
113 # the following code compensates the new behaviour, introduced by the
114 # enotif patch, that every single watched page needs now to be listed
115 # in watchlist namespace:page and namespace_talk:page had separate
116 # entries: clear them
117 $dbw->delete( 'watchlist',
118 array(
119 'wl_user' => $this->id,
120 'wl_namespace' => ($this->ns | 1),
121 'wl_title' => $this->ti
122 ), $fname
123 );
124
125 if ( $dbw->affectedRows() ) {
126 $success = true;
127 }
128 if ( $success ) {
129 $wgMemc->set( $this->watchkey(), 0 );
130 }
131 return $success;
132 }
133
134 /**
135 * Check if the given title already is watched by the user, and if so
136 * add watches on a new title. To be used for page renames and such.
137 *
138 * @param Title $ot Page title to duplicate entries from, if present
139 * @param Title $nt Page title to add watches on
140 * @static
141 */
142 function duplicateEntries( $ot, $nt ) {
143 WatchedItem::doDuplicateEntries( $ot->getSubjectPage(), $nt->getSubjectPage() );
144 WatchedItem::doDuplicateEntries( $ot->getTalkPage(), $nt->getTalkPage() );
145 }
146
147 /**
148 * @static
149 * @access private
150 */
151 function doDuplicateEntries( $ot, $nt ) {
152 $fname = "WatchedItem::duplicateEntries";
153 $oldnamespace = $ot->getNamespace();
154 $newnamespace = $nt->getNamespace();
155 $oldtitle = $ot->getDBkey();
156 $newtitle = $nt->getDBkey();
157
158 $dbw =& wfGetDB( DB_MASTER );
159 $res = $dbw->select( 'watchlist', 'wl_user',
160 array( 'wl_namespace' => $oldnamespace, 'wl_title' => $oldtitle ),
161 $fname, 'FOR UPDATE'
162 );
163 # Construct array to replace into the watchlist
164 $values = array();
165 while ( $s = $dbw->fetchObject( $res ) ) {
166 $values[] = array(
167 'wl_user' => $s->wl_user,
168 'wl_namespace' => $newnamespace,
169 'wl_title' => $newtitle
170 );
171 }
172 $dbw->freeResult( $res );
173
174 if( empty( $values ) ) {
175 // Nothing to do
176 return true;
177 }
178
179 # Perform replace
180 # Note that multi-row replace is very efficient for MySQL but may be inefficient for
181 # some other DBMSes, mostly due to poor simulation by us
182 $dbw->replace( 'watchlist', array(array( 'wl_user', 'wl_namespace', 'wl_title')), $values, $fname );
183 return true;
184 }
185
186
187 }
188
189 ?>