52e6675430046a071fea56e398e0590b36369d49
[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 class WatchAction extends FormAction {
24
25 public function getName() {
26 return 'watch';
27 }
28
29 public function getRestriction() {
30 return 'read';
31 }
32
33 public function requiresUnblock() {
34 return false;
35 }
36
37 protected function getDescription() {
38 return wfMsg( 'addwatch' );
39 }
40
41 /**
42 * Just get an empty form with a single submit button
43 * @return array
44 */
45 protected function getFormFields() {
46 return array();
47 }
48
49 public function onSubmit( $data ) {
50 wfProfileIn( __METHOD__ );
51 self::doWatch( $this->getTitle(), $this->getUser() );
52 wfProfileOut( __METHOD__ );
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()->getDBkey() );
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 ErrorPageError( 'watchnologin', 'watchnologintext' );
84 }
85
86 return parent::checkCanExecute( $user );
87 }
88
89 public static function doWatch( Title $title, User $user ) {
90 $page = new Article( $title, 0 );
91
92 if ( wfRunHooks( 'WatchArticle', array( &$user, &$page ) ) ) {
93 $user->addWatch( $title );
94 wfRunHooks( 'WatchArticleComplete', array( &$user, &$page ) );
95 }
96 return true;
97 }
98
99 public static function doUnwatch( Title $title, User $user ) {
100 $page = new Article( $title, 0 );
101
102 if ( wfRunHooks( 'UnwatchArticle', array( &$user, &$page ) ) ) {
103 $user->removeWatch( $title );
104 wfRunHooks( 'UnwatchArticleComplete', array( &$user, &$page ) );
105 }
106 return true;
107 }
108
109 /**
110 * Get token to watch (or unwatch) a page for a user
111 *
112 * @param Title $title Title object of page to watch
113 * @param User $title User for whom the action is going to be performed
114 * @param string $action Optionally override the action to 'unwatch'
115 * @return string Token
116 * @since 1.18
117 */
118 public static function getWatchToken( Title $title, User $user, $action = 'watch' ) {
119 if ( $action != 'unwatch' ) {
120 $action = 'watch';
121 }
122 $salt = array( $action, $title->getDBkey() );
123
124 // This token stronger salted and not compatible with ApiWatch
125 // It's title/action specific because index.php is GET and API is POST
126 return $user->editToken( $salt );
127 }
128
129 /**
130 * Get token to unwatch (or watch) a page for a user
131 *
132 * @param Title $title Title object of page to unwatch
133 * @param User $title User for whom the action is going to be performed
134 * @param string $action Optionally override the action to 'watch'
135 * @return string Token
136 * @since 1.18
137 */
138 public static function getUnwatchToken( Title $title, User $user, $action = 'unwatch' ) {
139 return self::getWatchToken( $title, $user, $action );
140 }
141
142 protected function alterForm( HTMLForm $form ) {
143 $form->setSubmitText( wfMsg( 'confirm-watch-button' ) );
144 }
145
146 protected function preText() {
147 return wfMessage( 'confirm-watch-top' )->parse();
148 }
149
150 public function onSuccess() {
151 $this->getOutput()->addWikiMsg( 'addedwatchtext', $this->getTitle()->getPrefixedText() );
152 }
153 }
154
155 class UnwatchAction extends WatchAction {
156
157 public function getName() {
158 return 'unwatch';
159 }
160
161 protected function getDescription() {
162 return wfMsg( 'removewatch' );
163 }
164
165 public function onSubmit( $data ) {
166 wfProfileIn( __METHOD__ );
167 self::doUnwatch( $this->getTitle(), $this->getUser() );
168 wfProfileOut( __METHOD__ );
169 return true;
170 }
171
172 protected function alterForm( HTMLForm $form ) {
173 $form->setSubmitText( wfMsg( 'confirm-unwatch-button' ) );
174 }
175
176 protected function preText() {
177 return wfMessage( 'confirm-unwatch-top' )->parse();
178 }
179
180 public function onSuccess() {
181 $this->getOutput()->addWikiMsg( 'removedwatchtext', $this->getTitle()->getPrefixedText() );
182 }
183 }