r62361 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r62360‎ | r62361 | r62362 >
Date:12:14, 12 February 2010
Author:dale
Status:deferred
Tags:
Comment:
* renamed new_source_cb to selectFileCb
* some fixes to $.fn.doDestCheck for use with diffrent targets
* added destination check to cross domain file upload
* some mw.ApiProxy refactoring sendClientMsg instead of outputResultsFrame removed extra paramaters
Modified paths:
  • /branches/js2-work/phase3/js/apiProxyPage.js (modified) (history)
  • /branches/js2-work/phase3/js/mwEmbed/modules/AddMedia/jquery.simpleUploadForm.js (modified) (history)
  • /branches/js2-work/phase3/js/mwEmbed/modules/AddMedia/mw.Firefogg.js (modified) (history)
  • /branches/js2-work/phase3/js/mwEmbed/modules/AddMedia/mw.UploadHandler.js (modified) (history)
  • /branches/js2-work/phase3/js/mwEmbed/modules/ApiProxy/mw.ApiProxy.js (modified) (history)
  • /branches/js2-work/phase3/js/mwEmbed/skins/common/images/magnify-clip.png (added) (history)
  • /branches/js2-work/phase3/js/mwEmbed/tests/testApiProxy.html (modified) (history)
  • /branches/js2-work/phase3/js/uploadPage.js (modified) (history)

Diff [purge]

Index: branches/js2-work/phase3/js/mwEmbed/tests/testApiProxy.html
@@ -79,27 +79,41 @@
8080
8181 function doFileUpload(){
8282 $j('#file-upload-container').fadeIn( 'slow' );
83 - $j('#browseTarget').html( mw.loading_spinner() );
 83+ $j('#browseTarget').loadingSpinner();
 84+
 85+ mw.load('AddMedia.firefogg', function(){
 86+ mw.load( 'ApiProxy', function(){
 87+ var browserFrame = mw.ApiProxy.browseFile( {
 88+ //Target div to put the iframe browser button:
 89+ 'target' : '#browseTarget',
8490
85 - mw.load( 'ApiProxy', function(){
86 - var browserFrame = mw.ApiProxy.browseFile( {
87 - 'target' : '#browseTarget',
88 - 'api_url' : remote_api_url,
89 - // File Destination Name change callback:
90 - 'destinationCB' : function( fname ){
91 - // Update our local target:
92 - $j('#target-fname').val( fname );
93 - // Run a destination file name check on the remote target
 91+ // Api url to upload to
 92+ 'api_url' : remote_api_url,
 93+
 94+ // File Destination Name change callback:
 95+ 'selectFileCb' : function( fname ){
 96+ // Update our local target:
 97+ $j('#target-fname').val( fname );
 98+ // Run a destination file name check on the remote target
 99+ $j('#target-fname').doDestCheck( {
 100+ 'api_url' : remote_api_url,
 101+ 'warn_target': '#file-warning'
 102+ } );
 103+ }
 104+ } );
 105+
 106+ // Setup doDestCheck on change
 107+ $j('#target-fname').change(function(){
94108 $j('#target-fname').doDestCheck( {
95109 'api_url' : remote_api_url,
96110 'warn_target': '#file-warning'
97 - } );
 111+ } );
 112+ });
98113
99 - }
100 - } );
 114+ })
101115 });
102 -
103 -}
 116+};
 117+
