Update formatting
[lhc/web/wiklou.git] / includes / actions / WatchAction.php
1 <?php
2 /**
3 * Performs the watch and unwatch actions on a page
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
18 *
19 * @file
20 * @ingroup Actions
21 */
22
23 /**
24 * Page addition to a user's watchlist
25 *
26 * @ingroup Actions
27 */
28 class WatchAction extends FormAction {
29
30 public function getName() {
31 return 'watch';
32 }
33
34 public function requiresUnblock() {
35 return false;
36 }
37
38 protected function getDescription() {
39 return $this->msg( 'addwatch' )->escaped();
40 }
41
42 /**
43 * Just get an empty form with a single submit button
44 * @return array
45 */
46 protected function getFormFields() {
47 return array();
48 }
49
50 public function onSubmit( $data ) {
51 wfProfileIn( __METHOD__ );
52 self::doWatch( $this->getTitle(), $this->getUser() );
53 wfProfileOut( __METHOD__ );
54
55 return true;
56 }
57
58 /**
59 * This can be either formed or formless depending on the session token given
60 */
61 public function show() {
62 $this->setHeaders();
63
64 $user = $this->getUser();
65 // This will throw exceptions if there's a problem
66 $this->checkCanExecute( $user );
67
68 // Must have valid token for this action/title
69 $salt = array( $this->getName(), $this->getTitle()->getDBkey() );
70
71 if ( $user->matchEditToken( $this->getRequest()->getVal( 'token' ), $salt ) ) {
72 $this->onSubmit( array() );
73 $this->onSuccess();
74 } else {
75 $form = $this->getForm();
76 if ( $form->show() ) {
77 $this->onSuccess();
78 }
79 }
80 }
81
82 protected function checkCanExecute( User $user ) {
83 // Must be logged in
84 if ( $user->isAnon() ) {
85 throw new ErrorPageError( 'watchnologin', 'watchnologintext' );
86 }
87
88 return parent::checkCanExecute( $user );
89 }
90
91 /**
92 * Watch or unwatch a page
93 * @since 1.22
94 * @param bool $watch Whether to watch or unwatch the page
95 * @param Title $title Page to watch/unwatch
96 * @param User $user User who is watching/unwatching
97 * @return Status
98 */
99 public static function doWatchOrUnwatch( $watch, Title $title, User $user ) {
100 if ( $user->isLoggedIn() && $user->isWatched( $title, WatchedItem::IGNORE_USER_RIGHTS ) != $watch ) {
101 // If the user doesn't have 'editmywatchlist', we still want to
102 // allow them to add but not remove items via edits and such.
103 if ( $watch ) {
104 return self::doWatch( $title, $user, WatchedItem::IGNORE_USER_RIGHTS );
105 } else {
106 return self::doUnwatch( $title, $user );
107 }
108 }
109
110 return Status::newGood();
111 }
112
113 /**
114 * Watch a page
115 * @since 1.22 Returns Status, $checkRights parameter added
116 * @param Title $title Page to watch/unwatch
117 * @param User $user User who is watching/unwatching
118 * @param int $checkRights Passed through to $user->addWatch()
119 * @return Status
120 */
121 public static function doWatch( Title $title, User $user, $checkRights = WatchedItem::CHECK_USER_RIGHTS ) {
122 if ( $checkRights !== WatchedItem::IGNORE_USER_RIGHTS && !$user->isAllowed( 'editmywatchlist' ) ) {
123 return User::newFatalPermissionDeniedStatus( 'editmywatchlist' );
124 }
125
126 $page = WikiPage::factory( $title );
127
128 $status = Status::newFatal( 'hookaborted' );
129 if ( wfRunHooks( 'WatchArticle', array( &$user, &$page, &$status ) ) ) {
130 $status = Status::newGood();
131 $user->addWatch( $title, $checkRights );
132 wfRunHooks( 'WatchArticleComplete', array( &$user, &$page ) );
133 }
134
135 return $status;
136 }
137
138 /**
139 * Unwatch a page
140 * @since 1.22 Returns Status
141 * @param Title $title Page to watch/unwatch
142 * @param User $user User who is watching/unwatching
143 * @return Status
144 */
145 public static function doUnwatch( Title $title, User $user ) {
146 if ( !$user->isAllowed( 'editmywatchlist' ) ) {
147 return User::newFatalPermissionDeniedStatus( 'editmywatchlist' );
148 }
149
150 $page = WikiPage::factory( $title );
151
152 $status = Status::newFatal( 'hookaborted' );
153 if ( wfRunHooks( 'UnwatchArticle', array( &$user, &$page, &$status ) ) ) {
154 $status = Status::newGood();
155 $user->removeWatch( $title );
156 wfRunHooks( 'UnwatchArticleComplete', array( &$user, &$page ) );
157 }
158
159 return $status;
160 }
161
162 /**
163 * Get token to watch (or unwatch) a page for a user
164 *
165 * @param Title $title Title object of page to watch
166 * @param User $user User for whom the action is going to be performed
167 * @param string $action Optionally override the action to 'unwatch'
168 * @return string Token
169 * @since 1.18
170 */
171 public static function getWatchToken( Title $title, User $user, $action = 'watch' ) {
172 if ( $action != 'unwatch' ) {
173 $action = 'watch';
174 }
175 $salt = array( $action, $title->getDBkey() );
176
177 // This token stronger salted and not compatible with ApiWatch
178 // It's title/action specific because index.php is GET and API is POST
179 return $user->getEditToken( $salt );
180 }
181
182 /**
183 * Get token to unwatch (or watch) a page for a user
184 *
185 * @param Title $title Title object of page to unwatch
186 * @param User $user User for whom the action is going to be performed
187 * @param string $action Optionally override the action to 'watch'
188 * @return string Token
189 * @since 1.18
190 */
191 public static function getUnwatchToken( Title $title, User $user, $action = 'unwatch' ) {
192 return self::getWatchToken( $title, $user, $action );
193 }
194
195 protected function alterForm( HTMLForm $form ) {
196 $form->setSubmitTextMsg( 'confirm-watch-button' );
197 }
198
199 protected function preText() {
200 return $this->msg( 'confirm-watch-top' )->parse();
201 }
202
203 public function onSuccess() {
204 $this->getOutput()->addWikiMsg( 'addedwatchtext', $this->getTitle()->getPrefixedText() );
205 }
206 }
207
208 /**
209 * Page removal from a user's watchlist
210 *
211 * @ingroup Actions
212 */
213 class UnwatchAction extends WatchAction {
214
215 public function getName() {
216 return 'unwatch';
217 }
218
219 protected function getDescription() {
220 return $this->msg( 'removewatch' )->escaped();
221 }
222
223 public function onSubmit( $data ) {
224 wfProfileIn( __METHOD__ );
225 self::doUnwatch( $this->getTitle(), $this->getUser() );
226 wfProfileOut( __METHOD__ );
227
228 return true;
229 }
230
231 protected function alterForm( HTMLForm $form ) {
232 $form->setSubmitTextMsg( 'confirm-unwatch-button' );
233 }
234
235 protected function preText() {
236 return $this->msg( 'confirm-unwatch-top' )->parse();
237 }
238
239 public function onSuccess() {
240 $this->getOutput()->addWikiMsg( 'removedwatchtext', $this->getTitle()->getPrefixedText() );
241 }
242 }