MediaWiki REL1_34
WatchAction.php
Go to the documentation of this file.
1<?php
24
30class WatchAction extends FormAction {
31
32 public function getName() {
33 return 'watch';
34 }
35
36 public function requiresUnblock() {
37 return false;
38 }
39
40 protected function getDescription() {
41 return '';
42 }
43
44 public function onSubmit( $data ) {
45 return self::doWatch( $this->getTitle(), $this->getUser() );
46 }
47
48 protected function checkCanExecute( User $user ) {
49 // Must be logged in
50 if ( $user->isAnon() ) {
51 throw new UserNotLoggedIn( 'watchlistanontext', 'watchnologin' );
52 }
53
54 parent::checkCanExecute( $user );
55 }
56
57 protected function usesOOUI() {
58 return true;
59 }
60
61 protected function getFormFields() {
62 return [
63 'intro' => [
64 'type' => 'info',
65 'vertical-label' => true,
66 'raw' => true,
67 'default' => $this->msg( 'confirm-watch-top' )->parse()
68 ]
69 ];
70 }
71
72 protected function alterForm( HTMLForm $form ) {
73 $form->setWrapperLegendMsg( 'addwatch' );
74 $form->setSubmitTextMsg( 'confirm-watch-button' );
75 $form->setTokenSalt( 'watch' );
76 }
77
78 public function onSuccess() {
79 $msgKey = $this->getTitle()->isTalkPage() ? 'addedwatchtext-talk' : 'addedwatchtext';
80 $this->getOutput()->addWikiMsg( $msgKey, $this->getTitle()->getPrefixedText() );
81 }
82
91 public static function doWatchOrUnwatch( $watch, Title $title, User $user ) {
92 if ( $user->isLoggedIn() &&
93 $user->isWatched( $title, User::IGNORE_USER_RIGHTS ) != $watch
94 ) {
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.
97 if ( $watch ) {
98 return self::doWatch( $title, $user, User::IGNORE_USER_RIGHTS );
99 } else {
100 return self::doUnwatch( $title, $user );
101 }
102 }
103
104 return Status::newGood();
105 }
106
116 public static function doWatch(
118 User $user,
119 $checkRights = User::CHECK_USER_RIGHTS
120 ) {
121 $permissionManager = MediaWikiServices::getInstance()->getPermissionManager();
122 if ( $checkRights && !$permissionManager->userHasRight( $user, 'editmywatchlist' ) ) {
123 return User::newFatalPermissionDeniedStatus( 'editmywatchlist' );
124 }
125
126 $page = WikiPage::factory( $title );
127
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 ] );
133 }
134
135 return $status;
136 }
137
145 public static function doUnwatch( Title $title, User $user ) {
146 if ( !MediaWikiServices::getInstance()
148 ->userHasRight( $user, 'editmywatchlist' ) ) {
149 return User::newFatalPermissionDeniedStatus( 'editmywatchlist' );
150 }
151
152 $page = WikiPage::factory( $title );
153
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 ] );
159 }
160
161 return $status;
162 }
163
173 public static function getWatchToken( Title $title, User $user, $action = 'watch' ) {
174 if ( $action != 'unwatch' ) {
175 $action = 'watch';
176 }
177 // Match ApiWatch and ResourceLoaderUserTokensModule
178 return $user->getEditToken( $action );
179 }
180
181 public function doesWrites() {
182 return true;
183 }
184}
getPermissionManager()
$page
Page on which we're performing the action.
Definition Action.php:46
getTitle()
Shortcut to get the Title object from the page.
Definition Action.php:247
getOutput()
Get the OutputPage being used for this instance.
Definition Action.php:208
getUser()
Shortcut to get the User being used for this instance.
Definition Action.php:218
msg( $key,... $params)
Get a Message object with context set Parameters are the same as wfMessage()
Definition Action.php:259
An action which shows a form and does something based on the input from the form.
Object handling generic submission, CSRF protection, layout and other logic for UI forms in a reusabl...
Definition HTMLForm.php:131
setSubmitTextMsg( $msg)
Set the text for the submit button to a message.
setWrapperLegendMsg( $msg)
Prompt the whole form to be wrapped in a "<fieldset>", with this message as its "<legend>" element.
setTokenSalt( $salt)
Set the salt for the edit token.
MediaWikiServices is the service locator for the application scope of MediaWiki.
Represents a title within MediaWiki.
Definition Title.php:42
Redirect a user to the login page.
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition User.php:51
addWatch( $title, $checkRights=self::CHECK_USER_RIGHTS)
Watch an article.
Definition User.php:3768
getEditToken( $salt='', $request=null)
Initialize (if necessary) and return a session token value which can be used in edit forms to show th...
Definition User.php:4486
isWatched( $title, $checkRights=self::CHECK_USER_RIGHTS)
Check the watched status of an article.
Definition User.php:3754
removeWatch( $title, $checkRights=self::CHECK_USER_RIGHTS)
Stop watching an article.
Definition User.php:3785
isLoggedIn()
Get whether the user is logged in.
Definition User.php:3630
isAnon()
Get whether the user is anonymous.
Definition User.php:3638
Page addition to a user's watchlist.
getDescription()
Returns the description that goes below the <h1> tag.
static doWatch(Title $title, User $user, $checkRights=User::CHECK_USER_RIGHTS)
Watch a page.
requiresUnblock()
Whether this action can still be executed by a blocked user.
static doWatchOrUnwatch( $watch, Title $title, User $user)
Watch or unwatch a page.
getFormFields()
Get an HTMLForm descriptor array.
static doUnwatch(Title $title, User $user)
Unwatch a page.
usesOOUI()
Whether the form should use OOUI.
alterForm(HTMLForm $form)
Play with the HTMLForm if you need to more substantially.
static getWatchToken(Title $title, User $user, $action='watch')
Get token to watch (or unwatch) a page for a user.
checkCanExecute(User $user)
Checks if the given user (identified by an object) can perform this action.
doesWrites()
Indicates whether this action may perform database writes.
onSuccess()
Do something exciting on successful processing of the form.
onSubmit( $data)
Process the form on POST submission.
getName()
Return the name of the action this object responds to.