mirror of
https://github.com/gnosygnu/xowa.git
synced 2026-03-02 03:49:30 +00:00
v2.8.5.1
This commit is contained in:
82
400_xowa/src/gplx/xowa/xtns/scribunto/libs/Kv_ary_utl.java
Normal file
82
400_xowa/src/gplx/xowa/xtns/scribunto/libs/Kv_ary_utl.java
Normal file
@@ -0,0 +1,82 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
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.core.json.*;
|
||||
class Kv_ary_utl {
|
||||
public static KeyVal[] new_(boolean base_1, Object... vals) {
|
||||
int len = vals.length;
|
||||
KeyVal[] rv = new KeyVal[len];
|
||||
for (int i = 0; i < len; ++i)
|
||||
rv[i] = KeyVal_.int_(i + (base_1 ? 1 : 0), vals[i]);
|
||||
return rv;
|
||||
}
|
||||
public static KeyVal[] new_kvs(KeyVal... vals) {return vals;}
|
||||
public static String Ary_to_str(Json_wtr wtr, KeyVal[] ary) {
|
||||
Ary_to_str(wtr, 0, ary);
|
||||
return wtr.To_str_and_clear();
|
||||
}
|
||||
private static void Ary_to_str(Json_wtr wtr, int indent, KeyVal[] ary) {
|
||||
int len = ary.length;
|
||||
for (int i = 0; i < len; ++i) {
|
||||
Ary_to_str__itm(wtr, indent, ary[i]);
|
||||
}
|
||||
}
|
||||
private static void Ary_to_str__itm(Json_wtr wtr, int indent, KeyVal itm) {
|
||||
Object val = itm.Val();
|
||||
Class<?> val_type = val.getClass();
|
||||
int type_tid = Type_adp_.To_tid_type(val_type);
|
||||
if (type_tid == Type_adp_.Tid__obj) {
|
||||
if (Type_adp_.Eq(val_type, KeyVal[].class))
|
||||
Ary_to_str__nde(wtr, indent, itm.Key(), (KeyVal[])itm.Val());
|
||||
else if (Type_adp_.Is_array(val_type))
|
||||
Ary_to_str__ary(wtr, indent, itm.Key(), Array_.cast(itm.Val()));
|
||||
else
|
||||
throw Err_.new_unhandled(type_tid);
|
||||
}
|
||||
else
|
||||
wtr.Kv_obj(Bry_.new_u8(itm.Key()), itm.Val(), type_tid);
|
||||
}
|
||||
private static void Ary_to_str__ary(Json_wtr wtr, int indent, String key, Object array) {
|
||||
wtr.Ary_bgn(key);
|
||||
Ary_to_str__ary_itms(wtr, indent + 1, array);
|
||||
wtr.Ary_end();
|
||||
}
|
||||
private static void Ary_to_str__nde(Json_wtr wtr, int indent, String key, KeyVal[] kv) {
|
||||
wtr.Nde_bgn(key);
|
||||
Ary_to_str(wtr, indent + 1, kv);
|
||||
wtr.Nde_end();
|
||||
}
|
||||
private static void Ary_to_str__ary_itms(Json_wtr wtr, int indent, Object array) {
|
||||
int len = Array_.Len(array);
|
||||
for (int j = 0; j < len; ++j) {
|
||||
Object itm = Array_.Get_at(array, j);
|
||||
Class<?> itm_type = itm.getClass();
|
||||
int itm_type_tid = Type_adp_.To_tid_type(itm_type);
|
||||
if (itm_type_tid == Type_adp_.Tid__obj) {
|
||||
if (Type_adp_.Eq(itm_type, KeyVal.class))
|
||||
Ary_to_str__itm(wtr, indent + 1, (KeyVal)itm);
|
||||
if (Type_adp_.Is_array(itm_type))
|
||||
Ary_to_str__ary_itms(wtr, indent + 1, Array_.cast(itm));
|
||||
else
|
||||
throw Err_.new_unhandled(itm_type);
|
||||
}
|
||||
else
|
||||
wtr.Ary_itm_by_type_tid(itm_type_tid, itm);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -93,7 +93,7 @@ public class Scrib_lib_language implements Scrib_lib {
|
||||
}
|
||||
public boolean IsValidCode(Scrib_proc_args args, Scrib_proc_rslt rslt) { // REF.MW: Language.php!isValidCode
|
||||
byte[] lang_code = args.Pull_bry(0);
|
||||
boolean valid = Xoa_ttl.parse_(core.Wiki(), lang_code) != null; // NOTE: MW calls Title::getTitleInvalidRegex()
|
||||
boolean valid = Xoa_ttl.parse(core.Wiki(), lang_code) != null; // NOTE: MW calls Title::getTitleInvalidRegex()
|
||||
if (valid) {
|
||||
int len = lang_code.length;
|
||||
for (int i = 0; i < len; i++) {
|
||||
@@ -170,7 +170,7 @@ public class Scrib_lib_language implements Scrib_lib {
|
||||
if (kv_ary != null) {
|
||||
Object skip_commafy_obj = KeyVal_.Ary_get_by_key_or_null(kv_ary, "noCommafy");
|
||||
if (skip_commafy_obj != null)
|
||||
skip_commafy = Bool_.cast_(skip_commafy_obj);
|
||||
skip_commafy = Bool_.cast(skip_commafy_obj);
|
||||
}
|
||||
}
|
||||
byte[] rv = lang.Num_mgr().Format_num(num, skip_commafy);
|
||||
|
||||
@@ -76,7 +76,7 @@ class Scrib_lib_message_data {
|
||||
break;
|
||||
case Key_tid_rawMessage: raw_msg_key = kv.Val_to_bry(); break;
|
||||
case Key_tid_lang: lang_key = kv.Val_to_bry(); break;
|
||||
case Key_tid_useDB: use_db = Bool_.cast_(kv.Val()); break;
|
||||
case Key_tid_useDB: use_db = Bool_.cast(kv.Val()); break;
|
||||
case Key_tid_title: title_bry = kv.Val_to_bry(); break;
|
||||
case Key_tid_params:
|
||||
KeyVal[] args_ary = (KeyVal[])kv.Val();
|
||||
@@ -96,7 +96,7 @@ class Scrib_lib_message_data {
|
||||
if (data_ttl == null)
|
||||
ttl = ctx.Cur_page().Ttl();
|
||||
else
|
||||
ttl = Xoa_ttl.parse_(wiki, data_ttl);
|
||||
ttl = Xoa_ttl.parse(wiki, data_ttl);
|
||||
}
|
||||
if (raw_msg_key != null) {
|
||||
Xol_msg_itm raw_msg_itm = new Xol_msg_itm(-1, Bry_.Empty);
|
||||
|
||||
@@ -94,7 +94,7 @@ public class Scrib_lib_mw implements Scrib_lib {
|
||||
String mod_code = fsys_mgr.Get_or_null(mod_name); // check if mod_name a file in /lualib/ directoryScribunto .lua file (in /lualib/)
|
||||
if (mod_code != null)
|
||||
return rslt.Init_obj(core.Interpreter().LoadString("@" + mod_name + ".lua", mod_code));
|
||||
Xoa_ttl ttl = Xoa_ttl.parse_(cur_wiki, Bry_.new_u8(mod_name));// NOTE: should have Module: prefix
|
||||
Xoa_ttl ttl = Xoa_ttl.parse(cur_wiki, Bry_.new_u8(mod_name));// NOTE: should have Module: prefix
|
||||
if (ttl == null) return rslt.Init_ary_empty();
|
||||
Xoae_page page = cur_wiki.Data_mgr().Get_page(ttl, false);
|
||||
if (page.Missing()) return rslt.Init_ary_empty();
|
||||
@@ -109,9 +109,9 @@ public class Scrib_lib_mw implements Scrib_lib {
|
||||
Xot_invk frame = Scrib_frame_.Get_frame(core, frame_id);
|
||||
int frame_arg_adj = Scrib_frame_.Get_arg_adj(frame.Frame_tid());
|
||||
String idx_str = args.Pull_str(1);
|
||||
int idx_int = Int_.parse_or_(idx_str, Int_.MinValue); // NOTE: should not receive int value < -1; idx >= 0
|
||||
int idx_int = Int_.parse_or(idx_str, Int_.Min_value); // NOTE: should not receive int value < -1; idx >= 0
|
||||
Bry_bfr tmp_bfr = Bry_bfr.new_(); // NOTE: do not make modular level variable, else random failures; DATE:2013-10-14
|
||||
if (idx_int != Int_.MinValue) { // idx is integer
|
||||
if (idx_int != Int_.Min_value) { // idx is integer
|
||||
Arg_nde_tkn nde = Get_arg(frame, idx_int, frame_arg_adj);
|
||||
//frame.Args_eval_by_idx(core.Ctx().Src(), idx_int); // NOTE: arg[0] is always MW function name; EX: {{#invoke:Mod_0|Func_0|Arg_1}}; arg_x = "Mod_0"; args[0] = "Func_0"; args[1] = "Arg_1"
|
||||
if (nde == null) return rslt.Init_ary_empty();
|
||||
@@ -183,13 +183,13 @@ public class Scrib_lib_mw implements Scrib_lib {
|
||||
nde.Key_tkn().Tmpl_evaluate(ctx, src, parent_frame, tmp_bfr);
|
||||
int key_len = tmp_bfr.Len();
|
||||
boolean key_missing = key_len == 0;
|
||||
String key_as_str = null; int key_as_int = Int_.MinValue;
|
||||
String key_as_str = null; int key_as_int = Int_.Min_value;
|
||||
boolean key_is_str = false;
|
||||
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_.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_int = Bry_.To_int_or(tmp_bfr.Bfr(), 0, tmp_bfr.Len(), Int_.Min_value);
|
||||
if (key_as_int == Int_.Min_value) { // key is not int; create str
|
||||
key_as_str = tmp_bfr.Xto_str_and_clear();
|
||||
key_is_str = true;
|
||||
}
|
||||
@@ -299,10 +299,10 @@ public class Scrib_lib_mw implements Scrib_lib {
|
||||
public boolean ExpandTemplate(Scrib_proc_args args, Scrib_proc_rslt rslt) {
|
||||
String ttl_str = args.Pull_str(1);
|
||||
byte[] ttl_bry = Bry_.new_u8(ttl_str);
|
||||
Xoa_ttl ttl = Xoa_ttl.parse_(cur_wiki, ttl_bry); // parse directly; handles titles where template is already part of title; EX: "Template:A"
|
||||
Xoa_ttl ttl = Xoa_ttl.parse(cur_wiki, ttl_bry); // parse directly; handles titles where template is already part of title; EX: "Template:A"
|
||||
if (ttl == null) return rslt.Init_ary_empty(); // invalid ttl;
|
||||
if (!ttl.ForceLiteralLink() && ttl.Ns().Id_main()) // title is not literal and is not prefixed with Template; parse again as template; EX: ":A" and "Template:A" are fine; "A" is parsed again as "Template:A"
|
||||
ttl = Xoa_ttl.parse_(cur_wiki, Bry_.Add(cur_wiki.Ns_mgr().Ns_template().Name_db_w_colon(), ttl_bry)); // parse again, but add "Template:"
|
||||
ttl = Xoa_ttl.parse(cur_wiki, Bry_.Add(cur_wiki.Ns_mgr().Ns_template().Name_db_w_colon(), ttl_bry)); // parse again, but add "Template:"
|
||||
KeyVal[] args_ary = args.Pull_kv_ary(2);
|
||||
// BLOCK.bgn:Xot_invk_tkn.Transclude; cannot reuse b/c Transclude needs invk_tkn, and invk_tkn is manufactured late; DATE:2014-01-02
|
||||
byte[] sub_src = null;
|
||||
@@ -350,10 +350,10 @@ public class Scrib_lib_mw implements Scrib_lib {
|
||||
Xoa_ttl ttl = null;
|
||||
if (Type_adp_.ClassOf_obj(ttl_obj) != String.class) { // title = false
|
||||
byte[] ttl_bry = frame.Frame_ttl();
|
||||
ttl = Xoa_ttl.parse_(core.Wiki(), ttl_bry);
|
||||
ttl = Xoa_ttl.parse(core.Wiki(), ttl_bry);
|
||||
}
|
||||
else {
|
||||
ttl = Xoa_ttl.parse_(cur_wiki, Bry_.new_u8((String)ttl_obj));
|
||||
ttl = Xoa_ttl.parse(cur_wiki, Bry_.new_u8((String)ttl_obj));
|
||||
if (ttl == null) throw Err_.new_wo_type("newChild: invalid title", "title", (String)ttl_obj);
|
||||
}
|
||||
KeyVal[] args_ary = args.Pull_kv_ary(2);
|
||||
@@ -385,9 +385,9 @@ class Scrib_lib_mw_callParserFunction_sorter implements gplx.lists.ComparerAble
|
||||
if (lhs_is_int != rhs_is_int) // different types (int vs String or String vs int)
|
||||
return lhs_is_int ? CompareAble_.Less : CompareAble_.More; // sort ints before strings
|
||||
if (lhs_is_int) // both are ints
|
||||
return Int_.Compare(Int_.cast_(lhs_key), Int_.cast_(rhs_key));
|
||||
return Int_.Compare(Int_.cast(lhs_key), Int_.cast(rhs_key));
|
||||
else // both are strings
|
||||
return String_.Compare(String_.cast_(lhs_key), String_.cast_(rhs_key));
|
||||
return String_.Compare(String_.cast(lhs_key), String_.cast(rhs_key));
|
||||
}
|
||||
public static final Scrib_lib_mw_callParserFunction_sorter _ = new Scrib_lib_mw_callParserFunction_sorter(); Scrib_lib_mw_callParserFunction_sorter() {}
|
||||
}
|
||||
|
||||
@@ -52,27 +52,25 @@ public class Scrib_lib_text implements Scrib_lib {
|
||||
return rslt.Init_obj(Html_entity_);
|
||||
} private static KeyVal[] Html_entity_;
|
||||
public boolean JsonEncode(Scrib_proc_args args, Scrib_proc_rslt rslt) {
|
||||
// KeyVal[] kv_ary = args.Pull_kv_ary(0);
|
||||
// int flags = args.Cast_int_or(1, 0);
|
||||
// if (!Enm_.Has_int(flags, Scrib_lib_text__json_util.Flag__preserve_keys))
|
||||
// kv_ary = json_util.Reindex_arrays(kv_ary, true);
|
||||
// byte[] rv = json_util.Encode(kv_ary, flags & Scrib_lib_text__json_util.Flag__pretty, Scrib_lib_text__json_util.Skip__all);
|
||||
// if (rv == null) throw Err_.new_("scribunto", "mw.text.jsonEncode: Unable to encode value");
|
||||
// return rslt.Init_obj(rv);
|
||||
return false;
|
||||
KeyVal[] kv_ary = args.Pull_kv_ary(0);
|
||||
int flags = args.Cast_int_or(1, 0);
|
||||
if (!Enm_.Has_int(flags, Scrib_lib_text__json_util.Flag__preserve_keys))
|
||||
kv_ary = json_util.Reindex_arrays(kv_ary, true);
|
||||
byte[] rv = json_util.Encode(kv_ary, flags & Scrib_lib_text__json_util.Flag__pretty, Scrib_lib_text__json_util.Skip__all);
|
||||
if (rv == null) throw Err_.new_("scribunto", "mw.text.jsonEncode: Unable to encode value");
|
||||
return rslt.Init_obj(rv);
|
||||
}
|
||||
public boolean JsonDecode(Scrib_proc_args args, Scrib_proc_rslt rslt) {
|
||||
// byte[] json = args.Pull_bry(0);
|
||||
// int flags = args.Cast_int_or(1, 0);
|
||||
// int opts = Scrib_lib_text__json_util.Opt__force_assoc;
|
||||
// if (Enm_.Has_int(flags, Scrib_lib_text__json_util.Flag__try_fixing))
|
||||
// opts = Enm_.Add_int(opts, Scrib_lib_text__json_util.Flag__try_fixing);
|
||||
// KeyVal[] rv = json_util.Decode(json, opts);
|
||||
// if (rv == null) throw Err_.new_("scribunto", "mw.text.jsonEncode: Unable to decode String " + String_.new_u8(json));
|
||||
// if (!(Enm_.Has_int(flags, Scrib_lib_text__json_util.Flag__preserve_keys)))
|
||||
// rv = json_util.Reindex_arrays(rv, false);
|
||||
// return rslt.Init_obj(rv);
|
||||
return false;
|
||||
byte[] json = args.Pull_bry(0);
|
||||
int flags = args.Cast_int_or(1, 0);
|
||||
int opts = Scrib_lib_text__json_util.Opt__force_assoc;
|
||||
if (Enm_.Has_int(flags, Scrib_lib_text__json_util.Flag__try_fixing))
|
||||
opts = Enm_.Add_int(opts, Scrib_lib_text__json_util.Flag__try_fixing);
|
||||
KeyVal[] rv = json_util.Decode(core.App().Utl__json_parser(), json, opts);
|
||||
if (rv == null) throw Err_.new_("scribunto", "mw.text.jsonEncode: Unable to decode String " + String_.new_u8(json));
|
||||
if (!(Enm_.Has_int(flags, Scrib_lib_text__json_util.Flag__preserve_keys)))
|
||||
rv = json_util.Reindex_arrays(rv, false);
|
||||
return rslt.Init_obj(rv);
|
||||
}
|
||||
public void Notify_wiki_changed() {if (notify_wiki_changed_fnc != null) core.Interpreter().CallFunction(notify_wiki_changed_fnc.Id(), KeyVal_.Ary_empty);}
|
||||
public boolean Init_text_for_wiki(Scrib_proc_args args, Scrib_proc_rslt rslt) {
|
||||
|
||||
@@ -34,8 +34,8 @@ class Scrib_lib_text__json_util {
|
||||
if (kv_val != null && Type_adp_.Eq(kv_val.getClass(), KeyVal[].class))
|
||||
kv_val = Reindex_arrays((KeyVal[])kv_val, is_encoding);
|
||||
if (is_sequence) {
|
||||
if (kv.Key_tid() == KeyVal_.Key_tid_int) {
|
||||
int kv_key_as_int = Int_.cast_(kv.Key_as_obj());
|
||||
if (kv.Key_tid() == Type_adp_.Tid__int) {
|
||||
int kv_key_as_int = Int_.cast(kv.Key_as_obj());
|
||||
is_sequence = next++ == kv_key_as_int;
|
||||
// } elseif ( $isEncoding && ctype_digit( $k ) ) {
|
||||
// // json_decode currently doesn't return integer keys for {}
|
||||
@@ -54,8 +54,44 @@ class Scrib_lib_text__json_util {
|
||||
}
|
||||
return kv_ary;
|
||||
}
|
||||
public KeyVal[] Decode(byte[] src, int flag) {
|
||||
return null;
|
||||
public KeyVal[] Decode(Json_parser parser, byte[] src, int flag) {
|
||||
Json_doc jdoc = parser.Parse(src);
|
||||
Json_nde root = jdoc.Root_nde();
|
||||
int len = root.Len();
|
||||
KeyVal[] rv = new KeyVal[len];
|
||||
for (int i = 0; i < len; ++i) {
|
||||
Json_kv json_kv = root.Get_at_as_kv(i);
|
||||
String kv_str = json_kv.Key_as_str();
|
||||
Object kv_val = Decode_obj(json_kv.Val());
|
||||
rv[i] = KeyVal_.new_(kv_str, kv_val);
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
private Object Decode_obj(Json_itm itm) {
|
||||
int itm_tid = itm.Tid();
|
||||
switch (itm_tid) {
|
||||
case Json_itm_.Tid__ary: return Decode_ary(Json_ary.cast(itm));
|
||||
case Json_itm_.Tid__nde: return Decode_nde(Json_nde.cast(itm));
|
||||
default: return itm.Data();
|
||||
}
|
||||
}
|
||||
private Object Decode_ary(Json_ary ary) {
|
||||
int len = ary.Len();
|
||||
Object rv = Array_.Create(int.class, len);
|
||||
for (int i = 0; i < len; ++i) {
|
||||
Json_itm itm = ary.Get_at(i);
|
||||
Array_.Set(rv, i, Decode_obj(itm));
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
private KeyVal[] Decode_nde(Json_nde nde) {
|
||||
int len = nde.Len();
|
||||
KeyVal[] rv = new KeyVal[len];
|
||||
for (int i = 0; i < len; ++i) {
|
||||
Json_kv itm = nde.Get_at_as_kv(i);
|
||||
rv[i] = KeyVal_.new_(itm.Key_as_str(), Decode_obj(itm.Val()));
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
public byte[] Encode(KeyVal[] kv_ary, int flag, int skip) {
|
||||
synchronized (wtr ) {
|
||||
@@ -87,11 +123,11 @@ class Scrib_lib_text__json_util {
|
||||
wtr.Ary_itm_obj(Array_.Get_at(ary, j));
|
||||
wtr.Ary_end();
|
||||
}
|
||||
else if (Type_adp_.Eq(type, Int_.Cls_ref_type)) wtr.Kv_int(kv.Key(), Int_.cast_(kv_val));
|
||||
else if (Type_adp_.Eq(type, Long_.Cls_ref_type)) wtr.Kv_long(kv.Key(), Long_.cast_(kv_val));
|
||||
else if (Type_adp_.Eq(type, Float_.Cls_ref_type)) wtr.Kv_float(kv.Key(), Float_.cast_(kv_val));
|
||||
else if (Type_adp_.Eq(type, Double_.Cls_ref_type)) wtr.Kv_double(kv.Key(), Double_.cast_(kv_val));
|
||||
else if (Type_adp_.Eq(type, Bool_.Cls_ref_type)) wtr.Kv_bool(kv.Key(), Bool_.cast_(kv_val));
|
||||
else if (Type_adp_.Eq(type, Int_.Cls_ref_type)) wtr.Kv_int(kv.Key(), Int_.cast(kv_val));
|
||||
else if (Type_adp_.Eq(type, Long_.Cls_ref_type)) wtr.Kv_long(kv.Key(), Long_.cast(kv_val));
|
||||
else if (Type_adp_.Eq(type, Float_.Cls_ref_type)) wtr.Kv_float(kv.Key(), Float_.cast(kv_val));
|
||||
else if (Type_adp_.Eq(type, Double_.Cls_ref_type)) wtr.Kv_double(kv.Key(), Double_.cast(kv_val));
|
||||
else if (Type_adp_.Eq(type, Bool_.Cls_ref_type)) wtr.Kv_bool(kv.Key(), Bool_.cast(kv_val));
|
||||
else wtr.Kv_str(kv.Key(), Object_.Xto_str_strict_or_null(kv_val));
|
||||
}
|
||||
public static final int
|
||||
|
||||
@@ -16,9 +16,10 @@ 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 org.junit.*;
|
||||
import org.junit.*; import gplx.core.json.*;
|
||||
public class Scrib_lib_text_tst {
|
||||
private Scrib_invoke_func_fxt fxt = new Scrib_invoke_func_fxt(); private Scrib_lib_text lib;
|
||||
private Scrib_lib_json_fxt json_fxt = new Scrib_lib_json_fxt();
|
||||
@Before public void init() {
|
||||
fxt.Clear_for_lib();
|
||||
lib = fxt.Core().Lib_text();
|
||||
@@ -32,45 +33,106 @@ public class Scrib_lib_text_tst {
|
||||
KeyVal[] actl = fxt.Test_scrib_proc_rv_as_kv_ary(lib, Scrib_lib_text.Invk_getEntityTable, Object_.Ary());
|
||||
Tfds.Eq(1510, actl.length); // large result; only test # of entries
|
||||
}
|
||||
// @Test public void JsonEncode() {
|
||||
// fxt.Test_scrib_proc_str_ary(lib, Scrib_lib_text.Invk_jsonEncode, Object_.Ary((Object)Kv_ary_utl.new_
|
||||
// ( true
|
||||
// , true
|
||||
// , 1
|
||||
// , "a"
|
||||
// , new int[] {1, 2, 3}
|
||||
// , Kv_ary_utl.new_(true, "b")
|
||||
// )), String_.Concat_lines_nl_skip_last
|
||||
// ( "1="
|
||||
// + "{ '1':true"
|
||||
// , ", '2':1"
|
||||
// , ", '3':'a'"
|
||||
// , ", '4':"
|
||||
// , " [ 1"
|
||||
// , " , 2"
|
||||
// , " , 3"
|
||||
// , " ]"
|
||||
// , ", '5':"
|
||||
// , " { '1':'b'"
|
||||
// , " }"
|
||||
// , "}"
|
||||
@Test public void JsonEncode() {
|
||||
json_fxt.Test_json_encode(fxt, lib, Kv_ary_utl.new_
|
||||
( true
|
||||
, true
|
||||
, 1
|
||||
, "a"
|
||||
, new int[] {1, 2, 3}
|
||||
, Kv_ary_utl.new_(true, "b")
|
||||
), String_.Concat_lines_nl_skip_last
|
||||
( "1="
|
||||
+ "{ '1':true"
|
||||
, ", '2':1"
|
||||
, ", '3':'a'"
|
||||
, ", '4':"
|
||||
, " [ 1"
|
||||
, " , 2"
|
||||
, " , 3"
|
||||
, " ]"
|
||||
, ", '5':"
|
||||
, " { '1':'b'"
|
||||
, " }"
|
||||
, "}"
|
||||
));
|
||||
}
|
||||
@Test public void JsonDecode() {
|
||||
json_fxt.Test_json_decode(fxt, lib
|
||||
, String_.Concat_lines_nl_skip_last
|
||||
( "{ '1':true"
|
||||
, ", '2':1"
|
||||
, ", '3':'a'"
|
||||
, ", '4':"
|
||||
, " [ 1"
|
||||
, " , 2"
|
||||
, " , 3"
|
||||
, " ]"
|
||||
, ", '5':"
|
||||
, " { '1':'b'"
|
||||
, " }"
|
||||
, "}"
|
||||
)
|
||||
, Kv_ary_utl.new_
|
||||
( true
|
||||
, true
|
||||
, 1
|
||||
, "a"
|
||||
, new int[] {1, 2, 3}
|
||||
, Kv_ary_utl.new_(true, "b")
|
||||
));
|
||||
}
|
||||
@Test public void JsonDecode__primitives() {
|
||||
json_fxt.Test_json_decode(fxt, lib
|
||||
, String_.Concat_lines_nl_skip_last
|
||||
( "{ 'int':1"
|
||||
, ", 'String':'abc'"
|
||||
, ", 'true':true"
|
||||
, "}"
|
||||
)
|
||||
, Kv_ary_utl.new_kvs
|
||||
( KeyVal_.new_("int", 1)
|
||||
, KeyVal_.new_("String", "abc")
|
||||
, KeyVal_.new_("true", true)
|
||||
));
|
||||
}
|
||||
@Test public void JsonDecode__numeric_keys() {
|
||||
json_fxt.Test_json_decode(fxt, lib
|
||||
, String_.Concat_lines_nl_skip_last
|
||||
( "{ 'x':'x'"
|
||||
, ", '1':1"
|
||||
, ", '2':2"
|
||||
, "}"
|
||||
)
|
||||
, Kv_ary_utl.new_kvs
|
||||
( KeyVal_.new_("x", "x")
|
||||
, KeyVal_.new_("1", 1)
|
||||
, KeyVal_.new_("2", 2)
|
||||
));
|
||||
}
|
||||
// @Test public void JsonDecode__array() {
|
||||
// json_fxt.Test_json_decode(fxt, lib
|
||||
// , String_.Concat_lines_nl_skip_last
|
||||
// ( "[ 1"
|
||||
// , ", 2"
|
||||
// , ", 3"
|
||||
// , "]"
|
||||
// )
|
||||
// , Kv_ary_utl.new_kvs
|
||||
// ( KeyVal_.new_("int", 1)
|
||||
// , KeyVal_.new_("String", "abc")
|
||||
// , KeyVal_.new_("true", true)
|
||||
// ));
|
||||
// }
|
||||
}
|
||||
class Kv_ary_utl {
|
||||
public static KeyVal[] new_(boolean base_1, Object... vals) {
|
||||
int len = vals.length;
|
||||
KeyVal[] rv = new KeyVal[len];
|
||||
for (int i = 0; i < len; ++i)
|
||||
rv[i] = KeyVal_.int_(i + (base_1 ? 1 : 0), vals[i]);
|
||||
return rv;
|
||||
class Scrib_lib_json_fxt {
|
||||
private final Json_wtr wtr = new Json_wtr().Opt_quote_byte_(Byte_ascii.Apos);
|
||||
public void Test_json_decode(Scrib_invoke_func_fxt fxt, Scrib_lib_text lib, String raw, KeyVal[] expd) {
|
||||
raw = String_.Replace(raw, "'", "\"");
|
||||
KeyVal[] actl = fxt.Test_scrib_proc_rv_as_kv_ary(lib, Scrib_lib_text.Invk_jsonDecode, Object_.Ary(raw));
|
||||
Tfds.Eq_str_lines(Kv_ary_utl.Ary_to_str(wtr, expd), Kv_ary_utl.Ary_to_str(wtr, actl), raw);
|
||||
}
|
||||
public void Test_json_encode(Scrib_invoke_func_fxt fxt, Scrib_lib_text lib, KeyVal[] raw, String expd) {
|
||||
fxt.Test_scrib_proc_str_ary(lib, Scrib_lib_text.Invk_jsonEncode, Object_.Ary((Object)raw), expd);
|
||||
}
|
||||
}
|
||||
// class Scrib_lib_text_fxt {
|
||||
// public void Test_json_decode(Scrib_invoke_func_fxt fxt, Scrib_lib_text lib, KeyVal[] kv_ary, String expd) {
|
||||
// fxt.Test_scrib_proc_str_ary(lib, Scrib_lib_text.Invk_jsonEncode, Object_.Ary((Object)Scrib_lib_language_tst.Kv_ary_("a")), String_.Concat_lines_nl_skip_last
|
||||
// ( "1={ '0':'a'"
|
||||
// , "}"
|
||||
// ));
|
||||
// }
|
||||
// }
|
||||
|
||||
@@ -68,7 +68,7 @@ public class Scrib_lib_title implements Scrib_lib {
|
||||
Bry_bfr bfr = wiki.Utl__bfr_mkr().Get_b512();
|
||||
ttl_bry = bfr.Add(ns_bry).Add_byte(Byte_ascii.Colon).Add(ttl_bry).To_bry_and_rls();
|
||||
}
|
||||
Xoa_ttl ttl = Xoa_ttl.parse_(core.Wiki(), ttl_bry);
|
||||
Xoa_ttl ttl = Xoa_ttl.parse(core.Wiki(), ttl_bry);
|
||||
if (ttl == null) return rslt.Init_obj(null); // invalid title; exit; matches MW logic
|
||||
return rslt.Init_obj(GetInexpensiveTitleData(ttl));
|
||||
}
|
||||
@@ -82,7 +82,7 @@ public class Scrib_lib_title implements Scrib_lib {
|
||||
byte url_func_tid = ((Byte_obj_val)url_func_obj).Val();
|
||||
byte[] qry_bry = args.Extract_qry_args(wiki, 2);
|
||||
// byte[] proto = Scrib_kv_utl_.Val_to_bry_or(values, 3, null); // NOTE: Scribunto has more conditional logic around argument 2 and setting protocols; DATE:2014-07-07
|
||||
Xoa_ttl ttl = Xoa_ttl.parse_(wiki, ttl_bry); if (ttl == null) return rslt.Init_obj(null);
|
||||
Xoa_ttl ttl = Xoa_ttl.parse(wiki, ttl_bry); if (ttl == null) return rslt.Init_obj(null);
|
||||
Bry_bfr bfr = wiki.Appe().Utl__bfr_mkr().Get_b512();
|
||||
//if (url_func_tid == Pfunc_urlfunc.Tid_full) {
|
||||
// if (proto == null) proto = Proto_relative;
|
||||
@@ -102,9 +102,9 @@ public class Scrib_lib_title implements Scrib_lib {
|
||||
// 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 (Type_adp_.Eq_typeSafe(ns_obj, String.class))
|
||||
return Bry_.new_u8(String_.cast_(ns_obj));
|
||||
return Bry_.new_u8(String_.cast(ns_obj));
|
||||
else {
|
||||
int ns_id = Int_.cast_(ns_obj);
|
||||
int ns_id = Int_.cast(ns_obj);
|
||||
Xow_ns ns = wiki.Ns_mgr().Ids_get_or_null(ns_id);
|
||||
if (ns != null)
|
||||
return ns.Name_bry();
|
||||
@@ -123,13 +123,13 @@ public class Scrib_lib_title implements Scrib_lib {
|
||||
tmp_bfr.Add(ns_bry).Add_byte(Byte_ascii.Colon);
|
||||
tmp_bfr.Add_str(ttl_str);
|
||||
if (anchor_str != null) tmp_bfr.Add_byte(Byte_ascii.Hash).Add_str(anchor_str);
|
||||
Xoa_ttl ttl = Xoa_ttl.parse_(wiki, tmp_bfr.To_bry_and_rls());
|
||||
Xoa_ttl ttl = Xoa_ttl.parse(wiki, tmp_bfr.To_bry_and_rls());
|
||||
if (ttl == null) return rslt.Init_obj(null); // invalid title; exit;
|
||||
return rslt.Init_obj(GetInexpensiveTitleData(ttl));
|
||||
}
|
||||
public boolean GetExpensiveData(Scrib_proc_args args, Scrib_proc_rslt rslt) {
|
||||
byte[] ttl_bry = args.Pull_bry(0);
|
||||
Xoa_ttl ttl = Xoa_ttl.parse_(core.Wiki(), ttl_bry);
|
||||
Xoa_ttl ttl = Xoa_ttl.parse(core.Wiki(), ttl_bry);
|
||||
if (ttl == Xoa_ttl.Null) return rslt.Init_null();
|
||||
// TODO: MW does extra logic here to cache ttl in ttl cache to avoid extra title lookups
|
||||
boolean ttl_exists = false, ttl_redirect = false; int ttl_id = 0;
|
||||
@@ -150,11 +150,11 @@ public class Scrib_lib_title implements Scrib_lib {
|
||||
public boolean GetFileInfo(Scrib_proc_args args, Scrib_proc_rslt rslt) {
|
||||
byte[] ttl_bry = args.Pull_bry(0);
|
||||
Xowe_wiki wiki = core.Wiki();
|
||||
Xoa_ttl ttl = Xoa_ttl.parse_(wiki, ttl_bry);
|
||||
Xoa_ttl ttl = Xoa_ttl.parse(wiki, ttl_bry);
|
||||
if ( ttl == null
|
||||
|| !ttl.Ns().Id_file_or_media()
|
||||
) return rslt.Init_obj(GetFileInfo_absent);
|
||||
if (ttl.Ns().Id_media()) ttl = Xoa_ttl.parse_(wiki, Xow_ns_.Id_file, ttl.Page_db()); // if [[Media:]] change to [[File:]]; theoretically, this should be changed in Get_page, but not sure if I want to put this logic that low; DATE:2014-01-07
|
||||
if (ttl.Ns().Id_media()) ttl = Xoa_ttl.parse(wiki, Xow_ns_.Id_file, ttl.Page_db()); // if [[Media:]] change to [[File:]]; theoretically, this should be changed in Get_page, but not sure if I want to put this logic that low; DATE:2014-01-07
|
||||
// Xoae_page file_page = Pfunc_filepath.Load_page(wiki, ttl); // EXPENSIVE
|
||||
// boolean exists = !file_page.Missing();
|
||||
// if (!exists) return rslt.Init_obj(KeyVal_.Ary(KeyVal_.new_("exists", false))); // NOTE: do not reinstate; will exit early if commons is not installed; DATE:2015-01-25; NOTE: Media objects are often flagged as absent in offline mode
|
||||
@@ -174,7 +174,7 @@ public class Scrib_lib_title implements Scrib_lib {
|
||||
public boolean GetContent(Scrib_proc_args args, Scrib_proc_rslt rslt) {
|
||||
byte[] ttl_bry = args.Pull_bry(0);
|
||||
Xowe_wiki wiki = core.Wiki();
|
||||
Xoa_ttl ttl = Xoa_ttl.parse_(wiki, ttl_bry); if (ttl == null) return rslt.Init_obj(null);
|
||||
Xoa_ttl ttl = Xoa_ttl.parse(wiki, ttl_bry); if (ttl == null) return rslt.Init_obj(null);
|
||||
Xow_page_cache_itm page_itm = wiki.Cache_mgr().Page_cache().Get_or_load_as_itm(ttl);
|
||||
byte[] rv = null;
|
||||
if (page_itm != null) {
|
||||
@@ -194,13 +194,13 @@ public class Scrib_lib_title implements Scrib_lib {
|
||||
public boolean ProtectionLevels(Scrib_proc_args args, Scrib_proc_rslt rslt) {
|
||||
// byte[] ttl_bry = args.Pull_bry(0);
|
||||
// Xowe_wiki wiki = core.Wiki();
|
||||
// Xoa_ttl ttl = Xoa_ttl.parse_(wiki, ttl_bry); if (ttl == null) return rslt.Init_obj(null);
|
||||
// Xoa_ttl ttl = Xoa_ttl.parse(wiki, ttl_bry); if (ttl == null) return rslt.Init_obj(null);
|
||||
return rslt.Init_many_objs(""); // NOTE: always return no protection; protectionLevels are stored in different table which is currently not mirrored; DATE:2014-04-09
|
||||
}
|
||||
public boolean CascadingProtection(Scrib_proc_args args, Scrib_proc_rslt rslt) {
|
||||
byte[] ttl_bry = args.Pull_bry(0);
|
||||
Xowe_wiki wiki = core.Wiki();
|
||||
Xoa_ttl ttl = Xoa_ttl.parse_(wiki, ttl_bry); if (ttl == null) return rslt.Init_obj(null);
|
||||
Xoa_ttl ttl = Xoa_ttl.parse(wiki, ttl_bry); if (ttl == null) return rslt.Init_obj(null);
|
||||
return rslt.Init_obj(CascadingProtection_rv);
|
||||
}
|
||||
public static final KeyVal[] CascadingProtection_rv = KeyVal_.Ary(KeyVal_.new_("sources", Bool_.N), KeyVal_.new_("restrictions", KeyVal_.Ary_empty));
|
||||
|
||||
@@ -133,10 +133,10 @@ public class Scrib_lib_title_tst {
|
||||
private static String ttl_slow(boolean exists, int ttl_id, boolean redirect) {
|
||||
return String_.Concat_lines_nl_skip_last
|
||||
( "1="
|
||||
, " isRedirect=" + Bool_.Xto_str_lower(redirect)
|
||||
, " isRedirect=" + Bool_.To_str_lower(redirect)
|
||||
, " id=" + Int_.Xto_str(ttl_id)
|
||||
, " contentModel=" + Scrib_lib_title.Key_wikitexet
|
||||
, " exists=" + Bool_.Xto_str_lower(exists)
|
||||
, " exists=" + Bool_.To_str_lower(exists)
|
||||
);
|
||||
}
|
||||
private static String file_info_absent() {
|
||||
|
||||
@@ -55,7 +55,7 @@ public class Scrib_lib_uri implements Scrib_lib {
|
||||
Xowe_wiki wiki = core.Wiki();
|
||||
byte[] ttl_bry = args.Pull_bry(0);
|
||||
byte[] qry_bry = args.Extract_qry_args(wiki, 1);
|
||||
Xoa_ttl ttl = Xoa_ttl.parse_(wiki, ttl_bry);
|
||||
Xoa_ttl ttl = Xoa_ttl.parse(wiki, ttl_bry);
|
||||
if (ttl == null) return rslt.Init_null();
|
||||
Bry_bfr bfr = core.App().Utl__bfr_mkr().Get_b512();
|
||||
if (ttl.Ns().Id() == Xow_ns_.Id_media) { // change "Media:" -> "File:"
|
||||
|
||||
@@ -140,7 +140,7 @@ public class Scrib_lib_ustring implements Scrib_lib {
|
||||
for (int j = 0; j < grps_len; j++) {
|
||||
Regx_group grp = grps[j];
|
||||
if ( j < capts_len // bounds check b/c null can be passed
|
||||
&& Bool_.cast_(capts[j].Val()) // check if true; indicates that group is "()" or "anypos" see regex converter; DATE:2014-04-23
|
||||
&& Bool_.cast(capts[j].Val()) // check if true; indicates that group is "()" or "anypos" see regex converter; DATE:2014-04-23
|
||||
)
|
||||
tmp_list.Add(Int_.Xto_str(grp.Bgn() + Scrib_lib_ustring.Base1)); // return index only for (); NOTE: always return as String; callers expect String, and may do operations like len(result), which will fail if int; DATE:2013-12-20
|
||||
else
|
||||
@@ -189,7 +189,7 @@ class Scrib_lib_ustring_gsub_mgr {
|
||||
}
|
||||
else if (Object_.Eq(repl_type, Int_.Cls_ref_type)) { // NOTE:@replace sometimes int; PAGE:en.d:λύω; DATE:2014-09-02
|
||||
tmp_repl_tid = Repl_tid_string;
|
||||
tmp_repl_bry = Bry_.new_u8(Int_.Xto_str(Int_.cast_(repl_obj)));
|
||||
tmp_repl_bry = Bry_.new_u8(Int_.Xto_str(Int_.cast(repl_obj)));
|
||||
}
|
||||
else if (Object_.Eq(repl_type, KeyVal[].class)) {
|
||||
tmp_repl_tid = Repl_tid_table;
|
||||
|
||||
@@ -120,7 +120,7 @@ public class Scrib_lib_ustring__lib_tst {
|
||||
@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
|
||||
Exec_gsub_regx("abC DEF gHI JKm NOP", "%f[%a]%u+%f[%A]", Int_.Max_value, "()", "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
|
||||
|
||||
@@ -65,7 +65,7 @@ public class Scrib_lib_wikibase implements Scrib_lib {
|
||||
public boolean GetEntityId(Scrib_proc_args args, Scrib_proc_rslt rslt) {
|
||||
byte[] ttl_bry = args.Pull_bry(0);
|
||||
Xowe_wiki wiki = core.Wiki();
|
||||
Xoa_ttl ttl = Xoa_ttl.parse_(wiki, ttl_bry);
|
||||
Xoa_ttl ttl = Xoa_ttl.parse(wiki, ttl_bry);
|
||||
byte[] rv = wiki.Appe().Wiki_mgr().Wdata_mgr().Qids_get(wiki, ttl); if (rv == null) rv = Bry_.Empty;
|
||||
return rslt.Init_obj(rv);
|
||||
}
|
||||
|
||||
@@ -72,7 +72,7 @@ public class Scrib_lib_wikibase_srl_tst {
|
||||
);
|
||||
}
|
||||
@Test public void Sitelinks_both_formats() { // PURPOSE: check that both formats are serializable; DATE:2014-02-06
|
||||
Json_doc jdoc = Json_doc.new_apos_concat_nl
|
||||
Json_doc jdoc = fxt.Wdata_fxt().App().Utl__json_parser().Parse_by_apos_ary
|
||||
( "{ 'entity':['item',2]"
|
||||
, ", 'links':"
|
||||
, " {"
|
||||
|
||||
@@ -82,8 +82,8 @@ class Scrib_lib_wikibase_srl_visitor implements Wdata_claim_visitor {
|
||||
}
|
||||
private static KeyVal[] Globecoordinate_value(Wdata_claim_itm_globecoordinate itm) {
|
||||
KeyVal[] rv = new KeyVal[5];
|
||||
rv[0] = KeyVal_.new_(Wdata_dict_value_globecoordinate.Str_latitude , Double_.parse_(String_.new_a7(itm.Lat())));
|
||||
rv[1] = KeyVal_.new_(Wdata_dict_value_globecoordinate.Str_longitude , Double_.parse_(String_.new_a7(itm.Lng())));
|
||||
rv[0] = KeyVal_.new_(Wdata_dict_value_globecoordinate.Str_latitude , Double_.parse(String_.new_a7(itm.Lat())));
|
||||
rv[1] = KeyVal_.new_(Wdata_dict_value_globecoordinate.Str_longitude , Double_.parse(String_.new_a7(itm.Lng())));
|
||||
rv[2] = KeyVal_.new_(Wdata_dict_value_globecoordinate.Str_altitude , null);
|
||||
rv[3] = KeyVal_.new_(Wdata_dict_value_globecoordinate.Str_globe , Wdata_dict_value_globecoordinate.Val_globe_dflt_str);
|
||||
rv[4] = KeyVal_.new_(Wdata_dict_value_globecoordinate.Str_precision , .00001d);
|
||||
|
||||
@@ -156,7 +156,7 @@ public class Scrib_regx_converter {
|
||||
}
|
||||
}
|
||||
}
|
||||
if (grps_open.Count() > 0) throw Err_.new_wo_type("Unclosed capture beginning at pattern character " + Int_.cast_(grps_open.Get_at(0)));
|
||||
if (grps_open.Count() > 0) throw Err_.new_wo_type("Unclosed capture beginning at pattern character " + Int_.cast(grps_open.Get_at(0)));
|
||||
// bfr.Add(Bry_regx_end); // NOTE: do not add PHP /us at end; u=PCRE_UTF8 which is not needed for Java; s=PCRE_DOTALL which will be specified elsewhere
|
||||
regx = bfr.Xto_str_and_clear();
|
||||
return regx;
|
||||
@@ -214,7 +214,7 @@ public class Scrib_regx_converter {
|
||||
int len = list.Count();
|
||||
for (int i = 0; i < len; i++) {
|
||||
Object o = list.Get_at(i);
|
||||
if (Int_.cast_(o) == v) return true;
|
||||
if (Int_.cast(o) == v) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user