3 * Performs the watch and unwatch 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
23 class WatchAction
extends FormlessAction
{
25 public function getName() {
29 public function getRestriction() {
33 public function requiresUnblock() {
37 protected function getDescription() {
38 return wfMsg( 'addedwatch' );
41 protected function checkCanExecute( User
$user ) {
44 if ( $user->isAnon() ) {
45 throw new ErrorPageError( 'watchnologin', 'watchnologintext' );
48 // Must have valid token for this action/title
49 $salt = array( $this->getName(), $this->getTitle()->getDBkey() );
51 if ( !$user->matchEditToken( $this->getRequest()->getVal( 'token' ), $salt ) ) {
52 throw new ErrorPageError( 'sessionfailure-title', 'sessionfailure' );
55 return parent
::checkCanExecute( $user );
58 public function onView() {
59 wfProfileIn( __METHOD__
);
61 $user = $this->getUser();
62 self
::doWatch( $this->getTitle(), $user );
64 wfProfileOut( __METHOD__
);
66 return wfMessage( 'addedwatchtext', $this->getTitle()->getPrefixedText() )->parse();
69 public static function doWatch( Title
$title, User
$user ) {
70 $page = new Article( $title );
72 if ( wfRunHooks( 'WatchArticle', array( &$user, &$page ) ) ) {
73 $user->addWatch( $title );
74 wfRunHooks( 'WatchArticleComplete', array( &$user, &$page ) );
79 public static function doUnwatch( Title
$title, User
$user ) {
80 $page = new Article( $title );
82 if ( wfRunHooks( 'UnwatchArticle', array( &$user, &$page ) ) ) {
83 $user->removeWatch( $title );
84 wfRunHooks( 'UnwatchArticleComplete', array( &$user, &$page ) );
90 * Get token to watch (or unwatch) a page for a user
92 * @param Title $title Title object of page to watch
93 * @param User $title User for whom the action is going to be performed
94 * @param string $action Optionally override the action to 'unwatch'
95 * @return string Token
98 public static function getWatchToken( Title
$title, User
$user, $action = 'watch' ) {
99 if ( $action != 'unwatch' ) {
102 $salt = array( $action, $title->getDBkey() );
104 // This token stronger salted and not compatible with ApiWatch
105 // It's title/action specific because index.php is GET and API is POST
106 return $user->editToken( $salt );
110 * Get token to unwatch (or watch) a page for a user
112 * @param Title $title Title object of page to unwatch
113 * @param User $title User for whom the action is going to be performed
114 * @param string $action Optionally override the action to 'watch'
115 * @return string Token
118 public static function getUnwatchToken( Title
$title, User
$user, $action = 'unwatch' ) {
119 return self
::getWatchToken( $title, $user, $action );
124 class UnwatchAction
extends WatchAction
{
126 public function getName() {
130 protected function getDescription() {
131 return wfMsg( 'removedwatch' );
134 public function onView() {
135 wfProfileIn( __METHOD__
);
137 $user = $this->getUser();
138 self
::doUnwatch( $this->getTitle(), $user );
140 wfProfileOut( __METHOD__
);
142 return wfMessage( 'removedwatchtext', $this->getTitle()->getPrefixedText() )->parse();