From: Michael Dale Date: Wed, 7 Oct 2009 21:42:40 +0000 (+0000) Subject: * improved plural support (still not quite there) X-Git-Tag: 1.31.0-rc.0~39351 X-Git-Url: http://git.cyclocoop.org/%24action?a=commitdiff_plain;h=158cd63833a1ba89dce77c7eecc71a1db47ab695;p=lhc%2Fweb%2Fwiklou.git * improved plural support (still not quite there) * refactoring of add-media-wizard code (removed non-upload-api code support) --- diff --git a/js2/mwEmbed/libAddMedia/remoteSearchDriver.js b/js2/mwEmbed/libAddMedia/remoteSearchDriver.js index c87bcebce8..115698d4fb 100644 --- a/js2/mwEmbed/libAddMedia/remoteSearchDriver.js +++ b/js2/mwEmbed/libAddMedia/remoteSearchDriver.js @@ -58,7 +58,7 @@ var default_remote_search_options = { 'caret_pos':null, 'local_wiki_api_url':null, - //can be 'api', 'form', 'autodetect', 'remote_link' + //can be 'api', 'autodetect', 'remote_link' 'import_url_mode': 'autodetect', 'target_title':null, @@ -695,52 +695,25 @@ remoteSearchDriver.prototype = { do_api_req( { 'data': { 'action':'paraminfo', 'modules':'upload' }, 'url': _this.local_wiki_api_url - }, function(data){ - if( typeof data.paraminfo.modules[0].classname == 'undefined'){ - //@@todo would be nice if API permission on: action=query&meta=userinfo&uiprop=rights - // upload_by_url property reflected if $wgAllowCopyUploads config value .. oh well. - $j.ajax({ - type: "GET", - dataType: 'html', - url: wgArticlePath.replace( '$1', 'Special:Upload' ), //@@todo may have problems in localized special pages - //(could hit meta=siteinfo & specialpagealiases ) - // but might be overkill for now cuz we want to switch to new-upload branch soon. - success: function( form_html ){ - if( form_html.indexOf( 'wpUploadFileURL' ) != -1){ - _this.import_url_mode = 'form'; + }, function(data){ + //jump right into api checks: + for( var i in data.paraminfo.modules[0].parameters ){ + var pname = data.paraminfo.modules[0].parameters[i].name; + if( pname == 'url' ){ + js_log( 'Autodetect Upload Mode: api: copy by url:: ' ); + //check permission too: + _this.checkForCopyURLPermission(function( canCopyUrl ){ + if(canCopyUrl){ + _this.import_url_mode = 'api'; + js_log('import mode: ' + _this.import_url_mode); + callback(); }else{ _this.import_url_mode = 'none'; + js_log('import mode: ' + _this.import_url_mode); + callback(); } - js_log('import mode: ' + _this.import_url_mode); - callback(); - }, - error: function(){ - js_log('error in getting Special:Upload page'); - _this.import_url_mode = 'none'; - - js_log('import mode: ' + _this.import_url_mode); - callback(); - } - }); - }else{ - for( var i in data.paraminfo.modules[0].parameters ){ - var pname = data.paraminfo.modules[0].parameters[i].name; - if( pname == 'url' ){ - js_log( 'Autodetect Upload Mode: api: copy by url:: ' ); - //check permission too: - _this.checkForCopyURLPermission(function( canCopyUrl ){ - if(canCopyUrl){ - _this.import_url_mode = 'api'; - js_log('import mode: ' + _this.import_url_mode); - callback(); - }else{ - _this.import_url_mode = 'none'; - js_log('import mode: ' + _this.import_url_mode); - callback(); - } - }); - break; - } + }); + break; } } }); @@ -748,7 +721,7 @@ remoteSearchDriver.prototype = { }, /* * checkForCopyURLPermission: - * not really nessesary the api request to upload will return apopprirate error if the user lacks permission. or $wgAllowCopyUploads is set to false + * not really nesesary the api request to upload will return apoprirate error if the user lacks permission. or $wgAllowCopyUploads is set to false * (use this function if we want to issue a warning up front) */ checkForCopyURLPermission:function( callback ){ @@ -833,7 +806,7 @@ remoteSearchDriver.prototype = { if( loading_done ){ this.drawOutputResults(); }else{ - //make sure the instance name is up-to-date refrence to _this; + //make sure the instance name is up-to-date refrerance to _this; eval( _this.instance_name + ' = _this'); setTimeout( _this.instance_name + '.checkResultsDone()', 50); } @@ -1277,153 +1250,161 @@ remoteSearchDriver.prototype = { rObj.target_resource_title = rObj.titleKey.replace(/File:|Image:/,'') //check if local repository - //or if import mode if just "linking" (we should alaredy have the 'url' + //or if import mode if just "linking" (we should already have the 'url' if( this.checkRepoLocal( cp ) || this.import_url_mode == 'remote_link'){ //local repo jump directly to check Import Resource callback: cir_callback( rObj ); - }else{ + }else{ + //check if the orginal + _this.checkForFile( //update target_resource_title with resource repository prefix: rObj.target_resource_title = cp.resource_prefix + rObj.target_resource_title; + reqObj['titles'] = _this.cFileNS + ':' + rObj.target_resource_title, + //check if the file exists: + _this.checkForFile( rObj.target_resource_title, function( imagePage ){ + if( imagePage ){ + //update to local src + rObj.local_src = imagePage['imageinfo'][0].url; + //@@todo maybe update poster too? + rObj.local_poster = imagePage['imageinfo'][0].thumburl; + + //resource is already present (or resource with same name is already present) + rObj.target_resource_title = found_title.replace(/File:|Image:/,''); + cir_callback( rObj ); + }else{ + js_log("resource not present: update:"+ _this.cFileNS + ':' + rObj.target_resource_title); - //check if the resource is not already on this wiki - reqObj={ - 'action':'query', - 'titles': _this.cFileNS + ':' + rObj.target_resource_title, - 'prop' : 'imageinfo', - 'iiprop' : 'url', - 'iiurlwidth': '400' - }; - - do_api_req( { - 'data':reqObj, - 'url':this.local_wiki_api_url - }, function(data){ - var found_title = false; - for(var i in data.query.pages){ - if( i != '-1' && i != '-2' ){ - js_log('found title: ' + i + ':' + data.query.pages[i]['title']); - found_title=data.query.pages[i]['title']; - //update to local src - rObj.local_src = data.query.pages[i]['imageinfo'][0].url; - //@@todo maybe update poster too? - rObj.local_poster = data.query.pages[i]['imageinfo'][0].thumburl; - } - } - if( found_title ){ - js_log("checkImportResource:found title:" + found_title); - //resource is already present (or resource with same name is already present) - rObj.target_resource_title = found_title.replace(/File:|Image:/,''); - cir_callback( rObj ); - }else{ - js_log("resource not present: update:"+ _this.cFileNS + ':' + rObj.target_resource_title); - - //update the rObj with import info - rObj.pSobj.updateDataForImport( rObj ); - - //setup the resource description from resource description: - var wt = '{{Information '+"\n"; - - if( rObj.desc ){ - wt += '|Description= ' + rObj.desc + "\n"; - }else{ - wt += '|Description= ' + gM('mwe-missing_desc_see_source', rObj.link ) + "\n"; - } - - //output search specific info - wt+='|Source=' + rObj.pSobj.getImportResourceDescWiki( rObj ) + "\n"; - - if( rObj.author ) - wt+='|Author=' + rObj.author +"\n"; + //update the rObj with import info + rObj.pSobj.updateDataForImport( rObj ); - if( rObj.date ) - wt+='|Date=' + rObj.date +"\n"; + //setup the resource description from resource description: + var wt = '{{Information '+"\n"; - //add the Permision info: - wt+='|Permission=' + rObj.pSobj.getPermissionWikiTag( rObj ) +"\n"; + if( rObj.desc ){ + wt += '|Description= ' + rObj.desc + "\n"; + }else{ + wt += '|Description= ' + gM('mwe-missing_desc_see_source', rObj.link ) + "\n"; + } - if( rObj.other_versions ) - wt+='|other_versions=' + rObj.other_versions + "\n"; + //output search specific info + wt+='|Source=' + rObj.pSobj.getImportResourceDescWiki( rObj ) + "\n"; - wt+='}}'; + if( rObj.author ) + wt+='|Author=' + rObj.author +"\n"; - //get any extra categories or helpful links - wt+= rObj.pSobj.getExtraResourceDescWiki( rObj ); + if( rObj.date ) + wt+='|Date=' + rObj.date +"\n"; + //add the Permision info: + wt+='|Permission=' + rObj.pSobj.getPermissionWikiTag( rObj ) +"\n"; - $j('#rsd_resource_import').remove();//remove any old resource imports + if( rObj.other_versions ) + wt+='|other_versions=' + rObj.other_versions + "\n"; - //@@ show user dialog to import the resource - $j( _this.target_container ).append('
' + - '

