Minor doc fix to r84741
[lhc/web/wiklou.git] / includes / actions / WatchAction.php
1 <?php
2 /**
3 * Performs the watch and unwatch actions on a page
4 *
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.
9 *
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.
14 *
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
18 *
19 * @file
20 * @ingroup Actions
21 */
22
23 class WatchAction extends FormlessAction {
24
25 public function getName() {
26 return 'watch';
27 }
28
29 public function getRestriction() {
30 return 'read';
31 }
32
33 public function requiresUnblock() {
34 return false;
35 }
36
37 protected function getDescription() {
38 return wfMsg( 'addedwatch' );
39 }
40
41 protected function checkCanExecute( User $user ) {
42
43 // Must be logged in
44 if ( $user->isAnon() ) {
45 throw new ErrorPageError( 'watchnologin', 'watchnologintext' );
46 }
47
48 // Must have valid token for this action/title
49 $salt = array( $this->getName(), $this->getTitle()->getDBkey() );
50
51 if ( !$user->matchEditToken( $this->getRequest()->getVal( 'token' ), $salt ) ) {
52 throw new ErrorPageError( 'sessionfailure-title', 'sessionfailure' );
53 }
54
55 return parent::checkCanExecute( $user );
56 }
57
58 public function onView() {
59 wfProfileIn( __METHOD__ );
60
61 $user = $this->getUser();
62 self::doWatch( $this->getTitle(), $user );
63
64 wfProfileOut( __METHOD__ );
65
66 return wfMessage( 'addedwatchtext', $this->getTitle()->getPrefixedText() )->parse();
67 }
68
69 public static function doWatch( Title $title, User $user ) {
70 $page = new Article( $title );
71
72 if ( wfRunHooks( 'WatchArticle', array( &$user, &$page ) ) ) {
73 $user->addWatch( $title );
74 wfRunHooks( 'WatchArticleComplete', array( &$user, &$page ) );
75 }
76 return true;
77 }
78
79 public static function doUnwatch( Title $title, User $user ) {
80 $page = new Article( $title );
81
82 if ( wfRunHooks( 'UnwatchArticle', array( &$user, &$page ) ) ) {
83 $user->removeWatch( $title );
84 wfRunHooks( 'UnwatchArticleComplete', array( &$user, &$page ) );
85 }
86 return true;
87 }
88
89 /**
90 * Get token to watch (or unwatch) a page for a user
91 *
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
96 * @since 1.19
97 */
98 public static function getWatchToken( Title $title, User $user, $action = 'watch' ) {
99 if ( $action != 'unwatch' ) {
100 $action = 'watch';
101 }
102 $salt = array( $action, $title->getDBkey() );
103
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 );
107 }
108
109 /**
110 * Get token to unwatch (or watch) a page for a user
111 *
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
116 * @since 1.19
117 */
118 public static function getUnwatchToken( Title $title, User $user, $action = 'unwatch' ) {
119 return self::getWatchToken( $title, $user, $action );
120 }
121
122 }
123
124 class UnwatchAction extends WatchAction {
125
126 public function getName() {
127 return 'unwatch';
128 }
129
130 protected function getDescription() {
131 return wfMsg( 'removedwatch' );
132 }
133
134 public function onView() {
135 wfProfileIn( __METHOD__ );
136
137 $user = $this->getUser();
138 self::doUnwatch( $this->getTitle(), $user );
139
140 wfProfileOut( __METHOD__ );
141
142 return wfMessage( 'removedwatchtext', $this->getTitle()->getPrefixedText() )->parse();
143 }
144 }