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