Merge "ApiSandbox: Visual separation of fields"
[lhc/web/wiklou.git] / includes / upload / UploadStash.php
index b971c00..d5e573b 100644 (file)
@@ -54,6 +54,7 @@
 class UploadStash {
        // Format of the key for files -- has to be suitable as a filename itself (e.g. ab12cd34ef.jpg)
        const KEY_FORMAT_REGEX = '/^[\w-\.]+\.\w*$/';
+       const MAX_US_PROPS_SIZE = 65535;
 
        /**
         * repository that this uses to store temp files
@@ -64,13 +65,13 @@ class UploadStash {
        public $repo;
 
        // array of initialized repo objects
-       protected $files = array();
+       protected $files = [];
 
        // cache of the file metadata that's stored in the database
-       protected $fileMetadata = array();
+       protected $fileMetadata = [];
 
        // fileprops cache
-       protected $fileProps = array();
+       protected $fileProps = [];
 
        // current user
        protected $user, $userId, $isLoggedIn;
@@ -225,8 +226,8 @@ class UploadStash {
                // see: http://www.jwz.org/doc/mid.html
                list( $usec, $sec ) = explode( ' ', microtime() );
                $usec = substr( $usec, 2 );
-               $key = wfBaseConvert( $sec . $usec, 10, 36 ) . '.' .
-                       wfBaseConvert( mt_rand(), 10, 36 ) . '.' .
+               $key = Wikimedia\base_convert( $sec . $usec, 10, 36 ) . '.' .
+                       Wikimedia\base_convert( mt_rand(), 10, 36 ) . '.' .
                        $this->userId . '.' .
                        $extension;
 
@@ -256,7 +257,7 @@ class UploadStash {
                                $error = $storeStatus->getWarningsArray();
                                $error = reset( $error );
                                if ( !count( $error ) ) {
-                                       $error = array( 'unknown', 'no error recorded' );
+                                       $error = [ 'unknown', 'no error recorded' ];
                                }
                        }
                        // At this point, $error should contain the single "most important"
@@ -277,13 +278,22 @@ class UploadStash {
                wfDebug( __METHOD__ . " inserting $stashPath under $key\n" );
                $dbw = $this->repo->getMasterDb();
 
-               $this->fileMetadata[$key] = array(
+               $serializedFileProps = serialize( $fileProps );
+               if ( strlen( $serializedFileProps ) > self::MAX_US_PROPS_SIZE ) {
+                       // Database is going to truncate this and make the field invalid.
+                       // Prioritize important metadata over file handler metadata.
+                       // File handler should be prepared to regenerate invalid metadata if needed.
+                       $fileProps['metadata'] = false;
+                       $serializedFileProps = serialize( $fileProps );
+               }
+
+               $this->fileMetadata[$key] = [
                        'us_id' => $dbw->nextSequenceValue( 'uploadstash_us_id_seq' ),
                        'us_user' => $this->userId,
                        'us_key' => $key,
                        'us_orig_path' => $path,
                        'us_path' => $stashPath, // virtual URL
-                       'us_props' => $dbw->encodeBlob( serialize( $fileProps ) ),
+                       'us_props' => $dbw->encodeBlob( $serializedFileProps ),
                        'us_size' => $fileProps['size'],
                        'us_sha1' => $fileProps['sha1'],
                        'us_mime' => $fileProps['mime'],
@@ -294,7 +304,7 @@ class UploadStash {
                        'us_source_type' => $sourceType,
                        'us_timestamp' => $dbw->timestamp(),
                        'us_status' => 'finished'
-               );
+               ];
 
                $dbw->insert(
                        'uploadstash',
@@ -329,13 +339,13 @@ class UploadStash {
                $dbw = $this->repo->getMasterDb();
                $dbw->delete(
                        'uploadstash',
-                       array( 'us_user' => $this->userId ),
+                       [ 'us_user' => $this->userId ],
                        __METHOD__
                );
 
                # destroy objects.
-               $this->files = array();
-               $this->fileMetadata = array();
+               $this->files = [];
+               $this->fileMetadata = [];
 
                return true;
        }
@@ -361,7 +371,7 @@ class UploadStash {
                $row = $dbw->selectRow(
                        'uploadstash',
                        'us_user',
-                       array( 'us_key' => $key ),
+                       [ 'us_key' => $key ],
                        __METHOD__
                );
 
@@ -393,7 +403,7 @@ class UploadStash {
 
                $dbw->delete(
                        'uploadstash',
-                       array( 'us_key' => $key ),
+                       [ 'us_key' => $key ],
                        __METHOD__
                );
 
@@ -424,7 +434,7 @@ class UploadStash {
                $res = $dbr->select(
                        'uploadstash',
                        'us_key',
-                       array( 'us_user' => $this->userId ),
+                       [ 'us_user' => $this->userId ],
                        __METHOD__
                );
 
@@ -434,7 +444,7 @@ class UploadStash {
                }
 
                // finish the read before starting writes.
-               $keys = array();
+               $keys = [];
                foreach ( $res as $row ) {
                        array_push( $keys, $row->us_key );
                }
@@ -505,7 +515,7 @@ class UploadStash {
                $row = $dbr->selectRow(
                        'uploadstash',
                        '*',
-                       array( 'us_key' => $key ),
+                       [ 'us_key' => $key ],
                        __METHOD__
                );