No need to do a database query when passing an IP address to User::idFromName()
[mediawiki.git] / includes / WatchedItem.php
blobecff5b55a436fae8e2ba0043eb577690943c62f5
1 <?php
2 /**
3 * @file
4 * @ingroup Watchlist
5 */
7 /**
8 * @ingroup Watchlist
9 */
10 class WatchedItem {
11 var $mTitle, $mUser, $id, $ns, $ti;
12 private $loaded = false, $watched, $timestamp;
14 /**
15 * Create a WatchedItem object with the given user and title
16 * @param $user User: the user to use for (un)watching
17 * @param $title Title: the title we're going to (un)watch
18 * @return WatchedItem object
20 public static function fromUserTitle( $user, $title ) {
21 $wl = new WatchedItem;
22 $wl->mUser = $user;
23 $wl->mTitle = $title;
24 $wl->id = $user->getId();
25 # Patch (also) for email notification on page changes T.Gries/M.Arndt 11.09.2004
26 # TG patch: here we do not consider pages and their talk pages equivalent - why should we ?
27 # The change results in talk-pages not automatically included in watchlists, when their parent page is included
28 # $wl->ns = $title->getNamespace() & ~1;
29 $wl->ns = $title->getNamespace();
31 $wl->ti = $title->getDBkey();
32 return $wl;
35 /**
36 * Return an array of conditions to select or update the appropriate database
37 * row.
39 * @return array
41 private function dbCond() {
42 return array( 'wl_user' => $this->id, 'wl_namespace' => $this->ns, 'wl_title' => $this->ti );
45 /**
46 * Load the object from the database
48 private function load() {
49 if ( $this->loaded ) {
50 return;
52 $this->loaded = true;
54 # Pages and their talk pages are considered equivalent for watching;
55 # remember that talk namespaces are numbered as page namespace+1.
57 $dbr = wfGetDB( DB_SLAVE );
58 $row = $dbr->selectRow( 'watchlist', 'wl_notificationtimestamp',
59 $this->dbCond(), __METHOD__ );
61 if ( $row === false ) {
62 $this->watched = false;
63 } else {
64 $this->watched = true;
65 $this->timestamp = $row->wl_notificationtimestamp;
69 /**
70 * Is mTitle being watched by mUser?
71 * @return bool
73 public function isWatched() {
74 $this->load();
75 return $this->watched;
78 /**
79 * Get the notification timestamp of this entry.
81 * @return false|null|string: false if the page is not watched, the value of
82 * the wl_notificationtimestamp field otherwise
84 public function getNotificationTimestamp() {
85 $this->load();
86 if ( $this->watched ) {
87 return $this->timestamp;
88 } else {
89 return false;
93 /**
94 * Reset the notification timestamp of this entry
96 * @param $force Whether to force the write query to be executed even if the
97 * page is not watched or the notification timestamp is already NULL.
99 public function resetNotificationTimestamp( $force = '' ) {
100 if ( $force != 'force' ) {
101 $this->load();
102 if ( !$this->watched || $this->timestamp === null ) {
103 return;
107 // If the page is watched by the user (or may be watched), update the timestamp on any
108 // any matching rows
109 $dbw = wfGetDB( DB_MASTER );
110 $dbw->update( 'watchlist', array( 'wl_notificationtimestamp' => null ),
111 $this->dbCond(), __METHOD__ );
112 $this->timestamp = null;
116 * Given a title and user (assumes the object is setup), add the watch to the
117 * database.
118 * @return bool (always true)
120 public function addWatch() {
121 wfProfileIn( __METHOD__ );
123 // Use INSERT IGNORE to avoid overwriting the notification timestamp
124 // if there's already an entry for this page
125 $dbw = wfGetDB( DB_MASTER );
126 $dbw->insert( 'watchlist',
127 array(
128 'wl_user' => $this->id,
129 'wl_namespace' => MWNamespace::getSubject($this->ns),
130 'wl_title' => $this->ti,
131 'wl_notificationtimestamp' => null
132 ), __METHOD__, 'IGNORE' );
134 // Every single watched page needs now to be listed in watchlist;
135 // namespace:page and namespace_talk:page need separate entries:
136 $dbw->insert( 'watchlist',
137 array(
138 'wl_user' => $this->id,
139 'wl_namespace' => MWNamespace::getTalk($this->ns),
140 'wl_title' => $this->ti,
141 'wl_notificationtimestamp' => null
142 ), __METHOD__, 'IGNORE' );
144 $this->watched = true;
146 wfProfileOut( __METHOD__ );
147 return true;
151 * Same as addWatch, only the opposite.
152 * @return bool
154 public function removeWatch() {
155 wfProfileIn( __METHOD__ );
157 $success = false;
158 $dbw = wfGetDB( DB_MASTER );
159 $dbw->delete( 'watchlist',
160 array(
161 'wl_user' => $this->id,
162 'wl_namespace' => MWNamespace::getSubject($this->ns),
163 'wl_title' => $this->ti
164 ), __METHOD__
166 if ( $dbw->affectedRows() ) {
167 $success = true;
170 # the following code compensates the new behaviour, introduced by the
171 # enotif patch, that every single watched page needs now to be listed
172 # in watchlist namespace:page and namespace_talk:page had separate
173 # entries: clear them
174 $dbw->delete( 'watchlist',
175 array(
176 'wl_user' => $this->id,
177 'wl_namespace' => MWNamespace::getTalk($this->ns),
178 'wl_title' => $this->ti
179 ), __METHOD__
182 if ( $dbw->affectedRows() ) {
183 $success = true;
186 $this->watched = false;
188 wfProfileOut( __METHOD__ );
189 return $success;
193 * Check if the given title already is watched by the user, and if so
194 * add watches on a new title. To be used for page renames and such.
196 * @param $ot Title: page title to duplicate entries from, if present
197 * @param $nt Title: page title to add watches on
199 public static function duplicateEntries( $ot, $nt ) {
200 WatchedItem::doDuplicateEntries( $ot->getSubjectPage(), $nt->getSubjectPage() );
201 WatchedItem::doDuplicateEntries( $ot->getTalkPage(), $nt->getTalkPage() );
205 * Handle duplicate entries. Backend for duplicateEntries().
207 * @param $ot Title
208 * @param $nt Title
210 * @return bool
212 private static function doDuplicateEntries( $ot, $nt ) {
213 $oldnamespace = $ot->getNamespace();
214 $newnamespace = $nt->getNamespace();
215 $oldtitle = $ot->getDBkey();
216 $newtitle = $nt->getDBkey();
218 $dbw = wfGetDB( DB_MASTER );
219 $res = $dbw->select( 'watchlist', 'wl_user',
220 array( 'wl_namespace' => $oldnamespace, 'wl_title' => $oldtitle ),
221 __METHOD__, 'FOR UPDATE'
223 # Construct array to replace into the watchlist
224 $values = array();
225 foreach ( $res as $s ) {
226 $values[] = array(
227 'wl_user' => $s->wl_user,
228 'wl_namespace' => $newnamespace,
229 'wl_title' => $newtitle
233 if( empty( $values ) ) {
234 // Nothing to do
235 return true;
238 # Perform replace
239 # Note that multi-row replace is very efficient for MySQL but may be inefficient for
240 # some other DBMSes, mostly due to poor simulation by us
241 $dbw->replace( 'watchlist', array( array( 'wl_user', 'wl_namespace', 'wl_title' ) ), $values, __METHOD__ );
242 return true;