| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Api; |
| 4 |
|
| 5 |
use MediaWiki\MediaWikiServices; |
| 6 |
use MediaWiki\Page\PageIdentity; |
| 7 |
use MediaWiki\User\Options\UserOptionsLookup; |
| 8 |
use MediaWiki\User\User; |
| 9 |
use MediaWiki\User\UserIdentity; |
| 10 |
use MediaWiki\Watchlist\WatchedItemStoreInterface; |
| 11 |
use MediaWiki\Watchlist\WatchlistManager; |
| 12 |
use Wikimedia\ParamValidator\ParamValidator; |
| 13 |
use Wikimedia\ParamValidator\TypeDef\ExpiryDef; |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
trait ApiWatchlistTrait { |
| 27 |
|
| 28 |
|
| 29 |
private $watchlistExpiryEnabled; |
| 30 |
|
| 31 |
|
| 32 |
private $watchlistMaxDuration; |
| 33 |
|
| 34 |
private WatchlistManager $watchlistManager; |
| 35 |
private UserOptionsLookup $userOptionsLookup; |
| 36 |
|
| 37 |
private function initServices() { |
| 38 |
|
| 39 |
if ( isset( $this->watchlistManager ) && isset( $this->userOptionsLookup ) ) { |
| 40 |
return; |
| 41 |
} |
| 42 |
|
| 43 |
$services = MediaWikiServices::getInstance(); |
| 44 |
$this->watchlistManager ??= $services->getWatchlistManager(); |
| 45 |
$this->userOptionsLookup ??= $services->getUserOptionsLookup(); |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
protected function getWatchlistParams( array $watchOptions = [] ): array { |
| 59 |
if ( !$watchOptions ) { |
| 60 |
$watchOptions = [ |
| 61 |
'watch', |
| 62 |
'unwatch', |
| 63 |
'preferences', |
| 64 |
'nochange', |
| 65 |
]; |
| 66 |
} |
| 67 |
|
| 68 |
$result = [ |
| 69 |
'watchlist' => [ |
| 70 |
ParamValidator::PARAM_DEFAULT => 'preferences', |
| 71 |
ParamValidator::PARAM_TYPE => $watchOptions, |
| 72 |
], |
| 73 |
]; |
| 74 |
|
| 75 |
if ( $this->watchlistExpiryEnabled ) { |
| 76 |
$result['watchlistexpiry'] = [ |
| 77 |
ParamValidator::PARAM_TYPE => 'expiry', |
| 78 |
ExpiryDef::PARAM_MAX => $this->watchlistMaxDuration, |
| 79 |
ExpiryDef::PARAM_USE_MAX => true, |
| 80 |
]; |
| 81 |
} |
| 82 |
|
| 83 |
return $result; |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
protected function setWatch( |
| 96 |
string $watch, |
| 97 |
PageIdentity $page, |
| 98 |
User $user, |
| 99 |
?string $userOption = null, |
| 100 |
?string $expiry = null |
| 101 |
): void { |
| 102 |
$value = $this->getWatchlistValue( $watch, $page, $user, $userOption ); |
| 103 |
$this->watchlistManager->setWatch( $value, $user, $page, $expiry ); |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
protected function getWatchlistValue( |
| 116 |
string $watchlist, |
| 117 |
PageIdentity $page, |
| 118 |
User $user, |
| 119 |
?string $userOption = null |
| 120 |
): bool { |
| 121 |
$this->initServices(); |
| 122 |
$userWatching = $this->watchlistManager->isWatchedIgnoringRights( $user, $page ); |
| 123 |
|
| 124 |
switch ( $watchlist ) { |
| 125 |
case 'watch': |
| 126 |
return true; |
| 127 |
|
| 128 |
case 'unwatch': |
| 129 |
return false; |
| 130 |
|
| 131 |
case 'preferences': |
| 132 |
|
| 133 |
if ( $userWatching ) { |
| 134 |
return true; |
| 135 |
} |
| 136 |
|
| 137 |
if ( $user->isBot() ) { |
| 138 |
return $userWatching; |
| 139 |
} |
| 140 |
|
| 141 |
if ( $userOption === null ) { |
| 142 |
return $this->userOptionsLookup->getBoolOption( $user, 'watchdefault' ) || |
| 143 |
( $this->userOptionsLookup->getBoolOption( $user, 'watchcreations' ) && !$page->exists() ); |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
return $this->userOptionsLookup->getBoolOption( $user, $userOption ); |
| 148 |
|
| 149 |
|
| 150 |
default: |
| 151 |
return $userWatching; |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
protected function getExpiryFromParams( array $params ): ?string { |
| 161 |
$watchlistExpiry = null; |
| 162 |
if ( $this->watchlistExpiryEnabled && isset( $params['watchlistexpiry'] ) ) { |
| 163 |
$watchlistExpiry = ApiResult::formatExpiry( $params['watchlistexpiry'] ); |
| 164 |
} |
| 165 |
|
| 166 |
return $watchlistExpiry; |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
protected function getWatchlistExpiry( |
| 178 |
WatchedItemStoreInterface $store, |
| 179 |
PageIdentity $page, |
| 180 |
UserIdentity $user |
| 181 |
): ?string { |
| 182 |
$watchedItem = $store->getWatchedItem( $user, $page ); |
| 183 |
|
| 184 |
if ( $watchedItem ) { |
| 185 |
$expiry = $watchedItem->getExpiry(); |
| 186 |
|
| 187 |
if ( $expiry !== null ) { |
| 188 |
return ApiResult::formatExpiry( $expiry ); |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
return null; |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
|
| 197 |
class_alias( ApiWatchlistTrait::class, 'ApiWatchlistTrait' ); |