You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
gnosygnu_xowa/400_xowa/src/gplx/xowa/xtns/scribunto/libs/Scrib_lib_wikibase.java

185 lines
12 KiB

/*
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; import gplx.*; import gplx.xowa.*; import gplx.xowa.xtns.*; import gplx.xowa.xtns.scribunto.*;
import gplx.langs.jsons.*; import gplx.xowa.xtns.wbases.*; import gplx.xowa.xtns.wbases.parsers.*; import gplx.xowa.xtns.wbases.claims.itms.*;
import gplx.xowa.wikis.domains.*;
import gplx.xowa.xtns.scribunto.procs.*;
import gplx.xowa.xtns.wbases.mediawiki.client.includes.*; import gplx.xowa.xtns.wbases.mediawiki.client.includes.dataAccess.scribunto.*;
public class Scrib_lib_wikibase implements Scrib_lib {
private Wbase_entity_accessor entity_accessor;
public Scrib_lib_wikibase(Scrib_core core) {this.core = core;} private Scrib_core core;
public Scrib_lua_mod Mod() {return mod;} private Scrib_lua_mod mod;
public Scrib_lib Init() {
procs.Init_by_lib(this, Proc_names);
this.entity_accessor = new Wbase_entity_accessor(core.App().Wiki_mgr().Wdata_mgr().Doc_mgr);
return this;
}
public Scrib_lib Clone_lib(Scrib_core core) {return new Scrib_lib_wikibase(core);}
public Scrib_lua_mod Register(Scrib_core core, Io_url script_dir) {
Init();
mod = core.RegisterInterface(this, script_dir.GenSubFil("mw.wikibase.lua"));
notify_page_changed_fnc = mod.Fncs_get_by_key("notify_page_changed");
return mod;
} private Scrib_lua_proc notify_page_changed_fnc;
public Scrib_proc_mgr Procs() {return procs;} private Scrib_proc_mgr procs = new Scrib_proc_mgr();
public boolean Procs_exec(int key, Scrib_proc_args args, Scrib_proc_rslt rslt) {
switch (key) {
case Proc_getLabel: return GetLabel(args, rslt);
// getLabelByLanguage
case Proc_getEntity: return GetEntity(args, rslt);
case Proc_getEntityStatements: return GetEntityStatements(args, rslt);
case Proc_getSetting: return GetSetting(args, rslt);
case Proc_getEntityUrl: return GetEntityUrl(args, rslt);
case Proc_renderSnak: return RenderSnak(args, rslt);
// formatValue
case Proc_renderSnaks: return RenderSnaks(args, rslt);
case Proc_getEntityId: return GetEntityId(args, rslt);
case Proc_getUserLang: return GetUserLang(args, rslt);
case Proc_getDescription: return GetDescription(args, rslt);
case Proc_resolvePropertyId: return ResolvePropertyId(args, rslt);
case Proc_getSiteLinkPageName: return GetSiteLinkPageName(args, rslt);
case Proc_incrementExpensiveFunctionCount: return IncrementExpensiveFunctionCount(args, rslt);
case Proc_getPropertyOrder: return GetPropertyOrder(args, rslt);
case Proc_orderProperties: return OrderProperties(args, rslt);
default: throw Err_.new_unhandled(key);
}
}
private static final int
Proc_getLabel = 0 //, Proc_getLabelByLanguage = 1
, Proc_getEntity = 2, Proc_getEntityStatements = 3, Proc_getSetting = 4, Proc_getEntityUrl = 5, Proc_renderSnak = 6//, Proc_formatValue = 7
, Proc_renderSnaks = 8, Proc_getEntityId = 9, Proc_getUserLang = 10, Proc_getDescription = 11, Proc_resolvePropertyId = 12
, Proc_getSiteLinkPageName = 13, Proc_incrementExpensiveFunctionCount = 14, Proc_getPropertyOrder = 15, Proc_orderProperties = 16;
public static final String
Invk_getLabel = "getLabel", Invk_getLabelByLanguage = "getLabelByLanguage", Invk_getEntity = "getEntity", Invk_getEntityStatements = "getEntityStatements"
, Invk_getSetting = "getSetting", Invk_getEntityUrl = "getEntityUrl", Invk_renderSnak = "renderSnak", Invk_formatValue = "formatValue", Invk_renderSnaks = "renderSnaks"
, Invk_getEntityId = "getEntityId", Invk_getUserLang = "getUserLang", Invk_getDescription = "getDescription", Invk_resolvePropertyId = "resolvePropertyId"
, Invk_getSiteLinkPageName = "getSiteLinkPageName", Invk_incrementExpensiveFunctionCount = "incrementExpensiveFunctionCount", Invk_getPropertyOrder = "getPropertyOrder", Invk_orderProperties = "orderProperties"
;
private static final String[] Proc_names = String_.Ary
( Invk_getLabel, Invk_getLabelByLanguage, Invk_getEntity, Invk_getEntityStatements, Invk_getSetting, Invk_getEntityUrl, Invk_renderSnak, Invk_formatValue, Invk_renderSnaks
, Invk_getEntityId, Invk_getUserLang, Invk_getDescription, Invk_resolvePropertyId, Invk_getSiteLinkPageName, Invk_incrementExpensiveFunctionCount
, Invk_getPropertyOrder, Invk_orderProperties
);
public void Notify_page_changed() {if (notify_page_changed_fnc != null) core.Interpreter().CallFunction(notify_page_changed_fnc.Id(), Keyval_.Ary_empty);}
public boolean GetLabel(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(wdoc.Label_list__get_or_fallback(core.Lang()));
}
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();
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) {
byte[] entityId = args.Pull_bry(0);
byte[] entity_url = Wbase_client.getDefaultInstance().RepoLinker().getEntityUrl(entityId);
return rslt.Init_obj(entity_url);
}
public boolean GetSetting(Scrib_proc_args args, Scrib_proc_rslt rslt) {throw Err_.new_("wbase", "getSetting not implemented", "url", core.Page().Url().To_str());}
public boolean RenderSnak(Scrib_proc_args args, Scrib_proc_rslt rslt) {
Xowe_wiki wiki = core.Wiki();
Wdata_wiki_mgr wdata_mgr = wiki.Appe().Wiki_mgr().Wdata_mgr();
String rv = Wdata_prop_val_visitor_.Render_snak(wdata_mgr, wiki, core.Page().Url_bry_safe(), args.Pull_kv_ary_safe(0));
return rslt.Init_obj(rv);
}
public boolean RenderSnaks(Scrib_proc_args args, Scrib_proc_rslt rslt) {
String rv = Wdata_prop_val_visitor_.Render_snaks(core.Wiki(), core.Page().Url_bry_safe(), args.Pull_kv_ary_safe(0));
return rslt.Init_obj(rv);
}
public boolean GetEntityId(Scrib_proc_args args, Scrib_proc_rslt rslt) {
byte[] ttl_bry = args.Pull_bry(0);
Xowe_wiki wiki = core.Wiki();
Xoa_ttl ttl = Xoa_ttl.Parse(wiki, ttl_bry);
byte[] rv = wiki.Appe().Wiki_mgr().Wdata_mgr().Qid_mgr.Get_or_null(wiki, ttl); if (rv == null) rv = Bry_.Empty;
return rslt.Init_obj(rv);
}
public boolean GetUserLang(Scrib_proc_args args, Scrib_proc_rslt rslt) {
return rslt.Init_obj(core.App().Usere().Lang().Key_bry());
}
public boolean GetDescription(Scrib_proc_args args, Scrib_proc_rslt rslt) {
Wdata_doc wdoc = Get_wdoc_or_null(args, core); if (wdoc == null) return rslt.Init_ary_empty();
return rslt.Init_obj(wdoc.Descr_list__get_or_fallback(core.Lang()));
}
public boolean ResolvePropertyId(Scrib_proc_args args, Scrib_proc_rslt rslt) {
Wdata_doc wdoc = Get_wdoc_or_null(args, core); 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()));
}
public boolean GetSiteLinkPageName(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(); // 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 IncrementExpensiveFunctionCount(Scrib_proc_args args, Scrib_proc_rslt rslt) {return rslt.Init_obj(Keyval_.Ary_empty);} // NOTE: for now, always return null (XOWA does not care about expensive parser functions)
public boolean GetGlobalSiteId(Scrib_proc_args args, Scrib_proc_rslt rslt) {
return rslt.Init_obj(core.Wiki().Domain_abrv()); // ;siteGlobalID: This site's global ID (e.g. <code>'itwiki'</code>), as used in the sites table. Default: <code>$wgDBname</code>.; REF:/xtns/Wikibase/docs/options.wiki
}
public boolean GetEntityStatements(Scrib_proc_args args, Scrib_proc_rslt rslt) {
byte[] prefixedEntityId = args.Pull_bry(0);
byte[] propertyId = args.Pull_bry(1);
byte[] rank = args.Pull_bry(2);
Wbase_prop_mgr prop_mgr = core.Wiki().Appe().Wiki_mgr().Wdata_mgr().Prop_mgr();
Wbase_claim_base[] statements = this.entity_accessor.getEntityStatements(prefixedEntityId, propertyId, rank);
if (statements == null)
return rslt.Init_null();
return rslt.Init_obj(Scrib_lib_wikibase_srl.Srl_claims_prop_ary(prop_mgr, String_.new_u8(propertyId), statements, 1));
}
/*
public function formatValues( $snaksSerialization ) {
$this->checkType( 'formatValues', 1, $snaksSerialization, 'table' );
try {
$ret = [ $this->getSnakSerializationRenderer( 'rich-wikitext' )->renderSnaks( $snaksSerialization ) ];
return $ret;
} catch ( DeserializationException $e ) {
throw new ScribuntoException( 'wikibase-error-deserialize-error' );
}
}
public function getLabelByLanguage( $prefixedEntityId, $languageCode ) {
$this->checkType( 'getLabelByLanguage', 1, $prefixedEntityId, 'String' );
$this->checkType( 'getLabelByLanguage', 2, $languageCode, 'String' );
return [ $this->getLanguageIndependentLuaBindings()->getLabelByLanguage( $prefixedEntityId, $languageCode ) ];
}
private function getLanguageIndependentLuaBindings() {
if ( $this->languageIndependentLuaBindings === null ) {
$this->languageIndependentLuaBindings = $this->newLanguageIndependentLuaBindings();
}
return $this->languageIndependentLuaBindings;
}
private function newLanguageIndependentLuaBindings() {
$wikibaseClient = WikibaseClient::getDefaultInstance();
return new WikibaseLanguageIndependentLuaBindings(
$wikibaseClient->getStore()->getSiteLinkLookup(),
$wikibaseClient->getSettings(),
$this->getUsageAccumulator(),
$this->getEntityIdParser(),
$wikibaseClient->getTermLookup(),
$wikibaseClient->getTermsLanguages(),
$wikibaseClient->getSettings()->getSetting( 'siteGlobalID' )
);
} */
private static Wdata_doc Get_wdoc_or_null(Scrib_proc_args args, Scrib_core core) {
// 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
// get wdoc
Wdata_doc wdoc = core.Wiki().Appe().Wiki_mgr().Wdata_mgr().Doc_mgr.Get_by_xid_or_null(xid_bry); // NOTE: by_xid b/c Module passes just "p1" not "Property:P1"
if (wdoc == null) Wdata_wiki_mgr.Log_missing_qid(core.Ctx(), xid_bry);
return wdoc;
}
public boolean GetPropertyOrder(Scrib_proc_args args, Scrib_proc_rslt rslt) {throw Err_.new_("wbase", "getPropertyOrder not implemented", "url", core.Page().Url().To_str());}
public boolean OrderProperties(Scrib_proc_args args, Scrib_proc_rslt rslt) {throw Err_.new_("wbase", "orderProperties not implemented", "url", core.Page().Url().To_str());}
}