MediaWiki  1.29.1
WatchAction.php
Go to the documentation of this file.
1 <?php
28 class WatchAction extends FormAction {
29 
30  public function getName() {
31  return 'watch';
32  }
33 
34  public function requiresUnblock() {
35  return false;
36  }
37 
38  protected function getDescription() {
39  return '';
40  }
41 
42  public function onSubmit( $data ) {
43  self::doWatch( $this->getTitle(), $this->getUser() );
44 
45  return true;
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 
83  /* Static utility methods */
84 
93  public static function doWatchOrUnwatch( $watch, Title $title, User $user ) {
94  if ( $user->isLoggedIn() &&
95  $user->isWatched( $title, User::IGNORE_USER_RIGHTS ) != $watch
96  ) {
97  // If the user doesn't have 'editmywatchlist', we still want to
98  // allow them to add but not remove items via edits and such.
99  if ( $watch ) {
101  } else {
102  return self::doUnwatch( $title, $user );
103  }
104  }
105 
106  return Status::newGood();
107  }
108 
118  public static function doWatch(
119  Title $title,
120  User $user,
121  $checkRights = User::CHECK_USER_RIGHTS
122  ) {
123  if ( $checkRights && !$user->isAllowed( 'editmywatchlist' ) ) {
124  return User::newFatalPermissionDeniedStatus( 'editmywatchlist' );
125  }
126 
128 
129  $status = Status::newFatal( 'hookaborted' );
130  if ( Hooks::run( 'WatchArticle', [ &$user, &$page, &$status ] ) ) {
132  $user->addWatch( $title, $checkRights );
133  Hooks::run( 'WatchArticleComplete', [ &$user, &$page ] );
134  }
135 
136  return $status;
137  }
138 
146  public static function doUnwatch( Title $title, User $user ) {
147  if ( !$user->isAllowed( 'editmywatchlist' ) ) {
148  return User::newFatalPermissionDeniedStatus( 'editmywatchlist' );
149  }
150 
152 
153  $status = Status::newFatal( 'hookaborted' );
154  if ( Hooks::run( 'UnwatchArticle', [ &$user, &$page, &$status ] ) ) {
156  $user->removeWatch( $title );
157  Hooks::run( 'UnwatchArticleComplete', [ &$user, &$page ] );
158  }
159 
160  return $status;
161  }
162 
172  public static function getWatchToken( Title $title, User $user, $action = 'watch' ) {
173  if ( $action != 'unwatch' ) {
174  $action = 'watch';
175  }
176  // Match ApiWatch and ResourceLoaderUserTokensModule
177  return $user->getEditToken( $action );
178  }
179 
189  public static function getUnwatchToken( Title $title, User $user, $action = 'unwatch' ) {
190  return self::getWatchToken( $title, $user, $action );
191  }
192 
193  public function doesWrites() {
194  return true;
195  }
196 }
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
User\newFatalPermissionDeniedStatus
static newFatalPermissionDeniedStatus( $permission)
Factory function for fatal permission-denied errors.
Definition: User.php:5467
WatchAction\requiresUnblock
requiresUnblock()
Whether this action can still be executed by a blocked user.
Definition: WatchAction.php:34
WatchAction\doUnwatch
static doUnwatch(Title $title, User $user)
Unwatch a page.
Definition: WatchAction.php:146
UserNotLoggedIn
Redirect a user to the login page.
Definition: UserNotLoggedIn.php:53
$status
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist Do not use this to implement individual filters if they are compatible with the ChangesListFilter and ChangesListFilterGroup structure use sub classes of those in conjunction with the ChangesListSpecialPageStructuredFilters hook This hook can be used to implement filters that do not implement that or custom behavior that is not an individual filter e g Watchlist and Watchlist you will want to construct new ChangesListBooleanFilter or ChangesListStringOptionsFilter objects When constructing you specify which group they belong to You can reuse existing or create your you must register them with $special registerFilterGroup removed from all revisions and log entries to which it was applied This gives extensions a chance to take it off their books as the deletion has already been partly carried out by this point or something similar the user will be unable to create the tag set $status
Definition: hooks.txt:1049
HTMLForm\setTokenSalt
setTokenSalt( $salt)
Set the salt for the edit token.
Definition: HTMLForm.php:991
$user
please add to it if you re going to add events to the MediaWiki code where normally authentication against an external auth plugin would be creating a account $user
Definition: hooks.txt:246
StatusValue\newFatal
static newFatal( $message)
Factory function for fatal errors.
Definition: StatusValue.php:63
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:38
WatchAction
Page addition to a user's watchlist.
Definition: WatchAction.php:28
WatchAction\onSubmit
onSubmit( $data)
Process the form on POST submission.
Definition: WatchAction.php:42
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
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:118
WatchAction\doesWrites
doesWrites()
Indicates whether this action may perform database writes.
Definition: WatchAction.php:193
$title
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:934
WikiPage\factory
static factory(Title $title)
Create a WikiPage object of the appropriate class for the given title.
Definition: WikiPage.php:120
WatchAction\checkCanExecute
checkCanExecute(User $user)
Checks if the given user (identified by an object) can perform this action.
Definition: WatchAction.php:48
WatchAction\doWatchOrUnwatch
static doWatchOrUnwatch( $watch, Title $title, User $user)
Watch or unwatch a page.
Definition: WatchAction.php:93
User\CHECK_USER_RIGHTS
const CHECK_USER_RIGHTS
Definition: User.php:82
Action\getUser
getUser()
Shortcut to get the User being used for this instance.
Definition: Action.php:217
StatusValue\newGood
static newGood( $value=null)
Factory function for good results.
Definition: StatusValue.php:76
Action\getTitle
getTitle()
Shortcut to get the Title object from the page.
Definition: Action.php:246
WatchAction\getName
getName()
Return the name of the action this object responds to.
Definition: WatchAction.php:30
HTMLForm\setSubmitTextMsg
setSubmitTextMsg( $msg)
Set the text for the submit button to a message.
Definition: HTMLForm.php:1359
Action\msg
msg()
Get a Message object with context set Parameters are the same as wfMessage()
Definition: Action.php:256
Title
Represents a title within MediaWiki.
Definition: Title.php:39
Action\$page
$page
Page on which we're performing the action.
Definition: Action.php:44
HTMLForm\setWrapperLegendMsg
setWrapperLegendMsg( $msg)
Prompt the whole form to be wrapped in a "<fieldset>", with this message as its "<legend>" element.
Definition: HTMLForm.php:1533
Action\getOutput
getOutput()
Get the OutputPage being used for this instance.
Definition: Action.php:207
WatchAction\getWatchToken
static getWatchToken(Title $title, User $user, $action='watch')
Get token to watch (or unwatch) a page for a user.
Definition: WatchAction.php:172
User\IGNORE_USER_RIGHTS
const IGNORE_USER_RIGHTS
Definition: User.php:87
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:50
Hooks\run
static run( $event, array $args=[], $deprecatedVersion=null)
Call hook functions defined in Hooks::register and $wgHooks.
Definition: Hooks.php:131
WatchAction\getUnwatchToken
static getUnwatchToken(Title $title, User $user, $action='unwatch')
Get token to unwatch (or watch) a page for a user.
Definition: WatchAction.php:189
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.
Definition: HTMLForm.php:128