r61693 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r61692‎ | r61693 | r61694 >
Date:22:37, 29 January 2010
Author:dale
Status:deferred (Comments)
Tags:
Comment:
* fixed abstraction of apiProxy so that it integrates with getJSON
* some jQuery based output of html
* fixed mw.getToken to be more consistent with mw.getJSON, $j.get etc
* updated testApiProxy.html
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.RemoteSearchDriver.js (modified) (history)
  • /branches/js2-work/phase3/js/mwEmbed/modules/ApiProxy/mw.proxy.js (modified) (history)
  • /branches/js2-work/phase3/js/mwEmbed/modules/Sequencer/mw.Sequencer.js (modified) (history)
  • /branches/js2-work/phase3/js/mwEmbed/modules/TimedText/mw.TimedTextEdit.js (modified) (history)
  • /branches/js2-work/phase3/js/mwEmbed/mwEmbed.js (modified) (history)
  • /branches/js2-work/phase3/js/mwEmbed/tests/testApiProxy.html (modified) (history)

Diff [purge]

Index: branches/js2-work/phase3/js/mwEmbed/tests/testApiProxy.html
@@ -9,23 +9,14 @@
1010 //HARD coded local test:
1111 var remote_wiki_host = 'http://test.wikipedia.org';
1212 var remote_script_path = '/w';
 13+var remote_api_url = remote_wiki_host + remote_script_path + '/api.php';
