Warn for uploads with new name but same content as local file
authorCormac Parle <cparle@wikimedia.org>
Thu, 30 Nov 2017 17:29:36 +0000 (17:29 +0000)
committerCormac Parle <cparle@wikimedia.org>
Thu, 30 Nov 2017 17:59:29 +0000 (17:59 +0000)
Previously warnings about the sha1 of an uploaded file being
the same as an existing file were stripped if the existing
file was in local storage - this was to avoid duplicate
warnings if a file with the same name had already been found
and had the same content. Now the warning is only stripped
for local files with the same name as the uploaded file.

Bug: T180691
Change-Id: I455df30085c05320dca976b9f7f8fb711a083271

includes/upload/UploadBase.php

index c335e2b..f5c8ee0 100644 (file)
@@ -674,7 +674,10 @@ abstract class UploadBase {
                        $warnings['was-deleted'] = $filename;
                }
 
-               $dupes = $this->checkAgainstExistingDupes( $hash );
+               // If a file with the same name exists locally then the local file has already been tested
+               // for duplication of content
+               $ignoreLocalDupes = isset( $warnings[ 'exists '] );
+               $dupes = $this->checkAgainstExistingDupes( $hash, $ignoreLocalDupes );
                if ( $dupes ) {
                        $warnings['duplicate'] = $dupes;
                }
@@ -789,15 +792,19 @@ abstract class UploadBase {
 
        /**
         * @param string $hash sha1 hash of the file to check
+        * @param bool $ignoreLocalDupes True to ignore local duplicates
         *
         * @return File[] Duplicate files, if found.
         */
-       private function checkAgainstExistingDupes( $hash ) {
+       private function checkAgainstExistingDupes( $hash, $ignoreLocalDupes ) {
                $dupes = RepoGroup::singleton()->findBySha1( $hash );
                $title = $this->getTitle();
-               // Remove all matches against self
                foreach ( $dupes as $key => $dupe ) {
-                       if ( $title->equals( $dupe->getTitle() ) ) {
+                       if (
+                               ( $dupe instanceof LocalFile ) &&
+                               $ignoreLocalDupes &&
+                               $title->equals( $dupe->getTitle() )
+                       ) {
                                unset( $dupes[$key] );
                        }
                }