Resource: ' + rObj.title + ' needs to be imported

'+ - '
' + - rObj.pSobj.getEmbedHTML( rObj, {'id': _this.target_container + '_rsd_pv_vid', 'max_height':'200','only_poster':true} )+ //get embedHTML with small thumb: - '
'+ - ''+gM('mwe-resource_page_desc') +''+ - '
'+ - mv_get_loading_img('position:absolute;top:5px;left:5px') + - '
'+ - '
'+ - '
'+ - '' + gM('mwe-local_resource_title') + '
'+ - '
'+ - '' + gM('mwe-edit_resource_desc') + '' + - '
' + - '' + - '


' + + wt+='}}'; - $j.btnHtml(gM('mwe-do_import_resource'), 'rsd_import_doimport', 'check' ) + ' ' + + //get any extra categories or helpful links + wt+= rObj.pSobj.getExtraResourceDescWiki( rObj ); - $j.btnHtml(gM('mwe-update_preview'), 'rsd_import_apreview', 'refresh' ) + '
' + - $j.btnHtml(gM('mwe-cancel_import'), 'rsd_import_acancel', 'close' ) + ' ' + + $j('#rsd_resource_import').remove();//remove any old resource imports + //@@ show user dialog to import the resource + $j( _this.target_container ).append('
' + + '

