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