Wikibase: Implement lexeme, form, and sense [#771]

staging
gnosygnu 4 years ago
parent a0c0b5b2fd
commit 7b6b3ed09a

@ -1,6 +1,6 @@
/* /*
XOWA: the XOWA Offline Wiki Application XOWA: the XOWA Offline Wiki Application
Copyright (C) 2012-2017 gnosygnu@gmail.com Copyright (C) 2012-2020 gnosygnu@gmail.com
XOWA is licensed under the terms of the General Public License (GPL) Version 3, XOWA is licensed under the terms of the General Public License (GPL) Version 3,
or alternatively under the terms of the Apache License Version 2.0. or alternatively under the terms of the Apache License Version 2.0.
@ -13,44 +13,50 @@ The terms of each license can be found in the source code repository:
GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
*/ */
package gplx; package gplx;
public class Type_ {//RF:2017-10-08
public static Class<?> Type_by_obj(Object o) {return o.getClass();} public class Type_ {//RF:2017-10-08
public static Class<?> Type_by_primitive(Object o) { public static Class<?> Type_by_obj(Object o) {return o.getClass();}
Class<?> rv = o.getClass(); public static Class<?> Type_by_primitive(Object o) {
if (rv == Integer.class) rv = int.class; Class<?> rv = o.getClass();
else if (rv == Long.class) rv = long.class; if (rv == Integer.class) rv = int.class;
else if (rv == Byte.class) rv = byte.class; else if (rv == Long.class) rv = long.class;
else if (rv == Short.class) rv = short.class; else if (rv == Byte.class) rv = byte.class;
return rv; else if (rv == Short.class) rv = short.class;
} return rv;
}
public static boolean Eq_by_obj(Object lhs_obj, Class<?> rhs_type) {
Class<?> lhs_type = lhs_obj == null ? null : lhs_obj.getClass(); public static boolean Eq_by_obj(Object lhs_obj, Class<?> rhs_type) {
return Type_.Eq(lhs_type, rhs_type); Class<?> lhs_type = lhs_obj == null ? null : lhs_obj.getClass();
} return Type_.Eq(lhs_type, rhs_type);
public static boolean Eq(Class<?> lhs, Class<?> rhs) {// DUPE_FOR_TRACKING: same as Object_.Eq }
if (lhs == null && rhs == null) return true; public static boolean Eq(Class<?> lhs, Class<?> rhs) {// DUPE_FOR_TRACKING: same as Object_.Eq
else if (lhs == null || rhs == null) return false; if (lhs == null && rhs == null) return true;
else return lhs.equals(rhs); else if (lhs == null || rhs == null) return false;
} else return lhs.equals(rhs);
}
public static String Canonical_name_by_obj(Object o) {return Canonical_name(o.getClass());}
public static String Canonical_name(Class<?> type) { public static String Canonical_name_by_obj(Object o) {return Canonical_name(o.getClass());}
return type.getCanonicalName(); public static String Canonical_name(Class<?> type) {
} return type.getCanonicalName();
}
public static String Name_by_obj(Object obj) {return obj == null ? String_.Null_mark : Name(Type_by_obj(obj));}
public static String Name(Class<?> type) { public static String Name_by_obj(Object obj) {return obj == null ? String_.Null_mark : Name(Type_by_obj(obj));}
return type.getName(); public static String Name(Class<?> type) {
} return type.getName();
}
public static boolean Is_array(Class<?> t) {
return t.isArray(); public static String SimpleName_by_obj(Object obj) {return obj == null ? String_.Null_mark : SimpleName(Type_by_obj(obj));}
} public static String SimpleName(Class<?> type) {
return type.getSimpleName();
public static boolean Is_assignable_from_by_obj(Object o, Class<?> generic) {return o == null ? false : Is_assignable_from(generic, o.getClass());} }
public static boolean Is_assignable_from(Class<?> generic, Class<?> specific) {
return generic.isAssignableFrom(specific); public static boolean Is_array(Class<?> t) {
} return t.isArray();
} }
public static boolean Is_assignable_from_by_obj(Object o, Class<?> generic) {return o == null ? false : Is_assignable_from(generic, o.getClass());}
public static boolean Is_assignable_from(Class<?> generic, Class<?> specific) {
return generic.isAssignableFrom(specific);
}
}

@ -1,6 +1,6 @@
/* /*
XOWA: the XOWA Offline Wiki Application XOWA: the XOWA Offline Wiki Application
Copyright (C) 2012-2017 gnosygnu@gmail.com Copyright (C) 2012-2020 gnosygnu@gmail.com
XOWA is licensed under the terms of the General Public License (GPL) Version 3, XOWA is licensed under the terms of the General Public License (GPL) Version 3,
or alternatively under the terms of the Apache License Version 2.0. or alternatively under the terms of the Apache License Version 2.0.
@ -13,200 +13,229 @@ The terms of each license can be found in the source code repository:
GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
*/ */
package gplx.xowa.xtns.scribunto.libs; import gplx.*; import gplx.xowa.*; import gplx.xowa.xtns.*; import gplx.xowa.xtns.scribunto.*; package gplx.xowa.xtns.scribunto.libs;
import gplx.xowa.xtns.wbases.*; import gplx.xowa.xtns.wbases.core.*; import gplx.xowa.xtns.wbases.claims.*; import gplx.xowa.xtns.wbases.claims.enums.*; import gplx.xowa.xtns.wbases.claims.itms.*; import gplx.xowa.xtns.wbases.parsers.*; import gplx.xowa.xtns.wbases.stores.*;
public class Scrib_lib_wikibase_srl { import gplx.Bry_;
public static Keyval[] Srl(Wbase_prop_mgr prop_mgr, Wdata_doc wdoc, boolean header_enabled, boolean legacy_style, byte[] page_url) {// REF.MW:/Wikibase/lib/includes/serializers/EntitySerializer.php!getSerialized; http://www.mediawiki.org/wiki/Extension:Wikibase_Client/Lua import gplx.Int_;
int base_adj = legacy_style ? 0 : 1; import gplx.Keyval;
List_adp rv = List_adp_.New(); import gplx.Keyval_;
if (header_enabled) { import gplx.List_adp;
byte[] qid = wdoc.Qid(); import gplx.List_adp_;
boolean doc_is_qid = Bry_.Has_at_bgn(qid, Byte_ascii.Ltr_q) || Bry_.Has_at_bgn(qid, Byte_ascii.Ltr_Q); import gplx.Ordered_hash;
rv.Add(Keyval_.new_("id", qid)); import gplx.String_;
rv.Add(Keyval_.new_("type", doc_is_qid ? Wbase_claim_entity_type_.Itm__item.Key_str() : Wbase_claim_entity_type_.Itm__property.Key_str())); // type should be "property"; PAGE:ru.w:Викитека:Проект:Викиданные DATE:2016-11-23 import gplx.xowa.xtns.wbases.Wdata_doc;
rv.Add(Keyval_.new_("schemaVersion", base_adj + 1)); // NOTE: needed by mw.wikibase.lua import gplx.xowa.xtns.wbases.Wdata_wiki_mgr;
import gplx.xowa.xtns.wbases.claims.Wbase_claim_grp;
// for Property pages, add a "datatype" property PAGE:ru.w:Маргарян,_Андраник_Наапетович; wd:Property:P18; DATE:2017-03-27 import gplx.xowa.xtns.wbases.claims.Wbase_claim_grp_list;
if (!doc_is_qid) { import gplx.xowa.xtns.wbases.claims.Wbase_references_grp;
String pid_name = String_.new_u8(Bry_.Mid(qid, Wdata_wiki_mgr.Ns_property_name_bry.length + 1));// +1 for ":" in "Property:" import gplx.xowa.xtns.wbases.claims.enums.Wbase_claim_entity_type_;
rv.Add(Keyval_.new_("datatype", prop_mgr.Get_or_null(pid_name, page_url))); import gplx.xowa.xtns.wbases.claims.enums.Wbase_claim_rank_;
} import gplx.xowa.xtns.wbases.claims.enums.Wbase_claim_type_;
} import gplx.xowa.xtns.wbases.claims.enums.Wbase_claim_value_type_;
Srl_root(rv, Wdata_doc_parser_v2.Str_labels , Srl_langtexts (Wdata_dict_langtext.Itm__language.Key_str(), Wdata_dict_langtext.Itm__value.Key_str(), wdoc.Label_list())); import gplx.xowa.xtns.wbases.claims.enums.Wbase_enum_itm;
Srl_root(rv, Wdata_doc_parser_v2.Str_descriptions , Srl_langtexts (Wdata_dict_langtext.Itm__language.Key_str(), Wdata_dict_langtext.Itm__value.Key_str(), wdoc.Descr_list())); import gplx.xowa.xtns.wbases.claims.itms.Wbase_claim_base;
Srl_root(rv, Wdata_doc_parser_v2.Str_sitelinks , Srl_sitelinks (Wdata_dict_sitelink.Itm__site.Key_str() , Wdata_dict_sitelink.Itm__title.Key_str(), wdoc.Slink_list(), base_adj)); import gplx.xowa.xtns.wbases.core.Wdata_alias_itm;
Srl_root(rv, Wdata_doc_parser_v2.Str_aliases , Srl_aliases (base_adj, wdoc.Alias_list())); import gplx.xowa.xtns.wbases.core.Wdata_dict_claim;
Srl_root(rv, Wdata_doc_parser_v2.Str_claims , Srl_claims (base_adj, legacy_style, prop_mgr, wdoc.Claim_list(), page_url)); import gplx.xowa.xtns.wbases.core.Wdata_dict_claim_v1;
return (Keyval[])rv.To_ary(Keyval.class); import gplx.xowa.xtns.wbases.core.Wdata_dict_langtext;
} import gplx.xowa.xtns.wbases.core.Wdata_dict_sitelink;
private static void Srl_root(List_adp rv, String label, Keyval[] ary) { import gplx.xowa.xtns.wbases.core.Wdata_langtext_itm;
if (ary == null) return; // don't add node if empty; EX: labels:{} should not add "labels" kv import gplx.xowa.xtns.wbases.core.Wdata_sitelink_itm;
rv.Add(Keyval_.new_(label, ary)); import gplx.xowa.xtns.wbases.parsers.Wdata_doc_parser_v2;
} import gplx.xowa.xtns.wbases.stores.Wbase_prop_mgr;
private static Keyval[] Srl_langtexts(String lang_label, String text_label, Ordered_hash list) {
int len = list.Count(); if (len == 0) return null; public class Scrib_lib_wikibase_srl {
Keyval[] rv = new Keyval[len]; public static Keyval[] Srl(Wbase_prop_mgr prop_mgr, Wdata_doc wdoc, boolean header_enabled, boolean legacy_style, byte[] page_url) {// REF.MW:/Wikibase/lib/includes/serializers/EntitySerializer.php!getSerialized; http://www.mediawiki.org/wiki/Extension:Wikibase_Client/Lua
for (int i = 0; i < len; i++) { int base_adj = legacy_style ? 0 : 1;
Wdata_langtext_itm itm = (Wdata_langtext_itm)list.Get_at(i); List_adp rv = List_adp_.New();
String lang = String_.new_u8(itm.Lang()); if (header_enabled) {
String text = String_.new_u8(itm.Text()); byte[] qid = wdoc.Qid();
rv[i] = Keyval_.new_(lang, Keyval_.Ary(Keyval_.new_(lang_label, lang), Keyval_.new_(text_label, text))); Wbase_enum_itm entity_itm = Wbase_claim_entity_type_.ToTid(qid);
} rv.Add(Keyval_.new_("id", qid));
return rv; rv.Add(Keyval_.new_("type", entity_itm.Key_str())); // type should be "property"; PAGE:ru.w:Викитека:Проект:Викиданные DATE:2016-11-23
} rv.Add(Keyval_.new_("schemaVersion", base_adj + 1)); // NOTE: needed by mw.wikibase.lua
private static Keyval[] Srl_sitelinks(String key_label, String val_label, Ordered_hash list, int base_adj) {
int len = list.Count(); if (len == 0) return null; // for Property pages, add a "datatype" property PAGE:ru.w:Маргарян,_Андраник_Наапетович; wd:Property:P18; DATE:2017-03-27
Keyval[] rv = new Keyval[len]; if (entity_itm == Wbase_claim_entity_type_.Itm__property) {
for (int i = 0; i < len; i++) { String pid_name = String_.new_u8(Bry_.Mid(qid, Wdata_wiki_mgr.Ns_property_name_bry.length + 1));// +1 for ":" in "Property:"
Wdata_sitelink_itm itm = (Wdata_sitelink_itm)list.Get_at(i); rv.Add(Keyval_.new_("datatype", prop_mgr.Get_or_null(pid_name, page_url)));
String site = String_.new_u8(itm.Site()); }
String name = String_.new_u8(itm.Name()); }
rv[i] = Keyval_.new_(site, Keyval_.Ary(Keyval_.new_(key_label, site), Keyval_.new_(val_label, name), Srl_sitelinks_badges(itm.Badges(), base_adj))); Srl_root(rv, Wdata_doc_parser_v2.Str_labels , Srl_langtexts (Wdata_dict_langtext.Itm__language.Key_str(), Wdata_dict_langtext.Itm__value.Key_str(), wdoc.Label_list()));
} Srl_root(rv, Wdata_doc_parser_v2.Str_descriptions , Srl_langtexts (Wdata_dict_langtext.Itm__language.Key_str(), Wdata_dict_langtext.Itm__value.Key_str(), wdoc.Descr_list()));
return rv; Srl_root(rv, Wdata_doc_parser_v2.Str_sitelinks , Srl_sitelinks (Wdata_dict_sitelink.Itm__site.Key_str() , Wdata_dict_sitelink.Itm__title.Key_str(), wdoc.Slink_list(), base_adj));
} Srl_root(rv, Wdata_doc_parser_v2.Str_aliases , Srl_aliases (base_adj, wdoc.Alias_list()));
private static Keyval Srl_sitelinks_badges(byte[][] badges, int base_adj) { // DATE:2014-11-13 Srl_root(rv, Wdata_doc_parser_v2.Str_claims , Srl_claims (base_adj, legacy_style, prop_mgr, wdoc.Claim_list(), page_url));
if (badges == null) badges = Bry_.Ary_empty; // null badges -> badges:[] return (Keyval[])rv.To_ary(Keyval.class);
int len = badges.length; }
Keyval[] kvs = len == 0 ? Keyval_.Ary_empty : new Keyval[len]; private static void Srl_root(List_adp rv, String label, Keyval[] ary) {
for (int i = 0; i < len; i++) { if (ary == null) return; // don't add node if empty; EX: labels:{} should not add "labels" kv
byte[] badge = badges[i]; rv.Add(Keyval_.new_(label, ary));
kvs[i] = Keyval_.int_(i + base_adj, String_.new_u8(badge)); }
} private static Keyval[] Srl_langtexts(String lang_label, String text_label, Ordered_hash list) {
return Keyval_.new_("badges", kvs); int len = list.Count(); if (len == 0) return null;
} Keyval[] rv = new Keyval[len];
private static Keyval[] Srl_aliases(int base_adj, Ordered_hash list) { for (int i = 0; i < len; i++) {
int len = list.Count(); if (len == 0) return null; Wdata_langtext_itm itm = (Wdata_langtext_itm)list.Get_at(i);
Keyval[] rv = new Keyval[len]; String lang = String_.new_u8(itm.Lang());
for (int i = 0; i < len; i++) { String text = String_.new_u8(itm.Text());
Wdata_alias_itm itm = (Wdata_alias_itm)list.Get_at(i); rv[i] = Keyval_.new_(lang, Keyval_.Ary(Keyval_.new_(lang_label, lang), Keyval_.new_(text_label, text)));
String lang = String_.new_u8(itm.Lang()); }
rv[i] = Keyval_.new_(lang, Srl_aliases_langs(base_adj, lang, itm.Vals())); return rv;
} }
return rv; private static Keyval[] Srl_sitelinks(String key_label, String val_label, Ordered_hash list, int base_adj) {
} int len = list.Count(); if (len == 0) return null;
private static Keyval[] Srl_aliases_langs(int base_adj, String lang, byte[][] ary) { Keyval[] rv = new Keyval[len];
int len = ary.length; for (int i = 0; i < len; i++) {
Keyval[] rv = new Keyval[len]; Wdata_sitelink_itm itm = (Wdata_sitelink_itm)list.Get_at(i);
for (int i = 0; i < len; i++) { String site = String_.new_u8(itm.Site());
byte[] itm = ary[i]; String name = String_.new_u8(itm.Name());
rv[i] = Keyval_.int_(i + base_adj, Keyval_.Ary(Keyval_.new_(Wdata_dict_langtext.Itm__language.Key_str(), lang), Keyval_.new_(Wdata_dict_langtext.Itm__value.Key_str(), String_.new_u8(itm)))); // NOTE: using same base_adj logic as claims rv[i] = Keyval_.new_(site, Keyval_.Ary(Keyval_.new_(key_label, site), Keyval_.new_(val_label, name), Srl_sitelinks_badges(itm.Badges(), base_adj)));
} }
return rv; return rv;
} }
private static Keyval[] Srl_claims(int base_adj, boolean legacy_style, Wbase_prop_mgr prop_mgr, Ordered_hash claim_grps, byte[] page_url) { private static Keyval Srl_sitelinks_badges(byte[][] badges, int base_adj) { // DATE:2014-11-13
int len = claim_grps.Count(); if (len == 0) return null; if (badges == null) badges = Bry_.Ary_empty; // null badges -> badges:[]
Scrib_lib_wikibase_srl_visitor visitor = new Scrib_lib_wikibase_srl_visitor(); int len = badges.length;
int rv_len = legacy_style ? len * 2 : len; // NOTE: legacyStyle returns 2 sets of properties: official "P" and legacy "p"; DATE:2014-05-11 Keyval[] kvs = len == 0 ? Keyval_.Ary_empty : new Keyval[len];
Keyval[] rv = new Keyval[rv_len]; for (int i = 0; i < len; i++) {
for (int i = 0; i < len; i++) { byte[] badge = badges[i];
Wbase_claim_grp grp = (Wbase_claim_grp)claim_grps.Get_at(i); kvs[i] = Keyval_.int_(i + base_adj, String_.new_u8(badge));
String pid_str = Int_.To_str(grp.Id()); }
Keyval[] grp_val = Srl_claims_prop_grp(prop_mgr, visitor, "P" + pid_str, grp, base_adj, page_url); return Keyval_.new_("badges", kvs);
rv[i] = Keyval_.new_("P" + pid_str, grp_val); }
if (legacy_style) private static Keyval[] Srl_aliases(int base_adj, Ordered_hash list) {
rv[i + len] = Keyval_.new_("p" + pid_str, grp_val); // SEE:WikibaseLuaBindings.php; This is a B/C hack to allow existing lua code to use hardcoded IDs in both lower (legacy) and upper case.; DATE:2014-05-11 int len = list.Count(); if (len == 0) return null;
} Keyval[] rv = new Keyval[len];
return rv; for (int i = 0; i < len; i++) {
} Wdata_alias_itm itm = (Wdata_alias_itm)list.Get_at(i);
private static Keyval[] Srl_claims_prop_grp(Wbase_prop_mgr prop_mgr, Scrib_lib_wikibase_srl_visitor visitor, String pid, Wbase_claim_grp grp, int base_adj, byte[] page_url) { String lang = String_.new_u8(itm.Lang());
int len = grp.Len(); rv[i] = Keyval_.new_(lang, Srl_aliases_langs(base_adj, lang, itm.Vals()));
Keyval[] rv = new Keyval[len]; }
for (int i = 0; i < len; i++) { return rv;
Wbase_claim_base itm = grp.Get_at(i); }
rv[i] = Keyval_.int_(i + base_adj, Srl_claims_prop_itm(prop_mgr, visitor, pid, itm, base_adj, page_url)); // NOTE: must be super 0 or super 1; DATE:2014-05-09 private static Keyval[] Srl_aliases_langs(int base_adj, String lang, byte[][] ary) {
} int len = ary.length;
return rv; Keyval[] rv = new Keyval[len];
} for (int i = 0; i < len; i++) {
public static Keyval[] Srl_claims_prop_ary(Wbase_prop_mgr prop_mgr, String pid, Wbase_claim_base[] itms, int base_adj, byte[] page_url) { byte[] itm = ary[i];
Scrib_lib_wikibase_srl_visitor visitor = new Scrib_lib_wikibase_srl_visitor(); rv[i] = Keyval_.int_(i + base_adj, Keyval_.Ary(Keyval_.new_(Wdata_dict_langtext.Itm__language.Key_str(), lang), Keyval_.new_(Wdata_dict_langtext.Itm__value.Key_str(), String_.new_u8(itm)))); // NOTE: using same base_adj logic as claims
int len = itms.length; }
Keyval[] rv = new Keyval[len]; return rv;
for (int i = 0; i < len; i++) { }
Wbase_claim_base itm = itms[i]; private static Keyval[] Srl_claims(int base_adj, boolean legacy_style, Wbase_prop_mgr prop_mgr, Ordered_hash claim_grps, byte[] page_url) {
rv[i] = Keyval_.int_(i + base_adj, Srl_claims_prop_itm(prop_mgr, visitor, pid, itm, base_adj, page_url)); // NOTE: must be super 0 or super 1; DATE:2014-05-09 int len = claim_grps.Count(); if (len == 0) return null;
} Scrib_lib_wikibase_srl_visitor visitor = new Scrib_lib_wikibase_srl_visitor();
return rv; int rv_len = legacy_style ? len * 2 : len; // NOTE: legacyStyle returns 2 sets of properties: official "P" and legacy "p"; DATE:2014-05-11
} Keyval[] rv = new Keyval[rv_len];
private static Keyval[] Srl_claims_prop_itm(Wbase_prop_mgr prop_mgr, Scrib_lib_wikibase_srl_visitor visitor, String pid, Wbase_claim_base itm, int base_adj, byte[] page_url) { for (int i = 0; i < len; i++) {
List_adp list = List_adp_.New(); Wbase_claim_grp grp = (Wbase_claim_grp)claim_grps.Get_at(i);
list.Add(Keyval_.new_("id", pid)); String pid_str = Int_.To_str(grp.Id());
list.Add(Keyval_.new_("mainsnak", Srl_claims_prop_itm_core(prop_mgr, visitor, pid, itm, page_url))); Keyval[] grp_val = Srl_claims_prop_grp(prop_mgr, visitor, "P" + pid_str, grp, base_adj, page_url);
list.Add(Keyval_.new_(Wdata_dict_claim_v1.Str_rank, Wbase_claim_rank_.Reg.Get_str_or_fail(itm.Rank_tid()))); rv[i] = Keyval_.new_("P" + pid_str, grp_val);
list.Add(Keyval_.new_("type", itm.Prop_type())); if (legacy_style)
Srl_root(list, Wdata_dict_claim.Itm__qualifiers.Key_str(), Srl_qualifiers(prop_mgr, visitor, itm.Qualifiers(), base_adj, page_url)); rv[i + len] = Keyval_.new_("p" + pid_str, grp_val); // SEE:WikibaseLuaBindings.php; This is a B/C hack to allow existing lua code to use hardcoded IDs in both lower (legacy) and upper case.; DATE:2014-05-11
Srl_root(list, Wdata_dict_claim.Itm__qualifiers_order.Key_str(), Srl_qualifiers_order(prop_mgr, visitor, itm.Qualifiers_order(), base_adj, page_url)); }
Srl_root(list, Wdata_dict_claim.Itm__references.Key_str(), Srl_references(prop_mgr, visitor, itm.References(), base_adj, page_url)); return rv;
return (Keyval[])list.To_ary_and_clear(Keyval.class); }
} private static Keyval[] Srl_claims_prop_grp(Wbase_prop_mgr prop_mgr, Scrib_lib_wikibase_srl_visitor visitor, String pid, Wbase_claim_grp grp, int base_adj, byte[] page_url) {
private static Keyval[] Srl_qualifiers(Wbase_prop_mgr prop_mgr, Scrib_lib_wikibase_srl_visitor visitor, Wbase_claim_grp_list list, int base_adj, byte[] page_url) { int len = grp.Len();
if (list == null) return null; Keyval[] rv = new Keyval[len];
int list_len = list.Len(); if (list_len == 0) return Keyval_.Ary_empty; for (int i = 0; i < len; i++) {
List_adp rv = List_adp_.New(); Wbase_claim_base itm = grp.Get_at(i);
List_adp pid_list = List_adp_.New(); rv[i] = Keyval_.int_(i + base_adj, Srl_claims_prop_itm(prop_mgr, visitor, pid, itm, base_adj, page_url)); // NOTE: must be super 0 or super 1; DATE:2014-05-09
for (int i = 0; i < list_len; ++i) { }
Wbase_claim_grp grp = list.Get_at(i); return rv;
int grp_len = grp.Len(); }
pid_list.Clear(); public static Keyval[] Srl_claims_prop_ary(Wbase_prop_mgr prop_mgr, String pid, Wbase_claim_base[] itms, int base_adj, byte[] page_url) {
String itm_pid = grp.Id_str(); Scrib_lib_wikibase_srl_visitor visitor = new Scrib_lib_wikibase_srl_visitor();
for (int j = 0; j < grp_len; ++j) { int len = itms.length;
Wbase_claim_base itm = grp.Get_at(j); Keyval[] rv = new Keyval[len];
pid_list.Add(Keyval_.int_(j + base_adj, Srl_claims_prop_itm_core(prop_mgr, visitor, itm_pid, itm, page_url))); // NOTE: was originally "+ 1"; changed to base_adj; PAGE:ru.w:Tor ru.w:Кактусовые DATE:2014-10-25 for (int i = 0; i < len; i++) {
} Wbase_claim_base itm = itms[i];
rv.Add(Keyval_.new_(itm_pid, (Keyval[])pid_list.To_ary_and_clear(Keyval.class))); rv[i] = Keyval_.int_(i + base_adj, Srl_claims_prop_itm(prop_mgr, visitor, pid, itm, base_adj, page_url)); // NOTE: must be super 0 or super 1; DATE:2014-05-09
} }
return (Keyval[])rv.To_ary_and_clear(Keyval.class); return rv;
} }
private static Keyval[] Srl_qualifiers_order(Wbase_prop_mgr prop_mgr, Scrib_lib_wikibase_srl_visitor visitor, int[] list, int base_adj, byte[] page_url) { private static Keyval[] Srl_claims_prop_itm(Wbase_prop_mgr prop_mgr, Scrib_lib_wikibase_srl_visitor visitor, String pid, Wbase_claim_base itm, int base_adj, byte[] page_url) {
if (list == null) return null; List_adp list = List_adp_.New();
int list_len = list.length; if (list_len == 0) return Keyval_.Ary_empty; list.Add(Keyval_.new_("id", pid));
List_adp rv = List_adp_.New(); list.Add(Keyval_.new_("mainsnak", Srl_claims_prop_itm_core(prop_mgr, visitor, pid, itm, page_url)));
for (int i = 0; i < list_len; ++i) { list.Add(Keyval_.new_(Wdata_dict_claim_v1.Str_rank, Wbase_claim_rank_.Reg.Get_str_or_fail(itm.Rank_tid())));
String itm_pid = "P" + Int_.To_str(list[i]); list.Add(Keyval_.new_("type", itm.Prop_type()));
rv.Add(Keyval_.int_(i + base_adj, itm_pid)); Srl_root(list, Wdata_dict_claim.Itm__qualifiers.Key_str(), Srl_qualifiers(prop_mgr, visitor, itm.Qualifiers(), base_adj, page_url));
} Srl_root(list, Wdata_dict_claim.Itm__qualifiers_order.Key_str(), Srl_qualifiers_order(prop_mgr, visitor, itm.Qualifiers_order(), base_adj, page_url));
return (Keyval[])rv.To_ary_and_clear(Keyval.class); Srl_root(list, Wdata_dict_claim.Itm__references.Key_str(), Srl_references(prop_mgr, visitor, itm.References(), base_adj, page_url));
} return (Keyval[])list.To_ary_and_clear(Keyval.class);
private static Keyval[] Srl_references(Wbase_prop_mgr prop_mgr, Scrib_lib_wikibase_srl_visitor visitor, Wbase_references_grp[] list, int base_adj, byte[] page_url) { }
if (list == null) return null; private static Keyval[] Srl_qualifiers(Wbase_prop_mgr prop_mgr, Scrib_lib_wikibase_srl_visitor visitor, Wbase_claim_grp_list list, int base_adj, byte[] page_url) {
int list_len = list.length; if (list_len == 0) return Keyval_.Ary_empty; if (list == null) return null;
List_adp rv = List_adp_.New(); int list_len = list.Len(); if (list_len == 0) return Keyval_.Ary_empty;
for (int i = 0; i < list_len; ++i) { List_adp rv = List_adp_.New();
Wbase_references_grp references_grp = list[i]; List_adp pid_list = List_adp_.New();
Keyval[] references_kvs = new Keyval[3]; for (int i = 0; i < list_len; ++i) {
references_kvs[0] = Keyval_.new_("hash", references_grp.Hash()); Wbase_claim_grp grp = list.Get_at(i);
references_kvs[1] = Keyval_.new_("snaks", Srl_qualifiers(prop_mgr, visitor, references_grp.Snaks(), base_adj, page_url)); int grp_len = grp.Len();
references_kvs[2] = Keyval_.new_("snaks-order", Srl_qualifiers_order(prop_mgr, visitor, references_grp.Snaks_order(), base_adj, page_url)); pid_list.Clear();
rv.Add(Keyval_.int_(i + base_adj, references_kvs)); String itm_pid = grp.Id_str();
} for (int j = 0; j < grp_len; ++j) {
return (Keyval[])rv.To_ary_and_clear(Keyval.class); Wbase_claim_base itm = grp.Get_at(j);
} pid_list.Add(Keyval_.int_(j + base_adj, Srl_claims_prop_itm_core(prop_mgr, visitor, itm_pid, itm, page_url))); // NOTE: was originally "+ 1"; changed to base_adj; PAGE:ru.w:Tor ru.w:Кактусовые DATE:2014-10-25
private static Keyval[] Srl_claims_prop_itm_core(Wbase_prop_mgr prop_mgr, Scrib_lib_wikibase_srl_visitor visitor, String pid, Wbase_claim_base itm, byte[] page_url) { }
boolean snak_is_valued = itm.Snak_tid() == Wbase_claim_value_type_.Tid__value; // PURPOSE: was != Wbase_claim_value_type_.Tid__novalue; PAGE:it.s:Autore:Anonimo DATE:2015-12-06 rv.Add(Keyval_.new_(itm_pid, pid_list.To_ary_and_clear(Keyval.class)));
int snak_is_valued_adj = snak_is_valued ? 1 : 0; }
Keyval[] rv = new Keyval[3 + snak_is_valued_adj]; return (Keyval[])rv.To_ary_and_clear(Keyval.class);
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(visitor, itm)); private static Keyval[] Srl_qualifiers_order(Wbase_prop_mgr prop_mgr, Scrib_lib_wikibase_srl_visitor visitor, int[] list, int base_adj, byte[] page_url) {
rv[0 + snak_is_valued_adj] = Keyval_.new_("property", pid); if (list == null) return null;
rv[1 + snak_is_valued_adj] = Keyval_.new_("snaktype", Wbase_claim_value_type_.Reg.Get_str_or_fail(itm.Snak_tid())); int list_len = list.length; if (list_len == 0) return Keyval_.Ary_empty;
List_adp rv = List_adp_.New();
// get prop datatype; NOTE: datatype needed for Modules; PAGE:eo.w:WikidataKoord; DATE:2015-11-08 for (int i = 0; i < list_len; ++i) {
String datatype = prop_mgr.Get_or_null(pid, page_url); String itm_pid = "P" + Int_.To_str(list[i]);
if (datatype == null) // if null, fallback to value based on tid; needed for (a) tests and (b) old wbase dbs that don't have wbase_prop tbl; DATE:2016-12-01 rv.Add(Keyval_.int_(i + base_adj, itm_pid));
datatype = Wbase_claim_type_.Get_scrib_or_unknown(itm.Val_tid()); }
rv[2 + snak_is_valued_adj] = Keyval_.new_("datatype", datatype); return (Keyval[])rv.To_ary_and_clear(Keyval.class);
return rv; }
} private static Keyval[] Srl_references(Wbase_prop_mgr prop_mgr, Scrib_lib_wikibase_srl_visitor visitor, Wbase_references_grp[] list, int base_adj, byte[] page_url) {
private static Keyval[] Srl_claims_prop_itm_core_val(Scrib_lib_wikibase_srl_visitor visitor, Wbase_claim_base itm) { if (list == null) return null;
switch (itm.Snak_tid()) { int list_len = list.length; if (list_len == 0) return Keyval_.Ary_empty;
case Wbase_claim_value_type_.Tid__somevalue: return Datavalue_somevalue; List_adp rv = List_adp_.New();
case Wbase_claim_value_type_.Tid__novalue: return Datavalue_novalue; // TODO_OLD: throw exc for (int i = 0; i < list_len; ++i) {
default: Wbase_references_grp references_grp = list[i];
itm.Welcome(visitor); Keyval[] references_kvs = new Keyval[3];
return visitor.Rv(); references_kvs[0] = Keyval_.new_("hash", references_grp.Hash());
} references_kvs[1] = Keyval_.new_("snaks", Srl_qualifiers(prop_mgr, visitor, references_grp.Snaks(), base_adj, page_url));
} references_kvs[2] = Keyval_.new_("snaks-order", Srl_qualifiers_order(prop_mgr, visitor, references_grp.Snaks_order(), base_adj, page_url));
public static final String Key_type = "type", Key_value = "value"; rv.Add(Keyval_.int_(i + base_adj, references_kvs));
private static final Keyval[] Datavalue_somevalue = Keyval_.Ary_empty; // changed to not return value-node; PAGE:it.s:Autore:Anonimo DATE:2015-12-06 // 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; return (Keyval[])rv.To_ary_and_clear(Keyval.class);
} }
private static Keyval[] Srl_claims_prop_itm_core(Wbase_prop_mgr prop_mgr, Scrib_lib_wikibase_srl_visitor visitor, String pid, Wbase_claim_base itm, byte[] page_url) {
boolean snak_is_valued = itm.Snak_tid() == Wbase_claim_value_type_.Tid__value; // PURPOSE: was != Wbase_claim_value_type_.Tid__novalue; PAGE:it.s:Autore:Anonimo DATE:2015-12-06
int snak_is_valued_adj = snak_is_valued ? 1 : 0;
Keyval[] rv = new Keyval[3 + snak_is_valued_adj];
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(visitor, itm));
rv[0 + snak_is_valued_adj] = Keyval_.new_("property", pid);
rv[1 + snak_is_valued_adj] = Keyval_.new_("snaktype", Wbase_claim_value_type_.Reg.Get_str_or_fail(itm.Snak_tid()));
// get prop datatype; NOTE: datatype needed for Modules; PAGE:eo.w:WikidataKoord; DATE:2015-11-08
String datatype = prop_mgr.Get_or_null(pid, page_url);
if (datatype == null) // if null, fallback to value based on tid; needed for (a) tests and (b) old wbase dbs that don't have wbase_prop tbl; DATE:2016-12-01
datatype = Wbase_claim_type_.Get_scrib_or_unknown(itm.Val_tid());
rv[2 + snak_is_valued_adj] = Keyval_.new_("datatype", datatype);
return rv;
}
private static Keyval[] Srl_claims_prop_itm_core_val(Scrib_lib_wikibase_srl_visitor visitor, Wbase_claim_base itm) {
switch (itm.Snak_tid()) {
case Wbase_claim_value_type_.Tid__somevalue: return Datavalue_somevalue;
case Wbase_claim_value_type_.Tid__novalue: return Datavalue_novalue; // TODO_OLD: throw exc
default:
itm.Welcome(visitor);
return visitor.Rv();
}
}
public static final String Key_type = "type", Key_value = "value";
private static final Keyval[] Datavalue_somevalue = Keyval_.Ary_empty; // changed to not return value-node; PAGE:it.s:Autore:Anonimo DATE:2015-12-06 // 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;
}

