Replacing var keyword with private / public as we now require PHP5.
[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 private
13 $mTitle,
14 $mUser;
15
16 /**
17 * Create a WatchedItem object with the given user and title
18 * @todo document
19 * @access private
20 */
21 function &fromUserTitle( &$user, &$title ) {
22 $wl = new WatchedItem;
23 $wl->mUser =& $user;
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();
31
32 $wl->ti = $title->getDBkey();
33 return $wl;
34 }
35
36 /**
37 * Returns the memcached key for this item
38 */
39 function watchKey() {
40 global $wgDBname;
41 return "$wgDBname:watchlist:user:$this->id:page:$this->ns:$this->ti";
42 }
43
44 /**
45 * Is mTitle being watched by mUser?
46 */
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.
50 global $wgMemc;
51 $fname = 'WatchedItem::isWatched';
52
53 $key = $this->watchKey();
54 $iswatched = $wgMemc->get( $key );
55 if( is_integer( $iswatched ) ) return $iswatched;
56
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 );
62 return $iswatched;
63 }
64
65 /**
66 * @todo document
67 */
68 function addWatch() {
69 $fname = 'WatchedItem::addWatch';
70 wfProfileIn( $fname );
71
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',
76 array(
77 'wl_user' => $this->id,
78 'wl_namespace' => ($this->ns & ~1),
79 'wl_title' => $this->ti,
80 'wl_notificationtimestamp' => NULL
81 ), $fname, 'IGNORE' );
82
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',
86 array(
87 'wl_user' => $this->id,
88 'wl_namespace' => ($this->ns | 1 ),
89 'wl_title' => $this->ti,
90 'wl_notificationtimestamp' => NULL
91 ), $fname, 'IGNORE' );
92
93 global $wgMemc;
94 $wgMemc->set( $this->watchkey(), 1 );
95 wfProfileOut( $fname );
96 return true;
97 }
98
99 function removeWatch() {
100 global $wgMemc;
101 $fname = 'WatchedItem::removeWatch';
102
103 $success = false;
104 $dbw =& wfGetDB( DB_MASTER );
105 $dbw->delete( 'watchlist',
106 array(
107 'wl_user' => $this->id,
108 'wl_namespace' => ($this->ns & ~1),
109 'wl_title' => $this->ti
110 ), $fname
111 );
112 if ( $dbw->affectedRows() ) {
113 $success = true;
114 }
115
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',
121 array(
122 'wl_user' => $this->id,
123 'wl_namespace' => ($this->ns | 1),
124 'wl_title' => $this->ti
125 ), $fname
126 );
127
128 if ( $dbw->affectedRows() ) {
129 $success = true;
130 }
131 if ( $success ) {
132 $wgMemc->set( $this->watchkey(), 0 );
133 }
134 return $success;
135 }
136
137 /**
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.
140 *
141 * @param Title $ot Page title to duplicate entries from, if present
142 * @param Title $nt Page title to add watches on
143 * @static
144 */
145 function duplicateEntries( $ot, $nt ) {
146 WatchedItem::doDuplicateEntries( $ot->getSubjectPage(), $nt->getSubjectPage() );
147 WatchedItem::doDuplicateEntries( $ot->getTalkPage(), $nt->getTalkPage() );
148 }
149
150 /**
151 * @static
152 * @access private
153 */
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();
160
161 $dbw =& wfGetDB( DB_MASTER );
162 $res = $dbw->select( 'watchlist', 'wl_user',
163 array( 'wl_namespace' => $oldnamespace, 'wl_title' => $oldtitle ),
164 $fname, 'FOR UPDATE'
165 );
166 # Construct array to replace into the watchlist
167 $values = array();
168 while ( $s = $dbw->fetchObject( $res ) ) {
169 $values[] = array(
170 'wl_user' => $s->wl_user,
171 'wl_namespace' => $newnamespace,
172 'wl_title' => $newtitle
173 );
174 }
175 $dbw->freeResult( $res );
176
177 if( empty( $values ) ) {
178 // Nothing to do
179 return true;
180 }
181
182 # Perform replace
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 );
186 return true;
187 }
188
189
190 }
191
192 ?>