mirror of
https://github.com/gnosygnu/xowa.git
synced 2026-03-02 03:49:30 +00:00
v2.10.3.1
This commit is contained in:
32
400_xowa/src/gplx/xowa/apps/urls/Xoa_url__basic__tst.java
Normal file
32
400_xowa/src/gplx/xowa/apps/urls/Xoa_url__basic__tst.java
Normal file
@@ -0,0 +1,32 @@
|
||||
/*
|
||||
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.apps.urls; import gplx.*; import gplx.xowa.*; import gplx.xowa.apps.*;
|
||||
import org.junit.*;
|
||||
public class Xoa_url__basic__tst {
|
||||
private final Xoa_url_fxt fxt = new Xoa_url_fxt();
|
||||
@Test public void Eq_page() {
|
||||
fxt.Test_eq_page(Bool_.Y, "en.wikipedia.org/wiki/A?redirect=yes", "en.wikipedia.org/wiki/A?redirect=yes");
|
||||
fxt.Test_eq_page(Bool_.N, "en.wikipedia.org/wiki/A?redirect=no" , "en.wikipedia.org/wiki/A?redirect=yes");
|
||||
}
|
||||
}
|
||||
class Xoa_url_fxt extends Xoa_url_parser_fxt { public void Test_eq_page(boolean expd, String lhs_str, String rhs_str) {
|
||||
Xoa_url lhs_url = parser.Parse(Bry_.new_u8(lhs_str));
|
||||
Xoa_url rhs_url = parser.Parse(Bry_.new_u8(rhs_str));
|
||||
Tfds.Eq_bool(expd, lhs_url.Eq_page(rhs_url), "Eq_page");
|
||||
}
|
||||
}
|
||||
56
400_xowa/src/gplx/xowa/apps/urls/Xoa_url__to_str__tst.java
Normal file
56
400_xowa/src/gplx/xowa/apps/urls/Xoa_url__to_str__tst.java
Normal 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.apps.urls; import gplx.*; import gplx.xowa.*; import gplx.xowa.apps.*;
|
||||
import org.junit.*; import gplx.xowa.htmls.hrefs.*;
|
||||
import gplx.xowa.wikis.nss.*;
|
||||
public class Xoa_url__to_str__tst {
|
||||
private final Xoa_url__to_str__fxt fxt = new Xoa_url__to_str__fxt();
|
||||
@Test public void Http() {fxt.Chk_to_str_href(Bool_.N, "http://a.org/b" , "http://a.org/b");}
|
||||
@Test public void File() {fxt.Chk_to_str_href(Bool_.N, "file:///C/xowa/file/a.png" , "file:///C/xowa/file/a.png");}
|
||||
@Test public void Abrv__page() {fxt.Chk_to_str_href(Bool_.N, "/wiki/A" , "A");}
|
||||
@Test public void Abrv__anch() {fxt.Chk_to_str_href(Bool_.N, "#b" , "#b");}
|
||||
@Test public void Full__page() {fxt.Chk_to_str_href(Bool_.Y, "/wiki/A" , "en.wikipedia.org/wiki/A");}
|
||||
@Test public void Full__anch() {fxt.Chk_to_str_href(Bool_.Y, "#b" , "en.wikipedia.org/wiki/Page_1#b");}
|
||||
@Test public void Vnt() {
|
||||
Xowe_wiki zh_wiki = fxt.Prep_create_wiki("zh.wikipedia.org");
|
||||
gplx.xowa.langs.vnts.Xol_vnt_regy_fxt.Init__vnt_mgr(zh_wiki.Lang().Vnt_mgr(), 0, String_.Ary("zh-hans", "zh-hant"));
|
||||
fxt.Chk_to_str_href(zh_wiki, Bool_.Y, "/site/zh.wikipedia.org/zh-hans/A" , "zh.wikipedia.org/zh-hans/A");
|
||||
fxt.Chk_to_str_href(zh_wiki, Bool_.Y, "/site/zh.wikipedia.org/zh-hant/A" , "zh.wikipedia.org/zh-hant/A");
|
||||
fxt.Chk_to_str_href(zh_wiki, Bool_.Y, "/site/zh.wikipedia.org/zh-cn/A" , "zh.wikipedia.org/wiki/A");
|
||||
fxt.Chk_to_str_href(zh_wiki, Bool_.Y, "/site/zh.wikipedia.org/wiki/A" , "zh.wikipedia.org/wiki/A");
|
||||
}
|
||||
@Test public void Xwiki() {
|
||||
fxt.Prep_add_xwiki_to_user("fr.wikipedia.org");
|
||||
fxt.Chk_to_str_href(Bool_.N, "/site/fr.wikipedia.org/wiki/Page", "fr.wikipedia.org/wiki/Page");
|
||||
}
|
||||
@Test public void Alias() {
|
||||
fxt.Prep_add_xwiki_to_wiki("wikt", "en.wiktionary.org");
|
||||
Xow_wiki en_d = fxt.Prep_create_wiki("en.wiktionary.org");
|
||||
en_d.Ns_mgr().Ns_main().Case_match_(Xow_ns_case_.Id_all);
|
||||
fxt.Chk_to_str_href(Bool_.N, "/wiki/wikt:a" , "en.wiktionary.org/wiki/a");
|
||||
}
|
||||
@Test public void Unknown() {fxt.Chk_to_str_href(Bool_.N, "/wiki/{{{extlink}}}" , "");} // {{{extlink}}} not a valid title; return empty
|
||||
}
|
||||
class Xoa_url__to_str__fxt extends Xoa_url_parser_fxt { private final Xoh_href_parser href_parser = new Xoh_href_parser();
|
||||
public void Chk_to_str_href(boolean full, String raw, String expd) {Chk_to_str_href(cur_wiki, full, raw, expd);}
|
||||
public void Chk_to_str_href(Xowe_wiki wiki, boolean full, String raw, String expd) {
|
||||
href_parser.Parse_as_url(actl_url, Bry_.new_u8(raw), wiki, Bry__page);
|
||||
this.Chk_to_str(full, expd);
|
||||
}
|
||||
private static final byte[] Bry__page = Bry_.new_a7("Page_1");
|
||||
}
|
||||
42
400_xowa/src/gplx/xowa/apps/urls/Xoa_url_encoder.java
Normal file
42
400_xowa/src/gplx/xowa/apps/urls/Xoa_url_encoder.java
Normal file
@@ -0,0 +1,42 @@
|
||||
/*
|
||||
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.apps.urls; import gplx.*; import gplx.xowa.*; import gplx.xowa.apps.*;
|
||||
public class Xoa_url_encoder {
|
||||
public byte[] Encode(byte[] src) {
|
||||
int src_len = src.length;
|
||||
for (int i = 0; i < src_len; i++) {
|
||||
byte b = src[i];
|
||||
switch (b) {
|
||||
case Byte_ascii.Space: bb.Add(Bry_underline); break;
|
||||
case Byte_ascii.Amp: bb.Add(Bry_amp); break;
|
||||
case Byte_ascii.Apos: bb.Add(Bry_apos); break;
|
||||
case Byte_ascii.Eq: bb.Add(Bry_eq); break;
|
||||
case Byte_ascii.Plus: bb.Add(Bry_plus); break;
|
||||
default: bb.Add_byte(b); break;
|
||||
// FUTURE: html_entities, etc:
|
||||
}
|
||||
}
|
||||
return bb.To_bry_and_clear();
|
||||
}
|
||||
private static final byte[] Bry_amp = Bry_.new_a7("%26"), Bry_eq = Bry_.new_a7("%3D")
|
||||
, Bry_plus = Bry_.new_a7("%2B"), Bry_apos = Bry_.new_a7("%27")
|
||||
, Bry_underline = new byte[] {Byte_ascii.Underline}
|
||||
;
|
||||
Bry_bfr bb = Bry_bfr.new_();
|
||||
public static final Xoa_url_encoder Instance = new Xoa_url_encoder(); Xoa_url_encoder() {}
|
||||
}
|
||||
314
400_xowa/src/gplx/xowa/apps/urls/Xoa_url_parser.java
Normal file
314
400_xowa/src/gplx/xowa/apps/urls/Xoa_url_parser.java
Normal file
@@ -0,0 +1,314 @@
|
||||
/*
|
||||
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.apps.urls; import gplx.*; import gplx.xowa.*; import gplx.xowa.apps.*;
|
||||
import gplx.core.primitives.*; import gplx.core.net.*; import gplx.langs.htmls.encoders.*;
|
||||
import gplx.xowa.htmls.hrefs.*;
|
||||
import gplx.xowa.langs.*; import gplx.xowa.langs.vnts.*;
|
||||
import gplx.xowa.wikis.nss.*;
|
||||
import gplx.xowa.wikis.domains.*; import gplx.xowa.wikis.xwikis.*; import gplx.xowa.files.*;
|
||||
public class Xoa_url_parser {
|
||||
private final Url_encoder encoder;
|
||||
private final Bry_bfr tmp_bfr = Bry_bfr.reset_(255);
|
||||
private final Gfo_url_parser url_parser = new Gfo_url_parser(); private final Gfo_url gfo_url = new Gfo_url();
|
||||
private final Xoa_app app; private final Xow_wiki wiki; private final byte[] domain_bry;
|
||||
private byte tmp_protocol_tid;
|
||||
private int tmp_tid;
|
||||
private byte[] tmp_raw, tmp_wiki, tmp_page, tmp_anch, tmp_protocol_bry; private Gfo_qarg_itm[] tmp_qargs;
|
||||
private byte[][] tmp_segs; private int tmp_segs_len;
|
||||
private boolean tmp_protocol_is_relative, tmp_page_is_main, tmp_wiki_is_missing;
|
||||
private byte[] tmp_vnt;
|
||||
private final Xol_vnt_mgr vnt_mgr;
|
||||
public Xoa_url_parser(Xow_wiki wiki) {
|
||||
this.app = wiki.App();
|
||||
this.wiki = wiki; this.domain_bry = wiki.Domain_bry();
|
||||
this.encoder = app.Utl__encoder_mgr().Xourl();
|
||||
this.vnt_mgr = wiki.Type_is_edit() ? wiki.Lang().Vnt_mgr() : null;
|
||||
}
|
||||
public Xoa_url Parse_by_urlbar_or_null(String str) {
|
||||
Xoae_app app = (Xoae_app)wiki.App();
|
||||
byte[] bry = Strip_mobile_segment(Bry_.new_u8(str));
|
||||
byte[] fmt = app.Gui_mgr().Url_macro_mgr().Fmt_or_null(bry);
|
||||
if (fmt != null) bry = fmt;
|
||||
Xoa_url rv = Xoa_url.blank();
|
||||
this.Parse(rv, bry, 0, bry.length); if (rv.Page_bry() == null) {Xoa_url_.Invalid_warn(str); return null;}
|
||||
byte[] wiki_bry = rv.Wiki_bry();
|
||||
Xow_xwiki_itm xwiki_itm = app.User().Wikii().Xwiki_mgr().Get_by_key(wiki_bry);
|
||||
if (xwiki_itm == null) {Xoa_url_.Invalid_warn(str); return null;} // if wiki doesn't exist, warn and return nothing; DATE:2015-08-25
|
||||
if (rv.Page_is_main()) { // Main_Page requested; EX: "zh.wikipedia.org"; "zh.wikipedia.org/wiki/"; DATE:2014-02-16
|
||||
Xow_wiki wiki_itm = app.Wiki_mgri().Get_by_key_or_make_init_y(wiki_bry); // NOTE: must call Init to load Main_Page; only call if from url_bar, else all sister wikis will be loaded when parsing Sister_wikis panel
|
||||
rv.Page_bry_(wiki_itm.Props().Main_page());
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
public Gfo_url_parser Url_parser() {return url_parser;}
|
||||
public Xoa_url Parse(byte[] src) {Xoa_url rv = Xoa_url.blank(); Parse(rv, src); return rv;}
|
||||
public Xoa_url Parse(byte[] src, int bgn, int end) {Xoa_url rv = Xoa_url.blank(); Parse(rv, src, bgn, end); return rv;}
|
||||
public boolean Parse(Xoa_url rv, byte[] src) {return Parse(rv, src, 0, src.length);}
|
||||
public boolean Parse(Xoa_url rv, byte[] src, int bgn, int end) {
|
||||
if (end - bgn == 0) {Init_tmp_vars(Gfo_url.Empty); Make(rv); return false;}
|
||||
src = encoder.Decode(src, bgn, end); // NOTE: must decode any url-encoded parameters
|
||||
int src_len = src.length;
|
||||
url_parser.Parse(gfo_url, src, 0, src_len); // parse to plain gfo_url
|
||||
Init_tmp_vars(gfo_url);
|
||||
if (src[0] == Byte_ascii.Hash) // src is anch; EX: #A
|
||||
Bld_anch();
|
||||
else {
|
||||
switch (tmp_protocol_tid) {
|
||||
case Gfo_protocol_itm.Tid_file:
|
||||
if (src_len > 5 && src[5] != Byte_ascii.Slash) // src is ttl in [[File]] ns; EX: "File:A.png"
|
||||
Bld_page_by_file_ns();
|
||||
else // src is file:///; EX: EX: "file:///C:/A.png"
|
||||
tmp_tid = Xoa_url_.Tid_file;
|
||||
break;
|
||||
case Gfo_protocol_itm.Tid_xowa:
|
||||
Bld_xowa();
|
||||
break;
|
||||
case Gfo_protocol_itm.Tid_http:
|
||||
case Gfo_protocol_itm.Tid_https:
|
||||
case Gfo_protocol_itm.Tid_relative_1:
|
||||
if (tmp_protocol_tid == Gfo_protocol_itm.Tid_relative_1) // interpret relative protocol links to match wiki's protocol; EX: [//a.org] -> https://a.org for all WMF wikis; DATE:2015-07-27
|
||||
tmp_protocol_tid = wiki.Props().Protocol_tid();
|
||||
if (app.User().Wikii().Xwiki_mgr().Get_by_key(tmp_wiki) != null)// src is offline wiki; EX: http://fr.wikipedia.org/wiki/A
|
||||
Bld_page(0);
|
||||
else if (Bry_.Eq(tmp_wiki, Bry_upload_wikimedia_org)) // src is upload.wikimedia.org; EX: "http://upload.wikimedia.org/wikipedia/commons/a/ab/C.svg"
|
||||
Bld_page_from_upload_wikimedia_org();
|
||||
else // src is unknown site: EX: "http://a.org"
|
||||
tmp_tid = Xoa_url_.Tid_inet;
|
||||
break;
|
||||
case Gfo_protocol_itm.Tid_unknown:
|
||||
Bld_page(0);
|
||||
break;
|
||||
default:
|
||||
tmp_tid = Xoa_url_.Tid_inet;
|
||||
break;
|
||||
}
|
||||
}
|
||||
Bld_qargs();
|
||||
if (tmp_page_is_main) tmp_page = Xoa_page_.Main_page_bry_empty;
|
||||
if (tmp_anch != null) {
|
||||
byte[] anchor_bry = Xoa_app_.Utl__encoder_mgr().Id().Encode(tmp_anch); // reencode for anchors (which use . encoding, not % encoding); PAGE:en.w:Enlightenment_Spain#Enlightened_despotism_.281759%E2%80%931788.29
|
||||
tmp_anch = anchor_bry;
|
||||
}
|
||||
Make(rv);
|
||||
return true;
|
||||
}
|
||||
private void Init_tmp_vars(Gfo_url gfo_url) {
|
||||
tmp_tid = Xoa_url_.Tid_unknown;
|
||||
tmp_raw = gfo_url.Raw();
|
||||
tmp_wiki = gfo_url.Segs__get_at_1st();
|
||||
tmp_page = gfo_url.Segs__get_at_nth();
|
||||
tmp_anch = gfo_url.Anch(); tmp_qargs = gfo_url.Qargs();
|
||||
tmp_wiki_is_missing = false;
|
||||
tmp_page_is_main = false;
|
||||
tmp_protocol_tid = gfo_url.Protocol_tid();
|
||||
tmp_protocol_bry = gfo_url.Protocol_bry();
|
||||
tmp_protocol_is_relative = gfo_url.Protocol_tid() == Gfo_protocol_itm.Tid_relative_1; // gfo_url.Protocol_is_relative();
|
||||
if (tmp_protocol_is_relative) tmp_protocol_tid = Gfo_protocol_itm.Tid_https;
|
||||
tmp_vnt = null;
|
||||
tmp_segs = gfo_url.Segs();
|
||||
tmp_segs_len = tmp_segs.length;
|
||||
}
|
||||
private Xoa_url Make(Xoa_url rv) {
|
||||
rv.Ctor
|
||||
( tmp_tid, tmp_raw, tmp_protocol_tid, tmp_protocol_bry, tmp_protocol_is_relative
|
||||
, tmp_wiki, tmp_page, tmp_qargs, tmp_anch
|
||||
, tmp_segs, tmp_vnt, tmp_wiki_is_missing, Bry_.Eq(tmp_wiki, wiki.Domain_bry()), tmp_page_is_main);
|
||||
return rv;
|
||||
}
|
||||
private void Bld_anch() {
|
||||
tmp_tid = Xoa_url_.Tid_anch;
|
||||
tmp_wiki = domain_bry; tmp_wiki_is_missing = true;
|
||||
tmp_page = Bry_.Empty;
|
||||
}
|
||||
private void Bld_xowa() {
|
||||
tmp_tid = Xoa_url_.Tid_xcmd;
|
||||
tmp_page = Bry_.Mid(tmp_raw, Gfo_protocol_itm.Len_xcmd); // NOTE: just get String after protocol; anchor (#) or query params (?) must not be parsed
|
||||
tmp_page = Xoa_app_.Utl__encoder_mgr().Gfs().Decode(tmp_page); // NOTE: should be decoded; EX: %20 -> " "
|
||||
}
|
||||
private void Bld_page_by_file_ns() {
|
||||
tmp_tid = Xoa_url_.Tid_page;
|
||||
tmp_segs[0] = Bry_.Add(Bry_file, tmp_wiki);
|
||||
tmp_page = Make_page_from_segs(0);
|
||||
tmp_wiki = domain_bry; tmp_wiki_is_missing = true;
|
||||
}
|
||||
private void Bld_page_from_upload_wikimedia_org() {
|
||||
// orig: https://upload.wikimedia.org/wikipedia/commons/a/ab/A.jpg
|
||||
// thum: https://upload.wikimedia.org/wikipedia/commons/thumb/a/ab/A.jpg/220px-A.jpg
|
||||
byte[] domain_type = tmp_segs[1]; // seg[0] = type; EX: "/wikipedia/"
|
||||
byte[] lang = tmp_segs[2]; // seg[1] = lang; EX: "en", "fr"; "commons"
|
||||
if (Bry_.Eq(lang, Xow_domain_tid_.Bry__commons)) // commons links will have fmt of "/wikipedia/commons"; must change to wikimedia
|
||||
domain_type = Xow_domain_tid_.Bry__wikimedia;
|
||||
tmp_wiki = tmp_bfr.Clear()
|
||||
.Add(lang).Add_byte(Byte_ascii.Dot) // add lang/type + .; EX: "en."; "fr."; "commons."
|
||||
.Add(domain_type).Add(Bry_dot_org) // add type + .org; EX: "wikipedia.org"; "wikimedia.org";
|
||||
.To_bry_and_clear();
|
||||
if (tmp_segs_len > 6 && Bry_.Eq(tmp_segs[3], Xof_url_bldr.Bry_thumb)) tmp_page = tmp_segs[6]; // if "/thumb/", set page from seg[n-1] to seg[6]; EX: using thumb example above, "A.jpg", not "220px-A.jpg"
|
||||
tmp_page = Bry_.Add(Bry_file, tmp_page); // always add "File:" ns
|
||||
}
|
||||
private boolean Find_wiki(byte[] domain) {
|
||||
Xow_xwiki_itm xwiki = app.User().Wikii().Xwiki_mgr().Get_by_key(domain); // check if tmp_wiki is known wiki
|
||||
if (xwiki != null) {
|
||||
if (!Bry_.Eq(xwiki.Domain_bry(), domain)) // ignore aliases by checking that xwiki.domain == wiki; avoids false lang matches like "so/page" or "C/page"; PAGE: ca.s:So/Natura_del_so; DATE:2014-04-26; PAGE:no.b:C/Variabler; DATE:2014-10-14
|
||||
xwiki = null;
|
||||
}
|
||||
if (xwiki != null) return true;
|
||||
if (app.Wiki_mgri().Has(domain)) return true;
|
||||
return Byte_.In(tmp_protocol_tid, Gfo_protocol_itm.Tid_http, Gfo_protocol_itm.Tid_https);
|
||||
}
|
||||
private void Bld_page(int bgn_seg) {
|
||||
tmp_tid = Xoa_url_.Tid_page;
|
||||
tmp_wiki = tmp_segs[bgn_seg]; // try seg[0] as wiki
|
||||
int seg_idx = bgn_seg + 1;
|
||||
boolean wiki_exists = Find_wiki(tmp_wiki);
|
||||
if (wiki_exists) {
|
||||
int tmp_seg_idx = Bld_main_page_or_vnt(bgn_seg);
|
||||
seg_idx = tmp_seg_idx;
|
||||
int tmp_vnt_seg = bgn_seg + 1;
|
||||
if (vnt_mgr.Enabled() && tmp_vnt_seg < tmp_segs_len && vnt_mgr.Regy().Has(tmp_segs[tmp_vnt_seg])) { // check if "/zh-hans/"
|
||||
tmp_vnt = tmp_segs[tmp_vnt_seg];
|
||||
}
|
||||
if (tmp_seg_idx == -1) return; // main_page or vnt; exit
|
||||
}
|
||||
else {
|
||||
tmp_wiki = domain_bry; // tmp_wiki is current
|
||||
tmp_wiki_is_missing = true;
|
||||
--seg_idx; // move seg_idx back to wiki
|
||||
}
|
||||
tmp_page = tmp_segs[seg_idx];
|
||||
byte[] frag = Bld_page_by_alias(tmp_page); // handle en.wikipedia.org/wiki/fr:A
|
||||
if (frag != null) {
|
||||
tmp_segs[seg_idx] = frag; // alias found; set page to rhs; EX: "fr:A" -> "A"
|
||||
if (frag.length == 0) // handle main_page; EX: "fr:"
|
||||
tmp_page_is_main = true;
|
||||
}
|
||||
tmp_page = Make_page_from_segs(seg_idx); // join segs together; needed for nesting; EX: A/B/C
|
||||
}
|
||||
private int Bld_main_page_or_vnt(int bgn_seg) {
|
||||
int rv = -1;
|
||||
switch (tmp_segs_len - bgn_seg) {
|
||||
case 1: // "en.wikipedia.org"
|
||||
if (Bry_.Eq(tmp_segs[0 + bgn_seg], Xow_domain_itm_.Bry__home)) { // ignore "home" which should always go to "home" of current wiki, not "home" wiki
|
||||
tmp_wiki = domain_bry;
|
||||
return 0;
|
||||
}
|
||||
else {
|
||||
tmp_page_is_main = true;
|
||||
return -1;
|
||||
}
|
||||
case 2: // "en.wikipedia.org/"; "en.wikipedia.org/wiki"; "en.wikipedia.org/A"
|
||||
if (Bry_.Len_eq_0(tmp_segs[1 + bgn_seg])) { // check for "en.wikipedia.org/"
|
||||
tmp_page_is_main = true;
|
||||
return -1;
|
||||
}
|
||||
else
|
||||
rv = 1;
|
||||
break;
|
||||
case 3:
|
||||
if (Bry_.Len_gt_0(tmp_segs[2 + bgn_seg])) // check only for "en.wikipedia.org/wiki/" where seg[2] is blank
|
||||
return 2;
|
||||
else
|
||||
rv = 2;
|
||||
break;
|
||||
default:
|
||||
return 2;
|
||||
}
|
||||
byte[] mid = tmp_segs[1 + bgn_seg];
|
||||
if (Bry_.Eq(mid, Bry_wiki)) { // check if "/wiki/"
|
||||
tmp_page_is_main = true;
|
||||
return -1;
|
||||
}
|
||||
else
|
||||
return rv;
|
||||
}
|
||||
private byte[] Bld_page_by_alias(byte[] bry) {
|
||||
if (bry == null) return null;
|
||||
int colon_pos = Bry_find_.Find_fwd(bry, Byte_ascii.Colon); // check for colon; EX: commons:Earth
|
||||
if (colon_pos == Bry_.NotFound) return null; // no colon
|
||||
Xow_wiki alias_wiki = wiki; // default alias_wiki to cur_wiki
|
||||
if (!tmp_wiki_is_missing) // tmp_wiki exists; use it for alias wikis; DATE:2015-09-17
|
||||
alias_wiki = wiki.App().Wiki_mgri().Get_by_key_or_make_init_n(tmp_wiki);
|
||||
Xow_xwiki_itm alias_itm = alias_wiki.Xwiki_mgr().Get_by_mid(bry, 0, colon_pos); // check for alias;
|
||||
if (alias_itm == null) return null; // colon-word is not alias; EX:A:B
|
||||
Xow_ns_mgr ns_mgr = tmp_wiki_is_missing ? wiki.Ns_mgr() : wiki.App().Meta_mgr().Ns__get_or_load(tmp_wiki);
|
||||
if (ns_mgr.Names_get_or_null(alias_itm.Key_bry()) != null) // special case to handle collision between "wikipedia" alias and "Wikipedia" namespace; if alias exists as ns, ignore it; EX:sv.wikipedia.org/wiki/Wikipedia:Main_Page DATE:2015-07-31
|
||||
return null;
|
||||
byte[] rv = Bry_.Mid(bry, colon_pos + 1);
|
||||
tmp_wiki = alias_itm.Domain_bry();
|
||||
return rv;
|
||||
}
|
||||
private void Bld_qargs() {
|
||||
int qargs_len = tmp_qargs.length;
|
||||
for (int i = 0; i < qargs_len; ++i) {
|
||||
Gfo_qarg_itm qarg = tmp_qargs[i];
|
||||
if ( Bry_.Eq(qarg.Key_bry(), Qarg__title)
|
||||
&& qarg.Val_bry() != null // HACK: handle "bad-urls" from xml-encoded entities where page is null b/c = is encoded as =; http://en.wikipedia.org/w/index.php?action=edit&preload=Template:Afd2+starter&editintro=Template:Afd3+starter&title=Wikipedia:Articles+for+deletion/Template standardisation/demometa DATE:2015-08-02
|
||||
)
|
||||
tmp_page = qarg.Val_bry(); // handle /w/index.php?title=Earth
|
||||
}
|
||||
}
|
||||
private byte[] Make_page_from_segs(int bgn) {
|
||||
if (tmp_segs_len - bgn == 1) return tmp_segs[tmp_segs_len - 1]; // only 1 item; just return it; don't build bry
|
||||
for (int i = bgn; i < tmp_segs_len; i++) {
|
||||
if (i != bgn) tmp_bfr.Add_byte(Byte_ascii.Slash);
|
||||
tmp_bfr.Add(tmp_segs[i]);
|
||||
}
|
||||
return tmp_bfr.To_bry_and_clear();
|
||||
}
|
||||
public String Build_str(Xoa_url url) { // transform to "canonical" form that fits url box for both XOWA and Mozilla Firefox
|
||||
tmp_bfr.Add(url.Wiki_bry()); // add wiki; EX: "en.wikipedia.org"
|
||||
tmp_bfr.Add(Xoh_href_.Bry__wiki); // add "/wiki/" EX: "/wiki/"
|
||||
tmp_bfr.Add(encoder.Decode(url.Page_bry())); // add page; EX: "A"
|
||||
int args_len = url.Qargs_ary().length;
|
||||
if (args_len > 0) {
|
||||
for (int i = 0; i < args_len; i++) {
|
||||
byte dlm = i == 0 ? Byte_ascii.Question : Byte_ascii.Amp;
|
||||
tmp_bfr.Add_byte(dlm);
|
||||
Gfo_qarg_itm arg = url.Qargs_ary()[i];
|
||||
tmp_bfr.Add(arg.Key_bry()).Add_byte(Byte_ascii.Eq).Add(arg.Val_bry());
|
||||
}
|
||||
}
|
||||
if (url.Anch_bry() != null)
|
||||
tmp_bfr.Add_byte(Byte_ascii.Hash).Add(url.Anch_bry()); // add anchor; EX: "#B"
|
||||
return tmp_bfr.To_str_and_clear();
|
||||
}
|
||||
private static byte[] Strip_mobile_segment(byte[] v) {// DATE:2014-05-03
|
||||
int pos = Bry_find_.Find_fwd(v, Byte_ascii.Dot);
|
||||
if ( pos == Bry_find_.Not_found // no dot; EX: "A"
|
||||
|| pos + 2 >= v.length // not enough space for .m.; EX: "A.b"
|
||||
)
|
||||
return v;
|
||||
switch (v[pos + 1]) { // check for m
|
||||
case Byte_ascii.Ltr_M:
|
||||
case Byte_ascii.Ltr_m:
|
||||
break;
|
||||
default:
|
||||
return v;
|
||||
}
|
||||
if (v[pos + 2] != Byte_ascii.Dot) return v;
|
||||
return Bry_.Add(Bry_.Mid(v, 0, pos), Bry_.Mid(v, pos + 2)); // skip ".m"
|
||||
}
|
||||
private static final byte[] Qarg__title = Bry_.new_a7("title");
|
||||
private static final byte[]
|
||||
Bry_upload_wikimedia_org = Bry_.new_a7("upload.wikimedia.org")
|
||||
, Bry_file = Bry_.new_a7("File:") // NOTE: File does not need i18n; is a canonical namespace
|
||||
, Bry_wiki = Bry_.new_a7("wiki")
|
||||
;
|
||||
public static final byte[]
|
||||
Bry_dot_org = Bry_.new_a7(".org")
|
||||
;
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
/*
|
||||
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.apps.urls; import gplx.*; import gplx.xowa.*; import gplx.xowa.apps.*;
|
||||
import org.junit.*;
|
||||
public class Xoa_url_parser__proto_tst {
|
||||
private final Xoa_url_parser_fxt tstr = new Xoa_url_parser_fxt();
|
||||
@Test public void Relative() {
|
||||
tstr.Run_parse("//en.wikipedia.org/wiki/A").Chk_wiki("en.wikipedia.org").Chk_page("A");
|
||||
}
|
||||
@Test public void Http__basic() {
|
||||
tstr.Run_parse("http://en.wikipedia.org/wiki/A").Chk_wiki("en.wikipedia.org").Chk_page("A");
|
||||
}
|
||||
@Test public void Upload__basic() {
|
||||
tstr.Prep_add_xwiki_to_user("commons.wikimedia.org"); // NOTE: need to add xwiki to be able to resolve "/commons/"
|
||||
tstr.Run_parse("http://upload.wikimedia.org/wikipedia/commons/a/ab/C.svg").Chk_wiki("commons.wikimedia.org").Chk_page("File:C.svg"); // orig
|
||||
tstr.Run_parse("http://upload.wikimedia.org/wikipedia/commons/thumb/7/70/A.png/220px-A.png").Chk_wiki("commons.wikimedia.org").Chk_page("File:A.png"); // thum
|
||||
}
|
||||
@Test public void File__basic() {
|
||||
tstr.Run_parse("file:///C:/a/b/c").Chk_tid(Xoa_url_.Tid_file);
|
||||
}
|
||||
@Test public void Ftp__basic() {
|
||||
tstr.Run_parse("ftp://en.wikipedia.org/wiki/A").Chk_tid(Xoa_url_.Tid_inet);
|
||||
}
|
||||
@Test public void Extended() {
|
||||
tstr.Run_parse("http://en.wikipedia.org/w/index.php?A=B").Chk_wiki("en.wikipedia.org").Chk_page("index.php").Chk_qargs("?A=B").Chk_anch(null);
|
||||
}
|
||||
}
|
||||
@@ -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.apps.urls; import gplx.*; import gplx.xowa.*; import gplx.xowa.apps.*;
|
||||
import org.junit.*;
|
||||
public class Xoa_url_parser__qarg_tst {
|
||||
private final Xoa_url_parser_fxt tstr = new Xoa_url_parser_fxt();
|
||||
@Test public void Redirect() {
|
||||
tstr.Run_parse("A?redirect=no").Chk_wiki("en.wikipedia.org").Chk_page("A").Chk_qargs("?redirect=no");
|
||||
}
|
||||
@Test public void Action_is_edit() {
|
||||
tstr.Run_parse("A?action=edit").Chk_wiki("en.wikipedia.org").Chk_page("A").Chk_action_is_edit_y();
|
||||
}
|
||||
@Test public void Assert_state_cleared() { // PURPOSE.fix: action_is_edit (et. al.) was not being cleared on parse even though Xoa_url reused; DATE:20121231
|
||||
tstr.Run_parse("A?action=edit") .Chk_action_is_edit_y();
|
||||
tstr.Run_parse_reuse("B") .Chk_action_is_edit_n();
|
||||
}
|
||||
@Test public void Query_arg() { // PURPOSE.fix: query args were not printing out
|
||||
tstr.Run_parse("en.wikipedia.org/wiki/Special:Search/Earth?fulltext=yes").Chk_build_str_is_same();
|
||||
}
|
||||
@Test public void Dupe_key() {
|
||||
tstr.Run_parse("A?B=C1&B=C2").Chk_page("A").Chk_qargs("?B=C1&B=C2");
|
||||
}
|
||||
@Test public void Question_is_eos() {
|
||||
tstr.Run_parse("A?").Chk_wiki("en.wikipedia.org").Chk_page("A?").Chk_qargs("");
|
||||
}
|
||||
@Test public void Question_is_page() {
|
||||
tstr.Run_parse("A?B").Chk_wiki("en.wikipedia.org").Chk_page("A?B").Chk_qargs("");
|
||||
}
|
||||
@Test public void Question_is_anchor() {
|
||||
tstr.Run_parse("A#b?c").Chk_wiki("en.wikipedia.org").Chk_page("A").Chk_anch("b.3Fc");
|
||||
}
|
||||
@Test public void Title_remove_w() { // PURPOSE: fix /w/ showing up as seg; DATE:2014-05-30
|
||||
tstr.Run_parse("http://en.wikipedia.org/w/index.php?title=A").Chk_wiki("en.wikipedia.org").Chk_page("A");
|
||||
}
|
||||
@Test public void Search() {
|
||||
tstr.Run_parse("Special:Search/Moon%3Ffulltext%3Dy%26xowa_page_index%3D1").Chk_page("Special:Search/Moon").Chk_qargs("?fulltext=y&xowa_page_index=1");
|
||||
}
|
||||
@Test public void Ctg() {
|
||||
tstr.Run_parse("Category:A?pagefrom=A#mw-pages").Chk_page("Category:A").Chk_qargs("?pagefrom=A").Chk_anch("mw-pages");
|
||||
}
|
||||
@Test public void Encoded() {
|
||||
tstr.Run_parse("en.wikipedia.org/wiki/A?action=edit&preload=B").Chk_wiki("en.wikipedia.org").Chk_page("A").Chk_qargs("?action==edit=&preload=&=").Chk_anch("61.3BB"); // NOTE: this is wrong; fix later
|
||||
}
|
||||
}
|
||||
@@ -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.apps.urls; import gplx.*; import gplx.xowa.*; import gplx.xowa.apps.*;
|
||||
import org.junit.*;
|
||||
public class Xoa_url_parser__ttl_tst {
|
||||
private final Xoa_url_parser_fxt tstr = new Xoa_url_parser_fxt();
|
||||
@Test public void Name() {
|
||||
tstr.Run_parse("A").Chk_wiki("en.wikipedia.org").Chk_page("A");
|
||||
}
|
||||
@Test public void Sub_1() {
|
||||
tstr.Run_parse("A/b").Chk_wiki("en.wikipedia.org").Chk_page("A/b");
|
||||
}
|
||||
@Test public void Sub_2() {
|
||||
tstr.Run_parse("A/b/c").Chk_wiki("en.wikipedia.org").Chk_page("A/b/c");
|
||||
}
|
||||
@Test public void Anch() {
|
||||
tstr.Run_parse("A#b").Chk_wiki("en.wikipedia.org").Chk_page("A").Chk_anch("b");
|
||||
}
|
||||
@Test public void Anch_w_slash() { // PURPOSE: A/b#c/d was not parsing correctly; PAGE:en.w:Enlightenment_Spain#Enlightened_despotism_.281759%E2%80%931788.29
|
||||
tstr.Run_parse("A/b#c/d").Chk_page("A/b").Chk_anch("c.2Fd");
|
||||
}
|
||||
@Test public void Ns_category() {
|
||||
tstr.Run_parse("Category:A").Chk_wiki("en.wikipedia.org").Chk_page("Category:A");
|
||||
}
|
||||
@Test public void Main_page__basic() {
|
||||
tstr.Run_parse("en.wikipedia.org") .Chk_wiki("en.wikipedia.org").Chk_page_is_main_y();
|
||||
tstr.Run_parse("en.wikipedia.org/") .Chk_wiki("en.wikipedia.org").Chk_page_is_main_y();
|
||||
tstr.Run_parse("en.wikipedia.org/wiki") .Chk_wiki("en.wikipedia.org").Chk_page_is_main_y();
|
||||
tstr.Run_parse("en.wikipedia.org/wiki/") .Chk_wiki("en.wikipedia.org").Chk_page_is_main_y();
|
||||
tstr.Run_parse("en.wikipedia.org/wiki/A") .Chk_wiki("en.wikipedia.org").Chk_page_is_main_n();
|
||||
}
|
||||
@Test public void Ns_file__basic() {// PURPOSE: "File:A" should not be mistaken for "file:///" ns
|
||||
tstr.Run_parse("File:A").Chk_wiki("en.wikipedia.org").Chk_page("File:A");
|
||||
}
|
||||
@Test public void Ns_file__nested() {// PURPOSE: handle fictitious "File:A/B/C.png"
|
||||
tstr.Run_parse("File:A/B/C.png").Chk_wiki("en.wikipedia.org").Chk_page("File:A/B/C.png"); // should not be C.png b/c of Gfo_url_parser_old
|
||||
}
|
||||
@Test public void Anch__basic() {// DATE:2015-07-26
|
||||
tstr.Run_parse("#A").Chk_tid(Xoa_url_.Tid_anch).Chk_wiki_is_missing(true).Chk_page("").Chk_anch("A");
|
||||
}
|
||||
}
|
||||
@@ -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.apps.urls; import gplx.*; import gplx.xowa.*; import gplx.xowa.apps.*;
|
||||
import org.junit.*;
|
||||
public class Xoa_url_parser__url_bar_tst {
|
||||
private final Xoa_url_parser_fxt tstr = new Xoa_url_parser_fxt();
|
||||
@Test public void Basic() {
|
||||
tstr.Run_parse_from_url_bar("Page_1").Chk_to_str("en.wikipedia.org/wiki/Page_1"); // basic
|
||||
}
|
||||
@Test public void Lang() {
|
||||
tstr.Prep_add_xwiki_to_user("uk", "uk.wikipedia.org");
|
||||
tstr.Run_parse_from_url_bar("uk").Chk_to_str("en.wikipedia.org/wiki/uk"); // lang-like page (uk=Ukraine) should not try to open wiki; DATE:2014-02-07
|
||||
}
|
||||
@Test public void Lang_like() {
|
||||
tstr.Prep_add_xwiki_to_user("uk", "uk.wikipedia.org", "http://~{1}.wikipedia.org"); // NOTE: fmt needed for Type_is_lang
|
||||
tstr.Run_parse_from_url_bar("uk/A").Chk_to_str("en.wikipedia.org/wiki/uk/A"); // uk/A should not try be interpreted as wiki="uk" page="A"; DATE:2014-04-26
|
||||
}
|
||||
@Test public void Macro() {
|
||||
tstr.Prep_add_xwiki_to_user("fr.wikisource.org");
|
||||
tstr.Run_parse_from_url_bar("fr.s:Auteur:Shakespeare").Chk_to_str("fr.wikisource.org/wiki/Auteur:Shakespeare"); // url_macros
|
||||
}
|
||||
@Test public void Main_page__home() {
|
||||
tstr.Run_parse_from_url_bar("home").Chk_to_str("en.wikipedia.org/wiki/home"); // home should go to current wiki's home; DATE:2014-02-09
|
||||
tstr.Run_parse_from_url_bar("home/wiki/Main_Page").Chk_to_str("home/wiki/Main_Page"); // home Main_Page should go to home; DATE:2014-02-09
|
||||
}
|
||||
@Test public void Main_page__zhw() {
|
||||
Xowe_wiki zh_wiki = tstr.Prep_create_wiki("zh.wikipedia.org");
|
||||
zh_wiki.Props().Main_page_(Bry_.new_a7("Zh_Main_Page"));
|
||||
tstr.Run_parse_from_url_bar("zh.w:Main_Page") .Chk_page_is_main_n().Chk_to_str("zh.wikipedia.org/wiki/Main_Page");
|
||||
tstr.Run_parse_from_url_bar("zh.w:") .Chk_page_is_main_y().Chk_to_str("zh.wikipedia.org/wiki/Zh_Main_Page");
|
||||
tstr.Run_parse_from_url_bar("en.w:") .Chk_page_is_main_y().Chk_to_str("en.wikipedia.org/wiki/Main_Page"); // old bug: still stuck at zh main page due to reused objects
|
||||
}
|
||||
@Test public void Mobile() { // PURPOSE: handle mobile links; DATE:2014-05-03
|
||||
tstr.Run_parse_from_url_bar("en.m.wikipedia.org/wiki/A" ).Chk_to_str("en.wikipedia.org/wiki/A"); // basic
|
||||
tstr.Run_parse_from_url_bar("en.M.wikipedia.org/wiki/A" ).Chk_to_str("en.wikipedia.org/wiki/A"); // upper
|
||||
tstr.Run_parse_from_url_bar("A" ).Chk_to_str("en.wikipedia.org/wiki/A"); // bounds-check: 0
|
||||
tstr.Run_parse_from_url_bar("A." ).Chk_to_str("en.wikipedia.org/wiki/A."); // bounds-check: 1
|
||||
tstr.Run_parse_from_url_bar("A.b" ).Chk_to_str("en.wikipedia.org/wiki/A.b"); // bounds-check: 2
|
||||
tstr.Run_parse_from_url_bar("A.b.m." ).Chk_to_str("en.wikipedia.org/wiki/A.b.m."); // false-match
|
||||
tstr.Run_parse_from_url_bar("en.x.wikipedia.org/wiki/A" ).Chk_to_str("en.wikipedia.org/wiki/en.x.wikipedia.org/wiki/A"); // fail
|
||||
}
|
||||
@Test public void Missing_page() {
|
||||
tstr.Run_parse_from_url_bar("http://a.org").Chk_is_null(); // unknown wiki; return null;
|
||||
tstr.Run_parse_from_url_bar("http://en.wikipedia.org").Chk_to_str("en.wikipedia.org/wiki/Main_Page"); // known wiki; return Main_Page
|
||||
}
|
||||
@Test public void Invalid_names() {
|
||||
tstr.Run_parse_from_url_bar("http://a/b/c").Chk_is_null(); // unknown url
|
||||
tstr.Run_parse_from_url_bar("war").Chk_to_str("en.wikipedia.org/wiki/war"); // word looks like lang, but is actually page; default to current
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
/*
|
||||
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.apps.urls; import gplx.*; import gplx.xowa.*; import gplx.xowa.apps.*;
|
||||
import org.junit.*;
|
||||
public class Xoa_url_parser__wiki_tst {
|
||||
private final Xoa_url_parser_fxt tstr = new Xoa_url_parser_fxt();
|
||||
@Test public void Basic() {
|
||||
tstr.Run_parse("en.wikipedia.org/wiki/A").Chk_tid(Xoa_url_.Tid_page).Chk_wiki("en.wikipedia.org").Chk_page("A");
|
||||
}
|
||||
@Test public void No_wiki() { // PURPOSE: no "/wiki/"
|
||||
tstr.Run_parse("en.wikipedia.org/A").Chk_wiki("en.wikipedia.org").Chk_page("A");
|
||||
}
|
||||
@Test public void Nested() {
|
||||
tstr.Run_parse("en.wikipedia.org/wiki/A/b").Chk_wiki("en.wikipedia.org").Chk_page("A/b");
|
||||
}
|
||||
@Test public void Slash() {
|
||||
tstr.Run_parse("en.wikipedia.org/wiki//A").Chk_wiki("en.wikipedia.org").Chk_page("/A");
|
||||
tstr.Run_parse("en.wikipedia.org/wiki/A//b").Chk_wiki("en.wikipedia.org").Chk_page("A//b");
|
||||
tstr.Run_parse("en.wikipedia.org/wiki///A").Chk_wiki("en.wikipedia.org").Chk_page("//A");
|
||||
}
|
||||
@Test public void Vnt() {
|
||||
Xowe_wiki wiki = tstr.Wiki();
|
||||
gplx.xowa.langs.vnts.Xol_vnt_regy_fxt.Init__vnt_mgr(wiki.Lang().Vnt_mgr(), 0, String_.Ary("zh-hans", "zh-hant"));
|
||||
tstr.Run_parse("en.wikipedia.org/zh-hans/A").Chk_wiki("en.wikipedia.org").Chk_page("A").Chk_vnt("zh-hans");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
/*
|
||||
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.apps.urls; import gplx.*; import gplx.xowa.*; import gplx.xowa.apps.*;
|
||||
import org.junit.*;
|
||||
public class Xoa_url_parser__xcmd_tst {
|
||||
private final Xoa_url_parser_fxt tstr = new Xoa_url_parser_fxt();
|
||||
@Test public void Basic() {
|
||||
tstr.Run_parse("xowa-cmd:xowa.app.version").Chk_tid(Xoa_url_.Tid_xcmd).Chk_page("xowa.app.version");
|
||||
}
|
||||
@Test public void Encoded() {
|
||||
tstr.Run_parse("xowa-cmd:a%22b*c").Chk_tid(Xoa_url_.Tid_xcmd).Chk_page("a\"b*c");
|
||||
}
|
||||
@Test public void Ignore_anchor_and_qargs() {
|
||||
tstr.Run_parse("xowa-cmd:a/b/c?d=e#f").Chk_tid(Xoa_url_.Tid_xcmd).Chk_page("a/b/c?d=e#f");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
/*
|
||||
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.apps.urls; import gplx.*; import gplx.xowa.*; import gplx.xowa.apps.*;
|
||||
import org.junit.*; import gplx.xowa.wikis.nss.*;
|
||||
public class Xoa_url_parser__xwiki_tst {
|
||||
private final Xoa_url_parser_fxt tstr = new Xoa_url_parser_fxt();
|
||||
@Test public void Commons() { // PURPOSE: "C" was being picked up as an xwiki to commons; PAGE:no.b:C/Variabler; DATE:2014-10-14
|
||||
tstr.Prep_add_xwiki_to_user("c", "commons.wikimedia.org"); // add alias of "c"
|
||||
tstr.Run_parse("C/D").Chk_tid(Xoa_url_.Tid_page).Chk_wiki("en.wikipedia.org").Chk_page("C/D"); // should use current wiki (enwiki), not xwiki to commons; also, page should be "C/D", not "D"
|
||||
}
|
||||
@Test public void Parse_lang() {
|
||||
tstr.Prep_add_xwiki_to_wiki("fr", "fr.wikipedia.org", "http://fr.wikipedia.org/~{0}");
|
||||
tstr.Run_parse("http://en.wikipedia.org/wiki/fr:A").Chk_tid(Xoa_url_.Tid_page).Chk_wiki("fr.wikipedia.org").Chk_page("A");
|
||||
}
|
||||
@Test public void Alias_wiki() {
|
||||
tstr.Prep_add_xwiki_to_wiki("s", "en.wikisource.org");
|
||||
tstr.Run_parse("s:A/b/c").Chk_wiki("en.wikisource.org").Chk_page("A/b/c");
|
||||
}
|
||||
@Test public void Xwiki_no_segs() { // PURPOSE: handle xwiki without full url; EX: "commons:Commons:Media_of_the_day"; DATE:2014-02-19
|
||||
tstr.Prep_add_xwiki_to_wiki("s", "en.wikisource.org");
|
||||
tstr.Run_parse("s:Project:A").Chk_wiki("en.wikisource.org").Chk_page("Project:A");
|
||||
}
|
||||
@Test public void Domain_only() {
|
||||
tstr.Prep_add_xwiki_to_user("fr.wikipedia.org");
|
||||
tstr.Run_parse("fr.wikipedia.org").Chk_wiki("fr.wikipedia.org").Chk_page("");
|
||||
}
|
||||
@Test public void Domain_and_wiki() {
|
||||
tstr.Prep_add_xwiki_to_user("fr.wikipedia.org");
|
||||
tstr.Run_parse("fr.wikipedia.org/wiki").Chk_wiki("fr.wikipedia.org").Chk_page("");
|
||||
}
|
||||
@Test public void Domain_and_wiki_w_http() {
|
||||
tstr.Prep_add_xwiki_to_user("fr.wikipedia.org");
|
||||
tstr.Run_parse("http://fr.wikipedia.org/wiki").Chk_wiki("fr.wikipedia.org").Chk_page("");
|
||||
}
|
||||
@Test public void Namespace_in_different_wiki() { // PURPOSE.fix: namespaced titles would default to default_wiki instead of current_wiki
|
||||
Xowe_wiki en_s = tstr.Prep_create_wiki("en.wikisource.org");
|
||||
tstr.Run_parse(en_s, "Category:A").Chk_wiki("en.wikisource.org").Chk_page("Category:A");
|
||||
}
|
||||
@Test public void Case_sensitive() {
|
||||
// tstr.Run_parse("en.wikipedia.org/wiki/a").Chk_wiki("en.wikipedia.org").Chk_page("A");
|
||||
Xowe_wiki en_d = tstr.Prep_create_wiki("en.wiktionary.org");
|
||||
Xow_ns_mgr ns_mgr = en_d.Ns_mgr();
|
||||
|
||||
ns_mgr.Ns_main().Case_match_(Xow_ns_case_.Id_all);
|
||||
tstr.Run_parse("en.wiktionary.org/wiki/a").Chk_wiki("en.wiktionary.org").Chk_page("a");
|
||||
|
||||
ns_mgr.Ns_category().Case_match_(Xow_ns_case_.Id_all);
|
||||
tstr.Run_parse("en.wiktionary.org/wiki/Category:a").Chk_wiki("en.wiktionary.org").Chk_page("Category:a");
|
||||
|
||||
tstr.Run_parse("en.wiktionary.org/wiki/A/B/C").Chk_page("A/B/C");
|
||||
}
|
||||
@Test public void Xwiki__to_enwiki() { // PURPOSE: handle alias of "wikipedia" and sv.wikipedia.org/wiki/Wikipedia:Main_Page; DATE:2015-07-31
|
||||
Xowe_wiki xwiki = tstr.Prep_create_wiki("sv.wikipedia.org");
|
||||
tstr.Prep_xwiki(xwiki, "wikipedia", "en.wikipedia.org", null);
|
||||
tstr.Prep_get_ns_mgr_from_meta("sv.wikipedia.org").Add_new(Xow_ns_.Id_project, "Wikipedia");
|
||||
tstr.Run_parse(xwiki, "sv.wikipedia.org/wiki/wikipedia:X").Chk_wiki("sv.wikipedia.org").Chk_page("wikipedia:X");
|
||||
tstr.Run_parse(xwiki, "sv.wikipedia.org/wiki/Wikipedia:X").Chk_wiki("sv.wikipedia.org").Chk_page("Wikipedia:X");
|
||||
}
|
||||
@Test public void Xwiki__to_ns() { // PURPOSE: handle alias of "wikipedia" in current, but no "Wikipedia" ns in other wiki; PAGE:pt.w:Wikipedia:P<>gina_de_testes DATE:2015-09-17
|
||||
tstr.Prep_create_wiki("pt.wikipedia.org");
|
||||
tstr.Prep_get_ns_mgr_from_meta("pt.wikipedia.org").Add_new(Xow_ns_.Id_project, "Project"); // clear ns_mgr and add only "Project" ns, not "Wikipedia" ns
|
||||
tstr.Prep_xwiki(tstr.Wiki(), "wikipedia", "en.wikipedia.org", null); // add alias of "wikipedia" in current wiki
|
||||
tstr.Run_parse(tstr.Wiki(), "pt.wikipedia.org/wiki/Wikipedia:X").Chk_wiki("pt.wikipedia.org").Chk_page("Wikipedia:X"); // should get "pt.wikipedia.org", not "en.wikipedia.org" (through alias)
|
||||
}
|
||||
}
|
||||
83
400_xowa/src/gplx/xowa/apps/urls/Xoa_url_parser_fxt.java
Normal file
83
400_xowa/src/gplx/xowa/apps/urls/Xoa_url_parser_fxt.java
Normal file
@@ -0,0 +1,83 @@
|
||||
/*
|
||||
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.apps.urls; import gplx.*; import gplx.xowa.*; import gplx.xowa.apps.*;
|
||||
import gplx.xowa.wikis.nss.*;
|
||||
public class Xoa_url_parser_fxt {
|
||||
protected final Xoae_app app; protected final Xowe_wiki cur_wiki;
|
||||
protected final Xoa_url_parser parser;
|
||||
protected Xoa_url actl_url;
|
||||
public Xoa_url_parser_fxt() {
|
||||
this.app = Xoa_app_fxt.app_();
|
||||
this.cur_wiki = Prep_create_wiki("en.wikipedia.org");
|
||||
this.parser = cur_wiki.Utl__url_parser();
|
||||
this.actl_url = Xoa_url.blank(); // default to blank for subclasses
|
||||
}
|
||||
public Xoae_app App() {return app;}
|
||||
public Xowe_wiki Wiki() {return cur_wiki;}
|
||||
public Xowe_wiki Prep_create_wiki(String domain) {
|
||||
Xowe_wiki rv = Xoa_app_fxt.wiki_(app, domain);
|
||||
Prep_add_xwiki_to_user(domain);
|
||||
return rv;
|
||||
}
|
||||
public Xoa_url_parser_fxt Prep_add_xwiki_to_wiki(String alias, String domain) {return Prep_xwiki(cur_wiki, alias, domain, null);}
|
||||
public Xoa_url_parser_fxt Prep_add_xwiki_to_wiki(String alias, String domain, String fmt) {return Prep_xwiki(cur_wiki, alias, domain, fmt);}
|
||||
public Xoa_url_parser_fxt Prep_add_xwiki_to_user(String domain) {return Prep_xwiki(app.Usere().Wiki(), domain, domain, null);}
|
||||
public Xoa_url_parser_fxt Prep_add_xwiki_to_user(String alias, String domain) {return Prep_xwiki(app.Usere().Wiki(), alias, domain, null);}
|
||||
public Xoa_url_parser_fxt Prep_add_xwiki_to_user(String alias, String domain, String fmt) {return Prep_xwiki(app.Usere().Wiki(), alias, domain, fmt);}
|
||||
public Xoa_url_parser_fxt Prep_xwiki(Xow_wiki wiki, String alias, String domain, String fmt) {
|
||||
wiki.Xwiki_mgr().Add_by_atrs(Bry_.new_u8(alias), Bry_.new_u8(domain), Bry_.new_u8_safe(fmt));
|
||||
return this;
|
||||
}
|
||||
public Xow_ns_mgr Prep_get_ns_mgr_from_meta(String wiki) {
|
||||
return app.Meta_mgr().Ns__get_or_load(Bry_.new_u8(wiki));
|
||||
}
|
||||
public Xoa_url_parser_fxt Run_parse(String actl_str) {return Run_parse(cur_wiki, actl_str);}
|
||||
public Xoa_url_parser_fxt Run_parse(Xow_wiki wiki, String actl_str) {
|
||||
this.actl_url = wiki.Utl__url_parser().Parse(Bry_.new_u8(actl_str));
|
||||
return this;
|
||||
}
|
||||
public Xoa_url_parser_fxt Run_parse_reuse(String actl_str) {
|
||||
this.actl_url = parser.Parse(Bry_.new_u8(actl_str));
|
||||
return this;
|
||||
}
|
||||
public Xoa_url_parser_fxt Run_parse_from_url_bar(String raw) {
|
||||
this.actl_url = parser.Parse_by_urlbar_or_null(raw);
|
||||
return this;
|
||||
}
|
||||
public Xoa_url_parser_fxt Chk_tid(int v) {Tfds.Eq_int(v, actl_url.Tid() , "tid"); return this;}
|
||||
public Xoa_url_parser_fxt Chk_is_null() {Tfds.Eq_bool(true, actl_url == null); return this;}
|
||||
public Xoa_url_parser_fxt Chk_vnt(String v) {Tfds.Eq_str(v, actl_url.Vnt_bry() , "vnt"); return this;}
|
||||
public Xoa_url_parser_fxt Chk_wiki(String v) {Tfds.Eq_str(v, actl_url.Wiki_bry() , "wiki"); return this;}
|
||||
public Xoa_url_parser_fxt Chk_wiki_is_missing(boolean v) {Tfds.Eq_bool(v, actl_url.Wiki_is_missing(), "wiki_is_missing"); return this;}
|
||||
public Xoa_url_parser_fxt Chk_page(String v) {Tfds.Eq_str(v, actl_url.Page_bry() , "page"); return this;}
|
||||
public Xoa_url_parser_fxt Chk_qargs(String v) {Tfds.Eq_str(v, actl_url.Qargs_mgr().To_bry(), "qargs"); return this;}
|
||||
public Xoa_url_parser_fxt Chk_page_is_main_y() {return Chk_page_is_main(Bool_.Y);}
|
||||
public Xoa_url_parser_fxt Chk_page_is_main_n() {return Chk_page_is_main(Bool_.N);}
|
||||
public Xoa_url_parser_fxt Chk_page_is_main(boolean v) {Tfds.Eq_bool(v, actl_url.Page_is_main() , "page_is_main"); return this;}
|
||||
public Xoa_url_parser_fxt Chk_anch(String v) {Tfds.Eq_str(v, actl_url.Anch_bry(), "anch"); return this;}
|
||||
public Xoa_url_parser_fxt Chk_action_is_edit_y() {return Chk_action_is_edit_(Bool_.Y);}
|
||||
public Xoa_url_parser_fxt Chk_action_is_edit_n() {return Chk_action_is_edit_(Bool_.N);}
|
||||
private Xoa_url_parser_fxt Chk_action_is_edit_(boolean v) {Tfds.Eq_bool(v, actl_url.Qargs_mgr().Match(Xoa_url_.Qarg__action, Xoa_url_.Qarg__action__edit), "action_is_edit"); return this;}
|
||||
public Xoa_url_parser_fxt Chk_to_str(String v) {return Chk_to_str(Bool_.Y, v);}
|
||||
public Xoa_url_parser_fxt Chk_to_str(boolean full, String v) {Tfds.Eq_str(v, actl_url.To_bry(full, Bool_.Y), "To_bry"); return this;}
|
||||
public Xoa_url_parser_fxt Chk_build_str_is_same() {
|
||||
Xoa_url_parser parser = new Xoa_url_parser(cur_wiki);
|
||||
Tfds.Eq_str(actl_url.Raw(), parser.Build_str(actl_url), "build_str");
|
||||
return this;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user