| 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 ChangeTags; |
| 26 |
use File; |
| 27 |
use MediaWiki\MainConfigNames; |
| 28 |
use MediaWiki\Page\DeletePage; |
| 29 |
use MediaWiki\Page\DeletePageFactory; |
| 30 |
use MediaWiki\Page\File\FileDeleteForm; |
| 31 |
use MediaWiki\Status\Status; |
| 32 |
use MediaWiki\Title\Title; |
| 33 |
use MediaWiki\User\Options\UserOptionsLookup; |
| 34 |
use MediaWiki\Watchlist\WatchlistManager; |
| 35 |
use RepoGroup; |
| 36 |
use StatusValue; |
| 37 |
use Wikimedia\ParamValidator\ParamValidator; |
| 38 |
use WikiPage; |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
class ApiDelete extends ApiBase { |
| 47 |
|
| 48 |
use ApiWatchlistTrait; |
| 49 |
|
| 50 |
private RepoGroup $repoGroup; |
| 51 |
private DeletePageFactory $deletePageFactory; |
| 52 |
|
| 53 |
public function __construct( |
| 54 |
ApiMain $mainModule, |
| 55 |
string $moduleName, |
| 56 |
RepoGroup $repoGroup, |
| 57 |
WatchlistManager $watchlistManager, |
| 58 |
UserOptionsLookup $userOptionsLookup, |
| 59 |
DeletePageFactory $deletePageFactory |
| 60 |
) { |
| 61 |
parent::__construct( $mainModule, $moduleName ); |
| 62 |
$this->repoGroup = $repoGroup; |
| 63 |
$this->deletePageFactory = $deletePageFactory; |
| 64 |
|
| 65 |
|
| 66 |
$this->watchlistExpiryEnabled = $this->getConfig()->get( MainConfigNames::WatchlistExpiry ); |
| 67 |
$this->watchlistMaxDuration = |
| 68 |
$this->getConfig()->get( MainConfigNames::WatchlistExpiryMaxDuration ); |
| 69 |
$this->watchlistManager = $watchlistManager; |
| 70 |
$this->userOptionsLookup = $userOptionsLookup; |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
public function execute() { |
| 81 |
$this->useTransactionalTimeLimit(); |
| 82 |
|
| 83 |
$params = $this->extractRequestParams(); |
| 84 |
|
| 85 |
$pageObj = $this->getTitleOrPageId( $params, 'fromdbmaster' ); |
| 86 |
$titleObj = $pageObj->getTitle(); |
| 87 |
$this->getErrorFormatter()->setContextTitle( $titleObj ); |
| 88 |
if ( !$pageObj->exists() && |
| 89 |
|
| 90 |
!( $titleObj->getNamespace() === NS_FILE && self::canDeleteFile( $pageObj->getFile() ) ) |
| 91 |
) { |
| 92 |
$this->dieWithError( 'apierror-missingtitle' ); |
| 93 |
} |
| 94 |
|
| 95 |
$reason = $params['reason']; |
| 96 |
$user = $this->getUser(); |
| 97 |
|
| 98 |
$tags = $params['tags'] ?: []; |
| 99 |
|
| 100 |
if ( $titleObj->getNamespace() === NS_FILE ) { |
| 101 |
$status = $this->deleteFile( |
| 102 |
$pageObj, |
| 103 |
$params['oldimage'], |
| 104 |
$reason, |
| 105 |
false, |
| 106 |
$tags, |
| 107 |
$params['deletetalk'] |
| 108 |
); |
| 109 |
|
| 110 |
$wasScheduled = $status->isOK() && $status->getValue() === false; |
| 111 |
} else { |
| 112 |
$status = $this->delete( $pageObj, $reason, $tags, $params['deletetalk'] ); |
| 113 |
$wasScheduled = $status->isGood() && $status->getValue() === false; |
| 114 |
} |
| 115 |
|
| 116 |
if ( !$status->isOK() ) { |
| 117 |
$this->dieStatus( $status ); |
| 118 |
} |
| 119 |
|
| 120 |
if ( $wasScheduled ) { |
| 121 |
$this->addWarning( [ 'delete-scheduled', $titleObj->getPrefixedText() ] ); |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
if ( $params['watch'] ) { |
| 126 |
$watch = 'watch'; |
| 127 |
} elseif ( $params['unwatch'] ) { |
| 128 |
$watch = 'unwatch'; |
| 129 |
} else { |
| 130 |
$watch = $params['watchlist']; |
| 131 |
} |
| 132 |
|
| 133 |
$watchlistExpiry = $this->getExpiryFromParams( $params ); |
| 134 |
$this->setWatch( $watch, $titleObj, $user, 'watchdeletion', $watchlistExpiry ); |
| 135 |
|
| 136 |
$r = [ |
| 137 |
'title' => $titleObj->getPrefixedText(), |
| 138 |
'reason' => $reason, |
| 139 |
]; |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
if ( $wasScheduled ) { |
| 144 |
$r['scheduled'] = true; |
| 145 |
} else { |
| 146 |
|
| 147 |
$r['logid'] = $status->value; |
| 148 |
} |
| 149 |
$this->getResult()->addValue( null, $this->getModuleName(), $r ); |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
private function delete( WikiPage $page, &$reason, array $tags, bool $deleteTalk ): StatusValue { |
| 165 |
$title = $page->getTitle(); |
| 166 |
|
| 167 |
|
| 168 |
if ( $reason === null ) { |
| 169 |
$reason = $page->getAutoDeleteReason(); |
| 170 |
if ( $reason === false ) { |
| 171 |
|
| 172 |
return Status::newFatal( 'cannotdelete', $title->getPrefixedText() ); |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
$deletePage = $this->deletePageFactory->newDeletePage( $page, $this->getAuthority() ); |
| 177 |
if ( $deleteTalk ) { |
| 178 |
$checkStatus = $deletePage->canProbablyDeleteAssociatedTalk(); |
| 179 |
if ( !$checkStatus->isGood() ) { |
| 180 |
foreach ( $checkStatus->getMessages() as $msg ) { |
| 181 |
$this->addWarning( $msg ); |
| 182 |
} |
| 183 |
} else { |
| 184 |
$deletePage->setDeleteAssociatedTalk( true ); |
| 185 |
} |
| 186 |
} |
| 187 |
$deletionStatus = $deletePage->setTags( $tags )->deleteIfAllowed( $reason ); |
| 188 |
if ( $deletionStatus->isGood() ) { |
| 189 |
$deletionStatus->value = $deletePage->deletionsWereScheduled()[DeletePage::PAGE_BASE] |
| 190 |
? false |
| 191 |
: $deletePage->getSuccessfulDeletionsIDs()[DeletePage::PAGE_BASE]; |
| 192 |
} |
| 193 |
return $deletionStatus; |
| 194 |
} |
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
protected static function canDeleteFile( File $file ) { |
| 201 |
return $file->exists() && $file->isLocal() && !$file->getRedirected(); |
| 202 |
} |
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
private function deleteFile( |
| 214 |
WikiPage $page, |
| 215 |
$oldimage, |
| 216 |
&$reason, |
| 217 |
bool $suppress, |
| 218 |
array $tags, |
| 219 |
bool $deleteTalk |
| 220 |
) { |
| 221 |
$title = $page->getTitle(); |
| 222 |
|
| 223 |
|
| 224 |
$file = $page->getFile(); |
| 225 |
if ( !self::canDeleteFile( $file ) ) { |
| 226 |
return $this->delete( $page, $reason, $tags, $deleteTalk ); |
| 227 |
} |
| 228 |
|
| 229 |
|
| 230 |
$this->checkTitleUserPermissions( $page->getTitle(), 'delete' ); |
| 231 |
if ( $tags ) { |
| 232 |
|
| 233 |
|
| 234 |
$tagStatus = ChangeTags::canAddTagsAccompanyingChange( $tags, $this->getAuthority() ); |
| 235 |
if ( !$tagStatus->isOK() ) { |
| 236 |
$this->dieStatus( $tagStatus ); |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
if ( $oldimage ) { |
| 241 |
if ( !FileDeleteForm::isValidOldSpec( $oldimage ) ) { |
| 242 |
return Status::newFatal( 'invalidoldimage' ); |
| 243 |
} |
| 244 |
$oldfile = $this->repoGroup->getLocalRepo()->newFromArchiveName( $title, $oldimage ); |
| 245 |
if ( !$oldfile->exists() || !$oldfile->isLocal() || $oldfile->getRedirected() ) { |
| 246 |
return Status::newFatal( 'nodeleteablefile' ); |
| 247 |
} |
| 248 |
} |
| 249 |
|
| 250 |
return FileDeleteForm::doDelete( |
| 251 |
$title, |
| 252 |
$file, |
| 253 |
$oldimage, |
| 254 |
|
| 255 |
$reason ?? '', |
| 256 |
$suppress, |
| 257 |
$this->getUser(), |
| 258 |
$tags, |
| 259 |
$deleteTalk |
| 260 |
); |
| 261 |
} |
| 262 |
|
| 263 |
public function mustBePosted() { |
| 264 |
return true; |
| 265 |
} |
| 266 |
|
| 267 |
public function isWriteMode() { |
| 268 |
return true; |
| 269 |
} |
| 270 |
|
| 271 |
public function getAllowedParams() { |
| 272 |
$params = [ |
| 273 |
'title' => null, |
| 274 |
'pageid' => [ |
| 275 |
ParamValidator::PARAM_TYPE => 'integer' |
| 276 |
], |
| 277 |
'reason' => null, |
| 278 |
'tags' => [ |
| 279 |
ParamValidator::PARAM_TYPE => 'tags', |
| 280 |
ParamValidator::PARAM_ISMULTI => true, |
| 281 |
], |
| 282 |
'deletetalk' => false, |
| 283 |
'watch' => [ |
| 284 |
ParamValidator::PARAM_DEFAULT => false, |
| 285 |
ParamValidator::PARAM_DEPRECATED => true, |
| 286 |
], |
| 287 |
]; |
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
$params += $this->getWatchlistParams(); |
| 292 |
|
| 293 |
return $params + [ |
| 294 |
'unwatch' => [ |
| 295 |
ParamValidator::PARAM_DEFAULT => false, |
| 296 |
ParamValidator::PARAM_DEPRECATED => true, |
| 297 |
], |
| 298 |
'oldimage' => null, |
| 299 |
]; |
| 300 |
} |
| 301 |
|
| 302 |
public function needsToken() { |
| 303 |
return 'csrf'; |
| 304 |
} |
| 305 |
|
| 306 |
protected function getExamplesMessages() { |
| 307 |
$title = Title::newMainPage()->getPrefixedText(); |
| 308 |
$mp = rawurlencode( $title ); |
| 309 |
|
| 310 |
return [ |
| 311 |
"action=delete&title={$mp}&token=123ABC" |
| 312 |
=> 'apihelp-delete-example-simple', |
| 313 |
"action=delete&title={$mp}&token=123ABC&reason=Preparing%20for%20move" |
| 314 |
=> 'apihelp-delete-example-reason', |
| 315 |
]; |
| 316 |
} |
| 317 |
|
| 318 |
public function getHelpUrls() { |
| 319 |
return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Delete'; |
| 320 |
} |
| 321 |
} |
| 322 |
|
| 323 |
|
| 324 |
class_alias( ApiDelete::class, 'ApiDelete' ); |