| 1 |
<?php |
| 2 |
|
| 3 |
, |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
namespace MediaWiki\Api; |
| 24 |
|
| 25 |
use MediaWiki\MainConfigNames; |
| 26 |
use MediaWiki\Page\PageIdentity; |
| 27 |
use MediaWiki\Title\Title; |
| 28 |
use MediaWiki\Title\TitleFormatter; |
| 29 |
use MediaWiki\User\User; |
| 30 |
use MediaWiki\Watchlist\WatchlistManager; |
| 31 |
use Wikimedia\ParamValidator\ParamValidator; |
| 32 |
use Wikimedia\ParamValidator\TypeDef\ExpiryDef; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
class ApiWatch extends ApiBase { |
| 40 |
|
| 41 |
private $mPageSet = null; |
| 42 |
|
| 43 |
|
| 44 |
private $expiryEnabled; |
| 45 |
|
| 46 |
|
| 47 |
private $maxDuration; |
| 48 |
|
| 49 |
protected WatchlistManager $watchlistManager; |
| 50 |
private TitleFormatter $titleFormatter; |
| 51 |
|
| 52 |
public function __construct( |
| 53 |
ApiMain $mainModule, |
| 54 |
string $moduleName, |
| 55 |
WatchlistManager $watchlistManager, |
| 56 |
TitleFormatter $titleFormatter |
| 57 |
) { |
| 58 |
parent::__construct( $mainModule, $moduleName ); |
| 59 |
|
| 60 |
$this->watchlistManager = $watchlistManager; |
| 61 |
$this->titleFormatter = $titleFormatter; |
| 62 |
$this->expiryEnabled = $this->getConfig()->get( MainConfigNames::WatchlistExpiry ); |
| 63 |
$this->maxDuration = $this->getConfig()->get( MainConfigNames::WatchlistExpiryMaxDuration ); |
| 64 |
} |
| 65 |
|
| 66 |
public function execute() { |
| 67 |
$user = $this->getUser(); |
| 68 |
if ( !$user->isRegistered() |
| 69 |
|| ( $user->isTemp() && !$user->isAllowed( 'editmywatchlist' ) ) |
| 70 |
) { |
| 71 |
$this->dieWithError( 'watchlistanontext', 'notloggedin' ); |
| 72 |
} |
| 73 |
|
| 74 |
$this->checkUserRightsAny( 'editmywatchlist' ); |
| 75 |
|
| 76 |
$params = $this->extractRequestParams(); |
| 77 |
|
| 78 |
$continuationManager = new ApiContinuationManager( $this, [], [] ); |
| 79 |
$this->setContinuationManager( $continuationManager ); |
| 80 |
|
| 81 |
$pageSet = $this->getPageSet(); |
| 82 |
|
| 83 |
|
| 84 |
if ( !isset( $params['title'] ) ) { |
| 85 |
$pageSet->execute(); |
| 86 |
$res = $pageSet->getInvalidTitlesAndRevisions( [ |
| 87 |
'invalidTitles', |
| 88 |
'special', |
| 89 |
'missingIds', |
| 90 |
'missingRevIds', |
| 91 |
'interwikiTitles' |
| 92 |
] ); |
| 93 |
|
| 94 |
foreach ( $pageSet->getMissingPages() as $page ) { |
| 95 |
$r = $this->watchTitle( $page, $user, $params ); |
| 96 |
$r['missing'] = true; |
| 97 |
$res[] = $r; |
| 98 |
} |
| 99 |
|
| 100 |
foreach ( $pageSet->getGoodPages() as $page ) { |
| 101 |
$r = $this->watchTitle( $page, $user, $params ); |
| 102 |
$res[] = $r; |
| 103 |
} |
| 104 |
ApiResult::setIndexedTagName( $res, 'w' ); |
| 105 |
} else { |
| 106 |
|
| 107 |
$extraParams = array_keys( array_filter( $pageSet->extractRequestParams(), static function ( $x ) { |
| 108 |
return $x !== null && $x !== false; |
| 109 |
} ) ); |
| 110 |
|
| 111 |
if ( $extraParams ) { |
| 112 |
$this->dieWithError( |
| 113 |
[ |
| 114 |
'apierror-invalidparammix-cannotusewith', |
| 115 |
$this->encodeParamName( 'title' ), |
| 116 |
$pageSet->encodeParamName( $extraParams[0] ) |
| 117 |
], |
| 118 |
'invalidparammix' |
| 119 |
); |
| 120 |
} |
| 121 |
|
| 122 |
$title = Title::newFromText( $params['title'] ); |
| 123 |
if ( !$title || !$this->watchlistManager->isWatchable( $title ) ) { |
| 124 |
$this->dieWithError( [ 'invalidtitle', $params['title'] ] ); |
| 125 |
} |
| 126 |
$res = $this->watchTitle( $title, $user, $params, true ); |
| 127 |
} |
| 128 |
$this->getResult()->addValue( null, $this->getModuleName(), $res ); |
| 129 |
|
| 130 |
$this->setContinuationManager( null ); |
| 131 |
$continuationManager->setContinuationIntoResult( $this->getResult() ); |
| 132 |
} |
| 133 |
|
| 134 |
private function watchTitle( PageIdentity $page, User $user, array $params, |
| 135 |
$compatibilityMode = false |
| 136 |
) { |
| 137 |
$res = [ 'title' => $this->titleFormatter->getPrefixedText( $page ), 'ns' => $page->getNamespace() ]; |
| 138 |
|
| 139 |
if ( !$this->watchlistManager->isWatchable( $page ) ) { |
| 140 |
$res['watchable'] = 0; |
| 141 |
return $res; |
| 142 |
} |
| 143 |
|
| 144 |
if ( $params['unwatch'] ) { |
| 145 |
$status = $this->watchlistManager->removeWatch( $user, $page ); |
| 146 |
$res['unwatched'] = $status->isOK(); |
| 147 |
} else { |
| 148 |
$expiry = null; |
| 149 |
|
| 150 |
|
| 151 |
if ( $this->expiryEnabled && isset( $params['expiry'] ) ) { |
| 152 |
$expiry = $params['expiry']; |
| 153 |
$res['expiry'] = ApiResult::formatExpiry( $expiry ); |
| 154 |
} |
| 155 |
|
| 156 |
$status = $this->watchlistManager->addWatch( $user, $page, $expiry ); |
| 157 |
$res['watched'] = $status->isOK(); |
| 158 |
} |
| 159 |
|
| 160 |
if ( !$status->isOK() ) { |
| 161 |
if ( $compatibilityMode ) { |
| 162 |
$this->dieStatus( $status ); |
| 163 |
} |
| 164 |
$res['errors'] = $this->getErrorFormatter()->arrayFromStatus( $status, 'error' ); |
| 165 |
$res['warnings'] = $this->getErrorFormatter()->arrayFromStatus( $status, 'warning' ); |
| 166 |
if ( !$res['warnings'] ) { |
| 167 |
unset( $res['warnings'] ); |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
return $res; |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
private function getPageSet() { |
| 179 |
$this->mPageSet ??= new ApiPageSet( $this ); |
| 180 |
|
| 181 |
return $this->mPageSet; |
| 182 |
} |
| 183 |
|
| 184 |
public function mustBePosted() { |
| 185 |
return true; |
| 186 |
} |
| 187 |
|
| 188 |
public function isWriteMode() { |
| 189 |
return true; |
| 190 |
} |
| 191 |
|
| 192 |
public function needsToken() { |
| 193 |
return 'watch'; |
| 194 |
} |
| 195 |
|
| 196 |
public function getAllowedParams( $flags = 0 ) { |
| 197 |
$result = [ |
| 198 |
'title' => [ |
| 199 |
ParamValidator::PARAM_TYPE => 'string', |
| 200 |
ParamValidator::PARAM_DEPRECATED => true, |
| 201 |
], |
| 202 |
'expiry' => [ |
| 203 |
ParamValidator::PARAM_TYPE => 'expiry', |
| 204 |
ExpiryDef::PARAM_MAX => $this->maxDuration, |
| 205 |
ExpiryDef::PARAM_USE_MAX => true, |
| 206 |
], |
| 207 |
'unwatch' => false, |
| 208 |
'continue' => [ |
| 209 |
ApiBase::PARAM_HELP_MSG => 'api-help-param-continue', |
| 210 |
], |
| 211 |
]; |
| 212 |
|
| 213 |
|
| 214 |
if ( !$this->expiryEnabled ) { |
| 215 |
unset( $result['expiry'] ); |
| 216 |
} |
| 217 |
|
| 218 |
if ( $flags ) { |
| 219 |
$result += $this->getPageSet()->getFinalParams( $flags ); |
| 220 |
} |
| 221 |
|
| 222 |
return $result; |
| 223 |
} |
| 224 |
|
| 225 |
protected function getExamplesMessages() { |
| 226 |
$title = Title::newMainPage()->getPrefixedText(); |
| 227 |
$mp = rawurlencode( $title ); |
| 228 |
|
| 229 |
|
| 230 |
$examples = [ |
| 231 |
"action=watch&titles={$mp}&token=123ABC" |
| 232 |
=> 'apihelp-watch-example-watch', |
| 233 |
]; |
| 234 |
if ( $this->expiryEnabled ) { |
| 235 |
$examples["action=watch&titles={$mp}|Foo|Bar&expiry=1%20month&token=123ABC"] |
| 236 |
= 'apihelp-watch-example-watch-expiry'; |
| 237 |
} |
| 238 |
|
| 239 |
return array_merge( $examples, [ |
| 240 |
"action=watch&titles={$mp}&unwatch=&token=123ABC" |
| 241 |
=> 'apihelp-watch-example-unwatch', |
| 242 |
'action=watch&generator=allpages&gapnamespace=0&token=123ABC' |
| 243 |
=> 'apihelp-watch-example-generator', |
| 244 |
] ); |
| 245 |
} |
| 246 |
|
| 247 |
public function getHelpUrls() { |
| 248 |
return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Watch'; |
| 249 |
} |
| 250 |
} |
| 251 |
|
| 252 |
|
| 253 |
class_alias( ApiWatch::class, 'ApiWatch' ); |