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