Merge "Follow-up Idae8d920 (8c65834): no need to call getContext() and escape the...
[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'] = $this->msg( 'removedwatchtext', $title->getPrefixedText() )->title( $title )->parseAsBlock();
56 $success = UnwatchAction::doUnwatch( $title, $user );
57 } else {
58 $res['watched'] = '';
59 $res['message'] = $this->msg( 'addedwatchtext', $title->getPrefixedText() )->title( $title )->parseAsBlock();
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 getResultProperties() {
104 return array(
105 '' => array(
106 'title' => 'string',
107 'unwatched' => 'boolean',
108 'watched' => 'boolean',
109 'message' => 'string'
110 )
111 );
112 }
113
114 public function getDescription() {
115 return 'Add or remove a page from/to the current user\'s watchlist';
116 }
117
118 public function getPossibleErrors() {
119 return array_merge( parent::getPossibleErrors(), array(
120 array( 'code' => 'notloggedin', 'info' => 'You must be logged-in to have a watchlist' ),
121 array( 'invalidtitle', 'title' ),
122 array( 'hookaborted' ),
123 ) );
124 }
125
126 public function getExamples() {
127 return array(
128 'api.php?action=watch&title=Main_Page' => 'Watch the page "Main Page"',
129 'api.php?action=watch&title=Main_Page&unwatch=' => 'Unwatch the page "Main Page"',
130 );
131 }
132
133 public function getHelpUrls() {
134 return 'https://www.mediawiki.org/wiki/API:Watch';
135 }
136
137 public function getVersion() {
138 return __CLASS__ . ': $Id$';
139 }
140 }