mirror of
https://github.com/gnosygnu/xowa.git
synced 2026-03-02 03:49:30 +00:00
Wikibase: Infer datatype from cached table not from JSON doc
This commit is contained in:
@@ -69,7 +69,8 @@ public class Scrib_lib_wikibase implements Scrib_lib {
|
||||
}
|
||||
public boolean GetEntity(Scrib_proc_args args, Scrib_proc_rslt rslt) {
|
||||
Wdata_doc wdoc = Get_wdoc_or_null(args, core); if (wdoc == null) return rslt.Init_ary_empty();
|
||||
return rslt.Init_obj(Scrib_lib_wikibase_srl.Srl(wdoc, true, false)); // "false": wbase now always uses v2; PAGE:ja.w:東京競馬場; DATE:2015-07-28
|
||||
Wbase_prop_mgr prop_mgr = core.Wiki().Appe().Wiki_mgr().Wdata_mgr().Prop_mgr();
|
||||
return rslt.Init_obj(Scrib_lib_wikibase_srl.Srl(prop_mgr, wdoc, true, false)); // "false": wbase now always uses v2; PAGE:ja.w:東京競馬場; DATE:2015-07-28
|
||||
}
|
||||
public boolean GetEntityUrl(Scrib_proc_args args, Scrib_proc_rslt rslt) {throw Err_.new_("wbase", "getEntityUrl not implemented", "url", core.Page().Url().To_str());}
|
||||
public boolean GetSetting(Scrib_proc_args args, Scrib_proc_rslt rslt) {throw Err_.new_("wbase", "getSetting not implemented", "url", core.Page().Url().To_str());}
|
||||
|
||||
@@ -18,7 +18,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
package gplx.xowa.xtns.scribunto.libs; import gplx.*; import gplx.xowa.*; import gplx.xowa.xtns.*; import gplx.xowa.xtns.scribunto.*;
|
||||
import gplx.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.*;
|
||||
class Scrib_lib_wikibase_srl {
|
||||
public static Keyval[] Srl(Wdata_doc wdoc, boolean header_enabled, boolean legacy_style) {// REF.MW:/Wikibase/lib/includes/serializers/EntitySerializer.php!getSerialized; http://www.mediawiki.org/wiki/Extension:Wikibase_Client/Lua
|
||||
public static Keyval[] Srl(Wbase_prop_mgr prop_mgr, Wdata_doc wdoc, boolean header_enabled, boolean legacy_style) {// REF.MW:/Wikibase/lib/includes/serializers/EntitySerializer.php!getSerialized; http://www.mediawiki.org/wiki/Extension:Wikibase_Client/Lua
|
||||
int base_adj = legacy_style ? 0 : 1;
|
||||
List_adp rv = List_adp_.New();
|
||||
if (header_enabled) {
|
||||
@@ -32,7 +32,7 @@ class Scrib_lib_wikibase_srl {
|
||||
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()));
|
||||
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()));
|
||||
Srl_root(rv, Wdata_doc_parser_v2.Str_claims , Srl_claims (base_adj, legacy_style, wdoc.Claim_list()));
|
||||
Srl_root(rv, Wdata_doc_parser_v2.Str_claims , Srl_claims (base_adj, legacy_style, prop_mgr, wdoc.Claim_list()));
|
||||
return (Keyval[])rv.To_ary(Keyval.class);
|
||||
}
|
||||
private static void Srl_root(List_adp rv, String label, Keyval[] ary) {
|
||||
@@ -90,7 +90,7 @@ class Scrib_lib_wikibase_srl {
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
private static Keyval[] Srl_claims(int base_adj, boolean legacy_style, Ordered_hash claim_grps) {
|
||||
private static Keyval[] Srl_claims(int base_adj, boolean legacy_style, Wbase_prop_mgr prop_mgr, Ordered_hash claim_grps) {
|
||||
int len = claim_grps.Count(); if (len == 0) return null;
|
||||
Scrib_lib_wikibase_srl_visitor visitor = new Scrib_lib_wikibase_srl_visitor();
|
||||
int rv_len = legacy_style ? len * 2 : len; // NOTE: legacyStyle returns 2 sets of properties: official "P" and legacy "p"; DATE:2014-05-11
|
||||
@@ -98,32 +98,32 @@ class Scrib_lib_wikibase_srl {
|
||||
for (int i = 0; i < len; i++) {
|
||||
Wbase_claim_grp grp = (Wbase_claim_grp)claim_grps.Get_at(i);
|
||||
String pid_str = Int_.To_str(grp.Id());
|
||||
Keyval[] grp_val = Srl_claims_prop_grp(visitor, "P" + pid_str, grp, base_adj);
|
||||
Keyval[] grp_val = Srl_claims_prop_grp(prop_mgr, visitor, "P" + pid_str, grp, base_adj);
|
||||
rv[i] = Keyval_.new_("P" + pid_str, grp_val);
|
||||
if (legacy_style)
|
||||
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
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
private static Keyval[] Srl_claims_prop_grp(Scrib_lib_wikibase_srl_visitor visitor, String pid, Wbase_claim_grp grp, int base_adj) {
|
||||
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) {
|
||||
int len = grp.Len();
|
||||
Keyval[] rv = new Keyval[len];
|
||||
for (int i = 0; i < len; i++) {
|
||||
Wbase_claim_base itm = grp.Get_at(i);
|
||||
rv[i] = Keyval_.int_(i + base_adj, Srl_claims_prop_itm(visitor, pid, itm, base_adj)); // NOTE: must be super 0 or super 1; DATE:2014-05-09
|
||||
rv[i] = Keyval_.int_(i + base_adj, Srl_claims_prop_itm(prop_mgr, visitor, pid, itm, base_adj)); // NOTE: must be super 0 or super 1; DATE:2014-05-09
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
private static Keyval[] Srl_claims_prop_itm(Scrib_lib_wikibase_srl_visitor visitor, String pid, Wbase_claim_base itm, int base_adj) {
|
||||
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) {
|
||||
List_adp list = List_adp_.New();
|
||||
list.Add(Keyval_.new_("id", pid));
|
||||
list.Add(Keyval_.new_("mainsnak", Srl_claims_prop_itm_core(visitor, pid, itm)));
|
||||
list.Add(Keyval_.new_("mainsnak", Srl_claims_prop_itm_core(prop_mgr, visitor, pid, itm)));
|
||||
list.Add(Keyval_.new_(Wdata_dict_claim_v1.Str_rank, Wbase_claim_rank_.Reg.Get_str_or_fail(itm.Rank_tid())));
|
||||
list.Add(Keyval_.new_("type", itm.Prop_type()));
|
||||
Srl_root(list, Wdata_dict_claim.Itm__qualifiers.Key_str(), Srl_qualifiers(visitor, itm.Qualifiers(), base_adj));
|
||||
Srl_root(list, Wdata_dict_claim.Itm__qualifiers.Key_str(), Srl_qualifiers(prop_mgr, visitor, itm.Qualifiers(), base_adj));
|
||||
return (Keyval[])list.To_ary_and_clear(Keyval.class);
|
||||
}
|
||||
private static Keyval[] Srl_qualifiers(Scrib_lib_wikibase_srl_visitor visitor, Wbase_claim_grp_list list, int base_adj) {
|
||||
private static Keyval[] Srl_qualifiers(Wbase_prop_mgr prop_mgr, Scrib_lib_wikibase_srl_visitor visitor, Wbase_claim_grp_list list, int base_adj) {
|
||||
if (list == null) return null;
|
||||
int list_len = list.Len(); if (list_len == 0) return Keyval_.Ary_empty;
|
||||
List_adp rv = List_adp_.New();
|
||||
@@ -135,13 +135,13 @@ class Scrib_lib_wikibase_srl {
|
||||
String itm_pid = grp.Id_str();
|
||||
for (int j = 0; j < grp_len; ++j) {
|
||||
Wbase_claim_base itm = grp.Get_at(j);
|
||||
pid_list.Add(Keyval_.int_(j + base_adj, Srl_claims_prop_itm_core(visitor, itm_pid, itm))); // NOTE: was originally "+ 1"; changed to base_adj; PAGE:ru.w:Tor ru.w:Кактусовые DATE:2014-10-25
|
||||
pid_list.Add(Keyval_.int_(j + base_adj, Srl_claims_prop_itm_core(prop_mgr, visitor, itm_pid, itm))); // NOTE: was originally "+ 1"; changed to base_adj; PAGE:ru.w:Tor ru.w:Кактусовые DATE:2014-10-25
|
||||
}
|
||||
rv.Add(Keyval_.new_(itm_pid, (Keyval[])pid_list.To_ary_and_clear(Keyval.class)));
|
||||
}
|
||||
return (Keyval[])rv.To_ary_and_clear(Keyval.class);
|
||||
}
|
||||
private static Keyval[] Srl_claims_prop_itm_core(Scrib_lib_wikibase_srl_visitor visitor, String pid, Wbase_claim_base itm) {
|
||||
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) {
|
||||
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];
|
||||
@@ -149,7 +149,12 @@ class Scrib_lib_wikibase_srl {
|
||||
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()));
|
||||
rv[2 + snak_is_valued_adj] = Keyval_.new_("datatype", Wbase_claim_type_.Get_scrib_or_unknown(itm.Val_tid())); // NOTE: datatype needed for Modules; PAGE:eo.w:WikidataKoord; DATE:2015-11-08
|
||||
|
||||
// get prop datatype; NOTE: datatype needed for Modules; PAGE:eo.w:WikidataKoord; DATE:2015-11-08
|
||||
String datatype = prop_mgr.Get_or_null(pid);
|
||||
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) {
|
||||
|
||||
@@ -368,6 +368,26 @@ public class Scrib_lib_wikibase_srl_tst {
|
||||
Gftest.Eq__str("timezone", keyval.Key());
|
||||
Gftest.Eq__int(0, (int)keyval.Val()); // fails when keyval.Val() is String; DATE:2016-10-28
|
||||
}
|
||||
@Test public void Claims__commonsMedia() {
|
||||
fxt.Wdata_fxt().Wdata_mgr().Prop_mgr().Loader_(Wbase_prop_mgr_loader_.New_mock(Keyval_.new_("P2", "commonsMedia")));
|
||||
fxt.Init_prop(fxt.Wdata_fxt().Make_claim_string(2, "abc"));
|
||||
fxt.Test
|
||||
( "claims:"
|
||||
, " P2:"
|
||||
, " 1:"
|
||||
, " id:'P2'"
|
||||
, " mainsnak:"
|
||||
, " datavalue:"
|
||||
, " type:'string'"
|
||||
, " value:'abc'"
|
||||
, " property:'P2'"
|
||||
, " snaktype:'value'"
|
||||
, " datatype:'commonsMedia'"
|
||||
, " rank:'normal'"
|
||||
, " type:'statement'"
|
||||
, ""
|
||||
);
|
||||
}
|
||||
@Test public void Type_is_property() { // PURPOSE: type should be "property"; PAGE:ru.w:Викитека:Проект:Викиданные DATE:2016-11-23
|
||||
fxt.Init_header_enabled_y_();
|
||||
fxt.Wdata_fxt().doc_("Property:P1", fxt.Wdata_fxt().Make_claim_string(123, "abc"));
|
||||
@@ -391,11 +411,13 @@ public class Scrib_lib_wikibase_srl_tst {
|
||||
}
|
||||
class Scrib_lib_wikibase_srl_fxt {
|
||||
private Wdata_doc_bldr wdoc_bldr;
|
||||
private Wbase_prop_mgr prop_mgr;
|
||||
public void Clear() {
|
||||
wdata_fxt = new Wdata_wiki_mgr_fxt();
|
||||
wdata_fxt.Init();
|
||||
wdoc_bldr = wdata_fxt.Wdoc_bldr("q2");
|
||||
header_enabled = false;
|
||||
this.prop_mgr = wdata_fxt.App().Wiki_mgr().Wdata_mgr().Prop_mgr();
|
||||
}
|
||||
public Wdata_wiki_mgr_fxt Wdata_fxt() {return wdata_fxt;} private Wdata_wiki_mgr_fxt wdata_fxt;
|
||||
private boolean header_enabled;
|
||||
@@ -419,13 +441,13 @@ class Scrib_lib_wikibase_srl_fxt {
|
||||
public Scrib_lib_wikibase_srl_fxt Init_prop(Wbase_claim_base prop) {wdoc_bldr.Add_claims(prop); return this;}
|
||||
public Scrib_lib_wikibase_srl_fxt Test(String... expd) {return Test(false, expd);}
|
||||
public Scrib_lib_wikibase_srl_fxt Test(boolean base0, String... expd) {
|
||||
Keyval[] actl = Scrib_lib_wikibase_srl.Srl(wdoc_bldr.Xto_wdoc(), header_enabled, base0);
|
||||
Keyval[] actl = Scrib_lib_wikibase_srl.Srl(prop_mgr, wdoc_bldr.Xto_wdoc(), header_enabled, base0);
|
||||
Tfds.Eq_ary_str(expd, String_.SplitLines_nl(Xto_str(actl)));
|
||||
return this;
|
||||
}
|
||||
public Scrib_lib_wikibase_srl_fxt Test(Wdata_doc wdoc, String... expd) {return Test(false, wdoc, expd);}
|
||||
public Scrib_lib_wikibase_srl_fxt Test(boolean base0, Wdata_doc wdoc, String... expd) {
|
||||
Keyval[] actl = Scrib_lib_wikibase_srl.Srl(wdoc, header_enabled, base0);
|
||||
Keyval[] actl = Scrib_lib_wikibase_srl.Srl(prop_mgr, wdoc, header_enabled, base0);
|
||||
Tfds.Eq_ary_str(expd, String_.SplitLines_nl(Xto_str(actl)));
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -121,15 +121,15 @@ public class Scrib_lib_wikibase_tst {
|
||||
class Wbase_snak_utl_ {
|
||||
public static Keyval[] Get_snaks_ary(Wdata_wiki_mgr_fxt wdata_fxt, Wbase_claim_base... ary) {
|
||||
Wdata_doc wdoc = wdata_fxt.Wdoc_bldr("q2").Add_claims(ary).Xto_wdoc();
|
||||
return Keyval_.Ary(Keyval_.int_(1, Get_snaks(wdoc)));
|
||||
return Keyval_.Ary(Keyval_.int_(1, Get_snaks(wdata_fxt, wdoc)));
|
||||
}
|
||||
public static Keyval[] Get_snak(Wdata_wiki_mgr_fxt wdata_fxt, Wbase_claim_base itm) {
|
||||
Wdata_doc wdoc = wdata_fxt.Wdoc_bldr("q2").Add_claims(itm).Xto_wdoc();
|
||||
Keyval[] snak_props = Get_subs_by_path(Get_snaks(wdoc), 0);
|
||||
Keyval[] snak_props = Get_subs_by_path(Get_snaks(wdata_fxt, wdoc), 0);
|
||||
return Keyval_.Ary(Keyval_.int_(1, snak_props));
|
||||
}
|
||||
private static Keyval[] Get_snaks(Wdata_doc wdoc) {
|
||||
Keyval[] wdoc_root = Scrib_lib_wikibase_srl.Srl(wdoc, false, false);
|
||||
private static Keyval[] Get_snaks(Wdata_wiki_mgr_fxt wdata_fxt, Wdata_doc wdoc) {
|
||||
Keyval[] wdoc_root = Scrib_lib_wikibase_srl.Srl(wdata_fxt.Wdata_mgr().Prop_mgr(), wdoc, false, false);
|
||||
Keyval[] snaks = Get_subs_by_path(wdoc_root, 0, 0);
|
||||
int snaks_len = snaks.length;
|
||||
Keyval[] rv = new Keyval[snaks_len];
|
||||
|
||||
Reference in New Issue
Block a user