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-07-12 21:10:02 -04:00
commit 794b5a232f
3099 changed files with 238212 additions and 0 deletions

View File

@@ -0,0 +1,88 @@
/*
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.bldrs.wiki_cfgs; import gplx.*; import gplx.xowa.*; import gplx.xowa.bldrs.*;
import gplx.php.*; import gplx.xowa.bldrs.langs.*;
class Xob_subpage_parser {
public Xob_subpage_wiki[] Parse(byte[] src) {
src = Bry_.Add(Bry_.new_a7("$a = array("), src, Bry_.new_a7(");"));
List_adp wikis_list = List_adp_.new_();
try {
Php_parser php_parser = new Php_parser();
Php_evaluator eval = new Php_evaluator(new Gfo_msg_log("test"));
php_parser.Parse_tkns(src, eval);
Php_line[] lines = (Php_line[])eval.List().To_ary(Php_line.class);
Php_line_assign line = (Php_line_assign)lines[0];
Php_itm_ary root_ary = (Php_itm_ary)line.Val();
Php_itm_kv root_kv = (Php_itm_kv)root_ary.Subs_get(0);
Php_itm_ary wiki_tkns = (Php_itm_ary)root_kv.Val();
int wiki_tkns_len = wiki_tkns.Subs_len();
for (int i = 0; i < wiki_tkns_len; i++) {
Xob_subpage_wiki wiki_itm = new Xob_subpage_wiki();
Php_itm_kv wiki_tkn = (Php_itm_kv)wiki_tkns.Subs_get(i);
Parse_wiki(wiki_tkn, wiki_itm);
wikis_list.Add(wiki_itm);
}
}
catch (Exception e) {
throw Exc_.new_exc(e, "xo", "parse failed", "src", String_.new_u8(src));
}
return (Xob_subpage_wiki[])wikis_list.To_ary(Xob_subpage_wiki.class);
}
private void Parse_wiki(Php_itm_kv wiki_tkn, Xob_subpage_wiki wiki_itm) {
wiki_itm.Name_(wiki_tkn.Key().Val_obj_bry());
Php_itm_ary ns_ary_tkns = (Php_itm_ary)wiki_tkn.Val();
int ns_ary_tkns_len = ns_ary_tkns.Subs_len();
for (int i = 0; i < ns_ary_tkns_len; i++) {
Php_itm_kv ns_tkn = (Php_itm_kv)ns_ary_tkns.Subs_get(i);
Xob_subpage_ns ns_itm = new Xob_subpage_ns();
ns_itm.Id_(Parse_ns_id(ns_tkn.Key()));
ns_itm.Enabled_(Parse_ns_enabled(ns_tkn.Val()));
wiki_itm.Ns_list().Add(ns_itm);
}
}
private int Parse_ns_id(Php_itm itm) {
switch (itm.Itm_tid()) {
case Php_itm_.Tid_int:
return ((Php_itm_int)itm).Val_obj_int();
case Php_itm_.Tid_var:
return Xol_mw_lang_parser.Id_by_mw_name(((Php_itm)itm).Val_obj_bry());
default:
throw Exc_.new_unhandled(itm.Itm_tid());
}
}
private boolean Parse_ns_enabled(Php_itm itm) {
switch (itm.Itm_tid()) {
case Php_itm_.Tid_int:
return ((Php_itm_int)itm).Val_obj_int() == Bool_.Y_int;
case Php_itm_.Tid_bool_false:
return false;
case Php_itm_.Tid_bool_true:
return true;
default:
throw Exc_.new_unhandled(itm.Itm_tid());
}
}
}
class Xob_subpage_ns {
public int Id() {return id;} public Xob_subpage_ns Id_(int v) {id = v; return this;} private int id;
public boolean Enabled() {return enabled;} public Xob_subpage_ns Enabled_(boolean v) {enabled = v; return this;} private boolean enabled;
}
class Xob_subpage_wiki {
public byte[] Name() {return name;} public Xob_subpage_wiki Name_(byte[] v) {this.name = v; return this;} private byte[] name;
public List_adp Ns_list() {return ns_list;} private List_adp ns_list = List_adp_.new_();
}

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.bldrs.wiki_cfgs; import gplx.*; import gplx.xowa.*; import gplx.xowa.bldrs.*;
public class Xoi_wiki_props_alias {
public int Id() {return id;} private int id;
public String Alias() {return alias;} private String alias;
public Xoi_wiki_props_alias Init_by_ctor(int id, String alias) {this.id = id; this.alias = alias; return this;}
public void Init_by_xml(gplx.xmls.XmlNde ns_nde) {
this.id = Int_.parse_(ns_nde.Atrs().FetchValOr("id", "-1"));
this.alias = String_.Replace(String_.Replace(ns_nde.Text_inner(), " ", "_"), "'", "''");
}
}

View File

@@ -0,0 +1,86 @@
/*
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.bldrs.wiki_cfgs; import gplx.*; import gplx.xowa.*; import gplx.xowa.bldrs.*;
import gplx.xmls.*; import gplx.ios.*;
public class Xoi_wiki_props_api {
private IoEngine_xrg_downloadFil download_args = IoEngine_xrg_downloadFil.new_("", Io_url_.Empty);
public String Api_src(String wiki_domain) {
return String_.Concat("https://", wiki_domain, "/w/api.php?action=query&format=xml&meta=siteinfo&siprop=namespacealiases|namespaces");
}
public byte[] Exec_api(String src) {
return download_args.Exec_as_bry(src);
}
public void Build_cfg(Bry_bfr bfr, Xoi_wiki_props_wiki wiki) {
bfr.Add_str_a7("app.bldr.wiki_cfg_bldr.get('").Add(wiki.Wiki_domain()).Add_str("').new_cmd_('wiki.ns_mgr.aliases', 'ns_mgr.add_alias_bulk(\"\n");
int len = 0;
len = wiki.Alias_ary().length;
for (int i = 0; i < len; i++) {
Xoi_wiki_props_alias alias = wiki.Alias_ary()[i];
bfr.Add_int_variable(alias.Id()).Add_byte_pipe().Add_str(alias.Alias()).Add_byte_nl();
}
bfr.Add_str_a7("\");');\n");
bfr.Add_str_a7("app.bldr.wiki_cfg_bldr.get('").Add(wiki.Wiki_domain()).Add_str("').new_cmd_('wiki.ns_mgr.subpages', \"");
len = wiki.Ns_ary().length;
boolean first = true;
for (int i = 0; i < len; i++) {
Xoi_wiki_props_ns ns = wiki.Ns_ary()[i];
if (ns.Subpages_enabled()) {
if (first) {
first = false;
}
else
bfr.Add_byte_nl();
bfr.Add_str_a7("ns_mgr.get_by_id_or_new(").Add_int_variable(ns.Id()).Add_str(").subpages_enabled_('y');");
}
}
bfr.Add_str_a7("\");\n");
bfr.Add_byte_nl();
}
public void Parse(Xoi_wiki_props_wiki wiki, String xml) {
XmlDoc xdoc = XmlDoc_.parse_(xml);
XmlNde query_xnde = Xpath_.SelectFirst(xdoc.Root(), "query");
XmlNde aliases_xnde = Xpath_.SelectFirst(query_xnde, "namespace"+"aliases");
wiki.Alias_ary_(Parse_alias_ary(aliases_xnde));
XmlNde ns_xnde = Xpath_.SelectFirst(query_xnde, "namespace"+"s");
wiki.Ns_ary_(Parse_ns_ary(ns_xnde));
}
private Xoi_wiki_props_alias[] Parse_alias_ary(XmlNde xnde) {
int xndes_len = xnde.SubNdes().Count();
List_adp list = List_adp_.new_();
for (int i = 0; i < xndes_len; i++) {
XmlNde sub_nde = xnde.SubNdes().Get_at(i);
if (!String_.Eq(sub_nde.Name(), "ns")) continue;
Xoi_wiki_props_alias sub_itm = new Xoi_wiki_props_alias();
sub_itm.Init_by_xml(sub_nde);
list.Add(sub_itm);
}
return (Xoi_wiki_props_alias[])list.To_ary_and_clear(Xoi_wiki_props_alias.class);
}
private Xoi_wiki_props_ns[] Parse_ns_ary(XmlNde xnde) {
int xndes_len = xnde.SubNdes().Count();
List_adp list = List_adp_.new_();
for (int i = 0; i < xndes_len; i++) {
XmlNde sub_nde = xnde.SubNdes().Get_at(i);
if (!String_.Eq(sub_nde.Name(), "ns")) continue;
Xoi_wiki_props_ns sub_itm = new Xoi_wiki_props_ns();
sub_itm.Init_by_xml(sub_nde);
list.Add(sub_itm);
}
return (Xoi_wiki_props_ns[])list.To_ary_and_clear(Xoi_wiki_props_ns.class);
}
}

View File

@@ -0,0 +1,142 @@
/*
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.bldrs.wiki_cfgs; import gplx.*; import gplx.xowa.*; import gplx.xowa.bldrs.*;
import org.junit.*; import gplx.core.strings.*;
import gplx.xmls.*;
import gplx.xowa.bldrs.cfgs.*;
public class Xoi_wiki_props_api_tst {
private Xoi_wiki_props_fxt fxt = new Xoi_wiki_props_fxt();
@Before public void init() {} // private Xob_subpage_tst_fxt fxt = new] Xob_subpage_tst_fxt();
@Test public void Parse() {
fxt.Test_parse(String_.Concat_lines_nl
( "<api>"
, "<query>"
, " <namespacealiases>"
, " <ns id=\"4\" xml:space=\"preserve\">WP</ns>"
, " <ns id=\"5\" xml:space=\"preserve\">WT</ns>"
, " </namespacealiases>"
, " <namespaces>"
, " <ns id=\"0\" case=\"first-letter\" content=\"\" xml:space=\"preserve\"/>"
, " <ns id=\"1\" case=\"first-letter\" subpages=\"\" canonical=\"Talk\" xml:space=\"preserve\">Talk</ns>"
, " </namespaces>"
, "</query>"
, "</api>"
), fxt.wiki_()
.Alias_ary_(fxt.alias_(4, "WP"), fxt.alias_(5, "WT"))
.Ns_ary_(fxt.ns_(0, false), fxt.ns_(1, true))
);
}
// @Test public void Build() {
// fxt.Test_build(fxt.wiki_("enwiki")
// .Alias_ary_(fxt.alias_(4, "WP"), fxt.alias_(5, "WT"))
// .Ns_ary_(fxt.ns_(0, false), fxt.ns_(1, true))
// , "");
// }
// Tfds.Eq_str_lines(Query_ns(protocol, gplx.ios.IoEngine_.MemKey, wikis), String_.Concat_lines_nl
// ( "app.bldr.wiki_cfg_bldr.get('en.wikipedia.org').new_cmd_('wiki.ns_mgr.aliases', 'ns_mgr.add_alias_bulk(\""
// , "4|WP"
// , "5|WT"
// , "6|Image"
// , "7|Image_talk"
// , "\");');"
// fxt.Test_parse(String_.Concat_lines_nl
// ( "'wgNamespacesWithSubpages' => array"
// , "( 'default' => array(2 => 1)"
// , ", 'enwiki' => array(0 => 1)"
// , ")"
// ));
}
class Xoi_wiki_props_fxt {
private Xoi_wiki_props_api api = new Xoi_wiki_props_api();
private Bry_bfr bfr = Bry_bfr.new_();
public Xoi_wiki_props_wiki wiki_() {return wiki_("domain_doesnt_matter");}
public Xoi_wiki_props_wiki wiki_(String wiki_domain) {return new Xoi_wiki_props_wiki().Wiki_domain_(Bry_.new_a7(wiki_domain));}
public Xoi_wiki_props_alias alias_(int id, String alias) {return new Xoi_wiki_props_alias().Init_by_ctor(id, alias);}
public Xoi_wiki_props_ns ns_(int id, boolean subpages_enabled) {return new Xoi_wiki_props_ns().Init_by_ctor(id, subpages_enabled);}
public void Test_parse(String xml, Xoi_wiki_props_wiki expd) {
Xoi_wiki_props_wiki actl = new Xoi_wiki_props_wiki();
api.Parse(actl, xml);
Tfds.Eq_str_lines(Xto_str(expd), Xto_str(actl));
}
public void Test_build(Xoi_wiki_props_wiki wiki, String expd) {
api.Build_cfg(bfr, wiki);
Tfds.Eq_str_lines(expd, bfr.Xto_str_and_clear());
}
private String Xto_str(Xoi_wiki_props_wiki v) {
int len = v.Alias_ary().length;
bfr.Add_str_a7("aliases").Add_byte_nl();
for (int i = 0; i < len; i++) {
Xoi_wiki_props_alias alias = v.Alias_ary()[i];
bfr.Add_int_variable(alias.Id()).Add_byte_pipe().Add_str(alias.Alias()).Add_byte_nl();
}
bfr.Add_str_a7("ns").Add_byte_nl();
len = v.Ns_ary().length;
for (int i = 0; i < len; i++) {
Xoi_wiki_props_ns ns = v.Ns_ary()[i];
bfr.Add_int_variable(ns.Id()).Add_byte_pipe().Add_int_bool(ns.Subpages_enabled()).Add_byte_nl();
}
bfr.Add_byte_nl();
return bfr.Xto_str_and_clear();
}
}
class Xob_subpage_tst_fxt {
public Xob_subpage_tst_fxt Clear() {
if (app == null) {
app = Xoa_app_fxt.app_();
mgr = app.Bldr().Wiki_cfg_bldr();
}
mgr.Clear();
hash.Clear();
return this;
} private Xoae_app app; Xob_wiki_cfg_bldr mgr; Ordered_hash hash = Ordered_hash_.new_();
private Xob_subpage_parser parser = new Xob_subpage_parser();
public Xob_subpage_tst_fxt Init_cmd(String wiki, String key, String text) {
// mgr.Itms_get_or_new(wiki).Itms_add(key, text);
return this;
}
public Xob_subpage_tst_fxt Expd_txt(String wiki, String text) {
// hash.Add(wiki, KeyVal_.new_(wiki, text));
return this;
}
private String_bldr sb = String_bldr_.new_();
public void Test_parse(String s) {
Xob_subpage_wiki[] actl = parser.Parse(Bry_.new_u8(s));
Tfds.Eq_str_lines("", X_str_wikis(actl));
}
public String X_str_wikis(Xob_subpage_wiki[] ary) {
X_str_wikis(sb, ary);
return sb.Xto_str_and_clear();
}
private void X_str_wikis(String_bldr sb, Xob_subpage_wiki[] ary) {
int len = ary.length;
for (int i = 0; i < len; i++) {
Xob_subpage_wiki wiki = ary[i];
X_str_wiki(sb, wiki);
}
}
private void X_str_wiki(String_bldr sb, Xob_subpage_wiki wiki) {
sb.Add(wiki.Name()).Add_char_nl();
int ns_len = wiki.Ns_list().Count();
for (int i = 0; i < ns_len; i++) {
Xob_subpage_ns ns = (Xob_subpage_ns)wiki.Ns_list().Get_at(i);
sb.Add(ns.Id()).Add("=").Add(Bool_.Xto_str_lower(ns.Enabled())).Add_char_nl();
}
sb.Add_char_nl();
}
}

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.bldrs.wiki_cfgs; import gplx.*; import gplx.xowa.*; import gplx.xowa.bldrs.*;
public class Xoi_wiki_props_ns {
public int Id() {return id;} private int id;
public boolean Subpages_enabled() {return subpages_enabled;} private boolean subpages_enabled;
public Xoi_wiki_props_ns Init_by_ctor(int id, boolean subpages_enabled) {this.id = id; this.subpages_enabled = subpages_enabled; return this;}
public void Init_by_xml(gplx.xmls.XmlNde ns_nde) {
this.id = Int_.parse_(ns_nde.Atrs().FetchValOr("id", "-1"));
this.subpages_enabled = ns_nde.Atrs().Fetch_or_null("subpages") != null;// per api, subpages="" means ns has subpages; no subpages attribute means no subpages
}
}

View File

@@ -0,0 +1,23 @@
/*
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.bldrs.wiki_cfgs; import gplx.*; import gplx.xowa.*; import gplx.xowa.bldrs.*;
public class Xoi_wiki_props_wiki {
public byte[] Wiki_domain() {return wiki_domain;} public Xoi_wiki_props_wiki Wiki_domain_(byte[] v) {wiki_domain = v; return this;} private byte[] wiki_domain;
public Xoi_wiki_props_ns[] Ns_ary() {return ns_ary;} public Xoi_wiki_props_wiki Ns_ary_(Xoi_wiki_props_ns... v) {this.ns_ary = v; return this;} private Xoi_wiki_props_ns[] ns_ary;
public Xoi_wiki_props_alias[] Alias_ary() {return alias_ary;} public Xoi_wiki_props_wiki Alias_ary_(Xoi_wiki_props_alias... v) {this.alias_ary = v; return this;} private Xoi_wiki_props_alias[] alias_ary;
}