Merge "Improve comments on fields and fix opening_text - needs no highlights."
[mediawiki.git] / includes / actions / WatchAction.php
blob890740fdd5ae19c608dc2e0a3dc2b249d5ea4ae9
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 $this->getOutput()->addWikiMsg( 'addedwatchtext', $this->getTitle()->getPrefixedText() );
73 /* Static utility methods */
75 /**
76 * Watch or unwatch a page
77 * @since 1.22
78 * @param bool $watch Whether to watch or unwatch the page
79 * @param Title $title Page to watch/unwatch
80 * @param User $user User who is watching/unwatching
81 * @return Status
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 );
96 return Status::newGood();
99 /**
100 * Watch a page
101 * @since 1.22 Returns Status, $checkRights parameter added
102 * @param Title $title Page to watch/unwatch
103 * @param User $user User who is watching/unwatching
104 * @param bool $checkRights Passed through to $user->addWatch()
105 * Pass User::CHECK_USER_RIGHTS or User::IGNORE_USER_RIGHTS.
106 * @return Status
108 public static function doWatch(
109 Title $title,
110 User $user,
111 $checkRights = User::CHECK_USER_RIGHTS
113 if ( $checkRights && !$user->isAllowed( 'editmywatchlist' ) ) {
114 return User::newFatalPermissionDeniedStatus( 'editmywatchlist' );
117 $page = WikiPage::factory( $title );
119 $status = Status::newFatal( 'hookaborted' );
120 if ( Hooks::run( 'WatchArticle', [ &$user, &$page, &$status ] ) ) {
121 $status = Status::newGood();
122 $user->addWatch( $title, $checkRights );
123 Hooks::run( 'WatchArticleComplete', [ &$user, &$page ] );
126 return $status;
130 * Unwatch a page
131 * @since 1.22 Returns Status
132 * @param Title $title Page to watch/unwatch
133 * @param User $user User who is watching/unwatching
134 * @return Status
136 public static function doUnwatch( Title $title, User $user ) {
137 if ( !$user->isAllowed( 'editmywatchlist' ) ) {
138 return User::newFatalPermissionDeniedStatus( 'editmywatchlist' );
141 $page = WikiPage::factory( $title );
143 $status = Status::newFatal( 'hookaborted' );
144 if ( Hooks::run( 'UnwatchArticle', [ &$user, &$page, &$status ] ) ) {
145 $status = Status::newGood();
146 $user->removeWatch( $title );
147 Hooks::run( 'UnwatchArticleComplete', [ &$user, &$page ] );
150 return $status;
154 * Get token to watch (or unwatch) a page for a user
156 * @param Title $title Title object of page to watch
157 * @param User $user User for whom the action is going to be performed
158 * @param string $action Optionally override the action to 'unwatch'
159 * @return string Token
160 * @since 1.18
162 public static function getWatchToken( Title $title, User $user, $action = 'watch' ) {
163 if ( $action != 'unwatch' ) {
164 $action = 'watch';
166 // Match ApiWatch and ResourceLoaderUserTokensModule
167 return $user->getEditToken( $action );
171 * Get token to unwatch (or watch) a page for a user
173 * @param Title $title Title object of page to unwatch
174 * @param User $user User for whom the action is going to be performed
175 * @param string $action Optionally override the action to 'watch'
176 * @return string Token
177 * @since 1.18
179 public static function getUnwatchToken( Title $title, User $user, $action = 'unwatch' ) {
180 return self::getWatchToken( $title, $user, $action );
183 public function doesWrites() {
184 return true;