Add API warnings when upload is same as older versions
authorMatthias Mullie <git@mullie.eu>
Wed, 24 Aug 2016 07:29:24 +0000 (09:29 +0200)
committerBartosz Dziewoński <matma.rex@gmail.com>
Tue, 6 Sep 2016 18:43:00 +0000 (18:43 +0000)
Bug: T141822
Change-Id: I115d84d865c59200dbb60bd962c093185c9afafe

includes/api/ApiUpload.php
includes/specials/SpecialUpload.php
includes/upload/UploadBase.php
languages/i18n/en.json
languages/i18n/qqq.json

index 54d3c3c..7b44f40 100644 (file)
@@ -704,6 +704,30 @@ class ApiUpload extends ApiBase {
                                        : $warning['file'];
                                $warnings[$warning['warning']] = $localFile->getName();
                        }
+
+                       if ( isset( $warnings['no-change'] ) ) {
+                               /** @var File $file */
+                               $file = $warnings['no-change'];
+                               unset( $warnings['no-change'] );
+
+                               $warnings['nochange'] = [
+                                       'timestamp' => wfTimestamp( TS_ISO_8601, $file->getTimestamp() )
+                               ];
+                       }
+
+                       if ( isset( $warnings['duplicate-version'] ) ) {
+                               $dupes = [];
+                               /** @var File $dupe */
+                               foreach ( $warnings['duplicate-version'] as $dupe ) {
+                                       $dupes[] = [
+                                               'timestamp' => wfTimestamp( TS_ISO_8601, $dupe->getTimestamp() )
+                                       ];
+                               }
+                               unset( $warnings['duplicate-version'] );
+
+                               ApiResult::setIndexedTagName( $dupes, 'ver' );
+                               $warnings['duplicateversions'] = $dupes;
+                       }
                }
 
                return $warnings;
index 6ca1100..305c0e9 100644 (file)
@@ -394,6 +394,18 @@ class SpecialUpload extends SpecialPage {
                        }
                        if ( $warning == 'exists' ) {
                                $msg = "\t<li>" . self::getExistsWarning( $args ) . "</li>\n";
+                       } elseif ( $warning == 'no-change' ) {
+                               $file = $args;
+                               $filename = $file->getTitle()->getPrefixedText();
+                               $msg = "\t<li>" . wfMessage( 'fileexists-no-change', $filename )->parse() . "</li>\n";
+                       } elseif ( $warning == 'duplicate-version' ) {
+                               $file = $args[0];
+                               $count = count( $args );
+                               $filename = $file->getTitle()->getPrefixedText();
+                               $message = wfMessage( 'fileexists-duplicate-version' )
+                                       ->params( $filename )
+                                       ->numParams( $count );
+                               $msg = "\t<li>" . $message->parse() . "</li>\n";
                        } elseif ( $warning == 'was-deleted' ) {
                                # If the file existed before and was deleted, warn the user of this
                                $ltitle = SpecialPage::getTitleFor( 'Log' );
index b37c831..1be5c24 100644 (file)
@@ -679,9 +679,23 @@ abstract class UploadBase {
                        $warnings['empty-file'] = true;
                }
 
+               $hash = $this->getTempFileSha1Base36();
                $exists = self::getExistsWarning( $localFile );
                if ( $exists !== false ) {
                        $warnings['exists'] = $exists;
+
+                       // check if file is an exact duplicate of current file version
+                       if ( $hash === $localFile->getSha1() ) {
+                               $warnings['no-change'] = $localFile;
+                       }
+
+                       // check if file is an exact duplicate of older versions of this file
+                       $history = $localFile->getHistory();
+                       foreach ( $history as $oldFile ) {
+                               if ( $hash === $oldFile->getSha1() ) {
+                                       $warnings['duplicate-version'][] = $oldFile;
+                               }
+                       }
                }
 
                if ( $localFile->wasDeleted() && !$localFile->exists() ) {
@@ -689,7 +703,6 @@ abstract class UploadBase {
                }
 
                // Check dupes against existing files
-               $hash = $this->getTempFileSha1Base36();
                $dupes = RepoGroup::singleton()->findBySha1( $hash );
                $title = $this->getTitle();
                // Remove all matches against self
index dcd3981..ecc1f54 100644 (file)
        "file-thumbnail-no": "The filename begins with <strong>$1</strong>.\nIt seems to be an image of reduced size <em>(thumbnail)</em>.\nIf you have this image in full resolution upload this one, otherwise change the filename please.",
        "fileexists-forbidden": "A file with this name already exists, and cannot be overwritten.\nIf you still want to upload your file, please go back and use a new name.\n[[File:$1|thumb|center|$1]]",
        "fileexists-shared-forbidden": "A file with this name exists already in the shared file repository.\nIf you still want to upload your file, please go back and use a new name.\n[[File:$1|thumb|center|$1]]",
+       "fileexists-no-change": "The upload is an exact duplicate of the current version of <strong>[[:$1]]</strong>.",
+       "fileexists-duplicate-version": "The upload is an exact duplicate of {{PLURAL:$2|an older version|older versions}} of <strong>[[:$1]]</strong>.",
        "file-exists-duplicate": "This file is a duplicate of the following {{PLURAL:$1|file|files}}:",
        "file-deleted-duplicate": "A file identical to this file ([[:$1]]) has previously been deleted.\nYou should check that file's deletion history before proceeding to re-upload it.",
        "file-deleted-duplicate-notitle": "A file identical to this file has previously been deleted, and the title has been suppressed.\nYou should ask someone with the ability to view suppressed file data to review the situation before proceeding to re-upload it.",
index 48f2013..3aae0f9 100644 (file)
        "file-thumbnail-no": "Error message at [[Special:Upload]]. Parameters:\n* $1 - String (e.g. \"180px-\")",
        "fileexists-forbidden": "{{doc-important|''thumb'' and ''center'' are magic words. Leave it untranslated!}}\nParameters:\n* $1 - name of the existing file",
        "fileexists-shared-forbidden": "{{doc-important|''thumb'' and ''center'' are magic words. Leave it untranslated!}}\nError message at [[Special:Upload]].\nParameters:\n* $1 - name of the existing file",
+       "fileexists-no-change": "Used in [[Special:Upload]] to warn when the upload is an exact duplicate of the current version of the file.\nParameters:\n* $1 - name of the existing file",
+       "fileexists-duplicate-version": "Used in [[Special:Upload]] to warn when the upload is an exact duplicate of (an) older version(s) of the file\nParameters:\n* $1 - name of the existing file\n* $2 - Amount of matching older versions (PLURAL support)",
        "file-exists-duplicate": "Used as warning in [[Special:Upload]].\nThis message is followed by the gallery of the duplicate files.\n\nParameters:\n* $1 - number of duplicate files",
        "file-deleted-duplicate": "Used in [[Special:Upload]. Parameters:\n* $1 - page title of the file\n\nSee also:\n* {{msg-mw|file-deleted-duplicate-notitle}}",
        "file-deleted-duplicate-notitle": "Used in [[Special:Upload]] when the title of the deleted duplicate is not available.\n\nSee also:\n* {{msg-mw|file-deleted-duplicate}}",