MediaWiki REL1_28
WatchAction.php
Go to the documentation of this file.
1<?php
28class 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 $msgKey = $this->getTitle()->isTalkPage() ? 'addedwatchtext-talk' : 'addedwatchtext';
71 $this->getOutput()->addWikiMsg( $msgKey, $this->getTitle()->getPrefixedText() );
72 }
73
74 /* Static utility methods */
75
84 public static function doWatchOrUnwatch( $watch, Title $title, User $user ) {
85 if ( $user->isLoggedIn() &&
86 $user->isWatched( $title, User::IGNORE_USER_RIGHTS ) != $watch
87 ) {
88 // If the user doesn't have 'editmywatchlist', we still want to
89 // allow them to add but not remove items via edits and such.
90 if ( $watch ) {
91 return self::doWatch( $title, $user, User::IGNORE_USER_RIGHTS );
92 } else {
93 return self::doUnwatch( $title, $user );
94 }
95 }
96
97 return Status::newGood();
98 }
99
109 public static function doWatch(
110 Title $title,
111 User $user,
112 $checkRights = User::CHECK_USER_RIGHTS
113 ) {
114 if ( $checkRights && !$user->isAllowed( 'editmywatchlist' ) ) {
115 return User::newFatalPermissionDeniedStatus( 'editmywatchlist' );
116 }
117
119
120 $status = Status::newFatal( 'hookaborted' );
121 if ( Hooks::run( 'WatchArticle', [ &$user, &$page, &$status ] ) ) {
122 $status = Status::newGood();
123 $user->addWatch( $title, $checkRights );
124 Hooks::run( 'WatchArticleComplete', [ &$user, &$page ] );
125 }
126
127 return $status;
128 }
129
137 public static function doUnwatch( Title $title, User $user ) {
138 if ( !$user->isAllowed( 'editmywatchlist' ) ) {
139 return User::newFatalPermissionDeniedStatus( 'editmywatchlist' );
140 }
141
143
144 $status = Status::newFatal( 'hookaborted' );
145 if ( Hooks::run( 'UnwatchArticle', [ &$user, &$page, &$status ] ) ) {
146 $status = Status::newGood();
147 $user->removeWatch( $title );
148 Hooks::run( 'UnwatchArticleComplete', [ &$user, &$page ] );
149 }
150
151 return $status;
152 }
153
163 public static function getWatchToken( Title $title, User $user, $action = 'watch' ) {
164 if ( $action != 'unwatch' ) {
165 $action = 'watch';
166 }
167 // Match ApiWatch and ResourceLoaderUserTokensModule
168 return $user->getEditToken( $action );
169 }
170
180 public static function getUnwatchToken( Title $title, User $user, $action = 'unwatch' ) {
181 return self::getWatchToken( $title, $user, $action );
182 }
183
184 public function doesWrites() {
185 return true;
186 }
187}
msg()
Get a Message object with context set Parameters are the same as wfMessage()
Definition Action.php:256
$page
Page on which we're performing the action.
Definition Action.php:44
getTitle()
Shortcut to get the Title object from the page.
Definition Action.php:246
getOutput()
Get the OutputPage being used for this instance.
Definition Action.php:207
getUser()
Shortcut to get the User being used for this instance.
Definition Action.php:217
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.
Definition HTMLForm.php:128
setSubmitTextMsg( $msg)
Set the text for the submit button to a message.
setTokenSalt( $salt)
Set the salt for the edit token.
Definition HTMLForm.php:984
Represents a title within MediaWiki.
Definition Title.php:36
Redirect a user to the login page.
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition User.php:48
Page addition to a user's watchlist.
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.
static doUnwatch(Title $title, User $user)
Unwatch a page.
alterForm(HTMLForm $form)
Play with the HTMLForm if you need to more substantially.
static getUnwatchToken(Title $title, User $user, $action='unwatch')
Get token to unwatch (or watch) a page for a user.
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.
preText()
Add pre- or post-text to the form.
onSubmit( $data)
Process the form on POST submission.
getName()
Return the name of the action this object responds to.
static factory(Title $title)
Create a WikiPage object of the appropriate class for the given title.
Definition WikiPage.php:115
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:1049
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:249
namespace and then decline to actually register it file or subcat img or subcat $title
Definition hooks.txt:986
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:37