(bug 30269) Strings like foobar//barfoo are linked to become foobar[//barfoo]
[mediawiki.git] / includes / actions / WatchAction.php
blobdc2d5d309510ee57ac7b14b9b89337a60d07e524
1 <?php
2 /**
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
19 * @file
20 * @ingroup Actions
23 class WatchAction extends FormAction {
25 public function getName() {
26 return 'watch';
29 public function getRestriction() {
30 return 'read';
33 public function requiresUnblock() {
34 return false;
37 protected function getDescription() {
38 return wfMsg( 'addwatch' );
41 /**
42 * Just get an empty form with a single submit button
43 * @return array
45 protected function getFormFields() {
46 return array();
49 public function onSubmit( $data ) {
50 wfProfileIn( __METHOD__ );
51 self::doWatch( $this->getTitle(), $this->getUser() );
52 wfProfileOut( __METHOD__ );
53 return true;
56 /**
57 * This can be either formed or formless depending on the session token given
59 public function show() {
60 $this->setHeaders();
62 $user = $this->getUser();
63 // This will throw exceptions if there's a problem
64 $this->checkCanExecute( $user );
66 // Must have valid token for this action/title
67 $salt = array( $this->getName(), $this->getTitle()->getDBkey() );
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();
80 protected function checkCanExecute( User $user ) {
81 // Must be logged in
82 if ( $user->isAnon() ) {
83 throw new ErrorPageError( 'watchnologin', 'watchnologintext' );
86 return parent::checkCanExecute( $user );
89 public static function doWatch( Title $title, User $user ) {
90 $page = new Article( $title );
92 if ( wfRunHooks( 'WatchArticle', array( &$user, &$page ) ) ) {
93 $user->addWatch( $title );
94 wfRunHooks( 'WatchArticleComplete', array( &$user, &$page ) );
96 return true;
99 public static function doUnwatch( Title $title, User $user ) {
100 $page = new Article( $title );
102 if ( wfRunHooks( 'UnwatchArticle', array( &$user, &$page ) ) ) {
103 $user->removeWatch( $title );
104 wfRunHooks( 'UnwatchArticleComplete', array( &$user, &$page ) );
106 return true;
110 * Get token to watch (or unwatch) a page for a user
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
118 public static function getWatchToken( Title $title, User $user, $action = 'watch' ) {
119 if ( $action != 'unwatch' ) {
120 $action = 'watch';
122 $salt = array( $action, $title->getDBkey() );
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 );
130 * Get token to unwatch (or watch) a page for a user
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
138 public static function getUnwatchToken( Title $title, User $user, $action = 'unwatch' ) {
139 return self::getWatchToken( $title, $user, $action );
142 protected function alterForm( HTMLForm $form ) {
143 $form->setSubmitText( wfMsg( 'confirm-watch-button' ) );
146 protected function preText() {
147 return wfMessage( 'confirm-watch-top' )->parse();
150 public function onSuccess() {
151 $this->getOutput()->addWikiMsg( 'addedwatchtext', $this->getTitle()->getPrefixedText() );
155 class UnwatchAction extends WatchAction {
157 public function getName() {
158 return 'unwatch';
161 protected function getDescription() {
162 return wfMsg( 'removewatch' );
165 public function onSubmit( $data ) {
166 wfProfileIn( __METHOD__ );
167 self::doUnwatch( $this->getTitle(), $this->getUser() );
168 wfProfileOut( __METHOD__ );
169 return true;
172 protected function alterForm( HTMLForm $form ) {
173 $form->setSubmitText( wfMsg( 'confirm-unwatch-button' ) );
176 protected function preText() {
177 return wfMessage( 'confirm-unwatch-top' )->parse();
180 public function onSuccess() {
181 $this->getOutput()->addWikiMsg( 'removedwatchtext', $this->getTitle()->getPrefixedText() );