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

Source: Restore broken commit

This commit is contained in:
gnosygnu
2017-02-06 22:14:55 -05:00
parent 938beac9f9
commit 3bfeb94b43
4380 changed files with 328018 additions and 0 deletions

View File

@@ -0,0 +1,21 @@
/*
XOWA: the XOWA Offline Wiki Application
Copyright (C) 2012 gnosygnu@gmail.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package gplx.xowa.addons.parsers.mediawikis; import gplx.*; import gplx.xowa.*; import gplx.xowa.addons.*; import gplx.xowa.addons.parsers.*;
public interface Xop_mediawiki_loader {
String LoadWikitext(String page);
}

View File

@@ -0,0 +1,59 @@
/*
XOWA: the XOWA Offline Wiki Application
Copyright (C) 2012 gnosygnu@gmail.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package gplx.xowa.addons.parsers.mediawikis; import gplx.*; import gplx.xowa.*; import gplx.xowa.addons.*; import gplx.xowa.addons.parsers.*;
public class Xop_mediawiki_mgr {
private final Xoae_app app;
private boolean mode_is_prod;
public Xop_mediawiki_mgr(String root_str, boolean mode_is_prod) {
Gfo_usr_dlg usr_dlg = Xoa_app_.New__usr_dlg__console();
Gfo_usr_dlg_.Instance = usr_dlg;
Io_url root_dir = Io_url_.new_dir_(root_str);
this.mode_is_prod = mode_is_prod;
if (mode_is_prod) {
gplx.dbs.Db_conn_bldr.Instance.Reg_default_sqlite();
gplx.core.envs.Env_.Init_swt(String_.Ary_empty, Type_adp_.ClassOf_obj(this)); // must call Init else unit_testing will be true
}
this.app = new Xoae_app(usr_dlg, gplx.xowa.apps.Xoa_app_mode.Itm_cmd
, root_dir
, root_dir.GenSubDir("wiki")
, root_dir.GenSubDir("file")
, root_dir.GenSubDir("user")
, root_dir.GenSubDir_nest("user", "anonymous", "wiki")
, gplx.xowa.apps.boots.Xoa_cmd_arg_mgr.Bin_dir_name()
);
if (mode_is_prod) {
app.Init_by_app();
app.Stage_(gplx.xowa.apps.Xoa_stage_.Tid_launch); // must set to Launch, else wiki.init_needed will never be false; DATE:2017-01-26
}
}
public Xop_mediawiki_wkr Make(String domain_str) {return Make(domain_str, null);}
public Xop_mediawiki_wkr Make(String domain_str, Xop_mediawiki_loader loader) {
Xowe_wiki wiki = (Xowe_wiki)app.Wiki_mgr().Make(Bry_.new_u8(domain_str), app.Fsys_mgr().Wiki_dir());
if (mode_is_prod) {
wiki.Init_by_wiki();
wiki.File_mgr().Version_2_y_(); // must set to version_2 else video files will use old v1 Meta_code; DATE:2017-01-26
wiki.File_mgr().Fsdb_mode().Tid__v2__mp__y_(); // must set to mass_parse mode, else will use old v1 Meta_code for xfer_itm and url_bldr; DATE:2017-01-26
}
return new Xop_mediawiki_wkr(wiki, loader);
}
public static Xop_mediawiki_mgr New(String root_str) {
return new Xop_mediawiki_mgr(root_str, true);
}
}

View File

@@ -0,0 +1,70 @@
/*
XOWA: the XOWA Offline Wiki Application
Copyright (C) 2012 gnosygnu@gmail.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package gplx.xowa.addons.parsers.mediawikis; import gplx.*; import gplx.xowa.*; import gplx.xowa.addons.*; import gplx.xowa.addons.parsers.*;
import gplx.xowa.wikis.*; import gplx.xowa.parsers.*; import gplx.xowa.wikis.pages.*; import gplx.xowa.htmls.core.htmls.*;
import gplx.xowa.wikis.caches.*;
public class Xop_mediawiki_wkr {
private final Xowe_wiki wiki;
private final Bry_bfr tmp_bfr = Bry_bfr_.New();
public Xop_mediawiki_wkr(Xowe_wiki wiki, Xop_mediawiki_loader loader) {
this.wiki = wiki;
this.Loader_(loader);
}
public void Loader_(Xop_mediawiki_loader loader) {
if (loader != null)
wiki.Cache_mgr().Load_wkr_(new Xow_page_cache_wkr__embeddable(loader));
}
public void Free_memory() {
wiki.Cache_mgr().Tmpl_result_cache().Clear();
wiki.Cache_mgr().Free_mem__page();
wiki.Parser_mgr().Scrib().Core_term();
wiki.Appe().Wiki_mgr().Wdata_mgr().Clear();
}
public void Clear_cache(String page) {
Xoa_ttl ttl = wiki.Ttl_parse(Bry_.new_u8(page));
wiki.Cache_mgr().Page_cache().Del(ttl.Full_db());
}
public String Parse(String page, String wikitext) {
Xoa_ttl ttl = wiki.Ttl_parse(Bry_.new_u8(page));
byte[] wtxt = Bry_.new_u8(wikitext);
Xoae_page wpg = Xoae_page.New(wiki, ttl);
wpg.Db().Text().Text_bry_(wtxt);
Xow_parser_mgr parser_mgr = wiki.Parser_mgr();
// parse page
Xop_ctx pctx = parser_mgr.Ctx();
pctx.Clear_all();
parser_mgr.Parse(wpg, true);
// write to html
boolean is_wikitext = Xow_page_tid.Identify(wpg.Wiki().Domain_tid(), ttl.Ns().Id(), ttl.Page_db()) == Xow_page_tid.Tid_wikitext;
byte[] orig_bry = Bry_.Empty;
if (is_wikitext) {
wiki.Html_mgr().Page_wtr_mgr().Wkr(Xopg_page_.Tid_read).Write_hdump(tmp_bfr, pctx, Xoh_wtr_ctx.Hdump, wpg);
orig_bry = tmp_bfr.To_bry_and_clear();
wpg.Db().Html().Html_bry_(orig_bry);
}
else { // not wikitext; EX: pages in MediaWiki: ns; DATE:2016-09-12
wpg.Db().Html().Html_bry_(wpg.Db().Text().Text_bry());
}
return String_.new_u8(orig_bry);
}
}

View File

@@ -0,0 +1,56 @@
/*
XOWA: the XOWA Offline Wiki Application
Copyright (C) 2012 gnosygnu@gmail.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package gplx.xowa.addons.parsers.mediawikis; import gplx.*; import gplx.xowa.*; import gplx.xowa.addons.*; import gplx.xowa.addons.parsers.*;
import org.junit.*; import gplx.core.tests.*;
public class Xop_mediawiki_wkr__tst {
private final Xop_mediawiki_wkr__fxt fxt = new Xop_mediawiki_wkr__fxt();
@After public void term() {Gfo_usr_dlg_.Instance = Gfo_usr_dlg_.Noop;}
@Test public void Basic() {
fxt.Init__wkr("en.wikipedia.org", null);
fxt.Test__parse("Page_1", "''{{PAGENAME}}''"
, "<p><i>Page 1</i>"
, "</p>"
);
}
@Test public void Template() {
fxt.Init__wkr("en.wikipedia.org", new Xop_mediawiki_loader__mock());
fxt.Test__parse("Page_1", "{{bold}}"
, "<p><b>bold</b>"
, "</p>"
);
}
}
class Xop_mediawiki_wkr__fxt {
private final Xop_mediawiki_mgr mgr = new Xop_mediawiki_mgr("mem/xowa/wiki/en.wikipedia.org/", false);
private Xop_mediawiki_wkr wkr;
public Xop_mediawiki_wkr__fxt() {
gplx.dbs.Db_conn_bldr.Instance.Reg_default_mem();
}
public void Init__wkr(String wiki, Xop_mediawiki_loader cbk) {
this.wkr = mgr.Make(wiki, cbk);
}
public void Test__parse(String page, String wtxt, String... expd) {
Gftest.Eq__ary__lines(String_.Concat_lines_nl_skip_last(expd), wkr.Parse(page, wtxt), "parse failed; wtxt={0}", wtxt);
}
}
class Xop_mediawiki_loader__mock implements Xop_mediawiki_loader {
public String LoadWikitext(String page) {
if (String_.Eq(page, "Template:Bold")) return "'''bold'''";
else return "text";
}
}

View File

@@ -0,0 +1,27 @@
/*
XOWA: the XOWA Offline Wiki Application
Copyright (C) 2012 gnosygnu@gmail.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package gplx.xowa.addons.parsers.mediawikis; import gplx.*; import gplx.xowa.*; import gplx.xowa.addons.*; import gplx.xowa.addons.parsers.*;
class Xow_page_cache_wkr__embeddable implements gplx.xowa.wikis.caches.Xow_page_cache_wkr {
private final Xop_mediawiki_loader cbk;
public Xow_page_cache_wkr__embeddable(Xop_mediawiki_loader cbk) {
this.cbk = cbk;
}
public byte[] Get_page_or_null(byte[] full_db) {
return Bry_.new_u8(cbk.LoadWikitext(String_.new_u8(full_db)));
}
}