| 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 LogicException; |
| 26 |
use MediaWiki\MainConfigNames; |
| 27 |
use MediaWiki\Page\MovePageFactory; |
| 28 |
use MediaWiki\Status\Status; |
| 29 |
use MediaWiki\Title\Title; |
| 30 |
use MediaWiki\User\Options\UserOptionsLookup; |
| 31 |
use MediaWiki\Watchlist\WatchlistManager; |
| 32 |
use RepoGroup; |
| 33 |
use Wikimedia\ParamValidator\ParamValidator; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
class ApiMove extends ApiBase { |
| 40 |
|
| 41 |
use ApiWatchlistTrait; |
| 42 |
|
| 43 |
private MovePageFactory $movePageFactory; |
| 44 |
private RepoGroup $repoGroup; |
| 45 |
|
| 46 |
public function __construct( |
| 47 |
ApiMain $mainModule, |
| 48 |
string $moduleName, |
| 49 |
MovePageFactory $movePageFactory, |
| 50 |
RepoGroup $repoGroup, |
| 51 |
WatchlistManager $watchlistManager, |
| 52 |
UserOptionsLookup $userOptionsLookup |
| 53 |
) { |
| 54 |
parent::__construct( $mainModule, $moduleName ); |
| 55 |
|
| 56 |
$this->movePageFactory = $movePageFactory; |
| 57 |
$this->repoGroup = $repoGroup; |
| 58 |
|
| 59 |
|
| 60 |
$this->watchlistExpiryEnabled = $this->getConfig()->get( MainConfigNames::WatchlistExpiry ); |
| 61 |
$this->watchlistMaxDuration = |
| 62 |
$this->getConfig()->get( MainConfigNames::WatchlistExpiryMaxDuration ); |
| 63 |
$this->watchlistManager = $watchlistManager; |
| 64 |
$this->userOptionsLookup = $userOptionsLookup; |
| 65 |
} |
| 66 |
|
| 67 |
public function execute() { |
| 68 |
$this->useTransactionalTimeLimit(); |
| 69 |
|
| 70 |
$user = $this->getUser(); |
| 71 |
$params = $this->extractRequestParams(); |
| 72 |
|
| 73 |
$this->requireOnlyOneParameter( $params, 'from', 'fromid' ); |
| 74 |
|
| 75 |
if ( isset( $params['from'] ) ) { |
| 76 |
$fromTitle = Title::newFromText( $params['from'] ); |
| 77 |
if ( !$fromTitle || $fromTitle->isExternal() ) { |
| 78 |
$this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $params['from'] ) ] ); |
| 79 |
} |
| 80 |
} elseif ( isset( $params['fromid'] ) ) { |
| 81 |
$fromTitle = Title::newFromID( $params['fromid'] ); |
| 82 |
if ( !$fromTitle ) { |
| 83 |
$this->dieWithError( [ 'apierror-nosuchpageid', $params['fromid'] ] ); |
| 84 |
} |
| 85 |
} else { |
| 86 |
throw new LogicException( 'Unreachable due to requireOnlyOneParameter' ); |
| 87 |
} |
| 88 |
|
| 89 |
if ( !$fromTitle->exists() ) { |
| 90 |
$this->dieWithError( 'apierror-missingtitle' ); |
| 91 |
} |
| 92 |
$fromTalk = $fromTitle->getTalkPage(); |
| 93 |
|
| 94 |
$toTitle = Title::newFromText( $params['to'] ); |
| 95 |
if ( !$toTitle || $toTitle->isExternal() ) { |
| 96 |
$this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $params['to'] ) ] ); |
| 97 |
} |
| 98 |
$toTalk = $toTitle->getTalkPageIfDefined(); |
| 99 |
|
| 100 |
if ( $toTitle->getNamespace() === NS_FILE |
| 101 |
&& !$this->repoGroup->getLocalRepo()->findFile( $toTitle ) |
| 102 |
&& $this->repoGroup->findFile( $toTitle ) |
| 103 |
) { |
| 104 |
if ( !$params['ignorewarnings'] && |
| 105 |
$this->getAuthority()->isAllowed( 'reupload-shared' ) ) { |
| 106 |
$this->dieWithError( 'apierror-fileexists-sharedrepo-perm' ); |
| 107 |
} elseif ( !$this->getAuthority()->isAllowed( 'reupload-shared' ) ) { |
| 108 |
$this->dieWithError( 'apierror-cantoverwrite-sharedfile' ); |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
$toTitleExists = $toTitle->exists(); |
| 114 |
$mp = $this->movePageFactory->newMovePage( $fromTitle, $toTitle ); |
| 115 |
$status = $mp->moveIfAllowed( |
| 116 |
$this->getAuthority(), |
| 117 |
$params['reason'], |
| 118 |
!$params['noredirect'], |
| 119 |
$params['tags'] ?: [] |
| 120 |
); |
| 121 |
if ( !$status->isOK() ) { |
| 122 |
$this->dieStatus( $status ); |
| 123 |
} |
| 124 |
|
| 125 |
$r = [ |
| 126 |
'from' => $fromTitle->getPrefixedText(), |
| 127 |
'to' => $toTitle->getPrefixedText(), |
| 128 |
'reason' => $params['reason'] |
| 129 |
]; |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
$r['redirectcreated'] = $fromTitle->exists(); |
| 136 |
|
| 137 |
$r['moveoverredirect'] = $toTitleExists; |
| 138 |
|
| 139 |
|
| 140 |
if ( $params['movetalk'] && $toTalk && $fromTalk->exists() && !$fromTitle->isTalkPage() ) { |
| 141 |
$toTalkExists = $toTalk->exists(); |
| 142 |
$mp = $this->movePageFactory->newMovePage( $fromTalk, $toTalk ); |
| 143 |
$status = $mp->moveIfAllowed( |
| 144 |
$this->getAuthority(), |
| 145 |
$params['reason'], |
| 146 |
!$params['noredirect'], |
| 147 |
$params['tags'] ?: [] |
| 148 |
); |
| 149 |
if ( $status->isOK() ) { |
| 150 |
$r['talkfrom'] = $fromTalk->getPrefixedText(); |
| 151 |
$r['talkto'] = $toTalk->getPrefixedText(); |
| 152 |
$r['talkmoveoverredirect'] = $toTalkExists; |
| 153 |
} else { |
| 154 |
|
| 155 |
$r['talkmove-errors'] = $this->getErrorFormatter()->arrayFromStatus( $status ); |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
$result = $this->getResult(); |
| 160 |
|
| 161 |
|
| 162 |
if ( $params['movesubpages'] ) { |
| 163 |
$r['subpages'] = $this->moveSubpages( |
| 164 |
$fromTitle, |
| 165 |
$toTitle, |
| 166 |
$params['reason'], |
| 167 |
$params['noredirect'], |
| 168 |
$params['tags'] ?: [] |
| 169 |
); |
| 170 |
ApiResult::setIndexedTagName( $r['subpages'], 'subpage' ); |
| 171 |
|
| 172 |
if ( $params['movetalk'] && $toTalk ) { |
| 173 |
$r['subpages-talk'] = $this->moveSubpages( |
| 174 |
$fromTalk, |
| 175 |
$toTalk, |
| 176 |
$params['reason'], |
| 177 |
$params['noredirect'], |
| 178 |
$params['tags'] ?: [] |
| 179 |
); |
| 180 |
ApiResult::setIndexedTagName( $r['subpages-talk'], 'subpage' ); |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
$watch = $params['watchlist'] ?? 'preferences'; |
| 185 |
$watchlistExpiry = $this->getExpiryFromParams( $params ); |
| 186 |
|
| 187 |
|
| 188 |
$this->setWatch( $watch, $fromTitle, $user, 'watchmoves', $watchlistExpiry ); |
| 189 |
$this->setWatch( $watch, $toTitle, $user, 'watchmoves', $watchlistExpiry ); |
| 190 |
|
| 191 |
$result->addValue( null, $this->getModuleName(), $r ); |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
public function moveSubpages( $fromTitle, $toTitle, $reason, $noredirect, $changeTags = [] ) { |
| 203 |
$retval = []; |
| 204 |
|
| 205 |
$mp = $this->movePageFactory->newMovePage( $fromTitle, $toTitle ); |
| 206 |
$result = |
| 207 |
$mp->moveSubpagesIfAllowed( $this->getAuthority(), $reason, !$noredirect, $changeTags ); |
| 208 |
if ( !$result->isOK() ) { |
| 209 |
|
| 210 |
return [ 'errors' => $this->getErrorFormatter()->arrayFromStatus( $result ) ]; |
| 211 |
} |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
foreach ( $result->getValue() as $oldTitle => $status ) { |
| 216 |
|
| 217 |
$r = [ 'from' => $oldTitle ]; |
| 218 |
if ( $status->isOK() ) { |
| 219 |
$r['to'] = $status->getValue(); |
| 220 |
} else { |
| 221 |
$r['errors'] = $this->getErrorFormatter()->arrayFromStatus( $status ); |
| 222 |
} |
| 223 |
$retval[] = $r; |
| 224 |
} |
| 225 |
|
| 226 |
return $retval; |
| 227 |
} |
| 228 |
|
| 229 |
public function mustBePosted() { |
| 230 |
return true; |
| 231 |
} |
| 232 |
|
| 233 |
public function isWriteMode() { |
| 234 |
return true; |
| 235 |
} |
| 236 |
|
| 237 |
public function getAllowedParams() { |
| 238 |
$params = [ |
| 239 |
'from' => null, |
| 240 |
'fromid' => [ |
| 241 |
ParamValidator::PARAM_TYPE => 'integer' |
| 242 |
], |
| 243 |
'to' => [ |
| 244 |
ParamValidator::PARAM_TYPE => 'string', |
| 245 |
ParamValidator::PARAM_REQUIRED => true |
| 246 |
], |
| 247 |
'reason' => '', |
| 248 |
'movetalk' => false, |
| 249 |
'movesubpages' => false, |
| 250 |
'noredirect' => false, |
| 251 |
]; |
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
$params += $this->getWatchlistParams(); |
| 256 |
|
| 257 |
return $params + [ |
| 258 |
'ignorewarnings' => false, |
| 259 |
'tags' => [ |
| 260 |
ParamValidator::PARAM_TYPE => 'tags', |
| 261 |
ParamValidator::PARAM_ISMULTI => true, |
| 262 |
], |
| 263 |
]; |
| 264 |
} |
| 265 |
|
| 266 |
public function needsToken() { |
| 267 |
return 'csrf'; |
| 268 |
} |
| 269 |
|
| 270 |
protected function getExamplesMessages() { |
| 271 |
return [ |
| 272 |
'action=move&from=Badtitle&to=Goodtitle&token=123ABC&' . |
| 273 |
'reason=Misspelled%20title&movetalk=&noredirect=' |
| 274 |
=> 'apihelp-move-example-move', |
| 275 |
]; |
| 276 |
} |
| 277 |
|
| 278 |
public function getHelpUrls() { |
| 279 |
return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Move'; |
| 280 |
} |
| 281 |
} |
| 282 |
|
| 283 |
|
| 284 |
class_alias( ApiMove::class, 'ApiMove' ); |