@ -1,6 +1,6 @@
/* /*
XOWA: the XOWA Offline Wiki Application XOWA: the XOWA Offline Wiki Application
Copyright (C) 2012-2017 gnosygnu@gmail.com Copyright (C) 2012-2020 gnosygnu@gmail.com
XOWA is licensed under the terms of the General Public License (GPL) Version 3, XOWA is licensed under the terms of the General Public License (GPL) Version 3,
or alternatively under the terms of the Apache License Version 2.0. or alternatively under the terms of the Apache License Version 2.0.
@ -13,170 +13,201 @@ The terms of each license can be found in the source code repository:
GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
*/ */
package gplx.xowa.xtns.wbases; import gplx.*; import gplx.xowa.*; import gplx.xowa.xtns.*; package gplx.xowa.xtns.wbases;
import gplx.core.brys.fmtrs.*;
import gplx.xowa.langs.*; import gplx.Bool_;
import gplx.xowa.xtns.wbases.core.*; import gplx.xowa.xtns.wbases.claims.*; import gplx.xowa.xtns.wbases.claims.itms.*; import gplx.xowa.xtns.wbases.hwtrs.*; import gplx.xowa.xtns.wbases.claims.itms.times.*; import gplx.Bry_;
public class Wdata_prop_val_visitor implements Wbase_claim_visitor { // THREAD.UNSAFE; callers must do synchronized import gplx.Bry_bfr;
private Wdata_wiki_mgr wdata_mgr; private Xoae_app app; private Bry_bfr bfr; import gplx.Bry_bfr_;
private Xol_lang_itm lang; import gplx.Bry_find_;
private final Bry_bfr tmp_time_bfr = Bry_bfr_.Reset(255); private final Bry_fmtr tmp_time_fmtr = Bry_fmtr.new_(); import gplx.Byte_ascii;
private Wdata_hwtr_msgs msgs; import gplx.Decimal_adp;
private boolean mode_is_statements; import gplx.Decimal_adp_;
public Wdata_prop_val_visitor(Xoae_app app, Wdata_wiki_mgr wdata_mgr) { import gplx.Err_;
this.app = app; this.wdata_mgr = wdata_mgr; import gplx.Gfo_usr_dlg_;
} import gplx.Math_;
public void Init(Bry_bfr bfr, Wdata_hwtr_msgs msgs, byte[] lang_key, boolean mode_is_statements) { import gplx.Object_;
// init some member variables; import gplx.String_;
this.bfr = bfr; this.msgs = msgs; import gplx.core.brys.fmtrs.Bry_fmtr;
this.lang = app.Lang_mgr().Get_by_or_null(lang_key); import gplx.xowa.Xoae_app;
if (lang == null) lang = app.Lang_mgr().Lang_en(); // TEST: needed for one test; DATE:2016-10-20 import gplx.xowa.langs.Xol_lang_itm;
this.mode_is_statements = mode_is_statements; import gplx.xowa.langs.Xol_lang_itm_;
} import gplx.xowa.xtns.wbases.claims.Wbase_claim_visitor;
public void Visit_str(Wbase_claim_string itm) {Write_str(bfr, itm.Val_bry());} import gplx.xowa.xtns.wbases.claims.itms.Wbase_claim_entity;
public static void Write_str(Bry_bfr bfr, byte[] bry) {bfr.Add(bry);} import gplx.xowa.xtns.wbases.claims.itms.Wbase_claim_globecoordinate;
public void Visit_time(Wbase_claim_time itm) { import gplx.xowa.xtns.wbases.claims.itms.Wbase_claim_monolingualtext;
Write_time(bfr, tmp_time_bfr, tmp_time_fmtr, msgs, Bry_.Empty, -1, itm.Time_as_date()); // for now, don't bother passing ttl; only used for error msg; DATE:2015-08-03 import gplx.xowa.xtns.wbases.claims.itms.Wbase_claim_quantity;
} import gplx.xowa.xtns.wbases.claims.itms.Wbase_claim_string;
public static void Write_time(Bry_bfr bfr, Bry_bfr tmp_bfr, Bry_fmtr tmp_fmtr, Wdata_hwtr_msgs msgs, byte[] page_url, int pid, Wbase_date date) { import gplx.xowa.xtns.wbases.claims.itms.Wbase_claim_time;
try { import gplx.xowa.xtns.wbases.claims.itms.Wbase_claim_value;
Wbase_date_.To_bfr(bfr, tmp_fmtr, tmp_bfr, msgs, date); import gplx.xowa.xtns.wbases.claims.itms.times.Wbase_date;
if (date.Calendar_is_julian()) bfr.Add_byte_space().Add(msgs.Time_julian()); import gplx.xowa.xtns.wbases.claims.itms.times.Wbase_date_;
} catch (Exception e) { import gplx.xowa.xtns.wbases.core.Wdata_langtext_itm;
Gfo_usr_dlg_.Instance.Warn_many("", "", "failed to write time; ttl=~{0} pid=~{1} err=~{2}", page_url, pid, Err_.Message_gplx_log(e)); import gplx.xowa.xtns.wbases.hwtrs.Wdata_hwtr_mgr;
} import gplx.xowa.xtns.wbases.hwtrs.Wdata_hwtr_msgs;
} import gplx.xowa.xtns.wbases.hwtrs.Wdata_lbl_itm;
public void Visit_monolingualtext(Wbase_claim_monolingualtext itm) {Write_langtext(bfr, itm.Text());} import gplx.xowa.xtns.wbases.hwtrs.Wdata_lbl_mgr;
public static void Write_langtext(Bry_bfr bfr, byte[] text) {bfr.Add(text);} // phrase only; PAGE:en.w:Alberta; EX: {{#property:motto}} -> "Fortis et libre"; DATE:2014-08-28
public void Visit_entity(Wbase_claim_entity itm) {Write_entity(bfr, wdata_mgr, lang.Key_bry(), itm.Page_ttl_db(), mode_is_statements);} public class Wdata_prop_val_visitor implements Wbase_claim_visitor { // THREAD.UNSAFE; callers must do synchronized
public static void Write_entity(Bry_bfr bfr, Wdata_wiki_mgr wdata_mgr, byte[] lang_key, byte[] entity_ttl_db, boolean mode_is_statements) { private Wdata_wiki_mgr wdata_mgr; private Xoae_app app; private Bry_bfr bfr;
// get entity private Xol_lang_itm lang;
Wdata_doc entity_doc = wdata_mgr.Doc_mgr.Get_by_xid_or_null(entity_ttl_db); private final Bry_bfr tmp_time_bfr = Bry_bfr_.Reset(255); private final Bry_fmtr tmp_time_fmtr = Bry_fmtr.new_();
private Wdata_hwtr_msgs msgs;
// NOTE: wiki may refer to entity that no longer exists; EX: {{#property:p1}} which links to Q1, but p1 links to Q2 and Q2 was deleted; DATE:2014-02-01 private boolean mode_is_statements;
if (entity_doc == null) public Wdata_prop_val_visitor(Xoae_app app, Wdata_wiki_mgr wdata_mgr) {
return; this.app = app; this.wdata_mgr = wdata_mgr;
}
// get label public void Init(Bry_bfr bfr, Wdata_hwtr_msgs msgs, byte[] lang_key, boolean mode_is_statements) {
byte[] label = entity_doc.Get_label_bry_or_null(lang_key); // init some member variables;
this.bfr = bfr; this.msgs = msgs;
// NOTE: some properties may not exist in language of wiki; default to english; DATE:2013-12-19 this.lang = app.Lang_mgr().Get_by_or_null(lang_key);
if (label == null && !Bry_.Eq(lang_key, Xol_lang_itm_.Key_en)) if (lang == null) lang = app.Lang_mgr().Lang_en(); // TEST: needed for one test; DATE:2016-10-20
label = entity_doc.Get_label_bry_or_null(Xol_lang_itm_.Key_en); this.mode_is_statements = mode_is_statements;
}
// if label is still not found, don't add null reference public void Visit_str(Wbase_claim_string itm) {Write_str(bfr, itm.Val_bry());}
if (label != null) { public static void Write_str(Bry_bfr bfr, byte[] bry) {bfr.Add(bry);}
// if statements, add "[[entity_val]]"; DATE:2017-04-04 public void Visit_time(Wbase_claim_time itm) {
if (mode_is_statements) { Write_time(bfr, tmp_time_bfr, tmp_time_fmtr, msgs, Bry_.Empty, -1, itm.Time_as_date()); // for now, don't bother passing ttl; only used for error msg; DATE:2015-08-03
bfr.Add(gplx.xowa.parsers.tmpls.Xop_tkn_.Lnki_bgn); }
bfr.Add(label); public static void Write_time(Bry_bfr bfr, Bry_bfr tmp_bfr, Bry_fmtr tmp_fmtr, Wdata_hwtr_msgs msgs, byte[] page_url, int pid, Wbase_date date) {
bfr.Add(gplx.xowa.parsers.tmpls.Xop_tkn_.Lnki_end); try {
} Wbase_date_.To_bfr(bfr, tmp_fmtr, tmp_bfr, msgs, date);
// else, just add "entity_val" if (date.Calendar_is_julian()) bfr.Add_byte_space().Add(msgs.Time_julian());
else } catch (Exception e) {
bfr.Add(label); Gfo_usr_dlg_.Instance.Warn_many("", "", "failed to write time; ttl=~{0} pid=~{1} err=~{2}", page_url, pid, Err_.Message_gplx_log(e));
} }
} }
public void Visit_quantity(Wbase_claim_quantity itm) {Write_quantity(bfr, wdata_mgr, lang, itm.Amount(), itm.Lbound(), itm.Ubound(), itm.Unit());} public void Visit_monolingualtext(Wbase_claim_monolingualtext itm) {Write_langtext(bfr, itm.Text());}
public static void Write_quantity(Bry_bfr bfr, Wdata_wiki_mgr wdata_mgr, Xol_lang_itm lang, byte[] val_bry, byte[] lo_bry, byte[] hi_bry, byte[] unit) { public static void Write_langtext(Bry_bfr bfr, byte[] text) {bfr.Add(text);} // phrase only; PAGE:en.w:Alberta; EX: {{#property:motto}} -> "Fortis et libre"; DATE:2014-08-28
// get val, lo, hi; NOTE: must handle large numbers; EX:{{#property:P1082}} PAGE:en.w:Earth; DATE:2015-08-02; NOTE: must handle decimals; PAGE:en.w:Malinao,_Aklan; DATE:2016-11-08 public void Visit_entity(Wbase_claim_entity itm) {Write_entity(bfr, wdata_mgr, lang.Key_bry(), itm.Page_ttl_db(), mode_is_statements);}
Decimal_adp val = Decimal__parse_or(val_bry, null); if (val == null) throw Err_.new_wo_type("wbase:quanity val can not be null"); public static void Write_entity(Bry_bfr bfr, Wdata_wiki_mgr wdata_mgr, byte[] lang_key, byte[] entity_ttl_db, boolean mode_is_statements) {
Decimal_adp lo = Decimal__parse_or(lo_bry, val); // get entity
Decimal_adp hi = Decimal__parse_or(hi_bry, val); Wdata_doc entity_doc = wdata_mgr.Doc_mgr.Get_by_xid_or_null(entity_ttl_db);
// fmt val // NOTE: wiki may refer to entity that no longer exists; EX: {{#property:p1}} which links to Q1, but p1 links to Q2 and Q2 was deleted; DATE:2014-02-01
if (lo.Eq(hi) && hi.Eq(val))// lo, hi, val are same; print val only; if (entity_doc == null)
bfr.Add(lang.Num_mgr().Format_num_by_decimal(val)); // amount; EX: 1,234 return;
else {
Wdata_hwtr_msgs msgs = wdata_mgr.Hwtr_mgr().Msgs(); // get label
Decimal_adp lo_dif = val.Subtract(lo); byte[] label = entity_doc.Get_label_bry_or_null(lang_key);
Decimal_adp hi_dif = hi.Subtract(val);
if (lo_dif.Eq(hi_dif)) { // lo_dif, hi_dif are same; print val±dif // NOTE: some properties may not exist in language of wiki; default to english; DATE:2013-12-19
bfr.Add(lang.Num_mgr().Format_num_by_decimal(val)); // amount; EX: 1,234 if (label == null && !Bry_.Eq(lang_key, Xol_lang_itm_.Key_en))
bfr.Add(msgs.Sym_plusminus()); // symbol: EX: ± label = entity_doc.Get_label_bry_or_null(Xol_lang_itm_.Key_en);
bfr.Add(lang.Num_mgr().Format_num_by_decimal(lo_dif)); // amount; EX: 4
} // if label is still not found, don't add null reference
else { // lo_dif, hi_dif are diff; print lo - hi; this may not be what MW does if (label != null) {
bfr.Add(lang.Num_mgr().Format_num_by_decimal(lo)); // lo; EX: 1,230 // if statements, add "[[entity_val]]"; DATE:2017-04-04
bfr.Add_byte(Byte_ascii.Dash); // dash: EX: - if (mode_is_statements) {
bfr.Add(lang.Num_mgr().Format_num_by_decimal(hi)); // hi; EX: 1,238 bfr.Add(gplx.xowa.parsers.tmpls.Xop_tkn_.Lnki_bgn);
} bfr.Add(label);
} bfr.Add(gplx.xowa.parsers.tmpls.Xop_tkn_.Lnki_end);
}
// output unit // else, just add "entity_val"
int unit_qid_bgn = unit == null ? Bry_find_.Not_found : Bry_find_.Find_fwd(unit, Wikidata_url); else
if (unit_qid_bgn == Bry_find_.Not_found) {} // entity missing; output nothing; EX:"unit":"1"; PAGE:en.w:Malinao,_Aklan DATE:2016-11-08 bfr.Add(label);
else { // entity exists; EX:"http://www.wikidata.org/entity/Q11573" (meter) }
bfr.Add_byte_space(); }
byte[] xid = Bry_.Mid(unit, Wikidata_url.length); public void Visit_quantity(Wbase_claim_quantity itm) {Write_quantity(bfr, wdata_mgr, lang, itm.Amount(), itm.Lbound(), itm.Ubound(), itm.Unit());}
Wdata_doc entity_doc = wdata_mgr.Doc_mgr.Get_by_xid_or_null(xid); public static void Write_quantity(Bry_bfr bfr, Wdata_wiki_mgr wdata_mgr, Xol_lang_itm lang, byte[] val_bry, byte[] lo_bry, byte[] hi_bry, byte[] unit) {
if (entity_doc != null) { // get val, lo, hi; NOTE: must handle large numbers; EX:{{#property:P1082}} PAGE:en.w:Earth; DATE:2015-08-02; NOTE: must handle decimals; PAGE:en.w:Malinao,_Aklan; DATE:2016-11-08
Wdata_langtext_itm label = entity_doc.Get_label_itm_or_null(lang); Decimal_adp val = Decimal__parse_or(val_bry, null); if (val == null) throw Err_.new_wo_type("wbase:quanity val can not be null");
if (label != null) Decimal_adp lo = Decimal__parse_or(lo_bry, val);
bfr.Add(label.Text()); Decimal_adp hi = Decimal__parse_or(hi_bry, val);
}
} // fmt val
} if (lo.Eq(hi) && hi.Eq(val))// lo, hi, val are same; print val only;
private static Decimal_adp Decimal__parse_or(byte[] bry, Decimal_adp or) { // handle missing lbound / ubound; DATE:2016-12-03 bfr.Add(lang.Num_mgr().Format_num_by_decimal(val)); // amount; EX: 1,234
return bry == null ? or : Decimal_adp_.parse(String_.new_u8(Normalize_for_decimal(bry))); else {
} Wdata_hwtr_msgs msgs = wdata_mgr.Hwtr_mgr().Msgs();
public static byte[] Normalize_for_decimal(byte[] bry) { // remove leading "+" and any commas; was Bry_.To_long_or(val_bry, Byte_ascii.Comma_bry, 0, val_bry.length, 0) Decimal_adp lo_dif = val.Subtract(lo);
if (bry == null) return null; Decimal_adp hi_dif = hi.Subtract(val);
Bry_bfr bfr = null; if (lo_dif.Eq(hi_dif)) { // lo_dif, hi_dif are same; print val±dif
int len = bry.length; bfr.Add(lang.Num_mgr().Format_num_by_decimal(val)); // amount; EX: 1,234
for (int i = 0; i < len; i++) { bfr.Add(msgs.Sym_plusminus()); // symbol: EX: ±
byte b = bry[i]; bfr.Add(lang.Num_mgr().Format_num_by_decimal(lo_dif)); // amount; EX: 4
switch (b) { }
case Byte_ascii.Plus: else { // lo_dif, hi_dif are diff; print lo - hi; this may not be what MW does
if (i == 0) { bfr.Add(lang.Num_mgr().Format_num_by_decimal(lo)); // lo; EX: 1,230
if (bfr == null) bfr = Bry_bfr_.New(); bfr.Add_byte(Byte_ascii.Dash); // dash: EX: -
} bfr.Add(lang.Num_mgr().Format_num_by_decimal(hi)); // hi; EX: 1,238
else { }
throw Err_.new_wo_type("invalid decimal format; plus must be at start of String", "raw", bry); }
}
break; // output unit
case Byte_ascii.Comma: int unit_qid_bgn = unit == null ? Bry_find_.Not_found : Bry_find_.Find_fwd(unit, Wikidata_url);
if (bfr == null) { if (unit_qid_bgn == Bry_find_.Not_found) {} // entity missing; output nothing; EX:"unit":"1"; PAGE:en.w:Malinao,_Aklan DATE:2016-11-08
bfr = Bry_bfr_.New(); else { // entity exists; EX:"http://www.wikidata.org/entity/Q11573" (meter)
bfr.Add_mid(bry, 0, i); bfr.Add_byte_space();
} byte[] xid = Bry_.Mid(unit, Wikidata_url.length);
break; Wdata_doc entity_doc = wdata_mgr.Doc_mgr.Get_by_xid_or_null(xid);
default: if (entity_doc != null) {
if (bfr != null) Wdata_langtext_itm label = entity_doc.Get_label_itm_or_null(lang);
bfr.Add_byte(b); if (label != null)
break; bfr.Add(label.Text());
} }
} }
return bfr == null ? bry : bfr.To_bry_and_clear(); }
} private static Decimal_adp Decimal__parse_or(byte[] bry, Decimal_adp or) { // handle missing lbound / ubound; DATE:2016-12-03
public void Visit_globecoordinate(Wbase_claim_globecoordinate itm) {Write_geo(Bool_.N, bfr, wdata_mgr.Hwtr_mgr().Lbl_mgr(), msgs, itm.Lat(), itm.Lng(), itm.Alt(), itm.Prc(), itm.Glb());} return bry == null ? or : Decimal_adp_.parse(String_.new_u8(Normalize_for_decimal(bry)));
public static void Write_geo(boolean wikidata_page, Bry_bfr bfr, Wdata_lbl_mgr lbl_mgr, Wdata_hwtr_msgs msgs, byte[] lat, byte[] lng, byte[] alt, byte[] prc, byte[] glb) { }
// get precision public static byte[] Normalize_for_decimal(byte[] bry) { // remove leading "+" and any commas; was Bry_.To_long_or(val_bry, Byte_ascii.Comma_bry, 0, val_bry.length, 0)
int precision_int = -1; if (bry == null) return null;
if (Bry_.Eq(prc, Object_.Bry__null) || Bry_.Eq(prc, Byte_ascii.Num_0_bry)) // "null" or "0" should be 1; PAGE:ru.w:Лысково_(Калужская_область) DATE:2016-11-24 Bry_bfr bfr = null;
precision_int = 1; int len = bry.length;
else { for (int i = 0; i < len; i++) {
Decimal_adp precision_frac = Decimal_adp_.parse(String_.new_a7(prc)); byte b = bry[i];
precision_int = Math_.Log10(Decimal_adp_.One.Divide(precision_frac).To_int()); // convert precision to log10 integer; EX: .00027777 -> 3600 -> 3 switch (b) {
} case Byte_ascii.Plus:
if (i == 0) {
// build String if (bfr == null) bfr = Bry_bfr_.New();
gplx.xowa.xtns.mapSources.Map_dd2dms_func.Deg_to_dms(bfr, Bool_.Y, Bool_.N, lat, precision_int); }
bfr.Add_byte_comma().Add_byte_space(); else {
gplx.xowa.xtns.mapSources.Map_dd2dms_func.Deg_to_dms(bfr, Bool_.Y, Bool_.Y, lng, precision_int); throw Err_.new_wo_type("invalid decimal format; plus must be at start of String", "raw", bry);
}
// write globe if any break;
if (wikidata_page) { case Byte_ascii.Comma:
byte[] glb_ttl = Wdata_lbl_itm.Extract_ttl(glb); if (bfr == null) {
if (glb_ttl != null) { bfr = Bry_bfr_.New();
byte[] glb_lbl = lbl_mgr.Get_text__ttl(glb_ttl, glb); bfr.Add_mid(bry, 0, i);
bfr.Add_byte_space().Add_byte(Byte_ascii.Paren_bgn); }
Wdata_hwtr_mgr.Write_link_wikidata(bfr, glb_ttl, glb_lbl); break;
bfr.Add_byte(Byte_ascii.Paren_end); default:
} if (bfr != null)
} bfr.Add_byte(b);
} break;
}
private static final byte[] Wikidata_url = Bry_.new_a7("http://www.wikidata.org/entity/"); }
public void Visit_system(Wbase_claim_value itm) {} return bfr == null ? bry : bfr.To_bry_and_clear();
} }
public void Visit_globecoordinate(Wbase_claim_globecoordinate itm) {Write_geo(Bool_.N, bfr, wdata_mgr.Hwtr_mgr().Lbl_mgr(), msgs, itm.Lat(), itm.Lng(), itm.Alt(), itm.Prc(), itm.Glb());}
public static void Write_geo(boolean wikidata_page, Bry_bfr bfr, Wdata_lbl_mgr lbl_mgr, Wdata_hwtr_msgs msgs, byte[] lat, byte[] lng, byte[] alt, byte[] prc, byte[] glb) {
// get precision
int precision_int = -1;
if (Bry_.Eq(prc, Object_.Bry__null) || Bry_.Eq(prc, Byte_ascii.Num_0_bry)) // "null" or "0" should be 1; PAGE:ru.w:Лысково_(Калужская_область) DATE:2016-11-24
precision_int = 1;
else {
Decimal_adp precision_frac = Decimal_adp_.parse(String_.new_a7(prc));
precision_int = Math_.Log10(Decimal_adp_.One.Divide(precision_frac).To_int()); // convert precision to log10 integer; EX: .00027777 -> 3600 -> 3
}
// build String
gplx.xowa.xtns.mapSources.Map_dd2dms_func.Deg_to_dms(bfr, Bool_.Y, Bool_.N, lat, precision_int);
bfr.Add_byte_comma().Add_byte_space();
gplx.xowa.xtns.mapSources.Map_dd2dms_func.Deg_to_dms(bfr, Bool_.Y, Bool_.Y, lng, precision_int);
// write globe if any
if (wikidata_page) {
byte[] glb_ttl = Wdata_lbl_itm.Extract_ttl(glb);
if (glb_ttl != null) {
byte[] glb_lbl = lbl_mgr.Get_text__ttl(glb_ttl, glb);
bfr.Add_byte_space().Add_byte(Byte_ascii.Paren_bgn);
Wdata_hwtr_mgr.Write_link_wikidata(bfr, glb_ttl, glb_lbl);
bfr.Add_byte(Byte_ascii.Paren_end);
}
}
}
private static final byte[] Wikidata_url = Bry_.new_a7("http://www.wikidata.org/entity/");
public void Visit_system(Wbase_claim_value itm) {}
}

