1
0
mirror of https://github.com/gnosygnu/xowa.git synced 2026-03-02 03:49:30 +00:00
This commit is contained in:
gnosygnu
2015-09-13 21:54:44 -04:00
parent 2145f6382c
commit 5fe27b5b3b
649 changed files with 4726 additions and 3432 deletions

View File

@@ -0,0 +1,97 @@
/*
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.wikis.data; import gplx.*; import gplx.xowa.*; import gplx.xowa.wikis.*;
import gplx.lists.*; /*ComparerAble*/ import gplx.xowa.bldrs.cmds.ctgs.*;
import gplx.xowa.dbs.*; import gplx.xowa.wikis.*; import gplx.xowa.langs.msgs.*;
import gplx.xowa.parsers.utils.*;
import gplx.xowa.wikis.data.tbls.*;
public class Xow_data_mgr implements GfoInvkAble {
private Xop_redirect_mgr redirect_mgr;
private Xoa_url tmp_url = Xoa_url.blank();
public Xow_data_mgr(Xowe_wiki wiki) {this.wiki = wiki; this.redirect_mgr = wiki.Redirect_mgr();}
public Xowe_wiki Wiki() {return wiki;} private Xowe_wiki wiki;
public boolean Version_is_1() {return Bool_.Y;}
public Xoae_page Get_page(Xoa_ttl ttl, boolean called_from_tmpl) {tmp_url = wiki.Utl__url_parser().Parse(ttl.Raw()); return Get_page(tmp_url, ttl, called_from_tmpl, false);}
public Xoae_page Get_page_from_msg(Xoa_ttl ttl) {tmp_url = wiki.Utl__url_parser().Parse(ttl.Raw()); return Get_page(tmp_url, ttl, false, true);}
public Xoae_page Get_page(Xoa_url url, Xoa_ttl ttl, boolean called_from_tmpl, boolean called_from_msg) {
Xoae_page rv = Xoae_page.new_(wiki, ttl);
return Get_page(rv, url, ttl, called_from_tmpl, called_from_msg);
}
public Xoae_page Get_page(Xoae_page rv, Xoa_url url, Xoa_ttl ttl, boolean called_from_tmpl, boolean called_from_msg) {
rv.Url_(url); // NOTE: must update page.Url(); should combine with Xoae_page.new_()
Xow_ns ns = ttl.Ns();
switch (ns.Id()) {
case Xow_ns_.Id_special:
wiki.Special_mgr().Special_gen(wiki, rv, url, ttl);
return rv;
case Xow_ns_.Id_mediawiki:
if ( !called_from_msg // if called from msg, fall through to actual data retrieval below, else infinite loop; DATE:2014-05-09
&& Xow_page_tid.Identify_by_ttl(ttl.Page_db()) == Xow_page_tid.Tid_wikitext // skip ".js" and ".css" pages in MediaWiki; DATE:2014-06-13
) {
Xol_lang lang = wiki.Lang();
byte[] msg_key = ttl.Page_db();
Bry_bfr tmp_bfr = wiki.Utl__bfr_mkr().Get_b512();
msg_key = lang.Case_mgr().Case_build_1st_lower(tmp_bfr, msg_key, 0, msg_key.length);
byte[] msg_val = Xol_msg_mgr_.Get_msg_itm(tmp_bfr, wiki, wiki.Lang(), msg_key).Val(); // NOTE: do not change to Get_msg_val; Get_msg_val, also replaces $1 with values, and $1 needs to be preserved for callers;
rv.Data_raw_(msg_val);
tmp_bfr.Mkr_rls();
return rv;
}
break;
}
return Get_page(rv, ns, ttl, called_from_tmpl, url.Qargs_mgr().Match(Xoa_url_.Qarg__redirect,Xoa_url_.Qarg__redirect__yes));
}
public Xoae_page Get_page(Xoae_page rv, Xow_ns ns, Xoa_ttl ttl, boolean called_from_tmpl, boolean redirect_force) {
int redirects = 0;
Xowd_page_itm db_page = Xowd_page_itm.new_tmp();
while (true) {
boolean exists = wiki.Db_mgr().Load_mgr().Load_by_ttl(db_page, ns, ttl.Page_db());
if (!exists) return rv.Missing_();
if (wiki.App().App_type().Uid_is_gui()) // NOTE: must check if gui, else will write during mass build; DATE:2014-05-03
wiki.Appe().Usr_dlg().Prog_many(GRP_KEY, "file_load", "loading page for ~{0}", String_.new_u8(ttl.Raw()));
wiki.Db_mgr().Load_mgr().Load_page(db_page, ns, !called_from_tmpl);
byte[] bry = db_page.Text();
rv.Data_raw_(bry).Revision_data().Modified_on_(db_page.Modified_on()).Id_(db_page.Id()).Html_db_id_(db_page.Html_db_id());
if (redirect_force) return rv;
Xoa_ttl redirect_ttl = redirect_mgr.Extract_redirect(bry, bry.length);
if ( redirect_ttl == null // not a redirect
|| redirects++ > 4) // too many redirects; something went wrong
break;
rv.Redirected_ttls().Add(ttl.Full_url()); // NOTE: must be url_encoded; EX: "en.wikipedia.org/?!" should generate link of "en.wikipedia.org/%3F!?redirect=no"
if (rv.Redirected_src() == null) rv.Redirected_src_(bry); // only add src for first redirect; DATE:2014-07-11
rv.Ttl_(redirect_ttl);
ns = redirect_ttl.Ns();
ttl = redirect_ttl;
}
return rv;
}
public Xoae_page Redirect(Xoae_page page, byte[] page_bry) {
Xoa_ttl trg_ttl = Xoa_ttl.parse(wiki, page_bry);
Xoa_url trg_url = Xoa_url.new_(wiki.Domain_bry(), page_bry);
page.Ttl_(trg_ttl).Url_(trg_url).Redirected_(true);
return wiki.Data_mgr().Get_page(page, trg_ttl.Ns(), trg_ttl, false, trg_url.Qargs_mgr().Match(Xoa_url_.Qarg__redirect, Xoa_url_.Qarg__redirect__yes));
}
public static final int File_idx_unknown = -1;
static final String GRP_KEY = "xowa.wiki.data";
public Object Invk(GfsCtx ctx, int ikey, String k, GfoMsg m) {
if (ctx.Match(k, Invk_create_enabled_)) wiki.Db_mgr().Save_mgr().Create_enabled_(m.ReadYn("v"));
else if (ctx.Match(k, Invk_update_modified_on_enabled_)) wiki.Db_mgr().Save_mgr().Update_modified_on_enabled_(m.ReadYn("v"));
else return GfoInvkAble_.Rv_unhandled;
return this;
} private static final String Invk_create_enabled_ = "create_enabled_", Invk_update_modified_on_enabled_ = "update_modified_on_enabled_";
}

