MediaWiki  master
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 ) {
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(
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 
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 }
static factory(Title $title)
Create a WikiPage object of the appropriate class for the given title.
Definition: WikiPage.php:142
static newFatal( $message,... $parameters)
Factory function for fatal errors.
Definition: StatusValue.php:69
getOutput()
Get the OutputPage being used for this instance.
Definition: Action.php:208
addWatch( $title, $checkRights=self::CHECK_USER_RIGHTS)
Watch an article.
Definition: User.php:3659
getTitle()
Shortcut to get the Title object from the page.
Definition: Action.php:247
setTokenSalt( $salt)
Set the salt for the edit token.
Definition: HTMLForm.php:1036
alterForm(HTMLForm $form)
Definition: WatchAction.php:72
getUser()
Shortcut to get the User being used for this instance.
Definition: Action.php:218
isLoggedIn()
Get whether the user is logged in.
Definition: User.php:3521
Redirect a user to the login page.
removeWatch( $title, $checkRights=self::CHECK_USER_RIGHTS)
Stop watching an article.
Definition: User.php:3676
onSubmit( $data)
Definition: WatchAction.php:44
The User object encapsulates all of the user-specific settings (user_id, name, rights, email address, options, last login time).
Definition: User.php:51
checkCanExecute(User $user)
Definition: WatchAction.php:48
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:4377
msg( $key,... $params)
Get a Message object with context set Parameters are the same as wfMessage()
Definition: Action.php:259
getPermissionManager()
An action which shows a form and does something based on the input from the form. ...
Definition: FormAction.php:28
static doWatchOrUnwatch( $watch, Title $title, User $user)
Watch or unwatch a page.
Definition: WatchAction.php:91
isAnon()
Get whether the user is anonymous.
Definition: User.php:3529
static newGood( $value=null)
Factory function for good results.
Definition: StatusValue.php:81
const IGNORE_USER_RIGHTS
Definition: User.php:83
static doWatch(Title $title, User $user, $checkRights=User::CHECK_USER_RIGHTS)
Watch a page.
isWatched( $title, $checkRights=self::CHECK_USER_RIGHTS)
Check the watched status of an article.
Definition: User.php:3645
$page
Page on which we&#39;re performing the action.
Definition: Action.php:46
static getWatchToken(Title $title, User $user, $action='watch')
Get token to watch (or unwatch) a page for a user.
static newFatalPermissionDeniedStatus( $permission)
Factory function for fatal permission-denied errors.
Definition: User.php:5281
Page addition to a user&#39;s watchlist.
Definition: WatchAction.php:30
setSubmitTextMsg( $msg)
Set the text for the submit button to a message.
Definition: HTMLForm.php:1392
setWrapperLegendMsg( $msg)
Prompt the whole form to be wrapped in a "<fieldset>", with this message as its "<legend>" element...
Definition: HTMLForm.php:1579
const CHECK_USER_RIGHTS
Definition: User.php:78
static run( $event, array $args=[], $deprecatedVersion=null)
Call hook functions defined in Hooks::register and $wgHooks.
Definition: Hooks.php:200
static doUnwatch(Title $title, User $user)
Unwatch a page.