From 3f01cebe9d2a0528822e0f3ee7b9cc9590424dae Mon Sep 17 00:00:00 2001 From: =?utf8?q?Bartosz=20Dziewo=C5=84ski?= Date: Mon, 28 Dec 2015 21:48:30 +0100 Subject: [PATCH] mw.Upload.BookletLayout: Don't show file extension in the filename input Also: * Strip/replace characters that are invalid in MediaWiki page titles from the default file name using mw.Title.newFromFileName * Upload files with the canonical extension for given file type (e.g. map '.JPEG' to '.jpg') using mw.Title.normalizeExtension Change-Id: Ied06682a61581303b720096bb087fc2d9ae4fdbe --- resources/Resources.php | 1 + .../mediawiki.Upload.BookletLayout.js | 34 ++++++++++++++++--- 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/resources/Resources.php b/resources/Resources.php index ad2f12cca0..ca82c84949 100644 --- a/resources/Resources.php +++ b/resources/Resources.php @@ -1186,6 +1186,7 @@ return array( ), 'dependencies' => array( 'oojs-ui', + 'mediawiki.Title', 'mediawiki.user', 'mediawiki.Upload', 'mediawiki.jqueryMsg', diff --git a/resources/src/mediawiki/mediawiki.Upload.BookletLayout.js b/resources/src/mediawiki/mediawiki.Upload.BookletLayout.js index 54f3ab68f4..3f04bb1814 100644 --- a/resources/src/mediawiki/mediawiki.Upload.BookletLayout.js +++ b/resources/src/mediawiki/mediawiki.Upload.BookletLayout.js @@ -202,7 +202,8 @@ layout = this, file = this.getFile(); - this.filenameWidget.setValue( file.name ); + this.setFilename( file.name ); + this.setPage( 'info' ); if ( this.shouldRecordBucket ) { @@ -210,8 +211,8 @@ } this.upload.setFile( file ); - // Explicitly set the filename so that the old filename isn't used in case of retry - this.upload.setFilenameFromFile(); + // The original file name might contain invalid characters, so use our sanitized one + this.upload.setFilename( this.getFilename() ); this.uploadPromise = this.upload.uploadToStash(); this.uploadPromise.then( function () { @@ -353,7 +354,7 @@ } else if ( warnings.badfilename !== undefined ) { // Change the name if the current name isn't acceptable // TODO This might not really be the best place to do this - this.filenameWidget.setValue( warnings.badfilename ); + this.setFilename( warnings.badfilename ); return new OO.ui.Error( $( '

' ).msg( 'badfilename', warnings.badfilename ) ); @@ -512,7 +513,30 @@ * @return {string} */ mw.Upload.BookletLayout.prototype.getFilename = function () { - return this.filenameWidget.getValue(); + var filename = this.filenameWidget.getValue(); + if ( this.filenameExtension ) { + filename += '.' + this.filenameExtension; + } + return filename; + }; + + /** + * Prefills the {@link #infoForm information form} with the given filename. + * + * @protected + * @param {string} filename + */ + mw.Upload.BookletLayout.prototype.setFilename = function ( filename ) { + var title = mw.Title.newFromFileName( filename ); + + if ( title ) { + this.filenameWidget.setValue( title.getNameText() ); + this.filenameExtension = mw.Title.normalizeExtension( title.getExtension() ); + } else { + // Seems to happen for files with no extension, which should fail some checks anyway... + this.filenameWidget.setValue( filename ); + this.filenameExtension = null; + } }; /** -- 2.20.1