SpecialLinkSearch: clean up munged query variable handling
[mediawiki.git] / includes / actions / WatchAction.php
blob96473409da1ca758e7c5aeabb634933d00bb9790
1 <?php
2 /**
3 * Performs the watch 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 self::doWatch( $this->getTitle(), $this->getUser() );
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 UserNotLoggedIn( 'watchlistanontext', 'watchnologin' );
86 parent::checkCanExecute( $user );
89 /**
90 * Watch or unwatch a page
91 * @since 1.22
92 * @param bool $watch Whether to watch or unwatch the page
93 * @param Title $title Page to watch/unwatch
94 * @param User $user User who is watching/unwatching
95 * @return Status
97 public static function doWatchOrUnwatch( $watch, Title $title, User $user ) {
98 if ( $user->isLoggedIn() &&
99 $user->isWatched( $title, WatchedItem::IGNORE_USER_RIGHTS ) != $watch
101 // If the user doesn't have 'editmywatchlist', we still want to
102 // allow them to add but not remove items via edits and such.
103 if ( $watch ) {
104 return self::doWatch( $title, $user, WatchedItem::IGNORE_USER_RIGHTS );
105 } else {
106 return self::doUnwatch( $title, $user );
110 return Status::newGood();
114 * Watch a page
115 * @since 1.22 Returns Status, $checkRights parameter added
116 * @param Title $title Page to watch/unwatch
117 * @param User $user User who is watching/unwatching
118 * @param int $checkRights Passed through to $user->addWatch()
119 * @return Status
121 public static function doWatch( Title $title, User $user,
122 $checkRights = WatchedItem::CHECK_USER_RIGHTS
124 if ( $checkRights !== WatchedItem::IGNORE_USER_RIGHTS &&
125 !$user->isAllowed( 'editmywatchlist' )
127 return User::newFatalPermissionDeniedStatus( 'editmywatchlist' );
130 $page = WikiPage::factory( $title );
132 $status = Status::newFatal( 'hookaborted' );
133 if ( Hooks::run( 'WatchArticle', array( &$user, &$page, &$status ) ) ) {
134 $status = Status::newGood();
135 $user->addWatch( $title, $checkRights );
136 Hooks::run( 'WatchArticleComplete', array( &$user, &$page ) );
139 return $status;
143 * Unwatch a page
144 * @since 1.22 Returns Status
145 * @param Title $title Page to watch/unwatch
146 * @param User $user User who is watching/unwatching
147 * @return Status
149 public static function doUnwatch( Title $title, User $user ) {
150 if ( !$user->isAllowed( 'editmywatchlist' ) ) {
151 return User::newFatalPermissionDeniedStatus( 'editmywatchlist' );
154 $page = WikiPage::factory( $title );
156 $status = Status::newFatal( 'hookaborted' );
157 if ( Hooks::run( 'UnwatchArticle', array( &$user, &$page, &$status ) ) ) {
158 $status = Status::newGood();
159 $user->removeWatch( $title );
160 Hooks::run( 'UnwatchArticleComplete', array( &$user, &$page ) );
163 return $status;
167 * Get token to watch (or unwatch) a page for a user
169 * @param Title $title Title object of page to watch
170 * @param User $user User for whom the action is going to be performed
171 * @param string $action Optionally override the action to 'unwatch'
172 * @return string Token
173 * @since 1.18
175 public static function getWatchToken( Title $title, User $user, $action = 'watch' ) {
176 if ( $action != 'unwatch' ) {
177 $action = 'watch';
179 $salt = array( $action, $title->getPrefixedDBkey() );
181 // This token stronger salted and not compatible with ApiWatch
182 // It's title/action specific because index.php is GET and API is POST
183 return $user->getEditToken( $salt );
187 * Get token to unwatch (or watch) a page for a user
189 * @param Title $title Title object of page to unwatch
190 * @param User $user User for whom the action is going to be performed
191 * @param string $action Optionally override the action to 'watch'
192 * @return string Token
193 * @since 1.18
195 public static function getUnwatchToken( Title $title, User $user, $action = 'unwatch' ) {
196 return self::getWatchToken( $title, $user, $action );
199 protected function alterForm( HTMLForm $form ) {
200 $form->setSubmitTextMsg( 'confirm-watch-button' );
203 protected function preText() {
204 return $this->msg( 'confirm-watch-top' )->parse();
207 public function onSuccess() {
208 $this->getOutput()->addWikiMsg( 'addedwatchtext', $this->getTitle()->getPrefixedText() );