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