93 $this->user =
$user ?: $wgUser;
95 if ( is_object( $this->user ) ) {
96 $this->userId = $this->user->getId();
97 $this->isLoggedIn = $this->user->isLoggedIn();
114 public function getFile( $key, $noAuth =
false ) {
115 if ( !preg_match( self::KEY_FORMAT_REGEX, $key ) ) {
117 wfMessage(
'uploadstash-bad-path-bad-format', $key )
121 if ( !$noAuth && !$this->isLoggedIn ) {
127 if ( !isset( $this->fileMetadata[$key] ) ) {
134 if ( !isset( $this->fileMetadata[$key] ) ) {
136 wfMessage(
'uploadstash-file-not-found', $key )
144 if ( strlen( $this->fileMetadata[$key][
'us_props'] ) ) {
145 $this->fileProps[$key] =
unserialize( $this->fileMetadata[$key][
'us_props'] );
147 wfDebug( __METHOD__ .
" fetched props for $key from file\n" );
148 $path = $this->fileMetadata[$key][
'us_path'];
149 $this->fileProps[$key] = $this->repo->getFileProps(
$path );
153 if ( !$this->files[$key]->exists() ) {
154 wfDebug( __METHOD__ .
" tried to get file at $key, but it doesn't exist\n" );
161 if ( !$noAuth && $this->fileMetadata[$key][
'us_user'] != $this->userId ) {
163 wfMessage(
'uploadstash-wrong-owner', $key )
167 return $this->files[$key];
179 return $this->fileMetadata[$key];
191 return $this->fileProps[$key];
207 if ( !is_file(
$path ) ) {
208 wfDebug( __METHOD__ .
" tried to stash file at '$path', but it doesn't exist\n" );
216 wfDebug( __METHOD__ .
" stashing file at '$path'\n" );
221 if ( !preg_match(
"/\\.\\Q$extension\\E$/",
$path ) ) {
222 $pathWithGoodExtension =
"$path.$extension";
224 $pathWithGoodExtension =
$path;
232 list( $usec, $sec ) = explode(
' ', microtime() );
233 $usec = substr( $usec, 2 );
234 $key = Wikimedia\base_convert( $sec . $usec, 10, 36 ) .
'.' .
235 Wikimedia\base_convert( mt_rand(), 10, 36 ) .
'.' .
236 $this->userId .
'.' .
241 if ( !preg_match( self::KEY_FORMAT_REGEX, $key ) ) {
243 wfMessage(
'uploadstash-bad-path-bad-format', $key )
247 wfDebug( __METHOD__ .
" key for '$path': $key\n" );
250 $storeStatus = $this->repo->storeTemp( basename( $pathWithGoodExtension ),
$path );
252 if ( !$storeStatus->isOK() ) {
261 $error = $storeStatus->getErrorsArray();
262 $error = reset( $error );
263 if ( !count( $error ) ) {
264 $error = $storeStatus->getWarningsArray();
265 $error = reset( $error );
266 if ( !count( $error ) ) {
267 $error = [
'unknown',
'no error recorded' ];
272 $errorMsg = array_shift( $error );
275 $stashPath = $storeStatus->value;
278 if ( !$this->isLoggedIn ) {
285 wfDebug( __METHOD__ .
" inserting $stashPath under $key\n" );
286 $dbw = $this->repo->getMasterDB();
289 if ( strlen( $serializedFileProps ) > self::MAX_US_PROPS_SIZE ) {
297 $this->fileMetadata[$key] = [
300 'us_orig_path' =>
$path,
301 'us_path' => $stashPath,
302 'us_props' => $dbw->encodeBlob( $serializedFileProps ),
310 'us_source_type' => $sourceType,
311 'us_timestamp' => $dbw->timestamp(),
312 'us_status' =>
'finished'
317 $this->fileMetadata[$key],
323 $this->fileMetadata[$key][
'us_id'] = $dbw->insertId();
325 # create the UploadStashFile object for this file.
339 if ( !$this->isLoggedIn ) {
345 wfDebug( __METHOD__ .
' clearing all rows for user ' . $this->userId .
"\n" );
346 $dbw = $this->repo->getMasterDB();
349 [
'us_user' => $this->userId ],
355 $this->fileMetadata = [];
369 if ( !$this->isLoggedIn ) {
375 $dbw = $this->repo->getMasterDB();
379 $row = $dbw->selectRow(
382 [
'us_key' => $key ],
388 wfMessage(
'uploadstash-no-such-key', $key )
392 if ( $row->us_user != $this->userId ) {
394 wfMessage(
'uploadstash-wrong-owner', $key )
408 wfDebug( __METHOD__ .
" clearing row $key\n" );
413 $dbw = $this->repo->getMasterDB();
417 [
'us_key' => $key ],
424 $this->files[$key]->remove();
426 unset( $this->files[$key] );
427 unset( $this->fileMetadata[$key] );
439 if ( !$this->isLoggedIn ) {
445 $dbr = $this->repo->getReplicaDB();
449 [
'us_user' => $this->userId ],
453 if ( !is_object(
$res ) ||
$res->numRows() == 0 ) {
460 foreach (
$res as $row ) {
461 array_push(
$keys, $row->us_key );
480 $n = strrpos(
$path,
'.' );
482 if ( $n !==
false ) {
483 $extension = $n ? substr(
$path, $n + 1 ) :
'';
487 $mimeType = $magic->guessMimeType(
$path );
488 $extensions = explode(
' ', $magic->getExtensionsForType( $mimeType ) );
489 if ( count( $extensions ) ) {
490 $extension = $extensions[0];
494 if ( is_null( $extension ) ) {
524 $dbr = $this->repo->getMasterDB();
526 $dbr = $this->repo->getReplicaDB();
529 $row =
$dbr->selectRow(
532 'us_user',
'us_key',
'us_orig_path',
'us_path',
'us_props',
533 'us_size',
'us_sha1',
'us_mime',
'us_media_type',
534 'us_image_width',
'us_image_height',
'us_image_bits',
535 'us_source_type',
'us_timestamp',
'us_status',
537 [
'us_key' => $key ],
541 if ( !is_object( $row ) ) {
546 $this->fileMetadata[$key] = (array)$row;
547 $this->fileMetadata[$key][
'us_props'] =
$dbr->decodeBlob( $row->us_props );
561 if (
$file->getSize() === 0 ) {
566 $this->files[$key] =
$file;