mw.ForeignStructuredUpload.BookletLayout: Check upload filename for local duplicates
authorMatthias Mullie <git@mullie.eu>
Thu, 15 Sep 2016 17:23:35 +0000 (19:23 +0200)
committerBartosz Dziewoński <matma.rex@gmail.com>
Tue, 4 Oct 2016 18:41:47 +0000 (20:41 +0200)
Bug: T117498
Change-Id: I9efd067b0f79eba7513ad245e22d11dedc71051e

resources/src/mediawiki/mediawiki.ForeignStructuredUpload.BookletLayout.js

index 0f61d97..844d74c 100644 (file)
                } );
        };
 
+       /**
+        * @param {mw.Title} filename
+        * @return {jQuery.Promise} Resolves (on success) or rejects with OO.ui.Error
+        */
+       mw.ForeignStructuredUpload.BookletLayout.prototype.validateFilename = function ( filename ) {
+               return ( new mw.Api() ).get( {
+                       action: 'query',
+                       prop: 'info',
+                       titles: filename.getPrefixedDb(),
+                       formatversion: 2
+               } ).then(
+                       function ( result ) {
+                               // if the file already exists, reject right away, before
+                               // ever firing finishStashUpload()
+                               if ( !result.query.pages[ 0 ].missing ) {
+                                       return $.Deferred().reject( new OO.ui.Error(
+                                               $( '<p>' ).msg( 'fileexists', filename.getPrefixedDb() ),
+                                               { recoverable: false }
+                                       ) );
+                               }
+                       },
+                       function () {
+                               // API call failed - this could be a connection hiccup...
+                               // Let's just ignore this validation step and turn this
+                               // failure into a successful resolve ;)
+                               return $.Deferred().resolve();
+                       }
+               );
+       };
+
+       /**
+        * @inheritdoc
+        */
+       mw.ForeignStructuredUpload.BookletLayout.prototype.saveFile = function () {
+               var title = mw.Title.newFromText(
+                               this.getFilename(),
+                               mw.config.get( 'wgNamespaceIds' ).file
+                       );
+
+               return this.uploadPromise
+                       .then( this.validateFilename.bind( this, title ) )
+                       .then( mw.ForeignStructuredUpload.BookletLayout.parent.prototype.saveFile.bind( this ) );
+       };
+
        /* Getters */
 
        /**