]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - includes/WatchedItem.php
MediaWiki 1.17.1
[autoinstallsdev/mediawiki.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         private static function doDuplicateEntries( $ot, $nt ) {        
138                 $oldnamespace = $ot->getNamespace();
139                 $newnamespace = $nt->getNamespace();
140                 $oldtitle = $ot->getDBkey();
141                 $newtitle = $nt->getDBkey();
142
143                 $dbw = wfGetDB( DB_MASTER );
144                 $res = $dbw->select( 'watchlist', 'wl_user',
145                         array( 'wl_namespace' => $oldnamespace, 'wl_title' => $oldtitle ),
146                         __METHOD__, 'FOR UPDATE'
147                 );
148                 # Construct array to replace into the watchlist
149                 $values = array();
150                 foreach ( $res as $s ) {
151                         $values[] = array(
152                                 'wl_user' => $s->wl_user,
153                                 'wl_namespace' => $newnamespace,
154                                 'wl_title' => $newtitle
155                         );
156                 }
157
158                 if( empty( $values ) ) {
159                         // Nothing to do
160                         return true;
161                 }
162
163                 # Perform replace
164                 # Note that multi-row replace is very efficient for MySQL but may be inefficient for
165                 # some other DBMSes, mostly due to poor simulation by us
166                 $dbw->replace( 'watchlist', array( array( 'wl_user', 'wl_namespace', 'wl_title' ) ), $values, __METHOD__ );
167                 return true;
168         }
169 }