]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - includes/WatchedItem.php
MediaWiki 1.16.4
[autoinstalls/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                 $success = false;
88                 $dbw = wfGetDB( DB_MASTER );
89                 $dbw->delete( 'watchlist',
90                         array(
91                                 'wl_user' => $this->id,
92                                 'wl_namespace' => MWNamespace::getSubject($this->ns),
93                                 'wl_title' => $this->ti
94                         ), __METHOD__
95                 );
96                 if ( $dbw->affectedRows() ) {
97                         $success = true;
98                 }
99
100                 # the following code compensates the new behaviour, introduced by the
101                 # enotif patch, that every single watched page needs now to be listed
102                 # in watchlist namespace:page and namespace_talk:page had separate
103                 # entries: clear them
104                 $dbw->delete( 'watchlist',
105                         array(
106                                 'wl_user' => $this->id,
107                                 'wl_namespace' => MWNamespace::getTalk($this->ns),
108                                 'wl_title' => $this->ti
109                         ), __METHOD__
110                 );
111
112                 if ( $dbw->affectedRows() ) {
113                         $success = true;
114                 }
115                 return $success;
116         }
117
118         /**
119          * Check if the given title already is watched by the user, and if so
120          * add watches on a new title. To be used for page renames and such.
121          *
122          * @param $ot Title: page title to duplicate entries from, if present
123          * @param $nt Title: page title to add watches on
124          */
125         public static function duplicateEntries( $ot, $nt ) {
126                 WatchedItem::doDuplicateEntries( $ot->getSubjectPage(), $nt->getSubjectPage() );
127                 WatchedItem::doDuplicateEntries( $ot->getTalkPage(), $nt->getTalkPage() );
128         }
129
130         /**
131          * Handle duplicate entries. Backend for duplicateEntries().
132          */
133         private static function doDuplicateEntries( $ot, $nt ) {        
134                 $oldnamespace = $ot->getNamespace();
135                 $newnamespace = $nt->getNamespace();
136                 $oldtitle = $ot->getDBkey();
137                 $newtitle = $nt->getDBkey();
138
139                 $dbw = wfGetDB( DB_MASTER );
140                 $res = $dbw->select( 'watchlist', 'wl_user',
141                         array( 'wl_namespace' => $oldnamespace, 'wl_title' => $oldtitle ),
142                         __METHOD__, 'FOR UPDATE'
143                 );
144                 # Construct array to replace into the watchlist
145                 $values = array();
146                 while ( $s = $dbw->fetchObject( $res ) ) {
147                         $values[] = array(
148                                 'wl_user' => $s->wl_user,
149                                 'wl_namespace' => $newnamespace,
150                                 'wl_title' => $newtitle
151                         );
152                 }
153                 $dbw->freeResult( $res );
154
155                 if( empty( $values ) ) {
156                         // Nothing to do
157                         return true;
158                 }
159
160                 # Perform replace
161                 # Note that multi-row replace is very efficient for MySQL but may be inefficient for
162                 # some other DBMSes, mostly due to poor simulation by us
163                 $dbw->replace( 'watchlist', array( array( 'wl_user', 'wl_namespace', 'wl_title' ) ), $values, __METHOD__ );
164                 return true;
165         }
166 }