Merge "Restored method for clearing a watchlist in web UI, and rebuilt code."
[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 if ( $params['entirewatchlist'] && $pageSet->getDataSource() !== null ) {
51 $this->dieUsage(
52 "Cannot use 'entirewatchlist' at the same time as '{$pageSet->getDataSource()}'",
53 'multisource'
54 );
55 }
56
57 if ( $params['entirewatchlist'] ) {
58 if ( !$params['unwatch'] ) {
59 $this->dieUsage(
60 "'entirewatchlist' option can only be used with 'unwatch' parameter.",
61 'invalidparammix'
62 );
63 } else {
64 // We're going to do this in the database as a bulk operation
65 // instead of one at a time, so it doesn't time out on largeish lists.
66 $dbw = wfGetDB( DB_MASTER );
67 $dbw->delete( 'watchlist', array( 'wl_user' => $user->getId() ), __METHOD__ );
68 $user->invalidateCache();
69 $res = array();
70 }
71 } else {
72 $pageSet->execute();
73 $res = $pageSet->getInvalidTitlesAndRevisions( array(
74 'invalidTitles',
75 'special',
76 'missingIds',
77 'missingRevIds',
78 'interwikiTitles'
79 ) );
80
81 foreach ( $pageSet->getMissingTitles() as $title ) {
82 $r = $this->watchTitle( $title, $user, $params );
83 $r['missing'] = 1;
84 $res[] = $r;
85 }
86
87 foreach ( $pageSet->getGoodTitles() as $title ) {
88 $r = $this->watchTitle( $title, $user, $params );
89 $res[] = $r;
90 }
91 $this->getResult()->setIndexedTagName( $res, 'w' );
92 }
93 } else {
94 // dont allow use of old title parameter with new pageset parameters.
95 $extraParams = array_keys( array_filter( $pageSet->extractRequestParams(), function ( $x ) {
96 return $x !== null && $x !== false;
97 } ) );
98
99 if ( $extraParams ) {
100 $p = $this->getModulePrefix();
101 $this->dieUsage(
102 "The parameter {$p}title can not be used with " . implode( ", ", $extraParams ),
103 'invalidparammix'
104 );
105 }
106
107 $title = Title::newFromText( $params['title'] );
108 if ( !$title || !$title->isWatchable() ) {
109 $this->dieUsageMsg( array( 'invalidtitle', $params['title'] ) );
110 }
111 $res = $this->watchTitle( $title, $user, $params, true );
112 }
113 $this->getResult()->addValue( null, $this->getModuleName(), $res );
114 }
115
116 private function watchTitle( Title $title, User $user, array $params,
117 $compatibilityMode = false
118 ) {
119 if ( !$title->isWatchable() ) {
120 return array( 'title' => $title->getPrefixedText(), 'watchable' => 0 );
121 }
122
123 $res = array( 'title' => $title->getPrefixedText() );
124
125 // Currently unnecessary, code to act as a safeguard against any change
126 // in current behavior of uselang.
127 // Copy from ApiParse
128 $oldLang = null;
129 if ( isset( $params['uselang'] ) &&
130 $params['uselang'] != $this->getContext()->getLanguage()->getCode()
131 ) {
132 $oldLang = $this->getContext()->getLanguage(); // Backup language
133 $this->getContext()->setLanguage( Language::factory( $params['uselang'] ) );
134 }
135
136 if ( $params['unwatch'] ) {
137 $status = UnwatchAction::doUnwatch( $title, $user );
138 if ( $status->isOK() ) {
139 $res['unwatched'] = '';
140 $res['message'] = $this->msg( 'removedwatchtext', $title->getPrefixedText() )
141 ->title( $title )->parseAsBlock();
142 }
143 } else {
144 $status = WatchAction::doWatch( $title, $user );
145 if ( $status->isOK() ) {
146 $res['watched'] = '';
147 $res['message'] = $this->msg( 'addedwatchtext', $title->getPrefixedText() )
148 ->title( $title )->parseAsBlock();
149 }
150 }
151
152 if ( !is_null( $oldLang ) ) {
153 $this->getContext()->setLanguage( $oldLang ); // Reset language to $oldLang
154 }
155
156 if ( !$status->isOK() ) {
157 if ( $compatibilityMode ) {
158 $this->dieStatus( $status );
159 }
160 $res['error'] = $this->getErrorFromStatus( $status );
161 }
162
163 return $res;
164 }
165
166 /**
167 * Get a cached instance of an ApiPageSet object
168 * @return ApiPageSet
169 */
170 private function getPageSet() {
171 if ( $this->mPageSet === null ) {
172 $this->mPageSet = new ApiPageSet( $this );
173 }
174
175 return $this->mPageSet;
176 }
177
178 public function mustBePosted() {
179 return true;
180 }
181
182 public function isWriteMode() {
183 return true;
184 }
185
186 public function needsToken() {
187 return true;
188 }
189
190 public function getTokenSalt() {
191 return 'watch';
192 }
193
194 public function getAllowedParams( $flags = 0 ) {
195 $result = array(
196 'title' => array(
197 ApiBase::PARAM_TYPE => 'string',
198 ApiBase::PARAM_DEPRECATED => true
199 ),
200 'unwatch' => false,
201 'entirewatchlist' => false,
202 'uselang' => null,
203 'token' => array(
204 ApiBase::PARAM_TYPE => 'string',
205 ApiBase::PARAM_REQUIRED => true
206 ),
207 );
208 if ( $flags ) {
209 $result += $this->getPageSet()->getFinalParams( $flags );
210 }
211
212 return $result;
213 }
214
215 public function getParamDescription() {
216 $psModule = $this->getPageSet();
217
218 return $psModule->getParamDescription() + array(
219 'title' => 'The page to (un)watch. use titles instead',
220 'unwatch' => 'If set the page will be unwatched rather than watched',
221 'entirewatchlist' => 'Work on all watched pages without returning a list; can only be used together with \'unwatch\' option.',
222 'uselang' => 'Language to show the message in',
223 'token' => 'A token previously acquired via prop=info',
224 );
225 }
226
227 public function getResultProperties() {
228 return array(
229 '' => array(
230 'title' => 'string',
231 'unwatched' => 'boolean',
232 'watched' => 'boolean',
233 'message' => 'string'
234 )
235 );
236 }
237
238 public function getDescription() {
239 return 'Add or remove pages from/to the current user\'s watchlist.';
240 }
241
242 public function getPossibleErrors() {
243 return array_merge( parent::getPossibleErrors(), array(
244 array( 'code' => 'notloggedin', 'info' => 'You must be logged-in to have a watchlist' ),
245 array( 'invalidtitle', 'title' ),
246 array( 'hookaborted' ),
247 ) );
248 }
249
250 public function getExamples() {
251 return array(
252 'api.php?action=watch&titles=Main_Page' => 'Watch the page "Main Page"',
253 'api.php?action=watch&titles=Main_Page&unwatch=' => 'Unwatch the page "Main Page"',
254 );
255 }
256
257 public function getHelpUrls() {
258 return 'https://www.mediawiki.org/wiki/API:Watch';
259 }
260 }