Localisation updates from http://translatewiki.net.
[mediawiki.git] / includes / actions / WatchAction.php
blobb1c48119b6a7f2370337e241819f7f6dca5ba1d9
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 /**
24 * Page addition to a user's watchlist
26 * @ingroup Actions
28 class WatchAction extends FormAction {
30 public function getName() {
31 return 'watch';
34 public function requiresUnblock() {
35 return false;
38 protected function getDescription() {
39 return $this->msg( 'addwatch' )->escaped();
42 /**
43 * Just get an empty form with a single submit button
44 * @return array
46 protected function getFormFields() {
47 return array();
50 public function onSubmit( $data ) {
51 wfProfileIn( __METHOD__ );
52 self::doWatch( $this->getTitle(), $this->getUser() );
53 wfProfileOut( __METHOD__ );
54 return true;
57 /**
58 * This can be either formed or formless depending on the session token given
60 public function show() {
61 $this->setHeaders();
63 $user = $this->getUser();
64 // This will throw exceptions if there's a problem
65 $this->checkCanExecute( $user );
67 // Must have valid token for this action/title
68 $salt = array( $this->getName(), $this->getTitle()->getDBkey() );
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();
81 protected function checkCanExecute( User $user ) {
82 // Must be logged in
83 if ( $user->isAnon() ) {
84 throw new ErrorPageError( 'watchnologin', 'watchnologintext' );
87 return parent::checkCanExecute( $user );
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
97 public static function doWatch( Title $title, User $user ) {
98 $page = WikiPage::factory( $title );
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 ) );
106 return $status;
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
116 public static function doUnwatch( Title $title, User $user ) {
117 $page = WikiPage::factory( $title );
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 ) );
125 return $status;
129 * Get token to watch (or unwatch) a page for a user
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
137 public static function getWatchToken( Title $title, User $user, $action = 'watch' ) {
138 if ( $action != 'unwatch' ) {
139 $action = 'watch';
141 $salt = array( $action, $title->getDBkey() );
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 );
149 * Get token to unwatch (or watch) a page for a user
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
157 public static function getUnwatchToken( Title $title, User $user, $action = 'unwatch' ) {
158 return self::getWatchToken( $title, $user, $action );
161 protected function alterForm( HTMLForm $form ) {
162 $form->setSubmitTextMsg( 'confirm-watch-button' );
165 protected function preText() {
166 return $this->msg( 'confirm-watch-top' )->parse();
169 public function onSuccess() {
170 $this->getOutput()->addWikiMsg( 'addedwatchtext', $this->getTitle()->getPrefixedText() );
175 * Page removal from a user's watchlist
177 * @ingroup Actions
179 class UnwatchAction extends WatchAction {
181 public function getName() {
182 return 'unwatch';
185 protected function getDescription() {
186 return $this->msg( 'removewatch' )->escaped();
189 public function onSubmit( $data ) {
190 wfProfileIn( __METHOD__ );
191 self::doUnwatch( $this->getTitle(), $this->getUser() );
192 wfProfileOut( __METHOD__ );
193 return true;
196 protected function alterForm( HTMLForm $form ) {
197 $form->setSubmitTextMsg( 'confirm-unwatch-button' );
200 protected function preText() {
201 return $this->msg( 'confirm-unwatch-top' )->parse();
204 public function onSuccess() {
205 $this->getOutput()->addWikiMsg( 'removedwatchtext', $this->getTitle()->getPrefixedText() );