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