From b64fabe4adff76b297339cdf6fdccb0e6efe98d4 Mon Sep 17 00:00:00 2001 From: Matthias Mullie Date: Thu, 15 Sep 2016 19:23:35 +0200 Subject: [PATCH] mw.ForeignStructuredUpload.BookletLayout: Check upload filename for local duplicates Bug: T117498 Change-Id: I9efd067b0f79eba7513ad245e22d11dedc71051e --- ...i.ForeignStructuredUpload.BookletLayout.js | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/resources/src/mediawiki/mediawiki.ForeignStructuredUpload.BookletLayout.js b/resources/src/mediawiki/mediawiki.ForeignStructuredUpload.BookletLayout.js index 0f61d975f1..844d74cc39 100644 --- a/resources/src/mediawiki/mediawiki.ForeignStructuredUpload.BookletLayout.js +++ b/resources/src/mediawiki/mediawiki.ForeignStructuredUpload.BookletLayout.js @@ -309,6 +309,50 @@ } ); }; + /** + * @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( + $( '

' ).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 */ /** -- 2.20.1