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:
@@ -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