mirror of https://github.com/gnosygnu/xowa
Mass_parse: Add error_filter to ignore logging certain errors [#793]
parent
1b6324938c
commit
f19228c886
@ -1,168 +1,183 @@
|
|||||||
/*
|
/*
|
||||||
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.
|
||||||
|
|
||||||
You may use XOWA according to either of these licenses as is most appropriate
|
You may use XOWA according to either of these licenses as is most appropriate
|
||||||
for your project on a case-by-case basis.
|
for your project on a case-by-case basis.
|
||||||
|
|
||||||
The terms of each license can be found in the source code repository:
|
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; import gplx.*;
|
package gplx.xowa.xtns.scribunto;
|
||||||
import gplx.core.threads.Thread_adp;
|
|
||||||
import gplx.core.threads.Thread_adp_;
|
import gplx.Bry_;
|
||||||
import gplx.xowa.*; import gplx.xowa.xtns.*;
|
import gplx.Bry_bfr;
|
||||||
import gplx.core.brys.fmtrs.*; import gplx.core.envs.*;
|
import gplx.Err;
|
||||||
import gplx.langs.htmls.*;
|
import gplx.Err_;
|
||||||
import gplx.xowa.langs.kwds.*; import gplx.xowa.langs.msgs.*;
|
import gplx.GfoMsg;
|
||||||
import gplx.xowa.wikis.nss.*;
|
import gplx.Gfo_invk;
|
||||||
import gplx.xowa.htmls.*;
|
import gplx.GfsCtx;
|
||||||
import gplx.xowa.parsers.*; import gplx.xowa.parsers.logs.*; import gplx.xowa.parsers.tmpls.*;
|
import gplx.String_;
|
||||||
import gplx.xowa.xtns.pfuncs.*;
|
import gplx.core.brys.fmtrs.Bry_fmtr;
|
||||||
import gplx.xowa.xtns.scribunto.cfgs.ScribCfg;
|
import gplx.core.envs.System_;
|
||||||
import gplx.xowa.xtns.scribunto.cfgs.ScribCfgResolver;
|
import gplx.core.threads.Thread_adp;
|
||||||
|
import gplx.core.threads.Thread_adp_;
|
||||||
public class Scrib_invoke_func extends Pf_func_base {
|
import gplx.xowa.Xoa_ttl;
|
||||||
@Override public int Id() {return Xol_kwd_grp_.Id_invoke;}
|
import gplx.xowa.Xowe_wiki;
|
||||||
@Override public Pf_func New(int id, byte[] name) {return new Scrib_invoke_func().Name_(name);}
|
import gplx.xowa.langs.kwds.Xol_kwd_grp_;
|
||||||
@Override public void Func_evaluate(Bry_bfr bfr, Xop_ctx ctx, Xot_invk caller, Xot_invk self, byte[] src) {// {{#invoke:mod_name|prc_name|prc_args...}}
|
import gplx.xowa.langs.msgs.Xol_msg_itm_;
|
||||||
boolean stat_enabled = ctx.Page().Stat_itm().Enabled();
|
import gplx.xowa.langs.msgs.Xow_msg_mgr;
|
||||||
if (stat_enabled) ctx.Page().Stat_itm().Scrib().Bgn();
|
import gplx.xowa.parsers.Xop_ctx;
|
||||||
Xowe_wiki wiki = ctx.Wiki();
|
import gplx.xowa.parsers.logs.Xop_log_invoke_wkr;
|
||||||
byte[] mod_name = Eval_argx(ctx, src, caller, self);
|
import gplx.xowa.parsers.tmpls.Xot_invk;
|
||||||
if (Bry_.Len_eq_0(mod_name)) {Error(bfr, wiki.Msg_mgr(), Err_mod_missing); return;} // EX: "{{#invoke:}}"
|
import gplx.xowa.wikis.nss.Xow_ns;
|
||||||
int args_len = self.Args_len();
|
import gplx.xowa.wikis.nss.Xow_ns_;
|
||||||
byte[] fnc_name = Pf_func_.Eval_arg_or(ctx, src, caller, self, args_len, 0, null);
|
import gplx.xowa.xtns.pfuncs.Pf_func;
|
||||||
Xop_log_invoke_wkr invoke_wkr = ctx.Xtn__scribunto__invoke_wkr();
|
import gplx.xowa.xtns.pfuncs.Pf_func_;
|
||||||
long log_time_bgn = 0;
|
import gplx.xowa.xtns.pfuncs.Pf_func_base;
|
||||||
if (invoke_wkr != null) {
|
import gplx.xowa.xtns.scribunto.cfgs.ScribCfg;
|
||||||
log_time_bgn = System_.Ticks();
|
import gplx.xowa.xtns.scribunto.cfgs.ScribCfgResolver;
|
||||||
if (!invoke_wkr.Eval_bgn(ctx.Page(), mod_name, fnc_name)) return;
|
|
||||||
}
|
public class Scrib_invoke_func extends Pf_func_base {
|
||||||
Scrib_core core = wiki.Parser_mgr().Scrib().Core();
|
@Override public int Id() {return Xol_kwd_grp_.Id_invoke;}
|
||||||
if (core == null) {
|
@Override public Pf_func New(int id, byte[] name) {return new Scrib_invoke_func().Name_(name);}
|
||||||
synchronized (this) {
|
@Override public void Func_evaluate(Bry_bfr bfr, Xop_ctx ctx, Xot_invk caller, Xot_invk self, byte[] src) {// {{#invoke:mod_name|prc_name|prc_args...}}
|
||||||
core = wiki.Parser_mgr().Scrib().Core_init(ctx);
|
boolean stat_enabled = ctx.Page().Stat_itm().Enabled();
|
||||||
core.Init();
|
if (stat_enabled) ctx.Page().Stat_itm().Scrib().Bgn();
|
||||||
core.When_page_changed(ctx.Page());
|
Xowe_wiki wiki = ctx.Wiki();
|
||||||
}
|
byte[] mod_name = Eval_argx(ctx, src, caller, self);
|
||||||
}
|
if (Bry_.Len_eq_0(mod_name)) {Error(bfr, wiki.Msg_mgr(), Err_mod_missing); return;} // EX: "{{#invoke:}}"
|
||||||
byte[] mod_raw = null;
|
int args_len = self.Args_len();
|
||||||
Scrib_lua_mod mod = core.Mods_get(mod_name);
|
byte[] fnc_name = Pf_func_.Eval_arg_or(ctx, src, caller, self, args_len, 0, null);
|
||||||
if (mod == null) {
|
Xop_log_invoke_wkr invoke_wkr = ctx.Xtn__scribunto__invoke_wkr();
|
||||||
Xow_ns module_ns = wiki.Ns_mgr().Ids_get_or_null(Xow_ns_.Tid__module);
|
long log_time_bgn = 0;
|
||||||
Xoa_ttl mod_ttl = Xoa_ttl.Parse(wiki, Bry_.Add(module_ns.Name_db_w_colon(), mod_name));
|
if (invoke_wkr != null) {
|
||||||
mod_raw = wiki.Cache_mgr().Page_cache().Get_src_else_load_or_null(mod_ttl);
|
log_time_bgn = System_.Ticks();
|
||||||
if (mod_raw == null) {Error(bfr, wiki.Msg_mgr(), Err_mod_missing); return;} // EX: "{{#invoke:missing_mod}}"
|
if (!invoke_wkr.Eval_bgn(ctx.Page(), mod_name, fnc_name)) return;
|
||||||
}
|
}
|
||||||
else
|
Scrib_core core = wiki.Parser_mgr().Scrib().Core();
|
||||||
mod_raw = mod.Text_bry();
|
if (core == null) {
|
||||||
if (!core.Enabled()) {bfr.Add_mid(src, self.Src_bgn(), self.Src_end()); return;}
|
synchronized (this) {
|
||||||
|
core = wiki.Parser_mgr().Scrib().Core_init(ctx);
|
||||||
try {
|
core.Init();
|
||||||
// DBG: test code; ISSUE#:737
|
core.When_page_changed(ctx.Page());
|
||||||
// if (String_.Eq(String_.new_u8(mod_name), "Authority control")) {
|
}
|
||||||
// Tfds.Write(String_.new_u8(ctx.Page().Ttl().Page_db()), String_.new_u8(mod_name), String_.new_u8(fnc_name));
|
}
|
||||||
// }
|
byte[] mod_raw = null;
|
||||||
|
Scrib_lua_mod mod = core.Mods_get(mod_name);
|
||||||
// check if configured for threaded execution
|
if (mod == null) {
|
||||||
boolean exec = true;
|
Xow_ns module_ns = wiki.Ns_mgr().Ids_get_or_null(Xow_ns_.Tid__module);
|
||||||
ScribCfgResolver resolver = wiki.Parser_mgr().Scrib().CfgResolver();
|
Xoa_ttl mod_ttl = Xoa_ttl.Parse(wiki, Bry_.Add(module_ns.Name_db_w_colon(), mod_name));
|
||||||
if (resolver != null) {
|
mod_raw = wiki.Cache_mgr().Page_cache().Get_src_else_load_or_null(mod_ttl);
|
||||||
ScribCfg cfg = resolver.Resolve(ctx.Page().Ttl().Page_db(), Xoa_ttl.Replace_spaces(mod_name), fnc_name);
|
if (mod_raw == null) {Error(bfr, wiki.Msg_mgr(), Err_mod_missing); return;} // EX: "{{#invoke:missing_mod}}"
|
||||||
if (cfg != null) {
|
}
|
||||||
if (cfg.TimeoutInMs() != 0) {
|
else
|
||||||
exec = false;
|
mod_raw = mod.Text_bry();
|
||||||
int timeoutInMs = cfg.TimeoutInMs();
|
if (!core.Enabled()) {bfr.Add_mid(src, self.Src_bgn(), self.Src_end()); return;}
|
||||||
long timeBgn = System_.Ticks();
|
|
||||||
|
try {
|
||||||
InvokeInvoker invoker = new InvokeInvoker(core, wiki, ctx, src, caller, self, bfr, mod_name, mod_raw, fnc_name);
|
// DBG: test code; ISSUE#:737
|
||||||
Thread_adp thread = Thread_adp_.Start_by_key("scribunto", invoker, "default");
|
// if (String_.Eq(String_.new_u8(mod_name), "Authority control")) {
|
||||||
while (thread.Thread__is_alive()) {
|
// Tfds.Write(String_.new_u8(ctx.Page().Ttl().Page_db()), String_.new_u8(mod_name), String_.new_u8(fnc_name));
|
||||||
Thread_adp_.Sleep(cfg.SleepInMs());
|
// }
|
||||||
if (System_.Ticks__elapsed_in_frac(timeBgn) > timeoutInMs) {
|
|
||||||
thread.Thread__stop();
|
// check if configured for threaded execution
|
||||||
invoker.Exc = Err_.new_wo_type(String_.Format("scribunto timeout: page={0} mod={1} func={2} time={3}", ctx.Page_url_str(), mod_name, fnc_name, timeoutInMs));
|
boolean exec = true;
|
||||||
}
|
ScribCfgResolver resolver = wiki.Parser_mgr().Scrib().CfgResolver();
|
||||||
}
|
if (resolver != null) {
|
||||||
if (invoker.Exc != null) {
|
ScribCfg cfg = resolver.Resolve(ctx.Page().Ttl().Page_db(), Xoa_ttl.Replace_spaces(mod_name), fnc_name);
|
||||||
throw invoker.Exc;
|
if (cfg != null) {
|
||||||
}
|
if (cfg.TimeoutInMs() != 0) {
|
||||||
}
|
exec = false;
|
||||||
}
|
int timeoutInMs = cfg.TimeoutInMs();
|
||||||
}
|
long timeBgn = System_.Ticks();
|
||||||
// no threaded execution; run sequentially
|
|
||||||
if (exec) {
|
InvokeInvoker invoker = new InvokeInvoker(core, wiki, ctx, src, caller, self, bfr, mod_name, mod_raw, fnc_name);
|
||||||
core.Invoke(wiki, ctx, src, caller, self, bfr, mod_name, mod_raw, fnc_name);
|
Thread_adp thread = Thread_adp_.Start_by_key("scribunto", invoker, "default");
|
||||||
}
|
while (thread.Thread__is_alive()) {
|
||||||
if (invoke_wkr != null)
|
Thread_adp_.Sleep(cfg.SleepInMs());
|
||||||
invoke_wkr.Eval_end(ctx.Page(), mod_name, fnc_name, log_time_bgn);
|
if (System_.Ticks__elapsed_in_frac(timeBgn) > timeoutInMs) {
|
||||||
}
|
thread.Thread__stop();
|
||||||
catch (Throwable e) {
|
invoker.Exc = Err_.new_wo_type(String_.Format("scribunto timeout: page={0} mod={1} func={2} time={3}", ctx.Page_url_str(), mod_name, fnc_name, timeoutInMs));
|
||||||
Err err = Err_.Cast_or_make(e);
|
}
|
||||||
Error(bfr, wiki.Msg_mgr(), err);
|
}
|
||||||
Scrib_err_filter_mgr err_filter_mgr = invoke_wkr == null ? null : invoke_wkr.Err_filter_mgr();
|
if (invoker.Exc != null) {
|
||||||
if ( err_filter_mgr == null // no err_filter_mgr defined;
|
throw invoker.Exc;
|
||||||
|| err_filter_mgr.Count_eq_0() // err_filter_mgr exists, but no definitions
|
}
|
||||||
|| !err_filter_mgr.Match(String_.new_u8(mod_name), String_.new_u8(fnc_name), err.To_str__msg_only())) // NOTE: must be To_str__msg_only; err_filter_mgr has defintion and it doesn't match current; print warn; DATE:2015-07-24
|
}
|
||||||
ctx.App().Usr_dlg().Warn_many("", "", "invoke failed: ~{0} ~{1} ~{2}", ctx.Page().Ttl().Raw(), Bry_.Replace_nl_w_tab(src, self.Src_bgn(), self.Src_end()), err.To_str__log());
|
}
|
||||||
wiki.Parser_mgr().Scrib().Terminate_when_page_changes_y_(); // NOTE: terminate core when page changes; not terminating now, else page with many errors will be very slow due to multiple remakes of core; PAGE:th.d:all; DATE:2014-10-03
|
}
|
||||||
}
|
// no threaded execution; run sequentially
|
||||||
if (stat_enabled) ctx.Page().Stat_itm().Scrib().End();
|
if (exec) {
|
||||||
}
|
core.Invoke(wiki, ctx, src, caller, self, bfr, mod_name, mod_raw, fnc_name);
|
||||||
public static void Error(Bry_bfr bfr, Xow_msg_mgr msg_mgr, Err err) {Error(bfr, msg_mgr, Err_.Cast_or_make(err).To_str__top_wo_args());}// NOTE: must use "short" error message to show in wikitext; DATE:2015-07-27
|
}
|
||||||
public static void Error(Bry_bfr bfr, Xow_msg_mgr msg_mgr, String error) {
|
if (invoke_wkr != null)
|
||||||
// for Luaj, msg combines both err; split out traceback else error message will be very long; note that Warn_many will still log traceback; DATE:2016-09-09
|
invoke_wkr.Eval_end(ctx.Page(), mod_name, fnc_name, log_time_bgn);
|
||||||
String error_visible = error;
|
}
|
||||||
int traceback_pos = String_.FindFwd(error, "\nstack traceback:\n"); // NOTE: produced by LuaError.getMessage()
|
catch (Throwable e) {
|
||||||
if (traceback_pos != String_.Find_none)
|
Err err = Err_.Cast_or_make(e);
|
||||||
error_visible = String_.Mid(error_visible, 0, traceback_pos);
|
Error(bfr, wiki.Msg_mgr(), err);
|
||||||
|
Scrib_err_filter_mgr err_filter_mgr = Scrib_err_filter_mgr.INSTANCE;
|
||||||
// write "Script error: some error"
|
if ( err_filter_mgr.Empty() // err_filter_mgr exists, but no definitions
|
||||||
byte[] script_error_msg = msg_mgr.Val_by_id(Xol_msg_itm_.Id_scribunto_parser_error);
|
|| !err_filter_mgr.Match(String_.new_u8(mod_name), String_.new_u8(fnc_name), err.To_str__msg_only())) // NOTE: must be To_str__msg_only; err_filter_mgr has defintion and it doesn't match current; print warn; DATE:2015-07-24
|
||||||
error_fmtr.Bld_bfr_many(bfr, script_error_msg, error_visible);
|
ctx.App().Usr_dlg().Warn_many("", "", "invoke failed: ~{0} ~{1} ~{2}", ctx.Page().Ttl().Raw(), Bry_.Replace_nl_w_tab(src, self.Src_bgn(), self.Src_end()), err.To_str__log());
|
||||||
}
|
wiki.Parser_mgr().Scrib().Terminate_when_page_changes_y_(); // NOTE: terminate core when page changes; not terminating now, else page with many errors will be very slow due to multiple remakes of core; PAGE:th.d:all; DATE:2014-10-03
|
||||||
private static final Bry_fmtr error_fmtr = Bry_fmtr.new_("<strong class=\"error\"><span class=\"scribunto-error\" id=\"mw-scribunto-error-0\">~{0}: ~{1}</span></strong>"); // NOTE: must be "error" not 'error'; iferror checks for quote not apos; DATE:2015-09-17
|
}
|
||||||
public static final String Err_mod_missing = "No such module";
|
if (stat_enabled) ctx.Page().Stat_itm().Scrib().End();
|
||||||
|
}
|
||||||
class InvokeInvoker implements Gfo_invk {
|
public static void Error(Bry_bfr bfr, Xow_msg_mgr msg_mgr, Err err) {Error(bfr, msg_mgr, Err_.Cast_or_make(err).To_str__top_wo_args());}// NOTE: must use "short" error message to show in wikitext; DATE:2015-07-27
|
||||||
private final Scrib_core core;
|
public static void Error(Bry_bfr bfr, Xow_msg_mgr msg_mgr, String error) {
|
||||||
private final Xowe_wiki wiki;
|
// for Luaj, msg combines both err; split out traceback else error message will be very long; note that Warn_many will still log traceback; DATE:2016-09-09
|
||||||
private final Xop_ctx ctx;
|
String error_visible = error;
|
||||||
private final byte[] src;
|
int traceback_pos = String_.FindFwd(error, "\nstack traceback:\n"); // NOTE: produced by LuaError.getMessage()
|
||||||
private final Xot_invk caller;
|
if (traceback_pos != String_.Find_none)
|
||||||
private final Xot_invk self;
|
error_visible = String_.Mid(error_visible, 0, traceback_pos);
|
||||||
private final Bry_bfr bfr;
|
|
||||||
private final byte[] mod_name;
|
// write "Script error: some error"
|
||||||
private final byte[] mod_raw;
|
byte[] script_error_msg = msg_mgr.Val_by_id(Xol_msg_itm_.Id_scribunto_parser_error);
|
||||||
private final byte[] fnc_name;
|
error_fmtr.Bld_bfr_many(bfr, script_error_msg, error_visible);
|
||||||
|
}
|
||||||
public InvokeInvoker(Scrib_core core, Xowe_wiki wiki, Xop_ctx ctx, byte[] src, Xot_invk caller, Xot_invk self, Bry_bfr bfr, byte[] mod_name, byte[] mod_raw, byte[] fnc_name) {
|
private static final Bry_fmtr error_fmtr = Bry_fmtr.new_("<strong class=\"error\"><span class=\"scribunto-error\" id=\"mw-scribunto-error-0\">~{0}: ~{1}</span></strong>"); // NOTE: must be "error" not 'error'; iferror checks for quote not apos; DATE:2015-09-17
|
||||||
this.core = core;
|
public static final String Err_mod_missing = "No such module";
|
||||||
this.wiki = wiki;
|
|
||||||
this.ctx = ctx;
|
class InvokeInvoker implements Gfo_invk {
|
||||||
this.src = src;
|
private final Scrib_core core;
|
||||||
this.caller = caller;
|
private final Xowe_wiki wiki;
|
||||||
this.self = self;
|
private final Xop_ctx ctx;
|
||||||
this.bfr = bfr;
|
private final byte[] src;
|
||||||
this.mod_name = mod_name;
|
private final Xot_invk caller;
|
||||||
this.mod_raw = mod_raw;
|
private final Xot_invk self;
|
||||||
this.fnc_name = fnc_name;
|
private final Bry_bfr bfr;
|
||||||
}
|
private final byte[] mod_name;
|
||||||
public Exception Exc;
|
private final byte[] mod_raw;
|
||||||
public Object Invk(GfsCtx gctx, int ikey, String k, GfoMsg m) {
|
private final byte[] fnc_name;
|
||||||
try {
|
|
||||||
core.Invoke(wiki, ctx, src, caller, self, bfr, mod_name, mod_raw, fnc_name);
|
public InvokeInvoker(Scrib_core core, Xowe_wiki wiki, Xop_ctx ctx, byte[] src, Xot_invk caller, Xot_invk self, Bry_bfr bfr, byte[] mod_name, byte[] mod_raw, byte[] fnc_name) {
|
||||||
}
|
this.core = core;
|
||||||
catch (Exception exc) {
|
this.wiki = wiki;
|
||||||
this.Exc = exc;
|
this.ctx = ctx;
|
||||||
}
|
this.src = src;
|
||||||
return null;
|
this.caller = caller;
|
||||||
}
|
this.self = self;
|
||||||
}
|
this.bfr = bfr;
|
||||||
}
|
this.mod_name = mod_name;
|
||||||
|
this.mod_raw = mod_raw;
|
||||||
|
this.fnc_name = fnc_name;
|
||||||
|
}
|
||||||
|
public Exception Exc;
|
||||||
|
public Object Invk(GfsCtx gctx, int ikey, String k, GfoMsg m) {
|
||||||
|
try {
|
||||||
|
core.Invoke(wiki, ctx, src, caller, self, bfr, mod_name, mod_raw, fnc_name);
|
||||||
|
}
|
||||||
|
catch (Exception exc) {
|
||||||
|
this.Exc = exc;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in new issue