1
0
mirror of https://github.com/gnosygnu/xowa.git synced 2026-03-02 03:49:30 +00:00
This commit is contained in:
gnosygnu
2015-08-24 00:32:13 -04:00
parent df10db140c
commit ed911e3de5
220 changed files with 2618 additions and 1569 deletions

View File

@@ -135,7 +135,7 @@ public class Scrib_lib_language_tst {
, " seconds=3"
));
}
private static KeyVal[] Kv_ary_(String... ary) {
public static KeyVal[] Kv_ary_(String... ary) {
int ary_len = ary.length;
KeyVal[] rv = new KeyVal[ary_len];
for (int i = 0; i < ary_len; i++) {

View File

@@ -295,7 +295,7 @@ public class Scrib_lib_mw implements Scrib_lib {
}
return (KeyVal[])rv.To_ary(KeyVal.class);
}
private static boolean Is_kv_ary(KeyVal kv) {return ClassAdp_.Eq_typeSafe(kv.Val(), KeyVal[].class);}
private static boolean Is_kv_ary(KeyVal kv) {return Type_adp_.Eq_typeSafe(kv.Val(), KeyVal[].class);}
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);
@@ -348,7 +348,7 @@ public class Scrib_lib_mw implements Scrib_lib {
Xot_invk frame = Scrib_frame_.Get_frame(core, frame_id);
Object ttl_obj = args.Cast_obj_or_null(1); // NOTE: callers must pass named title else title will be false; EX: frame:newChild{'current', 'title0'} -> false; frame:newChild{'current', title='title0'} -> 'title0'; DATE:2014-05-20
Xoa_ttl ttl = null;
if (ClassAdp_.ClassOf_obj(ttl_obj) != String.class) { // title = false
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);
}

View File

@@ -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.*;
public class Scrib_lib_text implements Scrib_lib {
private final Scrib_lib_text__json_util json_util = new Scrib_lib_text__json_util();
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;
public Scrib_lib Init() {procs.Init_by_lib(this, Proc_names); return this;}
@@ -51,19 +52,27 @@ 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) {
throw Err_.new_unimplemented();
// KeyVal[] kv_ary = args.Pull_kv_ary(0);
// int flags = args.Cast_int_or(1, 0);
// if (!(flags & Scrib_lib_text__json_util.Flag__preserve_keys)) { // && is_array( $value )
// text = Scrib_lib_text__json_util.Reindex_arrays(text, true);
// }
// byte[] rv = Scrib_lib_text__json_util.Encode(kv_ary, flags && Scrib_lib_text__json_util.Flag__pretty, Scrib_lib_text__json_util.Skip__all);
// 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;
}
public boolean JsonDecode(Scrib_proc_args args, Scrib_proc_rslt rslt) {
throw Err_.new_unimplemented();
// 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(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;
}
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) {
@@ -75,22 +84,8 @@ public class Scrib_lib_text implements Scrib_lib {
rv[3] = KeyVal_.new_("nowiki_protocols", KeyVal_.Ary_empty); // NOTE: code implemented, but waiting for it to be used; DATE:2014-03-20
return rslt.Init_obj(rv);
}
public void Init_for_tests() {json_util.Init_for_tests();}
private String Init_lib_text_get_msg(Xow_msg_mgr msg_mgr, String msg_key) {
return String_.new_u8(msg_mgr.Val_by_key_obj(Bry_.new_u8(msg_key)));
}
}
class Scrib_lib_text__json_util {
public static KeyVal[] Reindex_arrays(KeyVal[] kv_ary, boolean is_encoding) {
return kv_ary;
}
public static final int
Flag__preserve_keys = 1
, Flag__try_fixing = 2
, Flag__pretty = 4
;
public static final int
Skip__utf8 = 1
, Skip__xml = 2
, Skip__all = 3
;
}

View File

