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
23 use MediaWiki\MediaWikiServices
;
26 * Page addition to a user's watchlist
30 class WatchAction
extends FormAction
{
32 public function getName() {
36 public function requiresUnblock() {
40 protected function getDescription() {
44 public function onSubmit( $data ) {
45 return 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() );
84 * Watch or unwatch a page
86 * @param bool $watch Whether to watch or unwatch the page
87 * @param Title $title Page to watch/unwatch
88 * @param User $user User who is watching/unwatching
91 public static function doWatchOrUnwatch( $watch, Title
$title, User
$user ) {
92 if ( $user->isLoggedIn() &&
93 $user->isWatched( $title, User
::IGNORE_USER_RIGHTS
) != $watch
95 // If the user doesn't have 'editmywatchlist', we still want to
96 // allow them to add but not remove items via edits and such.
98 return self
::doWatch( $title, $user, User
::IGNORE_USER_RIGHTS
);
100 return self
::doUnwatch( $title, $user );
104 return Status
::newGood();
109 * @since 1.22 Returns Status, $checkRights parameter added
110 * @param Title $title Page to watch/unwatch
111 * @param User $user User who is watching/unwatching
112 * @param bool $checkRights Passed through to $user->addWatch()
113 * Pass User::CHECK_USER_RIGHTS or User::IGNORE_USER_RIGHTS.
116 public static function doWatch(
119 $checkRights = User
::CHECK_USER_RIGHTS
121 $permissionManager = MediaWikiServices
::getInstance()->getPermissionManager();
122 if ( $checkRights && !$permissionManager->userHasRight( $user, 'editmywatchlist' ) ) {
123 return User
::newFatalPermissionDeniedStatus( 'editmywatchlist' );
126 $page = WikiPage
::factory( $title );
128 $status = Status
::newFatal( 'hookaborted' );
129 if ( Hooks
::run( 'WatchArticle', [ &$user, &$page, &$status ] ) ) {
130 $status = Status
::newGood();
131 $user->addWatch( $title, $checkRights );
132 Hooks
::run( 'WatchArticleComplete', [ &$user, &$page ] );
140 * @since 1.22 Returns Status
141 * @param Title $title Page to watch/unwatch
142 * @param User $user User who is watching/unwatching
145 public static function doUnwatch( Title
$title, User
$user ) {
146 if ( !MediaWikiServices
::getInstance()
147 ->getPermissionManager()
148 ->userHasRight( $user, 'editmywatchlist' ) ) {
149 return User
::newFatalPermissionDeniedStatus( 'editmywatchlist' );
152 $page = WikiPage
::factory( $title );
154 $status = Status
::newFatal( 'hookaborted' );
155 if ( Hooks
::run( 'UnwatchArticle', [ &$user, &$page, &$status ] ) ) {
156 $status = Status
::newGood();
157 $user->removeWatch( $title );
158 Hooks
::run( 'UnwatchArticleComplete', [ &$user, &$page ] );
165 * Get token to watch (or unwatch) a page for a user
167 * @param Title $title Title object of page to watch
168 * @param User $user User for whom the action is going to be performed
169 * @param string $action Optionally override the action to 'unwatch'
170 * @return string Token
173 public static function getWatchToken( Title
$title, User
$user, $action = 'watch' ) {
174 if ( $action != 'unwatch' ) {
177 // Match ApiWatch and ResourceLoaderUserTokensModule
178 return $user->getEditToken( $action );
181 public function doesWrites() {