Merge "Fix installation failure due to unexpected dbpath under CLI installation"
[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 use MediaWiki\MediaWikiServices;
24
25 /**
26 * Page addition to a user's watchlist
27 *
28 * @ingroup Actions
29 */
30 class WatchAction extends FormAction {
31
32 public function getName() {
33 return 'watch';
34 }
35
36 public function requiresUnblock() {
37 return false;
38 }
39
40 protected function getDescription() {
41 return '';
42 }
43
44 public function onSubmit( $data ) {
45 return self::doWatch( $this->getTitle(), $this->getUser() );
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 $permissionManager = MediaWikiServices::getInstance()->getPermissionManager();
122 if ( $checkRights && !$permissionManager->userHasRight( $user, 'editmywatchlist' ) ) {
123 return User::newFatalPermissionDeniedStatus( 'editmywatchlist' );
124 }
125
126 $page = WikiPage::factory( $title );
127
128 $status = Status::newFatal( 'hookaborted' );
129 if ( Hooks::run( 'WatchArticle', [ &$user, &$page, &$status ] ) ) {
130 $status = Status::newGood();
131 $user->addWatch( $title, $checkRights );
132 Hooks::run( 'WatchArticleComplete', [ &$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 ( !MediaWikiServices::getInstance()
147 ->getPermissionManager()
148 ->userHasRight( $user, 'editmywatchlist' ) ) {
149 return User::newFatalPermissionDeniedStatus( 'editmywatchlist' );
150 }
151
152 $page = WikiPage::factory( $title );
153
154 $status = Status::newFatal( 'hookaborted' );
155 if ( Hooks::run( 'UnwatchArticle', [ &$user, &$page, &$status ] ) ) {
156 $status = Status::newGood();
157 $user->removeWatch( $title );
158 Hooks::run( 'UnwatchArticleComplete', [ &$user, &$page ] );
159 }
160
161 return $status;
162 }
163
164 /**
165 * Get token to watch (or unwatch) a page for a user
166 *
167 * @param Title $title Title object of page to watch
168 * @param User $user User for whom the action is going to be performed
169 * @param string $action Optionally override the action to 'unwatch'
170 * @return string Token
171 * @since 1.18
172 */
173 public static function getWatchToken( Title $title, User $user, $action = 'watch' ) {
174 if ( $action != 'unwatch' ) {
175 $action = 'watch';
176 }
177 // Match ApiWatch and ResourceLoaderUserTokensModule
178 return $user->getEditToken( $action );
179 }
180
181 public function doesWrites() {
182 return true;
183 }
184 }