Fix doxygen docs before REL1_19 branching
[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 requiresUnblock() {
30 return false;
31 }
32
33 protected function getDescription() {
34 return wfMsgHtml( 'addwatch' );
35 }
36
37 /**
38 * Just get an empty form with a single submit button
39 * @return array
40 */
41 protected function getFormFields() {
42 return array();
43 }
44
45 public function onSubmit( $data ) {
46 wfProfileIn( __METHOD__ );
47 self::doWatch( $this->getTitle(), $this->getUser() );
48 wfProfileOut( __METHOD__ );
49 return true;
50 }
51
52 /**
53 * This can be either formed or formless depending on the session token given
54 */
55 public function show() {
56 $this->setHeaders();
57
58 $user = $this->getUser();
59 // This will throw exceptions if there's a problem
60 $this->checkCanExecute( $user );
61
62 // Must have valid token for this action/title
63 $salt = array( $this->getName(), $this->getTitle()->getDBkey() );
64
65 if ( $user->matchEditToken( $this->getRequest()->getVal( 'token' ), $salt ) ) {
66 $this->onSubmit( array() );
67 $this->onSuccess();
68 } else {
69 $form = $this->getForm();
70 if ( $form->show() ) {
71 $this->onSuccess();
72 }
73 }
74 }
75
76 protected function checkCanExecute( User $user ) {
77 // Must be logged in
78 if ( $user->isAnon() ) {
79 throw new ErrorPageError( 'watchnologin', 'watchnologintext' );
80 }
81
82 return parent::checkCanExecute( $user );
83 }
84
85 public static function doWatch( Title $title, User $user ) {
86 $page = WikiPage::factory( $title );
87
88 if ( wfRunHooks( 'WatchArticle', array( &$user, &$page ) ) ) {
89 $user->addWatch( $title );
90 wfRunHooks( 'WatchArticleComplete', array( &$user, &$page ) );
91 }
92 return true;
93 }
94
95 public static function doUnwatch( Title $title, User $user ) {
96 $page = WikiPage::factory( $title );
97
98 if ( wfRunHooks( 'UnwatchArticle', array( &$user, &$page ) ) ) {
99 $user->removeWatch( $title );
100 wfRunHooks( 'UnwatchArticleComplete', array( &$user, &$page ) );
101 }
102 return true;
103 }
104
105 /**
106 * Get token to watch (or unwatch) a page for a user
107 *
108 * @param Title $title Title object of page to watch
109 * @param User $user User for whom the action is going to be performed
110 * @param string $action Optionally override the action to 'unwatch'
111 * @return string Token
112 * @since 1.18
113 */
114 public static function getWatchToken( Title $title, User $user, $action = 'watch' ) {
115 if ( $action != 'unwatch' ) {
116 $action = 'watch';
117 }
118 $salt = array( $action, $title->getDBkey() );
119
120 // This token stronger salted and not compatible with ApiWatch
121 // It's title/action specific because index.php is GET and API is POST
122 return $user->getEditToken( $salt );
123 }
124
125 /**
126 * Get token to unwatch (or watch) a page for a user
127 *
128 * @param Title $title Title object of page to unwatch
129 * @param User $user User for whom the action is going to be performed
130 * @param string $action Optionally override the action to 'watch'
131 * @return string Token
132 * @since 1.18
133 */
134 public static function getUnwatchToken( Title $title, User $user, $action = 'unwatch' ) {
135 return self::getWatchToken( $title, $user, $action );
136 }
137
138 protected function alterForm( HTMLForm $form ) {
139 $form->setSubmitText( wfMsg( 'confirm-watch-button' ) );
140 }
141
142 protected function preText() {
143 return wfMessage( 'confirm-watch-top' )->parse();
144 }
145
146 public function onSuccess() {
147 $this->getOutput()->addWikiMsg( 'addedwatchtext', $this->getTitle()->getPrefixedText() );
148 }
149 }
150
151 class UnwatchAction extends WatchAction {
152
153 public function getName() {
154 return 'unwatch';
155 }
156
157 protected function getDescription() {
158 return wfMsg( 'removewatch' );
159 }
160
161 public function onSubmit( $data ) {
162 wfProfileIn( __METHOD__ );
163 self::doUnwatch( $this->getTitle(), $this->getUser() );
164 wfProfileOut( __METHOD__ );
165 return true;
166 }
167
168 protected function alterForm( HTMLForm $form ) {
169 $form->setSubmitText( wfMsg( 'confirm-unwatch-button' ) );
170 }
171
172 protected function preText() {
173 return wfMessage( 'confirm-unwatch-top' )->parse();
174 }
175
176 public function onSuccess() {
177 $this->getOutput()->addWikiMsg( 'removedwatchtext', $this->getTitle()->getPrefixedText() );
178 }
179 }