275275e21fdfc9e74f29cfa06a37139c116dd75a
[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 // Currently unnecessary, code to act as a safeguard against any change in current behaviour of uselang
54 // Copy from ApiParse
55 $oldLang = null;
56 if ( isset( $params['uselang'] ) && $params['uselang'] != $this->getContext()->getLanguage()->getCode() ) {
57 $oldLang = $this->getContext()->getLanguage(); // Backup language
58 $this->getContext()->setLanguage( Language::factory( $params['uselang'] ) );
59 }
60
61 if ( $params['unwatch'] ) {
62 $res['unwatched'] = '';
63 $res['message'] = $this->msg( 'removedwatchtext', $title->getPrefixedText() )->title( $title )->parseAsBlock();
64 $success = UnwatchAction::doUnwatch( $title, $user );
65 } else {
66 $res['watched'] = '';
67 $res['message'] = $this->msg( 'addedwatchtext', $title->getPrefixedText() )->title( $title )->parseAsBlock();
68 $success = WatchAction::doWatch( $title, $user );
69 }
70
71 if ( !is_null( $oldLang ) ) {
72 $this->getContext()->setLanguage( $oldLang ); // Reset language to $oldLang
73 }
74
75 if ( !$success ) {
76 $this->dieUsageMsg( 'hookaborted' );
77 }
78 $this->getResult()->addValue( null, $this->getModuleName(), $res );
79 }
80
81 public function mustBePosted() {
82 return true;
83 }
84
85 public function isWriteMode() {
86 return true;
87 }
88
89 public function needsToken() {
90 return true;
91 }
92
93 public function getTokenSalt() {
94 return 'watch';
95 }
96
97 public function getAllowedParams() {
98 return array(
99 'title' => array(
100 ApiBase::PARAM_TYPE => 'string',
101 ApiBase::PARAM_REQUIRED => true
102 ),
103 'unwatch' => false,
104 'uselang' => null,
105 'token' => array(
106 ApiBase::PARAM_TYPE => 'string',
107 ApiBase::PARAM_REQUIRED => true
108 ),
109 );
110 }
111
112 public function getParamDescription() {
113 return array(
114 'title' => 'The page to (un)watch',
115 'unwatch' => 'If set the page will be unwatched rather than watched',
116 'uselang' => 'Language to show the message in',
117 'token' => 'A token previously acquired via prop=info',
118 );
119 }
120
121 public function getResultProperties() {
122 return array(
123 '' => array(
124 'title' => 'string',
125 'unwatched' => 'boolean',
126 'watched' => 'boolean',
127 'message' => 'string'
128 )
129 );
130 }
131
132 public function getDescription() {
133 return 'Add or remove a page from/to the current user\'s watchlist';
134 }
135
136 public function getPossibleErrors() {
137 return array_merge( parent::getPossibleErrors(), array(
138 array( 'code' => 'notloggedin', 'info' => 'You must be logged-in to have a watchlist' ),
139 array( 'invalidtitle', 'title' ),
140 array( 'hookaborted' ),
141 ) );
142 }
143
144 public function getExamples() {
145 return array(
146 'api.php?action=watch&title=Main_Page' => 'Watch the page "Main Page"',
147 'api.php?action=watch&title=Main_Page&unwatch=' => 'Unwatch the page "Main Page"',
148 );
149 }
150
151 public function getHelpUrls() {
152 return 'https://www.mediawiki.org/wiki/API:Watch';
153 }
154
155 public function getVersion() {
156 return __CLASS__ . ': $Id$';
157 }
158 }