104118 </script>
105119
106120 </head>
Index: branches/js2-work/phase3/js/mwEmbed/skins/common/images/magnify-clip.png
Cannot display: file marked as a binary type.
svn:mime-type = application/octet-stream
Property changes on: branches/js2-work/phase3/js/mwEmbed/skins/common/images/magnify-clip.png
___________________________________________________________________
Name: svn:mime-type
107121 + application/octet-stream
Index: branches/js2-work/phase3/js/mwEmbed/modules/AddMedia/mw.UploadHandler.js
@@ -28,8 +28,9 @@
2929 "mwe-ignorewarning" : "Ignore warning and save file anyway",
3030 "mwe-file-thumbnail-no" : "The filename begins with <b><tt>$1<\/tt><\/b>",
3131 "mwe-go-to-resource" : "Go to resource page",
32 - "mwe-upload-misc-error" : "Unknown upload error",
33 - "mwe-wgfogg_warning_bad_extension" : "You have selected a file with an unsuported extension (<a href=\"http:\/\/commons.wikimedia.org\/wiki\/Commons:Firefogg#Supported_File_Types\">more information<\/a>)."
 32+ "mwe-upload-misc-error" : "Unknown upload error",
 33+ "mwe-wgfogg_warning_bad_extension" : "You have selected a file with an unsuported extension (<a href=\"http:\/\/commons.wikimedia.org\/wiki\/Commons:Firefogg#Supported_File_Types\">more information<\/a>).",
 34+ "thumbnail-more" : "Enlarge"
3435 });
3536
3637 var default_bui_options = {
@@ -129,7 +130,7 @@
130131 this.api_url = mw.getLocalApiUrl();
131132 }
132133 // Setup the UploadInterface handler
133 - this.interface = mw.UploadInterface.factory( this.interface_type );
 134+ this.interface = mw.UploadInterface.factory( this.interface_type );
