MessageCache invalidation improvements
[mediawiki.git] / includes / actions / WatchAction.php
blob23505c020d11cd28e6458b800ce5e74954eb09d9
1 <?php
2 /**
3 * Performs the watch actions on a page
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
19 * @file
20 * @ingroup Actions
23 /**
24 * Page addition to a user's watchlist
26 * @ingroup Actions
28 class WatchAction extends FormAction {
30 public function getName() {
31 return 'watch';
34 public function requiresUnblock() {
35 return false;
38 /**
39 * @return string HTML
41 protected function getDescription() {
42 return $this->msg( 'addwatch' )->escaped();
45 public function onSubmit( $data ) {
46 self::doWatch( $this->getTitle(), $this->getUser() );
48 return true;
51 protected function checkCanExecute( User $user ) {
52 // Must be logged in
53 if ( $user->isAnon() ) {
54 throw new UserNotLoggedIn( 'watchlistanontext', 'watchnologin' );
57 parent::checkCanExecute( $user );
60 protected function alterForm( HTMLForm $form ) {
61 $form->setSubmitTextMsg( 'confirm-watch-button' );
62 $form->setTokenSalt( 'watch' );
65 protected function preText() {
66 return $this->msg( 'confirm-watch-top' )->parse();
69 public function onSuccess() {
70 $msgKey = $this->getTitle()->isTalkPage() ? 'addedwatchtext-talk' : 'addedwatchtext';
71 $this->getOutput()->addWikiMsg( $msgKey, $this->getTitle()->getPrefixedText() );
74 /* Static utility methods */
76 /**
77 * Watch or unwatch a page
78 * @since 1.22
79 * @param bool $watch Whether to watch or unwatch the page
80 * @param Title $title Page to watch/unwatch
81 * @param User $user User who is watching/unwatching
82 * @return Status
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 );
97 return Status::newGood();
101 * Watch a page
102 * @since 1.22 Returns Status, $checkRights parameter added
103 * @param Title $title Page to watch/unwatch
104 * @param User $user User who is watching/unwatching
105 * @param bool $checkRights Passed through to $user->addWatch()
106 * Pass User::CHECK_USER_RIGHTS or User::IGNORE_USER_RIGHTS.
107 * @return Status
109 public static function doWatch(
110 Title $title,
111 User $user,
112 $checkRights = User::CHECK_USER_RIGHTS
114 if ( $checkRights && !$user->isAllowed( 'editmywatchlist' ) ) {
115 return User::newFatalPermissionDeniedStatus( 'editmywatchlist' );
118 $page = WikiPage::factory( $title );
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 ] );
127 return $status;
131 * Unwatch a page
132 * @since 1.22 Returns Status
133 * @param Title $title Page to watch/unwatch
134 * @param User $user User who is watching/unwatching
135 * @return Status
137 public static function doUnwatch( Title $title, User $user ) {
138 if ( !$user->isAllowed( 'editmywatchlist' ) ) {
139 return User::newFatalPermissionDeniedStatus( 'editmywatchlist' );
142 $page = WikiPage::factory( $title );
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 ] );
151 return $status;
155 * Get token to watch (or unwatch) a page for a user
157 * @param Title $title Title object of page to watch
158 * @param User $user User for whom the action is going to be performed
159 * @param string $action Optionally override the action to 'unwatch'
160 * @return string Token
161 * @since 1.18
163 public static function getWatchToken( Title $title, User $user, $action = 'watch' ) {
164 if ( $action != 'unwatch' ) {
165 $action = 'watch';
167 // Match ApiWatch and ResourceLoaderUserTokensModule
168 return $user->getEditToken( $action );
172 * Get token to unwatch (or watch) a page for a user
174 * @param Title $title Title object of page to unwatch
175 * @param User $user User for whom the action is going to be performed
176 * @param string $action Optionally override the action to 'watch'
177 * @return string Token
178 * @since 1.18
180 public static function getUnwatchToken( Title $title, User $user, $action = 'unwatch' ) {
181 return self::getWatchToken( $title, $user, $action );
184 public function doesWrites() {
185 return true;