Merge "mediawiki.language: Fix infinite loop in commafy() when pattern has no grouping"
[lhc/web/wiklou.git] / resources / src / mediawiki.legacy / upload.js
1 /*jshint camelcase:false */
2 ( function ( mw, $ ) {
3 var ajaxUploadDestCheck = mw.config.get( 'wgAjaxUploadDestCheck' ),
4 $license = $( '#wpLicense' ), uploadWarning, uploadLicense;
5
6 window.wgUploadWarningObj = uploadWarning = {
7 responseCache: { '': ' ' },
8 nameToCheck: '',
9 typing: false,
10 delay: 500, // ms
11 timeoutID: false,
12
13 keypress: function () {
14 if ( !ajaxUploadDestCheck ) {
15 return;
16 }
17
18 // Find file to upload
19 if ( !$( '#wpDestFile' ).length || !$( '#wpDestFile-warning' ).length ) {
20 return;
21 }
22
23 this.nameToCheck = $( '#wpDestFile' ).val();
24
25 // Clear timer
26 if ( this.timeoutID ) {
27 clearTimeout( this.timeoutID );
28 }
29 // Check response cache
30 if ( this.responseCache.hasOwnProperty( this.nameToCheck ) ) {
31 this.setWarning( this.responseCache[this.nameToCheck] );
32 return;
33 }
34
35 this.timeoutID = setTimeout( function () {
36 uploadWarning.timeout();
37 }, this.delay );
38 },
39
40 checkNow: function ( fname ) {
41 if ( !ajaxUploadDestCheck ) {
42 return;
43 }
44 if ( this.timeoutID ) {
45 clearTimeout( this.timeoutID );
46 }
47 this.nameToCheck = fname;
48 this.timeout();
49 },
50
51 timeout: function () {
52 var $spinnerDestCheck;
53 if ( !ajaxUploadDestCheck || this.nameToCheck === '' ) {
54 return;
55 }
56 $spinnerDestCheck = $.createSpinner().insertAfter( '#wpDestFile' );
57
58 ( new mw.Api() ).get( {
59 action: 'query',
60 titles: ( new mw.Title( this.nameToCheck, mw.config.get( 'wgNamespaceIds' ).file ) ).getPrefixedText(),
61 prop: 'imageinfo',
62 iiprop: 'uploadwarning',
63 indexpageids: ''
64 } ).done( function ( result ) {
65 var resultOut = '';
66 if ( result.query ) {
67 resultOut = result.query.pages[result.query.pageids[0]].imageinfo[0];
68 }
69 $spinnerDestCheck.remove();
70 uploadWarning.processResult( resultOut, uploadWarning.nameToCheck );
71 } );
72 },
73
74 processResult: function ( result, fileName ) {
75 this.setWarning( result.html );
76 this.responseCache[fileName] = result.html;
77 },
78
79 setWarning: function ( warning ) {
80 $( '#wpDestFile-warning' ).html( warning );
81
82 // Set a value in the form indicating that the warning is acknowledged and
83 // doesn't need to be redisplayed post-upload
84 if ( !warning ) {
85 $( '#wpDestFileWarningAck' ).val( '' );
86 } else {
87 $( '#wpDestFileWarningAck' ).val( '1' );
88 }
89
90 }
91 };
92
93 uploadLicense = {
94
95 responseCache: { '': '' },
96
97 fetchPreview: function ( license ) {
98 var $spinnerLicense;
99 if ( !mw.config.get( 'wgAjaxLicensePreview' ) ) {
100 return;
101 }
102 if ( this.responseCache.hasOwnProperty( license ) ) {
103 this.showPreview( this.responseCache[license] );
104 return;
105 }
106
107 $spinnerLicense = $.createSpinner().insertAfter( '#wpLicense' );
108
109 ( new mw.Api() ).get( {
110 action: 'parse',
111 text: '{{' + license + '}}',
112 title: $( '#wpDestFile' ).val() || 'File:Sample.jpg',
113 prop: 'text',
114 pst: ''
115 } ).done( function ( result ) {
116 $spinnerLicense.remove();
117 uploadLicense.processResult( result, license );
118 } );
119 },
120
121 processResult: function ( result, license ) {
122 this.responseCache[license] = result.parse.text['*'];
123 this.showPreview( this.responseCache[license] );
124 },
125
126 showPreview: function ( preview ) {
127 $( '#mw-license-preview' ).html( preview );
128 }
129
130 };
131
132 $( function () {
133 // Disable URL box if the URL copy upload source type is not selected
134 if ( !$( '#wpSourceTypeurl' ).prop( 'checked' ) ) {
135 $( '#wpUploadFileURL' ).prop( 'disabled', true );
136 }
137
138 // AJAX wpDestFile warnings
139 if ( ajaxUploadDestCheck ) {
140 // Insert an event handler that fetches upload warnings when wpDestFile
141 // has been changed
142 $( '#wpDestFile' ).change( function () {
143 uploadWarning.checkNow( $( this ).val() );
144 } );
145 // Insert a row where the warnings will be displayed just below the
146 // wpDestFile row
147 $( '#mw-htmlform-description tbody' ).append(
148 $( '<tr>' ).append(
149 $( '<td>' )
150 .attr( 'id', 'wpDestFile-warning' )
151 .attr( 'colspan', 2 )
152 )
153 );
154 }
155
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() );
161 } );
162
163 // License selector table row
164 $license.closest( 'tr' ).after(
165 $( '<tr>' ).append(
166 $( '<td>' ),
167 $( '<td>' ).attr( 'id', 'mw-license-preview' )
168 )
169 );
170 }
171
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' ) ) {
177 return;
178 }
179 // Remove any previously flagged errors
180 $( '#mw-upload-permitted' ).attr( 'class', '' );
181 $( '#mw-upload-prohibited' ).attr( 'class', '' );
182
183 path = $( this ).val();
184 // Find trailing part
185 slash = path.lastIndexOf( '/' );
186 backslash = path.lastIndexOf( '\\' );
187 if ( slash === -1 && backslash === -1 ) {
188 fname = path;
189 } else if ( slash > backslash ) {
190 fname = path.substring( slash + 1 );
191 } else {
192 fname = path.substring( backslash + 1 );
193 }
194
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
197 // extension check.
198 if (
199 mw.config.get( 'wgStrictFileExtensions' ) &&
200 mw.config.get( 'wgFileExtensions' ) &&
201 $( this ).attr( 'id' ) !== 'wpUploadFileURL'
202 ) {
203 if (
204 fname.lastIndexOf( '.' ) === -1 ||
205 $.inArray(
206 fname.substr( fname.lastIndexOf( '.' ) + 1 ).toLowerCase(),
207 $.map( mw.config.get( 'wgFileExtensions' ), function ( element ) {
208 return element.toLowerCase();
209 } )
210 ) === -1
211 ) {
212 // Not a valid extension
213 // Clear the upload and set mw-upload-permitted to error
214 $( this ).val( '' );
215 $( '#mw-upload-permitted' ).attr( 'class', 'error' );
216 $( '#mw-upload-prohibited' ).attr( 'class', 'error' );
217 // Clear wpDestFile as well
218 $( '#wpDestFile' ).val( '' );
219
220 return false;
221 }
222 }
223
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.substring( 1 ) );
229 }
230
231 // Output result
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
236 try {
237 $( '#wpDestFile' ).val( decodeURIComponent( fname ) );
238 } catch ( err ) {
239 $( '#wpDestFile' ).val( fname );
240 }
241 uploadWarning.checkNow( fname );
242 }
243 } );
244 } );
245 } );
246 }( mediaWiki, jQuery ) );