Merge "Don't check namespace in SpecialWantedtemplates"
[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 $this->msg( 'addwatch' )->escaped();
40 }
41
42 /**
43 * Just get an empty form with a single submit button
44 * @return array
45 */
46 protected function getFormFields() {
47 return array();
48 }
49
50 public function onSubmit( $data ) {
51 self::doWatch( $this->getTitle(), $this->getUser() );
52
53 return true;
54 }
55
56 /**
57 * This can be either formed or formless depending on the session token given
58 */
59 public function show() {
60 $this->setHeaders();
61
62 $user = $this->getUser();
63 // This will throw exceptions if there's a problem
64 $this->checkCanExecute( $user );
65
66 // Must have valid token for this action/title
67 $salt = array( $this->getName(), $this->getTitle()->getPrefixedDBkey() );
68
69 if ( $user->matchEditToken( $this->getRequest()->getVal( 'token' ), $salt ) ) {
70 $this->onSubmit( array() );
71 $this->onSuccess();
72 } else {
73 $form = $this->getForm();
74 if ( $form->show() ) {
75 $this->onSuccess();
76 }
77 }
78 }
79
80 protected function checkCanExecute( User $user ) {
81 // Must be logged in
82 if ( $user->isAnon() ) {
83 throw new UserNotLoggedIn( 'watchlistanontext', 'watchnologin' );
84 }
85
86 parent::checkCanExecute( $user );
87 }
88
89 /**
90 * Watch or unwatch a page
91 * @since 1.22
92 * @param bool $watch Whether to watch or unwatch the page
93 * @param Title $title Page to watch/unwatch
94 * @param User $user User who is watching/unwatching
95 * @return Status
96 */
97 public static function doWatchOrUnwatch( $watch, Title $title, User $user ) {
98 if ( $user->isLoggedIn() &&
99 $user->isWatched( $title, WatchedItem::IGNORE_USER_RIGHTS ) != $watch
100 ) {
101 // If the user doesn't have 'editmywatchlist', we still want to
102 // allow them to add but not remove items via edits and such.
103 if ( $watch ) {
104 return self::doWatch( $title, $user, WatchedItem::IGNORE_USER_RIGHTS );
105 } else {
106 return self::doUnwatch( $title, $user );
107 }
108 }
109
110 return Status::newGood();
111 }
112
113 /**
114 * Watch a page
115 * @since 1.22 Returns Status, $checkRights parameter added
116 * @param Title $title Page to watch/unwatch
117 * @param User $user User who is watching/unwatching
118 * @param int $checkRights Passed through to $user->addWatch()
119 * @return Status
120 */
121 public static function doWatch( Title $title, User $user,
122 $checkRights = WatchedItem::CHECK_USER_RIGHTS
123 ) {
124 if ( $checkRights !== WatchedItem::IGNORE_USER_RIGHTS &&
125 !$user->isAllowed( 'editmywatchlist' )
126 ) {
127 return User::newFatalPermissionDeniedStatus( 'editmywatchlist' );
128 }
129
130 $page = WikiPage::factory( $title );
131
132 $status = Status::newFatal( 'hookaborted' );
133 if ( Hooks::run( 'WatchArticle', array( &$user, &$page, &$status ) ) ) {
134 $status = Status::newGood();
135 $user->addWatch( $title, $checkRights );
136 Hooks::run( 'WatchArticleComplete', array( &$user, &$page ) );
137 }
138
139 return $status;
140 }
141
142 /**
143 * Unwatch a page
144 * @since 1.22 Returns Status
145 * @param Title $title Page to watch/unwatch
146 * @param User $user User who is watching/unwatching
147 * @return Status
148 */
149 public static function doUnwatch( Title $title, User $user ) {
150 if ( !$user->isAllowed( 'editmywatchlist' ) ) {
151 return User::newFatalPermissionDeniedStatus( 'editmywatchlist' );
152 }
153
154 $page = WikiPage::factory( $title );
155
156 $status = Status::newFatal( 'hookaborted' );
157 if ( Hooks::run( 'UnwatchArticle', array( &$user, &$page, &$status ) ) ) {
158 $status = Status::newGood();
159 $user->removeWatch( $title );
160 Hooks::run( 'UnwatchArticleComplete', array( &$user, &$page ) );
161 }
162
163 return $status;
164 }
165
166 /**
167 * Get token to watch (or unwatch) a page for a user
168 *
169 * @param Title $title Title object of page to watch
170 * @param User $user User for whom the action is going to be performed
171 * @param string $action Optionally override the action to 'unwatch'
172 * @return string Token
173 * @since 1.18
174 */
175 public static function getWatchToken( Title $title, User $user, $action = 'watch' ) {
176 if ( $action != 'unwatch' ) {
177 $action = 'watch';
178 }
179 $salt = array( $action, $title->getPrefixedDBkey() );
180
181 // This token stronger salted and not compatible with ApiWatch
182 // It's title/action specific because index.php is GET and API is POST
183 return $user->getEditToken( $salt );
184 }
185
186 /**
187 * Get token to unwatch (or watch) a page for a user
188 *
189 * @param Title $title Title object of page to unwatch
190 * @param User $user User for whom the action is going to be performed
191 * @param string $action Optionally override the action to 'watch'
192 * @return string Token
193 * @since 1.18
194 */
195 public static function getUnwatchToken( Title $title, User $user, $action = 'unwatch' ) {
196 return self::getWatchToken( $title, $user, $action );
197 }
198
199 protected function alterForm( HTMLForm $form ) {
200 $form->setSubmitTextMsg( 'confirm-watch-button' );
201 }
202
203 protected function preText() {
204 return $this->msg( 'confirm-watch-top' )->parse();
205 }
206
207 public function onSuccess() {
208 $this->getOutput()->addWikiMsg( 'addedwatchtext', $this->getTitle()->getPrefixedText() );
209 }
210 }