Merge "Slight improvements to FormSpecialPage behavior."
[lhc/web/wiklou.git] / includes / actions / WatchAction.php
1 <?php
2 /**
3 * Performs the watch and unwatch 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 wfProfileIn( __METHOD__ );
52 self::doWatch( $this->getTitle(), $this->getUser() );
53 wfProfileOut( __METHOD__ );
54 return true;
55 }
56
57 /**
58 * This can be either formed or formless depending on the session token given
59 */
60 public function show() {
61 $this->setHeaders();
62
63 $user = $this->getUser();
64 // This will throw exceptions if there's a problem
65 $this->checkCanExecute( $user );
66
67 // Must have valid token for this action/title
68 $salt = array( $this->getName(), $this->getTitle()->getDBkey() );
69
70 if ( $user->matchEditToken( $this->getRequest()->getVal( 'token' ), $salt ) ) {
71 $this->onSubmit( array() );
72 $this->onSuccess();
73 } else {
74 $form = $this->getForm();
75 if ( $form->show() ) {
76 $this->onSuccess();
77 }
78 }
79 }
80
81 protected function checkCanExecute( User $user ) {
82 // Must be logged in
83 if ( $user->isAnon() ) {
84 throw new ErrorPageError( 'watchnologin', 'watchnologintext' );
85 }
86
87 return parent::checkCanExecute( $user );
88 }
89
90 public static function doWatch( Title $title, User $user ) {
91 $page = WikiPage::factory( $title );
92
93 if ( wfRunHooks( 'WatchArticle', array( &$user, &$page ) ) ) {
94 $user->addWatch( $title );
95 wfRunHooks( 'WatchArticleComplete', array( &$user, &$page ) );
96 }
97 return true;
98 }
99
100 public static function doUnwatch( Title $title, User $user ) {
101 $page = WikiPage::factory( $title );
102
103 if ( wfRunHooks( 'UnwatchArticle', array( &$user, &$page ) ) ) {
104 $user->removeWatch( $title );
105 wfRunHooks( 'UnwatchArticleComplete', array( &$user, &$page ) );
106 }
107 return true;
108 }
109
110 /**
111 * Get token to watch (or unwatch) a page for a user
112 *
113 * @param Title $title Title object of page to watch
114 * @param User $user User for whom the action is going to be performed
115 * @param string $action Optionally override the action to 'unwatch'
116 * @return string Token
117 * @since 1.18
118 */
119 public static function getWatchToken( Title $title, User $user, $action = 'watch' ) {
120 if ( $action != 'unwatch' ) {
121 $action = 'watch';
122 }
123 $salt = array( $action, $title->getDBkey() );
124
125 // This token stronger salted and not compatible with ApiWatch
126 // It's title/action specific because index.php is GET and API is POST
127 return $user->getEditToken( $salt );
128 }
129
130 /**
131 * Get token to unwatch (or watch) a page for a user
132 *
133 * @param Title $title Title object of page to unwatch
134 * @param User $user User for whom the action is going to be performed
135 * @param string $action Optionally override the action to 'watch'
136 * @return string Token
137 * @since 1.18
138 */
139 public static function getUnwatchToken( Title $title, User $user, $action = 'unwatch' ) {
140 return self::getWatchToken( $title, $user, $action );
141 }
142
143 protected function alterForm( HTMLForm $form ) {
144 $form->setSubmitTextMsg( 'confirm-watch-button' );
145 }
146
147 protected function preText() {
148 return $this->msg( 'confirm-watch-top' )->parse();
149 }
150
151 public function onSuccess() {
152 $this->getOutput()->addWikiMsg( 'addedwatchtext', $this->getTitle()->getPrefixedText() );
153 }
154 }
155
156 /**
157 * Page removal from a user's watchlist
158 *
159 * @ingroup Actions
160 */
161 class UnwatchAction extends WatchAction {
162
163 public function getName() {
164 return 'unwatch';
165 }
166
167 protected function getDescription() {
168 return $this->msg( 'removewatch' )->escaped();
169 }
170
171 public function onSubmit( $data ) {
172 wfProfileIn( __METHOD__ );
173 self::doUnwatch( $this->getTitle(), $this->getUser() );
174 wfProfileOut( __METHOD__ );
175 return true;
176 }
177
178 protected function alterForm( HTMLForm $form ) {
179 $form->setSubmitTextMsg( 'confirm-unwatch-button' );
180 }
181
182 protected function preText() {
183 return $this->msg( 'confirm-unwatch-top' )->parse();
184 }
185
186 public function onSuccess() {
187 $this->getOutput()->addWikiMsg( 'removedwatchtext', $this->getTitle()->getPrefixedText() );
188 }
189 }