@ -1,6 +1,6 @@
/* /*
XOWA: the XOWA Offline Wiki Application XOWA: the XOWA Offline Wiki Application
Copyright (C) 2012-2017 gnosygnu@gmail.com Copyright (C) 2012-2020 gnosygnu@gmail.com
XOWA is licensed under the terms of the General Public License (GPL) Version 3, XOWA is licensed under the terms of the General Public License (GPL) Version 3,
or alternatively under the terms of the Apache License Version 2.0. or alternatively under the terms of the Apache License Version 2.0.
@ -13,202 +13,245 @@ The terms of each license can be found in the source code repository:
GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
*/ */
package gplx.xowa.xtns.wbases; import gplx.*; import gplx.xowa.*; import gplx.xowa.xtns.*; package gplx.xowa.xtns.wbases;
import gplx.core.primitives.*;
import gplx.xowa.langs.msgs.*; import gplx.langs.jsons.*; import gplx.Bool_;
import gplx.xowa.wikis.nss.*; import gplx.Bry_;
import gplx.xowa.langs.*; import gplx.Bry_bfr;
import gplx.xowa.parsers.*; import gplx.Bry_bfr_;
import gplx.xowa.wikis.domains.*; import gplx.xowa.htmls.*; import gplx.xowa.parsers.logs.*; import gplx.xowa.apps.apis.xowa.xtns.*; import gplx.xowa.apps.apis.xowa.html.*; import gplx.xowa.users.*; import gplx.GfoMsg;
import gplx.xowa.xtns.wbases.core.*; import gplx.xowa.xtns.wbases.claims.*; import gplx.xowa.xtns.wbases.claims.enums.*; import gplx.xowa.xtns.wbases.claims.itms.*; import gplx.xowa.xtns.wbases.parsers.*; import gplx.xowa.xtns.wbases.pfuncs.*; import gplx.xowa.xtns.wbases.hwtrs.*; import gplx.xowa.xtns.wbases.stores.*; import gplx.Gfo_evt_itm;
import gplx.xowa.mediawiki.extensions.Wikibase.client.includes.dataAccess.scribunto.*; import gplx.Gfo_evt_mgr;
public class Wdata_wiki_mgr implements Gfo_evt_itm, Gfo_invk { import gplx.Gfo_evt_mgr_;
private final Xoae_app app; import gplx.Gfo_invk;
private final Wdata_prop_val_visitor prop_val_visitor; import gplx.Gfo_invk_;
private final Wdata_doc_parser wdoc_parser_v1 = new Wdata_doc_parser_v1(), wdoc_parser_v2 = new Wdata_doc_parser_v2(); import gplx.GfsCtx;
private final Object thread_lock = new Object(); import gplx.String_;
private final Bry_bfr tmp_bfr = Bry_bfr_.New_w_size(32); import gplx.Yn;
public Wdata_wiki_mgr(Xoae_app app) { import gplx.langs.jsons.Json_doc;
this.app = app; import gplx.langs.jsons.Json_kv;
this.evt_mgr = new Gfo_evt_mgr(this); import gplx.langs.jsons.Json_parser;
this.Qid_mgr = new Wbase_qid_mgr(this); import gplx.xowa.Xoa_ttl;
this.Pid_mgr = new Wbase_pid_mgr(this); import gplx.xowa.Xoae_app;
this.Doc_mgr = new Wbase_doc_mgr(this, this.Qid_mgr); import gplx.xowa.Xoae_page;
this.prop_mgr = new Wbase_prop_mgr(Wbase_prop_mgr_loader_.New_db(this)); import gplx.xowa.Xowe_wiki;
this.prop_val_visitor = new Wdata_prop_val_visitor(app, this); import gplx.xowa.apps.apis.xowa.html.Xoapi_toggle_mgr;
this.Enabled_(true); import gplx.xowa.apps.apis.xowa.xtns.Xoapi_wikibase;
} import gplx.xowa.htmls.Xoh_consts;
public Gfo_evt_mgr Evt_mgr() {return evt_mgr;} private final Gfo_evt_mgr evt_mgr; import gplx.xowa.langs.Xol_lang_itm;
public final Wbase_qid_mgr Qid_mgr; import gplx.xowa.langs.msgs.Xol_msg_itm_;
public final Wbase_pid_mgr Pid_mgr; import gplx.xowa.parsers.Xop_ctx;
public final Wbase_doc_mgr Doc_mgr; import gplx.xowa.parsers.logs.Xop_log_property_wkr;
public Wbase_prop_mgr Prop_mgr() {return prop_mgr;} private final Wbase_prop_mgr prop_mgr; import gplx.xowa.users.Xoue_user;
public boolean Enabled() {return enabled;} private boolean enabled; import gplx.xowa.wikis.domains.Xow_domain_itm;
public void Enabled_(boolean v) { import gplx.xowa.wikis.domains.Xow_domain_tid_;
this.enabled = v; import gplx.xowa.wikis.nss.Xow_ns_;
Qid_mgr.Enabled_(v); import gplx.xowa.xtns.wbases.claims.Wbase_claim_grp;
Pid_mgr.Enabled_(v); import gplx.xowa.xtns.wbases.claims.enums.Wbase_claim_rank_;
Doc_mgr.Enabled_(v); import gplx.xowa.xtns.wbases.claims.enums.Wbase_claim_value_type_;
} import gplx.xowa.xtns.wbases.claims.itms.Wbase_claim_base;
public byte[] Domain() {return domain;} public void Domain_(byte[] v) {domain = v;} private byte[] domain = Bry_.new_a7("www.wikidata.org"); import gplx.xowa.xtns.wbases.hwtrs.Wdata_hwtr_mgr;
public Wdata_hwtr_mgr Hwtr_mgr() { import gplx.xowa.xtns.wbases.hwtrs.Wdata_hwtr_msgs;
if (hwtr_mgr == null) import gplx.xowa.xtns.wbases.hwtrs.Wdata_lbl_wkr_wiki;
Hwtr_mgr_assert(); import gplx.xowa.xtns.wbases.parsers.Wdata_doc_parser;
return hwtr_mgr; import gplx.xowa.xtns.wbases.parsers.Wdata_doc_parser_v1;
} private Wdata_hwtr_mgr hwtr_mgr; import gplx.xowa.xtns.wbases.parsers.Wdata_doc_parser_v2;
public Xowe_wiki Wdata_wiki() { import gplx.xowa.xtns.wbases.stores.Wbase_doc_mgr;
if (wdata_wiki == null) { import gplx.xowa.xtns.wbases.stores.Wbase_pid_mgr;
synchronized (thread_lock) { // LOCK:must synchronized b/c multiple threads may init wdata_mgr at same time; import gplx.xowa.xtns.wbases.stores.Wbase_prop_mgr;
Xowe_wiki tmp_wdata_wiki = app.Wiki_mgr().Get_by_or_make(domain).Init_assert(); import gplx.xowa.xtns.wbases.stores.Wbase_prop_mgr_loader_;
if (wdata_wiki == null) // synchronized is not around "if (wdata_wiki == null)", so multiple threads may try to set; only set if null; DATE:2016-09-12 import gplx.xowa.xtns.wbases.stores.Wbase_qid_mgr;
wdata_wiki = tmp_wdata_wiki;
} public class Wdata_wiki_mgr implements Gfo_evt_itm, Gfo_invk {
} private final Xoae_app app;
return wdata_wiki; private final Wdata_prop_val_visitor prop_val_visitor;
} private Xowe_wiki wdata_wiki; private final Wdata_doc_parser wdoc_parser_v1 = new Wdata_doc_parser_v1(), wdoc_parser_v2 = new Wdata_doc_parser_v2();
public Json_parser Jdoc_parser() {return jdoc_parser;} private Json_parser jdoc_parser = new Json_parser(); private final Object thread_lock = new Object();
public void Init_by_app() {} private final Bry_bfr tmp_bfr = Bry_bfr_.New_w_size(32);
public Wdata_doc_parser Wdoc_parser(Json_doc jdoc) { public Wdata_wiki_mgr(Xoae_app app) {
Json_kv itm_0 = Json_kv.cast(jdoc.Root_nde().Get_at(0)); // get 1st node this.app = app;
return Bry_.Eq(itm_0.Key().Data_bry(), Wdata_doc_parser_v2.Bry_type) this.evt_mgr = new Gfo_evt_mgr(this);
|| Bry_.Eq(itm_0.Key().Data_bry(), Wdata_doc_parser_v2.Bry_id) this.Qid_mgr = new Wbase_qid_mgr(this);
? wdoc_parser_v2 : wdoc_parser_v1; // if "type", must be v2 this.Pid_mgr = new Wbase_pid_mgr(this);
} this.Doc_mgr = new Wbase_doc_mgr(this, this.Qid_mgr);
public Xop_log_property_wkr Property_wkr() {return property_wkr;} private Xop_log_property_wkr property_wkr; this.prop_mgr = new Wbase_prop_mgr(Wbase_prop_mgr_loader_.New_db(this));
public void Clear() { this.prop_val_visitor = new Wdata_prop_val_visitor(app, this);
synchronized (thread_lock) { // LOCK:app-level this.Enabled_(true);
Qid_mgr.Clear(); }
Pid_mgr.Clear(); public Gfo_evt_mgr Evt_mgr() {return evt_mgr;} private final Gfo_evt_mgr evt_mgr;
Doc_mgr.Clear(); public final Wbase_qid_mgr Qid_mgr;
} public final Wbase_pid_mgr Pid_mgr;
} public final Wbase_doc_mgr Doc_mgr;
public byte[] Get_claim_or(Xow_domain_itm domain, Xoa_ttl page_ttl, int pid, byte[] or) { public Wbase_prop_mgr Prop_mgr() {return prop_mgr;} private final Wbase_prop_mgr prop_mgr;
byte[] qid = this.Qid_mgr.Get_qid_or_null(domain.Abrv_wm(), page_ttl); if (qid == null) return or; public boolean Enabled() {return enabled;} private boolean enabled;
Wdata_doc wdoc = Doc_mgr.Get_by_loose_id_or_null(qid); if (wdoc == null) return or; public void Enabled_(boolean v) {
Wbase_claim_grp claim_grp = wdoc.Get_claim_grp_or_null(pid); this.enabled = v;
if (claim_grp == null || claim_grp.Len() == 0) return or; Qid_mgr.Enabled_(v);
Wbase_claim_base claim_itm = claim_grp.Get_at(0); Pid_mgr.Enabled_(v);
Resolve_claim(tmp_bfr, domain, claim_itm); Doc_mgr.Enabled_(v);
return tmp_bfr.To_bry_and_clear(); }
} public byte[] Domain() {return domain;} public void Domain_(byte[] v) {domain = v;} private byte[] domain = Bry_.new_a7("www.wikidata.org");
public void Resolve_claim(Bry_bfr rv, Xow_domain_itm domain, Wbase_claim_base claim_itm) { public Wdata_hwtr_mgr Hwtr_mgr() {
synchronized (thread_lock) { // LOCK:must synchronized b/c prop_val_visitor has member bfr which can get overwritten; DATE:2016-07-06 if (hwtr_mgr == null)
if (hwtr_mgr == null) Hwtr_mgr_assert(); Hwtr_mgr_assert();
prop_val_visitor.Init(rv, hwtr_mgr.Msgs(), domain.Lang_orig_key(), Bool_.N); return hwtr_mgr;
claim_itm.Welcome(prop_val_visitor); } private Wdata_hwtr_mgr hwtr_mgr;
} public Xowe_wiki Wdata_wiki() {
} if (wdata_wiki == null) {
public void Resolve_to_bfr(Bry_bfr bfr, Xowe_wiki wiki, Wbase_claim_grp prop_grp, byte[] lang_key, boolean mode_is_statements) { synchronized (thread_lock) { // LOCK:must synchronized b/c multiple threads may init wdata_mgr at same time;
synchronized (thread_lock) { // LOCK:must synchronized b/c prop_val_visitor has member bfr which can get overwritten; DATE:2016-07-06 Xowe_wiki tmp_wdata_wiki = app.Wiki_mgr().Get_by_or_make(domain).Init_assert();
if (hwtr_mgr == null) Hwtr_mgr_assert(); if (wdata_wiki == null) // synchronized is not around "if (wdata_wiki == null)", so multiple threads may try to set; only set if null; DATE:2016-09-12
int len = prop_grp.Len(); wdata_wiki = tmp_wdata_wiki;
Wbase_claim_base selected = null; }
for (int i = 0; i < len; i++) { // NOTE: multiple props possible; EX: {{#property:P1082}}; PAGE:en.w:Earth DATE:2015-08-02 }
Wbase_claim_base prop = prop_grp.Get_at(i); return wdata_wiki;
if (selected == null) selected = prop; // if selected not set, set it; will always set to 1st prop } private Xowe_wiki wdata_wiki;
if (prop.Rank_tid() == Wbase_claim_rank_.Tid__preferred) { // if prop is preferred, select it and exit; public Json_parser Jdoc_parser() {return jdoc_parser;} private Json_parser jdoc_parser = new Json_parser();
selected = prop; public void Init_by_app() {}
break; public Wdata_doc_parser Wdoc_parser(Json_doc jdoc) {
} Json_kv itm_0 = Json_kv.cast(jdoc.Root_nde().Get_at(0)); // get 1st node
} return Bry_.Eq(itm_0.Key().Data_bry(), Wdata_doc_parser_v2.Bry_type)
switch (selected.Snak_tid()) { // SEE:NOTE:novalue/somevalue || Bry_.Eq(itm_0.Key().Data_bry(), Wdata_doc_parser_v2.Bry_id)
case Wbase_claim_value_type_.Tid__novalue: ? wdoc_parser_v2 : wdoc_parser_v1; // if "type", must be v2
bfr.Add(wiki.Msg_mgr().Val_by_id(Xol_msg_itm_.Id_xowa_wikidata_novalue)); }
break; public Xop_log_property_wkr Property_wkr() {return property_wkr;} private Xop_log_property_wkr property_wkr;
case Wbase_claim_value_type_.Tid__somevalue: public void Clear() {
bfr.Add(wiki.Msg_mgr().Val_by_id(Xol_msg_itm_.Id_xowa_wikidata_somevalue)); synchronized (thread_lock) { // LOCK:app-level
break; Qid_mgr.Clear();
default: { Pid_mgr.Clear();
prop_val_visitor.Init(bfr, hwtr_mgr.Msgs(), lang_key, mode_is_statements); Doc_mgr.Clear();
selected.Welcome(prop_val_visitor); }
break; }
} public byte[] Get_claim_or(Xow_domain_itm domain, Xoa_ttl page_ttl, int pid, byte[] or) {
} byte[] qid = this.Qid_mgr.Get_qid_or_null(domain.Abrv_wm(), page_ttl); if (qid == null) return or;
} Wdata_doc wdoc = Doc_mgr.Get_by_loose_id_or_null(qid); if (wdoc == null) return or;
} Wbase_claim_grp claim_grp = wdoc.Get_claim_grp_or_null(pid);
public byte[] Popup_text(Xoae_page page) { if (claim_grp == null || claim_grp.Len() == 0) return or;
Hwtr_mgr_assert(); Wbase_claim_base claim_itm = claim_grp.Get_at(0);
Wdata_doc wdoc = Doc_mgr.Get_by_exact_id_or_null(page.Ttl().Full_db()); Resolve_claim(tmp_bfr, domain, claim_itm);
if (wdoc == null) return Bry_.Empty; return tmp_bfr.To_bry_and_clear();
return hwtr_mgr.Popup(wdoc); }
} public void Resolve_claim(Bry_bfr rv, Xow_domain_itm domain, Wbase_claim_base claim_itm) {
public void Write_json_as_html(Bry_bfr bfr, Xoa_ttl page_ttl, byte[] data_raw) { synchronized (thread_lock) { // LOCK:must synchronized b/c prop_val_visitor has member bfr which can get overwritten; DATE:2016-07-06
Hwtr_mgr_assert(); if (hwtr_mgr == null) Hwtr_mgr_assert();
Wdata_doc wdoc = Doc_mgr.Get_by_exact_id_or_null(page_ttl.Full_db()); prop_val_visitor.Init(rv, hwtr_mgr.Msgs(), domain.Lang_orig_key(), Bool_.N);
if (wdoc == null) return; claim_itm.Welcome(prop_val_visitor);
hwtr_mgr.Init_by_wdoc(wdoc); }
bfr.Add(hwtr_mgr.Write(wdoc)); }
} public void Resolve_to_bfr(Bry_bfr bfr, Xowe_wiki wiki, Wbase_claim_grp prop_grp, byte[] lang_key, boolean mode_is_statements) {
private void Hwtr_mgr_assert() { synchronized (thread_lock) { // LOCK:must synchronized b/c prop_val_visitor has member bfr which can get overwritten; DATE:2016-07-06
if (hwtr_mgr != null) return; if (hwtr_mgr == null) Hwtr_mgr_assert();
Xoapi_toggle_mgr toggle_mgr = app.Api_root().Html().Page().Toggle_mgr(); int len = prop_grp.Len();
Xoapi_wikibase wikibase_api = app.Api_root().Xtns().Wikibase(); Wbase_claim_base selected = null;
hwtr_mgr = new Wdata_hwtr_mgr(); for (int i = 0; i < len; i++) { // NOTE: multiple props possible; EX: {{#property:P1082}}; PAGE:en.w:Earth DATE:2015-08-02
hwtr_mgr.Init_by_ctor(wikibase_api, this, new Wdata_lbl_wkr_wiki(wikibase_api, this), gplx.langs.htmls.encoders.Gfo_url_encoder_.Href, toggle_mgr, app.Usere().Wiki().Xwiki_mgr()); Wbase_claim_base prop = prop_grp.Get_at(i);
this.Hwtr_msgs_make(); if (selected == null) selected = prop; // if selected not set, set it; will always set to 1st prop
Gfo_evt_mgr_.Sub_same_many(app.Usere(), this, Xoue_user.Evt_lang_changed); if (prop.Rank_tid() == Wbase_claim_rank_.Tid__preferred) { // if prop is preferred, select it and exit;
} selected = prop;
private void Hwtr_msgs_make() { break;
// if (!app.Wiki_mgr().Wiki_regy().Has(Xow_domain_itm_.Bry__wikidata)) return; // DELETE: don't know why guard is needed; breaks test; DATE:2016-10-20 }
Xol_lang_itm new_lang = app.Usere().Lang(); }
Xowe_wiki cur_wiki = this.Wdata_wiki(); switch (selected.Snak_tid()) { // SEE:NOTE:novalue/somevalue
cur_wiki.Xtn_mgr().Xtn_wikibase().Load_msgs(cur_wiki, new_lang); case Wbase_claim_value_type_.Tid__novalue:
Wdata_hwtr_msgs hwtr_msgs = Wdata_hwtr_msgs.new_(cur_wiki.Msg_mgr()); bfr.Add(wiki.Msg_mgr().Val_by_id(Xol_msg_itm_.Id_xowa_wikidata_novalue));
hwtr_mgr.Init_by_lang(new_lang, hwtr_msgs); break;
} case Wbase_claim_value_type_.Tid__somevalue:
public static void Write_json_as_html(Json_parser jdoc_parser, Bry_bfr bfr, byte[] data_raw) { bfr.Add(wiki.Msg_mgr().Val_by_id(Xol_msg_itm_.Id_xowa_wikidata_somevalue));
bfr.Add(Xoh_consts.Span_bgn_open).Add(Xoh_consts.Id_atr).Add(Html_json_id).Add(Xoh_consts.__end_quote); // <span id="xowa-wikidata-json"> break;
Json_doc json = jdoc_parser.Parse(data_raw); default: {
json.Root_nde().Print_as_json(bfr, 0); prop_val_visitor.Init(bfr, hwtr_mgr.Msgs(), lang_key, mode_is_statements);
bfr.Add(Xoh_consts.Span_end); selected.Welcome(prop_val_visitor);
} break;
public Object Invk(GfsCtx ctx, int ikey, String k, GfoMsg m) { }
if (ctx.Match(k, Invk_enabled)) return Yn.To_str(enabled); }
else if (ctx.Match(k, Invk_enabled_)) enabled = m.ReadYn("v"); }
else if (ctx.Match(k, Invk_domain)) return String_.new_u8(domain); }
else if (ctx.Match(k, Invk_domain_)) domain = m.ReadBry("v"); public byte[] Popup_text(Xoae_page page) {
else if (ctx.Match(k, Invk_property_wkr)) return m.ReadYnOrY("v") ? Property_wkr_or_new() : Gfo_invk_.Noop; Hwtr_mgr_assert();
else if (ctx.Match(k, Xoue_user.Evt_lang_changed)) Hwtr_msgs_make(); Wdata_doc wdoc = Doc_mgr.Get_by_exact_id_or_null(page.Ttl().Full_db());
else return Gfo_invk_.Rv_unhandled; if (wdoc == null) return Bry_.Empty;
return this; return hwtr_mgr.Popup(wdoc);
} }
private static final String Invk_enabled = "enabled", Invk_enabled_ = "enabled_", Invk_domain = "domain", Invk_domain_ = "domain_", Invk_property_wkr = "property_wkr"; public void Write_json_as_html(Bry_bfr bfr, Xoa_ttl page_ttl, byte[] data_raw) {
public Xop_log_property_wkr Property_wkr_or_new() { Hwtr_mgr_assert();
if (property_wkr == null) property_wkr = app.Log_mgr().Make_wkr_property(); Wdata_doc wdoc = Doc_mgr.Get_by_exact_id_or_null(page_ttl.Full_db());
return property_wkr; if (wdoc == null) return;
} hwtr_mgr.Init_by_wdoc(wdoc);
public static final int Ns_property = 120; bfr.Add(hwtr_mgr.Write(wdoc));
public static final String Ns_property_name = "Property"; }
public static final byte[] Ns_property_name_bry = Bry_.new_a7(Ns_property_name); private void Hwtr_mgr_assert() {
public static final byte[] Bry_q = Bry_.new_a7("q"); if (hwtr_mgr != null) return;
public static final byte[] Xoapi_toggle_mgr toggle_mgr = app.Api_root().Html().Page().Toggle_mgr();
Ttl_prefix_qid_bry_db = Bry_.new_a7("q") // NOTE: for historical reasons this is standardized as lowercase q not Q; DATE:2015-06-12 Xoapi_wikibase wikibase_api = app.Api_root().Xtns().Wikibase();
, Ttl_prefix_qid_bry_gui = Bry_.new_a7("Q") // NOTE: use uppercase Q for writing html; DATE:2015-06-12 hwtr_mgr = new Wdata_hwtr_mgr();
, Ttl_prefix_pid_bry = Bry_.new_a7("Property:P") hwtr_mgr.Init_by_ctor(wikibase_api, this, new Wdata_lbl_wkr_wiki(wikibase_api, this), gplx.langs.htmls.encoders.Gfo_url_encoder_.Href, toggle_mgr, app.Usere().Wiki().Xwiki_mgr());
; this.Hwtr_msgs_make();
public static final byte[] Html_json_id = Bry_.new_a7("xowa-wikidata-json"); Gfo_evt_mgr_.Sub_same_many(app.Usere(), this, Xoue_user.Evt_lang_changed);
public static boolean Wiki_page_is_json(int wiki_tid, int ns_id) { }
switch (wiki_tid) { private void Hwtr_msgs_make() {
case Xow_domain_tid_.Tid__wikidata: // if (!app.Wiki_mgr().Wiki_regy().Has(Xow_domain_itm_.Bry__wikidata)) return; // DELETE: don't know why guard is needed; breaks test; DATE:2016-10-20
if (ns_id == Xow_ns_.Tid__main || ns_id == gplx.xowa.xtns.wbases.Wdata_wiki_mgr.Ns_property) Xol_lang_itm new_lang = app.Usere().Lang();
return true; Xowe_wiki cur_wiki = this.Wdata_wiki();
break; cur_wiki.Xtn_mgr().Xtn_wikibase().Load_msgs(cur_wiki, new_lang);
case Xow_domain_tid_.Tid__home: Wdata_hwtr_msgs hwtr_msgs = Wdata_hwtr_msgs.new_(cur_wiki.Msg_mgr());
if (ns_id == gplx.xowa.xtns.wbases.Wdata_wiki_mgr.Ns_property) hwtr_mgr.Init_by_lang(new_lang, hwtr_msgs);
return true; }
break; public static void Write_json_as_html(Json_parser jdoc_parser, Bry_bfr bfr, byte[] data_raw) {
} bfr.Add(Xoh_consts.Span_bgn_open).Add(Xoh_consts.Id_atr).Add(Html_json_id).Add(Xoh_consts.__end_quote); // <span id="xowa-wikidata-json">
return false; Json_doc json = jdoc_parser.Parse(data_raw);
} json.Root_nde().Print_as_json(bfr, 0);
public static void Log_missing_qid(Xop_ctx ctx, String type, byte[] id) { bfr.Add(Xoh_consts.Span_end);
if (id == null) id = Bry_.Empty; }
ctx.Wiki().Appe().Usr_dlg().Log_many("", "", "Unknown id in wikidata; type=~{0} id=~{1} page=~{2}", type, id, ctx.Page().Url_bry_safe()); public Object Invk(GfsCtx ctx, int ikey, String k, GfoMsg m) {
} if (ctx.Match(k, Invk_enabled)) return Yn.To_str(enabled);
} else if (ctx.Match(k, Invk_enabled_)) enabled = m.ReadYn("v");
/* else if (ctx.Match(k, Invk_domain)) return String_.new_u8(domain);
NOTE:novalue/somevalue else if (ctx.Match(k, Invk_domain_)) domain = m.ReadBry("v");
Rough approximation of wikibase logic which is more involved with its different SnakFormatters else if (ctx.Match(k, Invk_property_wkr)) return m.ReadYnOrY("v") ? Property_wkr_or_new() : Gfo_invk_.Noop;
* https://github.com/wikimedia/mediawiki-extensions-Wikibase/blob/master/lib/includes/Formatters/OutputFormatSnakFormatterFactory.php: formatter factory; note lines for somevalue / novalue else if (ctx.Match(k, Xoue_user.Evt_lang_changed)) Hwtr_msgs_make();
* https://github.com/wikimedia/mediawiki-extensions-Wikibase/blob/master/lib/includes/Formatters/MessageSnakFormatter.php: formatter definition else return Gfo_invk_.Rv_unhandled;
* https://github.com/wikimedia/mediawiki-extensions-Wikibase/blob/master/repo/i18n/en.json: message definitions return this;
*/ }
private static final String Invk_enabled = "enabled", Invk_enabled_ = "enabled_", Invk_domain = "domain", Invk_domain_ = "domain_", Invk_property_wkr = "property_wkr";
public Xop_log_property_wkr Property_wkr_or_new() {
if (property_wkr == null) property_wkr = app.Log_mgr().Make_wkr_property();
return property_wkr;
}
public static final int Ns_property = 120;
public static final String Ns_property_name = "Property";
public static final byte[] Ns_property_name_bry = Bry_.new_a7(Ns_property_name);
public static final int Ns_lexeme = 146;
public static final String Ns_lexeme_name = "Lexeme";
public static final byte[] Ns_lexeme_name_bry = Bry_.new_a7(Ns_lexeme_name);
public static final byte[] Html_json_id = Bry_.new_a7("xowa-wikidata-json");
public static boolean Wiki_page_is_json(int wiki_tid, int ns_id) {
switch (wiki_tid) {
case Xow_domain_tid_.Tid__wikidata:
switch (ns_id) {
case Xow_ns_.Tid__main:
case Wdata_wiki_mgr.Ns_property:
case Wdata_wiki_mgr.Ns_lexeme:
return true;
default:
return false;
}
case Xow_domain_tid_.Tid__home:
if (ns_id == gplx.xowa.xtns.wbases.Wdata_wiki_mgr.Ns_property)
return true;
break;
}
return false;
}
public static void Log_missing_qid(Xop_ctx ctx, String type, byte[] id) {
if (id == null) id = Bry_.Empty;
ctx.Wiki().Appe().Usr_dlg().Log_many("", "", "Unknown id in wikidata; type=~{0} id=~{1} page=~{2}", type, id, ctx.Page().Url_bry_safe());
}
}
/*
NOTE:novalue/somevalue
Rough approximation of wikibase logic which is more involved with its different SnakFormatters
* https://github.com/wikimedia/mediawiki-extensions-Wikibase/blob/master/lib/includes/Formatters/OutputFormatSnakFormatterFactory.php: formatter factory; note lines for somevalue / novalue
* https://github.com/wikimedia/mediawiki-extensions-Wikibase/blob/master/lib/includes/Formatters/MessageSnakFormatter.php: formatter definition
* https://github.com/wikimedia/mediawiki-extensions-Wikibase/blob/master/repo/i18n/en.json: message definitions
*/

@ -1,6 +1,6 @@
/* /*
XOWA: the XOWA Offline Wiki Application XOWA: the XOWA Offline Wiki Application
Copyright (C) 2012-2017 gnosygnu@gmail.com Copyright (C) 2012-2020 gnosygnu@gmail.com
XOWA is licensed under the terms of the General Public License (GPL) Version 3, XOWA is licensed under the terms of the General Public License (GPL) Version 3,
or alternatively under the terms of the Apache License Version 2.0. or alternatively under the terms of the Apache License Version 2.0.
@ -13,14 +13,22 @@ The terms of each license can be found in the source code repository:
GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
*/ */
package gplx.xowa.xtns.wbases.claims; import gplx.*; import gplx.xowa.*; import gplx.xowa.xtns.*; import gplx.xowa.xtns.wbases.*; package gplx.xowa.xtns.wbases.claims;
import gplx.xowa.xtns.wbases.claims.itms.*;
public interface Wbase_claim_visitor { import gplx.xowa.xtns.wbases.claims.itms.Wbase_claim_entity;
void Visit_str (Wbase_claim_string itm); import gplx.xowa.xtns.wbases.claims.itms.Wbase_claim_globecoordinate;
void Visit_entity (Wbase_claim_entity itm); import gplx.xowa.xtns.wbases.claims.itms.Wbase_claim_monolingualtext;
void Visit_monolingualtext (Wbase_claim_monolingualtext itm); import gplx.xowa.xtns.wbases.claims.itms.Wbase_claim_quantity;
void Visit_quantity (Wbase_claim_quantity itm); import gplx.xowa.xtns.wbases.claims.itms.Wbase_claim_string;
void Visit_time (Wbase_claim_time itm); import gplx.xowa.xtns.wbases.claims.itms.Wbase_claim_time;
void Visit_globecoordinate (Wbase_claim_globecoordinate itm); import gplx.xowa.xtns.wbases.claims.itms.Wbase_claim_value;
void Visit_system (Wbase_claim_value itm);
} public interface Wbase_claim_visitor {
void Visit_str (Wbase_claim_string itm);
void Visit_entity (Wbase_claim_entity itm);
void Visit_monolingualtext (Wbase_claim_monolingualtext itm);
void Visit_quantity (Wbase_claim_quantity itm);
void Visit_time (Wbase_claim_time itm);
void Visit_globecoordinate (Wbase_claim_globecoordinate itm);
void Visit_system (Wbase_claim_value itm);
}

@ -1,6 +1,6 @@
/* /*
XOWA: the XOWA Offline Wiki Application XOWA: the XOWA Offline Wiki Application
Copyright (C) 2012-2017 gnosygnu@gmail.com Copyright (C) 2012-2020 gnosygnu@gmail.com
XOWA is licensed under the terms of the General Public License (GPL) Version 3, XOWA is licensed under the terms of the General Public License (GPL) Version 3,
or alternatively under the terms of the Apache License Version 2.0. or alternatively under the terms of the Apache License Version 2.0.
@ -13,17 +13,54 @@ The terms of each license can be found in the source code repository:
GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
*/ */
package gplx.xowa.xtns.wbases.claims.enums; import gplx.*; import gplx.xowa.*; import gplx.xowa.xtns.*; import gplx.xowa.xtns.wbases.*; import gplx.xowa.xtns.wbases.claims.*; package gplx.xowa.xtns.wbases.claims.enums;
public class Wbase_claim_entity_type_ {
public static final byte import gplx.Bry_;
Tid__item = 0 import gplx.Byte_ascii;
, Tid__property = 1 import gplx.Err_;
, Tid__lexeme = 2
; // NOTE: could not find definitive list, so using these links for now
public static final Wbase_enum_hash Reg = new Wbase_enum_hash("claim.entity_type", 3); // REF.MW:https://github.com/Wikidata/Wikidata-Toolkit/blob/master/wdtk-datamodel/src/main/java/org/wikidata/wdtk/datamodel/implementation/EntityIdValueImpl.java
public static final Wbase_enum_itm // REF.MW:https://github.com/wikimedia/wikibase-property-suggester-scripts/blob/1d25e76f894796bfd57dd107102cf39088885138/propertysuggester/parser/JsonReader.py
Itm__item = Reg.Add(Tid__item , "item") public class Wbase_claim_entity_type_ {
, Itm__property = Reg.Add(Tid__property , "property") public static final byte
, Itm__lexeme = Reg.Add(Tid__lexeme , "lexeme") Tid__item = 0
; , Tid__property = 1
, Tid__lexeme = 2
, Tid__sense = 3
, Tid__form = 4
;
public static final Wbase_enum_hash Reg = new Wbase_enum_hash("claim.entity_type", 5);
public static final Wbase_enum_itm
Itm__item = Reg.Add(Tid__item , "item")
, Itm__property = Reg.Add(Tid__property , "property")
, Itm__lexeme = Reg.Add(Tid__lexeme , "lexeme")
, Itm__sense = Reg.Add(Tid__sense , "sense")
, Itm__form = Reg.Add(Tid__form , "form")
;
public static Wbase_enum_itm ToTid(byte[] id) {
// fail if null or 0-length
if (Bry_.Len_eq_0(id)) {
throw Err_.new_unhandled_default(id);
}
// get 1st byte and uppercase it
byte b0 = id[0];
if (b0 > Byte_ascii.Ltr_Z) {
b0 -= 32; // uppercases
}
// return item; NOTE: only doing types which have namespaces (i.e.: there is no Sense:S1 or Form:F1)
switch (b0) {
case Byte_ascii.Ltr_Q:
return Wbase_claim_entity_type_.Itm__item;
case Byte_ascii.Ltr_P:
return Wbase_claim_entity_type_.Itm__property;
case Byte_ascii.Ltr_L:
return Wbase_claim_entity_type_.Itm__lexeme;
default:
throw Err_.new_unhandled_default(id);
}
}
} }

@ -1,6 +1,6 @@
/* /*
XOWA: the XOWA Offline Wiki Application XOWA: the XOWA Offline Wiki Application
Copyright (C) 2012-2017 gnosygnu@gmail.com Copyright (C) 2012-2020 gnosygnu@gmail.com
XOWA is licensed under the terms of the General Public License (GPL) Version 3, XOWA is licensed under the terms of the General Public License (GPL) Version 3,
or alternatively under the terms of the Apache License Version 2.0. or alternatively under the terms of the Apache License Version 2.0.
@ -13,50 +13,57 @@ The terms of each license can be found in the source code repository:
GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
*/ */
package gplx.xowa.xtns.wbases.claims.enums; import gplx.*; import gplx.xowa.*; import gplx.xowa.xtns.*; import gplx.xowa.xtns.wbases.*; import gplx.xowa.xtns.wbases.claims.*; package gplx.xowa.xtns.wbases.claims.enums;
public class Wbase_claim_type_ {
public static final byte // SERIALIZED:wbase_prop|datatype; REF:https://www.wikidata.org/wiki/Help:Data_type import gplx.Bry_;
Tid__unknown = 0
, Tid__value = 1 public class Wbase_claim_type_ {
, Tid__bad = 1 public static final byte // SERIALIZED:wbase_prop|datatype; REF:https://www.wikidata.org/wiki/Help:Data_type
, Tid__string = 2 Tid__unknown = 0
, Tid__quantity = 3 , Tid__value = 1
, Tid__time = 4 , Tid__bad = 1
, Tid__globecoordinate = 5 , Tid__string = 2
, Tid__monolingualtext = 6 , Tid__quantity = 3
, Tid__entity = 7 , Tid__time = 4
, Tid__property = 8 , Tid__globecoordinate = 5
, Tid__math = 9 , Tid__monolingualtext = 6
, Tid__url = 10 , Tid__entity = 7
, Tid__externalid = 11 , Tid__property = 8
, Tid__commonsmedia = 12 , Tid__math = 9
, Tid__geo_shape = 13 , Tid__url = 10
, Tid__tabular_data = 14 , Tid__externalid = 11
, Tid__lexeme = 15 , Tid__commonsmedia = 12
; , Tid__geo_shape = 13
public static final Wbase_enum_hash Reg = new Wbase_enum_hash("claim.data_type", 16); , Tid__tabular_data = 14
public static final Wbase_enum_itm , Tid__lexeme = 15
Itm__unknown = New(Tid__unknown , "unknown") , Tid__form = 16
, Itm__bad = New(Tid__bad , "bad") // NOTE: wikidata identifies several entries as "bad"; Q1615351|'s-Graveland, Q107538|Baco; DATE:2013-10-20 , Tid__sense = 17
, Itm__string = New(Tid__string , "string") // EX:wd:Property:P1030 ;
, Itm__quantity = New(Tid__quantity , "quantity") public static final Wbase_enum_hash Reg = new Wbase_enum_hash("claim.data_type", 18);
, Itm__time = New(Tid__time , "time") public static final Wbase_enum_itm
, Itm__globecoordinate = New(Tid__globecoordinate , "globecoordinate" , "globe-coordinate") Itm__unknown = New(Tid__unknown , "unknown")
, Itm__monolingualtext = New(Tid__monolingualtext , "monolingualtext") , Itm__bad = New(Tid__bad , "bad") // NOTE: wikidata identifies several entries as "bad"; Q1615351|'s-Graveland, Q107538|Baco; DATE:2013-10-20
, Itm__entity = New(Tid__entity , "wikibase-entityid" , "wikibase-item") , Itm__string = New(Tid__string , "string") // EX:wd:Property:P1030
, Itm__property = New(Tid__property , "wikibase-property") // EX:wd:Property:P1646 , Itm__quantity = New(Tid__quantity , "quantity")
, Itm__url = New(Tid__url , "url") // EX:wd:Property:P1019 , Itm__time = New(Tid__time , "time")
, Itm__commonsmedia = New(Tid__commonsmedia , "commonsMedia") // EX:wd:Property:P14 , Itm__globecoordinate = New(Tid__globecoordinate , "globecoordinate" , "globe-coordinate")
, Itm__externalid = New(Tid__externalid , "external-id") // EX:wd:Property:P1003 , Itm__monolingualtext = New(Tid__monolingualtext , "monolingualtext")
, Itm__math = New(Tid__math , "math") // EX:wd:Property:P2534 , Itm__entity = New(Tid__entity , "wikibase-entityid" , "wikibase-item")
, Itm__geo_shape = New(Tid__geo_shape , "geo-shape") // EX:wd:Property:P3896 , Itm__property = New(Tid__property , "wikibase-property") // EX:wd:Property:P1646
, Itm__tabular_data = New(Tid__tabular_data , "tabular-data") // EX:wd:Property:P4179 , Itm__url = New(Tid__url , "url") // EX:wd:Property:P1019
, Itm__lexeme = New(Tid__lexeme , "lexeme") // EX:wd:Property:P5188 , Itm__commonsmedia = New(Tid__commonsmedia , "commonsMedia") // EX:wd:Property:P14
; , Itm__externalid = New(Tid__externalid , "external-id") // EX:wd:Property:P1003
private static Wbase_enum_itm New(byte tid, String key) {return New(tid, key, key);} , Itm__math = New(Tid__math , "math") // EX:wd:Property:P2534
private static Wbase_enum_itm New(byte tid, String key, String scrib) {return Reg.Add(new Wbase_claim_type(tid, key, scrib));} , Itm__geo_shape = New(Tid__geo_shape , "geo-shape") // EX:wd:Property:P3896
public static String Get_scrib_or_unknown(byte tid) {return ((Wbase_claim_type)Reg.Get_itm_or(tid, Itm__unknown)).Key_for_scrib();} , Itm__tabular_data = New(Tid__tabular_data , "tabular-data") // EX:wd:Property:P4179
public static byte Get_tid_or_unknown(String key) {return Get_tid_or_unknown(Bry_.new_u8(key));} , Itm__lexeme = New(Tid__lexeme , "lexeme") // EX:wd:Lexeme:L2
public static byte Get_tid_or_unknown(byte[] key) {return Get_tid_or_unknown(key, 0, key.length);} , Itm__form = New(Tid__form , "form") // EX:wd:Lexeme:L2 P5830
public static byte Get_tid_or_unknown(byte[] key, int bgn, int end) {return Reg.Get_tid_or(key, bgn, end, Tid__unknown);} , Itm__sense = New(Tid__sense , "sense") // EX:wd:Lexeme:L2 P6072
;
private static Wbase_enum_itm New(byte tid, String key) {return New(tid, key, key);}
private static Wbase_enum_itm New(byte tid, String key, String scrib) {return Reg.Add(new Wbase_claim_type(tid, key, scrib));}
public static String Get_scrib_or_unknown(byte tid) {return ((Wbase_claim_type)Reg.Get_itm_or(tid, Itm__unknown)).Key_for_scrib();}
public static byte Get_tid_or_unknown(String key) {return Get_tid_or_unknown(Bry_.new_u8(key));}
public static byte Get_tid_or_unknown(byte[] key) {return Get_tid_or_unknown(key, 0, key.length);}
public static byte Get_tid_or_unknown(byte[] key, int bgn, int end) {return Reg.Get_tid_or(key, bgn, end, Tid__unknown);}
} }

@ -1,6 +1,6 @@
/* /*
XOWA: the XOWA Offline Wiki Application XOWA: the XOWA Offline Wiki Application
Copyright (C) 2012-2017 gnosygnu@gmail.com Copyright (C) 2012-2020 gnosygnu@gmail.com
XOWA is licensed under the terms of the General Public License (GPL) Version 3, XOWA is licensed under the terms of the General Public License (GPL) Version 3,
or alternatively under the terms of the Apache License Version 2.0. or alternatively under the terms of the Apache License Version 2.0.
@ -13,38 +13,78 @@ The terms of each license can be found in the source code repository:
GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
*/ */
package gplx.xowa.xtns.wbases.claims.itms; import gplx.*; import gplx.xowa.*; import gplx.xowa.xtns.*; import gplx.xowa.xtns.wbases.*; import gplx.xowa.xtns.wbases.claims.*; package gplx.xowa.xtns.wbases.claims.itms;
import gplx.xowa.xtns.wbases.claims.enums.*;
public class Wbase_claim_entity extends Wbase_claim_base { import gplx.Bry_;
public Wbase_claim_entity(int pid, byte snak_tid, byte entity_tid, byte[] entity_id_bry) {super(pid, snak_tid); import gplx.Byte_ascii;
this.entity_tid = entity_tid; import gplx.Err_;
this.entity_id_bry = entity_id_bry; import gplx.Int_;
this.entity_id = Bry_.To_int(entity_id_bry); import gplx.String_;
} import gplx.xowa.xtns.wbases.claims.Wbase_claim_visitor;
@Override public byte Val_tid() {return Wbase_claim_type_.Tid__entity;} import gplx.xowa.xtns.wbases.claims.enums.Wbase_claim_entity_type_;
public int Entity_id() {return entity_id;} private final int entity_id; import gplx.xowa.xtns.wbases.claims.enums.Wbase_claim_type_;
public byte[] Entity_id_bry() {return entity_id_bry;} private final byte[] entity_id_bry; import gplx.xowa.xtns.wbases.claims.enums.Wbase_claim_value_type_;
public byte Entity_tid() {return entity_tid;} private final byte entity_tid;
public boolean Entity_tid_is_qid() {return entity_tid == Wbase_claim_entity_type_.Tid__item;} public class Wbase_claim_entity extends Wbase_claim_base {
public String Entity_tid_str() {return Wbase_claim_entity_type_.Reg.Get_str_or_fail(entity_tid);} public Wbase_claim_entity(int pid, byte snak_tid, byte entityType, byte[] numericIdBry) {
public byte[] Entity_tid_bry() {return Wbase_claim_entity_type_.Reg.Get_bry_or_fail(entity_tid);} this(pid, snak_tid, entityType, numericIdBry, null);
}
public byte[] Page_ttl_db() {return To_xid__db(entity_tid, entity_id_bry);} public Wbase_claim_entity(int pid, byte snak_tid, byte entityType, byte[] numericIdBry, byte[] id) {
public byte[] Page_ttl_gui() { super(pid, snak_tid);
return entity_tid == Wbase_claim_entity_type_.Tid__item this.entityType = entityType;
? Bry_.Add(Wdata_wiki_mgr.Ttl_prefix_qid_bry_gui, entity_id_bry) this.numericIdBry = numericIdBry;
: Bry_.Add(Wdata_wiki_mgr.Ttl_prefix_pid_bry, entity_id_bry) // NOTE: form and sense claims do not have `numeric-id`; DATE:2020-07-27
; if (numericIdBry != null)
} this.numericId = Bry_.To_int(numericIdBry);
@Override public void Welcome(Wbase_claim_visitor visitor) {visitor.Visit_entity(this);} // NOTE: item, property, lexeme do not have an id (Make_claims calls don't pass them)
@Override public String toString() {// TEST: this.id = id == null ? ToId(entityType, numericIdBry) : id;
return String_.Concat_with_str("|", Wbase_claim_value_type_.Reg.Get_str_or_fail(this.Snak_tid()), Wbase_claim_type_.Reg.Get_str_or_fail(this.Val_tid()), this.Entity_tid_str(), Int_.To_str(entity_id)); }
} @Override public byte Val_tid() {return Wbase_claim_type_.Tid__entity;}
public byte[] Id() {return id;} private final byte[] id; // EX: Q123
public static byte[] To_xid__db(byte tid, byte[] bry) { // EX: 'item,2' -> q2; 'property,2' -> Property:P2 public int Entity_id() {return numericId;} private int numericId; // EX: 123
return tid == Wbase_claim_entity_type_.Tid__item public byte[] Entity_id_bry() {return numericIdBry;} private final byte[] numericIdBry;
? Bry_.Add(Wdata_wiki_mgr.Ttl_prefix_qid_bry_gui, bry) public byte Entity_tid() {return entityType;} private final byte entityType;
: Bry_.Add(Wdata_wiki_mgr.Ttl_prefix_pid_bry, bry) public boolean Entity_tid_is_qid() {return entityType == Wbase_claim_entity_type_.Tid__item;}
; public String Entity_tid_str() {return Wbase_claim_entity_type_.Reg.Get_str_or_fail(entityType);}
} public byte[] Entity_tid_bry() {return Wbase_claim_entity_type_.Reg.Get_bry_or_fail(entityType);}
} public byte[] Page_ttl_db() {return To_xid__db(entityType, numericIdBry);}
public byte[] Page_ttl_gui() {return Bry_.Add(ToTtlPrefix(entityType), numericIdBry);}
@Override public void Welcome(Wbase_claim_visitor visitor) {visitor.Visit_entity(this);}
@Override public String toString() {// TEST:
return String_.Concat_with_str("|", Wbase_claim_value_type_.Reg.Get_str_or_fail(this.Snak_tid()), Wbase_claim_type_.Reg.Get_str_or_fail(this.Val_tid()), this.Entity_tid_str(), Int_.To_str(numericId), String_.new_u8(id));
}
public static byte[] To_xid__db(byte tid, byte[] bry) {return Bry_.Add(ToTtlPrefix(tid), bry);} // EX: 'item,2' -> Q2; 'property,2' -> Property:P2
private static byte[] ToTtlPrefix(byte entityType) {
switch (entityType) {
case Wbase_claim_entity_type_.Tid__item:
return TTL_PREFIX_QID;
case Wbase_claim_entity_type_.Tid__property:
return TTL_PREFIX_PID;
case Wbase_claim_entity_type_.Tid__lexeme:
return TTL_PREFIX_LID;
default:
throw Err_.new_unhandled_default(entityType);
}
}
private static byte[] ToId(byte entityType, byte[] numericId) {
switch (entityType) {
case Wbase_claim_entity_type_.Tid__item:
return Bry_.Add(Byte_ascii.Ltr_Q, numericId);
case Wbase_claim_entity_type_.Tid__property:
return Bry_.Add(Byte_ascii.Ltr_P, numericId);
case Wbase_claim_entity_type_.Tid__lexeme:
return Bry_.Add(Byte_ascii.Ltr_L, numericId);
case Wbase_claim_entity_type_.Tid__form:
case Wbase_claim_entity_type_.Tid__sense:
default:
throw Err_.new_unhandled_default(entityType);
}
}
private static final byte[]
TTL_PREFIX_QID = Bry_.new_a7("Q") // NOTE: use uppercase Q for writing html; DATE:2015-06-12
, TTL_PREFIX_PID = Bry_.new_a7("Property:P")
, TTL_PREFIX_LID = Bry_.new_a7("Lexeme:L")
// TOMBSTONE: TTL_PREFIX_QID_OLD = Bry_.new_a7("q") // NOTE: for historical reasons this is standardized as lowercase q not Q; DATE:2015-06-12
;
}

@ -1,6 +1,6 @@
/* /*
XOWA: the XOWA Offline Wiki Application XOWA: the XOWA Offline Wiki Application
Copyright (C) 2012-2017 gnosygnu@gmail.com Copyright (C) 2012-2020 gnosygnu@gmail.com
XOWA is licensed under the terms of the General Public License (GPL) Version 3, XOWA is licensed under the terms of the General Public License (GPL) Version 3,
or alternatively under the terms of the Apache License Version 2.0. or alternatively under the terms of the Apache License Version 2.0.
@ -13,104 +13,126 @@ The terms of each license can be found in the source code repository:
GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
*/ */
package gplx.xowa.xtns.wbases.parsers; import gplx.*; import gplx.xowa.*; import gplx.xowa.xtns.*; import gplx.xowa.xtns.wbases.*; package gplx.xowa.xtns.wbases.parsers;
import gplx.langs.jsons.*;
import gplx.xowa.xtns.wbases.core.*; import gplx.xowa.xtns.wbases.claims.*; import gplx.xowa.xtns.wbases.claims.enums.*; import gplx.xowa.xtns.wbases.claims.itms.*; import gplx.Byte_;
public class Wbase_claim_factory { import gplx.Err_;
public Wbase_claim_base Parse(byte[] qid, int pid, byte snak_tid, Json_nde nde, byte value_tid, Json_itm value_itm) { import gplx.langs.jsons.Json_itm;
switch (value_tid) { import gplx.langs.jsons.Json_kv;
case Wbase_claim_type_.Tid__string: return new Wbase_claim_string(pid, snak_tid, value_itm.Data_bry()); import gplx.langs.jsons.Json_nde;
case Wbase_claim_type_.Tid__entity: return Parse_datavalue_entity (qid, pid, snak_tid, Json_nde.cast(value_itm)); import gplx.xowa.xtns.wbases.claims.enums.Wbase_claim_entity_type_;
case Wbase_claim_type_.Tid__time: return Parse_datavalue_time (qid, pid, snak_tid, Json_nde.cast(value_itm)); import gplx.xowa.xtns.wbases.claims.enums.Wbase_claim_type_;
case Wbase_claim_type_.Tid__quantity: return Parse_datavalue_quantity (qid, pid, snak_tid, Json_nde.cast(value_itm)); import gplx.xowa.xtns.wbases.claims.itms.Wbase_claim_base;
case Wbase_claim_type_.Tid__globecoordinate: return Parse_datavalue_globecoordinate (qid, pid, snak_tid, Json_nde.cast(value_itm)); import gplx.xowa.xtns.wbases.claims.itms.Wbase_claim_entity;
case Wbase_claim_type_.Tid__monolingualtext: return Parse_datavalue_monolingualtext (qid, pid, snak_tid, Json_nde.cast(value_itm)); import gplx.xowa.xtns.wbases.claims.itms.Wbase_claim_entity_;
default: throw Err_.new_unhandled_default(value_tid); import gplx.xowa.xtns.wbases.claims.itms.Wbase_claim_globecoordinate;
} import gplx.xowa.xtns.wbases.claims.itms.Wbase_claim_globecoordinate_;
} import gplx.xowa.xtns.wbases.claims.itms.Wbase_claim_monolingualtext;
private Wbase_claim_entity Parse_datavalue_entity(byte[] qid, int pid, byte snak_tid, Json_nde nde) { import gplx.xowa.xtns.wbases.claims.itms.Wbase_claim_monolingualtext_;
int len = nde.Len(); import gplx.xowa.xtns.wbases.claims.itms.Wbase_claim_quantity;
byte entity_tid = Byte_.Max_value_127; import gplx.xowa.xtns.wbases.claims.itms.Wbase_claim_quantity_;
byte[] entity_id_bry = null; import gplx.xowa.xtns.wbases.claims.itms.Wbase_claim_string;
for (int i = 0; i < len; ++i) { import gplx.xowa.xtns.wbases.claims.itms.Wbase_claim_time;
Json_kv sub = Json_kv.cast(nde.Get_at(i)); import gplx.xowa.xtns.wbases.claims.itms.Wbase_claim_time_;
byte tid = Wbase_claim_entity_.Reg.Get_tid_or_max_and_log(qid, sub.Key().Data_bry()); if (tid == Byte_.Max_value_127) continue;
switch (tid) { public class Wbase_claim_factory {
case Wbase_claim_entity_.Tid__entity_type: entity_tid = Wbase_claim_entity_type_.Reg.Get_tid_or_fail(sub.Val().Data_bry()); break; public Wbase_claim_base Parse(byte[] qid, int pid, byte snak_tid, Json_nde nde, byte value_tid, Json_itm value_itm) {
case Wbase_claim_entity_.Tid__numeric_id: entity_id_bry = sub.Val().Data_bry(); break; switch (value_tid) {
case Wbase_claim_entity_.Tid__id: break; // ignore case Wbase_claim_type_.Tid__string: return new Wbase_claim_string(pid, snak_tid, value_itm.Data_bry());
} case Wbase_claim_type_.Tid__entity: return Parse_datavalue_entity (qid, pid, snak_tid, Json_nde.cast(value_itm));
} case Wbase_claim_type_.Tid__time: return Parse_datavalue_time (qid, pid, snak_tid, Json_nde.cast(value_itm));
if (entity_id_bry == null) throw Err_.new_wo_type("pid is invalid entity", "pid", pid); case Wbase_claim_type_.Tid__quantity: return Parse_datavalue_quantity (qid, pid, snak_tid, Json_nde.cast(value_itm));
return new Wbase_claim_entity(pid, snak_tid, entity_tid, entity_id_bry); case Wbase_claim_type_.Tid__globecoordinate: return Parse_datavalue_globecoordinate (qid, pid, snak_tid, Json_nde.cast(value_itm));
} case Wbase_claim_type_.Tid__monolingualtext: return Parse_datavalue_monolingualtext (qid, pid, snak_tid, Json_nde.cast(value_itm));
private Wbase_claim_monolingualtext Parse_datavalue_monolingualtext(byte[] qid, int pid, byte snak_tid, Json_nde nde) { default: throw Err_.new_unhandled_default(value_tid);
int len = nde.Len(); }
byte[] lang = null, text = null; }
for (int i = 0; i < len; ++i) { private Wbase_claim_entity Parse_datavalue_entity(byte[] qid, int pid, byte snak_tid, Json_nde nde) {
Json_kv sub = Json_kv.cast(nde.Get_at(i)); int len = nde.Len();
byte tid = Wbase_claim_monolingualtext_.Reg.Get_tid_or_max_and_log(qid, sub.Key().Data_bry()); if (tid == Byte_.Max_value_127) continue; byte entityType = Byte_.Max_value_127;
byte[] sub_val_bry = sub.Val().Data_bry(); byte[] numericId = null;
switch (tid) { byte[] id = null;
case Wbase_claim_monolingualtext_.Tid__text: text = sub_val_bry; break; for (int i = 0; i < len; ++i) {
case Wbase_claim_monolingualtext_.Tid__language: lang = sub_val_bry; break; Json_kv sub = Json_kv.cast(nde.Get_at(i));
} byte tid = Wbase_claim_entity_.Reg.Get_tid_or_max_and_log(qid, sub.Key().Data_bry()); if (tid == Byte_.Max_value_127) continue;
} byte[] subValBry = sub.Val().Data_bry();
if (lang == null || text == null) throw Err_.new_wo_type("pid is invalid monolingualtext", "pid", pid); switch (tid) {
return new Wbase_claim_monolingualtext(pid, snak_tid, lang, text); case Wbase_claim_entity_.Tid__entity_type: entityType = Wbase_claim_entity_type_.Reg.Get_tid_or_fail(subValBry); break;
} case Wbase_claim_entity_.Tid__numeric_id: numericId = subValBry; break;
private Wbase_claim_globecoordinate Parse_datavalue_globecoordinate(byte[] qid, int pid, byte snak_tid, Json_nde nde) { case Wbase_claim_entity_.Tid__id: id = subValBry; break; // needed for sense and form
int len = nde.Len(); }
byte[] lat = null, lng = null, alt = null, prc = null, glb = null; }
for (int i = 0; i < len; ++i) { // TOMBSTONE:senses and forms do not have "numeric-id"; EX:wd:Lexeme:L2 and p6072 has a value of `{"entity-type":"form", "id":"L2-F3"}`; DATE:2020-07-27
Json_kv sub = Json_kv.cast(nde.Get_at(i)); // if (numericId == null) throw Err_.new_wo_type("pid is invalid entity", "pid", pid);
byte tid = Wbase_claim_globecoordinate_.Reg.Get_tid_or_max_and_log(qid, sub.Key().Data_bry()); if (tid == Byte_.Max_value_127) continue; return new Wbase_claim_entity(pid, snak_tid, entityType, numericId, id);
byte[] sub_val_bry = sub.Val().Data_bry(); }
switch (tid) { private Wbase_claim_monolingualtext Parse_datavalue_monolingualtext(byte[] qid, int pid, byte snak_tid, Json_nde nde) {
case Wbase_claim_globecoordinate_.Tid__latitude: lat = sub_val_bry; break; int len = nde.Len();
case Wbase_claim_globecoordinate_.Tid__longitude: lng = sub_val_bry; break; byte[] lang = null, text = null;
case Wbase_claim_globecoordinate_.Tid__altitude: alt = sub_val_bry; break; for (int i = 0; i < len; ++i) {
case Wbase_claim_globecoordinate_.Tid__precision: prc = sub_val_bry; break; Json_kv sub = Json_kv.cast(nde.Get_at(i));
case Wbase_claim_globecoordinate_.Tid__globe: glb = sub_val_bry; break; byte tid = Wbase_claim_monolingualtext_.Reg.Get_tid_or_max_and_log(qid, sub.Key().Data_bry()); if (tid == Byte_.Max_value_127) continue;
} byte[] sub_val_bry = sub.Val().Data_bry();
} switch (tid) {
if (lat == null || lng == null) throw Err_.new_wo_type("pid is invalid globecoordinate", "pid", pid); case Wbase_claim_monolingualtext_.Tid__text: text = sub_val_bry; break;
return new Wbase_claim_globecoordinate(pid, snak_tid, lat, lng, alt, prc, glb); case Wbase_claim_monolingualtext_.Tid__language: lang = sub_val_bry; break;
} }
private Wbase_claim_quantity Parse_datavalue_quantity(byte[] qid, int pid, byte snak_tid, Json_nde nde) { }
int len = nde.Len(); if (lang == null || text == null) throw Err_.new_wo_type("pid is invalid monolingualtext", "pid", pid);
byte[] amount = null, unit = null, ubound = null, lbound = null; return new Wbase_claim_monolingualtext(pid, snak_tid, lang, text);
for (int i = 0; i < len; ++i) { }
Json_kv sub = Json_kv.cast(nde.Get_at(i)); private Wbase_claim_globecoordinate Parse_datavalue_globecoordinate(byte[] qid, int pid, byte snak_tid, Json_nde nde) {
byte tid = Wbase_claim_quantity_.Reg.Get_tid_or_max_and_log(qid, sub.Key().Data_bry()); if (tid == Byte_.Max_value_127) continue; int len = nde.Len();
byte[] sub_val_bry = sub.Val().Data_bry(); byte[] lat = null, lng = null, alt = null, prc = null, glb = null;
switch (tid) { for (int i = 0; i < len; ++i) {
case Wbase_claim_quantity_.Tid__amount: amount = sub_val_bry; break; Json_kv sub = Json_kv.cast(nde.Get_at(i));
case Wbase_claim_quantity_.Tid__unit: unit = sub_val_bry; break; byte tid = Wbase_claim_globecoordinate_.Reg.Get_tid_or_max_and_log(qid, sub.Key().Data_bry()); if (tid == Byte_.Max_value_127) continue;
case Wbase_claim_quantity_.Tid__upperbound: ubound = sub_val_bry; break; byte[] sub_val_bry = sub.Val().Data_bry();
case Wbase_claim_quantity_.Tid__lowerbound: lbound = sub_val_bry; break; switch (tid) {
} case Wbase_claim_globecoordinate_.Tid__latitude: lat = sub_val_bry; break;
} case Wbase_claim_globecoordinate_.Tid__longitude: lng = sub_val_bry; break;
if (amount == null) throw Err_.new_wo_type("pid is invalid quantity", "pid", pid); case Wbase_claim_globecoordinate_.Tid__altitude: alt = sub_val_bry; break;
return new Wbase_claim_quantity(pid, snak_tid, amount, unit, ubound, lbound); case Wbase_claim_globecoordinate_.Tid__precision: prc = sub_val_bry; break;
} case Wbase_claim_globecoordinate_.Tid__globe: glb = sub_val_bry; break;
private Wbase_claim_time Parse_datavalue_time(byte[] qid, int pid, byte snak_tid, Json_nde nde) { }
int len = nde.Len(); }
byte[] time = null, timezone = null, before = null, after = null, precision = null, calendarmodel = null; if (lat == null || lng == null) throw Err_.new_wo_type("pid is invalid globecoordinate", "pid", pid);
for (int i = 0; i < len; ++i) { return new Wbase_claim_globecoordinate(pid, snak_tid, lat, lng, alt, prc, glb);
Json_kv sub = Json_kv.cast(nde.Get_at(i)); }
byte tid = Wbase_claim_time_.Reg.Get_tid_or_max_and_log(qid, sub.Key().Data_bry()); if (tid == Byte_.Max_value_127) continue; private Wbase_claim_quantity Parse_datavalue_quantity(byte[] qid, int pid, byte snak_tid, Json_nde nde) {
byte[] sub_val_bry = sub.Val().Data_bry(); int len = nde.Len();
switch (tid) { byte[] amount = null, unit = null, ubound = null, lbound = null;
case Wbase_claim_time_.Tid__time: time = sub_val_bry; break; for (int i = 0; i < len; ++i) {
case Wbase_claim_time_.Tid__timezone: timezone = sub_val_bry; break; Json_kv sub = Json_kv.cast(nde.Get_at(i));
case Wbase_claim_time_.Tid__before: before = sub_val_bry; break; byte tid = Wbase_claim_quantity_.Reg.Get_tid_or_max_and_log(qid, sub.Key().Data_bry()); if (tid == Byte_.Max_value_127) continue;
case Wbase_claim_time_.Tid__after: after = sub_val_bry; break; byte[] sub_val_bry = sub.Val().Data_bry();
case Wbase_claim_time_.Tid__precision: precision = sub_val_bry; break; switch (tid) {
case Wbase_claim_time_.Tid__calendarmodel: calendarmodel = sub_val_bry; break; case Wbase_claim_quantity_.Tid__amount: amount = sub_val_bry; break;
} case Wbase_claim_quantity_.Tid__unit: unit = sub_val_bry; break;
} case Wbase_claim_quantity_.Tid__upperbound: ubound = sub_val_bry; break;
if (time == null) throw Err_.new_wo_type("pid is invalid time", "pid", pid); case Wbase_claim_quantity_.Tid__lowerbound: lbound = sub_val_bry; break;
return new Wbase_claim_time(pid, snak_tid, time, timezone, before, after, precision, calendarmodel); }
} }
} if (amount == null) throw Err_.new_wo_type("pid is invalid quantity", "pid", pid);
return new Wbase_claim_quantity(pid, snak_tid, amount, unit, ubound, lbound);
}
private Wbase_claim_time Parse_datavalue_time(byte[] qid, int pid, byte snak_tid, Json_nde nde) {
int len = nde.Len();
byte[] time = null, timezone = null, before = null, after = null, precision = null, calendarmodel = null;
for (int i = 0; i < len; ++i) {
Json_kv sub = Json_kv.cast(nde.Get_at(i));
byte tid = Wbase_claim_time_.Reg.Get_tid_or_max_and_log(qid, sub.Key().Data_bry()); if (tid == Byte_.Max_value_127) continue;
byte[] sub_val_bry = sub.Val().Data_bry();
switch (tid) {
case Wbase_claim_time_.Tid__time: time = sub_val_bry; break;
case Wbase_claim_time_.Tid__timezone: timezone = sub_val_bry; break;
case Wbase_claim_time_.Tid__before: before = sub_val_bry; break;
case Wbase_claim_time_.Tid__after: after = sub_val_bry; break;
case Wbase_claim_time_.Tid__precision: precision = sub_val_bry; break;
case Wbase_claim_time_.Tid__calendarmodel: calendarmodel = sub_val_bry; break;
}
}
if (time == null) throw Err_.new_wo_type("pid is invalid time", "pid", pid);
return new Wbase_claim_time(pid, snak_tid, time, timezone, before, after, precision, calendarmodel);
}
}

@ -1,6 +1,6 @@
/* /*
XOWA: the XOWA Offline Wiki Application XOWA: the XOWA Offline Wiki Application
Copyright (C) 2012-2017 gnosygnu@gmail.com Copyright (C) 2012-2020 gnosygnu@gmail.com
XOWA is licensed under the terms of the General Public License (GPL) Version 3, XOWA is licensed under the terms of the General Public License (GPL) Version 3,
or alternatively under the terms of the Apache License Version 2.0. or alternatively under the terms of the Apache License Version 2.0.
@ -13,88 +13,126 @@ The terms of each license can be found in the source code repository:
GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
*/ */
package gplx.xowa.xtns.wbases.parsers; import gplx.*; import gplx.xowa.*; import gplx.xowa.xtns.*; import gplx.xowa.xtns.wbases.*; package gplx.xowa.xtns.wbases.parsers;
import gplx.langs.jsons.*; import gplx.xowa.xtns.wbases.core.*; import gplx.xowa.xtns.wbases.claims.*; import gplx.xowa.xtns.wbases.claims.enums.*; import gplx.xowa.xtns.wbases.claims.itms.*;
abstract class Wdata_doc_parser_fxt_base { import gplx.Bool_;
protected Wdata_doc_parser wdoc_parser; import gplx.Bry_;
private final Json_parser json_parser = new Json_parser(); import gplx.Bry_bfr;
private final Bry_bfr tmp_time_bfr = Bry_bfr_.New(); import gplx.Bry_bfr_;
public void Init() { import gplx.Int_;
if (wdoc_parser == null) wdoc_parser = Make_parser(); import gplx.List_adp;
} import gplx.List_adp_;
public abstract Wdata_doc_parser Make_parser(); import gplx.Object_;
public Wdata_sitelink_itm Make_sitelink(String site, String name, String... badges) {return new Wdata_sitelink_itm(Bry_.new_u8(site), Bry_.new_u8(name), Bry_.Ary(badges));} import gplx.Ordered_hash;
public Wdata_langtext_itm Make_langval(String lang, String text) {return new Wdata_langtext_itm(Bry_.new_u8(lang), Bry_.new_u8(text));} import gplx.String_;
public Wdata_alias_itm Make_alias(String lang, String... vals) {return new Wdata_alias_itm(Bry_.new_u8(lang), Bry_.Ary(vals));} import gplx.Tfds;
public Wbase_claim_base Make_claim_string (int pid, String val) {return new Wbase_claim_string(pid, Wbase_claim_value_type_.Tid__value, Bry_.new_u8(val));} import gplx.langs.jsons.Json_ary;
public Wbase_claim_base Make_claim_entity_qid (int pid, int eid) {return new Wbase_claim_entity(pid, Wbase_claim_value_type_.Tid__value, Wbase_claim_entity_type_.Tid__item, Int_.To_bry(eid));} import gplx.langs.jsons.Json_doc;
public Wbase_claim_base Make_claim_entity_pid (int pid, int eid) {return new Wbase_claim_entity(pid, Wbase_claim_value_type_.Tid__value, Wbase_claim_entity_type_.Tid__property, Int_.To_bry(eid));} import gplx.langs.jsons.Json_kv;
public Wbase_claim_base Make_claim_monolingualtext (int pid, String lang, String text) {return new Wbase_claim_monolingualtext(pid, Wbase_claim_value_type_.Tid__value, Bry_.new_u8(lang), Bry_.new_u8(text));} import gplx.langs.jsons.Json_nde;
public Wbase_claim_base Make_claim_globecoordinate (int pid, String lat, String lng, String prc) {return new Wbase_claim_globecoordinate(pid, Wbase_claim_value_type_.Tid__value, Bry_.new_u8(lat), Bry_.new_u8(lng), Object_.Bry__null, Bry_.new_u8(prc), Bry_.new_a7("http://www.wikidata.org/entity/Q2"));} import gplx.langs.jsons.Json_parser;
public Wbase_claim_base Make_claim_quantity (int pid, int val, int unit, int ubound, int lbound) {return new Wbase_claim_quantity(pid, Wbase_claim_value_type_.Tid__value, Bry_.new_u8(Int_.To_str(val)), Bry_.new_u8(Int_.To_str(unit)), Bry_.new_u8(Int_.To_str(ubound)), Bry_.new_u8(Int_.To_str(lbound)));} import gplx.xowa.xtns.wbases.claims.Wbase_claim_grp;
public Wbase_claim_base Make_claim_time (int pid, String val) {return new Wbase_claim_time(pid, Wbase_claim_value_type_.Tid__value, Wbase_claim_time_.To_bry(tmp_time_bfr, val), Wbase_claim_time_.Dflt__timezone.Val_bry(), Wbase_claim_time_.Dflt__before.Val_bry(), Wbase_claim_time_.Dflt__after.Val_bry(), Wbase_claim_time_.Dflt__precision.Val_bry(), Wbase_claim_time_.Dflt__calendarmodel.Val_bry());} import gplx.xowa.xtns.wbases.claims.Wbase_claim_grp_list;
public Wbase_claim_base Make_claim_novalue (int pid) {return new Wbase_claim_value(pid, Wbase_claim_type_.Tid__unknown, Wbase_claim_value_type_.Tid__novalue);} import gplx.xowa.xtns.wbases.claims.Wbase_references_grp;
import gplx.xowa.xtns.wbases.claims.enums.Wbase_claim_entity_type_;
public void Test_entity(String raw, String expd) {Tfds.Eq(expd, String_.new_u8(wdoc_parser.Parse_qid(json_parser.Parse_by_apos(raw))));} import gplx.xowa.xtns.wbases.claims.enums.Wbase_claim_type_;
public void Test_sitelinks(String raw, Wdata_sitelink_itm... expd) { import gplx.xowa.xtns.wbases.claims.enums.Wbase_claim_value_type_;
Ordered_hash actl_hash = wdoc_parser.Parse_sitelinks(Q1_bry, json_parser.Parse_by_apos(raw)); import gplx.xowa.xtns.wbases.claims.itms.Wbase_claim_base;
Tfds.Eq_ary_str((Wdata_sitelink_itm[])actl_hash.To_ary(Wdata_sitelink_itm.class), expd); import gplx.xowa.xtns.wbases.claims.itms.Wbase_claim_entity;
} import gplx.xowa.xtns.wbases.claims.itms.Wbase_claim_globecoordinate;
public void Test_labels(String raw, Wdata_langtext_itm... expd) {Test_langvals(raw, Bool_.Y, expd);} import gplx.xowa.xtns.wbases.claims.itms.Wbase_claim_monolingualtext;
public void Test_descriptions(String raw, Wdata_langtext_itm... expd) {Test_langvals(raw, Bool_.N, expd);} import gplx.xowa.xtns.wbases.claims.itms.Wbase_claim_quantity;
private void Test_langvals(String raw, boolean labels_or_descriptions, Wdata_langtext_itm... expd) { import gplx.xowa.xtns.wbases.claims.itms.Wbase_claim_string;
Ordered_hash actl_hash = wdoc_parser.Parse_langvals(Q1_bry, json_parser.Parse_by_apos(raw), labels_or_descriptions); import gplx.xowa.xtns.wbases.claims.itms.Wbase_claim_time;
Tfds.Eq_ary_str((Wdata_langtext_itm[])actl_hash.To_ary(Wdata_langtext_itm.class), expd); import gplx.xowa.xtns.wbases.claims.itms.Wbase_claim_time_;
} import gplx.xowa.xtns.wbases.claims.itms.Wbase_claim_value;
public void Test_aliases(String raw, Wdata_alias_itm... expd) { import gplx.xowa.xtns.wbases.core.Wdata_alias_itm;
Ordered_hash actl_hash = wdoc_parser.Parse_aliases(Q1_bry, json_parser.Parse_by_apos(raw)); import gplx.xowa.xtns.wbases.core.Wdata_langtext_itm;
Tfds.Eq_ary_str((Wdata_alias_itm[])actl_hash.To_ary(Wdata_alias_itm.class), expd); import gplx.xowa.xtns.wbases.core.Wdata_sitelink_itm;
}
public void Test_claims(String raw, Wbase_claim_base... expd) { abstract class Wdata_doc_parser_fxt_base {
Ordered_hash actl_hash = wdoc_parser.Parse_claims(Q1_bry, json_parser.Parse_by_apos(raw)); protected Wdata_doc_parser wdoc_parser;
List_adp actl_list = Wbase_claim_grp.Xto_list(actl_hash); private final Json_parser json_parser = new Json_parser();
Tfds.Eq_ary_str((Wbase_claim_base[])actl_list.To_ary(Wbase_claim_base.class), expd); private final Bry_bfr tmp_time_bfr = Bry_bfr_.New();
} public void Init() {
public void Test_claims_data(String raw, Wbase_claim_base expd) { if (wdoc_parser == null) wdoc_parser = Make_parser();
Json_doc jdoc = json_parser.Parse_by_apos(raw); }
Wbase_claim_base actl = wdoc_parser.Parse_claims_data(Q1_bry, 1, Wbase_claim_value_type_.Tid__value, jdoc.Root_nde()); public abstract Wdata_doc_parser Make_parser();
Tfds.Eq(expd.toString(), actl.toString()); public Wdata_sitelink_itm Make_sitelink(String site, String name, String... badges) {return new Wdata_sitelink_itm(Bry_.new_u8(site), Bry_.new_u8(name), Bry_.Ary(badges));}
} public Wdata_langtext_itm Make_langval(String lang, String text) {return new Wdata_langtext_itm(Bry_.new_u8(lang), Bry_.new_u8(text));}
public void Test_qualifiers(String raw, Wbase_claim_base... expd_itms) { public Wdata_alias_itm Make_alias(String lang, String... vals) {return new Wdata_alias_itm(Bry_.new_u8(lang), Bry_.Ary(vals));}
Json_doc jdoc = json_parser.Parse_by_apos(raw); public Wbase_claim_base Make_claim_string (int pid, String val) {return new Wbase_claim_string(pid, Wbase_claim_value_type_.Tid__value, Bry_.new_u8(val));}
Json_nde qualifiers_nde = Json_nde.cast(Json_kv.cast(jdoc.Root_nde().Get_at(0)).Val()); public Wbase_claim_base Make_claim_entity_qid (int pid, int eid) {return new Wbase_claim_entity(pid, Wbase_claim_value_type_.Tid__value, Wbase_claim_entity_type_.Tid__item, Int_.To_bry(eid));}
Wbase_claim_grp_list actl = wdoc_parser.Parse_qualifiers(Q1_bry, qualifiers_nde); public Wbase_claim_base Make_claim_entity_pid (int pid, int eid) {return new Wbase_claim_entity(pid, Wbase_claim_value_type_.Tid__value, Wbase_claim_entity_type_.Tid__property, Int_.To_bry(eid));}
Tfds.Eq_ary_str(expd_itms, To_ary(actl)); public Wbase_claim_base Make_claim_entity_lid (int pid, int eid) {return new Wbase_claim_entity(pid, Wbase_claim_value_type_.Tid__value, Wbase_claim_entity_type_.Tid__lexeme, Int_.To_bry(eid));}
} public Wbase_claim_base Make_claim_entity_fid (int pid, String id) {return new Wbase_claim_entity(pid, Wbase_claim_value_type_.Tid__value, Wbase_claim_entity_type_.Tid__form, null, Bry_.new_u8(id));}
public void Test_references(String raw, int[] expd_order, Wbase_claim_base... expd_itms) { public Wbase_claim_base Make_claim_entity_sid (int pid, String id) {return new Wbase_claim_entity(pid, Wbase_claim_value_type_.Tid__value, Wbase_claim_entity_type_.Tid__sense, null, Bry_.new_u8(id));}
Json_doc jdoc = json_parser.Parse_by_apos(raw); public Wbase_claim_base Make_claim_monolingualtext (int pid, String lang, String text) {return new Wbase_claim_monolingualtext(pid, Wbase_claim_value_type_.Tid__value, Bry_.new_u8(lang), Bry_.new_u8(text));}
Json_ary owner = Json_ary.cast_or_null(Json_kv.cast(jdoc.Root_nde().Get_at(0)).Val()); public Wbase_claim_base Make_claim_globecoordinate (int pid, String lat, String lng, String prc) {return new Wbase_claim_globecoordinate(pid, Wbase_claim_value_type_.Tid__value, Bry_.new_u8(lat), Bry_.new_u8(lng), Object_.Bry__null, Bry_.new_u8(prc), Bry_.new_a7("http://www.wikidata.org/entity/Q2"));}
Wbase_references_grp[] actl = wdoc_parser.Parse_references(Q1_bry, owner); public Wbase_claim_base Make_claim_quantity (int pid, int val, int unit, int ubound, int lbound) {return new Wbase_claim_quantity(pid, Wbase_claim_value_type_.Tid__value, Bry_.new_u8(Int_.To_str(val)), Bry_.new_u8(Int_.To_str(unit)), Bry_.new_u8(Int_.To_str(ubound)), Bry_.new_u8(Int_.To_str(lbound)));}
Wbase_references_grp actl_grp = actl[0]; public Wbase_claim_base Make_claim_time (int pid, String val) {return new Wbase_claim_time(pid, Wbase_claim_value_type_.Tid__value, Wbase_claim_time_.To_bry(tmp_time_bfr, val), Wbase_claim_time_.Dflt__timezone.Val_bry(), Wbase_claim_time_.Dflt__before.Val_bry(), Wbase_claim_time_.Dflt__after.Val_bry(), Wbase_claim_time_.Dflt__precision.Val_bry(), Wbase_claim_time_.Dflt__calendarmodel.Val_bry());}
Tfds.Eq_ary(expd_order, actl_grp.Snaks_order()); public Wbase_claim_base Make_claim_novalue (int pid) {return new Wbase_claim_value(pid, Wbase_claim_type_.Tid__unknown, Wbase_claim_value_type_.Tid__novalue);}
Tfds.Eq_ary_str(expd_itms, To_ary(actl_grp.Snaks()));
} public void Test_entity(String raw, String expd) {Tfds.Eq(expd, String_.new_u8(wdoc_parser.Parse_qid(json_parser.Parse_by_apos(raw))));}
public void Test_pid_order(String raw, int... expd) { public void Test_sitelinks(String raw, Wdata_sitelink_itm... expd) {
Json_doc jdoc = json_parser.Parse_by_apos(raw); Ordered_hash actl_hash = wdoc_parser.Parse_sitelinks(Q1_bry, json_parser.Parse_by_apos(raw));
Json_ary nde = Json_ary.cast_or_null(Json_kv.cast(jdoc.Root_nde().Get_at(0)).Val()); Tfds.Eq_ary_str(actl_hash.To_ary(Wdata_sitelink_itm.class), expd);
int[] actl = wdoc_parser.Parse_pid_order(Q1_bry, nde); }
Tfds.Eq_ary(expd, actl); public void Test_labels(String raw, Wdata_langtext_itm... expd) {Test_langvals(raw, Bool_.Y, expd);}
} public void Test_descriptions(String raw, Wdata_langtext_itm... expd) {Test_langvals(raw, Bool_.N, expd);}
Wbase_claim_base[] To_ary(Wbase_claim_grp_list list) { private void Test_langvals(String raw, boolean labels_or_descriptions, Wdata_langtext_itm... expd) {
List_adp rv = List_adp_.New(); Ordered_hash actl_hash = wdoc_parser.Parse_langvals(Q1_bry, json_parser.Parse_by_apos(raw), labels_or_descriptions);
int list_len = list.Len(); Tfds.Eq_ary_str(actl_hash.To_ary(Wdata_langtext_itm.class), expd);
for (int i = 0; i < list_len; ++i) { }
Wbase_claim_grp grp = list.Get_at(i); public void Test_aliases(String raw, Wdata_alias_itm... expd) {
int grp_len = grp.Len(); Ordered_hash actl_hash = wdoc_parser.Parse_aliases(Q1_bry, json_parser.Parse_by_apos(raw));
for (int j = 0; j < grp_len; ++j) { Tfds.Eq_ary_str(actl_hash.To_ary(Wdata_alias_itm.class), expd);
Wbase_claim_base itm = grp.Get_at(j); }
rv.Add(itm); public void Test_claims(String raw, Wbase_claim_base... expd) {
} Ordered_hash actl_hash = wdoc_parser.Parse_claims(Q1_bry, json_parser.Parse_by_apos(raw));
} List_adp actl_list = Wbase_claim_grp.Xto_list(actl_hash);
return (Wbase_claim_base[])rv.To_ary_and_clear(Wbase_claim_base.class); Tfds.Eq_ary_str(actl_list.To_ary(Wbase_claim_base.class), expd);
} }
private static final byte[] Q1_bry = Bry_.new_a7("Q1"); public void Test_claims_data(String raw, Wbase_claim_base expd) {
} Json_doc jdoc = json_parser.Parse_by_apos(raw);
class Wdata_doc_parser_v2_fxt extends Wdata_doc_parser_fxt_base { Wbase_claim_base actl = wdoc_parser.Parse_claims_data(Q1_bry, 1, Wbase_claim_value_type_.Tid__value, jdoc.Root_nde());
@Override public Wdata_doc_parser Make_parser() {return new Wdata_doc_parser_v2();} Tfds.Eq(expd.toString(), actl.toString());
} }
public void Test_qualifiers(String raw, Wbase_claim_base... expd_itms) {
Json_doc jdoc = json_parser.Parse_by_apos(raw);
Json_nde qualifiers_nde = Json_nde.cast(Json_kv.cast(jdoc.Root_nde().Get_at(0)).Val());
Wbase_claim_grp_list actl = wdoc_parser.Parse_qualifiers(Q1_bry, qualifiers_nde);
Tfds.Eq_ary_str(expd_itms, To_ary(actl));
}
public void Test_references(String raw, int[] expd_order, Wbase_claim_base... expd_itms) {
Json_doc jdoc = json_parser.Parse_by_apos(raw);
Json_ary owner = Json_ary.cast_or_null(Json_kv.cast(jdoc.Root_nde().Get_at(0)).Val());
Wbase_references_grp[] actl = wdoc_parser.Parse_references(Q1_bry, owner);
Wbase_references_grp actl_grp = actl[0];
Tfds.Eq_ary(expd_order, actl_grp.Snaks_order());
Tfds.Eq_ary_str(expd_itms, To_ary(actl_grp.Snaks()));
}
public void Test_pid_order(String raw, int... expd) {
Json_doc jdoc = json_parser.Parse_by_apos(raw);
Json_ary nde = Json_ary.cast_or_null(Json_kv.cast(jdoc.Root_nde().Get_at(0)).Val());
int[] actl = wdoc_parser.Parse_pid_order(Q1_bry, nde);
Tfds.Eq_ary(expd, actl);
}
Wbase_claim_base[] To_ary(Wbase_claim_grp_list list) {
List_adp rv = List_adp_.New();
int list_len = list.Len();
for (int i = 0; i < list_len; ++i) {
Wbase_claim_grp grp = list.Get_at(i);
int grp_len = grp.Len();
for (int j = 0; j < grp_len; ++j) {
Wbase_claim_base itm = grp.Get_at(j);
rv.Add(itm);
}
}
return (Wbase_claim_base[])rv.To_ary_and_clear(Wbase_claim_base.class);
}
private static final byte[] Q1_bry = Bry_.new_a7("Q1");
}
class Wdata_doc_parser_v2_fxt extends Wdata_doc_parser_fxt_base {
@Override public Wdata_doc_parser Make_parser() {return new Wdata_doc_parser_v2();}
}

@ -1,6 +1,6 @@
/* /*
XOWA: the XOWA Offline Wiki Application XOWA: the XOWA Offline Wiki Application
Copyright (C) 2012-2017 gnosygnu@gmail.com Copyright (C) 2012-2020 gnosygnu@gmail.com
XOWA is licensed under the terms of the General Public License (GPL) Version 3, XOWA is licensed under the terms of the General Public License (GPL) Version 3,
or alternatively under the terms of the Apache License Version 2.0. or alternatively under the terms of the Apache License Version 2.0.
@ -13,179 +13,218 @@ The terms of each license can be found in the source code repository:
GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
*/ */
package gplx.xowa.xtns.wbases.parsers; import gplx.*; import gplx.xowa.*; import gplx.xowa.xtns.*; import gplx.xowa.xtns.wbases.*; package gplx.xowa.xtns.wbases.parsers;
import org.junit.*;
import gplx.langs.jsons.*; import gplx.xowa.xtns.wbases.core.*; import gplx.String_;
public class Wdata_doc_parser_v2__claims__tst { import org.junit.Before;
@Before public void init() {fxt.Init();} private Wdata_doc_parser_v2_fxt fxt = new Wdata_doc_parser_v2_fxt(); import org.junit.Test;
@Test public void Full__string() {
fxt.Test_claims(String_.Concat_lines_nl_skip_last public class Wdata_doc_parser_v2__claims__tst {
( "{ 'claims':" @Before public void init() {fxt.Init();} private Wdata_doc_parser_v2_fxt fxt = new Wdata_doc_parser_v2_fxt();
, " { 'P1':" @Test public void Full__string() {
, " [" fxt.Test_claims(String_.Concat_lines_nl_skip_last
, " { 'mainsnak':" ( "{ 'claims':"
, " { 'snaktype':'value'" , " { 'P1':"
, " , 'property':'P1'" , " ["
, " , 'hash':'84487fc3f93b4f74ab1cc5a47d78f596f0b49390'" , " { 'mainsnak':"
, " , 'datavalue':" , " { 'snaktype':'value'"
, " { 'value':'abc'" , " , 'property':'P1'"
, " , 'type':'string'" , " , 'hash':'84487fc3f93b4f74ab1cc5a47d78f596f0b49390'"
, " }" , " , 'datavalue':"
, " }" , " { 'value':'abc'"
, " , 'type':'statement'" , " , 'type':'string'"
, " , 'id':'Q2$e8ba1188-4aec-9e37-a75e-f79466c1913e'" , " }"
, " , 'rank':'normal'" , " }"
, " }" , " , 'type':'statement'"
, " ]" , " , 'id':'Q2$e8ba1188-4aec-9e37-a75e-f79466c1913e'"
, " }" , " , 'rank':'normal'"
, "}" , " }"
) , " ]"
, fxt.Make_claim_string(1, "abc") , " }"
); , "}"
} )
@Test public void Full__novalue() { , fxt.Make_claim_string(1, "abc")
fxt.Test_claims(String_.Concat_lines_nl_skip_last );
( "{ 'claims':" }
, " { 'P1':" @Test public void Full__novalue() {
, " [" fxt.Test_claims(String_.Concat_lines_nl_skip_last
, " { 'mainsnak':" ( "{ 'claims':"
, " { 'snaktype':'novalue'" , " { 'P1':"
, " , 'property':'P1'" , " ["
, " , 'hash':'84487fc3f93b4f74ab1cc5a47d78f596f0b49390'" , " { 'mainsnak':"
, " }" , " { 'snaktype':'novalue'"
, " }" , " , 'property':'P1'"
, " ]" , " , 'hash':'84487fc3f93b4f74ab1cc5a47d78f596f0b49390'"
, " }" , " }"
, "}" , " }"
) , " ]"
, fxt.Make_claim_novalue(1) , " }"
); , "}"
} )
@Test public void Data__string() { , fxt.Make_claim_novalue(1)
fxt.Test_claims_data(String_.Concat_lines_nl_skip_last );
( "{ 'value':'abc'" }
, ", 'type':'string'" @Test public void Data__string() {
, "}" fxt.Test_claims_data(String_.Concat_lines_nl_skip_last
) ( "{ 'value':'abc'"
, fxt.Make_claim_string(1, "abc") , ", 'type':'string'"
); , "}"
} )
@Test public void Data__item() { , fxt.Make_claim_string(1, "abc")
fxt.Test_claims_data(String_.Concat_lines_nl_skip_last );
( "{ 'value':" }
, " { 'entity-type':'item'" @Test public void Data__item() {
, " , 'numeric-id':'123'" fxt.Test_claims_data(String_.Concat_lines_nl_skip_last
, " }" ( "{ 'value':"
, ", 'type':'wikibase-entityid'" , " { 'entity-type':'item'"
, "}" , " , 'numeric-id':'123'"
) , " }"
, fxt.Make_claim_entity_qid(1, 123) , ", 'type':'wikibase-entityid'"
); , "}"
} )
@Test public void Data__property() { , fxt.Make_claim_entity_qid(1, 123)
fxt.Test_claims_data(String_.Concat_lines_nl_skip_last );
( "{ 'value':" }
, " { 'entity-type':'property'" @Test public void Data__property() {
, " , 'numeric-id':'398'" fxt.Test_claims_data(String_.Concat_lines_nl_skip_last
, " }" ( "{ 'value':"
, ", 'type':'wikibase-entityid'" , " { 'entity-type':'property'"
, "}" , " , 'numeric-id':'398'"
) , " }"
, fxt.Make_claim_entity_pid(1, 398) , ", 'type':'wikibase-entityid'"
); , "}"
} )
@Test public void Data__monolingualtext() { , fxt.Make_claim_entity_pid(1, 398)
fxt.Test_claims_data(String_.Concat_lines_nl_skip_last );
( "{ 'value':" }
, " { 'text':'en_text'" @Test public void Data__monolingualtext() {
, " , 'language':'en'" fxt.Test_claims_data(String_.Concat_lines_nl_skip_last
, " }" ( "{ 'value':"
, ", 'type':'monolingualtext'" , " { 'text':'en_text'"
, "}" , " , 'language':'en'"
) , " }"
, fxt.Make_claim_monolingualtext(1, "en", "en_text") , ", 'type':'monolingualtext'"
); , "}"
} )
@Test public void Data__globecoordinate() { , fxt.Make_claim_monolingualtext(1, "en", "en_text")
fxt.Test_claims_data(String_.Concat_lines_nl_skip_last );
( "{ 'value':" }
, " { 'latitude':1.2" @Test public void Data__globecoordinate() {
, " , 'longitude':3.4" fxt.Test_claims_data(String_.Concat_lines_nl_skip_last
, " , 'altitude':null" ( "{ 'value':"
, " , 'precision':0.0002" , " { 'latitude':1.2"
, " , 'globe':'http:\\/\\/www.wikidata.org\\/entity\\/Q2'" , " , 'longitude':3.4"
, " }" , " , 'altitude':null"
, ", 'type':'globecoordinate'" , " , 'precision':0.0002"
, "}" , " , 'globe':'http:\\/\\/www.wikidata.org\\/entity\\/Q2'"
) , " }"
, fxt.Make_claim_globecoordinate(1, "1.2", "3.4", "0.0002") , ", 'type':'globecoordinate'"
); , "}"
} )
@Test public void Data__quantity() { , fxt.Make_claim_globecoordinate(1, "1.2", "3.4", "0.0002")
fxt.Test_claims_data(String_.Concat_lines_nl_skip_last );
( "{ 'value':" }
, " { 'amount':'123'" @Test public void Data__quantity() {
, " , 'unit':'2'" fxt.Test_claims_data(String_.Concat_lines_nl_skip_last
, " , 'upperBound':'125'" ( "{ 'value':"
, " , 'lowerBound':'121'" , " { 'amount':'123'"
, " }" , " , 'unit':'2'"
, ", 'type':'quantity'" , " , 'upperBound':'125'"
, "}" , " , 'lowerBound':'121'"
) , " }"
, fxt.Make_claim_quantity(1, 123, 2, 125, 121) , ", 'type':'quantity'"
); , "}"
} )
@Test public void Data__time() { , fxt.Make_claim_quantity(1, 123, 2, 125, 121)
fxt.Test_claims_data(String_.Concat_lines_nl_skip_last );
( "{ 'value':" }
, " { 'time':'+00000002001-02-03T04:05:06Z'" @Test public void Data__time() {
, " , 'timezone':0" fxt.Test_claims_data(String_.Concat_lines_nl_skip_last
, " , 'before':0" ( "{ 'value':"
, " , 'after':0" , " { 'time':'+00000002001-02-03T04:05:06Z'"
, " , 'precision':11" , " , 'timezone':0"
, " , 'calendarmodel':'http:\\/\\/www.wikidata.org\\/entity\\/Q1985727'" , " , 'before':0"
, " }" , " , 'after':0"
, ", 'type':'time'" , " , 'precision':11"
, "}" , " , 'calendarmodel':'http:\\/\\/www.wikidata.org\\/entity\\/Q1985727'"
) , " }"
, fxt.Make_claim_time(1, "2001-02-03 04:05:06") , ", 'type':'time'"
); , "}"
} )
@Test public void Data__url() { // NOTE:has "String" property-type; EX:wd:Q23548; DATE:2016-07-28 , fxt.Make_claim_time(1, "2001-02-03 04:05:06")
fxt.Test_claims_data(String_.Concat_lines_nl_skip_last );
( "{ 'value':'http:\\/\\/www.nasa.gov\\/rss\\/dyn\\/breaking_news.rss'" }
, ", 'type':'string'" @Test public void Data__url() { // NOTE:has "String" property-type; EX:wd:Q23548; DATE:2016-07-28
, "}" fxt.Test_claims_data(String_.Concat_lines_nl_skip_last
) ( "{ 'value':'http:\\/\\/www.nasa.gov\\/rss\\/dyn\\/breaking_news.rss'"
, fxt.Make_claim_string(1, "http://www.nasa.gov/rss/dyn/breaking_news.rss") , ", 'type':'string'"
); , "}"
} )
@Test public void Data__commonsMedia() { // NOTE:has "String" property-type; EX:wd:Q327162; DATE:2016-07-28 , fxt.Make_claim_string(1, "http://www.nasa.gov/rss/dyn/breaking_news.rss")
fxt.Test_claims_data(String_.Concat_lines_nl_skip_last );
( "{ 'value':'Tabliczka E40.svg'" }
, ", 'type':'string'" @Test public void Data__commonsMedia() { // NOTE:has "String" property-type; EX:wd:Q327162; DATE:2016-07-28
, "}" fxt.Test_claims_data(String_.Concat_lines_nl_skip_last
) ( "{ 'value':'Tabliczka E40.svg'"
, fxt.Make_claim_string(1, "Tabliczka E40.svg") , ", 'type':'string'"
); , "}"
} )
@Test public void Data__externalid() { // NOTE:has "String" property-type; EX:wd:Q77177; DATE:2016-07-28 , fxt.Make_claim_string(1, "Tabliczka E40.svg")
fxt.Test_claims_data(String_.Concat_lines_nl_skip_last );
( "{ 'value':'000331371'" }
, ", 'type':'string'" @Test public void Data__externalid() { // NOTE:has "String" property-type; EX:wd:Q77177; DATE:2016-07-28
, "}" fxt.Test_claims_data(String_.Concat_lines_nl_skip_last
) ( "{ 'value':'000331371'"
, fxt.Make_claim_string(1, "000331371") , ", 'type':'string'"
); , "}"
} )
@Test public void Data__math() { // NOTE:has "String" property-type; EX:wd:Q11518; DATE:2016-07-28 , fxt.Make_claim_string(1, "000331371")
fxt.Test_claims_data(String_.Concat_lines_nl_skip_last );
( "{ 'value':'a^2+b^2=c^2'" }
, ", 'type':'string'" @Test public void Data__math() { // NOTE:has "String" property-type; EX:wd:Q11518; DATE:2016-07-28
, "}" fxt.Test_claims_data(String_.Concat_lines_nl_skip_last
) ( "{ 'value':'a^2+b^2=c^2'"
, fxt.Make_claim_string(1, "a^2+b^2=c^2") , ", 'type':'string'"
); , "}"
} )
// www.wikidata.org/wiki/Q11518 , fxt.Make_claim_string(1, "a^2+b^2=c^2")
} );
}
@Test public void ValueLexeme() {// EX:wd:L2 P46028; DATE:2020-07-28
fxt.Test_claims_data(String_.Concat_lines_nl_skip_last
( "{ 'value':"
, " { 'entity-type':'lexeme'"
, " , 'numeric-id':'123'"
, " , 'id':'L123'"
, " }"
, ", 'type':'wikibase-entityid'"
, "}"
)
, fxt.Make_claim_entity_lid(1, 123)
);
}
@Test public void ValueForm() {// EX:wd:L2 L2-F3; DATE:2020-07-28
fxt.Test_claims_data(String_.Concat_lines_nl_skip_last
( "{ 'value':"
, " { 'entity-type':'form'"
, " , 'id':'L2-F3'"
, " }"
, ", 'type':'wikibase-entityid'"
, "}"
)
, fxt.Make_claim_entity_fid(1, "L2-F3")
);
}
@Test public void ValueSense() {// EX:wd:L2 L2-S1; DATE:2020-07-28
fxt.Test_claims_data(String_.Concat_lines_nl_skip_last
( "{ 'value':"
, " { 'entity-type':'sense'"
, " , 'id':'L2-S1'"
, " }"
, ", 'type':'wikibase-entityid'"
, "}"
)
, fxt.Make_claim_entity_sid(1, "L2-S1")
);
}
}

Loading…
Cancel
Save