Bug 20489 Configure illegal file characters https://bugzilla.wikimedia.org/show_bug...
[mediawiki.git] / js2 / uploadPage.js
blob205ca13f52425c0bbceae9f94760ecccc1cb05c8
1 /*
2  * uploadPage.js to be run on specialUpload page.
3  * controls the invocation of the mvUploader class based on local config.
4  */
5 js2AddOnloadHook( function(){
6         mwUploadHelper.init();
7 });
8 var mwUploadFormTarget = '#mw-upload-form';
9 //set up the upoload form bindings once all dom manipluation is done
10 var mwUploadHelper = {  
11         init:function(){
12                 var _this = this;
13                 //if not boolean false set to true:
14                 if(typeof wgEnableFirefogg == 'undefined')
15                         wgEnableFirefogg = true;
17                 if( wgEnableFirefogg ){
18                         //setup the upload handler to firefogg  (supports our upload proccess) (should work with the http uploads too)
19                         $j('#wpUploadFile').firefogg({
20                                 //an api url (we won't submit directly to action of the form)
21                                 'api_url' : wgServer + wgScriptPath + '/api.php',
22                                 'form_rewrite': true,
23                                 'target_edit_from' : mwUploadFormTarget,
24                                 'new_source_cb' : function( orgFilename, oggName ){
25                                 if($j('#wpDestFile').val() == "")
26                                             $j('#wpDestFile').val( oggName );
27                                         $j('#wpDestFile').doDestCheck({
28                                                 'warn_target':'#wpDestFile-warning'
29                                         });
30                                 }                               
31                         });                     
32                 }else{
33                         //Add basic upload profile support ( http status monitoring, progress box for browsers that support it etc.)
34                         if($j('#wpUploadFileURL').length != 0){
35                                 $j('#wpUploadFileURL').baseUploadInterface({
36                                         'api_url'   : wgServer + wgScriptPath + '/api.php',
37                                         'target_edit_from' : mwUploadFormTarget
38                                 });
39                         }
40                 }
42                 if( wgAjaxUploadDestCheck ){
43                         //do destination check:
44                         $j('#wpDestFile').change(function(){
45                                 $j('#wpDestFile').doDestCheck({
46                                         'warn_target':'#wpDestFile-warning'
47                                 });
48                         });
49                 }
51                 //check if we have http enabled & setup enable/disable toggle:
52                 if($j('#wpUploadFileURL').length != 0){
53                         //set the initial toggleUpType
54                         _this.toggleUpType(true);
56                         $j("input[name='wpSourceType']").click(function(){
57                                 _this.toggleUpType( this.id == 'wpSourceTypeFile' );
58                         });
59                 }
60                 $j('#wpUploadFile,#wpUploadFileURL').focus(function(){
61                         _this.toggleUpType( this.id == 'wpUploadFile' );
62                 }).change(function(){ //also setup the onChange event binding:
63                         if ( wgUploadAutoFill ) {
64                                 mwUploadHelper.doDestinationFill( this );
65                         }
66                 });
67         },
68         /**
69          * toggleUpType sets the upload radio buttons
70          *
71          * boolean set
72          */
73         toggleUpType:function( set ){
74                 $j('#wpSourceTypeFile').attr('checked', set);
75                 $j('#wpUploadFile').attr('disabled', !set);
77                 $j('#wpSourceTypeURL').attr('checked', !set);
78                 $j('#wpUploadFileURL').attr('disabled', set);
80                 //if firefogg is enbaled: toggle action per form select of http upload vs firefogg upload
81                 if( wgEnableFirefogg ){
82                         $j('#wpUploadFile').firefogg({
83                                         'firefogg_form_action': $j('#wpSourceTypeFile').attr('checked')
84                         });
85                 }
86         },      
87         /**
88          * doDestinationFill fills in a destination file-name based on a source asset name.
89          */
90         doDestinationFill : function( targetElm ){
91                 js_log("doDestinationFill")
92                 //remove any previously flagged errors
93                 $j('#mw-upload-permitted,#mw-upload-prohibited').hide();
95                 var path = $j(targetElm).val();
96                 // Find trailing part
97                 var slash = path.lastIndexOf('/');
98                 var backslash = path.lastIndexOf('\\');
99                 var fname;
100                 if (slash == -1 && backslash == -1) {
101                         fname = path;
102                 } else if (slash > backslash) {
103                         fname = path.substring(slash+1, 10000);
104                 } else {
105                         fname = path.substring(backslash+1, 10000);
106                 }
107                 //urls are less likely to have a usefull extension don't include them in the extention check
108                 if( wgFileExtensions && $j(targetElm).attr('id') != 'wpUploadFileURL' ){
109                         var found = false;
110                         if( fname.lastIndexOf('.')!=-1 ){
111                                 var ext = fname.substr( fname.lastIndexOf('.')+1 );
112                                 for(var i=0; i < wgFileExtensions.length; i++){
113                                         if(  wgFileExtensions[i].toLowerCase()   ==  ext.toLowerCase() )
114                                                 found = true;
115                                 }
116                         }
117                         if(!found){
118                                 //clear the upload set mw-upload-permitted to error
119                                 $j(targetElm).val('');
120                                 $j('#mw-upload-permitted,#mw-upload-prohibited').show().addClass('error');
121                                 //clear the wpDestFile as well:
122                                 $j('#wpDestFile').val('');
123                                 return false;
124                         }
125                 }
126                 // Capitalise first letter and replace spaces by underscores
127                 fname = fname.charAt(0).toUpperCase().concat(fname.substring(1,10000)).replace(/ /g, '_');
128                 // Output result
129                 $j('#wpDestFile').val( fname );
131                 //do a destination check
132                 $j('#wpDestFile').doDestCheck({
133                         'warn_target':'#wpDestFile-warning'
134                 });
135         }