(bug 454) Merge e-notif 2.00
[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 # Create a WatchedItem object with the given user and title
15 /* static */ function &fromUserTitle( &$user, &$title ) {
16 $wl = new WatchedItem;
17 $wl->mUser =& $user;
18 $wl->mTitle =& $title;
19 $wl->id = $user->getId();
20 # Patch (also) for email notification on page changes T.Gries/M.Arndt 11.09.2004
21 # TG patch: here we do not consider pages and their talk pages equivalent - why should we ?
22 # The change results in talk-pages not automatically included in watchlists, when their parent page is included
23 # $wl->ns = $title->getNamespace() & ~1;
24 $wl->ns = $title->getNamespace();
25
26 $wl->ti = $title->getDBkey();
27 return $wl;
28 }
29
30 /**
31 * Returns the memcached key for this item
32 */
33 function watchKey() {
34 global $wgDBname;
35 return "$wgDBname:watchlist:user:$this->id:page:$this->ns:$this->ti";
36 }
37
38 /**
39 * Is mTitle being watched by mUser?
40 */
41 function isWatched()
42 {
43 # Pages and their talk pages are considered equivalent for watching;
44 # remember that talk namespaces are numbered as page namespace+1.
45 global $wgMemc;
46 $fname = 'WatchedItem::isWatched';
47
48 $key = $this->watchKey();
49 $iswatched = $wgMemc->get( $key );
50 if( is_integer( $iswatched ) ) return $iswatched;
51
52 $dbr =& wfGetDB( DB_SLAVE );
53 $res = $dbr->select( 'watchlist', 1, array( 'wl_user' => $this->id, 'wl_namespace' => $this->ns,
54 'wl_title' => $this->ti ), $fname );
55 $iswatched = ($dbr->numRows( $res ) > 0) ? 1 : 0;
56 $wgMemc->set( $key, $iswatched );
57 return $iswatched;
58 }
59
60 function addWatch() {
61 $fname = "WatchedItem::addWatch";
62 # REPLACE instead of INSERT because occasionally someone
63 # accidentally reloads a watch-add operation.
64 $dbw =& wfGetDB( DB_MASTER );
65 $dbw->replace( 'watchlist', array(array('wl_user', 'wl_namespace', 'wl_title', 'wl_notificationtimestamp')),
66 array(
67 'wl_user' => $this->id,
68 'wl_namespace' => ($this->ns & ~1),
69 'wl_title' => $this->ti,
70 'wl_notificationtimestamp' => '0'
71 ), $fname );
72
73 # the following code compensates the new behaviour, introduced by the enotif patch,
74 # that every single watched page needs now to be listed in watchlist
75 # namespace:page and namespace_talk:page need separate entries: create them
76 $dbw->replace( 'watchlist', array(array('wl_user', 'wl_namespace', 'wl_title', 'wl_notificationtimestamp')),
77 array(
78 'wl_user' => $this->id,
79 'wl_namespace' => ($this->ns | 1 ),
80 'wl_title' => $this->ti,
81 'wl_notificationtimestamp' => '0'
82 ), $fname );
83
84 global $wgMemc;
85 $wgMemc->set( $this->watchkey(), 1 );
86 return true;
87 }
88
89 function removeWatch() {
90 $fname = 'WatchedItem::removeWatch';
91
92 $dbw =& wfGetDB( DB_MASTER );
93 $dbw->delete( 'watchlist',
94 array(
95 'wl_user' => $this->id,
96 'wl_namespace' => ($this->ns & ~1),
97 'wl_title' => $this->ti
98 ), $fname
99 );
100
101 # the following code compensates the new behaviour, introduced by the enotif patch,
102 # that every single watched page needs now to be listed in watchlist
103 # namespace:page and namespace_talk:page had separate entries: clear them
104 $dbw->delete( 'watchlist',
105 array(
106 'wl_user' => $this->id,
107 'wl_namespace' => ($this->ns | 1),
108 'wl_title' => $this->ti
109 ), $fname
110 );
111
112 if ( $dbw->affectedRows() ) {
113 global $wgMemc;
114 $wgMemc->set( $this->watchkey(), 0 );
115 return true;
116 } else {
117 return false;
118 }
119 }
120
121 /**
122 * @static
123 */
124 function duplicateEntries( $ot, $nt ) {
125 $fname = "WatchedItem::duplicateEntries";
126 global $wgMemc, $wgDBname;
127 $oldnamespace = $ot->getNamespace() & ~1;
128 $newnamespace = $nt->getNamespace() & ~1;
129 $oldtitle = $ot->getDBkey();
130 $newtitle = $nt->getDBkey();
131
132 $dbw =& wfGetDB( DB_MASTER );
133 $watchlist = $dbw->tableName( 'watchlist' );
134
135 $res = $dbw->select( 'watchlist', 'wl_user',
136 array( 'wl_namespace' => $oldnamespace, 'wl_title' => $oldtitle ),
137 $fname, 'FOR UPDATE'
138 );
139 # Construct array to replace into the watchlist
140 $values = array();
141 while ( $s = $dbw->fetchObject( $res ) ) {
142 $values[] = array(
143 'wl_user' => $s->wl_user,
144 'wl_namespace' => $newnamespace,
145 'wl_title' => $newtitle
146 );
147 }
148 $dbw->freeResult( $res );
149
150 # Perform replace
151 # Note that multi-row replace is very efficient for MySQL but may be inefficient for
152 # some other DBMSes, mostly due to poor simulation by us
153 $dbw->replace( 'watchlist', array(array( 'wl_user', 'wl_namespace', 'wl_title')), $values, $fname );
154 return true;
155 }
156
157
158 }
159
160 ?>