| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
use MediaWiki\Api\ApiUpload; |
| 23 |
use MediaWiki\Context\RequestContext; |
| 24 |
use MediaWiki\Logger\LoggerFactory; |
| 25 |
use MediaWiki\Status\Status; |
| 26 |
use MediaWiki\User\User; |
| 27 |
use Wikimedia\ScopedCallback; |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
trait UploadJobTrait { |
| 36 |
|
| 37 |
private $user; |
| 38 |
|
| 39 |
|
| 40 |
private $cacheKey; |
| 41 |
|
| 42 |
|
| 43 |
private $upload; |
| 44 |
|
| 45 |
|
| 46 |
public $params; |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
protected function initialiseUploadJob( $cacheKey ): void { |
| 55 |
$this->cacheKey = $cacheKey; |
| 56 |
$this->user = null; |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
public function allowRetries(): bool { |
| 65 |
return false; |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
public function run(): bool { |
| 74 |
$this->user = $this->getUserFromSession(); |
| 75 |
if ( $this->user === null ) { |
| 76 |
return false; |
| 77 |
} |
| 78 |
|
| 79 |
try { |
| 80 |
|
| 81 |
$startingStatus = UploadBase::getSessionStatus( $this->user, $this->cacheKey ); |
| 82 |
|
| 83 |
|
| 84 |
if ( |
| 85 |
!$startingStatus || |
| 86 |
( $startingStatus['result'] ?? '' ) !== 'Poll' || |
| 87 |
( $startingStatus['stage'] ?? '' ) !== 'queued' |
| 88 |
) { |
| 89 |
$logger = LoggerFactory::getInstance( 'upload' ); |
| 90 |
$logger->warning( "Tried to publish upload that is in stage {stage}/{result}", |
| 91 |
$this->logJobParams( $startingStatus ) |
| 92 |
); |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
if ( !$this->fetchFile() ) { |
| 97 |
return false; |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
if ( !$this->verifyUpload() ) { |
| 102 |
return false; |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
if ( !$this->performUpload() ) { |
| 107 |
return false; |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
$this->setStatusDone(); |
| 112 |
|
| 113 |
|
| 114 |
$this->getUpload()->cleanupTempFile(); |
| 115 |
|
| 116 |
} catch ( Exception $e ) { |
| 117 |
$this->setStatus( 'publish', 'Failure', Status::newFatal( 'api-error-publishfailed' ) ); |
| 118 |
$this->setLastError( get_class( $e ) . ": " . $e->getMessage() ); |
| 119 |
|
| 120 |
|
| 121 |
MWExceptionHandler::rollbackPrimaryChangesAndLog( $e ); |
| 122 |
return false; |
| 123 |
} |
| 124 |
|
| 125 |
return true; |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
public function getCacheKey() { |
| 134 |
return $this->cacheKey; |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
private function getUserFromSession() { |
| 143 |
$scope = RequestContext::importScopedSession( $this->params['session'] ); |
| 144 |
$this->addTeardownCallback( static function () use ( &$scope ) { |
| 145 |
ScopedCallback::consume( $scope ); |
| 146 |
} ); |
| 147 |
|
| 148 |
$context = RequestContext::getMain(); |
| 149 |
$user = $context->getUser(); |
| 150 |
if ( !$user->isRegistered() ) { |
| 151 |
$this->setLastError( "Could not load the author user from session." ); |
| 152 |
|
| 153 |
return null; |
| 154 |
} |
| 155 |
return $user; |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
private function setStatus( $stage, $result = 'Poll', $status = null, $additionalInfo = [] ) { |
| 168 |
|
| 169 |
|
| 170 |
if ( $this->user === null ) { |
| 171 |
return; |
| 172 |
} |
| 173 |
$status ??= Status::newGood(); |
| 174 |
$info = [ 'result' => $result, 'stage' => $stage, 'status' => $status ]; |
| 175 |
$info += $additionalInfo; |
| 176 |
UploadBase::setSessionStatus( |
| 177 |
$this->user, |
| 178 |
$this->cacheKey, |
| 179 |
$info |
| 180 |
); |
| 181 |
} |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
protected function fetchFile(): bool { |
| 189 |
$this->setStatus( 'fetching' ); |
| 190 |
|
| 191 |
$status = $this->getUpload()->fetchFile(); |
| 192 |
if ( !$status->isGood() ) { |
| 193 |
$this->setStatus( 'fetching', 'Failure', $status ); |
| 194 |
$this->setLastError( "Error while fetching the image." ); |
| 195 |
return false; |
| 196 |
} |
| 197 |
$this->setStatus( 'publish' ); |
| 198 |
|
| 199 |
|
| 200 |
return true; |
| 201 |
} |
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
private function verifyUpload(): bool { |
| 209 |
|
| 210 |
$verification = $this->getUpload()->verifyUpload(); |
| 211 |
if ( $verification['status'] !== UploadBase::OK ) { |
| 212 |
$status = Status::newFatal( 'verification-error' ); |
| 213 |
$status->value = [ 'verification' => $verification ]; |
| 214 |
$this->setStatus( 'publish', 'Failure', $status ); |
| 215 |
$this->setLastError( "Could not verify upload." ); |
| 216 |
return false; |
| 217 |
} |
| 218 |
|
| 219 |
$titleVerification = $this->getUpload()->verifyTitlePermissions( $this->user ); |
| 220 |
if ( $titleVerification !== true ) { |
| 221 |
$this->setStatus( 'publish', 'Failure', null, $titleVerification ); |
| 222 |
$this->setLastError( "Could not verify title permissions." ); |
| 223 |
return false; |
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
$ignoreWarnings = $this->params['ignorewarnings'] ?? false; |
| 228 |
$isReupload = $this->params['reupload'] ?? false; |
| 229 |
if ( $ignoreWarnings ) { |
| 230 |
|
| 231 |
return true; |
| 232 |
} |
| 233 |
$warnings = $this->getUpload()->checkWarnings( $this->user ); |
| 234 |
if ( $warnings ) { |
| 235 |
|
| 236 |
|
| 237 |
if ( count( $warnings ) === 1 && isset( $warnings['exists'] ) && $isReupload ) { |
| 238 |
return true; |
| 239 |
} |
| 240 |
|
| 241 |
$serializableWarnings = UploadBase::makeWarningsSerializable( $warnings ); |
| 242 |
$this->setStatus( 'publish', 'Warning', null, [ 'warnings' => $serializableWarnings ] ); |
| 243 |
$this->setLastError( "Upload warnings present." ); |
| 244 |
return false; |
| 245 |
} |
| 246 |
|
| 247 |
return true; |
| 248 |
} |
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
private function performUpload(): bool { |
| 256 |
if ( $this->user === null ) { |
| 257 |
return false; |
| 258 |
} |
| 259 |
$status = $this->getUpload()->performUpload( |
| 260 |
$this->params['comment'], |
| 261 |
$this->params['text'], |
| 262 |
$this->params['watch'], |
| 263 |
$this->user, |
| 264 |
$this->params['tags'] ?? [], |
| 265 |
$this->params['watchlistexpiry'] ?? null |
| 266 |
); |
| 267 |
if ( !$status->isGood() ) { |
| 268 |
$this->setStatus( 'publish', 'Failure', $status ); |
| 269 |
$this->setLastError( $status->getWikiText( false, false, 'en' ) ); |
| 270 |
return false; |
| 271 |
} |
| 272 |
return true; |
| 273 |
} |
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
private function setStatusDone() { |
| 280 |
|
| 281 |
$imageInfo = ApiUpload::getDummyInstance()->getUploadImageInfo( $this->getUpload() ); |
| 282 |
|
| 283 |
|
| 284 |
$this->setStatus( |
| 285 |
'publish', |
| 286 |
'Success', |
| 287 |
Status::newGood(), |
| 288 |
[ 'filename' => $this->getUpload()->getLocalFile()->getName(), 'imageinfo' => $imageInfo ] |
| 289 |
); |
| 290 |
} |
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
abstract protected function getUpload(): UploadBase; |
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
abstract protected function logJobParams( $status ): array; |
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
abstract protected function setLastError( $error ); |
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
abstract protected function addTeardownCallback( $callback ); |
| 322 |
|
| 323 |
} |