Merge "Rename autonym for 'no' from 'norsk bokmål' to 'norsk'"
[lhc/web/wiklou.git] / includes / actions / WatchAction.php
1 <?php
2 /**
3 * Performs the watch 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 '';
40 }
41
42 public function onSubmit( $data ) {
43 self::doWatch( $this->getTitle(), $this->getUser() );
44
45 return true;
46 }
47
48 protected function checkCanExecute( User $user ) {
49 // Must be logged in
50 if ( $user->isAnon() ) {
51 throw new UserNotLoggedIn( 'watchlistanontext', 'watchnologin' );
52 }
53
54 parent::checkCanExecute( $user );
55 }
56
57 protected function usesOOUI() {
58 return true;
59 }
60
61 protected function getFormFields() {
62 return [
63 'intro' => [
64 'type' => 'info',
65 'vertical-label' => true,
66 'raw' => true,
67 'default' => $this->msg( 'confirm-watch-top' )->parse()
68 ]
69 ];
70 }
71
72 protected function alterForm( HTMLForm $form ) {
73 $form->setWrapperLegendMsg( 'addwatch' );
74 $form->setSubmitTextMsg( 'confirm-watch-button' );
75 $form->setTokenSalt( 'watch' );
76 }
77
78 public function onSuccess() {
79 $msgKey = $this->getTitle()->isTalkPage() ? 'addedwatchtext-talk' : 'addedwatchtext';
80 $this->getOutput()->addWikiMsg( $msgKey, $this->getTitle()->getPrefixedText() );
81 }
82
83 /* Static utility methods */
84
85 /**
86 * Watch or unwatch a page
87 * @since 1.22
88 * @param bool $watch Whether to watch or unwatch the page
89 * @param Title $title Page to watch/unwatch
90 * @param User $user User who is watching/unwatching
91 * @return Status
92 */
93 public static function doWatchOrUnwatch( $watch, Title $title, User $user ) {
94 if ( $user->isLoggedIn() &&
95 $user->isWatched( $title, User::IGNORE_USER_RIGHTS ) != $watch
96 ) {
97 // If the user doesn't have 'editmywatchlist', we still want to
98 // allow them to add but not remove items via edits and such.
99 if ( $watch ) {
100 return self::doWatch( $title, $user, User::IGNORE_USER_RIGHTS );
101 } else {
102 return self::doUnwatch( $title, $user );
103 }
104 }
105
106 return Status::newGood();
107 }
108
109 /**
110 * Watch a page
111 * @since 1.22 Returns Status, $checkRights parameter added
112 * @param Title $title Page to watch/unwatch
113 * @param User $user User who is watching/unwatching
114 * @param bool $checkRights Passed through to $user->addWatch()
115 * Pass User::CHECK_USER_RIGHTS or User::IGNORE_USER_RIGHTS.
116 * @return Status
117 */
118 public static function doWatch(
119 Title $title,
120 User $user,
121 $checkRights = User::CHECK_USER_RIGHTS
122 ) {
123 if ( $checkRights && !$user->isAllowed( 'editmywatchlist' ) ) {
124 return User::newFatalPermissionDeniedStatus( 'editmywatchlist' );
125 }
126
127 $page = WikiPage::factory( $title );
128
129 $status = Status::newFatal( 'hookaborted' );
130 if ( Hooks::run( 'WatchArticle', [ &$user, &$page, &$status ] ) ) {
131 $status = Status::newGood();
132 $user->addWatch( $title, $checkRights );
133 Hooks::run( 'WatchArticleComplete', [ &$user, &$page ] );
134 }
135
136 return $status;
137 }
138
139 /**
140 * Unwatch a page
141 * @since 1.22 Returns Status
142 * @param Title $title Page to watch/unwatch
143 * @param User $user User who is watching/unwatching
144 * @return Status
145 */
146 public static function doUnwatch( Title $title, User $user ) {
147 if ( !$user->isAllowed( 'editmywatchlist' ) ) {
148 return User::newFatalPermissionDeniedStatus( 'editmywatchlist' );
149 }
150
151 $page = WikiPage::factory( $title );
152
153 $status = Status::newFatal( 'hookaborted' );
154 if ( Hooks::run( 'UnwatchArticle', [ &$user, &$page, &$status ] ) ) {
155 $status = Status::newGood();
156 $user->removeWatch( $title );
157 Hooks::run( 'UnwatchArticleComplete', [ &$user, &$page ] );
158 }
159
160 return $status;
161 }
162
163 /**
164 * Get token to watch (or unwatch) a page for a user
165 *
166 * @param Title $title Title object of page to watch
167 * @param User $user User for whom the action is going to be performed
168 * @param string $action Optionally override the action to 'unwatch'
169 * @return string Token
170 * @since 1.18
171 */
172 public static function getWatchToken( Title $title, User $user, $action = 'watch' ) {
173 if ( $action != 'unwatch' ) {
174 $action = 'watch';
175 }
176 // Match ApiWatch and ResourceLoaderUserTokensModule
177 return $user->getEditToken( $action );
178 }
179
180 /**
181 * Get token to unwatch (or watch) a page for a user
182 *
183 * @param Title $title Title object of page to unwatch
184 * @param User $user User for whom the action is going to be performed
185 * @param string $action Optionally override the action to 'watch'
186 * @return string Token
187 * @since 1.18
188 */
189 public static function getUnwatchToken( Title $title, User $user, $action = 'unwatch' ) {
190 return self::getWatchToken( $title, $user, $action );
191 }
192
193 public function doesWrites() {
194 return true;
195 }
196 }