MediaWiki  1.34.0
WatchAction.php
Go to the documentation of this file.
1 <?php
24 
30 class 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 ) {
99  } else {
100  return self::doUnwatch( $title, $user );
101  }
102  }
103 
104  return Status::newGood();
105  }
106 
116  public static function doWatch(
117  Title $title,
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 
127 
128  $status = Status::newFatal( 'hookaborted' );
129  if ( Hooks::run( 'WatchArticle', [ &$user, &$page, &$status ] ) ) {
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 
153 
154  $status = Status::newFatal( 'hookaborted' );
155  if ( Hooks::run( 'UnwatchArticle', [ &$user, &$page, &$status ] ) ) {
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 }
StatusValue\newFatal
static newFatal( $message,... $parameters)
Factory function for fatal errors.
Definition: StatusValue.php:69
User\isAnon
isAnon()
Get whether the user is anonymous.
Definition: User.php:3532
WatchAction\alterForm
alterForm(HTMLForm $form)
Play with the HTMLForm if you need to more substantially.
Definition: WatchAction.php:72
WatchAction\onSuccess
onSuccess()
Do something exciting on successful processing of the form.
Definition: WatchAction.php:78
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:117
User\newFatalPermissionDeniedStatus
static newFatalPermissionDeniedStatus( $permission)
Factory function for fatal permission-denied errors.
Definition: User.php:5284
WatchAction\requiresUnblock
requiresUnblock()
Whether this action can still be executed by a blocked user.
Definition: WatchAction.php:36
WatchAction\doUnwatch
static doUnwatch(Title $title, User $user)
Unwatch a page.
Definition: WatchAction.php:145
UserNotLoggedIn
Redirect a user to the login page.
Definition: UserNotLoggedIn.php:53
HTMLForm\setTokenSalt
setTokenSalt( $salt)
Set the salt for the edit token.
Definition: HTMLForm.php:1032
FormAction
An action which shows a form and does something based on the input from the form.
Definition: FormAction.php:28
WatchAction\getDescription
getDescription()
Returns the description that goes below the <h1> tag.
Definition: WatchAction.php:40
WatchAction
Page addition to a user's watchlist.
Definition: WatchAction.php:30
WatchAction\onSubmit
onSubmit( $data)
Process the form on POST submission.
Definition: WatchAction.php:44
WatchAction\getFormFields
getFormFields()
Get an HTMLForm descriptor array.
Definition: WatchAction.php:61
WatchAction\doWatch
static doWatch(Title $title, User $user, $checkRights=User::CHECK_USER_RIGHTS)
Watch a page.
Definition: WatchAction.php:116
WatchAction\doesWrites
doesWrites()
Indicates whether this action may perform database writes.
Definition: WatchAction.php:181
WikiPage\factory
static factory(Title $title)
Create a WikiPage object of the appropriate class for the given title.
Definition: WikiPage.php:142
WatchAction\checkCanExecute
checkCanExecute(User $user)
Checks if the given user (identified by an object) can perform this action.
Definition: WatchAction.php:48
getPermissionManager
getPermissionManager()
WatchAction\doWatchOrUnwatch
static doWatchOrUnwatch( $watch, Title $title, User $user)
Watch or unwatch a page.
Definition: WatchAction.php:91
$title
$title
Definition: testCompression.php:34
User\CHECK_USER_RIGHTS
const CHECK_USER_RIGHTS
Definition: User.php:78
User\addWatch
addWatch( $title, $checkRights=self::CHECK_USER_RIGHTS)
Watch an article.
Definition: User.php:3662
Action\getUser
getUser()
Shortcut to get the User being used for this instance.
Definition: Action.php:218
StatusValue\newGood
static newGood( $value=null)
Factory function for good results.
Definition: StatusValue.php:81
Action\getTitle
getTitle()
Shortcut to get the Title object from the page.
Definition: Action.php:247
User\removeWatch
removeWatch( $title, $checkRights=self::CHECK_USER_RIGHTS)
Stop watching an article.
Definition: User.php:3679
WatchAction\getName
getName()
Return the name of the action this object responds to.
Definition: WatchAction.php:32
HTMLForm\setSubmitTextMsg
setSubmitTextMsg( $msg)
Set the text for the submit button to a message.
Definition: HTMLForm.php:1388
Title
Represents a title within MediaWiki.
Definition: Title.php:42
$status
return $status
Definition: SyntaxHighlight.php:347
Action\$page
$page
Page on which we're performing the action.
Definition: Action.php:46
User\isLoggedIn
isLoggedIn()
Get whether the user is logged in.
Definition: User.php:3524
Action\msg
msg( $key,... $params)
Get a Message object with context set Parameters are the same as wfMessage()
Definition: Action.php:259
HTMLForm\setWrapperLegendMsg
setWrapperLegendMsg( $msg)
Prompt the whole form to be wrapped in a "<fieldset>", with this message as its "<legend>" element.
Definition: HTMLForm.php:1575
Action\getOutput
getOutput()
Get the OutputPage being used for this instance.
Definition: Action.php:208
WatchAction\getWatchToken
static getWatchToken(Title $title, User $user, $action='watch')
Get token to watch (or unwatch) a page for a user.
Definition: WatchAction.php:173
User\IGNORE_USER_RIGHTS
const IGNORE_USER_RIGHTS
Definition: User.php:83
User\isWatched
isWatched( $title, $checkRights=self::CHECK_USER_RIGHTS)
Check the watched status of an article.
Definition: User.php:3648
User\getEditToken
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:4380
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:51
Hooks\run
static run( $event, array $args=[], $deprecatedVersion=null)
Call hook functions defined in Hooks::register and $wgHooks.
Definition: Hooks.php:200
WatchAction\usesOOUI
usesOOUI()
Whether the form should use OOUI.
Definition: WatchAction.php:57
HTMLForm
Object handling generic submission, CSRF protection, layout and other logic for UI forms in a reusabl...
Definition: HTMLForm.php:131