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