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

ParserFunctions: Cache ifexist result of common wiki in current wiki

This commit is contained in:
gnosygnu
2018-07-03 08:42:02 -04:00
parent eb9cca66ed
commit f85cf8ad77
4 changed files with 44 additions and 35 deletions

View File

@@ -19,63 +19,69 @@ import gplx.xowa.apps.wms.apis.*; import gplx.xowa.wikis.data.tbls.*;
import gplx.xowa.wikis.nss.*;
public class Pfunc_ifexist_mgr {
public boolean Exists(Xowe_wiki wiki, byte[] raw_bry) {
// validate ttl; return false if invalid
if (Bry_.Len_eq_0(raw_bry)) return false; // return early; NOTE: {{autolink}} can pass in "" (see test)
Xoa_ttl ttl = Xoa_ttl.Parse(wiki, raw_bry); if (ttl == null) return false;
// try to get from cache
byte exists = wiki.Cache_mgr().Ifexist_cache().Get_by_mem(ttl);
byte exists = wiki.Cache_mgr().Ifexist_cache().Get_by_cache(ttl);
if (exists == Bool_.Y_byte) return true;
else if (exists == Bool_.N_byte) return false;
byte[] page_db = ttl.Page_db(); // NOTE: must use Page_db; EX: {{#ifexist:File:Peter & Paul fortress in SPB 03.jpg|y|n}}
Xow_ns ns = ttl.Ns();
boolean rv = false;
switch (ns.Id()) {
case Xow_ns_.Tid__special: rv = true; break; // NOTE: some pages call for [[Special]]; always return true for now; DATE:2014-07-17
case Xow_ns_.Tid__media: rv = Find_by_ns__media(wiki, ns, page_db); break;
default: rv = Find_by_ns(wiki, ttl, ns, page_db); break;
// not in cache; do find
switch (ttl.Ns().Id()) {
case Xow_ns_.Tid__special: return true; // NOTE: some pages call for [[Special]]; always return true for now; DATE:2014-07-17
case Xow_ns_.Tid__media: return Find_by_ns__media(wiki, ttl);
default: return Find_by_ns__other(wiki, ttl);
}
return rv;
}
private boolean Find_by_ns(Xowe_wiki wiki, Xoa_ttl ttl, Xow_ns ns, byte[] ttl_bry) {
boolean rv = wiki.Cache_mgr().Ifexist_cache().Load(ttl);
private boolean Find_by_ns__other(Xowe_wiki wiki, Xoa_ttl ttl) {
boolean rv = wiki.Cache_mgr().Ifexist_cache().Get_by_load(ttl);
// handle variants
if ( !rv
&& wiki.Lang().Vnt_mgr().Enabled()) {
Xowd_page_itm page = wiki.Lang().Vnt_mgr().Convert_mgr().Convert_ttl(wiki, ns, ttl_bry);
Xowd_page_itm page = wiki.Lang().Vnt_mgr().Convert_mgr().Convert_ttl(wiki, ttl.Ns(), ttl.Page_db());
if (page != Xowd_page_itm.Null)
rv = page.Exists();
}
return rv;
}
private boolean Find_by_ns__media(Xowe_wiki wiki, Xow_ns ns, byte[] page_db) {
private boolean Find_by_ns__media(Xowe_wiki wiki, Xoa_ttl ttl) {
// rarely true, but check local wiki's [[File:]] table anyway
Xow_ns file_ns = wiki.Ns_mgr().Ns_file();
byte[] page_db = ttl.Page_db(); // NOTE: must use Page_db; also handles Media:A.svg -> File:A.svg; EX: {{#ifexist:File:Peter & Paul fortress in SPB 03.jpg|y|n}}
Xoa_ttl file_ttl = wiki.Ttl_parse(file_ns.Id(), page_db);
if (file_ttl == null) return false; // NOTE: must check for NPE: PAGE:es.w:Elecciones_presidenciales_de_Venezuela_de_1998 DATE:2017-09-04
byte exists = wiki.Cache_mgr().Ifexist_cache().Get_by_mem(file_ttl);
// check cache
byte exists = wiki.Cache_mgr().Ifexist_cache().Get_by_cache(file_ttl);
if (exists == Bool_.Y_byte) return true;
else if (exists == Bool_.N_byte) return false;
if (Find_by_ns(wiki, file_ttl, file_ns, page_db)) return true;
// call Find_by_ns_other which will call load, but with "File:" instead of "Media:"
if (Find_by_ns__other(wiki, file_ttl)) return true;
// check commons; either (a) commons_wiki directly; or (b) tdb's file_mgr; note that (b) is obsolete
Xowe_wiki commons_wiki = wiki.Appe().Wiki_mgr().Wiki_commons();
boolean env_is_testing = Env_.Mode_testing();
boolean rv = false;
if ( commons_wiki != null // null check
&& ( commons_wiki.Init_assert().Db_mgr().Tid() == gplx.xowa.wikis.dbs.Xodb_mgr_sql.Tid_sql // make sure tid=sql; tid=txt automatically created for online images; DATE:2014-09-21
|| env_is_testing
)
) {
file_ns = commons_wiki.Ns_mgr().Ns_file();
return Find_by_ns(commons_wiki, file_ttl, file_ns, page_db); // accurate test using page table in commons wiki (provided commons is up to date)
rv = Find_by_ns__other(commons_wiki, file_ttl); // accurate test using page table in commons wiki (provided commons is up to date)
}
else {
if (!env_is_testing)
wiki.File_mgr().Init_file_mgr_by_load(wiki); // NOTE: must init Fsdb_mgr (else conn == null), and with bin_wkrs (else no images will ever load); DATE:2014-09-21
return wiki.File_mgr().Exists(page_db); // less-accurate test using either (1) orig_wiki table in local wiki (v2) or (2) meta_db_mgr (v1)
wiki.File_mgr().Init_file_mgr_by_load(wiki); // NOTE: must init Fsdb_mgr (else conn == null), and with bin_wkrs (else no images will ever load); DATE:2014-09-21
rv = wiki.File_mgr().Exists(page_db); // less-accurate test using either (1) orig_wiki table in local wiki (v2) or (2) meta_db_mgr (v1)
}
// add commons result to this wiki's cache; needed b/c cache will have "false" value (b/c Page does not exist in wiki), but should be "true" (since it exists in commons) PAGE:en.w:Harstad DATE:2018-07-03
wiki.Cache_mgr().Ifexist_cache().Add(file_ttl, rv);
return rv;
}
}

View File

@@ -38,6 +38,7 @@ public class Pfunc_ifexist_tst {
Xowe_wiki commons_wiki = fxt.App().Wiki_mgr().Get_by_or_make(gplx.xowa.wikis.domains.Xow_domain_itm_.Bry__commons);
fxt.Init_page_create(commons_wiki, "File:A.png", "");
fxt.Test_parse_tmpl_str_test("{{#ifexist:Media:A.png|y|n}}", "{{test}}", "y");
fxt.Test_parse_tmpl_str_test("{{#ifexist:Media:A.png|y|n}}", "{{test}}", "y"); // BUG:2nd call actually ends up as n; PAGE:en.w:Harstad DATE:2018-07-03
}
@Test public void Media_y_file_v1() {// DATE:2014-07-04
Xof_meta_itm meta_itm = fxt.Wiki().File_mgr().Dbmeta_mgr().Get_itm_or_new(Bry_.new_a7("A.png"));