Merge "(bug 36938) XSS in uselang parameter"
[lhc/web/wiklou.git] / skins / common / upload.js
index 2e8f54e..8e08af3 100644 (file)
@@ -1,3 +1,7 @@
+( function () {
+var    ajaxUploadDestCheck = mw.config.get( 'wgAjaxUploadDestCheck' ),
+       fileExtensions = mw.config.get( 'wgFileExtensions' );
+
 window.licenseSelectorCheck = function() {
        var selector = document.getElementById( "wpLicense" );
        var selection = selector.options[selector.selectedIndex].value;
@@ -11,7 +15,7 @@ window.licenseSelectorCheck = function() {
        wgUploadLicenseObj.fetchPreview( selection );
 };
 
-window.wgUploadSetup = function() {
+function uploadSetup() {
        // Disable URL box if the URL copy upload source type is not selected
        var e = document.getElementById( 'wpSourceTypeurl' );
        if( e ) {
@@ -36,7 +40,7 @@ window.wgUploadSetup = function() {
        }
 
        // AJAX wpDestFile warnings
-       if ( wgAjaxUploadDestCheck ) {
+       if ( ajaxUploadDestCheck ) {
                // Insert an event handler that fetches upload warnings when wpDestFile
                // has been changed
                document.getElementById( 'wpDestFile' ).onchange = function ( e ) {
@@ -54,7 +58,7 @@ window.wgUploadSetup = function() {
        }
 
        var wpLicense = document.getElementById( 'wpLicense' );
-       if ( wgAjaxLicensePreview && wpLicense ) {
+       if ( mw.config.get( 'wgAjaxLicensePreview' ) && wpLicense ) {
                // License selector check
                wpLicense.onchange = licenseSelectorCheck;
 
@@ -74,8 +78,11 @@ window.wgUploadSetup = function() {
 
 
        // fillDestFile setup
-       for ( var i = 0; i < wgUploadSourceIds.length; i++ )
-               document.getElementById( wgUploadSourceIds[i] ).onchange = function (e) {
+       var     i,
+               uploadSourceIds = mw.config.get( 'wgUploadSourceIds' ),
+               len = uploadSourceIds.length;
+       for ( i = 0; i < len; i += 1 )
+               document.getElementById( uploadSourceIds[i] ).onchange = function (e) {
                        fillDestFilename( this.id );
                };
 };
@@ -89,7 +96,7 @@ window.wgUploadWarningObj = {
        'timeoutID': false,
 
        'keypress': function () {
-               if ( !wgAjaxUploadDestCheck || !sajax_init_object() ) return;
+               if ( !ajaxUploadDestCheck || !sajax_init_object() ) return;
 
                // Find file to upload
                var destFile = document.getElementById('wpDestFile');
@@ -114,7 +121,7 @@ window.wgUploadWarningObj = {
        },
 
        'checkNow': function (fname) {
-               if ( !wgAjaxUploadDestCheck || !sajax_init_object() ) return;
+               if ( !ajaxUploadDestCheck || !sajax_init_object() ) return;
                if ( this.timeoutID ) {
                        window.clearTimeout( this.timeoutID );
                }
@@ -123,7 +130,7 @@ window.wgUploadWarningObj = {
        },
 
        'timeout' : function() {
-               if ( !wgAjaxUploadDestCheck || !sajax_init_object() ) return;
+               if ( !ajaxUploadDestCheck || !sajax_init_object() ) return;
                injectSpinner( document.getElementById( 'wpDestFile' ), 'destcheck' );
 
                // Get variables into local scope so that they will be preserved for the
@@ -168,7 +175,7 @@ window.wgUploadWarningObj = {
 };
 
 window.fillDestFilename = function(id) {
-       if (!wgUploadAutoFill) {
+       if ( !mw.config.get( 'wgUploadAutoFill' ) ) {
                return;
        }
        if (!document.getElementById) {
@@ -197,12 +204,12 @@ window.fillDestFilename = function(id) {
        // Clear the filename if it does not have a valid extension.
        // URLs are less likely to have a useful extension, so don't include them in the
        // extension check.
-       if( wgStrictFileExtensions && wgFileExtensions && id != 'wpUploadFileURL' ) {
+       if ( mw.config.get( 'wgStrictFileExtensions' ) && fileExtensions && id !== 'wpUploadFileURL' ) {
                var found = false;
-               if( fname.lastIndexOf( '.' ) != -1 ) {
+               if ( fname.lastIndexOf( '.' ) !== -1 ) {
                        var ext = fname.substr( fname.lastIndexOf( '.' ) + 1 );
-                       for( var i = 0; i < wgFileExtensions.length; i++ ) {
-                               if( wgFileExtensions[i].toLowerCase() == ext.toLowerCase() ) {
+                       for ( var i = 0; i < fileExtensions.length; i += 1 ) {
+                               if ( fileExtensions[i].toLowerCase() === ext.toLowerCase() ) {
                                        found = true;
                                        break;
                                }
@@ -229,7 +236,7 @@ window.fillDestFilename = function(id) {
        // Replace spaces by underscores
        fname = fname.replace( / /g, '_' );
        // Capitalise first letter if needed
-       if ( wgCapitalizeUploads ) {
+       if ( mw.config.get( 'wgCapitalizeUploads' ) ) {
                fname = fname.charAt( 0 ).toUpperCase().concat( fname.substring( 1, 10000 ) );
        }
 
@@ -253,7 +260,7 @@ window.wgUploadLicenseObj = {
        'responseCache' : { '' : '' },
 
        'fetchPreview': function( license ) {
-               if( !wgAjaxLicensePreview ) return;
+               if ( !mw.config.get( 'wgAjaxLicensePreview' ) ) return;
                for (cached in this.responseCache) {
                        if (cached == license) {
                                this.showPreview( this.responseCache[license] );
@@ -265,7 +272,7 @@ window.wgUploadLicenseObj = {
                var title = document.getElementById('wpDestFile').value;
                if ( !title ) title = 'File:Sample.jpg';
 
-               var url = wgScriptPath + '/api' + wgScriptExtension
+               var url = mw.util.wikiScript( 'api' )
                        + '?action=parse&text={{' + encodeURIComponent( license ) + '}}'
                        + '&title=' + encodeURIComponent( title )
                        + '&prop=text&pst&format=json';
@@ -293,4 +300,6 @@ window.wgUploadLicenseObj = {
 
 };
 
-addOnloadHook( wgUploadSetup );
+$( document ).ready( uploadSetup );
+
+}() );