031b2b48b03f7c8ded6ca111ea3d2c1eb6122027
[lhc/web/wiklou.git] / includes / WatchedItem.php
1 <?php
2 /**
3 * @file
4 * @ingroup Watchlist
5 */
6
7 /**
8 * @ingroup Watchlist
9 */
10 class WatchedItem {
11 var $mTitle, $mUser, $id, $ns, $ti;
12
13 /**
14 * Create a WatchedItem object with the given user and title
15 * @param $user User: the user to use for (un)watching
16 * @param $title Title: the title we're going to (un)watch
17 * @return WatchedItem object
18 */
19 public static 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 * @return bool
37 */
38 public function isWatched() {
39 # Pages and their talk pages are considered equivalent for watching;
40 # remember that talk namespaces are numbered as page namespace+1.
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 ), __METHOD__ );
45 $iswatched = ($dbr->numRows( $res ) > 0) ? 1 : 0;
46 return $iswatched;
47 }
48
49 /**
50 * Given a title and user (assumes the object is setup), add the watch to the
51 * database.
52 * @return bool (always true)
53 */
54 public function addWatch() {
55 wfProfileIn( __METHOD__ );
56
57 // Use INSERT IGNORE to avoid overwriting the notification timestamp
58 // if there's already an entry for this page
59 $dbw = wfGetDB( DB_MASTER );
60 $dbw->insert( 'watchlist',
61 array(
62 'wl_user' => $this->id,
63 'wl_namespace' => MWNamespace::getSubject($this->ns),
64 'wl_title' => $this->ti,
65 'wl_notificationtimestamp' => null
66 ), __METHOD__, 'IGNORE' );
67
68 // Every single watched page needs now to be listed in watchlist;
69 // namespace:page and namespace_talk:page need separate entries:
70 $dbw->insert( 'watchlist',
71 array(
72 'wl_user' => $this->id,
73 'wl_namespace' => MWNamespace::getTalk($this->ns),
74 'wl_title' => $this->ti,
75 'wl_notificationtimestamp' => null
76 ), __METHOD__, 'IGNORE' );
77
78 wfProfileOut( __METHOD__ );
79 return true;
80 }
81
82 /**
83 * Same as addWatch, only the opposite.
84 * @return bool
85 */
86 public function removeWatch() {
87 wfProfileIn( __METHOD__ );
88
89 $success = false;
90 $dbw = wfGetDB( DB_MASTER );
91 $dbw->delete( 'watchlist',
92 array(
93 'wl_user' => $this->id,
94 'wl_namespace' => MWNamespace::getSubject($this->ns),
95 'wl_title' => $this->ti
96 ), __METHOD__
97 );
98 if ( $dbw->affectedRows() ) {
99 $success = true;
100 }
101
102 # the following code compensates the new behaviour, introduced by the
103 # enotif patch, that every single watched page needs now to be listed
104 # in watchlist namespace:page and namespace_talk:page had separate
105 # entries: clear them
106 $dbw->delete( 'watchlist',
107 array(
108 'wl_user' => $this->id,
109 'wl_namespace' => MWNamespace::getTalk($this->ns),
110 'wl_title' => $this->ti
111 ), __METHOD__
112 );
113
114 if ( $dbw->affectedRows() ) {
115 $success = true;
116 }
117
118 wfProfileOut( __METHOD__ );
119 return $success;
120 }
121
122 /**
123 * Check if the given title already is watched by the user, and if so
124 * add watches on a new title. To be used for page renames and such.
125 *
126 * @param $ot Title: page title to duplicate entries from, if present
127 * @param $nt Title: page title to add watches on
128 */
129 public static function duplicateEntries( $ot, $nt ) {
130 WatchedItem::doDuplicateEntries( $ot->getSubjectPage(), $nt->getSubjectPage() );
131 WatchedItem::doDuplicateEntries( $ot->getTalkPage(), $nt->getTalkPage() );
132 }
133
134 /**
135 * Handle duplicate entries. Backend for duplicateEntries().
136 *
137 * @param $ot Title
138 * @param $nt Title
139 *
140 * @return bool
141 */
142 private static function doDuplicateEntries( $ot, $nt ) {
143 $oldnamespace = $ot->getNamespace();
144 $newnamespace = $nt->getNamespace();
145 $oldtitle = $ot->getDBkey();
146 $newtitle = $nt->getDBkey();
147
148 $dbw = wfGetDB( DB_MASTER );
149 $res = $dbw->select( 'watchlist', 'wl_user',
150 array( 'wl_namespace' => $oldnamespace, 'wl_title' => $oldtitle ),
151 __METHOD__, 'FOR UPDATE'
152 );
153 # Construct array to replace into the watchlist
154 $values = array();
155 foreach ( $res as $s ) {
156 $values[] = array(
157 'wl_user' => $s->wl_user,
158 'wl_namespace' => $newnamespace,
159 'wl_title' => $newtitle
160 );
161 }
162
163 if( empty( $values ) ) {
164 // Nothing to do
165 return true;
166 }
167
168 # Perform replace
169 # Note that multi-row replace is very efficient for MySQL but may be inefficient for
170 # some other DBMSes, mostly due to poor simulation by us
171 $dbw->replace( 'watchlist', array( array( 'wl_user', 'wl_namespace', 'wl_title' ) ), $values, __METHOD__ );
172 return true;
173 }
174 }