@@ -0,0 +1,110 @@
/*
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 Scrib_lib_text__json_util {
private final Json_wtr wtr = new Json_wtr();
public void Init_for_tests() {wtr.Opt_quote_byte_(Byte_ascii.Apos);}
public KeyVal[] Reindex_arrays(KeyVal[] kv_ary, boolean is_encoding) {
int next = 0;
if (is_encoding) {
// ksort( $arr, SORT_NUMERIC );
next = 1;
}
boolean is_sequence = true;
int len = kv_ary.length;
for (int i = 0; i < len; ++i) {
KeyVal kv = kv_ary[i];
Object kv_val = kv.Val();
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());
is_sequence = next++ == kv_key_as_int;
// } elseif ( $isEncoding && ctype_digit( $k ) ) {
// // json_decode currently doesn't return integer keys for {}
// $isSequence = $next++ === (int)$k;
} else {
is_sequence = false;
}
}
}
if (is_sequence) {
if (is_encoding) {
// return array_values( $arr );
} else {
// return $arr ? array_combine( range( 1, count( $arr ) ), $arr ) : $arr;
}
}
return kv_ary;
}
public KeyVal[] Decode(byte[] src, int flag) {
return null;
}
public byte[] Encode(KeyVal[] kv_ary, int flag, int skip) {
synchronized (wtr ) {
wtr.Clear().Doc_bgn();
Encode_kv_ary(kv_ary);
return wtr.Doc_end().To_bry_and_clear();
}
}
private void Encode_kv_ary(KeyVal[] kv_ary) {
int len = kv_ary.length;
for (int i = 0; i < len; ++i) {
KeyVal kv = kv_ary[i];
Encode_kv(kv);
}
}
private void Encode_kv(KeyVal kv) {
Object kv_val = kv.Val();
Class<?> type = Type_adp_.ClassOf_obj(kv_val);
if (Type_adp_.Eq(type, KeyVal[].class)) {
wtr.Nde_bgn(kv.Key());
Encode_kv_ary((KeyVal[])kv_val);
wtr.Nde_end();
}
else if (Type_adp_.Is_array(type)) { // encode as array
wtr.Ary_bgn(kv.Key());
Object ary = Array_.cast(kv_val);
int ary_len = Array_.Len(ary);
for (int j = 0; j < ary_len; ++j)
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 wtr.Kv_str(kv.Key(), Object_.Xto_str_strict_or_null(kv_val));
}
public static final int
Flag__preserve_keys = 1
, Flag__try_fixing = 2
, Flag__pretty = 4
;
public static final int
Skip__utf8 = 1
, Skip__xml = 2
, Skip__all = 3
;
public static final int
Opt__force_assoc = 1
;
}

View File

@@ -18,10 +18,13 @@ 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.*;
public class Scrib_lib_text_tst {
private Scrib_invoke_func_fxt fxt = new Scrib_invoke_func_fxt(); private Scrib_lib_text lib;
@Before public void init() {
fxt.Clear_for_lib();
lib = fxt.Core().Lib_text().Init();
} private Scrib_invoke_func_fxt fxt = new Scrib_invoke_func_fxt(); private Scrib_lib lib;
lib = fxt.Core().Lib_text();
lib.Init();
lib.Init_for_tests();
}
@Test public void Unstrip() {
fxt.Test_scrib_proc_str(lib, Scrib_lib_text.Invk_unstrip, Object_.Ary("a"), "a");
}
@@ -29,4 +32,45 @@ 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'"
// , " }"
// , "}"
// ));
// }
}
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_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'"
// , "}"
// ));
// }
// }

View File

@@ -101,7 +101,7 @@ public class Scrib_lib_title implements Scrib_lib {
// private static final byte[] Proto_relative = Bry_.new_a7("relative");
// 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))
if (Type_adp_.Eq_typeSafe(ns_obj, String.class))
return Bry_.new_u8(String_.cast_(ns_obj));
else {
int ns_id = Int_.cast_(ns_obj);

View File

@@ -18,7 +18,8 @@ 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 gplx.dbs.*; import gplx.xowa2.files.commons.*; import gplx.xowa.wikis.data.*;
import gplx.fsdb.*; import gplx.xowa.wikis.*; import gplx.xowa.files.*; import gplx.xowa.files.origs.*; import gplx.xowa.files.repos.*;
import gplx.fsdb.*;
import gplx.xowa.wikis.domains.*; import gplx.xowa.files.*; import gplx.xowa.files.origs.*; import gplx.xowa.files.repos.*;
public class Scrib_lib_title_tst {
@Before public void init() {
Db_conn_bldr.I.Reg_default_mem();
@@ -67,7 +68,7 @@ public class Scrib_lib_title_tst {
fxt.Test_scrib_proc_str_ary(lib, Scrib_lib_title.Invk_getFileInfo, Object_.Ary("File:A.png") , file_info_exists("A.png", 220, 200));
}
@Test public void GetFileInfo_commons() { // PURPOSE: check that Scribunto GetFileInfo calls filepath.FileExists; DATE:2014-01-07
Xowe_wiki commons_wiki = fxt.Parser_fxt().Wiki().Appe().Wiki_mgr().Get_by_key_or_make(Xow_domain_.Domain_bry_commons).Init_assert();
Xowe_wiki commons_wiki = fxt.Parser_fxt().Wiki().Appe().Wiki_mgr().Get_by_key_or_make(Xow_domain_itm_.Bry__commons).Init_assert();
Wiki_orig_tbl__create(fxt.Core().Wiki());
Wiki_orig_tbl__insert(fxt.Core().Wiki(), "A.png", 220, 200);
fxt.Parser_fxt().Init_page_create(commons_wiki, "File:A.png", "text_is_blank");

View File

@@ -209,7 +209,7 @@ class Scrib_lib_ustring_gsub_mgr {
tmp_repl_tid = Repl_tid_luacbk;
repl_func = (Scrib_lua_proc)repl_obj;
}
else throw Err_.new_unhandled(ClassAdp_.NameOf_type(repl_type));
else throw Err_.new_unhandled(Type_adp_.NameOf_type(repl_type));
}
private String Exec_repl(byte repl_tid, byte[] repl_bry, String text, String regx, int limit) {
Regx_adp regx_mgr = Scrib_lib_ustring.RegxAdp_new_(core.Ctx(), regx);

View File

@@ -17,7 +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.*;
import gplx.xowa.wikis.domains.*;
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;
@@ -85,10 +85,7 @@ public class Scrib_lib_wikibase implements Scrib_lib {
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));
}
Xow_domain_itm domain_itm = core.Wiki().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)

View File

@@ -410,8 +410,8 @@ class Scrib_lib_wikibase_srl_fxt {
Object kv_val = kv.Val();
if (kv_val == null) {bfr.Add_str_a7("null").Add_byte_nl(); return;}
Class<?> kv_val_cls = kv_val.getClass();
if (ClassAdp_.Eq(kv_val_cls, KeyVal[].class)) {bfr.Add_byte_nl(); Xto_str(bfr, (KeyVal[])kv_val, depth + 1);}
else if (ClassAdp_.Eq(kv_val_cls, KeyVal[].class)) {bfr.Add_byte_nl(); Xto_str(bfr, (KeyVal)kv_val, depth + 1);}
if (Type_adp_.Eq(kv_val_cls, KeyVal[].class)) {bfr.Add_byte_nl(); Xto_str(bfr, (KeyVal[])kv_val, depth + 1);}
else if (Type_adp_.Eq(kv_val_cls, KeyVal[].class)) {bfr.Add_byte_nl(); Xto_str(bfr, (KeyVal)kv_val, depth + 1);}
else bfr.Add_byte(Byte_ascii.Apos).Add_str(Object_.Xto_str_strict_or_empty(kv_val)).Add_byte(Byte_ascii.Apos).Add_byte_nl();
}
}