1 /*jshint camelcase:false */
3 var ajaxUploadDestCheck
= mw
.config
.get( 'wgAjaxUploadDestCheck' ),
4 $license
= $( '#wpLicense' ), uploadWarning
, uploadLicense
;
6 window
.wgUploadWarningObj
= uploadWarning
= {
7 responseCache
: { '': ' ' },
13 keypress: function () {
14 if ( !ajaxUploadDestCheck
) {
18 // Find file to upload
19 if ( !$( '#wpDestFile' ).length
|| !$( '#wpDestFile-warning' ).length
) {
23 this.nameToCheck
= $( '#wpDestFile' ).val();
26 if ( this.timeoutID
) {
27 clearTimeout( this.timeoutID
);
29 // Check response cache
30 if ( this.responseCache
.hasOwnProperty( this.nameToCheck
) ) {
31 this.setWarning( this.responseCache
[this.nameToCheck
] );
35 this.timeoutID
= setTimeout( function () {
36 uploadWarning
.timeout();
40 checkNow: function ( fname
) {
41 if ( !ajaxUploadDestCheck
) {
44 if ( this.timeoutID
) {
45 clearTimeout( this.timeoutID
);
47 this.nameToCheck
= fname
;
51 timeout: function () {
52 var $spinnerDestCheck
;
53 if ( !ajaxUploadDestCheck
|| this.nameToCheck
=== '' ) {
56 $spinnerDestCheck
= $.createSpinner().insertAfter( '#wpDestFile' );
58 ( new mw
.Api() ).get( {
60 titles
: ( new mw
.Title( this.nameToCheck
, mw
.config
.get( 'wgNamespaceIds' ).file
) ).getPrefixedText(),
62 iiprop
: 'uploadwarning',
64 } ).done( function ( result
) {
67 resultOut
= result
.query
.pages
[result
.query
.pageids
[0]].imageinfo
[0];
69 $spinnerDestCheck
.remove();
70 uploadWarning
.processResult( resultOut
, uploadWarning
.nameToCheck
);
74 processResult: function ( result
, fileName
) {
75 this.setWarning( result
.html
);
76 this.responseCache
[fileName
] = result
.html
;
79 setWarning: function ( warning
) {
80 $( '#wpDestFile-warning' ).html( warning
);
82 // Set a value in the form indicating that the warning is acknowledged and
83 // doesn't need to be redisplayed post-upload
85 $( '#wpDestFileWarningAck' ).val( '' );
87 $( '#wpDestFileWarningAck' ).val( '1' );
95 responseCache
: { '': '' },
97 fetchPreview: function ( license
) {
99 if ( !mw
.config
.get( 'wgAjaxLicensePreview' ) ) {
102 if ( this.responseCache
.hasOwnProperty( license
) ) {
103 this.showPreview( this.responseCache
[license
] );
107 $spinnerLicense
= $.createSpinner().insertAfter( '#wpLicense' );
109 ( new mw
.Api() ).get( {
111 text
: '{{' + license
+ '}}',
112 title
: $( '#wpDestFile' ).val() || 'File:Sample.jpg',
115 } ).done( function ( result
) {
116 $spinnerLicense
.remove();
117 uploadLicense
.processResult( result
, license
);
121 processResult: function ( result
, license
) {
122 this.responseCache
[license
] = result
.parse
.text
['*'];
123 this.showPreview( this.responseCache
[license
] );
126 showPreview: function ( preview
) {
127 $( '#mw-license-preview' ).html( preview
);
133 // Disable URL box if the URL copy upload source type is not selected
134 if ( !$( '#wpSourceTypeurl' ).prop( 'checked' ) ) {
135 $( '#wpUploadFileURL' ).prop( 'disabled', true );
138 // AJAX wpDestFile warnings
139 if ( ajaxUploadDestCheck
) {
140 // Insert an event handler that fetches upload warnings when wpDestFile
142 $( '#wpDestFile' ).change( function () {
143 uploadWarning
.checkNow( $( this ).val() );
145 // Insert a row where the warnings will be displayed just below the
147 $( '#mw-htmlform-description tbody' ).append(
150 .attr( 'id', 'wpDestFile-warning' )
151 .attr( 'colspan', 2 )
156 if ( mw
.config
.get( 'wgAjaxLicensePreview' ) && $license
.length
) {
157 // License selector check
158 $license
.change( function () {
159 // We might show a preview
160 uploadLicense
.fetchPreview( $license
.val() );
163 // License selector table row
164 $license
.closest( 'tr' ).after(
167 $( '<td>' ).attr( 'id', 'mw-license-preview' )
172 // fillDestFile setup
173 $.each( mw
.config
.get( 'wgUploadSourceIds' ), function ( index
, sourceId
) {
174 $( '#' + sourceId
).change( function () {
175 var path
, slash
, backslash
, fname
;
176 if ( !mw
.config
.get( 'wgUploadAutoFill' ) ) {
179 // Remove any previously flagged errors
180 $( '#mw-upload-permitted' ).attr( 'class', '' );
181 $( '#mw-upload-prohibited' ).attr( 'class', '' );
183 path
= $( this ).val();
184 // Find trailing part
185 slash
= path
.lastIndexOf( '/' );
186 backslash
= path
.lastIndexOf( '\\' );
187 if ( slash
=== -1 && backslash
=== -1 ) {
189 } else if ( slash
> backslash
) {
190 fname
= path
.slice( slash
+ 1 );
192 fname
= path
.slice( backslash
+ 1 );
195 // Clear the filename if it does not have a valid extension.
196 // URLs are less likely to have a useful extension, so don't include them in the
199 mw
.config
.get( 'wgStrictFileExtensions' ) &&
200 mw
.config
.get( 'wgFileExtensions' ) &&
201 $( this ).attr( 'id' ) !== 'wpUploadFileURL'
204 fname
.lastIndexOf( '.' ) === -1 ||
206 fname
.slice( fname
.lastIndexOf( '.' ) + 1 ).toLowerCase(),
207 $.map( mw
.config
.get( 'wgFileExtensions' ), function ( element
) {
208 return element
.toLowerCase();
212 // Not a valid extension
213 // Clear the upload and set mw-upload-permitted to error
215 $( '#mw-upload-permitted' ).attr( 'class', 'error' );
216 $( '#mw-upload-prohibited' ).attr( 'class', 'error' );
217 // Clear wpDestFile as well
218 $( '#wpDestFile' ).val( '' );
224 // Replace spaces by underscores
225 fname
= fname
.replace( / /g
, '_' );
226 // Capitalise first letter if needed
227 if ( mw
.config
.get( 'wgCapitalizeUploads' ) ) {
228 fname
= fname
.charAt( 0 ).toUpperCase().concat( fname
.slice( 1 ) );
232 if ( $( '#wpDestFile' ).length
) {
233 // Call decodeURIComponent function to remove possible URL-encoded characters
234 // from the file name (bug 30390). Especially likely with upload-form-url.
235 // decodeURIComponent can throw an exception if input is invalid utf-8
237 $( '#wpDestFile' ).val( decodeURIComponent( fname
) );
239 $( '#wpDestFile' ).val( fname
);
241 uploadWarning
.checkNow( fname
);
246 }( mediaWiki
, jQuery
) );