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() {
42 public function onSubmit( $data ) {
43 self
::doWatch( $this->getTitle(), $this->getUser() );
48 protected function checkCanExecute( User
$user ) {
50 if ( $user->isAnon() ) {
51 throw new UserNotLoggedIn( 'watchlistanontext', 'watchnologin' );
54 parent
::checkCanExecute( $user );
57 protected function usesOOUI() {
61 protected function getFormFields() {
65 'vertical-label' => true,
67 'default' => $this->msg( 'confirm-watch-top' )->parse()
72 protected function alterForm( HTMLForm
$form ) {
73 $form->setWrapperLegendMsg( 'addwatch' );
74 $form->setSubmitTextMsg( 'confirm-watch-button' );
75 $form->setTokenSalt( 'watch' );
78 public function onSuccess() {
79 $msgKey = $this->getTitle()->isTalkPage() ?
'addedwatchtext-talk' : 'addedwatchtext';
80 $this->getOutput()->addWikiMsg( $msgKey, $this->getTitle()->getPrefixedText() );
83 /* Static utility methods */
86 * Watch or unwatch a page
88 * @param bool $watch Whether to watch or unwatch the page
89 * @param Title $title Page to watch/unwatch
90 * @param User $user User who is watching/unwatching
93 public static function doWatchOrUnwatch( $watch, Title
$title, User
$user ) {
94 if ( $user->isLoggedIn() &&
95 $user->isWatched( $title, User
::IGNORE_USER_RIGHTS
) != $watch
97 // If the user doesn't have 'editmywatchlist', we still want to
98 // allow them to add but not remove items via edits and such.
100 return self
::doWatch( $title, $user, User
::IGNORE_USER_RIGHTS
);
102 return self
::doUnwatch( $title, $user );
106 return Status
::newGood();
111 * @since 1.22 Returns Status, $checkRights parameter added
112 * @param Title $title Page to watch/unwatch
113 * @param User $user User who is watching/unwatching
114 * @param bool $checkRights Passed through to $user->addWatch()
115 * Pass User::CHECK_USER_RIGHTS or User::IGNORE_USER_RIGHTS.
118 public static function doWatch(
121 $checkRights = User
::CHECK_USER_RIGHTS
123 if ( $checkRights && !$user->isAllowed( 'editmywatchlist' ) ) {
124 return User
::newFatalPermissionDeniedStatus( 'editmywatchlist' );
127 $page = WikiPage
::factory( $title );
129 $status = Status
::newFatal( 'hookaborted' );
130 if ( Hooks
::run( 'WatchArticle', [ &$user, &$page, &$status ] ) ) {
131 $status = Status
::newGood();
132 $user->addWatch( $title, $checkRights );
133 Hooks
::run( 'WatchArticleComplete', [ &$user, &$page ] );
141 * @since 1.22 Returns Status
142 * @param Title $title Page to watch/unwatch
143 * @param User $user User who is watching/unwatching
146 public static function doUnwatch( Title
$title, User
$user ) {
147 if ( !$user->isAllowed( 'editmywatchlist' ) ) {
148 return User
::newFatalPermissionDeniedStatus( 'editmywatchlist' );
151 $page = WikiPage
::factory( $title );
153 $status = Status
::newFatal( 'hookaborted' );
154 if ( Hooks
::run( 'UnwatchArticle', [ &$user, &$page, &$status ] ) ) {
155 $status = Status
::newGood();
156 $user->removeWatch( $title );
157 Hooks
::run( 'UnwatchArticleComplete', [ &$user, &$page ] );
164 * Get token to watch (or unwatch) a page for a user
166 * @param Title $title Title object of page to watch
167 * @param User $user User for whom the action is going to be performed
168 * @param string $action Optionally override the action to 'unwatch'
169 * @return string Token
172 public static function getWatchToken( Title
$title, User
$user, $action = 'watch' ) {
173 if ( $action != 'unwatch' ) {
176 // Match ApiWatch and ResourceLoaderUserTokensModule
177 return $user->getEditToken( $action );
181 * Get token to unwatch (or watch) a page for a user
183 * @param Title $title Title object of page to unwatch
184 * @param User $user User for whom the action is going to be performed
185 * @param string $action Optionally override the action to 'watch'
186 * @return string Token
189 public static function getUnwatchToken( Title
$title, User
$user, $action = 'unwatch' ) {
190 return self
::getWatchToken( $title, $user, $action );
193 public function doesWrites() {