3 * Performs the watch and unwatch actions on a page
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.
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.
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
23 class WatchAction
extends FormAction
{
25 public function getName() {
29 public function requiresUnblock() {
33 protected function getDescription() {
34 return $this->msg( 'addwatch' )->escaped();
38 * Just get an empty form with a single submit button
41 protected function getFormFields() {
45 public function onSubmit( $data ) {
46 wfProfileIn( __METHOD__
);
47 self
::doWatch( $this->getTitle(), $this->getUser() );
48 wfProfileOut( __METHOD__
);
53 * This can be either formed or formless depending on the session token given
55 public function show() {
58 $user = $this->getUser();
59 // This will throw exceptions if there's a problem
60 $this->checkCanExecute( $user );
62 // Must have valid token for this action/title
63 $salt = array( $this->getName(), $this->getTitle()->getDBkey() );
65 if ( $user->matchEditToken( $this->getRequest()->getVal( 'token' ), $salt ) ) {
66 $this->onSubmit( array() );
69 $form = $this->getForm();
70 if ( $form->show() ) {
76 protected function checkCanExecute( User
$user ) {
78 if ( $user->isAnon() ) {
79 throw new ErrorPageError( 'watchnologin', 'watchnologintext' );
82 return parent
::checkCanExecute( $user );
85 public static function doWatch( Title
$title, User
$user ) {
86 $page = WikiPage
::factory( $title );
88 if ( wfRunHooks( 'WatchArticle', array( &$user, &$page ) ) ) {
89 $user->addWatch( $title );
90 wfRunHooks( 'WatchArticleComplete', array( &$user, &$page ) );
95 public static function doUnwatch( Title
$title, User
$user ) {
96 $page = WikiPage
::factory( $title );
98 if ( wfRunHooks( 'UnwatchArticle', array( &$user, &$page ) ) ) {
99 $user->removeWatch( $title );
100 wfRunHooks( 'UnwatchArticleComplete', array( &$user, &$page ) );
106 * Get token to watch (or unwatch) a page for a user
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
114 public static function getWatchToken( Title
$title, User
$user, $action = 'watch' ) {
115 if ( $action != 'unwatch' ) {
118 $salt = array( $action, $title->getDBkey() );
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 );
126 * Get token to unwatch (or watch) a page for a user
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
134 public static function getUnwatchToken( Title
$title, User
$user, $action = 'unwatch' ) {
135 return self
::getWatchToken( $title, $user, $action );
138 protected function alterForm( HTMLForm
$form ) {
139 $form->setSubmitTextMsg( 'confirm-watch-button' );
142 protected function preText() {
143 return $this->msg( 'confirm-watch-top' )->parse();
146 public function onSuccess() {
147 $this->getOutput()->addWikiMsg( 'addedwatchtext', $this->getTitle()->getPrefixedText() );
151 class UnwatchAction
extends WatchAction
{
153 public function getName() {
157 protected function getDescription() {
158 return $this->msg( 'removewatch' )->escaped();
161 public function onSubmit( $data ) {
162 wfProfileIn( __METHOD__
);
163 self
::doUnwatch( $this->getTitle(), $this->getUser() );
164 wfProfileOut( __METHOD__
);
168 protected function alterForm( HTMLForm
$form ) {
169 $form->setSubmitTextMsg( 'confirm-unwatch-button' );
172 protected function preText() {
173 return $this->msg( 'confirm-unwatch-top' )->parse();
176 public function onSuccess() {
177 $this->getOutput()->addWikiMsg( 'removedwatchtext', $this->getTitle()->getPrefixedText() );