Resource: ' + rObj.title + ' needs to be imported

'+ + '
' + + rObj.pSobj.getEmbedHTML( rObj, {'id': _this.target_container + '_rsd_pv_vid', 'max_height':'200','only_poster':true} )+ //get embedHTML with small thumb: + '
'+ + ''+gM('mwe-resource_page_desc') +''+ + '
'+ + mv_get_loading_img('position:absolute;top:5px;left:5px') + '
'+ - //output the rendered and non-renderd version of description for easy swiching: - '
'); - //add hover: - //update video tag - rewrite_by_id(_this.target_container + '_rsd_pv_vid'); + '
'+ + '
'+ + '' + gM('mwe-local_resource_title') + '
'+ + '
'+ + '' + gM('mwe-edit_resource_desc') + '' + + '
' + + '' + + '


' + + + $j.btnHtml(gM('mwe-do_import_resource'), 'rsd_import_doimport', 'check' ) + ' ' + + + $j.btnHtml(gM('mwe-update_preview'), 'rsd_import_apreview', 'refresh' ) + '
' + + + $j.btnHtml(gM('mwe-cancel_import'), 'rsd_import_acancel', 'close' ) + ' ' + + + '
'+ + //output the rendered and non-renderd version of description for easy swiching: + '
'); + //add hover: + //update video tag + rewrite_by_id(_this.target_container + '_rsd_pv_vid'); + //load the preview text: + _this.getParsedWikiText( wt, _this.cFileNS +':'+ rObj.target_resource_title, function( o ){ + $j('#rsd_import_desc').html(o); + }); + //add bindings: + $j( _this.target_container + ' .rsd_import_apreview').btnBind().click(function(){ + /*$j('#rsd_import_desc').show().html( + mv_get_loading_img() + );*/ //load the preview text: - _this.getParsedWikiText( wt, _this.cFileNS +':'+ rObj.target_resource_title, function( o ){ + _this.getParsedWikiText( $j('#rsd_import_ta').val(), _this.cFileNS +':'+ rObj.target_resource_title, function( o ){ + js_log('got updated preivew: '); $j('#rsd_import_desc').html(o); }); - //add bidings: - $j( _this.target_container + ' .rsd_import_apreview').btnBind().click(function(){ - /*$j('#rsd_import_desc').show().html( - mv_get_loading_img() - );*/ - //load the preview text: - _this.getParsedWikiText( $j('#rsd_import_ta').val(), _this.cFileNS +':'+ rObj.target_resource_title, function( o ){ - js_log('got updated preivew: '); - $j('#rsd_import_desc').html(o); - }); - }); - $j(_this.target_container + ' .rsd_import_doimport').btnBind().click(function(){ - //check import mode: - if(_this.import_url_mode=='form'){ - _this.doImportSpecialPage( rObj, cir_callback ); - }else if( _this.import_url_mode=='api'){ - _this.doImportAPI( rObj , cir_callback); - }else{ - js_log("Error: import mode is not form or API (can not copy asset)"); - } - }); - $j( _this.target_container + ' .rsd_import_acancel').btnBind().click(function(){ - $j('#rsd_resource_import').fadeOut("fast",function(){ - $j(this).remove(); - }); + }); + $j(_this.target_container + ' .rsd_import_doimport').btnBind().click(function(){ + //check import mode: + if( _this.import_url_mode=='api'){ + _this.doImportAPI( rObj , cir_callback); + }else{ + js_log("Error: import mode is not form or API (can not copy asset)"); + } + }); + $j( _this.target_container + ' .rsd_import_acancel').btnBind().click(function(){ + $j('#rsd_resource_import').fadeOut("fast",function(){ + $j(this).remove(); }); - } + }); } ); } }, + checkForFile:function( fName, callback){ + reqObj={ + 'action':'query', + 'titles': _this.cFileNS + ':' + fName, + 'prop' : 'imageinfo', + 'iiprop' : 'url', + 'iiurlwidth': '400' + }; + //first check the api for imagerepository + do_api_req( { + 'data':reqObj, + 'url':this.local_wiki_api_url + },function(data){ + var found_title = false; + for(var i in data.query.pages){ + js_log('found title: ' + i + ':' + data.query.pages[i]['title']); + found_title=data.query.pages[i]['title']; + } + if( found_title ){ + js_log("checkForFile found title:" + found_title); + callback( data.query.pages[i] ); + }else{ + callback( false ); + } + } + ); + }, doImportAPI:function(rObj, cir_callback){ var _this = this; //baseUploadInterface @@ -1431,7 +1412,7 @@ remoteSearchDriver.prototype = { 'mvBaseUploadInterface', '$j.ui.progressbar' ],function(){ - //initicate a download similar to url copy: + //initiate a download similar to url copy: myUp = new mvBaseUploadInterface({ 'api_url' : _this.local_wiki_api_url, 'done_upload_cb':function(){ @@ -1473,95 +1454,7 @@ remoteSearchDriver.prototype = { } callback(false); return false; - }, - /** - * doImportSpecialPage - * can be depricated once we support upload api support is widespred. - */ - doImportSpecialPage:function(rObj, cir_callback){ - var _this = this; - //get an edittoken: - do_api_req( { - 'data': { 'action':'query', - 'prop':'info', - 'intoken':'edit', - 'titles': rObj.titleKey - }, - 'url':_this.local_wiki_api_url - }, function(data){ - //could recheck if it has been created in the mean time - if( data.query.pages[-1] ){ - var editToken = data.query.pages[-1]['edittoken']; - if(!editToken){ - //@@todo give an ajax login or be more friendly in some way: - js_error("You don't have permission to upload (are you logged in?)"); - //remove top level: - $j('#modalbox').fadeOut("normal",function(){ - $j(this).remove(); - $j('#mv_overlay').remove(); - }); - }else{ - //not sure if we can do remote url uploads (so just do a local post) - js_log('got token for new page:' +editToken); - var postVars = { - 'wpSourceType' :'web', - 'wpUploadFileURL' : rObj.src, - 'wpDestFile' : rObj.target_resource_title, - 'wpUploadDescription' : $j('#rsd_import_ta').val(), - 'wpWatchthis' : $j('#wpWatchthis').val(), - 'wpUpload' : 'Upload file' - } - //set to uploading: - $j('#rsd_resource_import').append('
'+ - '

