Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
68.54% |
122 / 178 |
|
50.00% |
6 / 12 |
CRAP | |
0.00% |
0 / 1 |
| ApiWatch | |
68.93% |
122 / 177 |
|
50.00% |
6 / 12 |
121.04 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| execute | |
62.75% |
32 / 51 |
|
0.00% |
0 / 1 |
21.74 | |||
| watchTitle | |
65.52% |
19 / 29 |
|
0.00% |
0 / 1 |
22.04 | |||
| applyLabelsToWatchedPage | |
80.00% |
24 / 30 |
|
0.00% |
0 / 1 |
8.51 | |||
| validateLabels | |
100.00% |
15 / 15 |
|
100.00% |
1 / 1 |
3 | |||
| getPageSet | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| mustBePosted | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| isWriteMode | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| needsToken | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getAllowedParams | |
92.00% |
23 / 25 |
|
0.00% |
0 / 1 |
3.00 | |||
| getExamplesMessages | |
0.00% |
0 / 17 |
|
0.00% |
0 / 1 |
6 | |||
| getHelpUrls | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Copyright © 2008 Yuri Astrakhan "<Firstname><Lastname>@gmail.com", |
| 4 | * |
| 5 | * @license GPL-2.0-or-later |
| 6 | * @file |
| 7 | */ |
| 8 | |
| 9 | namespace MediaWiki\Api; |
| 10 | |
| 11 | use MediaWiki\MainConfigNames; |
| 12 | use MediaWiki\Page\PageIdentity; |
| 13 | use MediaWiki\Page\PageReferenceValue; |
| 14 | use MediaWiki\Title\NamespaceInfo; |
| 15 | use MediaWiki\Title\Title; |
| 16 | use MediaWiki\Title\TitleFormatter; |
| 17 | use MediaWiki\User\User; |
| 18 | use MediaWiki\Watchlist\WatchedItemStoreInterface; |
| 19 | use MediaWiki\Watchlist\WatchlistLabel; |
| 20 | use MediaWiki\Watchlist\WatchlistLabelStore; |
| 21 | use MediaWiki\Watchlist\WatchlistManager; |
| 22 | use Wikimedia\ParamValidator\ParamValidator; |
| 23 | use Wikimedia\ParamValidator\TypeDef\ExpiryDef; |
| 24 | |
| 25 | /** |
| 26 | * API module to allow users to watch a page |
| 27 | * |
| 28 | * @ingroup API |
| 29 | */ |
| 30 | class ApiWatch extends ApiBase { |
| 31 | /** @var ApiPageSet|null */ |
| 32 | private $mPageSet = null; |
| 33 | |
| 34 | /** @var bool Whether watchlist expiries are enabled. */ |
| 35 | private $expiryEnabled; |
| 36 | |
| 37 | /** @var string Relative maximum expiry. */ |
| 38 | private $maxDuration; |
| 39 | |
| 40 | /** @var bool Whether watchlist labels are enabled. */ |
| 41 | private $labelsEnabled; |
| 42 | |
| 43 | public function __construct( |
| 44 | ApiMain $mainModule, |
| 45 | string $moduleName, |
| 46 | private readonly WatchlistManager $watchlistManager, |
| 47 | private readonly TitleFormatter $titleFormatter, |
| 48 | private readonly WatchlistLabelStore $watchlistLabelStore, |
| 49 | private readonly WatchedItemStoreInterface $watchedItemStore, |
| 50 | private readonly NamespaceInfo $namespaceInfo, |
| 51 | ) { |
| 52 | parent::__construct( $mainModule, $moduleName ); |
| 53 | |
| 54 | $this->expiryEnabled = $this->getConfig()->get( MainConfigNames::WatchlistExpiry ); |
| 55 | $this->maxDuration = $this->getConfig()->get( MainConfigNames::WatchlistExpiryMaxDuration ); |
| 56 | $this->labelsEnabled = $this->getConfig()->get( MainConfigNames::EnableWatchlistLabels ); |
| 57 | } |
| 58 | |
| 59 | public function execute() { |
| 60 | $user = $this->getUser(); |
| 61 | if ( !$user->isRegistered() |
| 62 | || ( $user->isTemp() && !$user->isAllowed( 'editmywatchlist' ) ) |
| 63 | ) { |
| 64 | $this->dieWithError( 'watchlistanontext', 'notloggedin' ); |
| 65 | } |
| 66 | |
| 67 | $this->checkUserRightsAny( 'editmywatchlist' ); |
| 68 | |
| 69 | $params = $this->extractRequestParams(); |
| 70 | |
| 71 | $continuationManager = new ApiContinuationManager( $this, [], [] ); |
| 72 | $this->setContinuationManager( $continuationManager ); |
| 73 | |
| 74 | // Validate labels |
| 75 | $validLabels = []; |
| 76 | $labelError = null; |
| 77 | if ( isset( $params['labels'] ) && $params['labels'] ) { |
| 78 | $validationResult = $this->validateLabels( $user, $params['labels'] ); |
| 79 | $validLabels = $validationResult['labels']; |
| 80 | $labelError = $validationResult['error']; |
| 81 | } |
| 82 | |
| 83 | $pageSet = $this->getPageSet(); |
| 84 | // by default we use pageset to extract the page to work on. |
| 85 | // title is still supported for backward compatibility |
| 86 | if ( !isset( $params['title'] ) ) { |
| 87 | $pageSet->execute(); |
| 88 | $res = $pageSet->getInvalidTitlesAndRevisions( [ |
| 89 | 'invalidTitles', |
| 90 | 'special', |
| 91 | 'missingIds', |
| 92 | 'missingRevIds', |
| 93 | 'interwikiTitles' |
| 94 | ] ); |
| 95 | |
| 96 | foreach ( $pageSet->getMissingPages() as $page ) { |
| 97 | $r = $this->watchTitle( $page, $user, $params, false, $validLabels, $labelError ); |
| 98 | $r['missing'] = true; |
| 99 | $res[] = $r; |
| 100 | } |
| 101 | |
| 102 | foreach ( $pageSet->getGoodPages() as $page ) { |
| 103 | $r = $this->watchTitle( $page, $user, $params, false, $validLabels, $labelError ); |
| 104 | $res[] = $r; |
| 105 | } |
| 106 | ApiResult::setIndexedTagName( $res, 'w' ); |
| 107 | } else { |
| 108 | // dont allow use of old title parameter with new pageset parameters. |
| 109 | $extraParams = array_keys( array_filter( $pageSet->extractRequestParams(), static function ( $x ) { |
| 110 | return $x !== null && $x !== false; |
| 111 | } ) ); |
| 112 | |
| 113 | if ( $extraParams ) { |
| 114 | $this->dieWithError( |
| 115 | [ |
| 116 | 'apierror-invalidparammix-cannotusewith', |
| 117 | $this->encodeParamName( 'title' ), |
| 118 | $pageSet->encodeParamName( $extraParams[0] ) |
| 119 | ], |
| 120 | 'invalidparammix' |
| 121 | ); |
| 122 | } |
| 123 | |
| 124 | $title = Title::newFromText( $params['title'] ); |
| 125 | if ( !$title || !$this->watchlistManager->isWatchable( $title ) ) { |
| 126 | $this->dieWithError( [ 'invalidtitle', $params['title'] ] ); |
| 127 | } |
| 128 | $res = $this->watchTitle( $title, $user, $params, true, $validLabels, $labelError ); |
| 129 | } |
| 130 | $this->getResult()->addValue( null, $this->getModuleName(), $res ); |
| 131 | |
| 132 | $this->setContinuationManager( null ); |
| 133 | $continuationManager->setContinuationIntoResult( $this->getResult() ); |
| 134 | } |
| 135 | |
| 136 | private function watchTitle( PageIdentity $page, User $user, array $params, |
| 137 | bool $compatibilityMode = false, |
| 138 | array $validLabels = [], |
| 139 | ?array $labelError = null |
| 140 | ): array { |
| 141 | $res = [ 'title' => $this->titleFormatter->getPrefixedText( $page ), 'ns' => $page->getNamespace() ]; |
| 142 | |
| 143 | if ( !$this->watchlistManager->isWatchable( $page ) ) { |
| 144 | $res['watchable'] = 0; |
| 145 | return $res; |
| 146 | } |
| 147 | |
| 148 | if ( $params['unwatch'] ) { |
| 149 | $status = $this->watchlistManager->removeWatch( $user, $page ); |
| 150 | $res['unwatched'] = $status->isOK(); |
| 151 | } else { |
| 152 | $expiry = null; |
| 153 | |
| 154 | // NOTE: If an expiry parameter isn't given, any existing expiries remain unchanged. |
| 155 | if ( $this->expiryEnabled && isset( $params['expiry'] ) ) { |
| 156 | $expiry = $params['expiry']; |
| 157 | $res['expiry'] = ApiResult::formatExpiry( $expiry ); |
| 158 | } |
| 159 | |
| 160 | $status = $this->watchlistManager->addWatch( $user, $page, $expiry ); |
| 161 | $res['watched'] = $status->isOK(); |
| 162 | |
| 163 | // Apply labels if provided and watching was successful |
| 164 | if ( $status->isOK() && $validLabels ) { |
| 165 | $this->applyLabelsToWatchedPage( $user, $page, $validLabels, $compatibilityMode, $res ); |
| 166 | // Add error to response if there were invalid labels but we applied the valid ones |
| 167 | if ( $labelError ) { |
| 168 | if ( !isset( $res['errors'] ) ) { |
| 169 | $res['errors'] = []; |
| 170 | } |
| 171 | $res['errors'][] = $labelError; |
| 172 | } |
| 173 | } elseif ( $status->isOK() && $labelError ) { |
| 174 | // Add label error to response if labels were requested but had an error |
| 175 | $res['errors'] = [ $labelError ]; |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | if ( !$status->isOK() ) { |
| 180 | if ( $compatibilityMode ) { |
| 181 | $this->dieStatus( $status ); |
| 182 | } |
| 183 | $res['errors'] = $this->getErrorFormatter()->arrayFromStatus( $status, 'error' ); |
| 184 | $res['warnings'] = $this->getErrorFormatter()->arrayFromStatus( $status, 'warning' ); |
| 185 | if ( !$res['warnings'] ) { |
| 186 | unset( $res['warnings'] ); |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | return $res; |
| 191 | } |
| 192 | |
| 193 | /** |
| 194 | * Apply watchlist labels to a newly watched page. |
| 195 | * |
| 196 | * Applies labels to both the page and its talk page, replacing any existing labels. |
| 197 | * |
| 198 | * @param User $user The user applying the labels |
| 199 | * @param PageIdentity $page The page being watched |
| 200 | * @param array $validLabels The pre-validated label objects to apply |
| 201 | * @param bool $compatibilityMode Whether to use legacy error reporting via dieWithError |
| 202 | * @param array &$res The response array to populate with label data or errors |
| 203 | */ |
| 204 | private function applyLabelsToWatchedPage( |
| 205 | User $user, |
| 206 | PageIdentity $page, |
| 207 | array $validLabels, |
| 208 | bool $compatibilityMode, |
| 209 | array &$res |
| 210 | ): void { |
| 211 | if ( !$this->labelsEnabled ) { |
| 212 | $res['errors'] = [ $this->getErrorFormatter()->formatMessage( |
| 213 | [ 'apierror-labels-disabled', 'labels-disabled' ] |
| 214 | ) ]; |
| 215 | if ( $compatibilityMode ) { |
| 216 | $this->dieWithError( 'apierror-labels-disabled', 'labels-disabled' ); |
| 217 | } |
| 218 | return; |
| 219 | } |
| 220 | |
| 221 | $title = Title::newFromPageIdentity( $page ); |
| 222 | $pagesToWatch = [ $page ]; |
| 223 | |
| 224 | // Also watch the talk page if this page can have one |
| 225 | if ( $this->namespaceInfo->canHaveTalkPage( $title ) ) { |
| 226 | $talkPageTarget = $this->namespaceInfo->getTalkPage( $title ); |
| 227 | // Convert LinkTarget to PageReferenceValue for consistency |
| 228 | $talkPage = PageReferenceValue::localReference( |
| 229 | $talkPageTarget->getNamespace(), |
| 230 | $talkPageTarget->getDBkey() |
| 231 | ); |
| 232 | $pagesToWatch[] = $talkPage; |
| 233 | } |
| 234 | |
| 235 | // Get existing labels to remove |
| 236 | foreach ( $pagesToWatch as $pageToWatch ) { |
| 237 | $watchedItem = $this->watchedItemStore->loadWatchedItem( $user, $pageToWatch ); |
| 238 | if ( $watchedItem ) { |
| 239 | $existingLabels = $watchedItem->getLabels(); |
| 240 | // Remove all existing labels before adding new ones |
| 241 | if ( $existingLabels ) { |
| 242 | $this->watchedItemStore->removeLabels( $user, [ $pageToWatch ], $existingLabels ); |
| 243 | } |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | // Add the new labels |
| 248 | if ( $validLabels ) { |
| 249 | $this->watchedItemStore->addLabels( $user, $pagesToWatch, $validLabels ); |
| 250 | // Return the labels that we just saved |
| 251 | $res['labels'] = array_map( static function ( WatchlistLabel $label ) { |
| 252 | return [ |
| 253 | 'id' => $label->getId(), |
| 254 | 'name' => $label->getName(), |
| 255 | ]; |
| 256 | }, $validLabels ); |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | /** |
| 261 | * Validate and retrieve label objects for the given label IDs. |
| 262 | * |
| 263 | * @param User $user The user whose labels to validate |
| 264 | * @param array $labelIds The label IDs to validate |
| 265 | * @return array An associative array with 'labels' (array of validated labels) and 'error' (null or error message) |
| 266 | */ |
| 267 | private function validateLabels( User $user, array $labelIds ): array { |
| 268 | // Check if labels are enabled |
| 269 | if ( !$this->labelsEnabled ) { |
| 270 | return [ |
| 271 | 'labels' => [], |
| 272 | 'error' => $this->getErrorFormatter()->formatMessage( |
| 273 | [ 'apierror-labels-disabled', 'labels-disabled' ] |
| 274 | ) |
| 275 | ]; |
| 276 | } |
| 277 | |
| 278 | $validLabels = $this->watchlistLabelStore->loadByIds( $user, $labelIds ); |
| 279 | $hasError = count( $labelIds ) !== count( $validLabels ); |
| 280 | |
| 281 | return [ |
| 282 | 'labels' => $validLabels, |
| 283 | 'error' => $hasError ? $this->getErrorFormatter()->formatMessage( |
| 284 | [ 'apierror-invalid-label-id', 'invalid-label-id' ] |
| 285 | ) : null |
| 286 | ]; |
| 287 | } |
| 288 | |
| 289 | /** |
| 290 | * Get a cached instance of an ApiPageSet object |
| 291 | * @return ApiPageSet |
| 292 | */ |
| 293 | private function getPageSet() { |
| 294 | $this->mPageSet ??= new ApiPageSet( $this ); |
| 295 | |
| 296 | return $this->mPageSet; |
| 297 | } |
| 298 | |
| 299 | /** @inheritDoc */ |
| 300 | public function mustBePosted() { |
| 301 | return true; |
| 302 | } |
| 303 | |
| 304 | /** @inheritDoc */ |
| 305 | public function isWriteMode() { |
| 306 | return true; |
| 307 | } |
| 308 | |
| 309 | /** @inheritDoc */ |
| 310 | public function needsToken() { |
| 311 | return 'watch'; |
| 312 | } |
| 313 | |
| 314 | /** @inheritDoc */ |
| 315 | public function getAllowedParams( $flags = 0 ) { |
| 316 | $result = [ |
| 317 | 'title' => [ |
| 318 | ParamValidator::PARAM_TYPE => 'string', |
| 319 | ParamValidator::PARAM_DEPRECATED => true, |
| 320 | ], |
| 321 | 'expiry' => [ |
| 322 | ParamValidator::PARAM_TYPE => 'expiry', |
| 323 | ExpiryDef::PARAM_MAX => $this->maxDuration, |
| 324 | ExpiryDef::PARAM_USE_MAX => true, |
| 325 | ], |
| 326 | 'labels' => [ |
| 327 | ParamValidator::PARAM_TYPE => 'integer', |
| 328 | ParamValidator::PARAM_ISMULTI => true, |
| 329 | ApiBase::PARAM_HELP_MSG => 'apihelp-watch-param-labels', |
| 330 | ], |
| 331 | 'unwatch' => false, |
| 332 | 'continue' => [ |
| 333 | ApiBase::PARAM_HELP_MSG => 'api-help-param-continue', |
| 334 | ], |
| 335 | ]; |
| 336 | |
| 337 | // If expiry is not enabled, don't accept the parameter. |
| 338 | if ( !$this->expiryEnabled ) { |
| 339 | unset( $result['expiry'] ); |
| 340 | } |
| 341 | |
| 342 | if ( $flags ) { |
| 343 | $result += $this->getPageSet()->getFinalParams( $flags ); |
| 344 | } |
| 345 | |
| 346 | return $result; |
| 347 | } |
| 348 | |
| 349 | /** @inheritDoc */ |
| 350 | protected function getExamplesMessages() { |
| 351 | $title = Title::newMainPage()->getPrefixedText(); |
| 352 | $mp = rawurlencode( $title ); |
| 353 | |
| 354 | // Logically expiry example should go before unwatch examples. |
| 355 | $examples = [ |
| 356 | "action=watch&titles={$mp}&token=123ABC" |
| 357 | => 'apihelp-watch-example-watch', |
| 358 | ]; |
| 359 | if ( $this->expiryEnabled ) { |
| 360 | $examples["action=watch&titles={$mp}|Foo|Bar&expiry=1%20month&token=123ABC"] |
| 361 | = 'apihelp-watch-example-watch-expiry'; |
| 362 | } |
| 363 | |
| 364 | // Add example with labels |
| 365 | $examples["action=watch&titles={$mp}&labels=1%7C2&token=123ABC"] |
| 366 | = 'apihelp-watch-example-watch-labels'; |
| 367 | |
| 368 | return array_merge( $examples, [ |
| 369 | "action=watch&titles={$mp}&unwatch=&token=123ABC" |
| 370 | => 'apihelp-watch-example-unwatch', |
| 371 | 'action=watch&generator=allpages&gapnamespace=0&token=123ABC' |
| 372 | => 'apihelp-watch-example-generator', |
| 373 | ] ); |
| 374 | } |
| 375 | |
| 376 | /** @inheritDoc */ |
| 377 | public function getHelpUrls() { |
| 378 | return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Watch'; |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | /** @deprecated class alias since 1.43 */ |
| 383 | class_alias( ApiWatch::class, 'ApiWatch' ); |