put in r110285 again now that 1.19 branched
[lhc/web/wiklou.git] / includes / api / ApiWatch.php
1 <?php
2 /**
3 *
4 *
5 * Created on Jan 4, 2008
6 *
7 * Copyright © 2008 Yuri Astrakhan <Firstname><Lastname>@gmail.com,
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * http://www.gnu.org/copyleft/gpl.html
23 *
24 * @file
25 */
26
27 /**
28 * API module to allow users to watch a page
29 *
30 * @ingroup API
31 */
32 class ApiWatch extends ApiBase {
33
34 public function __construct( $main, $action ) {
35 parent::__construct( $main, $action );
36 }
37
38 public function execute() {
39 $user = $this->getUser();
40 if ( !$user->isLoggedIn() ) {
41 $this->dieUsage( 'You must be logged-in to have a watchlist', 'notloggedin' );
42 }
43
44 $params = $this->extractRequestParams();
45 $title = Title::newFromText( $params['title'] );
46
47 if ( !$title || $title->getNamespace() < 0 ) {
48 $this->dieUsageMsg( array( 'invalidtitle', $params['title'] ) );
49 }
50
51 $res = array( 'title' => $title->getPrefixedText() );
52
53 if ( $params['unwatch'] ) {
54 $res['unwatched'] = '';
55 $res['message'] = wfMsgExt( 'removedwatchtext', array( 'parse' ), $title->getPrefixedText() );
56 $success = UnwatchAction::doUnwatch( $title, $user );
57 } else {
58 $res['watched'] = '';
59 $res['message'] = wfMsgExt( 'addedwatchtext', array( 'parse' ), $title->getPrefixedText() );
60 $success = WatchAction::doWatch( $title, $user );
61 }
62 if ( !$success ) {
63 $this->dieUsageMsg( 'hookaborted' );
64 }
65 $this->getResult()->addValue( null, $this->getModuleName(), $res );
66 }
67
68 public function mustBePosted() {
69 return true;
70 }
71
72 public function isWriteMode() {
73 return true;
74 }
75
76 public function needsToken() {
77 return true;
78 }
79
80 public function getTokenSalt() {
81 return 'watch';
82 }
83
84 public function getAllowedParams() {
85 return array(
86 'title' => array(
87 ApiBase::PARAM_TYPE => 'string',
88 ApiBase::PARAM_REQUIRED => true
89 ),
90 'unwatch' => false,
91 'token' => null,
92 );
93 }
94
95 public function getParamDescription() {
96 return array(
97 'title' => 'The page to (un)watch',
98 'unwatch' => 'If set the page will be unwatched rather than watched',
99 'token' => 'A token previously acquired via prop=info',
100 );
101 }
102
103 public function getDescription() {
104 return 'Add or remove a page from/to the current user\'s watchlist';
105 }
106
107 public function getPossibleErrors() {
108 return array_merge( parent::getPossibleErrors(), array(
109 array( 'code' => 'notloggedin', 'info' => 'You must be logged-in to have a watchlist' ),
110 array( 'invalidtitle', 'title' ),
111 array( 'hookaborted' ),
112 ) );
113 }
114
115 public function getExamples() {
116 return array(
117 'api.php?action=watch&title=Main_Page' => 'Watch the page "Main Page"',
118 'api.php?action=watch&title=Main_Page&unwatch=' => 'Unwatch the page "Main Page"',
119 );
120 }
121
122 public function getHelpUrls() {
123 return 'https://www.mediawiki.org/wiki/API:Watch';
124 }
125
126 public function getVersion() {
127 return __CLASS__ . ': $Id$';
128 }
129 }