MediaWiki  1.23.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 
38  protected function getDescription() {
39  return $this->msg( 'addwatch' )->escaped();
40  }
41 
46  protected function getFormFields() {
47  return array();
48  }
49 
50  public function onSubmit( $data ) {
51  wfProfileIn( __METHOD__ );
52  self::doWatch( $this->getTitle(), $this->getUser() );
53  wfProfileOut( __METHOD__ );
54 
55  return true;
56  }
57 
61  public function show() {
62  $this->setHeaders();
63 
64  $user = $this->getUser();
65  // This will throw exceptions if there's a problem
66  $this->checkCanExecute( $user );
67 
68  // Must have valid token for this action/title
69  $salt = array( $this->getName(), $this->getTitle()->getDBkey() );
70 
71  if ( $user->matchEditToken( $this->getRequest()->getVal( 'token' ), $salt ) ) {
72  $this->onSubmit( array() );
73  $this->onSuccess();
74  } else {
75  $form = $this->getForm();
76  if ( $form->show() ) {
77  $this->onSuccess();
78  }
79  }
80  }
81 
82  protected function checkCanExecute( User $user ) {
83  // Must be logged in
84  if ( $user->isAnon() ) {
85  $loginreqlink = Linker::linkKnown(
86  SpecialPage::getTitleFor( 'Userlogin' ),
87  $this->msg( 'loginreqlink' )->escaped(),
88  array(),
89  array( 'returnto' => $this->getPageTitle(), 'returntoquery' => 'action=' . $this->getName() )
90  );
91  $reasonMsg = $this->msg( 'watchlistanontext' )->rawParams( $loginreqlink );
92  throw new UserNotLoggedIn( $reasonMsg, 'watchnologin' );
93  }
94 
95  return parent::checkCanExecute( $user );
96  }
97 
106  public static function doWatchOrUnwatch( $watch, Title $title, User $user ) {
107  if ( $user->isLoggedIn() &&
108  $user->isWatched( $title, WatchedItem::IGNORE_USER_RIGHTS ) != $watch
109  ) {
110  // If the user doesn't have 'editmywatchlist', we still want to
111  // allow them to add but not remove items via edits and such.
112  if ( $watch ) {
114  } else {
115  return self::doUnwatch( $title, $user );
116  }
117  }
118 
119  return Status::newGood();
120  }
121 
130  public static function doWatch( Title $title, User $user,
131  $checkRights = WatchedItem::CHECK_USER_RIGHTS
132  ) {
133  if ( $checkRights !== WatchedItem::IGNORE_USER_RIGHTS &&
134  !$user->isAllowed( 'editmywatchlist' )
135  ) {
136  return User::newFatalPermissionDeniedStatus( 'editmywatchlist' );
137  }
138 
140 
141  $status = Status::newFatal( 'hookaborted' );
142  if ( wfRunHooks( 'WatchArticle', array( &$user, &$page, &$status ) ) ) {
143  $status = Status::newGood();
144  $user->addWatch( $title, $checkRights );
145  wfRunHooks( 'WatchArticleComplete', array( &$user, &$page ) );
146  }
147 
148  return $status;
149  }
150 
158  public static function doUnwatch( Title $title, User $user ) {
159  if ( !$user->isAllowed( 'editmywatchlist' ) ) {
160  return User::newFatalPermissionDeniedStatus( 'editmywatchlist' );
161  }
162 
164 
165  $status = Status::newFatal( 'hookaborted' );
166  if ( wfRunHooks( 'UnwatchArticle', array( &$user, &$page, &$status ) ) ) {
167  $status = Status::newGood();
168  $user->removeWatch( $title );
169  wfRunHooks( 'UnwatchArticleComplete', array( &$user, &$page ) );
170  }
171 
172  return $status;
173  }
174 
184  public static function getWatchToken( Title $title, User $user, $action = 'watch' ) {
185  if ( $action != 'unwatch' ) {
186  $action = 'watch';
187  }
188  $salt = array( $action, $title->getDBkey() );
189 
190  // This token stronger salted and not compatible with ApiWatch
191  // It's title/action specific because index.php is GET and API is POST
192  return $user->getEditToken( $salt );
193  }
194 
204  public static function getUnwatchToken( Title $title, User $user, $action = 'unwatch' ) {
205  return self::getWatchToken( $title, $user, $action );
206  }
207 
208  protected function alterForm( HTMLForm $form ) {
209  $form->setSubmitTextMsg( 'confirm-watch-button' );
210  }
211 
212  protected function preText() {
213  return $this->msg( 'confirm-watch-top' )->parse();
214  }
215 
216  public function onSuccess() {
217  $this->getOutput()->addWikiMsg( 'addedwatchtext', $this->getTitle()->getPrefixedText() );
218  }
219 }
220 
226 class UnwatchAction extends WatchAction {
227 
228  public function getName() {
229  return 'unwatch';
230  }
231 
232  protected function getDescription() {
233  return $this->msg( 'removewatch' )->escaped();
234  }
235 
236  public function onSubmit( $data ) {
237  wfProfileIn( __METHOD__ );
238  self::doUnwatch( $this->getTitle(), $this->getUser() );
239  wfProfileOut( __METHOD__ );
240 
241  return true;
242  }
243 
244  protected function alterForm( HTMLForm $form ) {
245  $form->setSubmitTextMsg( 'confirm-unwatch-button' );
246  }
247 
248  protected function preText() {
249  return $this->msg( 'confirm-unwatch-top' )->parse();
250  }
251 
252  public function onSuccess() {
253  $this->getOutput()->addWikiMsg( 'removedwatchtext', $this->getTitle()->getPrefixedText() );
254  }
255 }
WatchAction\show
show()
This can be either formed or formless depending on the session token given.
Definition: WatchAction.php:61
UnwatchAction\onSuccess
onSuccess()
Do something exciting on successful processing of the form.
Definition: WatchAction.php:252
php
skin txt MediaWiki includes four core it has been set as the default in MediaWiki since the replacing Monobook it had been been the default skin since before being replaced by Vector largely rewritten in while keeping its appearance Several legacy skins were removed in the as the burden of supporting them became too heavy to bear Those in etc for skin dependent CSS etc for skin dependent JavaScript These can also be customised on a per user by etc This feature has led to a wide variety of user styles becoming that gallery is a good place to ending in php
Definition: skin.txt:62
UnwatchAction\preText
preText()
Add pre- or post-text to the form.
Definition: WatchAction.php:248
UnwatchAction\getDescription
getDescription()
Returns the description that goes below the <h1> tag.
Definition: WatchAction.php:232
WatchAction\alterForm
alterForm(HTMLForm $form)
Play with the HTMLForm if you need to more substantially.
Definition: WatchAction.php:208
WatchAction\onSuccess
onSuccess()
Do something exciting on successful processing of the form.
Definition: WatchAction.php:216
Action\setHeaders
setHeaders()
Set output headers for noindexing etc.
Definition: Action.php:338
User\newFatalPermissionDeniedStatus
static newFatalPermissionDeniedStatus( $permission)
Factory function for fatal permission-denied errors.
Definition: User.php:4854
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:158
$form
usually copyright or history_copyright This message must be in HTML not wikitext $subpages will be ignored and the rest of subPageSubtitle() will run. 'SkinTemplateBuildNavUrlsNav_urlsAfterPermalink' whether MediaWiki currently thinks this is a CSS JS page Hooks may change this value to override the return value of Title::isCssOrJsPage(). 'TitleIsAlwaysKnown' whether MediaWiki currently thinks this page is known isMovable() always returns false. $title whether MediaWiki currently thinks this page is movable Hooks may change this value to override the return value of Title::isMovable(). 'TitleIsWikitextPage' whether MediaWiki currently thinks this is a wikitext page Hooks may change this value to override the return value of Title::isWikitextPage() 'TitleMove' use UploadVerification and UploadVerifyFile instead $form
Definition: hooks.txt:2573
UnwatchAction\alterForm
alterForm(HTMLForm $form)
Play with the HTMLForm if you need to more substantially.
Definition: WatchAction.php:244
UserNotLoggedIn
Shows a generic "user is not logged in" error page.
Definition: UserNotLoggedIn.php:48
wfProfileIn
wfProfileIn( $functionname)
Begin profiling of a function.
Definition: Profiler.php:33
Status\newGood
static newGood( $value=null)
Factory function for good results.
Definition: Status.php:77
FormAction
An action which shows a form and does something based on the input from the form.
Definition: FormAction.php:29
SpecialPage\getTitleFor
static getTitleFor( $name, $subpage=false, $fragment='')
Get a localised Title object for a specified special page name.
Definition: SpecialPage.php:74
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
UnwatchAction
Page removal from a user's watchlist.
Definition: WatchAction.php:226
WatchAction\onSubmit
onSubmit( $data)
Process the form on POST submission.
Definition: WatchAction.php:50
Linker\linkKnown
static linkKnown( $target, $html=null, $customAttribs=array(), $query=array(), $options=array( 'known', 'noclasses'))
Identical to link(), except $options defaults to 'known'.
Definition: Linker.php:264
WatchAction\getFormFields
getFormFields()
Just get an empty form with a single submit button.
Definition: WatchAction.php:46
WikiPage\factory
static factory(Title $title)
Create a WikiPage object of the appropriate class for the given title.
Definition: WikiPage.php:103
WatchAction\checkCanExecute
checkCanExecute(User $user)
Checks if the given user (identified by an object) can perform this action.
Definition: WatchAction.php:82
UnwatchAction\getName
getName()
Return the name of the action this object responds to.
Definition: WatchAction.php:228
UnwatchAction\onSubmit
onSubmit( $data)
Process the form on POST submission.
Definition: WatchAction.php:236
WatchedItem\IGNORE_USER_RIGHTS
const IGNORE_USER_RIGHTS
Constant to specify that user rights 'editmywatchlist' and 'viewmywatchlist' should not be checked.
Definition: WatchedItem.php:35
wfProfileOut
wfProfileOut( $functionname='missing')
Stop profiling of a function.
Definition: Profiler.php:46
wfRunHooks
wfRunHooks( $event, array $args=array(), $deprecatedVersion=null)
Call hook functions defined in $wgHooks.
Definition: GlobalFunctions.php:4001
WatchAction\doWatchOrUnwatch
static doWatchOrUnwatch( $watch, Title $title, User $user)
Watch or unwatch a page.
Definition: WatchAction.php:106
array
the array() calling protocol came about after MediaWiki 1.4rc1.
List of Api Query prop modules.
FormAction\getForm
getForm()
Get the HTMLForm to control behavior.
Definition: FormAction.php:63
Action\getUser
getUser()
Shortcut to get the User being used for this instance.
Definition: Action.php:200
$title
presenting them properly to the user as errors is done by the caller $title
Definition: hooks.txt:1324
WatchedItem\CHECK_USER_RIGHTS
const CHECK_USER_RIGHTS
Constant to specify that user rights 'editmywatchlist' and 'viewmywatchlist' should be checked.
Definition: WatchedItem.php:42
Action\getTitle
getTitle()
Shortcut to get the Title object from the page.
Definition: Action.php:237
$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:237
WatchAction\getName
getName()
Return the name of the action this object responds to.
Definition: WatchAction.php:30
Action\msg
msg()
Get a Message object with context set Parameters are the same as wfMessage()
Definition: Action.php:247
Title
Represents a title within MediaWiki.
Definition: Title.php:35
Action\$page
WikiPage Article ImagePage CategoryPage Page $page
Page on which we're performing the action $page.
Definition: Action.php:42
Action\getPageTitle
getPageTitle()
Returns the name that goes in the <h1> page title.
Definition: Action.php:351
Action\getOutput
getOutput()
Get the OutputPage being used for this instance.
Definition: Action.php:191
WatchAction\preText
preText()
Add pre- or post-text to the form.
Definition: WatchAction.php:212
WatchAction\getWatchToken
static getWatchToken(Title $title, User $user, $action='watch')
Get token to watch (or unwatch) a page for a user.
Definition: WatchAction.php:184
WatchAction\doWatch
static doWatch(Title $title, User $user, $checkRights=WatchedItem::CHECK_USER_RIGHTS)
Watch a page.
Definition: WatchAction.php:130
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:59
WatchAction\getUnwatchToken
static getUnwatchToken(Title $title, User $user, $action='unwatch')
Get token to unwatch (or watch) a page for a user.
Definition: WatchAction.php:204
Status\newFatal
static newFatal( $message)
Factory function for fatal errors.
Definition: Status.php:63
HTMLForm
Object handling generic submission, CSRF protection, layout and other logic for UI forms.
Definition: HTMLForm.php:100