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

Wiki: Support renamed folders (fix)

This commit is contained in:
gnosygnu
2017-02-06 22:12:56 -05:00
parent 6f9e92afff
commit 938beac9f9
4379 changed files with 0 additions and 327818 deletions

View File

@@ -1,40 +0,0 @@
/*
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.htmls.hrefs; import gplx.*; import gplx.xowa.*; import gplx.xowa.htmls.*;
public class Xoh_href_ {
public static final String
Str__file = "file://"
, Str__site = "/site/"
, Str__wiki = "/wiki/"
, Str__anch = "#"
;
public static final byte[]
Bry__file = Bry_.new_a7(Str__file)
, Bry__site = Bry_.new_a7(Str__site)
, Bry__wiki = Bry_.new_a7(Str__wiki)
, Bry__anch = Bry_.new_a7(Str__anch)
, Bry__https = Bry_.new_a7("https://") // NOTE: must be "https:" or wmf api won't work; DATE:2015-06-17
, Bry__xcmd = Bry_.new_a7("/xcmd/")
;
public static final int
Len__file = Bry__file.length
, Len__site = Bry__site.length
, Len__wiki = Bry__wiki.length
, Len__anch = Bry__anch.length
;
}

View File

@@ -1,94 +0,0 @@
/*
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.htmls.hrefs; import gplx.*; import gplx.xowa.*; import gplx.xowa.htmls.*;
import gplx.core.btries.*; import gplx.core.primitives.*;
public class Xoh_href_gui_utl {
public static String Html_extract_text(String site, String page, String text_str) {
byte[] text_bry = Bry_.new_u8(text_str);
int text_len = text_bry.length;
int text_tid = Byte_ascii.To_a7_int(text_bry[0]);
switch (text_tid) {
case Text_tid_none: return ""; // "0"
case Text_tid_text: return String_.new_u8(text_bry, 2, text_len); // 2 to skip "1|"
case Text_tid_href: break; // fall through to below
default: throw Err_.new_unhandled(text_tid);
}
int href_bgn = 2; // 2 to skip "2|"
if (Bry_.Has_at_bgn(text_bry, Xoh_href_.Bry__file, href_bgn, text_len))
href_bgn += Xoh_href_.Len__file; // skip "file://"
Byte_obj_val href_tid = (Byte_obj_val)href_trie.Match_bgn(text_bry, href_bgn, text_len);
if (href_tid != null) {
switch (href_tid.Val()) {
case Href_tid_wiki: return site + String_.new_u8(text_bry, href_bgn, text_len);
case Href_tid_site: return String_.new_u8(text_bry, href_bgn + 6, text_len); // +6 to skip "site/"
case Href_tid_anch: return site + "/wiki/" + page + String_.new_u8(text_bry, href_bgn, text_len);
}
}
return String_.new_u8(text_bry, 2, text_len); // 2 to skip "2|"; handles "http://" text as well as any fall-thru from above
}
public static String Standardize_xowa_link(String str) {
byte[] bry = Bry_.new_u8(str);
int skip = Skip_start_of_xowa_link(bry, bry.length, 0);
return skip == 0 ? str : String_.Mid(str, skip);
}
private static int Skip_start_of_xowa_link(byte[] src, int src_len, int bgn) {
if (!Bry_.Has_at_bgn(src, Xoh_href_.Bry__file, bgn, src_len)) return bgn; // does not start with "file://"
int pos = bgn + Xoh_href_.Len__file; // skip "file://"
Object tid_obj = href_trie.Match_bgn(src, pos, src_len);
if (tid_obj == null) {
return bgn; // if not a known xowa link, return original bgn;
}
switch (((Byte_obj_val)tid_obj).Val()) {
case Href_tid_site: return pos;
case Href_tid_wiki: return pos;
case Href_tid_anch: return pos;
default: throw Err_.new_unhandled(tid_obj);
}
}
private static final byte Text_tid_none = 0, Text_tid_text = 1, Text_tid_href = 2;
private static final byte Href_tid_wiki = 1, Href_tid_site = 2, Href_tid_anch = 3;
private static final Btrie_slim_mgr href_trie = Btrie_slim_mgr.cs()
.Add_bry_byte(Xoh_href_.Bry__site , Href_tid_site)
.Add_bry_byte(Xoh_href_.Bry__wiki , Href_tid_wiki)
.Add_bry_byte(Xoh_href_.Bry__anch , Href_tid_anch)
;
}
/*
NOTE_1:
. swt/mozilla treats text differently in href="{text}" when content_editable=n; occurs in LocationListener.changing
http://a.org -> http://a.org does nothing
A -> file:///A adds "file:///"
/wiki/A -> file:///wiki/A adds "file://"
Category:A -> Category:A noops; Category is assumed to be protocol?
//en.wiktionary.org/wiki/a -> file:///wiki/a strips out site name and prepends "file://"; no idea why
. so, to handle the above, the code does the following
http://a.org -> http://a.org does nothing; nothing needed
A -> /wiki/A always prepend /wiki/
Category:A -> /wiki/Category:A always prepend /wiki/
//en.wiktionary.org/wiki/A -> /site/en.wiktionary.org/wiki/A always transform relative url to /site/
. the href will still come here as file:///wiki/A or file:///site/en.wiktionary.org/wiki/A.
. however, the file:// can be lopped off and discarded and the rest of the href will fall into one of the following cases
.. /wiki/
.. /site/
.. /xcmd/
.. #
.. anything else -> assume to be really a file:// url; EX: file://C/dir/fil.txt -> C/dir/fil.txt
. the other advantage of this approach is that this proc can be reused outside of swt calls; i.e.: it can parse both "file:///wiki/A" and "/wiki/A"
*/

