Merge "Add semantic tags to license info text"
[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 /**
84 * Watch or unwatch a page
85 * @since 1.22
86 * @param bool $watch Whether to watch or unwatch the page
87 * @param Title $title Page to watch/unwatch
88 * @param User $user User who is watching/unwatching
89 * @return Status
90 */
91 public static function doWatchOrUnwatch( $watch, Title $title, User $user ) {
92 if ( $user->isLoggedIn() &&
93 $user->isWatched( $title, User::IGNORE_USER_RIGHTS ) != $watch
94 ) {
95 // If the user doesn't have 'editmywatchlist', we still want to
96 // allow them to add but not remove items via edits and such.
97 if ( $watch ) {
98 return self::doWatch( $title, $user, User::IGNORE_USER_RIGHTS );
99 } else {
100 return self::doUnwatch( $title, $user );
101 }
102 }
103
104 return Status::newGood();
105 }
106
107 /**
108 * Watch a page
109 * @since 1.22 Returns Status, $checkRights parameter added
110 * @param Title $title Page to watch/unwatch
111 * @param User $user User who is watching/unwatching
112 * @param bool $checkRights Passed through to $user->addWatch()
113 * Pass User::CHECK_USER_RIGHTS or User::IGNORE_USER_RIGHTS.
114 * @return Status
115 */
116 public static function doWatch(
117 Title $title,
118 User $user,
119 $checkRights = User::CHECK_USER_RIGHTS
120 ) {
121 if ( $checkRights && !$user->isAllowed( 'editmywatchlist' ) ) {
122 return User::newFatalPermissionDeniedStatus( 'editmywatchlist' );
123 }
124
125 $page = WikiPage::factory( $title );
126
127 $status = Status::newFatal( 'hookaborted' );
128 if ( Hooks::run( 'WatchArticle', [ &$user, &$page, &$status ] ) ) {
129 $status = Status::newGood();
130 $user->addWatch( $title, $checkRights );
131 Hooks::run( 'WatchArticleComplete', [ &$user, &$page ] );
132 }
133
134 return $status;
135 }
136
137 /**
138 * Unwatch a page
139 * @since 1.22 Returns Status
140 * @param Title $title Page to watch/unwatch
141 * @param User $user User who is watching/unwatching
142 * @return Status
143 */
144 public static function doUnwatch( Title $title, User $user ) {
145 if ( !$user->isAllowed( 'editmywatchlist' ) ) {
146 return User::newFatalPermissionDeniedStatus( 'editmywatchlist' );
147 }
148
149 $page = WikiPage::factory( $title );
150
151 $status = Status::newFatal( 'hookaborted' );
152 if ( Hooks::run( 'UnwatchArticle', [ &$user, &$page, &$status ] ) ) {
153 $status = Status::newGood();
154 $user->removeWatch( $title );
155 Hooks::run( 'UnwatchArticleComplete', [ &$user, &$page ] );
156 }
157
158 return $status;
159 }
160
161 /**
162 * Get token to watch (or unwatch) a page for a user
163 *
164 * @param Title $title Title object of page to watch
165 * @param User $user User for whom the action is going to be performed
166 * @param string $action Optionally override the action to 'unwatch'
167 * @return string Token
168 * @since 1.18
169 */
170 public static function getWatchToken( Title $title, User $user, $action = 'watch' ) {
171 if ( $action != 'unwatch' ) {
172 $action = 'watch';
173 }
174 // Match ApiWatch and ResourceLoaderUserTokensModule
175 return $user->getEditToken( $action );
176 }
177
178 /**
179 * Get token to unwatch (or watch) a page for a user
180 *
181 * @param Title $title Title object of page to unwatch
182 * @param User $user User for whom the action is going to be performed
183 * @param string $action Optionally override the action to 'watch'
184 * @return string Token
185 * @since 1.18
186 */
187 public static function getUnwatchToken( Title $title, User $user, $action = 'unwatch' ) {
188 return self::getWatchToken( $title, $user, $action );
189 }
190
191 public function doesWrites() {
192 return true;
193 }
194 }