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
24 * Page addition to a user's watchlist
28 class WatchAction
extends FormAction
{
30 public function getName() {
34 public function requiresUnblock() {
38 protected function getDescription() {
39 return $this->msg( 'addwatch' )->escaped();
43 * Just get an empty form with a single submit button
46 protected function getFormFields() {
50 public function onSubmit( $data ) {
51 wfProfileIn( __METHOD__
);
52 self
::doWatch( $this->getTitle(), $this->getUser() );
53 wfProfileOut( __METHOD__
);
58 * This can be either formed or formless depending on the session token given
60 public function show() {
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() );
74 $form = $this->getForm();
75 if ( $form->show() ) {
81 protected function checkCanExecute( User
$user ) {
83 if ( $user->isAnon() ) {
84 throw new ErrorPageError( 'watchnologin', 'watchnologintext' );
87 return parent
::checkCanExecute( $user );
90 public static function doWatch( Title
$title, User
$user ) {
91 $page = WikiPage
::factory( $title );
93 if ( wfRunHooks( 'WatchArticle', array( &$user, &$page ) ) ) {
94 $user->addWatch( $title );
95 wfRunHooks( 'WatchArticleComplete', array( &$user, &$page ) );
100 public static function doUnwatch( Title
$title, User
$user ) {
101 $page = WikiPage
::factory( $title );
103 if ( wfRunHooks( 'UnwatchArticle', array( &$user, &$page ) ) ) {
104 $user->removeWatch( $title );
105 wfRunHooks( 'UnwatchArticleComplete', array( &$user, &$page ) );
111 * Get token to watch (or unwatch) a page for a user
113 * @param Title $title Title object of page to watch
114 * @param User $user User for whom the action is going to be performed
115 * @param string $action Optionally override the action to 'unwatch'
116 * @return string Token
119 public static function getWatchToken( Title
$title, User
$user, $action = 'watch' ) {
120 if ( $action != 'unwatch' ) {
123 $salt = array( $action, $title->getDBkey() );
125 // This token stronger salted and not compatible with ApiWatch
126 // It's title/action specific because index.php is GET and API is POST
127 return $user->getEditToken( $salt );
131 * Get token to unwatch (or watch) a page for a user
133 * @param Title $title Title object of page to unwatch
134 * @param User $user User for whom the action is going to be performed
135 * @param string $action Optionally override the action to 'watch'
136 * @return string Token
139 public static function getUnwatchToken( Title
$title, User
$user, $action = 'unwatch' ) {
140 return self
::getWatchToken( $title, $user, $action );
143 protected function alterForm( HTMLForm
$form ) {
144 $form->setSubmitTextMsg( 'confirm-watch-button' );
147 protected function preText() {
148 return $this->msg( 'confirm-watch-top' )->parse();
151 public function onSuccess() {
152 $this->getOutput()->addWikiMsg( 'addedwatchtext', $this->getTitle()->getPrefixedText() );
157 * Page removal from a user's watchlist
161 class UnwatchAction
extends WatchAction
{
163 public function getName() {
167 protected function getDescription() {
168 return $this->msg( 'removewatch' )->escaped();
171 public function onSubmit( $data ) {
172 wfProfileIn( __METHOD__
);
173 self
::doUnwatch( $this->getTitle(), $this->getUser() );
174 wfProfileOut( __METHOD__
);
178 protected function alterForm( HTMLForm
$form ) {
179 $form->setSubmitTextMsg( 'confirm-unwatch-button' );
182 protected function preText() {
183 return $this->msg( 'confirm-unwatch-top' )->parse();
186 public function onSuccess() {
187 $this->getOutput()->addWikiMsg( 'removedwatchtext', $this->getTitle()->getPrefixedText() );