fix some doxygen warnings
[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 $fname = 'WatchedItem::isWatched';
42
43 $dbr = wfGetDB( DB_SLAVE );
44 $res = $dbr->select( 'watchlist', 1, array( 'wl_user' => $this->id, 'wl_namespace' => $this->ns,
45 'wl_title' => $this->ti ), $fname );
46 $iswatched = ($dbr->numRows( $res ) > 0) ? 1 : 0;
47 return $iswatched;
48 }
49
50 /**
51 * Given a title and user (assumes the object is setup), add the watch to the
52 * database.
53 * @return bool (always true)
54 */
55 public function addWatch() {
56 $fname = 'WatchedItem::addWatch';
57 wfProfileIn( $fname );
58
59 // Use INSERT IGNORE to avoid overwriting the notification timestamp
60 // if there's already an entry for this page
61 $dbw = wfGetDB( DB_MASTER );
62 $dbw->insert( 'watchlist',
63 array(
64 'wl_user' => $this->id,
65 'wl_namespace' => ($this->ns & ~1),
66 'wl_title' => $this->ti,
67 'wl_notificationtimestamp' => NULL
68 ), $fname, 'IGNORE' );
69
70 // Every single watched page needs now to be listed in watchlist;
71 // namespace:page and namespace_talk:page need separate entries:
72 $dbw->insert( 'watchlist',
73 array(
74 'wl_user' => $this->id,
75 'wl_namespace' => ($this->ns | 1 ),
76 'wl_title' => $this->ti,
77 'wl_notificationtimestamp' => NULL
78 ), $fname, 'IGNORE' );
79
80 wfProfileOut( $fname );
81 return true;
82 }
83
84 /**
85 * Same as addWatch, only the opposite.
86 * @return bool
87 */
88 public function removeWatch() {
89 $fname = 'WatchedItem::removeWatch';
90
91 $success = false;
92 $dbw = wfGetDB( DB_MASTER );
93 $dbw->delete( 'watchlist',
94 array(
95 'wl_user' => $this->id,
96 'wl_namespace' => ($this->ns & ~1),
97 'wl_title' => $this->ti
98 ), $fname
99 );
100 if ( $dbw->affectedRows() ) {
101 $success = true;
102 }
103
104 # the following code compensates the new behaviour, introduced by the
105 # enotif patch, that every single watched page needs now to be listed
106 # in watchlist namespace:page and namespace_talk:page had separate
107 # entries: clear them
108 $dbw->delete( 'watchlist',
109 array(
110 'wl_user' => $this->id,
111 'wl_namespace' => ($this->ns | 1),
112 'wl_title' => $this->ti
113 ), $fname
114 );
115
116 if ( $dbw->affectedRows() ) {
117 $success = true;
118 }
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 $fname = "WatchedItem::duplicateEntries";
139 $oldnamespace = $ot->getNamespace();
140 $newnamespace = $nt->getNamespace();
141 $oldtitle = $ot->getDBkey();
142 $newtitle = $nt->getDBkey();
143
144 $dbw = wfGetDB( DB_MASTER );
145 $res = $dbw->select( 'watchlist', 'wl_user',
146 array( 'wl_namespace' => $oldnamespace, 'wl_title' => $oldtitle ),
147 $fname, 'FOR UPDATE'
148 );
149 # Construct array to replace into the watchlist
150 $values = array();
151 while ( $s = $dbw->fetchObject( $res ) ) {
152 $values[] = array(
153 'wl_user' => $s->wl_user,
154 'wl_namespace' => $newnamespace,
155 'wl_title' => $newtitle
156 );
157 }
158 $dbw->freeResult( $res );
159
160 if( empty( $values ) ) {
161 // Nothing to do
162 return true;
163 }
164
165 # Perform replace
166 # Note that multi-row replace is very efficient for MySQL but may be inefficient for
167 # some other DBMSes, mostly due to poor simulation by us
168 $dbw->replace( 'watchlist', array(array( 'wl_user', 'wl_namespace', 'wl_title')), $values, $fname );
169 return true;
170 }
171 }