* Add exception hooks to output pretty messages
[lhc/web/wiklou.git] / includes / FileStore.php
index c81504a..a547e7e 100644 (file)
@@ -1,5 +1,8 @@
 <?php
 
+/**
+ * @todo document (needs one-sentence top-level class description).
+ */
 class FileStore {
        const DELETE_ORIGINAL = 1;
        
@@ -33,21 +36,22 @@ class FileStore {
         * suffer an uncaught error the lock will be released when the
         * connection is closed.
         *
-        * @fixme Probably only works on MySQL. Abstract to the Database class?
+        * @todo Probably only works on MySQL. Abstract to the Database class?
         */
        static function lock() {
-               $fname = __CLASS__ . '::' . __FUNCTION__;
-               
+               global $wgDBtype;
+               if ($wgDBtype != 'mysql')
+                       return true;
                $dbw = wfGetDB( DB_MASTER );
                $lockname = $dbw->addQuotes( FileStore::lockName() );
-               $result = $dbw->query( "SELECT GET_LOCK($lockname, 5) AS lockstatus", $fname );
+               $result = $dbw->query( "SELECT GET_LOCK($lockname, 5) AS lockstatus", __METHOD__ );
                $row = $dbw->fetchObject( $result );
                $dbw->freeResult( $result );
                
                if( $row->lockstatus == 1 ) {
                        return true;
                } else {
-                       wfDebug( "$fname failed to acquire lock\n" );
+                       wfDebug( __METHOD__." failed to acquire lock\n" );
                        return false;
                }
        }
@@ -56,18 +60,18 @@ class FileStore {
         * Release the global file store lock.
         */
        static function unlock() {
-               $fname = __CLASS__ . '::' . __FUNCTION__;
-               
+               global $wgDBtype;
+               if ($wgDBtype != 'mysql')
+                       return true;
                $dbw = wfGetDB( DB_MASTER );
                $lockname = $dbw->addQuotes( FileStore::lockName() );
-               $result = $dbw->query( "SELECT RELEASE_LOCK($lockname)", $fname );
-               $row = $dbw->fetchObject( $result );
+               $result = $dbw->query( "SELECT RELEASE_LOCK($lockname)", __METHOD__ );
+               $dbw->fetchObject( $result );
                $dbw->freeResult( $result );
        }
        
        private static function lockName() {
-               global $wgDBname, $wgDBprefix;
-               return "MediaWiki.{$wgDBname}.{$wgDBprefix}FileStore";
+               return 'MediaWiki.' . wfWikiID() . '.FileStore';
        }
        
        /**
@@ -103,11 +107,9 @@ class FileStore {
        }
        
        private function copyFile( $sourcePath, $destPath, $flags=0 ) {
-               $fname = __CLASS__ . '::' . __FUNCTION__;
-               
                if( !file_exists( $sourcePath ) ) {
                        // Abort! Abort!
-                       throw new FSException( "missing source file '$sourcePath'\n" );
+                       throw new FSException( "missing source file '$sourcePath'" );
                }
                
                $transaction = new FSTransaction();
@@ -126,7 +128,7 @@ class FileStore {
                                
                                if( !$ok ) {
                                        throw new FSException(
-                                               "failed to create directory for '$destPath'\n" );
+                                               "failed to create directory for '$destPath'" );
                                }
                        }
                        
@@ -135,11 +137,11 @@ class FileStore {
                        wfRestoreWarnings();
                        
                        if( $ok ) {
-                               wfDebug( "$fname copied '$sourcePath' to '$destPath'\n" );
+                               wfDebug( __METHOD__." copied '$sourcePath' to '$destPath'\n" );
                                $transaction->addRollback( FSTransaction::DELETE_FILE, $destPath );
                        } else {
                                throw new FSException(
-                                       "$fname failed to copy '$sourcePath' to '$destPath'\n" );
+                                       __METHOD__." failed to copy '$sourcePath' to '$destPath'" );
                        }
                }
                
@@ -160,7 +162,7 @@ class FileStore {
        function delete( $key ) {
                $destPath = $this->filePath( $key );
                if( false === $destPath ) {
-                       throw new FSExcepton( "file store does not contain file '$key'" );
+                       throw new FSException( "file store does not contain file '$key'" );
                } else {
                        return FileStore::deleteFile( $destPath );
                }
@@ -176,7 +178,7 @@ class FileStore {
         * @throws FSException if file can't be deleted
         * @return FSTransaction
         *
-        * @fixme Might be worth preliminary permissions check
+        * @todo Might be worth preliminary permissions check
         */
        static function deleteFile( $path ) {
                if( file_exists( $path ) ) {
@@ -217,7 +219,7 @@ class FileStore {
         * Confirm that the given file key is valid.
         * Note that a valid key may refer to a file that does not exist.
         *
-        * Key should consist of a 32-digit base-36 SHA-1 hash and
+        * Key should consist of a 31-digit base-36 SHA-1 hash and
         * an optional alphanumeric extension, all lowercase.
         * The whole must not exceed 64 characters.
         *
@@ -225,7 +227,7 @@ class FileStore {
         * @return boolean
         */
        static function validKey( $key ) {
-               return preg_match( '/^[0-9a-z]{32}(\.[0-9a-z]{1,31})?$/', $key );
+               return preg_match( '/^[0-9a-z]{31,32}(\.[0-9a-z]{1,31})?$/', $key );
        }
        
        
@@ -239,17 +241,15 @@ class FileStore {
         * @return string or false if could not open file or bad extension
         */
        static function calculateKey( $path, $extension ) {
-               $fname = __CLASS__ . '::' . __FUNCTION__;
-               
                wfSuppressWarnings();
                $hash = sha1_file( $path );
                wfRestoreWarnings();
                if( $hash === false ) {
-                       wfDebug( "$fname: couldn't hash file '$path'\n" );
+                       wfDebug( __METHOD__.": couldn't hash file '$path'\n" );
                        return false;
                }
                
-               $base36 = wfBaseConvert( $hash, 16, 36, 32 );
+               $base36 = wfBaseConvert( $hash, 16, 36, 31 );
                if( $extension == '' ) {
                        $key = $base36;
                } else {
@@ -260,7 +260,7 @@ class FileStore {
                if( self::validKey( $key ) ) {
                        return $key;
                } else {
-                       wfDebug( "$fname: generated bad key '$key'\n" );
+                       wfDebug( __METHOD__.": generated bad key '$key'\n" );
                        return false;
                }
        }
@@ -353,7 +353,6 @@ class FSTransaction {
        }
        
        private function apply( $actions ) {
-               $fname = __CLASS__ . '::' . __FUNCTION__;
                $result = true;
                foreach( $actions as $item ) {
                        list( $action, $path ) = $item;
@@ -362,9 +361,9 @@ class FSTransaction {
                                $ok = unlink( $path );
                                wfRestoreWarnings();
                                if( $ok )
-                                       wfDebug( "$fname: deleting file '$path'\n" );
+                                       wfDebug( __METHOD__.": deleting file '$path'\n" );
                                else
-                                       wfDebug( "$fname: failed to delete file '$path'\n" );
+                                       wfDebug( __METHOD__.": failed to delete file '$path'\n" );
                                $result = $result && $ok;
                        }
                }
@@ -372,6 +371,9 @@ class FSTransaction {
        }
 }
 
+/**
+ * @addtogroup Exception
+ */
 class FSException extends MWException { }
 
-?>
+