Scribunto: Implement`'lang` argument for `mw.ext.data.get` [#779]

staging
gnosygnu 4 years ago
parent 7b6b3ed09a
commit afed865c84

@ -1,24 +1,44 @@
/* /*
XOWA: the XOWA Offline Wiki Application XOWA: the XOWA Offline Wiki Application
Copyright (C) 2012-2017 gnosygnu@gmail.com Copyright (C) 2012-2020 gnosygnu@gmail.com
XOWA is licensed under the terms of the General Public License (GPL) Version 3, XOWA is licensed under the terms of the General Public License (GPL) Version 3,
or alternatively under the terms of the Apache License Version 2.0. or alternatively under the terms of the Apache License Version 2.0.
You may use XOWA according to either of these licenses as is most appropriate You may use XOWA according to either of these licenses as is most appropriate
for your project on a case-by-case basis. for your project on a case-by-case basis.
The terms of each license can be found in the source code repository: The terms of each license can be found in the source code repository:
GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
*/ */
package gplx.xowa.xtns.jsonConfigs.scribunto; import gplx.*; import gplx.xowa.*; import gplx.xowa.xtns.*; import gplx.xowa.xtns.jsonConfigs.*; package gplx.xowa.xtns.jsonConfigs.scribunto;
import gplx.xowa.xtns.scribunto.*; import gplx.xowa.xtns.scribunto.libs.*; import gplx.xowa.xtns.scribunto.procs.*;
import gplx.xowa.wikis.domains.*; import gplx.Bry_;
import gplx.Byte_ascii;
import gplx.Err_;
import gplx.Gfo_usr_dlg_;
import gplx.Io_url;
import gplx.Keyval;
import gplx.Keyval_;
import gplx.String_;
import gplx.xowa.Xowe_wiki;
import gplx.xowa.langs.Xol_lang_itm;
import gplx.xowa.wikis.domains.Xow_domain_itm_;
import gplx.xowa.xtns.scribunto.Scrib_core;
import gplx.xowa.xtns.scribunto.Scrib_lib;
import gplx.xowa.xtns.scribunto.Scrib_lua_mod;
import gplx.xowa.xtns.scribunto.libs.Scrib_lib_text;
import gplx.xowa.xtns.scribunto.libs.Scrib_lib_text__json_util;
import gplx.xowa.xtns.scribunto.libs.Scrib_lib_title;
import gplx.xowa.xtns.scribunto.procs.Scrib_proc_args;
import gplx.xowa.xtns.scribunto.procs.Scrib_proc_mgr;
import gplx.xowa.xtns.scribunto.procs.Scrib_proc_rslt;
public class Jscfg_scrib_lib implements Scrib_lib { public class Jscfg_scrib_lib implements Scrib_lib {
private final Scrib_lib_text__json_util json_util = new Scrib_lib_text__json_util(); private final Scrib_lib_text__json_util json_util = new Scrib_lib_text__json_util();
private final Jscfg_localizer localizer = new Jscfg_localizer(); private final Jscfg_localizer localizer = new Jscfg_localizer();
private Scrib_core core; private Scrib_core core;
public String Key() {return "JCLuaLibrary";} public String Key() {return "JCLuaLibrary";}
public Scrib_lua_mod Mod() {return mod;} private Scrib_lua_mod mod; public Scrib_lua_mod Mod() {return mod;} private Scrib_lua_mod mod;
@ -42,7 +62,20 @@ public class Jscfg_scrib_lib implements Scrib_lib {
public static final String Invk_get = "get"; public static final String Invk_get = "get";
private static final String[] Proc_names = String_.Ary(Invk_get); private static final String[] Proc_names = String_.Ary(Invk_get);
public boolean Get(Scrib_proc_args args, Scrib_proc_rslt rslt) { public boolean Get(Scrib_proc_args args, Scrib_proc_rslt rslt) {
byte[] ttl_bry = args.Xstr_bry_or_null(0); byte[] ttl_bry = args.Xstr_bry_or_null(0);
byte[] langCode = args.Xstr_bry_or_null(1);
// get language; ISSUE#:779; DATE:2020-08-03
Xol_lang_itm language = null;
if (langCode == null) {
language = this.core.Wiki().Lang();
}
else if (!Bry_.Eq(langCode, Byte_ascii.Underline_bry)) {
language = this.core.Wiki().App().Lang_mgr().Get_by_or_null(langCode);
}
else {
language = null;
}
// get commons wiki // get commons wiki
Xowe_wiki commons_wiki = (Xowe_wiki)core.App().Wiki_mgr().Get_by_or_null(Xow_domain_itm_.Bry__commons); Xowe_wiki commons_wiki = (Xowe_wiki)core.App().Wiki_mgr().Get_by_or_null(Xow_domain_itm_.Bry__commons);
@ -59,8 +92,20 @@ public class Jscfg_scrib_lib implements Scrib_lib {
throw Err_.new_wo_type("bad argument #1 to 'get' (not a valid title) " + String_.new_u8(ttl_bry)); throw Err_.new_wo_type("bad argument #1 to 'get' (not a valid title) " + String_.new_u8(ttl_bry));
} }
Keyval[] rv = Scrib_lib_text.JsonDecodeStatic(args, core, json_util, page, Scrib_lib_text__json_util.Opt__force_assoc, Scrib_lib_text__json_util.Flag__none); // get content
rv = localizer.Localize(core.Wiki().Lang(), page, rv); Keyval[] rv = null;
if (page == null) {
rv = Keyval_.Ary_empty;
Gfo_usr_dlg_.Instance.Warn_many("", "", "bad argument #1 to 'get' (page does not exist): ~{0}", ttl_bry);
} else {
rv = Scrib_lib_text.JsonDecodeStatic(args, core, json_util, page, Scrib_lib_text__json_util.Opt__force_assoc, Scrib_lib_text__json_util.Flag__none);
if (language == null) { // "_" passed in; return entire document
}
else {// COMMENT:desb42@: sometime should not be called (dewiki) BUT...
rv = localizer.Localize(language, page, rv);
}
}
return rslt.Init_obj(rv); return rslt.Init_obj(rv);
} }
} }

@ -1,6 +1,6 @@
/* /*
XOWA: the XOWA Offline Wiki Application XOWA: the XOWA Offline Wiki Application
Copyright (C) 2012-2017 gnosygnu@gmail.com Copyright (C) 2012-2020 gnosygnu@gmail.com
XOWA is licensed under the terms of the General Public License (GPL) Version 3, XOWA is licensed under the terms of the General Public License (GPL) Version 3,
or alternatively under the terms of the Apache License Version 2.0. or alternatively under the terms of the Apache License Version 2.0.
@ -13,161 +13,243 @@ The terms of each license can be found in the source code repository:
GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
*/ */
package gplx.xowa.xtns.jsonConfigs.scribunto; import gplx.*; import gplx.xowa.*; import gplx.xowa.xtns.*; import gplx.xowa.xtns.jsonConfigs.*; package gplx.xowa.xtns.jsonConfigs.scribunto;
import org.junit.*; import gplx.core.tests.*;
import gplx.xowa.xtns.scribunto.*; import gplx.xowa.xtns.scribunto.libs.*; import gplx.Bry_;
import gplx.xowa.langs.*; import gplx.xowa.langs.names.*; import gplx.Keyval;
import gplx.langs.jsons.*; import gplx.Keyval_;
public class Jscfg_scrib_lib_tst { import gplx.String_;
private final Jscfg_scrib_lib_fxt fxt = new Jscfg_scrib_lib_fxt(); import gplx.core.tests.Gftest;
@Test public void Get() { import gplx.langs.jsons.Json_doc;
fxt.Init__page("Data:Test.tab", Json_doc.Make_str_by_apos import gplx.xowa.Xowe_wiki;
( "{" import gplx.xowa.langs.Xoa_lang_mgr;
, " 'data':" import gplx.xowa.langs.Xol_lang_itm;
, " [" import gplx.xowa.xtns.scribunto.Scrib_invoke_func_fxt;
, " [" import org.junit.Test;
, " 'Q1'"
, " , 'Data:Q1'" public class Jscfg_scrib_lib_tst {
, " ]" private final Jscfg_scrib_lib_fxt fxt = new Jscfg_scrib_lib_fxt();
, " ," @Test public void Get() {
, " [" fxt.Init__page("Data:Test.tab", Json_doc.Make_str_by_apos
, " 'Q2'" ( "{"
, " , 'Data:Q2'" , " 'data':"
, " ]" , " ["
, " ]" , " ["
, "}" , " 'Q1'"
)); , " , 'Data:Q1'"
fxt.Test__get("Test.tab", String_.Concat_lines_nl_skip_last , " ]"
( "1=" , " ,"
, " data=" , " ["
, " 1=" , " 'Q2'"
, " 1=Q1" , " , 'Data:Q2'"
, " 2=Data:Q1" , " ]"
, " 2=" , " ]"
, " 1=Q2" , "}"
, " 2=Data:Q2" ));
)); fxt.Test__get("Test.tab", String_.Concat_lines_nl_skip_last
} ( "1="
@Test public void Get_localize() { , " data="
fxt.Init__page("Data:Test_localize.tab", Json_doc.Make_str_by_apos , " 1="
( "{" , " 1=Q1"
, " 'license': 'CC0-1.0'," , " 2=Data:Q1"
, " 'description': {" , " 2="
, " 'de': 'Objekttabelle'," , " 1=Q2"
, " 'en': 'Object table'" , " 2=Data:Q2"
, " }," ));
, " 'sources': 'Objects in Data:Data.tab completed by [https://www.wikidata.org Wikidata]'," }
, " 'schema': {" @Test public void Get_localize_null() { // null defaults to wiki.Lang
, " 'fields': [" fxt.Init__page__multilingual();
, " {" fxt.Test__get( "Test_localize.tab", null, String_.Concat_lines_nl_skip_last
, " 'name': 'wikidataID'," ( "1="
, " 'type': 'String'," , " license=CC0-1.0"
, " 'title': {" , " description=Object table"
, " 'de': 'Wikidata-Item'," , " sources=Objects in Data:Data.tab completed by [https://www.wikidata.org Wikidata]"
, " 'en': 'Wikidata item'" , " schema="
, " }" , " fields="
, " }," , " 1="
, " {" , " name=wikidataID"
, " 'name': 'wikidataLabel'," , " type=String"
, " 'type': 'localized'," , " title=Wikidata item"
, " 'title': {" , " 2="
, " 'de': 'Wikidata-Label'," , " name=wikidataLabel"
, " 'en': 'Wikidata label'" , " type=localized"
, " }" , " title=Wikidata label"
, " }" , " data="
, " ]" , " 1="
, " }," , " 1=Q183"
, " 'data': [" , " 2=Germany"
, " [" , " 2="
, " 'Q183'," , " 1=Q61912"
, " {" , " 2=Wertheim am Main"
, " 'de': 'Deutschland'," ));
, " 'en': 'Germany'" }
, " }" @Test public void Get_localize_underscore() {// underscore retrieves the entire document
, " ]," fxt.Init__page__multilingual();
, " [" fxt.Test__get( "Test_localize.tab", "_", String_.Concat_lines_nl_skip_last
, " 'Q61912'," ( "1="
, " {" , " license=CC0-1.0"
, " 'de': 'Wertheim'," , " description="
, " 'en': 'Wertheim am Main'" , " de=Objekttabelle"
, " }" , " en=Object table"
, " ]" , " sources=Objects in Data:Data.tab completed by [https://www.wikidata.org Wikidata]"
, " ]" , " schema="
, "}" , " fields="
)); , " 1="
fxt.Test__get( "Test_localize.tab", String_.Concat_lines_nl_skip_last , " name=wikidataID"
( "1=" , " type=String"
, " license=CC0-1.0" , " title="
, " description=Object table" , " de=Wikidata-Item"
, " sources=Objects in Data:Data.tab completed by [https://www.wikidata.org Wikidata]" , " en=Wikidata item"
, " schema=" , " 2="
, " fields=" , " name=wikidataLabel"
, " 1=" , " type=localized"
, " name=wikidataID" , " title="
, " type=String" , " de=Wikidata-Label"
, " title=Wikidata item" , " en=Wikidata label"
, " 2=" , " data="
, " name=wikidataLabel" , " 1="
, " type=localized" , " 1=Q183"
, " title=Wikidata label" , " 2="
, " data=" , " de=Deutschland"
, " 1=" , " en=Germany"
, " 1=Q183" , " 2="
, " 2=Germany" , " 1=Q61912"
, " 2=" , " 2="
, " 1=Q61912" , " de=Wertheim"
, " 2=Wertheim am Main" , " en=Wertheim am Main"
)); ));
} }
@Test public void pickLocalizedString() { @Test public void Get_localize_de() {
Xol_lang_itm lang = fxt.Init__lang("zh-cn", "zh1,zh0"); fxt.Init__lang("de", "en");
fxt.Init__page__multilingual();
// match key fxt.Test__get( "Test_localize.tab", "de", String_.Concat_lines_nl_skip_last
fxt.Test__pickLocalizedString(lang, fxt.Init__picklocalizedStringKvs("fr", "zh-cn"), "zh-cn"); ( "1="
, " license=CC0-1.0"
// match fallback; note that zh1 is higher in fallback list, but lower in kvs , " description=Objekttabelle"
fxt.Test__pickLocalizedString(lang, fxt.Init__picklocalizedStringKvs("zh0", "zh1"), "zh1"); , " sources=Objects in Data:Data.tab completed by [https://www.wikidata.org Wikidata]"
, " schema="
// match en if no key or fallbacks , " fields="
fxt.Test__pickLocalizedString(lang, fxt.Init__picklocalizedStringKvs("fr", "en"), "en"); , " 1="
, " name=wikidataID"
// pick 1st if no match , " type=String"
fxt.Test__pickLocalizedString(lang, fxt.Init__picklocalizedStringKvs("fr", "de"), "fr"); , " title=Wikidata-Item"
} , " 2="
} , " name=wikidataLabel"
class Jscfg_scrib_lib_fxt { , " type=localized"
private final Scrib_invoke_func_fxt fxt = new Scrib_invoke_func_fxt(); , " title=Wikidata-Label"
private final Jscfg_scrib_lib lib; , " data="
private final Xowe_wiki commons_wiki; , " 1="
public Jscfg_scrib_lib_fxt() { , " 1=Q183"
fxt.Clear_for_lib(); , " 2=Deutschland"
lib = new Jscfg_scrib_lib(); , " 2="
lib.Init(); , " 1=Q61912"
lib.Core_(fxt.Core()); , " 2=Wertheim"
this.commons_wiki = fxt.Parser_fxt().Wiki().Appe().Wiki_mgr().Get_by_or_make(gplx.xowa.wikis.domains.Xow_domain_itm_.Bry__commons).Init_assert(); ));
} }
public void Init__page(String page, String text) { @Test public void pickLocalizedString() {
fxt.Parser_fxt().Init_page_create(commons_wiki, page, text); Xol_lang_itm lang = fxt.Init__lang("zh-cn", "zh1,zh0");
}
public Xol_lang_itm Init__lang(String key, String fallbacks) { // match key
Xol_lang_itm lang = Xol_lang_itm.New(fxt.Core().App().Lang_mgr(), Bry_.new_u8(key)); fxt.Test__pickLocalizedString(lang, fxt.Init__picklocalizedStringKvs("fr", "zh-cn"), "zh-cn");
lang.Fallback_bry_(Bry_.new_a7(fallbacks));
return lang; // match fallback; note that zh1 is higher in fallback list, but lower in kvs
} fxt.Test__pickLocalizedString(lang, fxt.Init__picklocalizedStringKvs("zh0", "zh1"), "zh1");
public Keyval[] Init__picklocalizedStringKvs(String... vals) {
int len = vals.length; // match en if no key or fallbacks
Keyval[] rv = new Keyval[len]; fxt.Test__pickLocalizedString(lang, fxt.Init__picklocalizedStringKvs("fr", "en"), "en");
for (int i = 0; i < len; i++) {
String val = vals[i]; // pick 1st if no match
rv[i] = Keyval_.new_(val, val); fxt.Test__pickLocalizedString(lang, fxt.Init__picklocalizedStringKvs("fr", "de"), "fr");
} }
return rv; }
} class Jscfg_scrib_lib_fxt {
public void Test__get(String page, String expd) { private final Scrib_invoke_func_fxt fxt = new Scrib_invoke_func_fxt();
fxt.Test_scrib_proc_str_ary(lib, Jscfg_scrib_lib.Invk_get, Keyval_.Ary(Keyval_.int_(1, page)), expd); private final Jscfg_scrib_lib lib;
} private final Xowe_wiki commons_wiki;
public void Test__pickLocalizedString(Xol_lang_itm lang, Keyval[] kv_ary, String expd) { public Jscfg_scrib_lib_fxt() {
Keyval actl_kv = Jscfg_localizer.pickLocalizedString(lang, "key", kv_ary); fxt.Clear_for_lib();
Gftest.Eq__obj_or_null(expd, actl_kv.Val()); lib = new Jscfg_scrib_lib();
} lib.Init();
} lib.Core_(fxt.Core());
this.commons_wiki = fxt.Parser_fxt().Wiki().Appe().Wiki_mgr().Get_by_or_make(gplx.xowa.wikis.domains.Xow_domain_itm_.Bry__commons).Init_assert();
}
public void Init__page(String page, String text) {
fxt.Parser_fxt().Init_page_create(commons_wiki, page, text);
}
public void Init__page__multilingual() {
this.Init__page("Data:Test_localize.tab", Json_doc.Make_str_by_apos
( "{"
, " 'license': 'CC0-1.0',"
, " 'description': {"
, " 'de': 'Objekttabelle',"
, " 'en': 'Object table'"
, " },"
, " 'sources': 'Objects in Data:Data.tab completed by [https://www.wikidata.org Wikidata]',"
, " 'schema': {"
, " 'fields': ["
, " {"
, " 'name': 'wikidataID',"
, " 'type': 'String',"
, " 'title': {"
, " 'de': 'Wikidata-Item',"
, " 'en': 'Wikidata item'"
, " }"
, " },"
, " {"
, " 'name': 'wikidataLabel',"
, " 'type': 'localized',"
, " 'title': {"
, " 'de': 'Wikidata-Label',"
, " 'en': 'Wikidata label'"
, " }"
, " }"
, " ]"
, " },"
, " 'data': ["
, " ["
, " 'Q183',"
, " {"
, " 'de': 'Deutschland',"
, " 'en': 'Germany'"
, " }"
, " ],"
, " ["
, " 'Q61912',"
, " {"
, " 'de': 'Wertheim',"
, " 'en': 'Wertheim am Main'"
, " }"
, " ]"
, " ]"
, "}"
));
}
public Xol_lang_itm Init__lang(String key, String fallbacks) {
Xoa_lang_mgr lang_mgr = fxt.Core().App().Lang_mgr();
Xol_lang_itm lang = Xol_lang_itm.New(lang_mgr, Bry_.new_u8(key));
lang.Fallback_bry_(Bry_.new_a7(fallbacks));
lang_mgr.Add(lang);
return lang;
}
public Keyval[] Init__picklocalizedStringKvs(String... vals) {
int len = vals.length;
Keyval[] rv = new Keyval[len];
for (int i = 0; i < len; i++) {
String val = vals[i];
rv[i] = Keyval_.new_(val, val);
}
return rv;
}
public void Test__get(String page, String expd) {
Test__get(page, null, expd);
}
public void Test__get(String page, String lang, String expd) {
fxt.Test_scrib_proc_str_ary(lib, Jscfg_scrib_lib.Invk_get, Keyval_.Ary(Keyval_.int_(1, page), Keyval_.int_(2, lang)), expd);
}
public void Test__pickLocalizedString(Xol_lang_itm lang, Keyval[] kv_ary, String expd) {
Keyval actl_kv = Jscfg_localizer.pickLocalizedString(lang, "key", kv_ary);
Gftest.Eq__obj_or_null(expd, actl_kv.Val());
}
}

Loading…
Cancel
Save