Merge "Revert "Only pass strings to mysqli::real_escape_string""
[lhc/web/wiklou.git] / includes / filebackend / FileBackendStore.php
index fe3a068..50371f8 100644 (file)
@@ -655,10 +655,15 @@ abstract class FileBackendStore extends FileBackend {
                                $this->cheapCache->set( $path, 'sha1',
                                        array( 'hash' => $stat['sha1'], 'latest' => $latest ) );
                        }
+                       if ( isset( $stat['xattr'] ) ) { // some backends store headers/metadata
+                               $stat['xattr'] = self::normalizeXAttributes( $stat['xattr'] );
+                               $this->cheapCache->set( $path, 'xattr',
+                                       array( 'map' => $stat['xattr'], 'latest' => $latest ) );
+                       }
                } elseif ( $stat === false ) { // file does not exist
                        $this->cheapCache->set( $path, 'stat', $latest ? 'NOT_EXIST_LATEST' : 'NOT_EXIST' );
-                       $this->cheapCache->set( $path, 'sha1', // the SHA-1 must be false too
-                               array( 'hash' => false, 'latest' => $latest ) );
+                       $this->cheapCache->set( $path, 'xattr', array( 'map' => false, 'latest' => $latest ) );
+                       $this->cheapCache->set( $path, 'sha1', array( 'hash' => false, 'latest' => $latest ) );
                        wfDebug( __METHOD__ . ": File $path does not exist.\n" );
                } else { // an error occurred
                        wfDebug( __METHOD__ . ": Could not stat file $path.\n" );
@@ -697,6 +702,40 @@ abstract class FileBackendStore extends FileBackend {
                return $contents;
        }
 
+       final public function getFileXAttributes( array $params ) {
+               $path = self::normalizeStoragePath( $params['src'] );
+               if ( $path === null ) {
+                       return false; // invalid storage path
+               }
+               $section = new ProfileSection( __METHOD__ . "-{$this->name}" );
+               $latest = !empty( $params['latest'] ); // use latest data?
+               if ( $this->cheapCache->has( $path, 'xattr', self::CACHE_TTL ) ) {
+                       $stat = $this->cheapCache->get( $path, 'xattr' );
+                       // If we want the latest data, check that this cached
+                       // value was in fact fetched with the latest available data.
+                       if ( !$latest || $stat['latest'] ) {
+                               return $stat['map'];
+                       }
+               }
+               wfProfileIn( __METHOD__ . '-miss' );
+               wfProfileIn( __METHOD__ . '-miss-' . $this->name );
+               $fields = $this->doGetFileXAttributes( $params );
+               $fields = is_array( $fields ) ? self::normalizeXAttributes( $fields ) : false;
+               wfProfileOut( __METHOD__ . '-miss-' . $this->name );
+               wfProfileOut( __METHOD__ . '-miss' );
+               $this->cheapCache->set( $path, 'xattr', array( 'map' => $fields, 'latest' => $latest ) );
+
+               return $fields;
+       }
+
+       /**
+        * @see FileBackendStore::getFileXAttributes()
+        * @return bool|string
+        */
+       protected function doGetFileXAttributes( array $params ) {
+               return array( 'headers' => array(), 'metadata' => array() ); // not supported
+       }
+
        final public function getFileSha1Base36( array $params ) {
                $path = self::normalizeStoragePath( $params['src'] );
                if ( $path === null ) {
@@ -1625,10 +1664,36 @@ abstract class FileBackendStore extends FileBackend {
                                        $this->cheapCache->set( $path, 'sha1',
                                                array( 'hash' => $val['sha1'], 'latest' => $val['latest'] ) );
                                }
+                               if ( isset( $val['xattr'] ) ) { // some backends store headers/metadata
+                                       $val['xattr'] = self::normalizeXAttributes( $val['xattr'] );
+                                       $this->cheapCache->set( $path, 'xattr',
+                                               array( 'map' => $val['xattr'], 'latest' => $val['latest'] ) );
+                               }
                        }
                }
        }
 
+       /**
+        * Normalize file headers/metadata to the FileBackend::getFileXAttributes() format
+        *
+        * @param array $xattr
+        * @return array
+        * @since 1.22
+        */
+       final protected static function normalizeXAttributes( array $xattr ) {
+               $newXAttr = array( 'headers' => array(), 'metadata' => array() );
+
+               foreach ( $xattr['headers'] as $name => $value ) {
+                       $newXAttr['headers'][strtolower( $name )] = $value;
+               }
+
+               foreach ( $xattr['metadata'] as $name => $value ) {
+                       $newXAttr['metadata'][strtolower( $name )] = $value;
+               }
+
+               return $newXAttr;
+       }
+
        /**
         * Set the 'concurrency' option from a list of operation options
         *