1314
1415 mw.ready( function(){
15 - $j('#hostName').text( remote_wiki_host );
16 - //run the api-proxy setup:
17 - $j.apiProxy(
18 - 'client',
19 - {
20 - 'server_frame': remote_wiki_host + remote_script_path + '/index.php/MediaWiki:ApiProxy',
21 - 'client_frame_path' : mw.getMwEmbedPath() + 'libMwApi/NestedCallbackIframe.html',
22 - },
23 - function(){
24 - //callback function here:
25 - $j('#setupDone').show('slow');
26 - $j('#doHello').click( doHello );
27 - $j('#doAppend').click(doAppend);
28 - }
29 - );
 16+ $j('#hostName').text( remote_wiki_host );
 17+ //callback function here:
 18+ $j('#setupDone').show('slow');
 19+ $j('#doHello').click( doHello );
 20+ $j('#doAppend').click( doAppend );
3021 });
3122 function doHello(){
3223 mw.log('to a hello user');
@@ -42,8 +33,7 @@
4334 'meta':'userinfo'
4435 }
4536 // Do proxy request
46 - mw.proxy.doRequest( rObj,
47 - function( data ){
 37+ mw.getJSON( remote_api_url, rObj, function( data ){
4838 //now we get the data back for that domain
4939 if( !data.query || !data.query.userinfo ){
5040 mw.log("Error no query.userinfo ");
@@ -58,10 +48,10 @@
5949 function doAppend(){
6050 $j('#appendTarget').html( mw.loading_spinner() );
6151 //get user name
62 - getUserName(function(userName){
 52+ getUserName( function(userName){
6353 var eTitle = 'User_talk:' + userName;
64 - //get a edit token
65 - mw.getToken( eTitle, 'proxy', function( token ){
 54+ // Get a edit token
 55+ mw.getToken( remote_api_url, eTitle, function( token ) {
6656 mw.log("got token: " + token) ;
6757 var request = {
6858 'action':'edit',
@@ -70,7 +60,7 @@
7161 'appendtext': "\n\n==== Hello from Api proxy At: " + Date() + " ====",
7262 'token': token
7363 }
74 - mw.getJSON('proxy', request, function( result ){
 64+ mw.getJSON( remote_api_url, request, function( result ){
7565 if(result.edit && result.edit.newrevid){
7666 $j('#appendTarget').html( "success: " + JSON.stringify ( result) );
7767 url = remote_wiki_host + remote_script_path + '/index.php/';
Index: branches/js2-work/phase3/js/mwEmbed/modules/TimedText/mw.TimedTextEdit.js
@@ -94,7 +94,8 @@
9595 $j('<a>')
9696 .attr( 'href', '#tab-' + edit_stage_id )
9797 .append(
98 - $j('<span style="float:left;">')
 98+ $j('<span />')
 99+ .css( "float","left" )
99100 .addClass( 'ui-icon ui-icon-' + editStage.icon )
100101 ,
101102 $j('<span>')
@@ -155,12 +156,16 @@
156157 // Check if user has XHR file upload support & we are on the target wiki
157158
158159 $target.append(
159 - $j('<div class="leftcolumn">')
 160+ $j('<div />')
 161+ .addClass( "leftcolumn" )
160162 .append('<h4>')
161163 .text( gM('mwe-upload-text') ),
162 - $j('<div class="rightcolumn">')
 164+ $j('<div />')
 165+ .addClass( 'rightcolumn' )
163166 .append(
164 - $j('<span id="timed-text-rightcolum-desc">').append(
 167+ $j( '<span />' )
 168+ .attr('id', "timed-text-rightcolum-desc")
 169+ .append(
165170 $j('<h4>')
166171 .text( gM('mwe-upload-text-desc-title') ),
167172 $j('<i>').text ( gM( 'mwe-upload-text-desc-help' ) ),
@@ -190,14 +195,25 @@
191196 'width':'300px',
192197 'float': 'left'
193198 }).append(
194 - '<input type="file" id="timed-text-file-upload"></input><br />'
 199+ $j('<input />')
 200+ .attr( {
 201+ 'type': "file",
 202+ 'id' : "timed-text-file-upload"
 203+ }),
 204+ $j('<br />')
195205 )
196206 )
197207
198208
199209 $target.append(
200210 //Get a little helper input filed to update the language
201 - $j('<input id="timed-text-langKey-input" type="text" maxlength="10" size="3">')
 211+ $j('<input />')
 212+ .attr( {
 213+ 'id' : "timed-text-langKey-input",
 214+ 'type' : "text",
 215+ 'maxlength' : "10",
 216+ 'size' :"3"
 217+ } )
202218 .change(function(){
203219 var langKey = $j(this).val();
204220 if( mw.languages[ langKey ] ){
@@ -277,7 +293,10 @@
278294
279295 //Add an upload button:
280296 $target.append(
281 - $j('<div style="clear: both"></div><br /><br />'),
 297+ $j('<div />')
 298+ .css('clear', 'both'),
 299+ $j('<br />'),
 300+ $j('<br />'),
282301 $j.button( {
283302 'style': { 'float' : 'left' },
284303 'text': gM('mwe-upload-text'),
Index: branches/js2-work/phase3/js/mwEmbed/modules/AddMedia/mw.Firefogg.js
@@ -907,7 +907,7 @@
908908 }
909909
910910 // No edit token. Fetch it asynchronously and then do the upload.
911 - mw.getToken( 'File:'+ _this.formData['filename'], _this.api_url, function( editToken ) {
 911+ mw.getToken( _this.api_url, 'File:'+ _this.formData['filename'], function( editToken ) {
912912 if( !editToken || editToken == '+\\' ) {
913913 _this.updateProgressWin( gM( 'fogg-badtoken' ), gM( 'fogg-badtoken' ) );
914914 return false;
Index: branches/js2-work/phase3/js/mwEmbed/modules/AddMedia/mw.RemoteSearchDriver.js
@@ -2397,7 +2397,7 @@
23982398 }
23992399 } );
24002400 // Get the edit token
2401 - _this.getEditToken( function( token ) {
 2401+ mw.getToken( _this.upload_api_target, function( token ) {
24022402 uploader.editToken = token;
24032403
24042404 // Close the loader now that we are ready to present the progress dialog::
@@ -2413,29 +2413,6 @@
24142414 },
24152415
24162416 /**
2417 - * get an edit Token
2418 - * depends on upload_api_target being initialized
2419 - *
2420 - * @param {Function} callback Function to be called once the token is available
2421 - */
2422 - getEditToken: function( callback ) {
2423 - var _this = this;
2424 - if ( _this.upload_api_target != 'proxy' ) {
2425 - // (if not a proxy) first try to get the token from the page:
2426 - var editToken = $j( "input[name='wpEditToken']" ).val();
2427 - if ( editToken ) {
2428 - callback( editToken );
2429 - return;
2430 - }
2431 - }
2432 - // @@todo try to load over ajax if( _this.local_wiki_api_url ) is set
2433 - // ( for cases where inserting from a normal page view (that did not have wpEditToken)
2434 - mw.getToken( _this.upload_api_target, function( token ) {
2435 - callback( token );
2436 - } );
2437 - },
2438 -
2439 - /**
24402417 * Shows a preview of the given resource
24412418 */
24422419 showPreview: function( resource ) {
Index: branches/js2-work/phase3/js/mwEmbed/modules/AddMedia/jquery.simpleUploadForm.js
@@ -46,7 +46,7 @@
4747 }
4848
4949 // Get an edit Token for "uploading"
50 - mw.getToken( 'File:MyRandomFileTokenCheck.jpg', options.api_target, function( eToken ) {
 50+ mw.getToken( options.api_target, 'File:MyRandomFileTokenCheck.jpg', function( eToken ) {
5151 if ( !eToken || eToken == '+\\' ) {
5252 $( _this.selector ).html( gM( 'mwe-error_not_loggedin' ) );
5353 return false;
Index: branches/js2-work/phase3/js/mwEmbed/modules/Sequencer/mw.Sequencer.js
@@ -537,19 +537,13 @@
538538 this.sequenceEditToken = $j( 'input[wpEditToken]' ).val();
539539
540540 if ( typeof this.sequenceEditToken == 'undefined' && this.getLocalApiUrl() != null ) {
541 - mw.getToken( _this.plObj.mTitle, _this.getLocalApiUrl(),
542 - function( token ) {
 541+ mw.getToken( _this.getLocalApiUrl(), _this.plObj.mTitle, function( token ) {
543542 if ( token ) {
544543 _this.sequenceEditToken = token;
545544 _this.updateSeqSaveButtons();
546545 }
547 - }
548 - );
549 - get_mw_token( _this.plObj.mTalk, _this.getLocalApiUrl(),
550 - function( token ) {
551 - _this.clipboardEditToken = token;
552 - }
553 - );
 546+ } );
 547+
554548 // also grab permissions for sending clipboard commands to the server
555549
556550 // (calling the sequencer inline) try and get edit token via api call:
Index: branches/js2-work/phase3/js/mwEmbed/modules/ApiProxy/mw.proxy.js
@@ -1,4 +1,4 @@
2 -/*
 2+/**
33 *
44 * Api proxy system
55 *
@@ -55,19 +55,21 @@
5656 * @param {Object} proxyConfig Proxy configuration
5757 * @param {Function} callback The function called once proxy request is done
5858 */
59 -$.apiProxy = function( mode, proxyConfig, callback ) {
60 - mw.log( 'do apiProxy setup' );
61 - // do the proxy setup or
62 - if ( mode == 'client' ) {
63 - // just do the setup (no callbcak for client setup)
64 - mw.proxy.client( proxyConfig );
65 - if ( callback )
66 - callback();
67 - } else if ( mode == 'server' ) {
68 - // Do the request with the callback
69 - mw.proxy.server( proxyConfig , callback );
 59+( function( $ ) {
 60+ $.apiProxy = function( mode, proxyConfig, callback ) {
 61+ mw.log( 'do apiProxy setup' );
 62+ // do the proxy setup or
 63+ if ( mode == 'client' ) {
 64+ // just do the setup (no callbcak for client setup)
 65+ mw.proxy.client( proxyConfig );
 66+ if ( callback )
 67+ callback();
 68+ } else if ( mode == 'server' ) {
 69+ // Do the request with the callback
 70+ mw.proxy.server( proxyConfig , callback );
 71+ }
7072 }
71 -}
 73+} )( jQuery );
7274
7375 ( function( $ ) {
7476
@@ -91,9 +93,8 @@
9294 if ( proxyConfig.client_frame_path ) {
9395 $.proxy.client_frame_path = proxyConfig.client_frame_path;
9496 } else {
95 - // Set to default via mediaWiki vars:
96 -
97 - $.proxy.client_frame_path = wgServer + wgScriptPath + '/js/mwEmbed/libMwApi/NestedCallbackIframe.html';
 97+ // Set to default via mediaWiki vars:
 98+ $.proxy.client_frame_path = wgServer + wgScriptPath + '/js/mwEmbed/modules/ApiProxy/NestedCallbackIframe.html';
9899 }
99100
100101 if ( mw.isLocalDomain( $.proxy.server_frame ) ) {
@@ -190,14 +191,14 @@
191192
192193 /**
193194 * The nested iframe action that passes its result back up to the top frame instance
194 - *
 195+ *
195196 * Entry point for hashResult from nested iframe
196197 *
197198 * @param {Object} hashResult Value to be sent to parent frame
198199 */
199200 $.proxy.nested = function( hashResult ) {
200201 // Close the loader if present:
201 - $j.closeLoaderDialog();
 202+ mw.closeLoaderDialog();
202203 mw.log( '$.proxy.nested callback :: ' + unescape( hashResult ) );
203204 frameProxyOk = true;
204205
@@ -217,7 +218,6 @@
218219 $.proxy.callback( resultObject );
219220 }
220221
221 -
222222
223223
224224
@@ -250,12 +250,13 @@
251251 /**
252252 * Outputs the result object to the client domain
253253 *
254 - * @param {String} nestName Name of iframe
 254+ * @param {clientFrame} clientFrame Client frame name
 255+ * @param {String} nestName Name of iframe
255256 * @param {resultObj} the result to pass back to the client domain
256257 */
257258 function outputResultsFrame( clientFrame, nestName, resultObj ) {
258259 $j( '#nested_push' ).remove();
259 - // Setup the nested iframe proxy that points back to top domain:
 260+ // Setup the nested iframe proxy that points back to top domain:
260261 $j( 'body' ).append(
261262 $j('<iframe>').attr({
262263 'id' : nestName,
@@ -308,7 +309,14 @@
309310
310311 /**
311312 * HERE WE CHECK IF THE DOMAIN IS ALLOWED per the proxyConfig
312 - */
 313+ */
 314+ // Check master blacklist
 315+ for ( var i in proxyConfig.master_blacklist ) {
 316+ if ( clientDomain == proxyConfig.master_blacklist ) {
 317+ mw.log( 'domain: ' + clientDomain + ' is blacklisted ( no request )' );
 318+ return false;
 319+ }
 320+ }
313321 // Check the master whitelist:
314322 for ( var i in proxyConfig.master_whitelist ) {
315323 if ( clientDomain == proxyConfig.master_whitelist[ i ] ) {
@@ -316,14 +324,7 @@
317325 return doApiRequest( clientRequest );
318326 }
319327 }
320 -
321 - // Check master blacklist
322 - for ( var i in proxyConfig.master_blacklist ) {
323 - if ( clientDomain == proxyConfig.master_blacklist ) {
324 - mw.log( 'domain: ' + clientDomain + ' is blacklisted ( no request )' );
325 - return false;
326 - }
327 - }
 328+
328329 // FIXME Add in user based approval ::
329330
330331 // offer the user the ability to "approve" requested domain save to
Index: branches/js2-work/phase3/js/mwEmbed/mwEmbed.js
@@ -1360,19 +1360,18 @@
13611361
13621362 /**
13631363 * mediaWiki JSON a wrapper for jQuery getJSON:
1364 - * $j.getJSON( url, [data], [callback] )
13651364 *
13661365 * The mediaWiki version lets you skip the url part
13671366 * mw.getJSON( [url], data, callback );
13681367 *
1369 - * Lets you assume a few things:
 1368+ * Lets you assume:
13701369 * url is optional
13711370 * ( If the first argument is not a string we assume a local mediaWiki api request )
13721371 * callback parameter is not needed we setup the callback automatically
13731372 * url param 'action'=>'query' is assumed ( if not set to something else in the "data" param
13741373 * format is set to "json" automatically
13751374 * automatically issues request over "POST" if action={postActions}
1376 - * ~soon~ will setup apiProxy where needed.
 1375+ * automatically will setup apiProxy where needed.
13771376 *
13781377 * @param {Mixed} url or data request
13791378 * @param {Mixed} data or callback
@@ -1396,8 +1395,7 @@
13971396 callback = ( typeof arg2 == 'function') ? arg2 : false;
13981397 if( ! callback && ( typeof arg3 == 'function') ){
13991398 callback = arg3;
1400 - }
1401 -
 1399+ }
14021400
14031401 // Make sure we got a url:
14041402 if( !url ){
@@ -1406,26 +1404,44 @@
14071405 }
14081406
14091407 // Add default action if unset:
1410 - if( !data['action'] )
 1408+ if( !data['action'] ){
14111409 data['action'] = 'query';
 1410+ }
14121411
14131412 // Add default format if not set:
1414 - if( !data['format'] )
 1413+ if( !data['format'] ){
14151414 data['format'] = 'json';
 1415+ }
14161416
1417 - mw.log("run getJSON: " + mw.replaceUrlParams(url, data ) );
1418 -
1419 - // Check if we need to setup proxy or do the request as a "post"
1420 - if( $j.inArray( data['action'], mw.getConfig( 'apiPostActions' ) ) != -1 ){
 1417+ mw.log("run getJSON: " + mw.replaceUrlParams( url, data ) );
 1418+ // Check if the request requires a "post" (that does not work with callbacks cross domain)
 1419+ if( mw.checkRequestPost( data ) ){
 1420+ // Check if we need to setup a proxy
14211421 if( ! mw.isLocalDomain( url ) ){
1422 - mw.log( "Error:: Do setup proxy here" );
1423 - return ;
 1422+ // Load the proxy and issue the request
 1423+ mw.load( 'ApiProxy', function(){
 1424+ var parsedUrl = mw.parseUri( url );
 1425+ var server_frame = parsedUrl.protocol + '://' + parsedUrl.authority + '/w/index.php/MediaWiki:ApiProxy';
 1426+ $j.apiProxy(
 1427+ 'client',
 1428+ {
 1429+ // NOTE:: we need to setup a special page rather than the rewrite hack I use bellow:
 1430+ // The rewrite hack is very slow cuz it loads the whole mediaWiki interface & skin
 1431+ 'server_frame' : server_frame,
 1432+ 'client_frame_path' : mw.getMwEmbedPath() + 'modules/ApiProxy/NestedCallbackIframe.html',
 1433+ },
 1434+ function(){
 1435+ mw.proxy.doRequest( data, callback )
 1436+ }
 1437+ )
 1438+ });
14241439 }else{
1425 - $j.post( url, data, callback, 'json');
1426 - return ;
 1440+ // Do the request an ajax post
 1441+ $j.post( url, data, callback, 'json');
14271442 }
 1443+ return ;
14281444 }
1429 - //If cross domain setup a callback:
 1445+ // If cross domain setup a callback:
14301446 if( ! mw.isLocalDomain( url ) ){
14311447 if( url.indexOf( 'callback=' ) == -1 || data[ 'callback' ] == -1 ){
14321448 // jQuery specific: ( second ? is replaced with the callback )
@@ -1434,9 +1450,29 @@
14351451 }
14361452 // Pass off the jQuery getJSON request:
14371453 $j.getJSON( url, data, callback );
1438 - }
 1454+ }
14391455
14401456 /**
 1457+ * Checks if a mw request data requires a proxy or not
 1458+ * @param {Object}
 1459+ * @return {Boolean}
 1460+ * true if the request requires a proxy
 1461+ * false if the request does not
 1462+ */
 1463+ mw.checkRequestPost = function ( data ){
 1464+ if( $j.inArray( data['action'], mw.getConfig( 'apiPostActions' ) ) != -1 ){
 1465+ return true;
 1466+ }
 1467+ if( data['prop'] == 'info' && data['intoken'] ){
 1468+ return true;
 1469+ }
 1470+ if( data['meta'] = 'userinfo' ){
 1471+ return true;
 1472+ }
 1473+ return false;
 1474+ }
 1475+
 1476+ /**
14411477 * Metavid specific roe request helper function
14421478 *
14431479 * NOTE: depreciated, will be removed once standard callback is
@@ -1477,29 +1513,20 @@
14781514 /**
14791515 * Simple api helper to grab an edit token
14801516 *
1481 - * @param {String} [title] The wiki page title you want to edit
1482 - * @param {String} [api_url] The target API URL
 1517+ * @param {String} [api_url] Optional target API URL (uses default local api if unset)
 1518+ * @param {String} title The wiki page title you want to edit
14831519 * @param {callback} callback Function to pass the token to
14841520 */
1485 - mw.getToken = function( title, api_url, callback ) {
1486 - if( typeof api_url == 'function' )
1487 - callback = api_url;
1488 - if( typeof title == 'function')
 1521+ mw.getToken = function( api_url, title, callback ) {
 1522+ // Make the api_url be optional:
 1523+ if( typeof title == 'function' ){
14891524 callback = title;
 1525+ title = api_url;
 1526+ api_url = mw.getLocalApiUrl();
 1527+ }
14901528
1491 - mw.log( 'mw:getToken' );
 1529+ mw.log( 'mw:getToken' );
14921530
1493 - // If no title is provided get a token for the user page:
1494 - if ( typeof title != 'string' ) {
1495 - if( typeof wgUserName != 'undefined' && wgUserName ){
1496 - title = 'User:' + wgUserName;
1497 - }else{
1498 - // Try maintalk page:
1499 - title = 'Talk:Main_Page';
1500 - }
1501 - }
1502 -
1503 -
15041531 var request = {
15051532 'prop': 'info',
15061533 'intoken': 'edit',
@@ -1547,7 +1574,7 @@
15481575 * @param {Mixed} buttons A button object for the dialog
15491576 * Can be 'ok' for oky button.
15501577 */
1551 - mw.addDialog = function ( title, msg_txt, buttons ) {
 1578+ mw.addDialog = function ( title, msg_html, buttons ) {
15521579 $j( '#mwe_tmp_loader' ).remove();
15531580 // Append the style free loader ontop:
15541581 $j( 'body' ).append(
@@ -1557,7 +1584,7 @@
15581585 'title' : title
15591586 })
15601587 .css('display', 'none')
1561 - .text( msg_text )
 1588+ .html( msg_html )
15621589 );
15631590 // Special buttons == ok gives empty give a single "oky" -> "close"
15641591 if ( buttons == 'ok' ) {
@@ -2644,14 +2671,8 @@
26452672 'right':'0px',
26462673 'bottom':'0px'
26472674 } );
2648 - }
2649 -
2650 - $.mwProxy = function( apiConf ) {
2651 - mw.load( ['mw.apiProxy'],
2652 - function() {
2653 - mw.apiProxy( apiConf );
2654 - } );
2655 - }
 2675+ }
 2676+
26562677 } )( jQuery );
26572678 }
26582679
Index: branches/js2-work/phase3/js/apiProxyPage.js
@@ -22,9 +22,9 @@
2323
2424 // User white_list should also be checked and configured at runtime.
2525 mw.ready( function() {
26 -
27 - // Build our configuration from the default and mwApiProxyConfig vars
28 - mwApiProxyConfig = $j.extend( true, mwApiProxyDefaultConfig, mwApiProxyConfig );
29 - $j.apiProxy( 'server', mwApiProxyConfig );
30 -
 26+ mw.load( 'ApiProxy', function(){
 27+ // Build our configuration from the default and mwApiProxyConfig vars
 28+ mwApiProxyConfig = $j.extend( true, mwApiProxyDefaultConfig, mwApiProxyConfig );
 29+ $j.apiProxy( 'server', mwApiProxyConfig );
 30+ });
3131 } );

Comments

#Comment by Spamsplace (talk | contribs)   09:20, 31 January 2010

This seems to break functionality, both for the Add_Media_Wizard test and when using mwEmbed from a local mediawiki setup. It seems the control flow in mwEmbed.js is altered for the following block:

if( mw.checkRequestPost( data )  )

Currently I am commenting this out to restore functionality.

(If it is intended that this control block be accessed, then there is probably a problem with the ApiProxy.)

#Comment by Mdale (talk | contribs)   22:05, 31 January 2010

opps. Hastily commit had a broken proxy check. fixed in r61758

Status & tagging log