MediaWiki  1.27.2
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 
41  protected function getDescription() {
42  return $this->msg( 'addwatch' )->escaped();
43  }
44 
45  public function onSubmit( $data ) {
46  self::doWatch( $this->getTitle(), $this->getUser() );
47 
48  return true;
49  }
50 
51  protected function checkCanExecute( User $user ) {
52  // Must be logged in
53  if ( $user->isAnon() ) {
54  throw new UserNotLoggedIn( 'watchlistanontext', 'watchnologin' );
55  }
56 
57  parent::checkCanExecute( $user );
58  }
59 
60  protected function alterForm( HTMLForm $form ) {
61  $form->setSubmitTextMsg( 'confirm-watch-button' );
62  $form->setTokenSalt( 'watch' );
63  }
64 
65  protected function preText() {
66  return $this->msg( 'confirm-watch-top' )->parse();
67  }
68 
69  public function onSuccess() {
70  $this->getOutput()->addWikiMsg( 'addedwatchtext', $this->getTitle()->getPrefixedText() );
71  }
72 
73  /* Static utility methods */
74 
83  public static function doWatchOrUnwatch( $watch, Title $title, User $user ) {
84  if ( $user->isLoggedIn() &&
85  $user->isWatched( $title, User::IGNORE_USER_RIGHTS ) != $watch
86  ) {
87  // If the user doesn't have 'editmywatchlist', we still want to
88  // allow them to add but not remove items via edits and such.
89  if ( $watch ) {
90  return self::doWatch( $title, $user, User::IGNORE_USER_RIGHTS );
91  } else {
92  return self::doUnwatch( $title, $user );
93  }
94  }
95 
96  return Status::newGood();
97  }
98 
108  public static function doWatch(
109  Title $title,
110  User $user,
111  $checkRights = User::CHECK_USER_RIGHTS
112  ) {
113  if ( $checkRights && !$user->isAllowed( 'editmywatchlist' ) ) {
114  return User::newFatalPermissionDeniedStatus( 'editmywatchlist' );
115  }
116 
117  $page = WikiPage::factory( $title );
118 
119  $status = Status::newFatal( 'hookaborted' );
120  if ( Hooks::run( 'WatchArticle', [ &$user, &$page, &$status ] ) ) {
122  $user->addWatch( $title, $checkRights );
123  Hooks::run( 'WatchArticleComplete', [ &$user, &$page ] );
124  }
125 
126  return $status;
127  }
128 
136  public static function doUnwatch( Title $title, User $user ) {
137  if ( !$user->isAllowed( 'editmywatchlist' ) ) {
138  return User::newFatalPermissionDeniedStatus( 'editmywatchlist' );
139  }
140 
141  $page = WikiPage::factory( $title );
142 
143  $status = Status::newFatal( 'hookaborted' );
144  if ( Hooks::run( 'UnwatchArticle', [ &$user, &$page, &$status ] ) ) {
146  $user->removeWatch( $title );
147  Hooks::run( 'UnwatchArticleComplete', [ &$user, &$page ] );
148  }
149 
150  return $status;
151  }
152 
162  public static function getWatchToken( Title $title, User $user, $action = 'watch' ) {
163  if ( $action != 'unwatch' ) {
164  $action = 'watch';
165  }
166  // Match ApiWatch and ResourceLoaderUserTokensModule
167  return $user->getEditToken( $action );
168  }
169 
179  public static function getUnwatchToken( Title $title, User $user, $action = 'unwatch' ) {
180  return self::getWatchToken( $title, $user, $action );
181  }
182 
183  public function doesWrites() {
184  return true;
185  }
186 }
static factory(Title $title)
Create a WikiPage object of the appropriate class for the given title.
Definition: WikiPage.php:99
setTokenSalt($salt)
Set the salt for the edit token.
Definition: HTMLForm.php:939
getOutput()
Get the OutputPage being used for this instance.
Definition: Action.php:207
onSubmit($data)
Definition: WatchAction.php:45
getTitle()
Shortcut to get the Title object from the page.
Definition: Action.php:246
alterForm(HTMLForm $form)
Definition: WatchAction.php:60
getUser()
Shortcut to get the User being used for this instance.
Definition: Action.php:217
isLoggedIn()
Get whether the user is logged in.
Definition: User.php:3360
Represents a title within MediaWiki.
Definition: Title.php:34
static newFatal($message)
Factory function for fatal errors.
Definition: Status.php:89
Redirect a user to the login page.
checkCanExecute(User $user)
Definition: WatchAction.php:51
An action which shows a form and does something based on the input from the form. ...
Definition: FormAction.php:28
isAllowed($action= '')
Internal mechanics of testing a permission.
Definition: User.php:3408
isAnon()
Get whether the user is anonymous.
Definition: User.php:3368
const IGNORE_USER_RIGHTS
Definition: User.php:84
static doWatch(Title $title, User $user, $checkRights=User::CHECK_USER_RIGHTS)
Watch a page.
setSubmitTextMsg($msg)
Set the text for the submit button to a message.
Definition: HTMLForm.php:1250
Object handling generic submission, CSRF protection, layout and other logic for UI forms...
Definition: HTMLForm.php:123
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:912
static run($event, array $args=[], $deprecatedVersion=null)
Call hook functions defined in Hooks::register and $wgHooks.
Definition: Hooks.php:131
isWatched($title, $checkRights=self::CHECK_USER_RIGHTS)
Check the watched status of an article.
Definition: User.php:3472
$page
Page on which we're performing the action.
Definition: Action.php:44
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 local account $user
Definition: hooks.txt:242
static getWatchToken(Title $title, User $user, $action= 'watch')
Get token to watch (or unwatch) a page for a user.
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
removeWatch($title, $checkRights=self::CHECK_USER_RIGHTS)
Stop watching an article.
Definition: User.php:3503
Page addition to a user's watchlist.
Definition: WatchAction.php:28
static getUnwatchToken(Title $title, User $user, $action= 'unwatch')
Get token to unwatch (or watch) a page for a user.
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist e g Watchlist 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:1004
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:4180
static newFatalPermissionDeniedStatus($permission)
Factory function for fatal permission-denied errors.
Definition: User.php:5264
addWatch($title, $checkRights=self::CHECK_USER_RIGHTS)
Watch an article.
Definition: User.php:3486
msg()
Get a Message object with context set Parameters are the same as wfMessage()
Definition: Action.php:256
static doWatchOrUnwatch($watch, Title $title, User $user)
Watch or unwatch a page.
Definition: WatchAction.php:83
const CHECK_USER_RIGHTS
Definition: User.php:79
static newGood($value=null)
Factory function for good results.
Definition: Status.php:101
static doUnwatch(Title $title, User $user)
Unwatch a page.