View File

@@ -0,0 +1,163 @@
/*
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.wikis.data; import gplx.*; import gplx.xowa.*; import gplx.xowa.wikis.*;
import org.junit.*; import gplx.xowa.tdbs.*;
public class Xow_data_mgr_tst {
Xow_data_mgr_fxt fxt = new Xow_data_mgr_fxt();
@Before public void init() {fxt.Clear(); Tfds.Now_enabled_y_();}
@After public void term() {Tfds.Now_enabled_n_();}
@Test public void Create() {
fxt .Create("A1", "A1 data")
.Create("B12", "B12 data")
.Create("C123", "C123 data")
.Tst_regy_title("0|A1|C123|3\n")
.Tst_data_title(String_.Concat_lines_nl
( "!!!!>|!!!!?|!!!!@|"
, "!!!!!|!!!!!|!!!!!|0|!!!!(|A1"
, "!!!!\"|!!!!!|!!!!\"|0|!!!!)|B12"
, "!!!!#|!!!!!|!!!!#|0|!!!!*|C123"
))
.Tst_data_page(String_.Concat_lines_nl
( "!!!!9|!!!!;|!!!!=|"
, "!!!!!\t##PX+\tA1\tA1 data\t"
, "!!!!\"\t##PX/\tB12\tB12 data\t"
, "!!!!#\t##PX0\tC123\tC123 data\t"
))
;
}
@Test public void Update() {
fxt .Create("A1", "A1 data")
.Create("B12", "B12 data")
.Create("C123", "C123 data")
.Update("B12", "B12 changed")
.Tst_regy_title("0|A1|C123|3\n")
.Tst_data_title(String_.Concat_lines_nl
( "!!!!>|!!!!?|!!!!@|"
, "!!!!!|!!!!!|!!!!!|0|!!!!(|A1"
, "!!!!\"|!!!!!|!!!!\"|0|!!!!,|B12"
, "!!!!#|!!!!!|!!!!#|0|!!!!*|C123"
))
.Tst_data_page(String_.Concat_lines_nl
( "!!!!9|!!!!>|!!!!=|"
, "!!!!!\t##PX+\tA1\tA1 data\t"
, "!!!!\"\t##PX/\tB12\tB12 changed\t"
, "!!!!#\t##PX0\tC123\tC123 data\t"
))
;
}
@Test public void Update_zip() {
// fxt.Wiki().Fsys_mgr().Dir_regy()[Xow_ns_.Id_main].Ext_tid_(gplx.ios.Io_stream_.Tid_zip);
// fxt.Wiki().Data_mgr().Zip_mgr_(new Io_zip_mgr_mok());
// fxt .Create("A1", "A1 data")
// .Create("B12", "B12 data")
// .Create("C123", "C123 data")
// .Update("B12", "B12 changed")
// .Tst_regy_title("0|A1|C123|3\n")
// .Tst_data_title(String_.Concat_lines_nl
// ( "!!!!>|!!!!?|!!!!@|"
// , "!!!!!|!!!!!|!!!!!|0|!!!!(|A1"
// , "!!!!\"|!!!!!|!!!!\"|0|!!!!,|B12"
// , "!!!!#|!!!!!|!!!!#|0|!!!!*|C123"
// ))
// .Tst_data_page(String_.Concat_lines_nl
// ( "zipped:!!!!9|!!!!>|!!!!=|"
// , "!!!!!\t##PX+\tA1\tA1 data\t"
// , "!!!!\"\t##PX/\tB12\tB12 changed\t"
// , "!!!!#\t##PX0\tC123\tC123 data\t"
// ))
// ;
}
@Test public void Create_out_of_order() {
fxt .Create("C123", "C123 data")
.Create("B12", "B12 data")
.Create("A1", "A1 data")
.Tst_regy_title("0|A1|C123|3\n")
.Tst_data_title(String_.Concat_lines_nl
( "!!!!>|!!!!?|!!!!@|"
, "!!!!#|!!!!!|!!!!#|0|!!!!(|A1"
, "!!!!\"|!!!!!|!!!!\"|0|!!!!)|B12"
, "!!!!!|!!!!!|!!!!!|0|!!!!*|C123"
))
.Tst_data_page(String_.Concat_lines_nl
( "!!!!=|!!!!;|!!!!9|"
, "!!!!!\t##PX+\tC123\tC123 data\t"
, "!!!!\"\t##PX/\tB12\tB12 data\t"
, "!!!!#\t##PX0\tA1\tA1 data\t"
))
;
}
@Test public void Rename() {
fxt .Create("A1", "A1 data")
.Create("B12", "B12 data")
.Create("C123", "C123 data")
.Rename("C123", "C1234")
.Tst_regy_title("0|A1|C123|3\n")
.Tst_data_title(String_.Concat_lines_nl
( "!!!!>|!!!!?|!!!!@|"
, "!!!!!|!!!!!|!!!!!|0|!!!!(|A1"
, "!!!!\"|!!!!!|!!!!\"|0|!!!!)|B12"
, "!!!!#|!!!!!|!!!!#|0|!!!!*|C123"
))
.Tst_data_page(String_.Concat_lines_nl
( "!!!!9|!!!!;|!!!!=|"
, "!!!!!\t##PX+\tA1\tA1 data\t"
, "!!!!\"\t##PX/\tB12\tB12 data\t"
, "!!!!#\t##PX0\tC123\tC123 data\t"
))
;
}
}
class Xow_data_mgr_fxt {
Xoae_app app;
public Xowe_wiki Wiki() {return wiki;} private Xowe_wiki wiki;
public void Clear() {
app = Xoa_app_fxt.app_();
wiki = Xoa_app_fxt.wiki_tst_(app);
wiki.Db_mgr().Save_mgr().Page_id_next_(0);
}
public Xow_data_mgr_fxt Create(String ttl_str, String data) {
Xoa_ttl ttl = Xoa_ttl.parse(wiki, Bry_.new_u8(ttl_str));
wiki.Db_mgr().Save_mgr().Data_create(ttl, Bry_.new_u8(data));
return this;
}
public Xow_data_mgr_fxt Update(String ttl_str, String data) {
Xoa_ttl ttl = Xoa_ttl.parse(wiki, Bry_.new_u8(ttl_str));
Xoae_page page = Xoae_page.test_(wiki, ttl);
wiki.Db_mgr().Save_mgr().Data_update(page, Bry_.new_u8(data));
return this;
}
public Xow_data_mgr_fxt Rename(String old_ttl, String new_ttl) {
Xoa_ttl ttl = Xoa_ttl.parse(wiki, Bry_.new_u8(old_ttl));
Xoae_page page = Xoae_page.test_(wiki, ttl);
wiki.Db_mgr().Save_mgr().Data_rename(page, ttl.Ns().Id(), Bry_.new_u8(new_ttl));
return this;
}
public Xow_data_mgr_fxt Tst_regy_title(String expd) {return Tst_regy(Xotdb_dir_info_.Name_title, expd);}
Xow_data_mgr_fxt Tst_regy(String name, String expd) {
Io_url file_orig = Io_url_.mem_fil_("mem/xowa/wiki/en.wikipedia.org/ns/000/" + name + "/reg.csv");
Tfds.Eq_str_lines(expd, Io_mgr.I.LoadFilStr(file_orig));
return this;
}
public Xow_data_mgr_fxt Tst_data_page(String expd) {return Tst_data(Xotdb_dir_info_.Tid_page , Xow_ns_.Id_main, 0, expd);}
public Xow_data_mgr_fxt Tst_data_title(String expd) {return Tst_data(Xotdb_dir_info_.Tid_ttl, Xow_ns_.Id_main, 0, expd);}
public Xow_data_mgr_fxt Tst_data(byte dir_tid, int ns_id, int fil, String expd) {
Io_url url = wiki.Tdb_fsys_mgr().Url_ns_fil(dir_tid, ns_id, fil);
Tfds.Eq_str_lines(expd, Io_mgr.I.LoadFilStr(url));
return this;
}
}

View File

@@ -17,7 +17,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package gplx.xowa.wikis.data.tbls; import gplx.*; import gplx.xowa.*; import gplx.xowa.wikis.*; import gplx.xowa.wikis.data.*;
import gplx.core.primitives.*;
public class Xowd_page_itm implements Xobl_data_itm {
public class Xowd_page_itm {
public Xowd_page_itm() {this.Clear();}
public int Id() {return id;} public Xowd_page_itm Id_(int v) {id = v; id_val = null; return this;} private int id;
public Int_obj_val Id_val() {if (id_val == null) id_val = Int_obj_val.new_(id); return id_val;} private Int_obj_val id_val;