Merge "Update kss style guide wording and README"
[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 private $mPageSet = null;
34
35 public function execute() {
36 $user = $this->getUser();
37 if ( !$user->isLoggedIn() ) {
38 $this->dieUsage( 'You must be logged-in to have a watchlist', 'notloggedin' );
39 }
40
41 if ( !$user->isAllowed( 'editmywatchlist' ) ) {
42 $this->dieUsage( 'You don\'t have permission to edit your watchlist', 'permissiondenied' );
43 }
44
45 $params = $this->extractRequestParams();
46 $pageSet = $this->getPageSet();
47 // by default we use pageset to extract the page to work on.
48 // title is still supported for backward compatibility
49 if ( !isset( $params['title'] ) ) {
50 $pageSet->execute();
51 $res = $pageSet->getInvalidTitlesAndRevisions( array(
52 'invalidTitles',
53 'special',
54 'missingIds',
55 'missingRevIds',
56 'interwikiTitles'
57 ) );
58
59 foreach ( $pageSet->getMissingTitles() as $title ) {
60 $r = $this->watchTitle( $title, $user, $params );
61 $r['missing'] = 1;
62 $res[] = $r;
63 }
64
65 foreach ( $pageSet->getGoodTitles() as $title ) {
66 $r = $this->watchTitle( $title, $user, $params );
67 $res[] = $r;
68 }
69 $this->getResult()->setIndexedTagName( $res, 'w' );
70 } else {
71 // dont allow use of old title parameter with new pageset parameters.
72 $extraParams = array_keys( array_filter( $pageSet->extractRequestParams(), function ( $x ) {
73 return $x !== null && $x !== false;
74 } ) );
75
76 if ( $extraParams ) {
77 $p = $this->getModulePrefix();
78 $this->dieUsage(
79 "The parameter {$p}title can not be used with " . implode( ", ", $extraParams ),
80 'invalidparammix'
81 );
82 }
83
84 $title = Title::newFromText( $params['title'] );
85 if ( !$title || !$title->isWatchable() ) {
86 $this->dieUsageMsg( array( 'invalidtitle', $params['title'] ) );
87 }
88 $res = $this->watchTitle( $title, $user, $params, true );
89 }
90 $this->getResult()->addValue( null, $this->getModuleName(), $res );
91 }
92
93 private function watchTitle( Title $title, User $user, array $params,
94 $compatibilityMode = false
95 ) {
96 if ( !$title->isWatchable() ) {
97 return array( 'title' => $title->getPrefixedText(), 'watchable' => 0 );
98 }
99
100 $res = array( 'title' => $title->getPrefixedText() );
101
102 // Currently unnecessary, code to act as a safeguard against any change
103 // in current behavior of uselang.
104 // Copy from ApiParse
105 $oldLang = null;
106 if ( isset( $params['uselang'] ) &&
107 $params['uselang'] != $this->getContext()->getLanguage()->getCode()
108 ) {
109 $oldLang = $this->getContext()->getLanguage(); // Backup language
110 $this->getContext()->setLanguage( Language::factory( $params['uselang'] ) );
111 }
112
113 if ( $params['unwatch'] ) {
114 $status = UnwatchAction::doUnwatch( $title, $user );
115 if ( $status->isOK() ) {
116 $res['unwatched'] = '';
117 $res['message'] = $this->msg( 'removedwatchtext', $title->getPrefixedText() )
118 ->title( $title )->parseAsBlock();
119 }
120 } else {
121 $status = WatchAction::doWatch( $title, $user );
122 if ( $status->isOK() ) {
123 $res['watched'] = '';
124 $res['message'] = $this->msg( 'addedwatchtext', $title->getPrefixedText() )
125 ->title( $title )->parseAsBlock();
126 }
127 }
128
129 if ( !is_null( $oldLang ) ) {
130 $this->getContext()->setLanguage( $oldLang ); // Reset language to $oldLang
131 }
132
133 if ( !$status->isOK() ) {
134 if ( $compatibilityMode ) {
135 $this->dieStatus( $status );
136 }
137 $res['error'] = $this->getErrorFromStatus( $status );
138 }
139
140 return $res;
141 }
142
143 /**
144 * Get a cached instance of an ApiPageSet object
145 * @return ApiPageSet
146 */
147 private function getPageSet() {
148 if ( $this->mPageSet === null ) {
149 $this->mPageSet = new ApiPageSet( $this );
150 }
151
152 return $this->mPageSet;
153 }
154
155 public function mustBePosted() {
156 return true;
157 }
158
159 public function isWriteMode() {
160 return true;
161 }
162
163 public function needsToken() {
164 return true;
165 }
166
167 public function getTokenSalt() {
168 return 'watch';
169 }
170
171 public function getAllowedParams( $flags = 0 ) {
172 $result = array(
173 'title' => array(
174 ApiBase::PARAM_TYPE => 'string',
175 ApiBase::PARAM_DEPRECATED => true
176 ),
177 'unwatch' => false,
178 'uselang' => null,
179 'token' => array(
180 ApiBase::PARAM_TYPE => 'string',
181 ApiBase::PARAM_REQUIRED => true
182 ),
183 );
184 if ( $flags ) {
185 $result += $this->getPageSet()->getFinalParams( $flags );
186 }
187
188 return $result;
189 }
190
191 public function getParamDescription() {
192 $psModule = $this->getPageSet();
193
194 return $psModule->getParamDescription() + array(
195 'title' => 'The page to (un)watch. use titles instead',
196 'unwatch' => 'If set the page will be unwatched rather than watched',
197 'uselang' => 'Language to show the message in',
198 'token' => 'A token previously acquired via prop=info',
199 );
200 }
201
202 public function getResultProperties() {
203 return array(
204 '' => array(
205 'title' => 'string',
206 'unwatched' => 'boolean',
207 'watched' => 'boolean',
208 'message' => 'string'
209 )
210 );
211 }
212
213 public function getDescription() {
214 return 'Add or remove pages from/to the current user\'s watchlist.';
215 }
216
217 public function getPossibleErrors() {
218 return array_merge( parent::getPossibleErrors(), array(
219 array( 'code' => 'notloggedin', 'info' => 'You must be logged-in to have a watchlist' ),
220 array( 'invalidtitle', 'title' ),
221 array( 'hookaborted' ),
222 ) );
223 }
224
225 public function getExamples() {
226 return array(
227 'api.php?action=watch&titles=Main_Page' => 'Watch the page "Main Page"',
228 'api.php?action=watch&titles=Main_Page&unwatch=' => 'Unwatch the page "Main Page"',
229 );
230 }
231
232 public function getHelpUrls() {
233 return 'https://www.mediawiki.org/wiki/API:Watch';
234 }
235 }