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
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 self
::doWatch( $this->getTitle(), $this->getUser() );
57 * This can be either formed or formless depending on the session token given
59 public function show() {
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() );
73 $form = $this->getForm();
74 if ( $form->show() ) {
80 protected function checkCanExecute( User
$user ) {
82 if ( $user->isAnon() ) {
83 throw new UserNotLoggedIn( 'watchlistanontext', 'watchnologin' );
86 parent
::checkCanExecute( $user );
90 * Watch or unwatch a page
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
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.
104 return self
::doWatch( $title, $user, WatchedItem
::IGNORE_USER_RIGHTS
);
106 return self
::doUnwatch( $title, $user );
110 return Status
::newGood();
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()
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 ) );
144 * @since 1.22 Returns Status
145 * @param Title $title Page to watch/unwatch
146 * @param User $user User who is watching/unwatching
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 ) );
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
175 public static function getWatchToken( Title
$title, User
$user, $action = 'watch' ) {
176 if ( $action != 'unwatch' ) {
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
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() );