'+gM('mwe-importing_asset')+'


' + - mv_get_loading_img('','mv_loading_bar_img') + - '
'+ - '
' - ); - $j.post(wgArticlePath.replace(/\$1/,'Special:Upload'), - postVars, - function(data){ - //@@todo this will be replaced once we add upload image support to the api. - - //very basic test to see if we got passed to the image page: - //@@todo more normalization stuff - var sstring ='var wgPageName = "' + _this.cFileNS + ':' + rObj.target_resource_title.replace(/ /g,'_') +'"'; - if(data.indexOf( sstring ) !=-1){ - js_log('found: ' + sstring); - $j('#rsd_resource_import').remove(); - cir_callback( rObj ); - }else{ - js_log("Error or warning: (did not find: \"" + sstring + ' in output' ); - pos_etitle = '

'; - var error_txt = form_txt = ''; - var res = grabWikiFormError( data ); - - if( res.error_txt ) - error_txt = res.error_txt; - - if( res.form_txt ) - form_txt = res.form_txt; - - js_log( 'error text is: ' + error_txt ); - $j( '#rsd_resource_import' ).html( '

Error

' + error_txt + '
' + form_txt + - '
'+ - '' + gM('mwe-cancel_import') + '' - ); - //set up cancel action: - $j('#rsd_import_error').click(function(){ - $j('#rsd_resource_import').remove(); - }); - } - } - ); - } - } - } - ); - }, + }, previewResource:function( rObj ){ var _this = this; this.checkImportResource( rObj, function(){ @@ -1640,7 +1533,8 @@ remoteSearchDriver.prototype = { ); }, insertResource:function( rObj){ - js_log('insertResource: ' + rObj.title); + js_log('insertResource: ' + rObj.title); + var _this = this //dobule check that the resource is present: this.checkImportResource( rObj, function(){ diff --git a/js2/mwEmbed/mv_embed.js b/js2/mwEmbed/mv_embed.js index 9a1b12efbd..d3f732b21c 100644 --- a/js2/mwEmbed/mv_embed.js +++ b/js2/mwEmbed/mv_embed.js @@ -150,11 +150,14 @@ if( !mv_embed_path ) { return '<' + key + '>';// Missing key placeholder //swap in the arg values - var ms = $.lang.gMsgSwap( key, args); + var ms = $.lang.gMsgSwap( key, args) ; + //a quick check to see if we need to send the msg via the 'parser' //(we can add more detailed check once we support more wiki syntax) - if(ms.indexOf('{{')==-1) + if(ms.indexOf('{{')==-1){ return ms; + //return ms; + } //make sure we have the lagMagic setup: $.lang.magicSetup(); @@ -229,9 +232,54 @@ if( !mv_embed_path ) { */ function matchRuleTest(cRule, val){ js_log("matchRuleTest:: " + typeof cRule + ' ' + cRule + ' == ' + val ); + + function checkValue(compare, val){ + if(typeof compare == 'string'){ + range = compare.split('-'); + if( range.length >= 1 ){ + if( val >= range[0] && val <= range[1] ) + return true; + } + } + //else do a direct compare + if(compare == val){ + return true; + } + return false; + } //check for simple cRule type: if( typeof cRule == 'number'){ return ( parseInt( val ) == parseInt( cRule) ); + }else if( typeof cRule == 'object' ){ + var cmatch = {}; + //if a list we need to match all for rule match + for(var i in cRule){ + var cr = cRule[i]; + //set cr type + var crType = ''; + for( var j in cr ){ + if(j == 'mod') + crType = 'mod' + } + switch(crType){ + case 'mod': + if( cr ['is'] ){ + if( checkValue( val % cr['mod'], cr ['is'] ) ) + cmatch[i] = true; + }else if( cr['not']){ + if( ! checkValue( val % cr['mod'], cr ['not'] ) ) + cmatch[i] = true; + } + break; + } + } + //check all the matches (taking into consideration "or" order) + for(var i in cRule){ + if( ! cmatch[i] ) + return false; + } + return true; + } } /** diff --git a/js2/mwEmbed/tests/testLang.html b/js2/mwEmbed/tests/testLang.html index 12d8dc1ab9..5e9bf050c3 100644 --- a/js2/mwEmbed/tests/testLang.html +++ b/js2/mwEmbed/tests/testLang.html @@ -16,6 +16,7 @@ js2AddOnloadHook( function(){ var msgTestSet = { 'test_plural_msg':[ 0, 1, 2, 5, 21, 30 ], + 'undelete_short': [ 0, 1, 2, 5, 21, 30 ], //category-subcat-count' has two params: @@ -23,6 +24,7 @@ js2AddOnloadHook( function(){ [0,10], [1,2], [3,30] ] + }; var passTest=0; diff --git a/js2/mwEmbed/tests/testLangEn.js b/js2/mwEmbed/tests/testLangEn.js index 711da44b44..ca575d0f45 100644 --- a/js2/mwEmbed/tests/testLangEn.js +++ b/js2/mwEmbed/tests/testLangEn.js @@ -1,15 +1,16 @@ /* a simple language tester replacements - */ loadGM({ - //test msg with english words to see whats going on + //test msg with English words to see whats going on 'test_plural_msg' : '{{PLURAL:$1|one|other}}', //sample real world msgs: 'undelete_short' : 'Undelete {{PLURAL:$1|one edit|$1 edits}}', - 'category-subcat-count' : "fish {{PLURAL:$2|This {{PLURAL:$1|fistsub|$1 secondsub}} category has only the following subcategory.|This category has the following {{PLURAL:$1|subcategory|$1 subcategories}}, out of $2 total.}}" + 'category-subcat-count' : "fish {{PLURAL:$2|This {{PLURAL:$1|fistsub|$1 secondsub}} category has only the following subcategory.|This category has the following {{PLURAL:$1|subcategory|$1 subcategories}}, out of $2 total.}}" }); $mw.lang.loadRS({ 'PLURAL' : { "one":1 } -}); \ No newline at end of file +}); + +//need to run through script-loader \ No newline at end of file diff --git a/js2/mwEmbed/tests/testLangRU.js b/js2/mwEmbed/tests/testLangRU.js index 2772c394ff..c41399d017 100644 --- a/js2/mwEmbed/tests/testLangRU.js +++ b/js2/mwEmbed/tests/testLangRU.js @@ -4,11 +4,11 @@ loadGM({ //test msg with english words to see whats going on - 'test_plural_msg' : '{{PLURAL:$1|one|few|many}}' + 'test_plural_msg' : '{{PLURAL:$1|one|few|many}}', //sample real world msgs: - 'undelete_short' => 'Восстановить $1 {{PLURAL:$1|правку|правки|правок}}', + 'undelete_short' : 'Восстановить $1 {{PLURAL:$1|правку|правки|правок}}' }); -loadRS({ +$mw.lang.loadRS({ 'PLURAL' : { "one":[{"mod":10,"is":1},{"mod":100,"not":11}], @@ -21,3 +21,9 @@ loadRS({ ] } }); +/* +one 1, 21, 31, 41, 51, 61... +few 2-4, 22-24, 32-34... +many 0, 5-20, 25-30, 35-40... +other 1.31, 2.31, 5.31... +*/