Merge "DifferenceEngine: Don't display empty header row"
[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 /**
91 * Watch a page
92 * @since 1.22 Returns Status object
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 doWatch( Title $title, User $user ) {
98 $page = WikiPage::factory( $title );
99
100 $status = Status::newFatal( 'hookaborted' );
101 if ( wfRunHooks( 'WatchArticle', array( &$user, &$page, &$status ) ) ) {
102 $status = Status::newGood();
103 $user->addWatch( $title );
104 wfRunHooks( 'WatchArticleComplete', array( &$user, &$page ) );
105 }
106 return $status;
107 }
108
109 /**
110 * Unwatch a page
111 * @since 1.22 Returns Status
112 * @param Title $title Page to watch/unwatch
113 * @param User $user User who is watching/unwatching
114 * @return Status
115 */
116 public static function doUnwatch( Title $title, User $user ) {
117 $page = WikiPage::factory( $title );
118
119 $status = Status::newFatal( 'hookaborted' );
120 if ( wfRunHooks( 'UnwatchArticle', array( &$user, &$page, &$status ) ) ) {
121 $status = Status::newGood();
122 $user->removeWatch( $title );
123 wfRunHooks( 'UnwatchArticleComplete', array( &$user, &$page ) );
124 }
125 return $status;
126 }
127
128 /**
129 * Get token to watch (or unwatch) a page for a user
130 *
131 * @param Title $title Title object of page to watch
132 * @param User $user User for whom the action is going to be performed
133 * @param string $action Optionally override the action to 'unwatch'
134 * @return string Token
135 * @since 1.18
136 */
137 public static function getWatchToken( Title $title, User $user, $action = 'watch' ) {
138 if ( $action != 'unwatch' ) {
139 $action = 'watch';
140 }
141 $salt = array( $action, $title->getDBkey() );
142
143 // This token stronger salted and not compatible with ApiWatch
144 // It's title/action specific because index.php is GET and API is POST
145 return $user->getEditToken( $salt );
146 }
147
148 /**
149 * Get token to unwatch (or watch) a page for a user
150 *
151 * @param Title $title Title object of page to unwatch
152 * @param User $user User for whom the action is going to be performed
153 * @param string $action Optionally override the action to 'watch'
154 * @return string Token
155 * @since 1.18
156 */
157 public static function getUnwatchToken( Title $title, User $user, $action = 'unwatch' ) {
158 return self::getWatchToken( $title, $user, $action );
159 }
160
161 protected function alterForm( HTMLForm $form ) {
162 $form->setSubmitTextMsg( 'confirm-watch-button' );
163 }
164
165 protected function preText() {
166 return $this->msg( 'confirm-watch-top' )->parse();
167 }
168
169 public function onSuccess() {
170 $this->getOutput()->addWikiMsg( 'addedwatchtext', $this->getTitle()->getPrefixedText() );
171 }
172 }
173
174 /**
175 * Page removal from a user's watchlist
176 *
177 * @ingroup Actions
178 */
179 class UnwatchAction extends WatchAction {
180
181 public function getName() {
182 return 'unwatch';
183 }
184
185 protected function getDescription() {
186 return $this->msg( 'removewatch' )->escaped();
187 }
188
189 public function onSubmit( $data ) {
190 wfProfileIn( __METHOD__ );
191 self::doUnwatch( $this->getTitle(), $this->getUser() );
192 wfProfileOut( __METHOD__ );
193 return true;
194 }
195
196 protected function alterForm( HTMLForm $form ) {
197 $form->setSubmitTextMsg( 'confirm-unwatch-button' );
198 }
199
200 protected function preText() {
201 return $this->msg( 'confirm-unwatch-top' )->parse();
202 }
203
204 public function onSuccess() {
205 $this->getOutput()->addWikiMsg( 'removedwatchtext', $this->getTitle()->getPrefixedText() );
206 }
207 }