mirror of
https://github.com/gnosygnu/xowa.git
synced 2024-10-27 20:34:16 +00:00
Parser.Wikibase: Retrieve by label if pid is not present; also, return pid, not label [#354]
This commit is contained in:
parent
623c7a129e
commit
09d9f93d20
@ -163,11 +163,11 @@ public class Scrib_lib_wikibase implements Scrib_lib {
|
||||
return rslt.Init_obj(wkr.Get_referenced_entity());
|
||||
}
|
||||
public boolean EntityExists(Scrib_proc_args args, Scrib_proc_rslt rslt) {
|
||||
Wdata_doc wdoc = Get_wdoc_or_null(args, core, false);
|
||||
Wdata_doc wdoc = Get_wdoc_or_null(args, core, "EntityExists", false);
|
||||
return rslt.Init_obj(wdoc != null);
|
||||
}
|
||||
public boolean GetEntity(Scrib_proc_args args, Scrib_proc_rslt rslt) {
|
||||
Wdata_doc wdoc = Get_wdoc_or_null(args, core, true);
|
||||
Wdata_doc wdoc = Get_wdoc_or_null(args, core, "GetEntity", true);
|
||||
if (wdoc == null) {
|
||||
return rslt.Init_ary_empty();
|
||||
// return rslt.Init_obj(Keyval_.Ary(Keyval_.new_("schemaVersion", 2))); // NOTE: was "rslt.Init_ary_empty()" PAGE:de.w:Critérium_International_2016 DATE:2017-12-30
|
||||
@ -218,8 +218,46 @@ public function formatValues( $snaksSerialization ) {
|
||||
throw Err_.new_unimplemented();
|
||||
}
|
||||
public boolean ResolvePropertyId(Scrib_proc_args args, Scrib_proc_rslt rslt) {
|
||||
Wdata_doc wdoc = Get_wdoc_or_null(args, core, true); if (wdoc == null) return rslt.Init_ary_empty(); // NOTE: prop should be of form "P123"; do not add "P"; PAGE:no.w:Anne_Enger; DATE:2015-10-27
|
||||
return rslt.Init_obj(wdoc.Label_list__get_or_fallback(core.Lang()));
|
||||
byte[] rv = null;
|
||||
byte[] pid = Get_xid_from_args(args);
|
||||
if (pid != null) {
|
||||
// if pid is "P####", look-up in db by title
|
||||
byte b0 = pid[0];
|
||||
if (b0 == Byte_ascii.Ltr_P || b0 == Byte_ascii.Ltr_p) {
|
||||
// check if rest is numeric
|
||||
boolean numeric = true;
|
||||
for (int i = 1; i < pid.length; i++) {
|
||||
if (!Byte_ascii.Is_num(pid[i])) {
|
||||
numeric = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (numeric) {
|
||||
byte[] key = pid;
|
||||
if (b0 == Byte_ascii.Ltr_p) key[0] = Byte_ascii.Ltr_P; // uppercase key
|
||||
Wdata_doc wdoc = entity_mgr.Get_by_xid_or_null(key); // NOTE: by_xid b/c Module passes just "p1" not "Property:P1"
|
||||
if (wdoc != null) rv = key; // found wdoc; set rv
|
||||
}
|
||||
}
|
||||
|
||||
// pid is non-numeric or does not exist; look-up by label
|
||||
if (rv == null) {
|
||||
Wdata_wiki_mgr wdata_mgr = core.Wiki().Appe().Wiki_mgr().Wdata_mgr();
|
||||
int pid_int = wdata_mgr.Pid_mgr.Get_pid_or_neg1(core.Wiki().Wdata_wiki_lang(), pid);
|
||||
if (pid_int != gplx.xowa.xtns.wbases.core.Wbase_pid.Id_null) {
|
||||
Bry_bfr tmp_bfr = Bry_bfr_.New();
|
||||
tmp_bfr.Add_byte(Byte_ascii.Ltr_P);
|
||||
tmp_bfr.Add_long_variable(pid_int);
|
||||
rv = tmp_bfr.To_bry_and_clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
if (rv == null) {
|
||||
Wdata_wiki_mgr.Log_missing_qid(core.Ctx(), "ResolvePropertyId", pid);
|
||||
return rslt.Init_null(); // NOTE: should be null, not empty; verified with "=mw.wikibase.resolvePropertyId('')" -> nil; DATE:2019-04-07
|
||||
}
|
||||
else
|
||||
return rslt.Init_obj(rv);
|
||||
}
|
||||
public boolean GetPropertyOrder(Scrib_proc_args args, Scrib_proc_rslt rslt) {
|
||||
throw Err_.new_("wbase", "getPropertyOrder not implemented", "url", core.Page().Url().To_str());
|
||||
@ -228,7 +266,7 @@ public function formatValues( $snaksSerialization ) {
|
||||
throw Err_.new_("wbase", "orderProperties not implemented", "url", core.Page().Url().To_str());
|
||||
}
|
||||
public boolean GetLabel(Scrib_proc_args args, Scrib_proc_rslt rslt) {
|
||||
Wdata_doc wdoc = Get_wdoc_or_null(args, core, true);
|
||||
Wdata_doc wdoc = Get_wdoc_or_null(args, core, "GetLabel", true);
|
||||
if (wdoc == null)
|
||||
return rslt.Init_ary_empty();
|
||||
else
|
||||
@ -240,12 +278,12 @@ public function formatValues( $snaksSerialization ) {
|
||||
return rslt.Init_obj(wikibaseLanguageIndependentLuaBindings.getLabelByLanguage(prefixedEntityId, languageCode));
|
||||
}
|
||||
public boolean GetSiteLinkPageName(Scrib_proc_args args, Scrib_proc_rslt rslt) {
|
||||
Wdata_doc wdoc = Get_wdoc_or_null(args, core, true); if (wdoc == null) return rslt.Init_ary_empty(); // NOTE: prop should be of form "P123"; do not add "P"; PAGE:no.w:Anne_Enger; DATE:2015-10-27
|
||||
Wdata_doc wdoc = Get_wdoc_or_null(args, core, "GetSiteLinkPageName", true); if (wdoc == null) return rslt.Init_ary_empty(); // NOTE: prop should be of form "P123"; do not add "P"; PAGE:no.w:Anne_Enger; DATE:2015-10-27
|
||||
Xow_domain_itm domain_itm = core.Wiki().Domain_itm();
|
||||
return rslt.Init_obj(wdoc.Slink_list__get_or_fallback(domain_itm.Abrv_wm()));
|
||||
}
|
||||
public boolean GetDescription(Scrib_proc_args args, Scrib_proc_rslt rslt) {
|
||||
Wdata_doc wdoc = Get_wdoc_or_null(args, core, true); if (wdoc == null) return rslt.Init_ary_empty();
|
||||
Wdata_doc wdoc = Get_wdoc_or_null(args, core, "GetDescription", true); if (wdoc == null) return rslt.Init_ary_empty();
|
||||
return rslt.Init_obj(wdoc.Descr_list__get_or_fallback(core.Lang()));
|
||||
}
|
||||
public boolean GetUserLang(Scrib_proc_args args, Scrib_proc_rslt rslt) {
|
||||
@ -264,14 +302,21 @@ public function formatValues( $snaksSerialization ) {
|
||||
public boolean IncrementExpensiveFunctionCount(Scrib_proc_args args, Scrib_proc_rslt rslt) {
|
||||
return rslt.Init_obj(Keyval_.Ary_empty); // NOTE: for now, always return null (XOWA does not care about expensive parser functions)
|
||||
}
|
||||
public Wdata_doc Get_wdoc_or_null(Scrib_proc_args args, Scrib_core core, boolean logMissing) {
|
||||
// get qid / pid from scrib_arg[0]; if none, return null;
|
||||
byte[] xid_bry = args.Pull_bry(0); if (Bry_.Len_eq_0(xid_bry)) return null; // NOTE: some Modules do not pass in an argument; return early, else spurious warning "invalid qid for ttl" (since ttl is blank); EX:w:Module:Authority_control; DATE:2013-10-27
|
||||
xid_bry = Bry_.Trim(xid_bry); // trim, b/c some pages will literally pass in "Property:P5\n"; PAGE:de.w:Mailand–Sanremo_2016 ISSUE#:363; DATE:2019-02-12
|
||||
private byte[] Get_xid_from_args(Scrib_proc_args args) {
|
||||
// get qid / pid from scrib_arg[0]
|
||||
byte[] xid_bry = args.Pull_bry(0);
|
||||
// NOTE: some Modules do not pass in an argument; return early, else spurious warning "invalid qid for ttl" (since ttl is blank); EX:w:Module:Authority_control; DATE:2013-10-27
|
||||
return Bry_.Len_eq_0(xid_bry)
|
||||
? null
|
||||
: Bry_.Trim(xid_bry); // trim, b/c some pages will literally pass in "Property:P5\n"; PAGE:de.w:Mailand–Sanremo_2016 ISSUE#:363; DATE:2019-02-12
|
||||
}
|
||||
private Wdata_doc Get_wdoc_or_null(Scrib_proc_args args, Scrib_core core, String type, boolean logMissing) {
|
||||
byte[] xid_bry = Get_xid_from_args(args);
|
||||
if (xid_bry == null) return null;
|
||||
|
||||
// get wdoc
|
||||
Wdata_doc wdoc = entity_mgr.Get_by_xid_or_null(xid_bry); // NOTE: by_xid b/c Module passes just "p1" not "Property:P1"
|
||||
if (wdoc == null && logMissing) Wdata_wiki_mgr.Log_missing_qid(core.Ctx(), xid_bry);
|
||||
if (wdoc == null && logMissing) Wdata_wiki_mgr.Log_missing_qid(core.Ctx(), type, xid_bry);
|
||||
return wdoc;
|
||||
}
|
||||
}
|
||||
|
@ -63,7 +63,7 @@ public class Scrib_lib_wikibase_entity implements Scrib_lib {
|
||||
// get wdoc
|
||||
Wdata_doc wdoc = wdata_mgr.Doc_mgr.Get_by_loose_id_or_null(qid);
|
||||
if (wdoc == null) {
|
||||
Wdata_wiki_mgr.Log_missing_qid(core.Ctx(), qid);
|
||||
Wdata_wiki_mgr.Log_missing_qid(core.Ctx(), "FormatPropertyValues", qid);
|
||||
return rslt.Init_str_empty(); // NOTE: return empty String, not nil; PAGE:fr.s:Henri_Bergson; DATE:2014-08-13
|
||||
}
|
||||
|
||||
|
@ -140,10 +140,6 @@ public class Basic__tst {
|
||||
// ( "1=" // not ""
|
||||
// ));
|
||||
// }
|
||||
@Test public void ResolvePropertyId() {
|
||||
wdata_fxt.Init__docs__add(wdata_fxt.Wdoc_bldr("Property:p2").Add_label("zh-hans", "prop_a").Xto_wdoc());
|
||||
fxt.Test_scrib_proc_str(lib, Scrib_lib_wikibase.Invk_resolvePropertyId, Object_.Ary("p2"), "prop_a");
|
||||
}
|
||||
@Test public void RenderSnaks() {
|
||||
Keyval[] args = Wbase_snak_utl_.Get_snaks_ary(wdata_fxt, wdata_fxt.Make_claim_monolingual(3, "en", "P3_en"), wdata_fxt.Make_claim_monolingual(3, "de", "P3_de"));
|
||||
fxt.Test__proc__kvps__flat(lib, Scrib_lib_wikibase.Invk_renderSnaks, args, "P3_en, P3_de");
|
||||
|
@ -0,0 +1,56 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012-2017 gnosygnu@gmail.com
|
||||
|
||||
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.
|
||||
|
||||
You may use XOWA according to either of these licenses as is most appropriate
|
||||
for your project on a case-by-case basis.
|
||||
|
||||
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
|
||||
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
|
||||
*/
|
||||
package gplx.xowa.xtns.scribunto.libs.wikibases; import gplx.*; import gplx.xowa.*; import gplx.xowa.xtns.*; import gplx.xowa.xtns.scribunto.*; import gplx.xowa.xtns.scribunto.libs.*;
|
||||
import org.junit.*; import gplx.xowa.xtns.wbases.*; import gplx.xowa.xtns.wbases.core.*; import gplx.xowa.xtns.wbases.claims.*; import gplx.xowa.xtns.wbases.claims.itms.*;
|
||||
import gplx.xowa.xtns.wbases.claims.enums.*;
|
||||
public class ResolvePropertyId__tst {
|
||||
private final ResolvePropertyId__fxt fxt = new ResolvePropertyId__fxt();
|
||||
@Before public void init() {
|
||||
fxt.Init();
|
||||
}
|
||||
@Test public void Basic() {
|
||||
fxt.Init__doc("Property:P2");
|
||||
fxt.Init__pid("de", "de0", 2);
|
||||
fxt.Init__pid("en", "en0", 2);
|
||||
|
||||
fxt.Test__ResolvePropertyId("P2" , "P2"); // match exact
|
||||
fxt.Test__ResolvePropertyId("p2" , "P2"); // match by lower-case
|
||||
fxt.Test__ResolvePropertyId("P3" , null); // do not match if unknown doc
|
||||
fxt.Test__ResolvePropertyId("de0", "P2"); // match by label
|
||||
fxt.Test__ResolvePropertyId("en0", null); // do not match if label is not same lang as wiki
|
||||
fxt.Test__ResolvePropertyId("fr0", null); // do not match if unknown label
|
||||
}
|
||||
}
|
||||
class ResolvePropertyId__fxt {
|
||||
private final Scrib_invoke_func_fxt fxt = new Scrib_invoke_func_fxt();
|
||||
private final Wdata_wiki_mgr_fxt wdata_fxt = new Wdata_wiki_mgr_fxt();
|
||||
private Scrib_lib lib;
|
||||
public void Init() {
|
||||
fxt.Clear_for_lib("de.wikipedia.org", "de");
|
||||
lib = fxt.Core().Lib_wikibase().Init();
|
||||
wdata_fxt.Init(fxt.Parser_fxt(), false);
|
||||
}
|
||||
public void Init__doc(String title) {
|
||||
Wdata_doc_bldr bldr = wdata_fxt.Wdoc_bldr(title);
|
||||
wdata_fxt.Init__docs__add(bldr.Xto_wdoc());
|
||||
}
|
||||
public void Init__pid(String lang_key, String pid_name, int pid) {
|
||||
wdata_fxt.Init_pids_add(lang_key, pid_name, pid);
|
||||
}
|
||||
public void Test__ResolvePropertyId(String arg, String expd) {
|
||||
fxt.Test_scrib_proc_str(lib, Scrib_lib_wikibase.Invk_resolvePropertyId, Object_.Ary(arg), expd);
|
||||
}
|
||||
}
|
@ -194,7 +194,8 @@ public class Wdata_wiki_mgr implements Gfo_evt_itm, Gfo_invk {
|
||||
}
|
||||
return false;
|
||||
}
|
||||
public static void Log_missing_qid(Xop_ctx ctx, byte[] qid) {
|
||||
ctx.Wiki().Appe().Usr_dlg().Log_many("", "", "qid not found in wikidata; qid=~{0} page=~{1}", String_.new_u8(qid), String_.new_u8(ctx.Page().Url_bry_safe()));
|
||||
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());
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user