phpdoc comment / code formatting
[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 # REPLACE instead of INSERT because occasionally someone
69 # accidentally reloads a watch-add operation.
70 $dbw =& wfGetDB( DB_MASTER );
71 $dbw->replace( 'watchlist', array(array('wl_user', 'wl_namespace', 'wl_title', 'wl_notificationtimestamp')),
72 array(
73 'wl_user' => $this->id,
74 'wl_namespace' => ($this->ns & ~1),
75 'wl_title' => $this->ti,
76 'wl_notificationtimestamp' => '0'
77 ), $fname );
78
79 # the following code compensates the new behaviour, introduced by the enotif patch,
80 # that every single watched page needs now to be listed in watchlist
81 # namespace:page and namespace_talk:page need separate entries: create them
82 $dbw->replace( 'watchlist', array(array('wl_user', 'wl_namespace', 'wl_title', 'wl_notificationtimestamp')),
83 array(
84 'wl_user' => $this->id,
85 'wl_namespace' => ($this->ns | 1 ),
86 'wl_title' => $this->ti,
87 'wl_notificationtimestamp' => '0'
88 ), $fname );
89
90 global $wgMemc;
91 $wgMemc->set( $this->watchkey(), 1 );
92 return true;
93 }
94
95 function removeWatch() {
96 $fname = 'WatchedItem::removeWatch';
97
98 $dbw =& wfGetDB( DB_MASTER );
99 $dbw->delete( 'watchlist',
100 array(
101 'wl_user' => $this->id,
102 'wl_namespace' => ($this->ns & ~1),
103 'wl_title' => $this->ti
104 ), $fname
105 );
106
107 # the following code compensates the new behaviour, introduced by the
108 # enotif patch, that every single watched page needs now to be listed
109 # in watchlist namespace:page and namespace_talk:page had separate
110 # entries: clear them
111 $dbw->delete( 'watchlist',
112 array(
113 'wl_user' => $this->id,
114 'wl_namespace' => ($this->ns | 1),
115 'wl_title' => $this->ti
116 ), $fname
117 );
118
119 if ( $dbw->affectedRows() ) {
120 global $wgMemc;
121 $wgMemc->set( $this->watchkey(), 0 );
122 return true;
123 } else {
124 return false;
125 }
126 }
127
128 /**
129 * @static
130 */
131 function duplicateEntries( $ot, $nt ) {
132 $fname = "WatchedItem::duplicateEntries";
133 global $wgMemc, $wgDBname;
134 $oldnamespace = $ot->getNamespace() & ~1;
135 $newnamespace = $nt->getNamespace() & ~1;
136 $oldtitle = $ot->getDBkey();
137 $newtitle = $nt->getDBkey();
138
139 $dbw =& wfGetDB( DB_MASTER );
140 $watchlist = $dbw->tableName( 'watchlist' );
141
142 $res = $dbw->select( 'watchlist', 'wl_user',
143 array( 'wl_namespace' => $oldnamespace, 'wl_title' => $oldtitle ),
144 $fname, 'FOR UPDATE'
145 );
146 # Construct array to replace into the watchlist
147 $values = array();
148 while ( $s = $dbw->fetchObject( $res ) ) {
149 $values[] = array(
150 'wl_user' => $s->wl_user,
151 'wl_namespace' => $newnamespace,
152 'wl_title' => $newtitle
153 );
154 }
155 $dbw->freeResult( $res );
156
157 # Perform replace
158 # Note that multi-row replace is very efficient for MySQL but may be inefficient for
159 # some other DBMSes, mostly due to poor simulation by us
160 $dbw->replace( 'watchlist', array(array( 'wl_user', 'wl_namespace', 'wl_title')), $values, $fname );
161 return true;
162 }
163
164
165 }
166
167 ?>