aaccc0c489e95170bb429846f9e567ba1edd64ff
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 return self
::doWatch( $this->getTitle(), $this->getUser() );
46 protected function checkCanExecute( User
$user ) {
48 if ( $user->isAnon() ) {
49 throw new UserNotLoggedIn( 'watchlistanontext', 'watchnologin' );
52 parent
::checkCanExecute( $user );
55 protected function usesOOUI() {
59 protected function getFormFields() {
63 'vertical-label' => true,
65 'default' => $this->msg( 'confirm-watch-top' )->parse()
70 protected function alterForm( HTMLForm
$form ) {
71 $form->setWrapperLegendMsg( 'addwatch' );
72 $form->setSubmitTextMsg( 'confirm-watch-button' );
73 $form->setTokenSalt( 'watch' );
76 public function onSuccess() {
77 $msgKey = $this->getTitle()->isTalkPage() ?
'addedwatchtext-talk' : 'addedwatchtext';
78 $this->getOutput()->addWikiMsg( $msgKey, $this->getTitle()->getPrefixedText() );
82 * Watch or unwatch a page
84 * @param bool $watch Whether to watch or unwatch the page
85 * @param Title $title Page to watch/unwatch
86 * @param User $user User who is watching/unwatching
89 public static function doWatchOrUnwatch( $watch, Title
$title, User
$user ) {
90 if ( $user->isLoggedIn() &&
91 $user->isWatched( $title, User
::IGNORE_USER_RIGHTS
) != $watch
93 // If the user doesn't have 'editmywatchlist', we still want to
94 // allow them to add but not remove items via edits and such.
96 return self
::doWatch( $title, $user, User
::IGNORE_USER_RIGHTS
);
98 return self
::doUnwatch( $title, $user );
102 return Status
::newGood();
107 * @since 1.22 Returns Status, $checkRights parameter added
108 * @param Title $title Page to watch/unwatch
109 * @param User $user User who is watching/unwatching
110 * @param bool $checkRights Passed through to $user->addWatch()
111 * Pass User::CHECK_USER_RIGHTS or User::IGNORE_USER_RIGHTS.
114 public static function doWatch(
117 $checkRights = User
::CHECK_USER_RIGHTS
119 if ( $checkRights && !$user->isAllowed( 'editmywatchlist' ) ) {
120 return User
::newFatalPermissionDeniedStatus( 'editmywatchlist' );
123 $page = WikiPage
::factory( $title );
125 $status = Status
::newFatal( 'hookaborted' );
126 if ( Hooks
::run( 'WatchArticle', [ &$user, &$page, &$status ] ) ) {
127 $status = Status
::newGood();
128 $user->addWatch( $title, $checkRights );
129 Hooks
::run( 'WatchArticleComplete', [ &$user, &$page ] );
137 * @since 1.22 Returns Status
138 * @param Title $title Page to watch/unwatch
139 * @param User $user User who is watching/unwatching
142 public static function doUnwatch( Title
$title, User
$user ) {
143 if ( !$user->isAllowed( 'editmywatchlist' ) ) {
144 return User
::newFatalPermissionDeniedStatus( 'editmywatchlist' );
147 $page = WikiPage
::factory( $title );
149 $status = Status
::newFatal( 'hookaborted' );
150 if ( Hooks
::run( 'UnwatchArticle', [ &$user, &$page, &$status ] ) ) {
151 $status = Status
::newGood();
152 $user->removeWatch( $title );
153 Hooks
::run( 'UnwatchArticleComplete', [ &$user, &$page ] );
160 * Get token to watch (or unwatch) a page for a user
162 * @param Title $title Title object of page to watch
163 * @param User $user User for whom the action is going to be performed
164 * @param string $action Optionally override the action to 'unwatch'
165 * @return string Token
168 public static function getWatchToken( Title
$title, User
$user, $action = 'watch' ) {
169 if ( $action != 'unwatch' ) {
172 // Match ApiWatch and ResourceLoaderUserTokensModule
173 return $user->getEditToken( $action );
177 * Get token to unwatch (or watch) a page for a user
179 * @param Title $title Title object of page to unwatch
180 * @param User $user User for whom the action is going to be performed
181 * @param string $action Optionally override the action to 'watch'
182 * @return string Token
184 * @deprecated since 1.32 Use WatchAction::getWatchToken() with action 'unwatch' directly.
186 public static function getUnwatchToken( Title
$title, User
$user, $action = 'unwatch' ) {
187 wfDeprecated( __METHOD__
, '1.32' );
188 return self
::getWatchToken( $title, $user, $action );
191 public function doesWrites() {