mirror of
https://github.com/gnosygnu/xowa.git
synced 2026-03-02 03:49:30 +00:00
v2.8.1.1
This commit is contained in:
@@ -19,10 +19,11 @@ package gplx.xowa.xtns.scribunto; import gplx.*; import gplx.xowa.*; import gplx
|
||||
import gplx.xowa.xtns.scribunto.libs.*;
|
||||
import gplx.xowa.xtns.scribunto.engines.*;
|
||||
public class Scrib_core {
|
||||
private Hash_adp_bry mods = Hash_adp_bry.cs_();
|
||||
private Hash_adp_bry mods = Hash_adp_bry.cs();
|
||||
public Scrib_core(Xoae_app app, Xop_ctx ctx) {// NOTE: ctx needed for language reg
|
||||
this.app = app; this.ctx = ctx;
|
||||
this.wiki = ctx.Wiki(); this.page = ctx.Cur_page(); // NOTE: wiki / page needed for title reg; DATE:2014-02-05
|
||||
this.lang = wiki.Lang();
|
||||
this.Engine_(Scrib_engine_type.Type_lua, false); // TEST: default to lua
|
||||
fsys_mgr.Root_dir_(app.Fsys_mgr().Bin_xtns_dir().GenSubDir_nest("Scribunto"));
|
||||
lib_mw = new Scrib_lib_mw(this);
|
||||
@@ -39,6 +40,7 @@ public class Scrib_core {
|
||||
}
|
||||
public Xoae_app App() {return app;} private Xoae_app app;
|
||||
public Xowe_wiki Wiki() {return wiki;} private Xowe_wiki wiki;
|
||||
public Xol_lang Lang() {return lang;} private Xol_lang lang;
|
||||
@gplx.Internal protected void Wiki_(Xowe_wiki v) {this.wiki = v;} // TEST:
|
||||
public Xoae_page Page() {return page;} private Xoae_page page;
|
||||
public boolean Enabled() {return enabled;} private boolean enabled = true;
|
||||
|
||||
@@ -19,7 +19,7 @@ package gplx.xowa.xtns.scribunto; import gplx.*; import gplx.xowa.*; import gplx
|
||||
public class Scrib_err_filter_mgr implements GfoInvkAble {
|
||||
private final Ordered_hash hash_by_mod = Ordered_hash_.new_();
|
||||
public void Clear() {hash_by_mod.Clear();}
|
||||
public boolean Count_gt_0() {return hash_by_mod.Count() > 0;}
|
||||
public boolean Count_eq_0() {return hash_by_mod.Count() == 0;}
|
||||
public boolean Match(String mod, String fnc, String err) {
|
||||
List_adp itms = Get_itms_or_null(mod, fnc); if (itms == null) return false;
|
||||
int itms_len = itms.Count();
|
||||
|
||||
@@ -57,25 +57,22 @@ public class Scrib_invoke_func extends Pf_func_base {
|
||||
invoke_wkr.Eval_end(ctx.Cur_page(), mod_name, fnc_name, log_time_bgn);
|
||||
}
|
||||
catch (Exception e) {
|
||||
String invoke_error = Err_msg_make(e);
|
||||
Error(bfr, wiki.Msg_mgr(), invoke_error);
|
||||
bfr.Add(Html_tag_.Comm_bgn).Add_str(Err_.Message_gplx_full(e)).Add(Html_tag_.Comm_end);
|
||||
Error(bfr, wiki.Msg_mgr(), e);
|
||||
bfr.Add(Html_tag_.Comm_bgn).Add_str(Err_.Message_lang(e)).Add(Html_tag_.Comm_end);
|
||||
String invoke_error = String_.Replace(Err_.Message_gplx_log(e), "\n", ""); // NOTE: replace \n as error may have excerpt which will have \n
|
||||
Scrib_err_filter_mgr err_filter_mgr = invoke_wkr == null ? null : invoke_wkr.Err_filter_mgr();
|
||||
if (err_filter_mgr == null || (err_filter_mgr.Count_gt_0() && err_filter_mgr.Match(String_.new_u8(mod_name), String_.new_u8(fnc_name), invoke_error)))
|
||||
ctx.App().Usr_dlg().Warn_many("", "", "invoke failed: ~{0} ~{1} ~{2}", ctx.Cur_page().Ttl().Raw(), String_.new_u8(src, self.Src_bgn(), self.Src_end()), Err_.Message_gplx_log(e));
|
||||
if ( err_filter_mgr == null // no err_filter_mgr defined;
|
||||
|| err_filter_mgr.Count_eq_0() // err_filter_mgr exists, but no definitions
|
||||
|| !err_filter_mgr.Match(String_.new_u8(mod_name), String_.new_u8(fnc_name), invoke_error)) // err_filter_mgr has defintion and it doesn't match current; print warn; DATE:2015-07-24
|
||||
ctx.App().Usr_dlg().Warn_many("", "", "invoke failed: ~{0} ~{1} ~{2}", ctx.Cur_page().Ttl().Raw(), String_.new_u8(src, self.Src_bgn(), self.Src_end()), String_.Replace(Err_.Message_gplx_log(e), "\n", "\t"));
|
||||
Scrib_core.Core_invalidate_when_page_changes(); // NOTE: invalidate core when page changes, not for rest of page, else page with many errors will be very slow due to multiple invalidations; PAGE:th.d:all; DATE:2014-10-03
|
||||
}
|
||||
}
|
||||
private static String Err_msg_make(Exception e) {// DATE:2015-02-03
|
||||
String rv = Err_.Message_lang(e); // EX: class gplx.Err =Module:testBK:16 The title of the template is missing ':'\n[STACK]
|
||||
int nl_pos = String_.FindFwd(rv, "\n",0); if (nl_pos == Bry_.NotFound) nl_pos = String_.Len(rv); rv = String_.Mid(rv, 0, nl_pos); // gplx errors include message stack which should be removed
|
||||
return String_.Trim(String_.Replace(rv, "class gplx.Err", "")); // remove leading "gplx.Err" and trim
|
||||
}
|
||||
public static void Error(Bry_bfr bfr, Xow_msg_mgr msg_mgr, String error) {Error(bfr, msg_mgr, Bry_.new_u8(error));}
|
||||
public static void Error(Bry_bfr bfr, Xow_msg_mgr msg_mgr, byte[] error) {
|
||||
Bry_fmtr fmtr = Bry_fmtr.new_("<strong class=\"error\"><span class=\"scribunto-error\" id=\"mw-scribunto-error-0\">~{0} ~{1}</span></strong>"); // <!--~{0}: ~{1}.-->
|
||||
public static void Error(Bry_bfr bfr, Xow_msg_mgr msg_mgr, Exception e) {Error(bfr, msg_mgr, Err_.cast_or_make(e).To_str__top_wo_args());}// NOTE: must use "short" error message to show in wikitext; DATE:2015-07-27
|
||||
public static void Error(Bry_bfr bfr, Xow_msg_mgr msg_mgr, String error) {
|
||||
byte[] script_error_msg = msg_mgr.Val_by_id(Xol_msg_itm_.Id_scribunto_parser_error);
|
||||
fmtr.Bld_bfr_many(bfr, script_error_msg, error);
|
||||
error_fmtr.Bld_bfr_many(bfr, script_error_msg, error);
|
||||
}
|
||||
private static final Bry_fmtr error_fmtr = Bry_fmtr.new_("<strong class='error'><span class='scribunto-error' id='mw-scribunto-error-0'>~{0}: ~{1}</span></strong>"); // <!--~{0}: ~{1}.-->
|
||||
public static final String Err_mod_missing = "No such module";
|
||||
}
|
||||
|
||||
@@ -85,8 +85,11 @@ public class Scrib_invoke_func_fxt {
|
||||
}
|
||||
public void Test_parse_err(String raw, String expd_err_type) {
|
||||
Scrib_invoke_func.Error(tmp_bfr, fxt.Wiki().Msg_mgr(), expd_err_type);
|
||||
byte[] expd_err = tmp_bfr.Xto_bry_and_clear();
|
||||
fxt.Test_parse_page_tmpl_str(raw, String_.new_u8(expd_err));
|
||||
fxt.Test_parse_page_tmpl_str(raw, tmp_bfr.Xto_str_and_clear());
|
||||
}
|
||||
public void Test_error(Exception e, String expd) {
|
||||
Scrib_invoke_func.Error(tmp_bfr, fxt.Wiki().Msg_mgr(), e);
|
||||
Tfds.Eq_str(expd, tmp_bfr.Xto_str_and_clear(), "error");
|
||||
}
|
||||
public void Test_lib_proc(Scrib_lib lib, String func_name, Object[] args, String expd) {Test_lib_proc_kv(lib, func_name, Scrib_kv_utl_.base1_many_(args), expd);}
|
||||
public void Test_lib_proc_kv(Scrib_lib lib, String func_name, KeyVal[] args, String expd) {
|
||||
@@ -108,8 +111,10 @@ public class Scrib_invoke_func_fxt {
|
||||
public void Init_frame_current(KeyVal... ary) {
|
||||
core.Frame_current_(Xot_invk_mock.test_(Bry_.new_a7("Module:Mod_0"), ary));
|
||||
}
|
||||
public void Clear_for_lib() {
|
||||
fxt = new Xop_fxt(); // NOTE: don't try to cache fxt on func_fxt level; causes errors in Language_lib
|
||||
public void Clear_for_lib() {Clear_for_lib("en.wikipedia.org", "en");}
|
||||
public void Clear_for_lib(String domain, String lang) {
|
||||
Xoae_app app = Xoa_app_fxt.app_();
|
||||
fxt = new Xop_fxt(app, Xoa_app_fxt.wiki_(app, domain, app.Lang_mgr().Get_by_key_or_new(Bry_.new_u8(lang)))); // NOTE: don't try to cache fxt on func_fxt level; causes errors in Language_lib
|
||||
core_fxt = new Scrib_core_fxt(fxt);
|
||||
core = core_fxt.Core();
|
||||
Xot_invk parent_frame = new Xot_invk_temp(true); parent_frame.Frame_tid_(Scrib_frame_.Tid_null);
|
||||
|
||||
@@ -23,7 +23,7 @@ public interface Gfo_comp_op_1 {
|
||||
boolean Comp_long(long val, long comp);
|
||||
boolean Comp_float(float val, float comp);
|
||||
boolean Comp_double(double val, double comp);
|
||||
boolean Comp_decimal(DecimalAdp val, DecimalAdp comp);
|
||||
boolean Comp_decimal(Decimal_adp val, Decimal_adp comp);
|
||||
boolean Comp_char(char val, char comp);
|
||||
boolean Comp_str(String val, String comp);
|
||||
boolean Comp_bry(byte[] val, byte[] comp);
|
||||
@@ -39,7 +39,7 @@ class Gfo_comp_op_eq implements Gfo_comp_op_1 {
|
||||
public boolean Comp_long(long val, long comp) {return val == comp;}
|
||||
public boolean Comp_float(float val, float comp) {return val == comp;}
|
||||
public boolean Comp_double(double val, double comp) {return val == comp;}
|
||||
public boolean Comp_decimal(DecimalAdp val, DecimalAdp comp) {return val.Eq(comp);}
|
||||
public boolean Comp_decimal(Decimal_adp val, Decimal_adp comp) {return val.Eq(comp);}
|
||||
public boolean Comp_char(char val, char comp) {return val == comp;}
|
||||
public boolean Comp_str(String val, String comp) {return String_.Eq(val, comp);}
|
||||
public boolean Comp_bry(byte[] val, byte[] comp) {return Bry_.Eq(val, comp);}
|
||||
@@ -55,7 +55,7 @@ class Gfo_comp_op_eqn implements Gfo_comp_op_1 {
|
||||
public boolean Comp_long(long val, long comp) {return val != comp;}
|
||||
public boolean Comp_float(float val, float comp) {return val != comp;}
|
||||
public boolean Comp_double(double val, double comp) {return val != comp;}
|
||||
public boolean Comp_decimal(DecimalAdp val, DecimalAdp comp) {return !val.Eq(comp);}
|
||||
public boolean Comp_decimal(Decimal_adp val, Decimal_adp comp) {return !val.Eq(comp);}
|
||||
public boolean Comp_char(char val, char comp) {return val != comp;}
|
||||
public boolean Comp_str(String val, String comp) {return !String_.Eq(val, comp);}
|
||||
public boolean Comp_bry(byte[] val, byte[] comp) {return !Bry_.Eq(val, comp);}
|
||||
@@ -71,7 +71,7 @@ class Gfo_comp_op_lt implements Gfo_comp_op_1 {
|
||||
public boolean Comp_long(long val, long comp) {return val < comp;}
|
||||
public boolean Comp_float(float val, float comp) {return val < comp;}
|
||||
public boolean Comp_double(double val, double comp) {return val < comp;}
|
||||
public boolean Comp_decimal(DecimalAdp val, DecimalAdp comp) {return val.Comp_lt(comp);}
|
||||
public boolean Comp_decimal(Decimal_adp val, Decimal_adp comp) {return val.Comp_lt(comp);}
|
||||
public boolean Comp_char(char val, char comp) {return val < comp;}
|
||||
public boolean Comp_str(String val, String comp) {return String_.Compare(val, comp) < CompareAble_.Same;}
|
||||
public boolean Comp_bry(byte[] val, byte[] comp) {return Bry_.Compare(val, comp) < CompareAble_.Same;}
|
||||
@@ -87,7 +87,7 @@ class Gfo_comp_op_lte implements Gfo_comp_op_1 {
|
||||
public boolean Comp_long(long val, long comp) {return val <= comp;}
|
||||
public boolean Comp_float(float val, float comp) {return val <= comp;}
|
||||
public boolean Comp_double(double val, double comp) {return val <= comp;}
|
||||
public boolean Comp_decimal(DecimalAdp val, DecimalAdp comp) {return val.Comp_lte(comp);}
|
||||
public boolean Comp_decimal(Decimal_adp val, Decimal_adp comp) {return val.Comp_lte(comp);}
|
||||
public boolean Comp_char(char val, char comp) {return val <= comp;}
|
||||
public boolean Comp_str(String val, String comp) {return String_.Compare(val, comp) <= CompareAble_.Same;}
|
||||
public boolean Comp_bry(byte[] val, byte[] comp) {return Bry_.Compare(val, comp) <= CompareAble_.Same;}
|
||||
@@ -103,7 +103,7 @@ class Gfo_comp_op_mt implements Gfo_comp_op_1 {
|
||||
public boolean Comp_long(long val, long comp) {return val > comp;}
|
||||
public boolean Comp_float(float val, float comp) {return val > comp;}
|
||||
public boolean Comp_double(double val, double comp) {return val > comp;}
|
||||
public boolean Comp_decimal(DecimalAdp val, DecimalAdp comp) {return val.Comp_lt(comp);}
|
||||
public boolean Comp_decimal(Decimal_adp val, Decimal_adp comp) {return val.Comp_lt(comp);}
|
||||
public boolean Comp_char(char val, char comp) {return val > comp;}
|
||||
public boolean Comp_str(String val, String comp) {return String_.Compare(val, comp) > CompareAble_.Same;}
|
||||
public boolean Comp_bry(byte[] val, byte[] comp) {return Bry_.Compare(val, comp) > CompareAble_.Same;}
|
||||
@@ -119,7 +119,7 @@ class Gfo_comp_op_mte implements Gfo_comp_op_1 {
|
||||
public boolean Comp_long(long val, long comp) {return val >= comp;}
|
||||
public boolean Comp_float(float val, float comp) {return val >= comp;}
|
||||
public boolean Comp_double(double val, double comp) {return val >= comp;}
|
||||
public boolean Comp_decimal(DecimalAdp val, DecimalAdp comp) {return val.Comp_lte(comp);}
|
||||
public boolean Comp_decimal(Decimal_adp val, Decimal_adp comp) {return val.Comp_lte(comp);}
|
||||
public boolean Comp_char(char val, char comp) {return val >= comp;}
|
||||
public boolean Comp_str(String val, String comp) {return String_.Compare(val, comp) >= CompareAble_.Same;}
|
||||
public boolean Comp_bry(byte[] val, byte[] comp) {return Bry_.Compare(val, comp) >= CompareAble_.Same;}
|
||||
|
||||
@@ -21,7 +21,7 @@ class Scrib_err_mgr implements GfoInvkAble {
|
||||
private int key_id = 0;
|
||||
private static final byte[] Key_prefix = Bry_.new_a7("scrib_err_");
|
||||
private Scrib_err_cmd Set(byte[] key) {
|
||||
if (key == null) Bry_.Add(Key_prefix, Bry_.XbyInt(key_id++));
|
||||
if (key == null) Bry_.Add(Key_prefix, Bry_.new_by_int(key_id++));
|
||||
Scrib_err_cmd rv = new Scrib_err_cmd(key);
|
||||
hash.Add_if_dupe_use_1st(key, rv);
|
||||
return rv;
|
||||
|
||||
@@ -183,11 +183,20 @@ public class Scrib_lib_language implements Scrib_lib {
|
||||
boolean utc = args.Cast_bool_or_n(3);
|
||||
Bry_bfr tmp_bfr = core.App().Utl__bfr_mkr().Get_b512();
|
||||
Pft_fmt_itm[] fmt_ary = Pft_fmt_itm_.Parse(core.Ctx(), fmt_bry);
|
||||
DateAdp date
|
||||
= Bry_.Len_eq_0(date_bry)
|
||||
? DateAdp_.Now()
|
||||
: Pft_func_time.ParseDate(date_bry, utc, tmp_bfr)
|
||||
; // NOTE: MW is actually more strict about date; however, not sure about PHP's date parse, so using more lax version
|
||||
DateAdp date = null;
|
||||
if (Bry_.Len_eq_0(date_bry))
|
||||
date = DateAdp_.Now();
|
||||
else {
|
||||
if (date_bry[0] == Byte_ascii.Plus) { // detect wikidata-style dates; EX: +00000002010-05-01T00:00:00Z; PAGE:en.w:Mountain_Province; DATE:2015-07-29
|
||||
int date_bry_len = date_bry.length;
|
||||
if ( date_bry[date_bry_len - 1] == Byte_ascii.Ltr_Z
|
||||
&& date_bry[date_bry_len - 10] == Byte_ascii.Ltr_T) {
|
||||
int year_bgn = Bry_finder.Find_fwd_while(date_bry, 1, date_bry_len, Byte_ascii.Num_0);
|
||||
date_bry = Bry_.Mid(date_bry, year_bgn); // lop off beginning "+000000..."
|
||||
}
|
||||
}
|
||||
date = Pft_func_time.ParseDate(date_bry, utc, tmp_bfr); // NOTE: not using java's datetime parse b/c it is more strict; not reconstructing PHP's datetime parse b/c it is very complicated (state machine); re-using MW's parser b/c it is inbetween; DATE:2015-07-29
|
||||
}
|
||||
if (date == null || tmp_bfr.Len() > 0) {tmp_bfr.Mkr_rls(); return rslt.Init_fail("bad argument #2 to 'formatDate' (not a valid timestamp)");}
|
||||
Pft_func_formatdate.Date_bldr().Format(tmp_bfr, core.Wiki(), lang, date, fmt_ary);
|
||||
byte[] rv = tmp_bfr.To_bry_and_rls();
|
||||
|
||||
@@ -75,6 +75,12 @@ public class Scrib_lib_language_tst {
|
||||
fxt.Test_scrib_proc_str(lib, Scrib_lib_language.Invk_formatDate, Object_.Ary("en", "Y-m-d", "2013-03-17", false), "2013-03-17");
|
||||
fxt.Test_scrib_proc_str(lib, Scrib_lib_language.Invk_formatDate, Object_.Ary("en", "Y-m-d"), DateAdp_.Now().XtoStr_fmt_yyyy_MM_dd()); // empty date should default to today;
|
||||
}
|
||||
@Test public void FormatDate__ymd_12() {
|
||||
fxt.Test_scrib_proc_str(lib, Scrib_lib_language.Invk_formatDate, Object_.Ary("en", "Y-m-d", "201603160102", false), "2016-03-16"); // handle long numeric date (12 digits); PAGE:en.w:Boron; DATE:2015-07-29
|
||||
}
|
||||
@Test public void FormatDate__utc() {
|
||||
fxt.Test_scrib_proc_str(lib, Scrib_lib_language.Invk_formatDate, Object_.Ary("en", "Y-m-d", "+00000002010-05-01T00:00:00Z", false), "2010-05-01"); // handle Wikidata style dates; PAGE:en.w:Mountain_Province; DATE:2015-07-29
|
||||
}
|
||||
@Test public void FormatDate_date_omitted() { // PURPOSE: some calls skip the date; retrieve arg_4 by int; EX: pl.w:L._Frank_Baum
|
||||
Tfds.Now_enabled_y_();
|
||||
Tfds.Now_set(DateAdp_.new_(2013, 12, 19, 1, 2, 3, 4));
|
||||
|
||||
@@ -150,7 +150,7 @@ class Scrib_lib_message_data {
|
||||
return msg_val;
|
||||
}
|
||||
static final byte Key_tid_keys = 1, Key_tid_rawMessage = 2, Key_tid_lang = 3, Key_tid_useDB = 4, Key_tid_title = 5, Key_tid_params = 6;
|
||||
private static final Hash_adp_bry key_hash = Hash_adp_bry.ci_ascii_()
|
||||
private static final Hash_adp_bry key_hash = Hash_adp_bry.ci_a7()
|
||||
.Add_str_byte("keys", Key_tid_keys)
|
||||
.Add_str_byte("rawMessage", Key_tid_rawMessage)
|
||||
.Add_str_byte("lang", Key_tid_lang)
|
||||
@@ -165,14 +165,14 @@ class Scrib_lib_message_data {
|
||||
return ((Byte_obj_val)o).Val();
|
||||
}
|
||||
public static final byte Fmt_tid_parse = 1, Fmt_tid_text = 2, Fmt_tid_plain = 3, Fmt_tid_escaped = 4, Fmt_tid_parseAsBlock = 5;
|
||||
private static final Hash_adp_bry fmt_hash = Hash_adp_bry.ci_ascii_()
|
||||
private static final Hash_adp_bry fmt_hash = Hash_adp_bry.ci_a7()
|
||||
.Add_str_byte("parse", Fmt_tid_parse)
|
||||
.Add_str_byte("text", Fmt_tid_text)
|
||||
.Add_str_byte("plain", Fmt_tid_plain)
|
||||
.Add_str_byte("escaped", Fmt_tid_escaped)
|
||||
.Add_str_byte("parseAsBlock", Fmt_tid_parseAsBlock);
|
||||
public static final byte Check_tid_exists = 1, Check_tid_isBlank = 2, Check_tid_isDisabled = 3;
|
||||
private static final Hash_adp_bry check_hash = Hash_adp_bry.ci_ascii_()
|
||||
private static final Hash_adp_bry check_hash = Hash_adp_bry.ci_a7()
|
||||
.Add_str_byte("exists", Check_tid_exists)
|
||||
.Add_str_byte("isBlank", Check_tid_isBlank)
|
||||
.Add_str_byte("isDisabled", Check_tid_isDisabled);
|
||||
|
||||
@@ -135,18 +135,18 @@ public class Scrib_lib_mw implements Scrib_lib {
|
||||
if (idx == cur) return nde;
|
||||
else ++cur;
|
||||
}
|
||||
return invk.Args_get_by_key(src, Bry_.XtoStrBytesByInt(idx + 1, 1));
|
||||
return invk.Args_get_by_key(src, Bry_.To_a7_bry(idx + 1, 1));
|
||||
}
|
||||
private static boolean Verify_arg_key(byte[] src, int idx, Arg_nde_tkn nde) {
|
||||
int key_int = Bry_.NotFound;
|
||||
byte[] key_dat_ary = nde.Key_tkn().Dat_ary();
|
||||
if (Env_.Mode_testing() && src == null) // some tests will always pass a null src;
|
||||
key_int = Bry_.Xto_int_or(key_dat_ary, 0, key_dat_ary.length, Bry_.NotFound);
|
||||
key_int = Bry_.To_int_or(key_dat_ary, 0, key_dat_ary.length, Bry_.NotFound);
|
||||
else {
|
||||
if (Bry_.Len_eq_0(key_dat_ary)) // should be called by current context;
|
||||
key_int = Bry_.Xto_int_or(src, nde.Key_tkn().Src_bgn(), nde.Key_tkn().Src_end(), Bry_.NotFound);
|
||||
key_int = Bry_.To_int_or(src, nde.Key_tkn().Src_bgn(), nde.Key_tkn().Src_end(), Bry_.NotFound);
|
||||
else // will be called by parent context; note that this calls Xot_defn_tmpl_.Make_itm which sets a key_dat_ary; DATE:2013-09-23
|
||||
key_int = Bry_.Xto_int_or(key_dat_ary, 0, key_dat_ary.length, Bry_.NotFound);
|
||||
key_int = Bry_.To_int_or(key_dat_ary, 0, key_dat_ary.length, Bry_.NotFound);
|
||||
}
|
||||
if (key_int == Bry_.NotFound) // key is not-numeric
|
||||
return false;
|
||||
@@ -185,7 +185,7 @@ public class Scrib_lib_mw implements Scrib_lib {
|
||||
if (key_missing) // key missing; EX: {{a|val}}
|
||||
key_as_int = ++arg_idx;// NOTE: MW requires a key; if none, then default to int index; NOTE: must be int, not String; NOTE: must be indexed to keyless args; EX: in "key1=val1,val2", "val2" must be "1" (1st keyless arg) not "2" (2nd arg); DATE:2013-11-09
|
||||
else { // key exists; EX:{{a|key=val}}
|
||||
key_as_int = Bry_.Xto_int_or(tmp_bfr.Bfr(), 0, tmp_bfr.Len(), Int_.MinValue);
|
||||
key_as_int = Bry_.To_int_or(tmp_bfr.Bfr(), 0, tmp_bfr.Len(), Int_.MinValue);
|
||||
if (key_as_int == Int_.MinValue) { // key is not int; create str
|
||||
key_as_str = tmp_bfr.Xto_str_and_clear();
|
||||
key_is_str = true;
|
||||
|
||||
@@ -112,6 +112,9 @@ public class Scrib_lib_mw__invoke_tst {
|
||||
}
|
||||
@Test public void Err_mod_blank() {fxt.Test_parse_err("{{#invoke:}}" , Scrib_invoke_func.Err_mod_missing);}
|
||||
@Test public void Err_mod_missing() {fxt.Test_parse_err("{{#invoke:Missing}}" , Scrib_invoke_func.Err_mod_missing);}
|
||||
@Test public void Err_mod_custom() {
|
||||
fxt.Test_error(Err_.new_("err_type", "fail", "key0", "val0"), "<strong class='error'><span class='scribunto-error' id='mw-scribunto-error-0'>Script error: fail</span></strong>");
|
||||
}
|
||||
private void Init_preprocess() {
|
||||
fxt.Init_tmpl("{{#invoke:Mod_0|Func_0|1|c|d}}"); // current
|
||||
fxt.Init_page("{{test|1|a|b|c}}"); // parent
|
||||
|
||||
@@ -16,7 +16,7 @@ You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.xowa.xtns.scribunto.libs; import gplx.*; import gplx.xowa.*; import gplx.xowa.xtns.*; import gplx.xowa.xtns.scribunto.*;
|
||||
import gplx.xowa.net.*;
|
||||
import gplx.core.net.*;
|
||||
public class Scrib_lib_text implements Scrib_lib {
|
||||
public Scrib_lib_text(Scrib_core core) {this.core = core;} private Scrib_core core;
|
||||
public Scrib_lua_mod Mod() {return mod;} private Scrib_lua_mod mod;
|
||||
@@ -65,11 +65,11 @@ public class Scrib_lib_text implements Scrib_lib {
|
||||
class Scrib_lib_text_ {
|
||||
public static KeyVal[] Init_nowiki_protocols(Xowe_wiki wiki) {
|
||||
Bry_bfr bfr = wiki.Utl__bfr_mkr().Get_b128();
|
||||
Ordered_hash protocols = Xoo_protocol_itm.Regy;
|
||||
Ordered_hash protocols = Gfo_protocol_itm.Regy;
|
||||
int len = protocols.Count();
|
||||
List_adp rv = List_adp_.new_();
|
||||
for (int i = 0; i < len; i++) {
|
||||
Xoo_protocol_itm itm = (Xoo_protocol_itm)protocols.Get_at(i);
|
||||
Gfo_protocol_itm itm = (Gfo_protocol_itm)protocols.Get_at(i);
|
||||
if (itm.Text_ends_w_colon()) { // To convert the protocol into a case-insensitive Lua pattern, we need to replace letters with a character class like [Xx] and insert a '%' before various punctuation.
|
||||
KeyVal kv = Init_nowiki_protocols_itm(bfr, itm);
|
||||
rv.Add(kv);
|
||||
@@ -78,7 +78,7 @@ class Scrib_lib_text_ {
|
||||
bfr.Mkr_rls();
|
||||
return (KeyVal[])rv.To_ary(KeyVal.class);
|
||||
}
|
||||
private static KeyVal Init_nowiki_protocols_itm(Bry_bfr bfr, Xoo_protocol_itm itm) {
|
||||
private static KeyVal Init_nowiki_protocols_itm(Bry_bfr bfr, Gfo_protocol_itm itm) {
|
||||
byte[] key = itm.Key_wo_colon_bry();
|
||||
int end = key.length - 1; // -1 to ignore final colon
|
||||
for (int i = 0; i < end; i++) {
|
||||
|
||||
@@ -62,7 +62,7 @@ public class Scrib_lib_title implements Scrib_lib {
|
||||
Xowe_wiki wiki = core.Wiki();
|
||||
byte[] ns_bry = null;
|
||||
if (ns_obj != null) {
|
||||
ns_bry = Parse_ns(wiki, ns_obj); if (ns_bry == null) throw Err_.new_wo_type("unknown ns", "ns", Object_.Xto_str_strict_or_empty(ns_bry));
|
||||
ns_bry = Parse_ns(wiki, ns_obj); if (ns_bry == null) throw Err_.new_wo_type("unknown ns", "ns", Object_.Xto_str_strict_or_empty(ns_obj));
|
||||
}
|
||||
if (ns_bry != null) {
|
||||
Bry_bfr bfr = wiki.Utl__bfr_mkr().Get_b512();
|
||||
@@ -94,12 +94,12 @@ public class Scrib_lib_title implements Scrib_lib {
|
||||
Pfunc_urlfunc.UrlString(core.Ctx(), url_func_tid, false, ttl_bry, bfr, qry_bry);
|
||||
return rslt.Init_obj(bfr.To_str_and_rls());
|
||||
}
|
||||
private static final Hash_adp_bry url_func_hash = Hash_adp_bry.ci_ascii_()
|
||||
private static final Hash_adp_bry url_func_hash = Hash_adp_bry.ci_a7()
|
||||
.Add_str_byte("fullUrl", Pfunc_urlfunc.Tid_full)
|
||||
.Add_str_byte("localUrl", Pfunc_urlfunc.Tid_local)
|
||||
.Add_str_byte("canonicalUrl", Pfunc_urlfunc.Tid_canonical);
|
||||
// private static final byte[] Proto_relative = Bry_.new_a7("relative");
|
||||
// private static final Hash_adp_bry proto_hash = Hash_adp_bry.ci_ascii_().Add_str_obj("http", Bry_.new_a7("http://")).Add_str_obj("https", Bry_.new_a7("https://")).Add_str_obj("relative", Bry_.new_a7("//")).Add_str_obj("canonical", Bry_.new_a7("1"));
|
||||
// private static final Hash_adp_bry proto_hash = Hash_adp_bry.ci_a7().Add_str_obj("http", Bry_.new_a7("http://")).Add_str_obj("https", Bry_.new_a7("https://")).Add_str_obj("relative", Bry_.new_a7("//")).Add_str_obj("canonical", Bry_.new_a7("1"));
|
||||
private byte[] Parse_ns(Xowe_wiki wiki, Object ns_obj) {
|
||||
if (ClassAdp_.Eq_typeSafe(ns_obj, String.class))
|
||||
return Bry_.new_u8(String_.cast_(ns_obj));
|
||||
|
||||
@@ -249,14 +249,19 @@ class Scrib_lib_ustring_gsub_mgr {
|
||||
switch (b) {
|
||||
case Byte_ascii.Num_0: case Byte_ascii.Num_1: case Byte_ascii.Num_2: case Byte_ascii.Num_3: case Byte_ascii.Num_4:
|
||||
case Byte_ascii.Num_5: case Byte_ascii.Num_6: case Byte_ascii.Num_7: case Byte_ascii.Num_8: case Byte_ascii.Num_9:
|
||||
int idx = b - Byte_ascii.Num_0 - List_adp_.Base1;
|
||||
if (idx < match.Groups().length) { // retrieve numbered capture; TODO: support more than 9 captures
|
||||
Regx_group grp = match.Groups()[idx];
|
||||
tmp_bfr.Add_str(String_.Mid(text, grp.Bgn(), grp.End())); // NOTE: grp.Bgn() / .End() is for String pos (bry pos will fail for utf8 strings)
|
||||
}
|
||||
else {
|
||||
tmp_bfr.Add_byte(Byte_ascii.Percent);
|
||||
tmp_bfr.Add_byte(b);
|
||||
int idx = b - Byte_ascii.Num_0;
|
||||
if (idx == 0) // NOTE: 0 means take result; REF.MW:if ($x === '0'); return $m[0]; PAGE:Wikipedia:Wikipedia_Signpost/Templates/Voter/testcases; DATE:2015-08-02
|
||||
tmp_bfr.Add_str_u8(String_.Mid(text, match.Find_bgn(), match.Find_end()));
|
||||
else { // NOTE: > 0 means get from groups if it exists; REF.MW:elseif (isset($m["m$x"])) return $m["m$x"]; PAGE:Wikipedia:Wikipedia_Signpost/Templates/Voter/testcases; DATE:2015-08-02
|
||||
idx -= List_adp_.Base1;
|
||||
if (idx < match.Groups().length) { // retrieve numbered capture; TODO: support more than 9 captures
|
||||
Regx_group grp = match.Groups()[idx];
|
||||
tmp_bfr.Add_str(String_.Mid(text, grp.Bgn(), grp.End())); // NOTE: grp.Bgn() / .End() is for String pos (bry pos will fail for utf8 strings)
|
||||
}
|
||||
else {
|
||||
tmp_bfr.Add_byte(Byte_ascii.Percent);
|
||||
tmp_bfr.Add_byte(b);
|
||||
}
|
||||
}
|
||||
break;
|
||||
case Byte_ascii.Percent:
|
||||
|
||||
@@ -70,7 +70,8 @@ public class Scrib_lib_ustring__lib_tst {
|
||||
Exec_gsub_regx("a%b%c", "%%(%w+)%%" , -1, Scrib_kv_utl_.flat_many_("b", "B") , "aBc;1");
|
||||
}
|
||||
@Test public void Gsub_capture() {
|
||||
Exec_gsub_regx("aa" , "(a)%1" , 1, "%1z", "az;1"); // capture
|
||||
Exec_gsub_regx("aa" , "(a)%1" , 1, "%0z", "aaz;1"); // capture; handle %0; PAGE:en.w:Wikipedia:Wikipedia_Signpost/Templates/Voter/testcases; DATE:2015-08-02
|
||||
Exec_gsub_regx("aa" , "(a)%1" , 1, "%1z", "az;1"); // capture; handle %1+; PAGE:en.w:Wikipedia:Wikipedia_Signpost/Templates/Voter/testcases; DATE:2015-08-02
|
||||
Exec_gsub_regx("a\"b'c\"d" , "([\"'])(.-)%1" , 1, "%1z", "a\"zd;1"); // capture; http://www.lua.org/pil/20.3.html; {{#invoke:test|gsub_string|a"b'c"d|(["'])(.-)%1|%1z}}
|
||||
}
|
||||
@Test public void Gsub_no_replace() {// PURPOSE: gsub with no replace argument should not fail; EX:d:'orse; DATE:2013-10-14
|
||||
@@ -116,19 +117,19 @@ public class Scrib_lib_ustring__lib_tst {
|
||||
, " 1=2"
|
||||
));
|
||||
}
|
||||
// @Test public void Gsub_frontier_pattern() { // PURPOSE: handle frontier pattern; EX:"%f[%a]"; DATE:2015-07-21
|
||||
// fxt.Init_cbk(Scrib_core.Key_mw_interface, fxt.Core().Lib_ustring(), Scrib_lib_ustring.Invk_gsub);
|
||||
// Exec_gsub_regx("a b c", "%f[%W]", 5, "()", "a() b() c();3");
|
||||
// Exec_gsub_regx("abC DEF gHI JKm NOP", "%f[%a]%u+%f[%A]", Int_.MaxValue, "()", "abC () gHI JKm ();2"); // based on http://lua-users.org/wiki/FrontierPattern
|
||||
// }
|
||||
// @Test public void Gsub_frontier_pattern_utl() {// PURPOSE: standalone test for \0 logic in frontier pattern; note that verified against PHP: echo(preg_match( "/[\w]/us", "\0" )); DATE:2015-07-21
|
||||
// Tfds.Eq(Bool_.N, Regx_adp_.Match("\0", "a")); // \0 not matched by a
|
||||
// Tfds.Eq(Bool_.N, Regx_adp_.Match("\0", "0")); // \0 not matched by numeric 0
|
||||
// Tfds.Eq(Bool_.N, Regx_adp_.Match("\0", "[\\w]")); // \0 not matched by word_char
|
||||
// Tfds.Eq(Bool_.Y, Regx_adp_.Match("\0", "[\\W]")); // \0 matched by !word_char
|
||||
// Tfds.Eq(Bool_.Y, Regx_adp_.Match("\0", "[\\x]")); // \0 matched by any_char
|
||||
// Tfds.Eq(Bool_.Y, Regx_adp_.Match("\0", "[\\X]")); // \0 matched by !any_char
|
||||
// }
|
||||
@Test public void Gsub_frontier_pattern() { // PURPOSE: handle frontier pattern; EX:"%f[%a]"; DATE:2015-07-21
|
||||
fxt.Init_cbk(Scrib_core.Key_mw_interface, fxt.Core().Lib_ustring(), Scrib_lib_ustring.Invk_gsub);
|
||||
Exec_gsub_regx("a b c", "%f[%W]", 5, "()", "a() b() c();3");
|
||||
Exec_gsub_regx("abC DEF gHI JKm NOP", "%f[%a]%u+%f[%A]", Int_.MaxValue, "()", "abC () gHI JKm ();2"); // based on http://lua-users.org/wiki/FrontierPattern
|
||||
}
|
||||
@Test public void Gsub_frontier_pattern_utl() {// PURPOSE: standalone test for \0 logic in frontier pattern; note that verified against PHP: echo(preg_match( "/[\w]/us", "\0" )); DATE:2015-07-21
|
||||
Tfds.Eq(Bool_.N, Regx_adp_.Match("\0", "a")); // \0 not matched by a
|
||||
Tfds.Eq(Bool_.N, Regx_adp_.Match("\0", "0")); // \0 not matched by numeric 0
|
||||
Tfds.Eq(Bool_.N, Regx_adp_.Match("\0", "[\\w]")); // \0 not matched by word_char
|
||||
Tfds.Eq(Bool_.Y, Regx_adp_.Match("\0", "[\\W]")); // \0 matched by !word_char
|
||||
Tfds.Eq(Bool_.Y, Regx_adp_.Match("\0", "[\\x]")); // \0 matched by any_char
|
||||
Tfds.Eq(Bool_.Y, Regx_adp_.Match("\0", "[\\X]")); // \0 matched by !any_char
|
||||
}
|
||||
// @Test public void Match_viwiktionary() {
|
||||
// fxt.Init_cbk(Scrib_core.Key_mw_interface, fxt.Core().Lib_ustring(), Scrib_lib_ustring.Invk_match);
|
||||
// Exec_match("tr" , "()(r)", 1, ";"); // should return all matches
|
||||
|
||||
@@ -17,6 +17,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.xowa.xtns.scribunto.libs; import gplx.*; import gplx.xowa.*; import gplx.xowa.xtns.*; import gplx.xowa.xtns.scribunto.*;
|
||||
import gplx.core.json.*; import gplx.xowa.xtns.wdatas.*; import gplx.xowa.xtns.wdatas.parsers.*;
|
||||
import gplx.xowa.wikis.*;
|
||||
public class Scrib_lib_wikibase implements Scrib_lib {
|
||||
public Scrib_lib_wikibase(Scrib_core core) {this.core = core;} private Scrib_core core;
|
||||
public Scrib_lua_mod Mod() {return mod;} private Scrib_lua_mod mod;
|
||||
@@ -30,35 +31,73 @@ public class Scrib_lib_wikibase implements Scrib_lib {
|
||||
public Scrib_proc_mgr Procs() {return procs;} private Scrib_proc_mgr procs = new Scrib_proc_mgr();
|
||||
public boolean Procs_exec(int key, Scrib_proc_args args, Scrib_proc_rslt rslt) {
|
||||
switch (key) {
|
||||
case Proc_getLabel: return GetLabel(args, rslt);
|
||||
case Proc_getEntity: return GetEntity(args, rslt);
|
||||
case Proc_getSetting: return GetSetting(args, rslt);
|
||||
case Proc_renderSnak: return RenderSnak(args, rslt);
|
||||
case Proc_renderSnaks: return RenderSnaks(args, rslt);
|
||||
case Proc_getEntityId: return GetEntityId(args, rslt);
|
||||
case Proc_getGlobalSiteId: return GetGlobalSiteId(args, rslt);
|
||||
case Proc_getUserLang: return GetUserLang(args, rslt);
|
||||
case Proc_getDescription: return GetDescription(args, rslt);
|
||||
case Proc_resolvePropertyId: return ResolvePropertyId(args, rslt);
|
||||
case Proc_getSiteLinkPageName: return GetSiteLinkPageName(args, rslt);
|
||||
case Proc_incrementExpensiveFunctionCount: return IncrementExpensiveFunctionCount(args, rslt);
|
||||
default: throw Err_.new_unhandled(key);
|
||||
}
|
||||
}
|
||||
private static final int Proc_getEntity = 0, Proc_getEntityId = 1, Proc_getGlobalSiteId = 2;
|
||||
public static final String Invk_getEntity = "getEntity", Invk_getEntityId = "getEntityId", Invk_getGlobalSiteId = "getGlobalSiteId";
|
||||
private static final String[] Proc_names = String_.Ary(Invk_getEntity, Invk_getEntityId, Invk_getGlobalSiteId);
|
||||
private static final int Proc_getLabel = 0, Proc_getEntity = 1, Proc_getSetting = 2, Proc_renderSnak = 3, Proc_renderSnaks = 4, Proc_getEntityId = 5, Proc_getUserLang = 6, Proc_getDescription = 7, Proc_resolvePropertyId = 8, Proc_getSiteLinkPageName = 9, Proc_incrementExpensiveFunctionCount = 10;
|
||||
public static final String Invk_getLabel = "getLabel", Invk_getEntity = "getEntity", Invk_getSetting = "getSetting", Invk_renderSnak = "renderSnak", Invk_renderSnaks = "renderSnaks", Invk_getEntityId = "getEntityId", Invk_getUserLang = "getUserLang", Invk_getDescription = "getDescription", Invk_resolvePropertyId = "resolvePropertyId", Invk_getSiteLinkPageName = "getSiteLinkPageName", Invk_incrementExpensiveFunctionCount = "incrementExpensiveFunctionCount";
|
||||
private static final String[] Proc_names = String_.Ary(Invk_getLabel, Invk_getEntity, Invk_getSetting, Invk_renderSnak, Invk_renderSnaks, Invk_getEntityId, Invk_getUserLang, Invk_getDescription, Invk_resolvePropertyId, Invk_getSiteLinkPageName, Invk_incrementExpensiveFunctionCount);
|
||||
public void Notify_page_changed() {if (notify_page_changed_fnc != null) core.Interpreter().CallFunction(notify_page_changed_fnc.Id(), KeyVal_.Ary_empty);}
|
||||
public boolean GetEntity(Scrib_proc_args args, Scrib_proc_rslt rslt) {
|
||||
byte[] ttl_bry = args.Pull_bry(0);
|
||||
if (Bry_.Len_eq_0(ttl_bry)) return rslt.Init_ary_empty(); // NOTE: some Modules do not pass in an argument; return early, else spurious warning "invalid qid for ttl" (since ttl is blank); EX:w:Module:Authority_control; DATE:2013-10-27
|
||||
boolean base_0 = args.Pull_bool(1);
|
||||
Xowe_wiki wiki = core.Wiki();
|
||||
Wdata_wiki_mgr wdata_mgr = wiki.Appe().Wiki_mgr().Wdata_mgr();
|
||||
Wdata_doc wdoc = wdata_mgr.Pages_get_by_ttl_name(ttl_bry);
|
||||
if (wdoc == null) {Wdata_wiki_mgr.Log_missing_qid(core.Ctx(), ttl_bry); return rslt.Init_ary_empty();}
|
||||
return rslt.Init_obj(Scrib_lib_wikibase_srl.Srl(wdoc, true, base_0));
|
||||
public boolean GetLabel(Scrib_proc_args args, Scrib_proc_rslt rslt) {
|
||||
byte[] ttl_bry = args.Pull_bry(0); if (Bry_.Len_eq_0(ttl_bry)) return rslt.Init_ary_empty();
|
||||
Wdata_doc wdoc = Get_wdoc(ttl_bry); if (wdoc == null) return rslt.Init_ary_empty();
|
||||
return rslt.Init_obj(wdoc.Label_list__get_or_fallback(core.Lang()));
|
||||
}
|
||||
public boolean GetEntity(Scrib_proc_args args, Scrib_proc_rslt rslt) {
|
||||
byte[] ttl_bry = args.Pull_bry(0); if (Bry_.Len_eq_0(ttl_bry)) return rslt.Init_ary_empty(); // NOTE: some Modules do not pass in an argument; return early, else spurious warning "invalid qid for ttl" (since ttl is blank); EX:w:Module:Authority_control; DATE:2013-10-27
|
||||
Wdata_doc wdoc = Get_wdoc(ttl_bry);
|
||||
return wdoc == null ? rslt.Init_ary_empty() : rslt.Init_obj(Scrib_lib_wikibase_srl.Srl(wdoc, true, false)); // "false": wbase now always uses v2; PAGE:ja.w:東京競馬場; DATE:2015-07-28
|
||||
}
|
||||
public boolean GetSetting(Scrib_proc_args args, Scrib_proc_rslt rslt) {throw Err_.new_("wbase", "getSetting not implemented", "url", core.Page().Url().To_str(), "arg", args.Pull_bry(0));}
|
||||
public boolean RenderSnak(Scrib_proc_args args, Scrib_proc_rslt rslt) {throw Err_.new_("wbase", "renderSnak not implemented", "url", core.Page().Url().To_str(), "arg", args.Pull_bry(0));}
|
||||
public boolean RenderSnaks(Scrib_proc_args args, Scrib_proc_rslt rslt) {throw Err_.new_("wbase", "renderSnaks not implemented", "url", core.Page().Url().To_str(), "arg", args.Pull_bry(0));}
|
||||
public boolean GetEntityId(Scrib_proc_args args, Scrib_proc_rslt rslt) {
|
||||
byte[] ttl_bry = args.Pull_bry(0);
|
||||
Xowe_wiki wiki = core.Wiki();
|
||||
Wdata_wiki_mgr wdata_mgr = wiki.Appe().Wiki_mgr().Wdata_mgr();
|
||||
Xoa_ttl ttl = Xoa_ttl.parse_(wiki, ttl_bry);
|
||||
byte[] rv = wdata_mgr.Qids_get(wiki, ttl); if (rv == null) rv = Bry_.Empty;
|
||||
byte[] rv = wiki.Appe().Wiki_mgr().Wdata_mgr().Qids_get(wiki, ttl); if (rv == null) rv = Bry_.Empty;
|
||||
return rslt.Init_obj(rv);
|
||||
}
|
||||
public boolean GetUserLang(Scrib_proc_args args, Scrib_proc_rslt rslt) {
|
||||
return rslt.Init_obj(core.App().Usere().Lang().Key_bry());
|
||||
}
|
||||
public boolean GetDescription(Scrib_proc_args args, Scrib_proc_rslt rslt) {
|
||||
byte[] ttl_bry = args.Pull_bry(0); if (Bry_.Len_eq_0(ttl_bry)) return rslt.Init_ary_empty();
|
||||
Wdata_doc wdoc = Get_wdoc(ttl_bry); if (wdoc == null) return rslt.Init_ary_empty();
|
||||
return rslt.Init_obj(wdoc.Descr_list__get_or_fallback(core.Lang()));
|
||||
}
|
||||
public boolean ResolvePropertyId(Scrib_proc_args args, Scrib_proc_rslt rslt) {
|
||||
byte[] prop = args.Pull_bry(0); if (Bry_.Len_eq_0(prop)) return rslt.Init_ary_empty();
|
||||
Wdata_doc wdoc = Get_wdoc(Bry_.Add(Byte_ascii.Ltr_p, prop)); if (wdoc == null) return rslt.Init_ary_empty();
|
||||
return rslt.Init_obj(wdoc.Label_list__get_or_fallback(core.Lang()));
|
||||
}
|
||||
public boolean GetSiteLinkPageName(Scrib_proc_args args, Scrib_proc_rslt rslt) {
|
||||
byte[] ttl_bry = args.Pull_bry(0); if (Bry_.Len_eq_0(ttl_bry)) return rslt.Init_ary_empty();
|
||||
Wdata_doc wdoc = Get_wdoc(ttl_bry); if (wdoc == null) return rslt.Init_ary_empty();
|
||||
Xow_domain domain_itm = core.Wiki().Domain_itm();
|
||||
if (domain_itm.Abrv_wm() == null) {
|
||||
domain_itm.Abrv_wm_(Xow_wiki_alias.Build_alias(domain_itm));
|
||||
}
|
||||
return rslt.Init_obj(wdoc.Slink_list__get_or_fallback(domain_itm.Abrv_wm()));
|
||||
}
|
||||
public boolean IncrementExpensiveFunctionCount(Scrib_proc_args args, Scrib_proc_rslt rslt) {return rslt.Init_obj(KeyVal_.Ary_empty);} // NOTE: for now, always return null (XOWA does not care about expensive parser functions)
|
||||
public boolean GetGlobalSiteId(Scrib_proc_args args, Scrib_proc_rslt rslt) {
|
||||
return rslt.Init_obj(core.Wiki().Domain_abrv()); // ;siteGlobalID: This site's global ID (e.g. <code>'itwiki'</code>), as used in the sites table. Default: <code>$wgDBname</code>.; REF:/xtns/Wikibase/docs/options.wiki
|
||||
}
|
||||
private Wdata_doc Get_wdoc(byte[] ttl_bry) {
|
||||
Wdata_doc wdoc = core.Wiki().Appe().Wiki_mgr().Wdata_mgr().Pages_get_by_ttl_name(ttl_bry);
|
||||
if (wdoc == null) Wdata_wiki_mgr.Log_missing_qid(core.Ctx(), ttl_bry);
|
||||
return wdoc;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -142,7 +142,7 @@ class Scrib_lib_wikibase_srl {
|
||||
boolean snak_is_valued = itm.Snak_tid() != Wdata_dict_snak_tid.Tid_novalue;
|
||||
int snak_is_valued_adj = snak_is_valued ? 1 : 0;
|
||||
KeyVal[] rv = new KeyVal[2 + snak_is_valued_adj];
|
||||
if (snak_is_valued)
|
||||
if (snak_is_valued) // NOTE: novalue must not return slot (no datavalue node in json); PAGE:ru.w:Лимонов,_Эдуард_Вениаминович; DATE:2015-02-16; ALSO: sv.w:Joseph_Jaquet; DATE:2015-07-31
|
||||
rv[0] = KeyVal_.new_("datavalue", Srl_claims_prop_itm_core_val(itm));
|
||||
rv[0 + snak_is_valued_adj] = KeyVal_.new_("property", pid);
|
||||
rv[1 + snak_is_valued_adj] = KeyVal_.new_("snaktype", Wdata_dict_snak_tid.Xto_str(itm.Snak_tid()));
|
||||
@@ -152,7 +152,7 @@ class Scrib_lib_wikibase_srl {
|
||||
private static KeyVal[] Srl_claims_prop_itm_core_val(Wdata_claim_itm_core itm) {
|
||||
switch (itm.Snak_tid()) {
|
||||
case Wdata_dict_snak_tid.Tid_somevalue: return Datavalue_somevalue;
|
||||
case Wdata_dict_snak_tid.Tid_novalue: return Datavalue_novalue;
|
||||
case Wdata_dict_snak_tid.Tid_novalue: return Datavalue_novalue; // TODO: throw exc
|
||||
default:
|
||||
itm.Welcome(visitor);
|
||||
return visitor.Rv();
|
||||
@@ -160,5 +160,5 @@ class Scrib_lib_wikibase_srl {
|
||||
}
|
||||
public static final String Key_type = "type", Key_value = "value";
|
||||
private static final KeyVal[] Datavalue_somevalue = new KeyVal[] {KeyVal_.new_(Key_type, ""), KeyVal_.new_(Key_value, "")}; // NOTE: must return ""; null fails; EX:w:Joseph-François_Malgaigne; DATE:2014-04-07
|
||||
private static final KeyVal[] Datavalue_novalue = KeyVal_.Ary_empty; // NOTE: novalue must return empty array (no datavalue node in json); PAGE:ru.w:Лимонов,_Эдуард_Вениаминович; DATE:2015-02-16
|
||||
private static final KeyVal[] Datavalue_novalue = KeyVal_.Ary_empty;
|
||||
}
|
||||
|
||||
@@ -46,7 +46,8 @@ class Scrib_lib_wikibase_srl_visitor implements Wdata_claim_visitor {
|
||||
rv[0] = KeyVal_.new_(Wdata_dict_value_monolingualtext.Str_text , String_.new_u8(itm.Text()));
|
||||
rv[1] = KeyVal_.new_(Wdata_dict_value_monolingualtext.Str_language , String_.new_u8(itm.Lang()));
|
||||
return rv;
|
||||
} public void Visit_quantity(Wdata_claim_itm_quantity itm) {
|
||||
}
|
||||
public void Visit_quantity(Wdata_claim_itm_quantity itm) {
|
||||
rv = new KeyVal[2];
|
||||
rv[0] = KeyVal_.new_(Scrib_lib_wikibase_srl.Key_type, Wdata_dict_val_tid.Str_quantity);
|
||||
rv[1] = KeyVal_.new_(Scrib_lib_wikibase_srl.Key_value, Quantity_value(itm));
|
||||
|
||||
@@ -19,21 +19,47 @@ package gplx.xowa.xtns.scribunto.libs; import gplx.*; import gplx.xowa.*; import
|
||||
import org.junit.*;
|
||||
import gplx.xowa.xtns.wdatas.*;
|
||||
public class Scrib_lib_wikibase_tst {
|
||||
private final Scrib_invoke_func_fxt fxt = new Scrib_invoke_func_fxt(); private Scrib_lib lib;
|
||||
private final Wdata_wiki_mgr_fxt wdata_fxt = new Wdata_wiki_mgr_fxt();
|
||||
@Before public void init() {
|
||||
fxt.Clear_for_lib();
|
||||
fxt.Clear_for_lib("en.wikipedia.org", "zh-hans");
|
||||
lib = fxt.Core().Lib_wikibase().Init();
|
||||
} private Scrib_invoke_func_fxt fxt = new Scrib_invoke_func_fxt(); private Scrib_lib lib;
|
||||
@Test public void GetGlobalSiteId() {
|
||||
fxt.Test_scrib_proc_str(lib, Scrib_lib_wikibase.Invk_getGlobalSiteId, Object_.Ary_empty, "enwiki");
|
||||
wdata_fxt.Init(fxt.Parser_fxt(), false);
|
||||
wdata_fxt.Init_lang_fallbacks("zh-hant", "zh-hk");
|
||||
}
|
||||
// @Test public void GetGlobalSiteId() {
|
||||
// fxt.Test_scrib_proc_str(lib, Scrib_lib_wikibase.Invk_getGlobalSiteId, Object_.Ary_empty, "enwiki");
|
||||
// }
|
||||
@Test public void GetEntityId() {
|
||||
Wdata_wiki_mgr_fxt wdata_fxt = new Wdata_wiki_mgr_fxt().Init(fxt.Parser_fxt(), false);
|
||||
wdata_fxt.Init_links_add("enwiki", "Earth", "q2");
|
||||
fxt.Test_scrib_proc_str(lib, Scrib_lib_wikibase.Invk_getEntityId, Object_.Ary("Earth" ), "q2");
|
||||
fxt.Test_scrib_proc_str(lib, Scrib_lib_wikibase.Invk_getEntityId, Object_.Ary("missing_page" ), "");
|
||||
}
|
||||
@Test public void GetLabel__cur() {
|
||||
wdata_fxt.Init_pages_add(wdata_fxt.Wdoc_bldr("q2").Add_label("zh-hans", "s").Add_label("zh-hant", "t").Xto_wdoc());
|
||||
fxt.Test_scrib_proc_str(lib, Scrib_lib_wikibase.Invk_getLabel, Object_.Ary("q2"), "s"); // do not get fallback
|
||||
}
|
||||
@Test public void GetLabel__fallback_1() {
|
||||
wdata_fxt.Init_pages_add(wdata_fxt.Wdoc_bldr("q2").Add_label("zh-hant", "t").Add_label("zh-hk", "h").Xto_wdoc());
|
||||
fxt.Test_scrib_proc_str(lib, Scrib_lib_wikibase.Invk_getLabel, Object_.Ary("q2"), "t"); // get 1st fallback
|
||||
}
|
||||
@Test public void GetLabel__fallback_2() {
|
||||
wdata_fxt.Init_pages_add(wdata_fxt.Wdoc_bldr("q2").Add_label("zh-hk", "hk").Xto_wdoc());
|
||||
fxt.Test_scrib_proc_str(lib, Scrib_lib_wikibase.Invk_getLabel, Object_.Ary("q2"), "hk"); // get 2nd fallback
|
||||
}
|
||||
@Test public void GetLabel__fallback_en() {
|
||||
wdata_fxt.Init_pages_add(wdata_fxt.Wdoc_bldr("q2").Add_label("en", "en").Xto_wdoc());
|
||||
fxt.Test_scrib_proc_str(lib, Scrib_lib_wikibase.Invk_getLabel, Object_.Ary("q2"), "en"); // get en
|
||||
}
|
||||
@Test public void GetDescr() {
|
||||
wdata_fxt.Init_pages_add(wdata_fxt.Wdoc_bldr("q2").Add_description("zh-hans", "s").Add_description("zh-hant", "t").Xto_wdoc());
|
||||
fxt.Test_scrib_proc_str(lib, Scrib_lib_wikibase.Invk_getDescription, Object_.Ary("q2"), "s");
|
||||
}
|
||||
@Test public void GetSlink() {
|
||||
wdata_fxt.Init_pages_add(wdata_fxt.Wdoc_bldr("q2").Add_sitelink("enwiki", "a").Xto_wdoc());
|
||||
fxt.Test_scrib_proc_str(lib, Scrib_lib_wikibase.Invk_getSiteLinkPageName, Object_.Ary("q2"), "a");
|
||||
}
|
||||
@Test public void GetEntity() {
|
||||
Wdata_wiki_mgr_fxt wdata_fxt = new Wdata_wiki_mgr_fxt().Init(fxt.Parser_fxt(), false);
|
||||
wdata_fxt.Init_pages_add(wdata_fxt.Wdoc_bldr("q2").Add_label("en", "b").Xto_wdoc());
|
||||
fxt.Test_scrib_proc_str_ary(lib, Scrib_lib_wikibase.Invk_getEntity, Object_.Ary("q2", false), String_.Concat_lines_nl_skip_last
|
||||
( "1="
|
||||
@@ -47,7 +73,6 @@ public class Scrib_lib_wikibase_tst {
|
||||
));
|
||||
}
|
||||
@Test public void GetEntity_property() { // PURPOSE: getEntity should be able to convert "p2" to "Property:P2"; EX:es.w:Arnold_Gesell; DATE:2014-02-18
|
||||
Wdata_wiki_mgr_fxt wdata_fxt = new Wdata_wiki_mgr_fxt().Init(fxt.Parser_fxt(), false);
|
||||
wdata_fxt.Init_pages_add(wdata_fxt.Wdoc_bldr("Property:p2").Add_label("en", "b").Xto_wdoc());
|
||||
fxt.Test_scrib_proc_str_ary(lib, Scrib_lib_wikibase.Invk_getEntity, Object_.Ary("p2", false), String_.Concat_lines_nl_skip_last
|
||||
( "1="
|
||||
@@ -60,4 +85,8 @@ public class Scrib_lib_wikibase_tst {
|
||||
, " value=b"
|
||||
));
|
||||
}
|
||||
@Test public void ResolvePropertyId() {
|
||||
wdata_fxt.Init_pages_add(wdata_fxt.Wdoc_bldr("Property:p2").Add_label("zh-hans", "prop_a").Xto_wdoc());
|
||||
fxt.Test_scrib_proc_str(lib, Scrib_lib_wikibase.Invk_resolvePropertyId, Object_.Ary("2"), "prop_a");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -95,23 +95,23 @@ public class Scrib_regx_converter {
|
||||
}
|
||||
}
|
||||
break;
|
||||
// case Byte_ascii.Ltr_f: { // EX: lua frontier pattern; "%f[%a]"; DATE:2015-07-21
|
||||
// ++i;
|
||||
// if (i + 1 >= len || src[i] != Byte_ascii.Brack_bgn) throw Err_.new_("scribunto", "missing '[' after %f in pattern at pattern character $ii");
|
||||
//
|
||||
// // %f always followed by bracketed term; convert lua bracketed term to regex
|
||||
// Bry_bfr tmp_bfr = Xoa_app_.Utl__bfr_mkr().Get_b128();
|
||||
// i = bracketedCharSetToRegex(tmp_bfr, src, i, len);
|
||||
// byte[] bracketed_regx = tmp_bfr.To_bry_and_rls();
|
||||
//
|
||||
// // scrib has following comment: 'Because %f considers the beginning and end of the String to be \0, determine if $re2 matches that and take it into account with "^" and "$".'
|
||||
// // if the bracketed_regx is a negative class it will match \0; so, \W means anything not a word char, which will match \0; \w means word char which will not match \0
|
||||
// if (Regx_adp_.Match("\0", String_.new_u8(bracketed_regx)))
|
||||
// bfr.Add_str_a7("(?<!^)(?<!").Add(bracketed_regx).Add_str_a7(")(?=").Add(bracketed_regx).Add_str_a7("|$)"); // match bgn / end of String
|
||||
// else
|
||||
// bfr .Add_str_a7("(?<!").Add(bracketed_regx).Add_str_a7(")(?=").Add(bracketed_regx).Add_str_a7( ")");
|
||||
// break;
|
||||
// }
|
||||
case Byte_ascii.Ltr_f: { // EX: lua frontier pattern; "%f[%a]"; DATE:2015-07-21
|
||||
++i;
|
||||
if (i + 1 >= len || src[i] != Byte_ascii.Brack_bgn) throw Err_.new_("scribunto", "missing '[' after %f in pattern at pattern character $ii");
|
||||
|
||||
// %f always followed by bracketed term; convert lua bracketed term to regex
|
||||
Bry_bfr tmp_bfr = Xoa_app_.Utl__bfr_mkr().Get_b128();
|
||||
i = bracketedCharSetToRegex(tmp_bfr, src, i, len);
|
||||
byte[] bracketed_regx = tmp_bfr.To_bry_and_rls();
|
||||
|
||||
// scrib has following comment: 'Because %f considers the beginning and end of the String to be \0, determine if $re2 matches that and take it into account with "^" and "$".'
|
||||
// if the bracketed_regx is a negative class it will match \0; so, \W means anything not a word char, which will match \0; \w means word char which will not match \0
|
||||
if (Regx_adp_.Match("\0", String_.new_u8(bracketed_regx)))
|
||||
bfr.Add_str_a7("(?<!^)(?<!").Add(bracketed_regx).Add_str_a7(")(?=").Add(bracketed_regx).Add_str_a7("|$)"); // match bgn / end of String
|
||||
else
|
||||
bfr .Add_str_a7("(?<!").Add(bracketed_regx).Add_str_a7(")(?=").Add(bracketed_regx).Add_str_a7( ")");
|
||||
break;
|
||||
}
|
||||
case Byte_ascii.Num_0: case Byte_ascii.Num_1: case Byte_ascii.Num_2: case Byte_ascii.Num_3: case Byte_ascii.Num_4:
|
||||
case Byte_ascii.Num_5: case Byte_ascii.Num_6: case Byte_ascii.Num_7: case Byte_ascii.Num_8: case Byte_ascii.Num_9:
|
||||
grps_len = nxt - Byte_ascii.Num_0;
|
||||
@@ -282,5 +282,5 @@ public class Scrib_regx_converter {
|
||||
brack_hash.Add_if_dupe_use_nth(lua_bry, php_bry); // replace percent_hash definitions
|
||||
}
|
||||
}
|
||||
private final Hash_adp_bry percent_hash = Hash_adp_bry.cs_(), brack_hash = Hash_adp_bry.cs_();
|
||||
private final Hash_adp_bry percent_hash = Hash_adp_bry.cs(), brack_hash = Hash_adp_bry.cs();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user