MediaWiki 1.40.4
ApiWatch.php
Go to the documentation of this file.
1<?php
28
34class ApiWatch extends ApiBase {
35 private $mPageSet = null;
36
38 private $expiryEnabled;
39
41 private $maxDuration;
42
45
46 public function __construct( ApiMain $mainModule, $moduleName, WatchlistManager $watchlistManager ) {
47 parent::__construct( $mainModule, $moduleName );
48
49 $this->watchlistManager = $watchlistManager;
50 $this->expiryEnabled = $this->getConfig()->get( MainConfigNames::WatchlistExpiry );
51 $this->maxDuration = $this->getConfig()->get( MainConfigNames::WatchlistExpiryMaxDuration );
52 }
53
54 public function execute() {
55 $user = $this->getUser();
56 if ( !$user->isRegistered() ) {
57 $this->dieWithError( 'watchlistanontext', 'notloggedin' );
58 }
59
60 $this->checkUserRightsAny( 'editmywatchlist' );
61
62 $params = $this->extractRequestParams();
63
64 $continuationManager = new ApiContinuationManager( $this, [], [] );
65 $this->setContinuationManager( $continuationManager );
66
67 $pageSet = $this->getPageSet();
68 // by default we use pageset to extract the page to work on.
69 // title is still supported for backward compatibility
70 if ( !isset( $params['title'] ) ) {
71 $pageSet->execute();
72 $res = $pageSet->getInvalidTitlesAndRevisions( [
73 'invalidTitles',
74 'special',
75 'missingIds',
76 'missingRevIds',
77 'interwikiTitles'
78 ] );
79
80 foreach ( $pageSet->getMissingTitles() as $title ) {
81 $r = $this->watchTitle( $title, $user, $params );
82 $r['missing'] = true;
83 $res[] = $r;
84 }
85
86 foreach ( $pageSet->getGoodTitles() as $title ) {
87 $r = $this->watchTitle( $title, $user, $params );
88 $res[] = $r;
89 }
90 ApiResult::setIndexedTagName( $res, 'w' );
91 } else {
92 // dont allow use of old title parameter with new pageset parameters.
93 $extraParams = array_keys( array_filter( $pageSet->extractRequestParams(), static function ( $x ) {
94 return $x !== null && $x !== false;
95 } ) );
96
97 if ( $extraParams ) {
98 $this->dieWithError(
99 [
100 'apierror-invalidparammix-cannotusewith',
101 $this->encodeParamName( 'title' ),
102 $pageSet->encodeParamName( $extraParams[0] )
103 ],
104 'invalidparammix'
105 );
106 }
107
108 $title = Title::newFromText( $params['title'] );
109 if ( !$title || !$this->watchlistManager->isWatchable( $title ) ) {
110 $this->dieWithError( [ 'invalidtitle', $params['title'] ] );
111 }
112 $res = $this->watchTitle( $title, $user, $params, true );
113 }
114 $this->getResult()->addValue( null, $this->getModuleName(), $res );
115
116 $this->setContinuationManager( null );
117 $continuationManager->setContinuationIntoResult( $this->getResult() );
118 }
119
120 private function watchTitle( Title $title, User $user, array $params,
121 $compatibilityMode = false
122 ) {
123 $res = [ 'title' => $title->getPrefixedText(), 'ns' => $title->getNamespace() ];
124
125 if ( !$this->watchlistManager->isWatchable( $title ) ) {
126 $res['watchable'] = 0;
127 return $res;
128 }
129
130 if ( $params['unwatch'] ) {
131 $status = $this->watchlistManager->removeWatch( $user, $title );
132 $res['unwatched'] = $status->isOK();
133 } else {
134 $expiry = null;
135
136 // NOTE: If an expiry parameter isn't given, any existing expiries remain unchanged.
137 if ( $this->expiryEnabled && isset( $params['expiry'] ) ) {
138 $expiry = $params['expiry'];
139 $res['expiry'] = ApiResult::formatExpiry( $expiry );
140 }
141
142 $status = $this->watchlistManager->addWatch( $user, $title, $expiry );
143 $res['watched'] = $status->isOK();
144 }
145
146 if ( !$status->isOK() ) {
147 if ( $compatibilityMode ) {
148 $this->dieStatus( $status );
149 }
150 $res['errors'] = $this->getErrorFormatter()->arrayFromStatus( $status, 'error' );
151 $res['warnings'] = $this->getErrorFormatter()->arrayFromStatus( $status, 'warning' );
152 if ( !$res['warnings'] ) {
153 unset( $res['warnings'] );
154 }
155 }
156
157 return $res;
158 }
159
164 private function getPageSet() {
165 $this->mPageSet ??= new ApiPageSet( $this );
166
167 return $this->mPageSet;
168 }
169
170 public function mustBePosted() {
171 return true;
172 }
173
174 public function isWriteMode() {
175 return true;
176 }
177
178 public function needsToken() {
179 return 'watch';
180 }
181
182 public function getAllowedParams( $flags = 0 ) {
183 $result = [
184 'title' => [
185 ParamValidator::PARAM_TYPE => 'string',
186 ParamValidator::PARAM_DEPRECATED => true,
187 ],
188 'expiry' => [
189 ParamValidator::PARAM_TYPE => 'expiry',
190 ExpiryDef::PARAM_MAX => $this->maxDuration,
191 ExpiryDef::PARAM_USE_MAX => true,
192 ],
193 'unwatch' => false,
194 'continue' => [
195 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
196 ],
197 ];
198
199 // If expiry is not enabled, don't accept the parameter.
200 if ( !$this->expiryEnabled ) {
201 unset( $result['expiry'] );
202 }
203
204 if ( $flags ) {
205 $result += $this->getPageSet()->getFinalParams( $flags );
206 }
207
208 return $result;
209 }
210
211 protected function getExamplesMessages() {
212 $title = Title::newMainPage()->getPrefixedText();
213 $mp = rawurlencode( $title );
214
215 // Logically expiry example should go before unwatch examples.
216 $examples = [
217 "action=watch&titles={$mp}&token=123ABC"
218 => 'apihelp-watch-example-watch',
219 ];
220 if ( $this->expiryEnabled ) {
221 $examples["action=watch&titles={$mp}|Foo|Bar&expiry=1%20month&token=123ABC"]
222 = 'apihelp-watch-example-watch-expiry';
223 }
224
225 return array_merge( $examples, [
226 "action=watch&titles={$mp}&unwatch=&token=123ABC"
227 => 'apihelp-watch-example-unwatch',
228 'action=watch&generator=allpages&gapnamespace=0&token=123ABC'
229 => 'apihelp-watch-example-generator',
230 ] );
231 }
232
233 public function getHelpUrls() {
234 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Watch';
235 }
236}
This abstract class implements many basic API functions, and is the base of all API classes.
Definition ApiBase.php:59
dieWithError( $msg, $code=null, $data=null, $httpCode=0)
Abort execution with an error.
Definition ApiBase.php:1460
checkUserRightsAny( $rights, $user=null)
Helper function for permission-denied errors.
Definition ApiBase.php:1566
encodeParamName( $paramName)
This method mangles parameter name based on the prefix supplied to the constructor.
Definition ApiBase.php:751
getErrorFormatter()
Definition ApiBase.php:648
setContinuationManager(ApiContinuationManager $manager=null)
Definition ApiBase.php:681
getResult()
Get the result object.
Definition ApiBase.php:637
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:773
const PARAM_HELP_MSG
(string|array|Message) Specify an alternative i18n documentation message for this parameter.
Definition ApiBase.php:166
getModuleName()
Get the name of the module being executed by this instance.
Definition ApiBase.php:506
dieStatus(StatusValue $status)
Throw an ApiUsageException based on the Status object.
Definition ApiBase.php:1521
This manages continuation state.
This is the main API class, used for both external and internal processing.
Definition ApiMain.php:58
This class contains a list of pages that the client has requested.
static formatExpiry( $expiry, $infinity='infinity')
Format an expiry timestamp for API output.
API module to allow users to watch a page.
Definition ApiWatch.php:34
getAllowedParams( $flags=0)
Definition ApiWatch.php:182
isWriteMode()
Indicates whether this module requires write mode.
Definition ApiWatch.php:174
__construct(ApiMain $mainModule, $moduleName, WatchlistManager $watchlistManager)
Definition ApiWatch.php:46
needsToken()
Returns the token type this module requires in order to execute.
Definition ApiWatch.php:178
getHelpUrls()
Return links to more detailed help pages about the module.
Definition ApiWatch.php:233
getExamplesMessages()
Returns usage examples for this module.
Definition ApiWatch.php:211
mustBePosted()
Indicates whether this module must be called with a POST request.
Definition ApiWatch.php:170
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
Definition ApiWatch.php:54
WatchlistManager $watchlistManager
Definition ApiWatch.php:44
A class containing constants representing the names of configuration variables.
Represents a title within MediaWiki.
Definition Title.php:82
internal since 1.36
Definition User.php:71
isRegistered()
Get whether the user is registered.
Definition User.php:2312
Service for formatting and validating API parameters.
Type definition for expiry timestamps.
Definition ExpiryDef.php:17