RevertAction: Prevent file revert if current version is identical
authorBartosz Dziewoński <matma.rex@gmail.com>
Thu, 1 Sep 2016 21:49:19 +0000 (23:49 +0200)
committerBartosz Dziewoński <matma.rex@gmail.com>
Fri, 2 Sep 2016 11:21:55 +0000 (11:21 +0000)
There are several reasons for this:

* The file thumbnail shown on the main file page is cached by the
  browser, as images tend to be. This often confuses users into
  thinking their revert did not work, and into attempting it again.
  Recent examples:
  * https://commons.wikimedia.org/wiki/Commons:Village_pump/Archive/2016/07#Wrong_SVG_rendering_on_File:DuckDuckGo_logo_and_wordmark.svg
  * https://commons.wikimedia.org/w/index.php?title=Commons:Upload_help&oldid=205348523#Reversion_is_not_working_for_me.
  Ideally we'd prevent the caching, but preventing repeated reverts
  should also work.

* Refreshing the success page causes the revert to be attempted again
  (T53383). The usual solution to this is the Post/Redirect/Get
  pattern, but we want to show a success message so that would require
  some more changes (something similar to the post-edit notification).

* It can serve as a "revert conflict" detection mechanism, crude but
  better than none.

In the unlikely case that uploading an identical older version of
the file is necessary, it can still be done using Special:Upload.

Bug: T53383
Change-Id: I37e04a536c5c2fc6cdbe59f6f598bb0c7f25d7a7

includes/actions/RevertAction.php
languages/i18n/en.json
languages/i18n/qqq.json

index c800ce7..e466e65 100644 (file)
@@ -112,13 +112,19 @@ class RevertAction extends FormAction {
        public function onSubmit( $data ) {
                $this->useTransactionalTimeLimit();
 
-               $source = $this->page->getFile()->getArchiveVirtualUrl(
-                       $this->getRequest()->getText( 'oldimage' )
-               );
+               $old = $this->getRequest()->getText( 'oldimage' );
+               $localFile = $this->page->getFile();
+               $oldFile = OldLocalFile::newFromArchiveName( $this->getTitle(), $localFile->getRepo(), $old );
+
+               $source = $localFile->getArchiveVirtualUrl( $old );
                $comment = $data['comment'];
 
+               if ( $localFile->getSha1() === $oldFile->getSha1() ) {
+                       return Status::newFatal( 'filerevert-identical' );
+               }
+
                // TODO: Preserve file properties from database instead of reloading from file
-               return $this->page->getFile()->upload(
+               return $localFile->upload(
                        $source,
                        $comment,
                        $comment,
index dcd3981..bcd740c 100644 (file)
        "filerevert-submit": "Revert",
        "filerevert-success": "<strong>[[Media:$1|$1]]</strong> has been reverted to the [$4 version as of $3, $2].",
        "filerevert-badversion": "There is no previous local version of this file with the provided timestamp.",
+       "filerevert-identical": "The current version of the file is already identical to the selected one.",
        "filedelete": "Delete $1",
        "filedelete-legend": "Delete file",
        "filedelete-intro": "You are about to delete the file <strong>[[Media:$1|$1]]</strong> along with all of its history.",
index 48f2013..655ddfa 100644 (file)
        "filerevert-submit": "{{Identical|Revert}}",
        "filerevert-success": "Message displayed when you succeed in reverting a version of a file.\n* $1 is the name of the media\n* $2 is a date\n* $3 is a time\n* $4 is an URL and must follow square bracket: [$4\n{{Identical|Revert}}",
        "filerevert-badversion": "Used as error message.",
+       "filerevert-identical": "Used as error message.",
        "filedelete": "Used as page title. Parameters:\n* $1 - file title\nSee also:\n* {{msg-mw|Filedelete-intro}}",
        "filedelete-legend": "Used as fieldset label in the \"Delete file\" form.\n{{Identical|Delete file}}",
        "filedelete-intro": "Used as introduction for FileDelete form. Parameters:\n* $1 - page title for file\nSee also:\n* {{msg-mw|Filedelete|page title}}",