1
0
mirror of https://github.com/gnosygnu/xowa.git synced 2026-03-02 03:49:30 +00:00

Scribunto: Set frame title for pages when invoking Scribunto code [#476]

This commit is contained in:
gnosygnu
2019-05-25 17:19:47 -04:00
parent cca043dff6
commit ee03d79536
11 changed files with 220 additions and 50 deletions

View File

@@ -45,7 +45,7 @@ public class Lst_pfunc_itm {
// eval tmpl
Bry_bfr tmp_bfr = wiki.Utl__bfr_mkr().Get_m001();
try {
tmpl.Tmpl_evaluate(sub_ctx, Xot_invk_temp.Page_is_caller, tmp_bfr);
tmpl.Tmpl_evaluate(sub_ctx, Xot_invk_temp.New_root(), tmp_bfr);
sub_src = tmp_bfr.To_bry_and_clear();
} finally {
tmp_bfr.Mkr_rls();

View File

@@ -115,7 +115,7 @@ public class Scrib_invoke_func_fxt {
fxt = new Xop_fxt(app, Xoa_app_fxt.Make__wiki__edit(app, domain, app.Lang_mgr().Get_by_or_new(Bry_.new_u8(lang)))); // NOTE: don't try to cache fxt on func_fxt level; causes errors in Language_lib
core_fxt = new Scrib_core_fxt(fxt);
core = core_fxt.Core();
Xot_invk parent_frame = new Xot_invk_temp(true); parent_frame.Frame_tid_(Scrib_frame_.Tid_null);
Xot_invk parent_frame = Xot_invk_temp.New_root(); parent_frame.Frame_tid_(Scrib_frame_.Tid_null);
Xot_invk current_frame = Xot_invk_mock.test_(Bry_.new_a7("Module:Mod_0"));
core.Invoke_init(core.Wiki(), core.Ctx(), Bry_.Empty, parent_frame, current_frame);
core.When_page_changed(fxt.Page());

View File

@@ -0,0 +1,59 @@
/*
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.engines.mocks; import gplx.*; import gplx.xowa.*; import gplx.xowa.xtns.*; import gplx.xowa.xtns.scribunto.*; import gplx.xowa.xtns.scribunto.engines.*;
import gplx.core.primitives.*;
import gplx.core.lists.hashs.*;
public class Mock_engine implements Scrib_engine {
private final Hash_adp__int fnc_hash = new Hash_adp__int();
private final Hash_adp mod_init_hash = Hash_adp_.New();
private final Hash_adp__int mod_fnc_hash = new Hash_adp__int();
public boolean Dbg_print() {return false;} public void Dbg_print_(boolean v) {}
public Scrib_server Server() {return server;} public void Server_(Scrib_server v) {} private final Mock_server server = new Mock_server();
public Scrib_lua_proc LoadString(String name, String text) {
return (Scrib_lua_proc)mod_init_hash.Get_by(name);
}
public Keyval[] CallFunction(int id, Keyval[] args) {
Mock_proc_fxt fnc = (Mock_proc_fxt)fnc_hash.Get_by_or_fail(id);
return fnc.Exec_by_scrib(args);
}
public void RegisterLibrary(Keyval[] functions_ary) {}
public Keyval[] ExecuteModule(int mod_id) {throw Err_.new_unimplemented();}
public void CleanupChunks(Keyval[] ids) {}
public void Clear() {fnc_hash.Clear();}
public void InitFunctionForTest(Mock_proc_fxt proc) {
fnc_hash.Add(proc.Id(), proc);
}
public void Init_module(String mod_name, int mod_id) {
mod_init_hash.Add_if_dupe_use_nth(mod_name, new Scrib_lua_proc(mod_name, mod_id));
}
public void Init_module_func(int mod_id, Mock_proc_fxt fnc) {
Hash_adp funcs = (Hash_adp)mod_fnc_hash.Get_by_or_null(mod_id);
if (funcs == null) {
funcs = Hash_adp_.New();
mod_fnc_hash.Add(mod_id, funcs);
}
fnc_hash.Add(fnc.Id(), fnc);
funcs.Add(fnc.Key(), fnc.To_scrib_lua_proc());
}
public Scrib_lua_proc Get_module_func(int mod_id, String fnc_name) {
Hash_adp funcs = (Hash_adp)mod_fnc_hash.Get_by_or_fail(mod_id);
return (Scrib_lua_proc)funcs.Get_by_or_fail(fnc_name);
}
}

View File

@@ -15,6 +15,7 @@ Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
*/
package gplx.xowa.xtns.scribunto.engines.mocks; import gplx.*; import gplx.xowa.*; import gplx.xowa.xtns.*; import gplx.xowa.xtns.scribunto.*; import gplx.xowa.xtns.scribunto.engines.*;
import gplx.core.primitives.*;
import gplx.xowa.xtns.scribunto.procs.*;
public abstract class Mock_proc_fxt {
public Mock_proc_fxt(int id, String key) {this.id = id; this.key = key;}
public int Id() {return id;} private final int id;
@@ -22,21 +23,44 @@ public abstract class Mock_proc_fxt {
public Scrib_lua_proc To_scrib_lua_proc() {return new Scrib_lua_proc(key, id);}
public abstract Keyval[] Exec_by_scrib(Keyval[] args);
}
class Mock_engine implements Scrib_engine {
private final Hash_adp hash = Hash_adp_.New();
private final Int_obj_ref tmp_hash_id = Int_obj_ref.New_neg1();
public boolean Dbg_print() {return false;} public void Dbg_print_(boolean v) {}
public Scrib_server Server() {return server;} public void Server_(Scrib_server v) {} private final Mock_server server = new Mock_server();
public Scrib_lua_proc LoadString(String name, String text) {return null;}
public Keyval[] CallFunction(int id, Keyval[] args) {
Mock_proc_fxt proc = (Mock_proc_fxt)hash.Get_by_or_fail(tmp_hash_id.Val_(id));
return proc.Exec_by_scrib(args);
class Mock_exec_module extends Mock_proc_fxt { private final Mock_engine engine;
public Mock_exec_module(int mod_id, Mock_engine engine) {super(mod_id, "mockExecuteModule");
this.engine = engine;
}
public void RegisterLibrary(Keyval[] functions_ary) {}
public Keyval[] ExecuteModule(int mod_id) {return null;}
public void CleanupChunks(Keyval[] ids) {}
public void Clear() {hash.Clear();}
public void RegisterLibraryForTest(Mock_proc_fxt proc) {
hash.Add(Int_obj_ref.New(proc.Id()), proc);
@Override public Keyval[] Exec_by_scrib(Keyval[] args) {
Scrib_lua_proc mod_proc = (Scrib_lua_proc)args[0].Val();
String fnc_name = (String)args[1].Val();
return new Keyval[] {Keyval_.int_(0, true), Keyval_.int_(1, engine.Get_module_func(mod_proc.Id(), fnc_name))};
}
}
class Mock_exec_function extends Mock_proc_fxt { private final Mock_engine engine;
public Mock_exec_function(int func_id, Mock_engine engine) {super(func_id, "mockExecuteFuntion");
this.engine = engine;
}
@Override public Keyval[] Exec_by_scrib(Keyval[] args) {
Scrib_lua_proc mod_proc = (Scrib_lua_proc)args[0].Val();
return engine.CallFunction(mod_proc.Id(), args);
}
}
class Mock_exec_lib extends Mock_proc_fxt { private Scrib_lib lib;
private String proc_name;
private Keyval[] proc_args;
public Mock_exec_lib(int fnc_id, String fnc_name, Scrib_lib lib, String proc_name, Object... proc_obj_args) {super(fnc_id, fnc_name);
this.lib = lib;
this.proc_name = proc_name;
int len = proc_obj_args.length;
this.proc_args = new Keyval[len];
for (int i = 0; i < len; i++) {
proc_args[i] = Keyval_.int_(i + 1, proc_obj_args[i]);
}
}
@Override public Keyval[] Exec_by_scrib(Keyval[] args) {
Scrib_proc proc = lib.Procs().Get_by_key(proc_name);
Scrib_proc_rslt proc_rslt = new Scrib_proc_rslt();
proc.Proc_exec(new Scrib_proc_args(proc_args), proc_rslt);
return Extract_rslt(proc_rslt);
}
private Keyval[] Extract_rslt(Scrib_proc_rslt proc_rslt) {
return new Keyval[] {Keyval_.int_(1, Object_.Xto_str_strict_or_null_mark(proc_rslt.Ary()[0].Val()))};
}
}

View File

@@ -19,7 +19,9 @@ import gplx.xowa.xtns.scribunto.procs.*;
public class Mock_scrib_fxt {
private final Mock_engine engine = new Mock_engine();
private final Mock_server server = new Mock_server();
private int mock_mod_id_next = 10;
public Scrib_core Core() {return core;} private Scrib_core core;
public Mock_engine Engine() {return engine;}
public Xop_fxt Parser_fxt() {return parser_fxt;} private Xop_fxt parser_fxt;
public void Clear() {Clear("en.wikipedia.org", "en");}
public void Clear(String domain, String lang) {
@@ -29,7 +31,7 @@ public class Mock_scrib_fxt {
core = wiki.Parser_mgr().Scrib().Core_init(wiki.Parser_mgr().Ctx());
core.Engine_(engine); engine.Clear();
core.Interpreter().Server_(server);
Xot_invk parent_frame = new Xot_invk_temp(true); parent_frame.Frame_tid_(Scrib_frame_.Tid_null);
Xot_invk parent_frame = Xot_invk_temp.New_root(); parent_frame.Frame_tid_(Scrib_frame_.Tid_null);
Xot_invk current_frame = Xot_invk_mock.test_(Bry_.new_a7("Module:Mod_0"));
core.Invoke_init(core.Wiki(), core.Ctx(), Bry_.Empty, parent_frame, current_frame);
core.When_page_changed(parser_fxt.Page());
@@ -37,9 +39,30 @@ public class Mock_scrib_fxt {
public void Init__cbk(Mock_proc_fxt... ary) {
engine.Clear();
for (Mock_proc_fxt proc : ary)
engine.RegisterLibraryForTest(proc);
engine.InitFunctionForTest(proc);
}
public void Init__page(String ttl, String txt) {parser_fxt.Init_page_create(ttl, txt);}
public void Init__mock_mod_system() {
Scrib_lua_mod mod = new Scrib_lua_mod(core, "mw.lua");
core.Lib_mw().Mod_(mod);
mod.Fncs_add(new Scrib_lua_proc("executeModule", 8));
mod.Fncs_add(new Scrib_lua_proc("executeFunction", 9));
engine.InitFunctionForTest(new Mock_exec_module(8, engine));
engine.InitFunctionForTest(new Mock_exec_function(9, engine));
}
public void Init__mock_mod(Scrib_lib lib, String mod_name, Mock_proc_fxt... prc_ary) {
int mod_id = mock_mod_id_next++;
String mod_text = "";
engine.Init_module("=" + mod_name, mod_id);
for (Mock_proc_fxt prc : prc_ary) {
mod_text = mod_text + prc.Key() + "\n";
engine.Init_module_func(mod_id, prc);
}
parser_fxt.Init_page_create(mod_name, mod_text);
}
public Mock_proc_fxt Init__mock_fnc_for_lib(String fnc_name, Scrib_lib lib, String proc_name, Object... proc_args) {
return new Mock_exec_lib(mock_mod_id_next++, fnc_name, lib, proc_name, proc_args);
}
public void Test__proc__ints (Scrib_lib lib, String proc_name, Object[] args, int expd) {Test__proc__kvps(lib, proc_name, Bool_.Y, Int_.To_str(expd), Scrib_kv_utl_.base1_many_(args));}
public void Test__proc__objs__flat(Scrib_lib lib, String proc_name, Object[] args, String expd) {Test__proc__kvps(lib, proc_name, Bool_.Y, expd, Scrib_kv_utl_.base1_many_(args));}
public void Test__proc__objs__nest(Scrib_lib lib, String proc_name, Object[] args, Keyval[] expd) {Test__proc__kvps(lib, proc_name, Bool_.N, Keyval_.Ary__to_str__nest(new Keyval[] {Keyval_.int_(Scrib_core.Base_1, expd)}), Scrib_kv_utl_.base1_many_(args));}
@@ -62,6 +85,7 @@ public class Mock_scrib_fxt {
Object[] actl_ary = Mock_scrib_fxt_.Kvp_vals_to_objs(actl_kvs);
Tfds.Eq_ary(expd_ary, actl_ary);
}
public void Test__parse__tmpl_to_html(String raw, String expd) {parser_fxt.Test__parse__tmpl_to_html(raw, expd);}
}
class Mock_scrib_fxt_ {
public static Keyval[] Test__lib_proc__core(Scrib_lib lib, String proc_name, Keyval[] args) {

View File

@@ -0,0 +1,48 @@
/*
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 org.junit.*;
import gplx.xowa.xtns.scribunto.engines.mocks.*;
import gplx.xowa.xtns.scribunto.procs.*;
public class Scrib_lib_mw__frame_tst {
private final Mock_scrib_fxt fxt = new Mock_scrib_fxt(); private Scrib_lib lib;
@Before public void init() {
fxt.Clear();
fxt.Init__mock_mod_system();
lib = fxt.Core().Lib_mw().Init();
fxt.Init__mock_mod(lib, "Module:Mod_1"
, fxt.Init__mock_fnc_for_lib("Get_frame_title_current", lib, Scrib_lib_mw.Invk_getFrameTitle, "current")
, fxt.Init__mock_fnc_for_lib("Get_frame_title_parent", lib, Scrib_lib_mw.Invk_getFrameTitle, "parent")
);
}
@Test public void GetFrameTitle__template() {
fxt.Init__page("Template:Template_1", "{{#invoke:Mod_1|Get_frame_title_{{{1}}}}}");
fxt.Test__parse__tmpl_to_html("{{Template_1|current}}", "Module:Mod 1");
fxt.Test__parse__tmpl_to_html("{{Template_1|parent}}" , "Template:Template 1");
}
@Test public void GetFrameTitle__transclude() {
fxt.Init__page("Page_1", "{{#invoke:Mod_1|Get_frame_title_parent}}");
fxt.Test__parse__tmpl_to_html("{{:Page_1}}", "Page 1");
fxt.Init__page("Page_2", "{{#invoke:Mod_1|Get_frame_title_current}}");
fxt.Test__parse__tmpl_to_html("{{:Page_2}}", "Module:Mod 1");
}
@Test public void GetFrameTitle__page() {
fxt.Test__parse__tmpl_to_html("{{#invoke:Mod_1|Get_frame_title_parent}}", "Test page");
fxt.Test__parse__tmpl_to_html("{{#invoke:Mod_1|Get_frame_title_current}}", "Module:Mod 1");
}
}