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,85 @@
/*
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.xwikis; import gplx.*; import gplx.xowa.*; import gplx.xowa.wikis.*;
public class Xow_lang_grp implements GfoInvkAble {
public int Id() {return id;} private int id;
public byte[] Key() {return key;} private byte[] key;
public byte[] Name() {return name;} public Xow_lang_grp Name_(byte[] v) {name = v; return this;} private byte[] name;
public int Sort_idx() {return sort_idx;} public Xow_lang_grp Sort_idx_(int v) {sort_idx = v; return this;} private int sort_idx = 0;
public byte Sort_mode() {return sort_mode;} public Xow_lang_grp Sort_mode_(byte v) {sort_mode = v; return this;} private byte sort_mode = Sort_mode_page_name;
public Xow_lang_itm[] Itms() {if (itms == null) itms = (Xow_lang_itm[])itm_list.To_ary(Xow_lang_itm.class); return itms;} private Xow_lang_itm[] itms;
public int Itms_len() {return this.Itms().length;}
public Xow_lang_itm Itms_get(int i) {return this.Itms()[i];}
public void Itms_add(Xow_lang_itm itm) {itms = null; itm_list.Add(itm);} List_adp itm_list = List_adp_.new_();
public void Itms_active_len_add_one_() {++itms_active_len;}
public int Itms_active_len() {return itms_active_len;} private int itms_active_len;
public void Itms_reset() {
Xow_lang_itm[] itms_ary = this.Itms();
int itms_len = itms_ary.length;
for (int i = 0; i < itms_len; i++)
itms_ary[i].Atrs_set(null, false, null); // clear out pre-existing page names; needed b/c this struct is a singleton for entire wiki
itms_active_len = 0;
}
public Bry_fmtr Html_all() {return html_all;} Bry_fmtr html_all; public Xow_lang_grp Html_all_(String s) {html_all = Bry_fmtr.new_(s, "all_name", "grps"); return this;}
public byte[] Html_grp_bgn() {return html_grp_bgn;} private byte[] html_grp_bgn = Bry_.new_a7("\n <tr>");
public byte[] Html_grp_end() {return html_grp_end;} private byte[] html_grp_end = Bry_.new_a7("\n </tr>");
public Bry_fmtr Html_itm() {return html_itm;} Bry_fmtr html_itm; public Xow_lang_grp Html_itm_(String s) {html_itm = Bry_fmtr.new_(s, "lang_code", "lang_domain", "lang_name", "lang_href", "pagename_translation", "page_badge"); return this;}
public void Html_bld(Bry_bfr bfr, Xowe_wiki wiki) {
Xow_lang_itm[] itms_ary = this.Itms();
if (sort_mode == Xow_lang_grp.Sort_mode_page_name)
Array_.Sort(itms_ary, Xow_lang_itm_sorter_page_name._);
int itms_ary_len = itms_ary.length;
for (int i = 0; i < itms_ary_len; i++)
itms_ary[i].Html_bld(bfr, wiki);
}
public Object Invk(GfsCtx ctx, int ikey, String k, GfoMsg m) {
if (ctx.Match(k, Invk_owner)) return lang_mgr;
else if (ctx.Match(k, Invk_name_)) name = m.ReadBry("v");
else if (ctx.Match(k, Invk_sort_idx_)) sort_idx = m.ReadInt("v");
else if (ctx.Match(k, Invk_sort_mode_)) sort_mode = (byte)m.ReadInt("v");
else if (ctx.Match(k, Invk_html_all_)) Html_all_(m.ReadStr("v"));
else if (ctx.Match(k, Invk_html_itm_)) Html_itm_(m.ReadStr("v"));
else return GfoInvkAble_.Rv_unhandled;
return this;
} private static final String Invk_owner = "owner", Invk_name_ = "name_", Invk_sort_mode_ = "sort_mode_", Invk_sort_idx_ = "sort_idx_", Invk_html_all_ = "html_all_", Invk_html_itm_ = "html_itm_";
public static final byte Sort_mode_lang_idx = 0, Sort_mode_lang_name = 1, Sort_mode_page_name = 2;
public static Xow_lang_grp dflt_(Xow_lang_mgr lang_mgr, int id, byte[] key) {
Xow_lang_grp rv = new Xow_lang_grp();
rv.lang_mgr = lang_mgr; rv.id = id; rv.key = key; rv.name = key;
rv.Html_all_(String_.Concat_lines_nl_skip_last
( ""
, " <h4>~{all_name}</h4>"
, " <table style='width: 100%;'>~{grps}"
, " </table>"
));
rv.Html_itm_(String_.Concat_lines_nl_skip_last
( ""
, " <td style='width: 10%; padding-bottom: 5px;'>~{lang_name}</td><td style='width: 20%; padding-bottom: 5px;'><li~{page_badge}><a hreflang=\"~{lang_code}\" title=\"~{pagename_translation}\" href=\"~{lang_href}\">~{pagename_translation}</a></li></td><td style='width: 3%; padding-bottom: 5px;'></td>"
));
return rv;
} private Xow_lang_grp() {}
Xow_lang_mgr lang_mgr;
}
class Xow_lang_itm_sorter_page_name implements gplx.lists.ComparerAble {
public int compare(Object lhsObj, Object rhsObj) {return Bry_.Compare(((Xow_lang_itm)lhsObj).Page_name(), ((Xow_lang_itm)rhsObj).Page_name());}
public static final Xow_lang_itm_sorter_page_name _ = new Xow_lang_itm_sorter_page_name(); Xow_lang_itm_sorter_page_name() {}
}
class Xow_lang_grp_sorter_sort_idx implements gplx.lists.ComparerAble {
public int compare(Object lhsObj, Object rhsObj) {return Int_.Compare(((Xow_lang_grp)lhsObj).Sort_idx(), ((Xow_lang_grp)rhsObj).Sort_idx());}
public static final Xow_lang_grp_sorter_sort_idx _ = new Xow_lang_grp_sorter_sort_idx(); Xow_lang_grp_sorter_sort_idx() {}
}

View File

@@ -0,0 +1,38 @@
/*
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.xwikis; import gplx.*; import gplx.xowa.*; import gplx.xowa.wikis.*;
public class Xow_lang_itm {
private final Xow_lang_grp html_grp; private final Xow_xwiki_itm xwiki; private final Xoac_lang_itm lang;
public Xow_lang_itm(Xow_lang_grp html_grp, Xow_xwiki_itm xwiki, Xoac_lang_itm lang) {
this.html_grp = html_grp; this.xwiki = xwiki; this.lang = lang;
}
public byte[] Lang_key() {return lang.Key_bry();}
public byte[] Lang_domain() {return xwiki.Domain_bry();}
public byte[] Lang_name() {return lang.Local_name_bry();}
public byte[] Page_name() {return page_name;} private byte[] page_name;
public boolean Page_name_has() {return Bry_.Len_gt_0(page_name);}
public boolean Empty_xwiki() {return empty_xwiki;} private boolean empty_xwiki;
public byte[][] Page_badges() {return page_badges;} private byte[][] page_badges;
public void Html_bld(Bry_bfr bfr, Xowe_wiki wiki) {
html_grp.Html_itm().Bld_bfr(bfr, lang.Key_bry(), xwiki.Domain_bry(), lang.Local_name_bry(), page_name);
}
public void Atrs_set(byte[] page_name, boolean empty_xwiki, byte[][] page_badges) {
this.page_name = page_name; this.empty_xwiki = empty_xwiki; this.page_badges = page_badges;
html_grp.Itms_active_len_add_one_();
}
}

View File

@@ -0,0 +1,193 @@
/*
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.xwikis; import gplx.*; import gplx.xowa.*; import gplx.xowa.wikis.*;
import gplx.xowa.langs.*; import gplx.xowa.wikis.xwikis.*; import gplx.xowa.apis.xowa.html.*; import gplx.xowa.xtns.wdatas.core.*;
import gplx.xowa.html.hrefs.*;
public class Xow_lang_mgr {
Xow_lang_mgr() {
int len = Xol_lang_itm_.Id__max;
itms = new Xow_lang_itm[len];
}
public Bry_fmtr Html_div() {return html_div;} private final Bry_fmtr html_div = Bry_fmtr.new_(String_.Concat_lines_nl_skip_last
( "<div id=\"xowa-lang\">"
, " <h5>~{toggle_btn}~{wikidata_link}</h5>"
, " <div~{toggle_hdr}>~{grps}"
, " </div>"
, "</div>"
), "wikidata_link", "toggle_btn", "toggle_hdr", "grps");
public Bry_fmtr Html_wikidata_link() {return html_wikidata_link;} private final Bry_fmtr html_wikidata_link = Bry_fmtr.new_(" (<a href=\"/site/www.wikidata.org/wiki/~{qid}\">wikidata</a>)", "qid");
public void Clear() {hash.Clear();}
public void Itms_reg(Xow_xwiki_itm xwiki, Xoac_lang_itm lang) {
int lang_id = xwiki.Lang_id();
Xoac_lang_grp ini_grp = lang.Grp();
Xow_lang_grp grp = (Xow_lang_grp)hash.Get_by(ini_grp.Key_bry());
if (grp == null) {
grp = Grps_get_or_new(ini_grp.Key_bry());
grp.Name_(ini_grp.Name_bry());
grp.Sort_idx_(ini_grp.Sort_idx());
}
Xow_lang_itm itm = itms[lang_id];
if (itm == null) {
itm = new Xow_lang_itm(grp, xwiki, lang);
itms[lang_id] = itm;
}
grp.Itms_add(itm);
}
public int Grps_len() {return hash.Count();}
public Xow_lang_grp Grps_get_at(int i) {return (Xow_lang_grp)hash.Get_at(i);}
Xow_lang_grp Grps_get_or_new(byte[] key) {
Xow_lang_grp rv = (Xow_lang_grp)hash.Get_by(key);
if (rv == null) {
int id = hash.Count();
rv = Xow_lang_grp.dflt_(this, id, key);
rv.Sort_idx_(id);
hash.Add(key, rv);
rv.Name_(key);
}
return rv;
} private Ordered_hash hash = Ordered_hash_.new_bry_();
public void Grps_sort() {hash.Sort_by(Xow_lang_grp_sorter_sort_idx._);}
public void Html_bld(Bry_bfr bfr, Xowe_wiki wiki, List_adp slink_list, byte[] qid) {
int grp_len = hash.Count();
for (int i = 0; i < grp_len; i++) {
Xow_lang_grp grp = (Xow_lang_grp)hash.Get_at(i);
grp.Itms_reset();
}
int slink_len = slink_list.Count();
for (int i = 0; i < slink_len; i++) {
Wdata_sitelink_itm slink = (Wdata_sitelink_itm)slink_list.Get_at(i);
Xoa_ttl ttl = slink.Page_ttl();
Xow_xwiki_itm xwiki = ttl.Wik_itm();
int lang_id = xwiki.Lang_id();
Xow_lang_itm itm = itms[lang_id]; // NOTE: handles ttls like [[fr:]] and [[:fr;]] which have an empty Page_txt, but a valued Full_txt_raw
byte[] ttl_bry = ttl.Page_txt_w_anchor();
boolean empty_xwiki = false;
if (Bry_.Len_eq_0(ttl_bry)) {
ttl_bry = wiki.Ctx().Cur_page().Ttl().Page_txt();
empty_xwiki = true;
}
itm.Atrs_set(ttl_bry, empty_xwiki, slink.Badges());
}
html_bldr.Init(this, wiki, slink_list, slink_len, qid);
html_bldr.XferAry(bfr, -1);
} private Xow_lang_itm[] itms = null; Xow_lang_html html_bldr = new Xow_lang_html();
public Object Invk(GfsCtx ctx, int ikey, String k, GfoMsg m) {
if (ctx.Match(k, Invk_get)) return Grps_get_or_new(m.ReadBry("v"));
else if (ctx.Match(k, Invk_sort)) Grps_sort();
else return GfoInvkAble_.Rv_unhandled;
return this;
} private static final String Invk_get = "get", Invk_sort = "sort";
public static Xow_lang_mgr dflt_() {return new Xow_lang_mgr();}
}
class Xow_lang_html implements Bry_fmtr_arg {
private Xow_lang_mgr lang_mgr; Xowe_wiki wiki; List_adp ttl_list; private byte[] qid;
private Xoapi_toggle_itm toggle_itm;
private int stage = 0;
public Xow_lang_html Init(Xow_lang_mgr lang_mgr, Xowe_wiki wiki, List_adp ttl_list, int ttl_list_len, byte[] qid) {
this.lang_mgr = lang_mgr; this.wiki = wiki; this.ttl_list = ttl_list; this.qid = qid;
toggle_itm = wiki.Appe().Api_root().Html().Page().Toggle_mgr().Get_or_new("wikidata-langs");
return this;
}
public void XferAry(Bry_bfr bfr, int idx) {
switch (stage) {
case 0: {
stage = 1;
Bry_bfr tmp_bfr = wiki.Utl__bfr_mkr().Get_b128().Mkr_rls();
byte[] msg_lang = wiki.Msg_mgr().Val_by_id(Xol_msg_itm_.Id_page_lang_header);
byte[] wikidata_link = Bry_.Len_eq_0(qid) ? Bry_.Empty : lang_mgr.Html_wikidata_link().Bld_bry_many(tmp_bfr, qid);
toggle_itm.Init(wiki, msg_lang);
lang_mgr.Html_div().Bld_bfr_many(bfr, wikidata_link, toggle_itm.Html_toggle_btn(), toggle_itm.Html_toggle_hdr(), this);
stage = 0;
break;
}
case 1:
stage = 2;
int grps_len = lang_mgr.Grps_len();
for (int i = 0; i < grps_len; i++) {
grp = lang_mgr.Grps_get_at(i);
if (grp.Itms_active_len() == 0) continue; // skip grps with no items
grp.Html_all().Bld_bfr_many(bfr, grp.Name(), this);
}
stage = 1;
break;
case 2:
int itms_len = grp.Itms_len();
int grp_counter = 0; boolean row_opened = false;
for (int i = 0; i < itms_len; i++) {
Xow_lang_itm itm = grp.Itms_get(i);
if (!itm.Page_name_has()) continue;
if (grp_counter == 0) {
bfr.Add(grp.Html_grp_bgn());
row_opened = true;
}
byte[] lang_key = itm.Lang_key();
byte[] domain = itm.Lang_domain();
byte[] page_name = itm.Page_name();
byte[] local_name = itm.Lang_name();
byte[] badge_cls = Badge_cls(tmp_bfr, itm.Page_badges());
if (wiki.Appe().Usere().Wiki().Xwiki_mgr().Get_by_key(domain) == null)
tmp_bfr.Add(Xoh_href_parser.Href_https_bry).Add(domain).Add(Xoh_href_parser.Href_wiki_bry);
else
tmp_bfr.Add(Xoh_href_parser.Href_site_bry).Add(domain).Add(Xoh_href_parser.Href_wiki_bry);
if (!itm.Empty_xwiki()) tmp_bfr.Add(page_name);
grp.Html_itm().Bld_bfr_many(bfr, lang_key, domain, local_name, tmp_bfr.Xto_bry_and_clear(), page_name, badge_cls);
++grp_counter;
if (grp_counter == 3) {
row_opened = false;
bfr.Add(grp.Html_grp_end());
grp_counter = 0;
}
}
if (row_opened) {
bfr.Add(grp.Html_grp_end());
grp_counter = 0;
}
stage = 2;
break;
}
} private Xow_lang_grp grp; Bry_bfr tmp_bfr = Bry_bfr.reset_(255);
private static byte[] Badge_cls(Bry_bfr bfr, byte[][] badges) {
if (badges == null) badges = Bry_.Ary_empty;
int badges_len = badges.length;
bfr.Add(Cls_bgn);
if (badges_len == 0)
bfr.Add(Badge_none_cls);
else {
for (int i = 0; i < badges_len; ++i) {
if (i != 0) bfr.Add_byte_comma();
byte[] badge = badges[i];
byte[] badge_cls = (byte[])badges_hash.Get_by_bry(badge);
if (badge_cls == null) Gfo_usr_dlg_.I.Warn_many("", "", "unknown badge: badge=~{0}", String_.new_u8(badge));
else bfr.Add(badge_cls);
}
}
bfr.Add_byte_apos();
return bfr.Xto_bry_and_clear();
}
private static final byte[]
Badge_none_cls = Bry_.new_a7("badge-none")
, Cls_bgn = Bry_.new_a7(" class='")
;
private static Hash_adp_bry badges_hash = Hash_adp_bry.ci_ascii_()
.Add_str_obj("Q17437798", Bry_.new_a7("badge-goodarticle"))
.Add_str_obj("Q17437796", Bry_.new_a7("badge-featuredarticle"))
.Add_str_obj("Q17559452", Bry_.new_a7("badge-recommendedarticle"))
.Add_str_obj("Q17506997", Bry_.new_a7("badge-featuredlist"))
.Add_str_obj("Q17580674", Bry_.new_a7("badge-featuredportal"))
;
}

View File

@@ -0,0 +1,65 @@
/*
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.xwikis; import gplx.*; import gplx.xowa.*; import gplx.xowa.wikis.*;
import gplx.xowa.langs.*;
public class Xow_lang_mgr_fxt {
public void Clear() {
app = Xoa_app_fxt.app_();
wiki = Xoa_app_fxt.wiki_tst_(app);
Init_langs(wiki);
}
public static void Init_langs(Xowe_wiki wiki) {
Xoae_app app = wiki.Appe();
Xoa_lang_mgr lang_mgr = app.Lang_mgr();
lang_mgr.Groups().Set_bulk(Bry_.new_a7(String_.Concat_lines_nl
( "+||grp|wiki"
, "+|wiki|grp|grp1"
, "+|wiki|grp|grp2"
, "+|grp1|itm|simple|Simple"
, "+|grp2|itm|fr|French"
, "+|grp1|itm|es|Spanish"
, "+|grp2|itm|de|German"
, "+|grp1|itm|it|Italian"
, "+|grp1|itm|zh|Chinese"
)));
wiki.Xwiki_mgr().Add_bulk_langs(Bry_.new_a7("wiki"));
String bulk = String_.Concat_lines_nl
( "simple.wikipedia.org|simple.wikipedia.org"
, "fr.wikipedia.org|fr.wikipedia.org"
, "es.wikipedia.org|es.wikipedia.org"
, "de.wikipedia.org|de.wikipedia.org"
, "it.wikipedia.org|it.wikipedia.org"
);
wiki.Appe().Usere().Wiki().Xwiki_mgr().Add_bulk(Bry_.new_a7(bulk));
}
public Xowe_wiki Wiki() {return wiki;} private Xowe_wiki wiki;
Xoae_app app;
public void tst(String raw, String expd) {
Xop_ctx ctx = wiki.Ctx();
ctx.Cur_page().Ttl_(Xoa_ttl.parse_(wiki, Bry_.new_a7("test_page")));
byte[] raw_bry = Bry_.new_u8(raw);
Bry_bfr bfr = Bry_bfr.new_();
Xop_root_tkn root = ctx.Tkn_mkr().Root(raw_bry);
wiki.Parser().Parse_page_all_clear(root, ctx, ctx.Tkn_mkr(), raw_bry);
wiki.Html_mgr().Html_wtr().Write_all(bfr, ctx, raw_bry, root);
Bry_bfr html_bfr = Bry_bfr.new_();
wiki.Xwiki_mgr().Lang_mgr().Html_bld(html_bfr, wiki, ctx.Cur_page().Slink_list(), gplx.xowa.xtns.wdatas.Wdata_xwiki_link_wtr.Qid_null);
Tfds.Eq_str_lines(expd, html_bfr.Xto_str_and_clear());
}
}

View File

@@ -0,0 +1,93 @@
/*
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.xwikis; import gplx.*; import gplx.xowa.*; import gplx.xowa.wikis.*;
import org.junit.*;
public class Xow_lang_mgr_tst {
private Xow_lang_mgr_fxt fxt = new Xow_lang_mgr_fxt();
@Before public void init() {fxt.Clear();}
@Test public void Basic() {
fxt.tst("[[simple:Earth]] [[fr:Terre]] [[es:Tierra]] [[de:Erde]] [[it:Terre]]", String_.Concat_lines_nl_skip_last
( "<div id=\"xowa-lang\">"
, " <h5><a href='javascript:xowa_toggle_visible(\"wikidata-langs\");' style='text-decoration: none !important;'>In other languages<img id='wikidata-langs-toggle-icon' src='file:///mem/xowa/user/test_user/app/img/window/portal/twisty_right.png' title='' /></a></h5>"
, " <div id='wikidata-langs-toggle-elem' style='display:none;'>"
, " <h4>grp1</h4>"
, " <table style='width: 100%;'>"
, " <tr>"
, " <td style='width: 10%; padding-bottom: 5px;'>Simple</td><td style='width: 20%; padding-bottom: 5px;'><li class='badge-none'><a hreflang=\"simple\" title=\"Earth\" href=\"/site/simple.wikipedia.org/wiki/Earth\">Earth</a></li></td><td style='width: 3%; padding-bottom: 5px;'></td>"
, " <td style='width: 10%; padding-bottom: 5px;'>Spanish</td><td style='width: 20%; padding-bottom: 5px;'><li class='badge-none'><a hreflang=\"es\" title=\"Tierra\" href=\"/site/es.wikipedia.org/wiki/Tierra\">Tierra</a></li></td><td style='width: 3%; padding-bottom: 5px;'></td>"
, " <td style='width: 10%; padding-bottom: 5px;'>Italian</td><td style='width: 20%; padding-bottom: 5px;'><li class='badge-none'><a hreflang=\"it\" title=\"Terre\" href=\"/site/it.wikipedia.org/wiki/Terre\">Terre</a></li></td><td style='width: 3%; padding-bottom: 5px;'></td>"
, " </tr>"
, " </table>"
, " <h4>grp2</h4>"
, " <table style='width: 100%;'>"
, " <tr>"
, " <td style='width: 10%; padding-bottom: 5px;'>French</td><td style='width: 20%; padding-bottom: 5px;'><li class='badge-none'><a hreflang=\"fr\" title=\"Terre\" href=\"/site/fr.wikipedia.org/wiki/Terre\">Terre</a></li></td><td style='width: 3%; padding-bottom: 5px;'></td>"
, " <td style='width: 10%; padding-bottom: 5px;'>German</td><td style='width: 20%; padding-bottom: 5px;'><li class='badge-none'><a hreflang=\"de\" title=\"Erde\" href=\"/site/de.wikipedia.org/wiki/Erde\">Erde</a></li></td><td style='width: 3%; padding-bottom: 5px;'></td>"
, " </tr>"
, " </table>"
, " </div>"
, "</div>"
));
}
@Test public void Empty() {
fxt.tst("[[simple:]]", String_.Concat_lines_nl_skip_last
( "<div id=\"xowa-lang\">"
, " <h5><a href='javascript:xowa_toggle_visible(\"wikidata-langs\");' style='text-decoration: none !important;'>In other languages<img id='wikidata-langs-toggle-icon' src='file:///mem/xowa/user/test_user/app/img/window/portal/twisty_right.png' title='' /></a></h5>"
, " <div id='wikidata-langs-toggle-elem' style='display:none;'>"
, " <h4>grp1</h4>"
, " <table style='width: 100%;'>"
, " <tr>"
, " <td style='width: 10%; padding-bottom: 5px;'>Simple</td><td style='width: 20%; padding-bottom: 5px;'><li class='badge-none'><a hreflang=\"simple\" title=\"Test page\" href=\"/site/simple.wikipedia.org/wiki/\">Test page</a></li></td><td style='width: 3%; padding-bottom: 5px;'></td>"
, " </tr>"
, " </table>"
, " </div>"
, "</div>"
));
}
@Test public void Unregistered() {
// fxt.Wiki().Xwiki_mgr().Add_full(Bry_.new_u8("zh"), Bry_.new_u8("zh.wikipedia.org"), Bry_.new_u8("http://zh.wikipedia.org/~{0}"));
fxt.tst("[[zh:Earth]]", String_.Concat_lines_nl_skip_last
( "<div id=\"xowa-lang\">"
, " <h5><a href='javascript:xowa_toggle_visible(\"wikidata-langs\");' style='text-decoration: none !important;'>In other languages<img id='wikidata-langs-toggle-icon' src='file:///mem/xowa/user/test_user/app/img/window/portal/twisty_right.png' title='' /></a></h5>"
, " <div id='wikidata-langs-toggle-elem' style='display:none;'>"
, " <h4>grp1</h4>"
, " <table style='width: 100%;'>"
, " <tr>"
, " <td style='width: 10%; padding-bottom: 5px;'>Chinese</td><td style='width: 20%; padding-bottom: 5px;'><li class='badge-none'><a hreflang=\"zh\" title=\"Earth\" href=\"https://zh.wikipedia.org/wiki/Earth\">Earth</a></li></td><td style='width: 3%; padding-bottom: 5px;'></td>"
, " </tr>"
, " </table>"
, " </div>"
, "</div>"
));
}
@Test public void Anchor() {// PURPOSE: A#b was not showing anchor "#b"; DATE:2013-10-23
fxt.tst("[[simple:A#b]]", String_.Concat_lines_nl_skip_last
( "<div id=\"xowa-lang\">"
, " <h5><a href='javascript:xowa_toggle_visible(\"wikidata-langs\");' style='text-decoration: none !important;'>In other languages<img id='wikidata-langs-toggle-icon' src='file:///mem/xowa/user/test_user/app/img/window/portal/twisty_right.png' title='' /></a></h5>"
, " <div id='wikidata-langs-toggle-elem' style='display:none;'>"
, " <h4>grp1</h4>"
, " <table style='width: 100%;'>"
, " <tr>"
, " <td style='width: 10%; padding-bottom: 5px;'>Simple</td><td style='width: 20%; padding-bottom: 5px;'><li class='badge-none'><a hreflang=\"simple\" title=\"A#b\" href=\"/site/simple.wikipedia.org/wiki/A#b\">A#b</a></li></td><td style='width: 3%; padding-bottom: 5px;'></td>"
, " </tr>"
, " </table>"
, " </div>"
, "</div>"
));
}
}

View File

@@ -0,0 +1,57 @@
/*
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.xwikis; import gplx.*; import gplx.xowa.*; import gplx.xowa.wikis.*;
import gplx.xowa.langs.*;
public class Xow_xwiki_itm implements gplx.CompareAble {
public Xow_xwiki_itm(byte[] key_bry, byte[] url_fmt, int lang_id, int domain_tid, byte[] domain_bry, byte[] domain_name) {
this.key_bry = key_bry; this.key_str = String_.new_u8(key_bry);
this.url_fmt = url_fmt; this.lang_id = lang_id;
this.url_fmtr = Bry_.Len_eq_0(url_fmt) ? null : Bry_fmtr.new_(url_fmt, "0");
this.domain_tid = domain_tid; this.domain_bry = domain_bry; this.domain_name = domain_name;
}
public byte[] Key_bry() {return key_bry;} private final byte[] key_bry; // EX: commons
public String Key_str() {return key_str;} private final String key_str;
public byte[] Url_fmt() {return url_fmt;} private final byte[] url_fmt; // EX: //commons.wikimedia.org/wiki/Category:$1
public Bry_fmtr Url_fmtr(){return url_fmtr;} private final Bry_fmtr url_fmtr;
public int Lang_id() {return lang_id;} private final int lang_id; // EX: Id__unknown
public int Domain_tid() {return domain_tid;} private final int domain_tid; // EX: Tid_int_commons
public byte[] Domain_bry() {return domain_bry;} private final byte[] domain_bry; // EX: commons.wikimedia.org
public byte[] Domain_name() {return domain_name;} private final byte[] domain_name; // EX: Wikimedia Commons
public boolean Offline() {return offline;} public Xow_xwiki_itm Offline_(boolean v) {offline = v; return this;} private boolean offline;
public int compareTo(Object obj) {Xow_xwiki_itm comp = (Xow_xwiki_itm)obj; return Bry_.Compare(key_bry, comp.key_bry);}
public boolean Type_is_xwiki_lang(int cur_lang_id) {
return lang_id != Xol_lang_itm_.Id__unknown // valid lang code
&& domain_tid != Xow_domain_type_.Tid_commons // commons should never be considered an xwiki_lang; EX:[[commons:A]] PAGE:species:Scarabaeidae; DATE:2014-09-10
&& lang_id != cur_lang_id // lang is different than current; EX: [[en:A]] in en.wikipedia.org shouldn't link back to self
&& Bry_.Len_gt_0(url_fmt) // url_fmt exists
;
}
public static Xow_xwiki_itm new_(byte[] key_bry, byte[] url_fmt, int lang_id, int domain_tid, byte[] domain_bry) {
return new Xow_xwiki_itm(key_bry, url_fmt, lang_id, domain_tid, domain_bry, domain_bry);
}
public static Xow_xwiki_itm new_by_mw(Bry_bfr bfr, Gfo_url_parser url_parser, Gfo_url url, byte[] key, byte[] mw_url, byte[] domain_name) {// EX: "commons|//commons.wikimedia.org/wiki/Category:$1|Wikimedia Commons" "DMOZ|http://www.dmoz.org/Regional/Europe/$1/"|DMOZ"
byte[] gfs_url = gplx.xowa.apps.Xoa_gfs_php_mgr.Xto_gfs(bfr, mw_url); // EX: "//commons.wikimedia.org/wiki/Category:$1" -> "//commons.wikimedia.org/wiki/Category:~{0}"
url_parser.Parse(url, gfs_url, 0, gfs_url.length);
byte[] domain_bry = url.Site(); // extract "commons.wikimedia.org"
Xow_domain domain = Xow_domain_.parse(domain_bry);
Xol_lang_itm lang_itm = Xol_lang_itm_.Get_by_key(domain.Lang_key());
int lang_id = lang_itm == null ? Xol_lang_itm_.Id__unknown : lang_itm.Id();
return new Xow_xwiki_itm(key, gfs_url, lang_id, domain.Domain_tid(), domain_bry, domain_name);
}
}

View File

@@ -0,0 +1,44 @@
/*
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.xwikis; import gplx.*; import gplx.xowa.*; import gplx.xowa.wikis.*;
import org.junit.*; import gplx.xowa.wikis.*; import gplx.xowa.langs.*;
public class Xow_xwiki_itm_tst {
@Before public void init() {fxt.Clear();} private Xow_xwiki_itm_fxt fxt = new Xow_xwiki_itm_fxt();
@Test public void Commons() {fxt.Test_new_by_mw("commons.wikimedia.org/wiki/$1" , "commons.wikimedia.org" , "commons.wikimedia.org/wiki/~{0}" , Xow_domain_type_.Tid_commons , Xol_lang_itm_.Id__unknown);}
@Test public void Wiktionary() {fxt.Test_new_by_mw("fr.wiktionary.org/wiki/$1" , "fr.wiktionary.org" , "fr.wiktionary.org/wiki/~{0}" , Xow_domain_type_.Tid_wiktionary , Xol_lang_itm_.Id_fr);}
@Test public void Lang() {fxt.Test_new_by_mw("fr.wikipedia.org/wiki/$1" , "fr.wikipedia.org" , "fr.wikipedia.org/wiki/~{0}" , Xow_domain_type_.Tid_wikipedia , Xol_lang_itm_.Id_fr);}
}
class Xow_xwiki_itm_fxt {
private Bry_bfr tmp_bfr;
private Gfo_url_parser url_parser;
private Gfo_url tmp_url;
private byte[] key;
public void Clear() {
tmp_bfr = Bry_bfr.new_(255);
url_parser = new Gfo_url_parser();
tmp_url = new Gfo_url();
key = Bry_.new_a7("test");
}
public void Test_new_by_mw(String url_php, String expd_domain, String expd_url_fmt, int expd_wiki_tid, int expd_lang_tid) {
Xow_xwiki_itm itm = Xow_xwiki_itm.new_by_mw(tmp_bfr, url_parser, tmp_url, key, Bry_.new_u8(url_php), key);
Tfds.Eq(expd_domain , String_.new_u8(itm.Domain_bry()));
Tfds.Eq(expd_url_fmt , String_.new_u8(itm.Url_fmt()));
Tfds.Eq(expd_wiki_tid , itm.Domain_tid(), "wiki");
Tfds.Eq(expd_lang_tid , itm.Lang_id(), "lang");
}
}

View File

@@ -0,0 +1,215 @@
/*
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.xwikis; import gplx.*; import gplx.xowa.*; import gplx.xowa.wikis.*;
import gplx.xowa.langs.*;
import gplx.xowa.html.hrefs.*;
public class Xow_xwiki_mgr implements GfoInvkAble {
private Xowe_wiki wiki; private Xow_xwiki_mgr_srl srl;
public Xow_xwiki_mgr() {} // FIXME: current placeholder for viewer
public Xow_xwiki_mgr(Xowe_wiki wiki, Gfo_url_parser url_parser) {
this.wiki = wiki;
srl = new Xow_xwiki_mgr_srl(this, url_parser);
}
public Xow_lang_mgr Lang_mgr() {return lang_mgr;} private final Xow_lang_mgr lang_mgr = Xow_lang_mgr.dflt_();
public int Len() {return list.Count();} private Ordered_hash list = Ordered_hash_.new_bry_(); private Hash_adp_bry hash = Hash_adp_bry.ci_ascii_(); // ASCII:lang_code
public void Clear() {hash.Clear(); list.Clear();}
public Xow_xwiki_itm Get_at(int i) {return (Xow_xwiki_itm)list.Get_at(i);}
public Xow_xwiki_itm Get_by_key(byte[] key) {return (Xow_xwiki_itm)hash.Get_by_bry(key);}
public Xow_xwiki_itm Get_by_mid(byte[] src, int bgn, int end) {return (Xow_xwiki_itm)hash.Get_by_mid(src, bgn, end);}
public Xow_xwiki_itm Add_full(String alias, String domain) {return Add_full(Bry_.new_a7(alias), Bry_.new_a7(domain), null);}
public Xow_xwiki_itm Add_full(byte[] alias, byte[] domain) {return Add_full(alias, domain, null);}
public Xow_xwiki_itm Add_full(byte[] alias, byte[] domain_bry, byte[] url_fmt) {
int domain_tid = Byte_.Zero;
int lang_id = -1;
Xow_domain wiki_type = Xow_domain_.parse(domain_bry);
domain_tid = wiki_type.Domain_tid();
if (Bry_.Len_gt_0(wiki_type.Lang_key())) { // domain_bry has lang (EX: "en.")
Xol_lang_itm lang_itm = Xol_lang_itm_.Get_by_key(wiki_type.Lang_key());
if (lang_itm == null) return null; // unknown lang: do not add to wiki collection; EX: en1.wikipedia.org
lang_id = lang_itm.Id();
}
Xow_xwiki_itm itm = Xow_xwiki_itm.new_(alias, url_fmt, lang_id, domain_tid, domain_bry);
Add_itm(itm, null);
return itm;
}
public void Sort_by_key() {
list.Sort();
}
public Xow_domain[] Get_by_crt(Xow_domain cur, gplx.xowa.wikis.domains.crts.Xow_domain_crt_itm crt) {
List_adp rv = List_adp_.new_();
int len = this.Len();
for (int i = 0; i < len; ++i) {
Xow_xwiki_itm wiki = this.Get_at(i);
if (!wiki.Offline()) continue;
Xow_domain domain_itm = Xow_domain_.parse(wiki.Domain_bry());
if (crt.Matches(cur, domain_itm)) rv.Add(domain_itm);
}
return (Xow_domain[])rv.To_ary_and_clear(Xow_domain.class);
}
public void Add_bulk(byte[] raw) {
byte[][] rows = Bry_.Split(raw, Byte_ascii.Nl);
int rows_len = rows.length;
Hash_adp_bry lang_regy = Xol_lang_itm_.Regy();
for (int i = 0; i < rows_len; i++) {
byte[] row = rows[i]; if (Bry_.Len_eq_0(row)) continue; // ignore blank rows
Xow_xwiki_itm itm = Add_bulk_row(lang_regy, row);
Add_itm(itm, null);
}
}
public Xow_xwiki_itm Add_bulk_row(Hash_adp_bry lang_regy, byte[] row) {
byte[][] flds = Bry_.Split(row, Byte_ascii.Pipe); int flds_len = flds.length;
byte[] alias = Bry_.Empty, domain_bry = Bry_.Empty;
for (int j = 0; j < flds_len; j++) {
byte[] fld = flds[j];
switch (j) {
case 0: alias = fld; break;
case 1: domain_bry = fld; break;
case 2: break; // reserved for 0,1 (0=custom; 1=wmf)
default: throw Exc_.new_unhandled(j);
}
}
Xow_domain domain = Xow_domain_.parse(domain_bry);
int lang_id = Xol_lang_itm_.Id__unknown;
if (Bry_.Len_gt_0(domain.Lang_key())) {
Xol_lang_itm lang_itm = Xol_lang_itm_.Get_by_key(domain.Lang_key());
if (lang_itm != null // lang exists
&& Bry_.Eq(alias, lang_itm.Key())) // alias == lang.key; only assign langs to aliases that have lang key; EX: w|en.wikipedia.org; "w" alias should not be registered for "en"; DATE:2013-07-25
lang_id = lang_itm.Id();
}
byte[] url_fmt = Bry_.Add(Xoh_href_parser.Href_https_bry, domain_bry, Xoh_href_parser.Href_wiki_bry, Arg_0);
return Xow_xwiki_itm.new_(alias, url_fmt, lang_id, domain.Domain_tid(), domain_bry);
} static final byte[] Arg_0 = Bry_.new_a7("~{0}");
String Exec_itms_print(byte[] raw) {
Bry_fmtr fmtr = Bry_fmtr.new_bry_(raw, "wiki_key");//, "wiki_type_url", "wiki_lang", "wiki_name", "wiki_logo_url");
Bry_bfr tmp_bfr = Xoa_app_.Utl__bfr_mkr().Get_k004();
Hash_adp_bry seen = Hash_adp_bry.ci_ascii_(); // ASCII:url_domain; EX:en.wikipedia.org
int wikis_len = list.Count();
for (int i = 0; i < wikis_len; i++) {
Xow_xwiki_itm itm = (Xow_xwiki_itm)list.Get_at(i);
byte[] key = itm.Key_bry();
if (Bry_.Eq(key, Xow_domain_type_.Key_bry_home)) continue; // skip home
byte[] domain = itm.Domain_bry();
if (seen.Has(domain)) continue;
seen.Add_as_key_and_val(domain);
fmtr.Bld_bfr_many(tmp_bfr, key);
}
return tmp_bfr.To_str_and_rls();
}
public void Add_bulk_peers(byte[] raw) {
byte[][] keys = Bry_.Split(raw, Byte_ascii.Tilde);
int len = keys.length;
Ordered_hash peers = Ordered_hash_.new_();
Cfg_nde_root peer_root = wiki.Appe().Wiki_mgr().Groups();
for (int i = 0; i < len; i++) {
byte[] key = keys[i];
Cfg_nde_obj peer_grp = peer_root.Grps_get(key);
if (peer_grp == null)
throw Exc_.new_("unknown peer group", "key", String_.new_u8(key));
else
Cfg_nde_obj_.Fill_recurse(peers, peer_grp);
}
len = peers.Count();
byte[] lang_key_bry = wiki.Lang().Key_bry();
if (lang_key_bry == Xol_lang_itm_.Key__unknown) lang_key_bry = Xol_lang_.Key_en; // default non-lang wikis to english
String lang_key_str = String_.new_u8(lang_key_bry);
int lang_id = Xol_lang_itm_.Get_by_key(lang_key_bry).Id();
for (int i = 0; i < len; i++) {
Xoac_wiki_itm wiki_itm = (Xoac_wiki_itm)peers.Get_at(i);
byte[] wiki_name_bry = wiki_itm.Key_bry();
String wiki_name = String_.new_u8(wiki_name_bry);
String domain_str = null;
int domain_tid = Xow_domain_type_.Get_type_as_tid(wiki_name_bry);
switch (domain_tid) {
case Xow_domain_type_.Tid_commons:
case Xow_domain_type_.Tid_species:
case Xow_domain_type_.Tid_meta:
case Xow_domain_type_.Tid_incubator: domain_str = String_.Format("{0}.wikimedia.org", wiki_name); break; // EX: commons.wikimedia.org
case Xow_domain_type_.Tid_wikidata: domain_str = String_.Format("www.wikidata.org", wiki_name); break; // EX: www.wikidata.org
case Xow_domain_type_.Tid_mediawiki: domain_str = String_.Format("www.mediawiki.org", wiki_name); break;
case Xow_domain_type_.Tid_wmfblog: domain_str = String_.Format("wikimediafoundation.org", wiki_name); break;
default: domain_str = String_.Format("{0}.{1}.org", lang_key_str, wiki_name); break; // EX: en.wiktionary.org
}
byte[] domain_bry = Bry_.new_u8(domain_str);
Xowe_wiki lang_wiki = wiki.Appe().Wiki_mgr().Get_by_key_or_null(domain_bry);
boolean offline_exists = lang_wiki != null;
String fmt = String_.Format("http://" + domain_str + "/wiki/~{0}");
int aliases_len = wiki_itm.Aliases().length;
for (int j = 0; j < aliases_len; j++) {
byte[] alias = wiki_itm.Aliases()[j];
if (wiki.Ns_mgr().Names_get_or_null(alias, 0, alias.length) != null) continue; // NOTE: do not add xwiki if alias matches namespace; EX: en.wiktionary.org has ns of "Wiktionary"; do not add alias of "wiktionary"; note that wikipedia does have an alias to wiktionary
Xow_xwiki_itm xwiki = Xow_xwiki_itm.new_(alias, Bry_.new_u8(fmt), lang_id, domain_tid, domain_bry).Offline_(offline_exists); // NOTE: domain_tid must be used, not wiki.Domain_tid; DATE:2014-09-14
Add_itm(xwiki, null);
}
}
}
public void Add_bulk_langs(GfoMsg m) {
byte[] grp_key = m.ReadBry("grp_key");
byte[] wiki_type_name = m.ReadBryOr("wiki_type_name", null);
int wiki_tid = wiki_type_name == null ? wiki.Domain_tid() : Xow_domain_type_.Get_type_as_tid(wiki_type_name);
Add_bulk_langs(grp_key, wiki_tid);
}
public void Add_bulk_langs(byte[] grp_key) {Add_bulk_langs(grp_key, wiki.Domain_tid());}
public void Add_bulk_langs(byte[] grp_key, int domain_tid) {
Ordered_hash langs = wiki.Appe().Lang_mgr().Xto_hash(grp_key);
int len = langs.Count();
byte[] wiki_tid_name = Xow_domain_type_.Get_type_as_bry(domain_tid);
String wiki_tid_name_str = String_.new_u8(wiki_tid_name);
for (int i = 0; i < len; i++) {
Xoac_lang_itm lang = (Xoac_lang_itm)langs.Get_at(i);
String domain_str = String_.Format("{0}.{1}.org", String_.new_u8(lang.Key_bry()), wiki_tid_name_str); // EX: fr.wikipedia.org
byte[] domain_bry = Bry_.new_u8(domain_str);
Xowe_wiki lang_wiki = wiki.Appe().Wiki_mgr().Get_by_key_or_null(domain_bry);
boolean offline_exists = lang_wiki != null;
String url_fmt = String_.Format("http://" + domain_str + "/wiki/~{0}");
int lang_id = Xol_lang_itm_.Get_by_key(lang.Key_bry()).Id();
Xow_xwiki_itm xwiki = Xow_xwiki_itm.new_(lang.Key_bry(), Bry_.new_u8(url_fmt), lang_id, domain_tid, domain_bry).Offline_(offline_exists);
Add_itm(xwiki, lang);
}
lang_mgr.Grps_sort();
}
public void Add_itm(Xow_xwiki_itm itm) {Add_itm(itm, null);}
private void Add_itm(Xow_xwiki_itm xwiki, Xoac_lang_itm lang) {
byte[] xwiki_key = xwiki.Key_bry();
if ( !hash.Has(xwiki.Key_bry()) // only register xwiki / lang pair once
&& lang != null) // null lang should not be registered
lang_mgr.Itms_reg(xwiki, lang);
byte[] xwiki_domain = xwiki.Domain_bry();
if (!domain_hash.Has(xwiki_domain)) { // domain is new
domain_hash.Add(xwiki_domain, xwiki_key);
list.Add_if_dupe_use_nth(xwiki_key, xwiki); // only add to list if domain is new; some wikis like commons will be added multiple times under different aliases (commons, c, commons.wikimedia.org); need to check domain and add only once DATE:2014-11-07
}
hash.Add_if_dupe_use_nth(xwiki_key, xwiki);
} private final Hash_adp_bry domain_hash = Hash_adp_bry.ci_ascii_();
public void Add_many(byte[] v) {srl.Load_by_bry(v);}
public Object Invk(GfsCtx ctx, int ikey, String k, GfoMsg m) {
if (ctx.Match(k, Invk_count)) return list.Count();
else if (ctx.Match(k, Invk_add_bulk)) Add_bulk(m.ReadBry("v"));
else if (ctx.Match(k, Invk_add_bulk_langs)) Add_bulk_langs(m);
else if (ctx.Match(k, Invk_add_bulk_peers)) Add_bulk_peers(m.ReadBry("v"));
else if (ctx.Match(k, Invk_add_many)) Add_many(m.ReadBry("v"));
else if (ctx.Match(k, Invk_itms_print)) return Exec_itms_print(m.ReadBry("v"));
else if (ctx.Match(k, Invk_clear)) this.Clear();
else return GfoInvkAble_.Rv_unhandled;
return this;
}
private static final String
Invk_add_bulk = "add_bulk", Invk_add_bulk_langs = "add_bulk_langs", Invk_add_bulk_peers = "add_bulk_peers", Invk_add_many = "add_many"
, Invk_itms_print = "itms_print", Invk_count = "count", Invk_clear = "clear"
;
}

View File

@@ -0,0 +1,40 @@
/*
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.xwikis; import gplx.*; import gplx.xowa.*; import gplx.xowa.wikis.*;
import gplx.srls.dsvs.*; import gplx.xowa.wikis.*; import gplx.xowa.wikis.xwikis.*;
class Xow_xwiki_mgr_srl extends Dsv_wkr_base {
private byte[] key, url_fmt, name;
private final Xow_xwiki_mgr mgr;
private final Gfo_url_parser url_parser; private final Gfo_url tmp_url = new Gfo_url(); private final Bry_bfr tmp_bfr = Bry_bfr.reset_(255);
public Xow_xwiki_mgr_srl(Xow_xwiki_mgr mgr, Gfo_url_parser url_parser) {
this.mgr = mgr; this.url_parser = url_parser;
}
@Override public Dsv_fld_parser[] Fld_parsers() {return new Dsv_fld_parser[] {Dsv_fld_parser_.Bry_parser, Dsv_fld_parser_.Bry_parser, Dsv_fld_parser_.Bry_parser};}
@Override public boolean Write_bry(Dsv_tbl_parser parser, int fld_idx, byte[] src, int bgn, int end) {
switch (fld_idx) {
case 0: key = Bry_.Mid(src, bgn, end); return true;
case 1: url_fmt = Bry_.Mid(src, bgn, end); return true;
case 2: name = Bry_.Mid(src, bgn, end); return true;
default: return false;
}
}
@Override public void Commit_itm(Dsv_tbl_parser parser, int pos) {
Xow_xwiki_itm itm = Xow_xwiki_itm.new_by_mw(tmp_bfr, url_parser, tmp_url, key, url_fmt, name);
mgr.Add_itm(itm);
}
}

View File

@@ -0,0 +1,140 @@
/*
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.xwikis; import gplx.*; import gplx.xowa.*; import gplx.xowa.wikis.*;
import org.junit.*; import gplx.core.strings.*; import gplx.xowa.wikis.*; import gplx.xowa.langs.*;
public class Xow_xwiki_mgr_tst {
@Before public void init() {fxt.Clear();} private Xow_xwiki_mgr_fxt fxt = new Xow_xwiki_mgr_fxt();
@Test public void Add_bulk_wiki_en() {fxt.Test_add_bulk("w|en.wikipedia.org" , Xol_lang_itm_.Id__unknown , Xow_domain_type_.Tid_wikipedia , "w" , "https://en.wikipedia.org/wiki/~{0}", "en.wikipedia.org");}
@Test public void Add_bulk_wiki_fr() {fxt.Test_add_bulk("fr|fr.wikipedia.org" , Xol_lang_itm_.Id_fr , Xow_domain_type_.Tid_wikipedia , "fr" , "https://fr.wikipedia.org/wiki/~{0}", "fr.wikipedia.org");}
@Test public void Add_bulk_wikt_en() {fxt.Test_add_bulk("wikt|en.wiktionary.org" , Xol_lang_itm_.Id__unknown , Xow_domain_type_.Tid_wiktionary , "wikt" , "https://en.wiktionary.org/wiki/~{0}", "en.wiktionary.org");}
@Test public void Add_bulk_commons() {fxt.Test_add_bulk("commons|commons.wikimedia.org" , Xol_lang_itm_.Id__unknown , Xow_domain_type_.Tid_commons , "commons" , "https://commons.wikimedia.org/wiki/~{0}", "commons.wikimedia.org");}
@Test public void Add_bulk_commons_cap() {fxt.Test_add_bulk("Commons|commons.wikimedia.org" , Xol_lang_itm_.Id__unknown , Xow_domain_type_.Tid_commons , "Commons" , "https://commons.wikimedia.org/wiki/~{0}", "commons.wikimedia.org");}
@Test public void Add_bulk_langs_wiki() {fxt.Init_langs().Test_add_bulk_langs("wiki", fxt.xwiki_("en", "en.wikipedia.org", "http://en.wikipedia.org/wiki/~{0}"), fxt.xwiki_("de", "de.wikipedia.org", "http://de.wikipedia.org/wiki/~{0}"), fxt.xwiki_("fr", "fr.wikipedia.org", "http://fr.wikipedia.org/wiki/~{0}"), fxt.xwiki_("ja", "ja.wikipedia.org", "http://ja.wikipedia.org/wiki/~{0}"));}
@Test public void Add_bulk_langs_grps() {fxt.Init_langs().Test_add_bulk_langs("europe_west~asia_east", fxt.xwiki_("de", "de.wikipedia.org", "http://de.wikipedia.org/wiki/~{0}"), fxt.xwiki_("fr", "fr.wikipedia.org", "http://fr.wikipedia.org/wiki/~{0}"), fxt.xwiki_("ja", "ja.wikipedia.org", "http://ja.wikipedia.org/wiki/~{0}"));}
@Test public void Add_bulk_langs_grp_itm() {fxt.Init_langs().Test_add_bulk_langs("europe_west~ja", fxt.xwiki_("de", "de.wikipedia.org", "http://de.wikipedia.org/wiki/~{0}"), fxt.xwiki_("fr", "fr.wikipedia.org", "http://fr.wikipedia.org/wiki/~{0}"), fxt.xwiki_("ja", "ja.wikipedia.org", "http://ja.wikipedia.org/wiki/~{0}"));}
@Test public void Add_bulk_langs_grp_commons() {
fxt.Init_langs();
fxt.Wiki().Xwiki_mgr().Add_bulk_langs(Bry_.new_a7("europe_west"), Xow_domain_type_.Tid_wikipedia);
fxt.Tst_itms(fxt.xwiki_("de", "de.wikipedia.org", "http://de.wikipedia.org/wiki/~{0}"), fxt.xwiki_("fr", "fr.wikipedia.org", "http://fr.wikipedia.org/wiki/~{0}"));
}
@Test public void Add_bulk_peers() {fxt.Init_peers().Test_add_bulk_peers("peer", fxt.xwiki_null_("commons"), fxt.xwiki_null_("m"), fxt.xwiki_("wikt", "en.wiktionary.org", "http://en.wiktionary.org/wiki/~{0}"), fxt.xwiki_("wiktionary", "en.wiktionary.org", "http://en.wiktionary.org/wiki/~{0}"), fxt.xwiki_("s", "en.wikisource.org", "http://en.wikisource.org/wiki/~{0}"));}
@Test public void Add_bulk_peers_skip_self() {fxt.Init_peers().Test_add_bulk_peers("peer", fxt.xwiki_null_("wikipedia"), fxt.xwiki_("w", "en.wikipedia.org", "http://en.wikipedia.org/wiki/~{0}"));} // PURPOSE: skip "wikipedia" as alias since "Wikipedia" is namespace; needed for titles of "Wikipedia:Main page" (which would otherwise try to go to page "Main Page" in the main names of xwiki "Wikipedia"
@Test public void Add_bulk_core_wikidata() {fxt.Init_peers().Test_add_bulk_peers("core", fxt.xwiki_("d", "www.wikidata.org", "http://www.wikidata.org/wiki/~{0}"));}
@Test public void Add_bulk_peers_tid() { // PURPOSE:wikt should generate wiki_tid of wiktionary, not wikipedia; PAGE:en.s:Main_Page DATE:2014-09-14
fxt.Init_wikt ().Test_add_bulk_peers("peer", fxt.xwiki_("wikt", "en.wiktionary.org", "http://en.wiktionary.org/wiki/~{0}"));
}
@Test public void Multiple_aliases_should_only_add_once() { // PURPOSE.FIX: multiple aliases for same domain should only be added once to Get_at's list; DATE:2014-11-07
fxt.Exec_add_bulk("a1|a.org\na2|a.org").Test_len(1);
}
}
class Xow_xwiki_mgr_fxt {
Xow_xwiki_mgr xwiki_mgr; Xoa_lang_mgr lang_mgr; String_bldr sb = String_bldr_.new_(); Xoae_app app; Xowe_wiki wiki;
public void Clear() {
if (xwiki_mgr == null) {
app = Xoa_app_fxt.app_();
wiki = Xoa_app_fxt.wiki_tst_(app);
xwiki_mgr = wiki.Xwiki_mgr();
lang_mgr = app.Lang_mgr();
}
xwiki_mgr.Clear();
lang_mgr.Clear();
}
public Xowe_wiki Wiki() {return wiki;}
public Xow_xwiki_itm xwiki_null_(String key) {return Xow_xwiki_itm.new_(Bry_.new_u8(key), Bry_.Empty, Xol_lang_itm_.Id__unknown, Xow_domain_type_.Tid_other, Bry_.Empty);}
public Xow_xwiki_itm xwiki_(String key, String domain_str, String url_fmt) {
Xow_domain domain = Xow_domain_.parse(Bry_.new_u8(domain_str));
return Xow_xwiki_itm.new_(Bry_.new_u8(key), Bry_.new_u8(url_fmt), domain.Lang_itm().Id(), domain.Domain_tid(), domain.Domain_bry());
}
public Xow_xwiki_mgr_fxt Test_add_bulk(String raw, int lang_tid, int wiki_tid, String alias, String fmt, String domain) {
Xow_xwiki_itm itm = xwiki_mgr.Add_bulk_row(Xol_lang_itm_.Regy(), Bry_.new_a7(raw));
Tfds.Eq(alias, String_.new_a7(itm.Key_bry()));
Tfds.Eq(fmt, String_.new_a7(itm.Url_fmt()));
Tfds.Eq(wiki_tid, itm.Domain_tid(), "wiki_tid");
Tfds.Eq(lang_tid, itm.Lang_id(), "lang_id");
return this;
}
public Xow_xwiki_mgr_fxt Init_langs() {
lang_mgr.Groups().Set_bulk(Bry_.new_u8(String_.Concat_lines_nl
( "+||grp|wiki"
, "+|wiki|grp|english"
, "+|wiki|grp|europe_west"
, "+|wiki|grp|asia_east"
, "+|english|itm|en|English"
, "+|europe_west|itm|fr|French"
, "+|europe_west|itm|de|German"
, "+|asia_east|itm|ja|Japanese"
)));
return this;
}
public Xow_xwiki_mgr_fxt Init_peers() {
app.Wiki_mgr().Groups().Set_bulk(Bry_.new_u8(String_.Concat_lines_nl
( "+|core|itm|commons|commons"
, "+|core|itm|meta|meta;m"
, "+|core|itm|wikidata|d"
, "+|peer|itm|wiktionary|wikt;wiktionary"
, "+|peer|itm|wikisource|s"
, "+|peer|itm|wikipedia|w;wikipedia"
)));
return this;
}
public Xow_xwiki_mgr_fxt Init_wikt() {
app.Wiki_mgr().Groups().Set_bulk(Bry_.new_u8(String_.Concat_lines_nl
( "+|peer|itm|wiktionary|wikt;wiktionary"
)));
return this;
}
public Xow_xwiki_mgr_fxt Test_add_bulk_langs(String langs, Xow_xwiki_itm... itms) {
xwiki_mgr.Add_bulk_langs(Bry_.new_u8(langs));
Tfds.Eq_str_lines(Xto_str(itms), Xto_str(To_ary(itms)));
return this;
}
public Xow_xwiki_mgr_fxt Test_add_bulk_peers(String peers, Xow_xwiki_itm... itms) {
xwiki_mgr.Add_bulk_peers(Bry_.new_u8(peers));
Tfds.Eq_str_lines(Xto_str(itms), Xto_str(To_ary(itms)));
return this;
}
public Xow_xwiki_mgr_fxt Tst_itms(Xow_xwiki_itm... itms) {
Tfds.Eq_str_lines(Xto_str(itms), Xto_str(To_ary(itms)));
return this;
}
public Xow_xwiki_mgr_fxt Exec_add_bulk(String raw) {xwiki_mgr.Add_bulk(Bry_.new_u8(raw)); return this;}
public Xow_xwiki_mgr_fxt Test_len(int expd) {Tfds.Eq(expd, xwiki_mgr.Len()); return this;}
Xow_xwiki_itm[] To_ary(Xow_xwiki_itm[] itms) {
int len = itms.length;
List_adp rv = List_adp_.new_();
for (int i = 0; i < len; i++) {
byte[] alias = itms[i].Key_bry();
Xow_xwiki_itm itm = xwiki_mgr.Get_by_key(alias);
if (itm == null) itm = xwiki_null_(String_.new_u8(alias)); // "null", ignore
rv.Add(itm);
}
return (Xow_xwiki_itm[])rv.To_ary(Xow_xwiki_itm.class);
}
String Xto_str(Xow_xwiki_itm[] itms) {
int len = itms.length;
for (int i = 0; i < len; i++) {
Xow_xwiki_itm itm = itms[i];
if (Bry_.Len_eq_0(itm.Domain_bry())) // "null", ignore
sb.Add(itm.Key_bry()).Add_char_nl();
else {
sb.Add(itm.Key_bry()).Add_char_pipe().Add(itm.Domain_bry()).Add_char_pipe().Add(itm.Url_fmt()).Add_char_pipe().Add(itm.Domain_tid()).Add_char_nl();
}
}
return sb.Xto_str_and_clear();
}
}