134135
135136 mw.log( "init mvUploadHandler:: " + this.api_url + ' interface: ' + this.interface );
136137 },
@@ -979,8 +980,8 @@
980981 options.api_url = mw.getLocalApiUrl();
981982 }
982983
983 - // Add the wpDestFile-warning row
984 - if ( $j( '#wpDestFile-warning' ).length == 0 ) {
 984+ // Add the wpDestFile-warning row ( if in mediaWiki upload page )
 985+ if ( $j( options.warn_target ).length == 0 ) {
985986 $j( '#mw-htmlform-options tr:last' )
986987 .after(
987988 $j('<tr />' )
@@ -995,11 +996,11 @@
996997
997998 // Show the AJAX spinner
998999 $j( _this.selector ).after(
999 - $j('<img />')
 1000+ $j('<div />')
10001001 .attr({
1001 - 'id' : "mw-spinner-wpDestFile",
1002 - 'src' : stylepath + '/common/images/spinner.gif'
 1002+ 'id' : "mw-spinner-wpDestFile",
10031003 })
 1004+ .loadingSpinner()
10041005 );
10051006 // Setup the request
10061007 var request = {
@@ -1013,7 +1014,7 @@
10141015 mw.getJSON( options.api_url, request, function( data ) {
10151016 // Remove spinner
10161017 $j( '#mw-spinner-wpDestFile' ).remove();
1017 -
 1018+
10181019 if ( !data || !data.query || !data.query.pages ) {
10191020 // Ignore a null result
10201021 return;
@@ -1035,7 +1036,7 @@
10361037 var ntitle = data.query.pages[ page_id ].title
10371038 }
10381039 var img = data.query.pages[ page_id ].imageinfo[0];
1039 - $j( '#wpDestFile-warning' ).html(
 1040+ $j( options.warn_target ).html(
10401041 gM( 'mwe-fileexists', ntitle ) +
10411042 '<div class="thumb tright">' +
10421043 '<div ' +
@@ -1057,8 +1058,8 @@
10581059 '<div class="magnify">' +
10591060 '<a title="' + gM('thumbnail-more') + '" class="internal" ' +
10601061 'href="' + img.descriptionurl +'">' +
1061 - '<img width="15" height="11" alt="" ' +
1062 - 'src="' + stylepath + "/common/images/magnify-clip.png\" />" +
 1062+ '<img border="0" width="15" height="11" alt="" ' +
 1063+ 'src="' + mw.getConfig( 'images_path' ) + 'magnify-clip.png" />' +
10631064 '</a>' +
10641065 '</div>' +
10651066 gM( 'mwe-fileexists-thumb' ) +
Index: branches/js2-work/phase3/js/mwEmbed/modules/AddMedia/mw.Firefogg.js
@@ -1,4 +1,5 @@
2 -/* Firefogg support.
 2+/**
 3+ * Firefogg support.
34 * autodetects: new upload api or old http POST.
45 */
56
@@ -45,7 +46,7 @@
4647 'only_firefogg': false,
4748
4849 // Callback which is called when the source name changes
49 - 'new_source_cb': false,
 50+ 'selectFileCb': false,
5051
5152 // jQuery selector identifying the target control container or form (can't be left null)
5253 'selector': '',
@@ -160,7 +161,7 @@
161162 // Prefix conflicting members with parent_
162163 for ( var i in myBUI ) {
163164 if ( this[i] ) {
164 - this['parent_'+ i] = myBUI[i];
 165+ this[ 'parent_'+ i ] = myBUI[i];
165166 } else {
166167 this[i] = myBUI[i];
167168 }
@@ -621,8 +622,8 @@
622623 .show();
623624
624625
625 - // Notify callback new_source_cb
626 - if ( _this.new_source_cb ) {
 626+ // Notify callback selectFileCb
 627+ if ( _this.selectFileCb ) {
627628 if ( settings['passthrough'] ) {
628629 var fName = _this.fogg.sourceFilename;
629630 } else {
@@ -633,7 +634,7 @@
634635 _this.fogg.sourceFilename.lastIndexOf( '.' ) );
635636 var fName = oggName + '.' + oggExt;
636637 }
637 - _this.new_source_cb( _this.fogg.sourceFilename, fName );
 638+ _this.selectFileCb( fName );
638639 }
639640 },
640641
@@ -1059,7 +1060,7 @@
10601061
10611062
10621063 if ( _this.oldResponseText != response_text ) {
1063 - mw.log( 'new result text:' + response_text + ' state:' + _this.fogg.state );
 1064+ mw.log( 'Fogg: new result text:' + response_text + ' state:' + _this.fogg.state );
10641065 _this.oldResponseText = response_text;
10651066 // Parse the response text and check for errors
10661067 try {
@@ -1072,8 +1073,8 @@
10731074 } catch( e ) {
10741075 var apiResult = null;
10751076 }
1076 - }
1077 -
 1077+ }
 1078+
10781079 //Check for success:
10791080 if( apiResult && _this.isApiSuccess( apiResult ) ){
10801081 if( _this.processApiResult ( apiResult ) ){
@@ -1144,7 +1145,7 @@
11451146 * @param {Element} dialogElement Dialog element that was "canceled"
11461147 */
11471148 onCancel: function( dialogElement ) {
1148 - if ( !this.have_firefogg ) {
 1149+ if ( !this.getFirefogg() ) {
11491150 return this.parent_cancel_action();
11501151 }
11511152 mw.log( 'firefogg:cancel' )
Index: branches/js2-work/phase3/js/mwEmbed/modules/AddMedia/jquery.simpleUploadForm.js
@@ -147,7 +147,7 @@
148148 'enable_chunks' : false,
149149
150150 'form_selector' : '#suf_upload',
151 - 'new_source_cb' : function( orgFilename, oggName ) {
 151+ 'selectFileCb' : function( fileName ) {
152152 $j( "#wpDestFile" ).val( oggName ).doDestCheck( {
153153 warn_target: "#wpDestFile-warning"
154154 } );
Index: branches/js2-work/phase3/js/mwEmbed/modules/ApiProxy/mw.ApiProxy.js
@@ -193,41 +193,64 @@
194194
195195 // Setup the proxy callback to display the upload unhide the iframe upload form
196196 proxyCallback = function( iframeData ){
197 - //hide the loading spinner
198 - $j( options.target ).find('.loading_spinner').fadeOut('fast');
199 - mw.log("iframe ready callback");
200 - $j( '#' + iFrameName ).fadeIn( 'fast' );
 197+ //proccess fileBrowse callbacks::
 198+
 199+ // check for basic status "ok"
 200+ if( iframeData['status'] == 'ok' ){
 201+ // Hide the loading spinner
 202+ $j( options.target ).find('.loading_spinner').fadeOut('fast');
 203+ mw.log("iframe ready callback");
 204+ $j( '#' + iFrameName ).fadeIn( 'fast' );
 205+ }
 206+ // else check for event
 207+ if( iframeData['event'] ){
 208+ switch( iframeData['event'] ){
 209+ case 'selectFileCb':
 210+ if( options.selectFileCb ){
 211+ options.selectFileCb( iframeData['fileName'] );
 212+ }
 213+ break;
 214+ default:
 215+ mw.log(" Error unreconginzed event " + iframeData['event'] );
 216+ }
 217+
 218+ }
 219+
 220+
201221 }
202222
203223 }
204224
205225 /**
206226 * Api server proxy entry point:
207 - *
208 - * @param {Object} proxyConfig The server side proxy configuration
209 - * @param {Function} callbcak Function to call once server is setup
210 - *
 227+ * validates the server frame request
 228+ * and proccess the request type
211229 */
212 - $.server = function( proxyConfig, callback ) {
 230+ $.server = function() {
 231+ var proxyConfig = mw.getConfig( 'apiProxyConfig' );
213232 // Validate the server request:
214 - if( validateIframeRequest( proxyConfig ) ){
215 - var clientRequest = getClientRequest();
216 -
217 - // Inform the client frame that we passed validation
218 - outputResultsFrame( clientRequest.clientFrame, 'nested_ok', { 'state':'ok' } );
219 - // Process request type
220 - if( clientRequest['browseFile'] ){
221 - mw.log( "DO BROWSE FILE" );
222 - serverBrowseFile( proxyConfig );
223 - return ;
224 - }
225 - // Else do a normal api request :
226 - return doApiRequest();
227 - }
 233+ if( !validateIframeRequest( proxyConfig ) ){
 234+ mw.log( "Not a valid iframe request");
 235+ return false;
 236+ }
 237+ var clientRequest = getClientRequest();
 238+
 239+ // Inform the client frame that we passed validation
 240+ sendClientMsg( { 'state':'ok' } );
 241+
 242+ // Process request type
 243+ if( clientRequest['browseFile'] ){
 244+ mw.log( "DO BROWSE FILE" );
 245+ serverBrowseFile( proxyConfig );
 246+ return ;
 247+ }
 248+ // Else do a normal api request :
 249+ return doApiRequest();
 250+
228251 }
229252
230253 /**
231 - * Local scoped helper functions:
 254+ * Local scoped helper functions: (works well with closure compiler )
232255 */
233256
234257 /**
@@ -337,12 +360,24 @@
338361
339362 mw.log( "Setup server on: " + mw.parseUri( document.URL ).host );
340363 mw.log('Client frame: ' + clientRequest.clientFrame );
341 -
342 - var clientDomain = mw.parseUri( clientRequest.clientFrame ).host ;
343 -
 364+
344365 /**
345 - * HERE WE CHECK IF THE DOMAIN IS ALLOWED per the proxyConfig
 366+ * HERE WE CHECK IF THE DOMAIN IS ALLOWED per the proxyConfig
346367 */
 368+ return isAllowedClientFrame( clientRequest.clientFrame );
 369+
 370+
 371+ // Not a valid request return false
 372+ }
 373+
 374+ /**
 375+ * Check if a domain is allowed.
 376+ * @param {Object} clientFrame
 377+ */
 378+ function isAllowedClientFrame( clientFrame ){
 379+ var clientDomain = mw.parseUri( clientFrame ).host ;
 380+ // Get the proxy config
 381+ var proxyConfig = mw.getConfig( 'apiProxyConfig' );
347382 // Check master blacklist
348383 for ( var i in proxyConfig.master_blacklist ) {
349384 if ( clientDomain == proxyConfig.master_blacklist ) {
@@ -356,16 +391,13 @@
357392 return true;
358393 }
359394 }
360 -
361395 // FIXME Add in user based approval ::
362396
363397 // FIXME offer the user the ability to "approve" requested domain save to
364398 // their user preference setup )
365399
366 - // FIXME grab the users whitelist for our current domain
367 -
368 - // Not a valid request return false
369 - return false;
 400+ // FIXME grab the users whitelist for our current domain
 401+ return false;
370402 }
371403
372404 /**
@@ -437,8 +469,8 @@
438470 $j.post( wgScriptPath + '/api' + wgScriptExtension,
439471 clientRequest.request,
440472 function( data ) {
441 - // Put it result into nested frame hash string:
442 - outputResultsFrame( clientRequest.clientFrame, 'nested_push', JSON.parse( data ) );
 473+ // Send the result data to the client
 474+ sendClientMsg( JSON.parse( data ) );
443475 }
444476 );
445477 }
@@ -451,7 +483,7 @@
452484 function serverBrowseFile( proxyConfig ){
453485 //check for fw ( file width )
454486 if( ! proxyConfig.fileWidth ){
455 - proxyConfig.fileWidth = 200;
 487+ proxyConfig.fileWidth = 130;
456488 }
457489 //Build a form with bindings similar to uploadPage.js ( but only the browse button )
458490 $j('body').html(
@@ -478,12 +510,6 @@
479511 )
480512 );
481513
482 - // Local function to call once frame is ready:
483 - function clientFrameReady( status ){
484 - var clientRequest = getClientRequest();
485 - // Inform the client frame that we passed validation
486 - outputResultsFrame( clientRequest.clientFrame, 'browse_file', status );
487 - }
488514
489515 // load the mw.upload library with iframe interface (similar to uploadPage.js)
490516
@@ -491,53 +517,70 @@
492518 if ( typeof wgEnableFirefogg == 'undefined' ){
493519 wgEnableFirefogg = true;
494520 }
495 -
 521+
 522+
 523+ var uploadConfig = {
 524+ // Set the interface type
 525+ 'interface_type' : 'iframe',
 526+
 527+ // Set the select file callback to update clientFrame
 528+ 'selectFileCb' : function( fileName ){
 529+ sendClientMsg( {
 530+ 'event': 'selectFileCb',
 531+ 'fileName' : fileName
 532+ } );
 533+ }
 534+ }
 535+
496536 if( wgEnableFirefogg ){
497537 mw.load( 'AddMedia.firefogg', function(){
498 - $j( '#wpUploadFile' ).firefogg( {
499 - // Set the interface type
500 - 'interface_type' : 'iframe'
501 - });
 538+ $j( '#wpUploadFile' ).firefogg( uploadConfig );
502539 // Update status
503 - clientFrameReady( { 'firefogg' : 'ok' } );
 540+ sendClientMsg( {'status':'ok'} );
504541 });
505542 } else {
506543 mw.load( 'AddMedia.UploadHandler', function(){
507 - $j( mwUploadFormSelector ).uploadHandler({
508 - // Set the interface type
509 - 'interface_type' : 'iframe'
510 - });
511 - clientFrameReady();
 544+ $j( 'mw-upload-form' ).uploadHandler( uploadConfig );
 545+ sendClientMsg( {'status':'ok'} );
512546 });
513 - }
514 -
515 -
516 -
517 -
518 -
 547+ }
519548 };
520549
521550 /**
522551 * Outputs the result object to the client domain
523552 *
524 - * @param {clientFrame} clientFrame Client frame name
525 - * @param {String} nestName Name of iframe
526 - * @param {resultObj} the result to pass back to the client domain
 553+ * @param {msgObj} msgObj Msg to send to client domain
527554 */
528 - function outputResultsFrame( clientFrame, nestName, resultObj ) {
529 - $j( '#nested_push' ).remove();
 555+ function sendClientMsg( msgObj ) {
 556+
 557+ // Get a local refrence to the client request
 558+ var clientFrame = getClientRequest()['clientFrame'];
 559+
 560+ // Double check that the client is an approved domain before outputing the iframe
 561+ if( ! isAllowedClientFrame ( clientFrame ) ){
 562+ mw.log( "cant send msg to " + clientFrame );
 563+ return false;
 564+ }
 565+ var nestName = 'NestedFrame_' + $j('iframe').length;
 566+
530567 // Setup the nested iframe proxy that points back to top domain
531 - // (can use jQuery buildout since we don't need to refrence this frame again)
 568+ // can't use jquery build out because of IE name attribute bug
532569 $j( 'body' ).append(
533 - $j('<iframe>').attr({
534 - 'id' : nestName,
535 - 'name' : nestName,
536 - 'src' : clientFrame + '#' + escape( JSON.stringify( resultObj ) )
537 - })
538 - .css({
539 - 'display': 'none'
540 - })
541 - );
 570+ '<iframe ' +
 571+ 'id="' + nestName + '" ' +
 572+ 'name="' + nestName + '" ' +
 573+ 'src="' + clientFrame + '#' + escape( JSON.stringify( msgObj ) ) + '" ' +
 574+ 'style="display:none" ' +
 575+ '></iframe>'
 576+ );
 577+
 578+ // After the nested frame is done loading schedule its removal
 579+ $j( '#' + nestName ).get( 0 ).onload = function() {
 580+ // use a settimeout to give time for client frame to propogate update.
 581+ setTimeout( function(){
 582+ $j('#' + nestName ).remove();
 583+ }, 10 );
 584+ }
542585 };
543586
544587 } )( window.mw.ApiProxy );
Index: branches/js2-work/phase3/js/apiProxyPage.js
@@ -24,15 +24,15 @@
2525
2626 // User white_list should also be checked and configured at runtime.
2727 mw.ready( function() {
 28+ // Build our configuration from the default and mwApiProxyConfig vars
 29+ mwApiProxyConfig = $j.extend( true, mwApiProxyDefaultConfig, mwApiProxyConfig );
 30+ mw.setConfig( 'apiProxyConfig', mwApiProxyConfig);
 31+
2832 //Do a setTimeout to 0 to call after other zero delay async events
2933 // ( once everyone is doing buildout withthin mwsetup priror to .ready this won't be needed. )
30 - mw.load( 'ApiProxy', function(){
31 -
 34+ mw.load( 'ApiProxy', function(){
3235 //Clear out the page content ( not needed for iframe proxy )
3336 $j( 'body' ).html( '' );
34 -
35 - // Build our configuration from the default and mwApiProxyConfig vars
36 - mwApiProxyConfig = $j.extend( true, mwApiProxyDefaultConfig, mwApiProxyConfig );
37 - mw.ApiProxy.server( mwApiProxyConfig );
 37+ mw.ApiProxy.server();
3838 });
3939 } );
Index: branches/js2-work/phase3/js/uploadPage.js
@@ -28,8 +28,10 @@
2929 'interface_type' : 'dialog',
3030
3131 'form_selector': mwUploadFormSelector,
32 - 'new_source_cb': function( orgFilename, oggName ) {
33 - $j( '#wpDestFile' ).val( oggName );
 32+
 33+ // Set the select file callback:
 34+ 'selectFileCb': function( fileName ) {
 35+ $j( '#wpDestFile' ).val( fileName );
3436 $j( '#wpDestFile' ).doDestCheck( {
3537 'warn_target': '#wpDestFile-warning'
3638 } );

Status & tagging log