]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - includes/WatchedItem.php
MediaWiki 1.11.0
[autoinstallsdev/mediawiki.git] / includes / WatchedItem.php
1 <?php
2 /**
3  *
4  */
5
6 /**
7  *
8  */
9 class WatchedItem {
10         var $mTitle, $mUser;
11
12         /**
13          * Create a WatchedItem object with the given user and title
14          * @todo document
15          * @access private
16          */
17         static function fromUserTitle( $user, $title ) {
18                 $wl = new WatchedItem;
19                 $wl->mUser = $user;
20                 $wl->mTitle = $title;
21                 $wl->id = $user->getId();
22 # Patch (also) for email notification on page changes T.Gries/M.Arndt 11.09.2004
23 # TG patch: here we do not consider pages and their talk pages equivalent - why should we ?
24 # The change results in talk-pages not automatically included in watchlists, when their parent page is included
25 #               $wl->ns = $title->getNamespace() & ~1;
26                 $wl->ns = $title->getNamespace();
27
28                 $wl->ti = $title->getDBkey();
29                 return $wl;
30         }
31
32         /**
33          * Is mTitle being watched by mUser?
34          */
35         function isWatched() {
36                 # Pages and their talk pages are considered equivalent for watching;
37                 # remember that talk namespaces are numbered as page namespace+1.
38                 $fname = 'WatchedItem::isWatched';
39
40                 $dbr = wfGetDB( DB_SLAVE );
41                 $res = $dbr->select( 'watchlist', 1, array( 'wl_user' => $this->id, 'wl_namespace' => $this->ns,
42                         'wl_title' => $this->ti ), $fname );
43                 $iswatched = ($dbr->numRows( $res ) > 0) ? 1 : 0;
44                 return $iswatched;
45         }
46
47         /**
48          * @todo document
49          */
50         function addWatch() {
51                 $fname = 'WatchedItem::addWatch';
52                 wfProfileIn( $fname );
53
54                 // Use INSERT IGNORE to avoid overwriting the notification timestamp
55                 // if there's already an entry for this page
56                 $dbw = wfGetDB( DB_MASTER );
57                 $dbw->insert( 'watchlist',
58                   array(
59                     'wl_user' => $this->id,
60                         'wl_namespace' => ($this->ns & ~1),
61                         'wl_title' => $this->ti,
62                         'wl_notificationtimestamp' => NULL
63                   ), $fname, 'IGNORE' );
64
65                 // Every single watched page needs now to be listed in watchlist;
66                 // namespace:page and namespace_talk:page need separate entries:
67                 $dbw->insert( 'watchlist',
68                   array(
69                         'wl_user' => $this->id,
70                         'wl_namespace' => ($this->ns | 1 ),
71                         'wl_title' => $this->ti,
72                         'wl_notificationtimestamp' => NULL
73                   ), $fname, 'IGNORE' );
74
75                 wfProfileOut( $fname );
76                 return true;
77         }
78
79         function removeWatch() {
80                 $fname = 'WatchedItem::removeWatch';
81
82                 $success = false;
83                 $dbw = wfGetDB( DB_MASTER );
84                 $dbw->delete( 'watchlist',
85                         array(
86                                 'wl_user' => $this->id,
87                                 'wl_namespace' => ($this->ns & ~1),
88                                 'wl_title' => $this->ti
89                         ), $fname
90                 );
91                 if ( $dbw->affectedRows() ) {
92                         $success = true;
93                 }
94
95                 # the following code compensates the new behaviour, introduced by the
96                 # enotif patch, that every single watched page needs now to be listed
97                 # in watchlist namespace:page and namespace_talk:page had separate
98                 # entries: clear them
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                 if ( $dbw->affectedRows() ) {
108                         $success = true;
109                 }
110                 return $success;
111         }
112
113         /**
114          * Check if the given title already is watched by the user, and if so
115          * add watches on a new title. To be used for page renames and such.
116          *
117          * @param Title $ot Page title to duplicate entries from, if present
118          * @param Title $nt Page title to add watches on
119          */
120         static function duplicateEntries( $ot, $nt ) {
121                 WatchedItem::doDuplicateEntries( $ot->getSubjectPage(), $nt->getSubjectPage() );
122                 WatchedItem::doDuplicateEntries( $ot->getTalkPage(), $nt->getTalkPage() );
123         }
124
125         private static function doDuplicateEntries( $ot, $nt ) {
126                 $fname = "WatchedItem::duplicateEntries";
127                 $oldnamespace = $ot->getNamespace();
128                 $newnamespace = $nt->getNamespace();
129                 $oldtitle = $ot->getDBkey();
130                 $newtitle = $nt->getDBkey();
131
132                 $dbw = wfGetDB( DB_MASTER );
133                 $res = $dbw->select( 'watchlist', 'wl_user',
134                         array( 'wl_namespace' => $oldnamespace, 'wl_title' => $oldtitle ),
135                         $fname, 'FOR UPDATE'
136                 );
137                 # Construct array to replace into the watchlist
138                 $values = array();
139                 while ( $s = $dbw->fetchObject( $res ) ) {
140                         $values[] = array(
141                                 'wl_user' => $s->wl_user,
142                                 'wl_namespace' => $newnamespace,
143                                 'wl_title' => $newtitle
144                         );
145                 }
146                 $dbw->freeResult( $res );
147
148                 if( empty( $values ) ) {
149                         // Nothing to do
150                         return true;
151                 }
152
153                 # Perform replace
154                 # Note that multi-row replace is very efficient for MySQL but may be inefficient for
155                 # some other DBMSes, mostly due to poor simulation by us
156                 $dbw->replace( 'watchlist', array(array( 'wl_user', 'wl_namespace', 'wl_title')), $values, $fname );
157                 return true;
158         }
159
160
161 }
162
163