View File

@@ -1,68 +0,0 @@
/*
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.htmls.hrefs; import gplx.*; import gplx.xowa.*; import gplx.xowa.htmls.*;
import org.junit.*;
import gplx.core.primitives.*; import gplx.xowa.htmls.hrefs.*; import gplx.xowa.guis.views.*;
public class Xoh_href_gui_utl_tst {
@Before public void init() {fxt.Clear();} private Xoh_href_gui_utl_fxt fxt = new Xoh_href_gui_utl_fxt();
@Test public void Extract_href__text() {
fxt.Test_extract_href("0|" , "");
fxt.Test_extract_href("1|selected_text" , "selected_text");
fxt.Test_extract_href("2|http://a.org" , "http://a.org");
}
@Test public void Extract_href__file() {
fxt.Test_extract_href("2|file:///site/en.wiktionary.org/wiki/Page_1" , "en.wiktionary.org/wiki/Page_1");
fxt.Test_extract_href("2|file:///wiki/Page_2" , "en.wikipedia.org/wiki/Page_2");
fxt.Test_extract_href("2|file://#anchor" , "en.wikipedia.org/wiki/Page_0#anchor");
}
@Test public void Extract_href__internal() {
fxt.Test_extract_href("2|/site/en.wiktionary.org/wiki/Page_1" , "en.wiktionary.org/wiki/Page_1");
fxt.Test_extract_href("2|/wiki/Page_2" , "en.wikipedia.org/wiki/Page_2");
fxt.Test_extract_href("2|#anchor" , "en.wikipedia.org/wiki/Page_0#anchor");
}
@Test public void Html_window_vpos_parse() {
fxt.Test_Html_window_vpos_parse("0|0,1,2", "0", "'0','1','2'");
fxt.Test_Html_window_vpos_parse("org.eclipse.swt.SWTException: Permission denied for <file://> to get property Selection.rangeCount", null, null); // check that invalid path doesn't fail; DATE:2014-04-05
}
@Test public void Standardize_xowa_link() {
fxt.Test_standardize_xowa_link("file:///site/en.wikipedia.org/wiki/A" , "/site/en.wikipedia.org/wiki/A");
fxt.Test_standardize_xowa_link("file:///wiki/A" , "/wiki/A");
fxt.Test_standardize_xowa_link("file://#A" , "#A");
}
}
class Xoh_href_gui_utl_fxt {
public void Clear() {
cur_wiki = "en.wikipedia.org";
cur_page = "Page_0";
}
public String Cur_wiki() {return cur_wiki;} public Xoh_href_gui_utl_fxt Cur_wiki_(String v) {cur_wiki = v; return this;} private String cur_wiki;
public String Cur_page() {return cur_page;} public Xoh_href_gui_utl_fxt Cur_page_(String v) {cur_page = v; return this;} private String cur_page;
public void Test_extract_href(String text_str, String expd) {
Tfds.Eq(expd, Xoh_href_gui_utl.Html_extract_text(cur_wiki, cur_page, text_str));
}
private String_obj_ref scroll_top = String_obj_ref.null_(), node_path = String_obj_ref.null_();
public void Test_Html_window_vpos_parse(String raw, String expd_scroll_top, String expd_node_path) {
scroll_top.Val_null_(); node_path.Val_null_();
Xog_html_itm.Html_window_vpos_parse(raw, scroll_top, node_path);
Tfds.Eq(expd_scroll_top, scroll_top.Val(), expd_scroll_top);
Tfds.Eq(expd_node_path, node_path.Val(), expd_node_path);
}
public void Test_standardize_xowa_link(String raw, String expd) {
Tfds.Eq_str(expd, Xoh_href_gui_utl.Standardize_xowa_link(raw), "standardize");
}
}

View File

@@ -1,84 +0,0 @@
/*
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.htmls.hrefs; import gplx.*; import gplx.xowa.*; import gplx.xowa.htmls.*;
import gplx.core.primitives.*; import gplx.core.btries.*; import gplx.core.net.*;
import gplx.xowa.langs.vnts.*;
public class Xoh_href_parser {
private final Btrie_rv trv = new Btrie_rv();
public void Parse_as_url(Xoa_url rv, byte[] raw, Xowe_wiki wiki, byte[] cur_page) {
int bgn = 0;
Object seg_obj = btrie.Match_at(trv, raw, bgn, raw.length); // match /wiki/ or /site/ or /xcmd/
if (seg_obj == null) {
Xol_vnt_mgr vnt_mgr = wiki.Lang().Vnt_mgr();
if (vnt_mgr.Enabled() && raw[0] == Byte_ascii.Slash) {
int slash_end = Bry_find_.Find_fwd(raw, Byte_ascii.Slash, 1);
if (vnt_mgr.Regy().Has(Bry_.Mid(raw, 1, slash_end))) {
raw = Bry_.Add(wiki.Domain_bry(), raw);
}
}
}
else { // something matched
switch (((Byte_obj_val)seg_obj).Val()) {
case Seg_xcmd_tid: // convert "/xcmd/a" to "xowa-cmd:a"
raw = Bry_.Add(Gfo_protocol_itm.Bry_xcmd, Bry_.Mid(raw, trv.Pos()));
break;
case Seg_wiki_tid: // add domain_bry; NOTE: needed for url-like pages; EX:"/wiki/http://A"; PAGE:esolangs.org/wiki/Language_list; DATE:2015-11-14
raw = Bry_.Add(wiki.Domain_bry(), raw);
break;
case Seg_site_tid: // skip "/site"
bgn = trv.Pos();
break;
default:
break;
}
}
wiki.Utl__url_parser().Parse(rv, raw, bgn, raw.length);
switch (rv.Tid()) {
case Xoa_url_.Tid_anch:
rv.Wiki_bry_(wiki.Domain_bry());
rv.Page_bry_(cur_page);
break;
case Xoa_url_.Tid_page:
Xow_wiki ttl_wiki = wiki.App().Wiki_mgri().Get_by_or_make_init_n(rv.Wiki_bry());
byte[] tmp_page = rv.Page_bry();
if (rv.Page_is_main())
tmp_page = ttl_wiki.Props().Main_page();
else {
if (tmp_page != null) {
if (ttl_wiki != null) {
Xoa_ttl ttl = ttl_wiki.Ttl_parse(tmp_page);
if (ttl == null) // invalid ttl; null out page;
tmp_page = Bry_.Empty;
else
tmp_page = ttl.Full_txt_w_ttl_case();
}
}
}
rv.Page_bry_(tmp_page);
if (tmp_page == null) { // handle xwiki lnke's to history page else null ref; EX:[http://ru.wikipedia.org/w/index.php?title&diff=19103464&oldid=18910980 извещен]; PAGE:ru.w:Project:Заявки_на_снятие_флагов/Архив/Патрулирующие/2009 DATE:2016-11-24
rv.Tid_(Xoa_url_.Tid_inet);
}
break;
}
}
private static final byte Seg_wiki_tid = 0, Seg_site_tid = 1, Seg_xcmd_tid = 2;
private static final Btrie_slim_mgr btrie = Btrie_slim_mgr.ci_a7() // NOTE:ci.ascii:XO_const.en; /wiki/, /site/ etc.
.Add_bry_tid(Xoh_href_.Bry__wiki, Seg_wiki_tid)
.Add_bry_tid(Xoh_href_.Bry__site, Seg_site_tid)
.Add_bry_tid(Xoh_href_.Bry__xcmd, Seg_xcmd_tid);
}

View File

@@ -1,96 +0,0 @@
/*
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.htmls.hrefs; import gplx.*; import gplx.xowa.*; import gplx.xowa.htmls.*;
import org.junit.*; import gplx.xowa.apps.urls.*; import gplx.xowa.wikis.nss.*;
public class Xoh_href_parser__basic__tst {
private final Xoh_href_parser_fxt fxt = new Xoh_href_parser_fxt();
@Test public void Site__basic() {
fxt.Exec__parse_as_url("/site/en.wikipedia.org/wiki/A").Test__tid(Xoa_url_.Tid_page).Test__to_str("en.wikipedia.org/wiki/A").Test__page("A");
}
@Test public void Site__ns_case() {
fxt.Exec__parse_as_url("/site/en.wikipedia.org/wiki/file:A").Test__page("File:A");
}
@Test public void Site__main_page() {
fxt.Exec__parse_as_url("/site/en.wikipedia.org/wiki/").Test__page("Main_Page").Test__page_is_main_y();
}
@Test public void Site__anch() {
fxt.Exec__parse_as_url("/site/en.wikipedia.org/wiki/A#b_c").Test__page("A").Test__anch("b_c");
}
@Test public void Site__qarg() {
fxt.Exec__parse_as_url("/site/en.wikipedia.org/wiki/A?action=edit").Test__page("A").Test__qargs("?action=edit");
}
@Test public void Site__invalid_ttl_shouldnt_fail() { // PURPOSE: invalid title shouldn't fail; EX: A{{B}} is invalid (b/c of braces);
fxt.Exec__parse_as_url("/site/en.wikipedia.org/wiki/A{{B}}").Test__page("");
}
@Test public void Site__xwiki_cases_correctly() { // PURPOSE: xwiki links should use case_match of xwiki (en.wiktionary.org) not cur_wiki (en.wikipedia.org); EX:w:Alphabet
Xowe_wiki en_wiktionary_org = fxt.Prep_create_wiki("en.wiktionary.org");
en_wiktionary_org.Ns_mgr().Ns_main().Case_match_(Xow_ns_case_.Tid__all);
fxt.Prep_add_xwiki_to_user("en.wiktionary.org", "en.wiktionary.org");
fxt.Exec__parse_as_url("/site/en.wiktionary.org/wiki/alphabet");
fxt.Test__to_str("en.wiktionary.org/wiki/alphabet").Test__page("alphabet");
}
@Test public void Site__xwiki_compound() { // PURPOSE: [[[w:wikt:]] not handled; DATE:2013-07-25
fxt.Prep_add_xwiki_to_wiki("wikt", "en.wiktionary.org");
fxt.Exec__parse_as_url("/site/en.wikipedia.org/wiki/wikt:")
.Test__tid(Xoa_url_.Tid_page)
.Test__page("Main_Page")
.Test__to_str("en.wiktionary.org/wiki/Main_Page")
;
}
// @Test public void Vnt() {
// Xowe_wiki wiki = fxt.Wiki();
// fxt.Prep_add_xwiki_to_user("zh.wikipedia.org");
// wiki.Lang().Vnt_mgr().Enabled_(true);
// wiki.Lang().Vnt_mgr().Vnt_grp().Add(new gplx.xowa.langs.vnts.Vnt_mnu_itm(Bry_.new_a7("zh-hans"), Bry_.new_a7("zh-hant")));
// fxt.Exec__parse_as_url("/site/zh.wikipedia.org/zh-hant/A").Test__page("A").Chk_vnt("zh-hant");
// }
@Test public void Http__basic() {
fxt.Exec__parse_as_url("http://a.org/b").Test__tid(Xoa_url_.Tid_inet);
}
@Test public void Prot__ftp() { // PURPOSE: check that urls with form of "ftp://" return back Tid_ftp; DATE:2014-04-25
fxt.Exec__parse_as_url("ftp://a.org").Test__tid(Xoa_url_.Tid_inet);
}
@Test public void File__basic() {
fxt.Exec__parse_as_url("file:///C/xowa/file/a.png").Test__tid(Xoa_url_.Tid_file);
}
@Test public void Anchor__basic() {
fxt.Exec__parse_as_url("#a").Test__tid(Xoa_url_.Tid_anch).Test__to_str("en.wikipedia.org/wiki/Page 1#a").Test__anch("a");
}
@Test public void Xcmd__basic() {
fxt.Exec__parse_as_url("/xcmd/page_edit").Test__tid(Xoa_url_.Tid_xcmd).Test__page("page_edit");
}
@Test public void Xowa__basic() {
fxt.Exec__parse_as_url("xowa-cmd:a%22b*c").Test__tid(Xoa_url_.Tid_xcmd).Test__page("a\"b*c");
}
// COMMENTED: this seems wrong; [//wikisource.org] should go to https://wikisource.org not https://en.wikisource.org; both sites are different; DATE:2015-08-02
// @Test public void Site__user_wiki() {// PURPOSE: outlier for wikisource.org which is alias to en.wikisource.org; alias added in user_wiki; EX: [//wikisource.org a]; in browser, automatically goes to http://wikisource.org; in xowa, should go to /site/en.wikisource.org
// fxt.Prep_xwiki(fxt.App().User().Wikii(), "en_wiki_alias", "en.wikipedia.org", null);
// fxt.Exec__parse_as_url("/site/en_wiki_alias/wiki/")
// .Test__tid(Xoa_url_.Tid_page)
// .Test__page("Main_Page")
// .Test__to_str("en.wikipedia.org/wiki/Main_Page")
// ;
// }
}
class Xoh_href_parser_fxt extends Xow_url_parser_fxt { private final Xoh_href_parser href_parser = new Xoh_href_parser();
public Xoh_href_parser_fxt Exec__parse_as_url(String raw) {
href_parser.Parse_as_url(actl_url, Bry_.new_u8(raw), cur_wiki, Bry__page_1);
return this;
}
private static final byte[] Bry__page_1 = Bry_.new_a7("Page 1");
}

View File

@@ -1,38 +0,0 @@
/*
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.htmls.hrefs; import gplx.*; import gplx.xowa.*; import gplx.xowa.htmls.*;
import org.junit.*; import gplx.xowa.apps.urls.*; import gplx.xowa.wikis.nss.*;
public class Xoh_href_parser__qargs__tst {
private final Xoh_href_parser_fxt fxt = new Xoh_href_parser_fxt();
@Test public void Basic() {
fxt.Exec__parse_as_url("/wiki/A?k1=v1&k2=v2");
fxt.Test__page("A");
fxt.Test__to_str("en.wikipedia.org/wiki/A?k1=v1&k2=v2");
}
@Test public void Anch() { // PURPOSE.fix: anchor was being placed before qargs; DATE:2016-10-08
fxt.Exec__parse_as_url("/wiki/Category:A?pagefrom=A#mw-pages");
fxt.Test__page("Category:A");
fxt.Test__to_str("en.wikipedia.org/wiki/Category:A?pagefrom=A#mw-pages"); // was Category:A#mw-page?pagefrom=A
}
// FUTURE: qargs should be unencoded by default; decoded on request
@Test public void Encoded() { // PURPOSE.fix: do not use decoded String; DATE:2016-10-08
fxt.Exec__parse_as_url("/wiki/Category:A?pagefrom=A%26B#mw-pages");
fxt.Test__page("Category:A");
fxt.Test__qargs("?pagefrom=A&B");
}
}

View File

@@ -1,50 +0,0 @@
/*
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.htmls.hrefs; import gplx.*; import gplx.xowa.*; import gplx.xowa.htmls.*;
import org.junit.*; import gplx.xowa.apps.urls.*; import gplx.xowa.wikis.nss.*;
public class Xoh_href_parser__wiki__tst {
private final Xoh_href_parser_fxt fxt = new Xoh_href_parser_fxt();
@Test public void Basic() {
fxt.Exec__parse_as_url("/wiki/A").Test__tid(Xoa_url_.Tid_page).Test__to_str("en.wikipedia.org/wiki/A").Test__wiki("en.wikipedia.org").Test__page("A");
}
@Test public void Page__w_question() {
fxt.Exec__parse_as_url("/wiki/%3F").Test__page("?");
}
@Test public void Qarg() {
fxt.Exec__parse_as_url("/wiki/A?action=edit").Test__page("A").Test__qargs("?action=edit").Test__to_str("en.wikipedia.org/wiki/A?action=edit");
}
@Test public void Qarg__w_question() {
fxt.Exec__parse_as_url("/wiki/A%3F?action=edit").Test__page("A?").Test__qargs("?action=edit");
}
@Test public void Anchor() {
fxt.Exec__parse_as_url("/wiki/A#b").Test__to_str("en.wikipedia.org/wiki/A#b").Test__anch("b");
}
@Test public void Xwiki__only() {
fxt.Prep_add_xwiki_to_wiki("c", "commons.wikimedia.org");
fxt.Exec__parse_as_url("/wiki/c:").Test__page_is_main_y().Test__page("Main_Page").Test__to_str("commons.wikimedia.org/wiki/Main_Page");
}
@Test public void Encoded() {
fxt.Exec__parse_as_url("/wiki/A%22b%22c").Test__page("A\"b\"c");
}
@Test public void Triple_slash() { // PURPOSE: handle triple slashes; PAGE:esolangs.org/wiki/Language_list; DATE:2015-11-14
fxt.Exec__parse_as_url("/wiki////").Test__to_str("en.wikipedia.org/wiki////").Test__wiki("en.wikipedia.org").Test__page("///");
}
@Test public void Http() { // PURPOSE: variant of triple slashes; DATE:2015-11-14
fxt.Exec__parse_as_url("/wiki/http://a").Test__to_str("en.wikipedia.org/wiki/Http://a").Test__wiki("en.wikipedia.org").Test__page("Http://a");
}
}

View File

@@ -1,107 +0,0 @@
/*
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.htmls.hrefs; import gplx.*; import gplx.xowa.*; import gplx.xowa.htmls.*;
import gplx.core.brys.fmtrs.*;
import gplx.langs.htmls.encoders.*; import gplx.xowa.htmls.core.htmls.*;
import gplx.xowa.wikis.xwikis.*;
public class Xoh_href_wtr { // TS:do not move to app-level
private final Gfo_url_encoder encoder = Gfo_url_encoder_.New__html_href_mw(Bool_.Y).Make();
private final Bry_bfr encoder_bfr = Bry_bfr_.Reset(255), tmp_bfr = Bry_bfr_.Reset(255);
public byte[] Build_to_bry(Xow_wiki wiki, Xoa_ttl ttl) {
Build_to_bfr(tmp_bfr, wiki.App(), Xoh_wtr_ctx.Basic, wiki.Domain_bry(), ttl);
return tmp_bfr.To_bry_and_clear();
}
public void Build_to_bfr(Bry_bfr bfr, Xoa_app app, byte[] domain_bry, Xoa_ttl ttl) {Build_to_bfr(bfr, app, Xoh_wtr_ctx.Basic, domain_bry, ttl);}
public void Build_to_bfr(Bry_bfr bfr, Xoa_app app, Xoh_wtr_ctx hctx, byte[] domain_bry, Xoa_ttl ttl) { // given ttl, write href; EX: A -> '/wiki/A'
int hctx_mode = hctx.Mode();
byte[] page = ttl.Full_txt_raw();
Xow_xwiki_itm xwiki = ttl.Wik_itm();
if (xwiki == null) // not an xwiki; EX: [[wikt:Word]]
Build_to_bfr_page(ttl, hctx, page, 0); // write page only; NOTE: changed to remove leaf logic DATE:2014-09-07
else { // xwiki; skip wiki and encode page only;
byte[] wik_txt = ttl.Wik_txt();
Build_to_bfr_page(ttl, hctx, page, wik_txt.length + 1);
}
if (xwiki == null) { // not an xwiki
if (ttl.Anch_bgn() != 1) { // not an anchor-only; EX: "#A"
switch (hctx_mode) {
case Xoh_wtr_ctx.Mode_popup: { // popup parser always writes as "/site/"
bfr.Add(Xoh_href_.Bry__site); // add "/site/"; EX: /site/
bfr.Add(domain_bry); // add xwiki; EX: en_dict
bfr.Add(Xoh_href_.Bry__wiki); // add "/wiki/"; EX: /wiki/
break;
}
case Xoh_wtr_ctx.Mode_file_dump: {
bfr.Add(hctx.Anch__href__bgn());
break;
}
default: {
bfr.Add(Xoh_href_.Bry__wiki); // add "/wiki/"; EX: /wiki/Page
break;
}
}
}
else {} // anchor: noop
}
else { // xwiki
if ( app.Xwiki_mgr__missing(xwiki.Domain_bry()) // xwiki is not offline; use http:
|| hctx_mode == Xoh_wtr_ctx.Mode_hdump // hdump should never dump as "/site/"
|| hctx_mode == Xoh_wtr_ctx.Mode_file_dump // filedump should never dump as "/site/"
) {
Bry_fmtr url_fmtr = xwiki.Url_fmtr();
if (url_fmtr == null) {
bfr.Add(Xoh_href_.Bry__https); // add "https://"; EX: https://
bfr.Add(xwiki.Domain_bry()); // add xwiki; EX: en_dict
bfr.Add(Xoh_href_.Bry__wiki); // add "/wiki/"; EX: /wiki/
}
else { // url_fmtr exists; DATE:2015-04-22
url_fmtr.Bld_bfr(bfr, encoder_bfr.To_bry_and_clear()); // use it and pass encoder_bfr for page_name;
return;
}
}
else { // xwiki is avaiable; use /site/
bfr.Add(Xoh_href_.Bry__site); // add "/site/"; EX: /site/
bfr.Add(xwiki.Domain_bry()); // add xwiki; EX: en_dict
bfr.Add(Xoh_href_.Bry__wiki); // add "/wiki/"; EX: /wiki/
}
}
bfr.Add_bfr_and_clear(encoder_bfr);
}
private void Build_to_bfr_page(Xoa_ttl ttl, Xoh_wtr_ctx hctx, byte[] ttl_full, int page_bgn) {
int anch_bgn = Bry_find_.Find_fwd(ttl_full, Byte_ascii.Hash); // NOTE: cannot use Anch_bgn b/c Anch_bgn has bug with whitespace
if (anch_bgn == Bry_find_.Not_found){ // no anchor; just add page
encoder.Encode(encoder_bfr, ttl_full, page_bgn, ttl_full.length);
if (hctx.Mode() == Xoh_wtr_ctx.Mode_file_dump) {
byte[] href_end = hctx.Anch__href__end();
if (href_end != null) encoder_bfr.Add(href_end);
}
}
else { // anchor exists; check if anchor is preceded by ws; EX: [[A #b]] -> "/wiki/A#b"
int page_end = Bry_find_.Find_bwd_last_ws(ttl_full, anch_bgn); // first 1st ws before #; handles multiple ws
page_end = page_end == Bry_find_.Not_found ? anch_bgn : page_end; // if ws not found, use # pos; else use 1st ws pos
encoder.Encode(encoder_bfr, ttl_full, page_bgn, page_end); // add page
if (hctx.Mode() == Xoh_wtr_ctx.Mode_file_dump) {
byte[] href_end = hctx.Anch__href__end();
if ( href_end != null
&& page_end - page_bgn > 0) // handle [[#A]] which will have no page; else will dump "home/page/.html#A"; DATE:2016-04-12
encoder_bfr.Add(href_end);
}
encoder.Encode(encoder_bfr, ttl_full, anch_bgn, ttl_full.length); // add anchor
}
}
}

View File

@@ -1,59 +0,0 @@
/*
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.htmls.hrefs; import gplx.*; import gplx.xowa.*; import gplx.xowa.htmls.*;
import org.junit.*;
import gplx.core.net.*; import gplx.xowa.wikis.nss.*;
public class Xoh_href_wtr_tst {
private final Xoh_href_wtr_fxt fxt = new Xoh_href_wtr_fxt();
@Test public void Xwiki_enc() {fxt.Test_build("wikt:abc?d" , "/site/en.wiktionary.org/wiki/abc%3Fd");}
@Test public void Page_quote() {fxt.Test_build("a\"b\"c" , "/wiki/A%22b%22c");}
@Test public void Page() {fxt.Test_build("abc" , "/wiki/Abc");}
@Test public void Page_ns() {fxt.Test_build("Image:A.png" , "/wiki/Image:A.png");}
@Test public void Anchor() {fxt.Test_build("#abc" , "#abc");}
@Test public void Page_anchor() {fxt.Test_build("Abc#def" , "/wiki/Abc#def");}
@Test public void Xwiki() {fxt.Test_build("wikt:abc" , "/site/en.wiktionary.org/wiki/abc");} // NOTE: "abc" not capitalized, b/c other wiki's case sensitivity is not known; this emulates WP's behavior
@Test public void Xwiki_2() {fxt.Test_build("wikt:Special:Search/a" , "/site/en.wiktionary.org/wiki/Special:Search/a");}
@Test public void Category() {fxt.Test_build("Category:abc" , "/wiki/Category:Abc");}
@Test public void Xwiki_wikimedia_mail() { // PURPOSE: DATE:2015-04-22
fxt.Prep_xwiki_by_many("0|mail|https://lists.wikimedia.org/mailman/listinfo/~{0}|Wikitech Mailing List");
fxt.Test_build("mail:A" , "https://lists.wikimedia.org/mailman/listinfo/A");
}
}
class Xoh_href_wtr_fxt {
private final Xowe_wiki wiki;
private final Bry_bfr tmp_bfr = Bry_bfr_.Reset(255);
private final Xoh_href_wtr href_wtr = new Xoh_href_wtr();
public Xoh_href_wtr_fxt() {
this.app = Xoa_app_fxt.Make__app__edit();
this.wiki = Xoa_app_fxt.Make__wiki__edit(app);
wiki.Xwiki_mgr().Add_by_csv(Bry_.new_a7("1|wikt|en.wiktionary.org"));
app.Usere().Wiki().Xwiki_mgr().Add_by_csv(Bry_.new_a7("1|en.wiktionary.org|en.wiktionary.org"));
}
public Xoae_app App() {return app;} private final Xoae_app app;
public Xoh_href_wtr_fxt Prep_wiki_cs(String domain) {
Xow_wiki wiki = app.Wiki_mgr().Get_by_or_make_init_n(Bry_.new_u8(domain));
wiki.Ns_mgr().Ns_main().Case_match_(Xow_ns_case_.Tid__all);
return this;
}
public Xoh_href_wtr_fxt Prep_xwiki_by_many(String raw) {wiki.Xwiki_mgr().Add_by_csv(Bry_.new_u8(raw)); return this;} // need to add to wiki's xwiki_mgr for ttl_parse
public void Test_build(String raw, String expd) {
Xoa_ttl ttl = Xoa_ttl.Parse(wiki, Bry_.new_u8(raw));
href_wtr.Build_to_bfr(tmp_bfr, app, wiki.Domain_bry(), ttl);
Tfds.Eq(expd, tmp_bfr.To_str_and_clear());
}
}