Make file purging also purge old versions
[lhc/web/wiklou.git] / includes / filerepo / file / LocalFile.php
index 194c0ed..f71e1dc 100644 (file)
@@ -593,7 +593,7 @@ class LocalFile extends File {
                if ( $upgrade ) {
                        $this->upgrading = true;
                        // Defer updates unless in auto-commit CLI mode
-                       DeferredUpdates::addCallableUpdate( function() {
+                       DeferredUpdates::addCallableUpdate( function () {
                                $this->upgrading = false; // avoid duplicate updates
                                try {
                                        $this->upgradeRow();
@@ -716,6 +716,11 @@ class LocalFile extends File {
         * @return int
         */
        public function getWidth( $page = 1 ) {
+               $page = (int)$page;
+               if ( $page < 1 ) {
+                       $page = 1;
+               }
+
                $this->load();
 
                if ( $this->isMultipage() ) {
@@ -743,6 +748,11 @@ class LocalFile extends File {
         * @return int
         */
        public function getHeight( $page = 1 ) {
+               $page = (int)$page;
+               if ( $page < 1 ) {
+                       $page = 1;
+               }
+
                $this->load();
 
                if ( $this->isMultipage() ) {
@@ -1022,9 +1032,15 @@ class LocalFile extends File {
 
                $purgeList = [];
                foreach ( $files as $file ) {
-                       # Check that the base file name is part of the thumb name
+                       if ( $this->repo->supportsSha1URLs() ) {
+                               $reference = $this->getSha1();
+                       } else {
+                               $reference = $this->getName();
+                       }
+
+                       # Check that the reference (filename or sha1) is part of the thumb name
                        # This is a basic sanity check to avoid erasing unrelated directories
-                       if ( strpos( $file, $this->getName() ) !== false
+                       if ( strpos( $file, $reference ) !== false
                                || strpos( $file, "-thumbnail" ) !== false // "short" thumb name
                        ) {
                                $purgeList[] = "{$dir}/{$file}";
@@ -1202,6 +1218,10 @@ class LocalFile extends File {
                if ( $handler ) {
                        $metadata = MediaWiki\quietCall( 'unserialize', $props['metadata'] );
 
+                       if ( !is_array( $metadata ) ) {
+                               $metadata = [];
+                       }
+
                        $options['headers'] = $handler->getContentHeaders( $metadata );
                } else {
                        $options['headers'] = [];