Merge "jsduck: Add @inheritable to static methods from jQuery plugins"
[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 $loginreqlink = Linker::linkKnown(
86 SpecialPage::getTitleFor( 'Userlogin' ),
87 $this->msg( 'loginreqlink' )->escaped(),
88 array(),
89 array( 'returnto' => $this->getPageTitle(), 'returntoquery' => 'action=' . $this->getName() )
90 );
91 $reasonMsg = $this->msg( 'watchlistanontext' )->rawParams( $loginreqlink );
92 throw new UserNotLoggedIn( $reasonMsg, 'watchnologin' );
93 }
94
95 return parent::checkCanExecute( $user );
96 }
97
98 /**
99 * Watch or unwatch a page
100 * @since 1.22
101 * @param bool $watch Whether to watch or unwatch the page
102 * @param Title $title Page to watch/unwatch
103 * @param User $user User who is watching/unwatching
104 * @return Status
105 */
106 public static function doWatchOrUnwatch( $watch, Title $title, User $user ) {
107 if ( $user->isLoggedIn() &&
108 $user->isWatched( $title, WatchedItem::IGNORE_USER_RIGHTS ) != $watch
109 ) {
110 // If the user doesn't have 'editmywatchlist', we still want to
111 // allow them to add but not remove items via edits and such.
112 if ( $watch ) {
113 return self::doWatch( $title, $user, WatchedItem::IGNORE_USER_RIGHTS );
114 } else {
115 return self::doUnwatch( $title, $user );
116 }
117 }
118
119 return Status::newGood();
120 }
121
122 /**
123 * Watch a page
124 * @since 1.22 Returns Status, $checkRights parameter added
125 * @param Title $title Page to watch/unwatch
126 * @param User $user User who is watching/unwatching
127 * @param int $checkRights Passed through to $user->addWatch()
128 * @return Status
129 */
130 public static function doWatch( Title $title, User $user,
131 $checkRights = WatchedItem::CHECK_USER_RIGHTS
132 ) {
133 if ( $checkRights !== WatchedItem::IGNORE_USER_RIGHTS &&
134 !$user->isAllowed( 'editmywatchlist' )
135 ) {
136 return User::newFatalPermissionDeniedStatus( 'editmywatchlist' );
137 }
138
139 $page = WikiPage::factory( $title );
140
141 $status = Status::newFatal( 'hookaborted' );
142 if ( wfRunHooks( 'WatchArticle', array( &$user, &$page, &$status ) ) ) {
143 $status = Status::newGood();
144 $user->addWatch( $title, $checkRights );
145 wfRunHooks( 'WatchArticleComplete', array( &$user, &$page ) );
146 }
147
148 return $status;
149 }
150
151 /**
152 * Unwatch a page
153 * @since 1.22 Returns Status
154 * @param Title $title Page to watch/unwatch
155 * @param User $user User who is watching/unwatching
156 * @return Status
157 */
158 public static function doUnwatch( Title $title, User $user ) {
159 if ( !$user->isAllowed( 'editmywatchlist' ) ) {
160 return User::newFatalPermissionDeniedStatus( 'editmywatchlist' );
161 }
162
163 $page = WikiPage::factory( $title );
164
165 $status = Status::newFatal( 'hookaborted' );
166 if ( wfRunHooks( 'UnwatchArticle', array( &$user, &$page, &$status ) ) ) {
167 $status = Status::newGood();
168 $user->removeWatch( $title );
169 wfRunHooks( 'UnwatchArticleComplete', array( &$user, &$page ) );
170 }
171
172 return $status;
173 }
174
175 /**
176 * Get token to watch (or unwatch) a page for a user
177 *
178 * @param Title $title Title object of page to watch
179 * @param User $user User for whom the action is going to be performed
180 * @param string $action Optionally override the action to 'unwatch'
181 * @return string Token
182 * @since 1.18
183 */
184 public static function getWatchToken( Title $title, User $user, $action = 'watch' ) {
185 if ( $action != 'unwatch' ) {
186 $action = 'watch';
187 }
188 $salt = array( $action, $title->getDBkey() );
189
190 // This token stronger salted and not compatible with ApiWatch
191 // It's title/action specific because index.php is GET and API is POST
192 return $user->getEditToken( $salt );
193 }
194
195 /**
196 * Get token to unwatch (or watch) a page for a user
197 *
198 * @param Title $title Title object of page to unwatch
199 * @param User $user User for whom the action is going to be performed
200 * @param string $action Optionally override the action to 'watch'
201 * @return string Token
202 * @since 1.18
203 */
204 public static function getUnwatchToken( Title $title, User $user, $action = 'unwatch' ) {
205 return self::getWatchToken( $title, $user, $action );
206 }
207
208 protected function alterForm( HTMLForm $form ) {
209 $form->setSubmitTextMsg( 'confirm-watch-button' );
210 }
211
212 protected function preText() {
213 return $this->msg( 'confirm-watch-top' )->parse();
214 }
215
216 public function onSuccess() {
217 $this->getOutput()->addWikiMsg( 'addedwatchtext', $this->getTitle()->getPrefixedText() );
218 }
219 }
220
221 /**
222 * Page removal from a user's watchlist
223 *
224 * @ingroup Actions
225 */
226 class UnwatchAction extends WatchAction {
227
228 public function getName() {
229 return 'unwatch';
230 }
231
232 protected function getDescription() {
233 return $this->msg( 'removewatch' )->escaped();
234 }
235
236 public function onSubmit( $data ) {
237 wfProfileIn( __METHOD__ );
238 self::doUnwatch( $this->getTitle(), $this->getUser() );
239 wfProfileOut( __METHOD__ );
240
241 return true;
242 }
243
244 protected function alterForm( HTMLForm $form ) {
245 $form->setSubmitTextMsg( 'confirm-unwatch-button' );
246 }
247
248 protected function preText() {
249 return $this->msg( 'confirm-unwatch-top' )->parse();
250 }
251
252 public function onSuccess() {
253 $this->getOutput()->addWikiMsg( 'removedwatchtext', $this->getTitle()->getPrefixedText() );
254 }
255 }