mirror of https://github.com/gnosygnu/xowa
parent
e78382a8ac
commit
a8f6fd0fb0
@ -0,0 +1,43 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012-2017 gnosygnu@gmail.com
|
||||
|
||||
XOWA is licensed under the terms of the General Public License (GPL) Version 3,
|
||||
or alternatively under the terms of the Apache License Version 2.0.
|
||||
|
||||
You may use XOWA according to either of these licenses as is most appropriate
|
||||
for your project on a case-by-case basis.
|
||||
|
||||
The terms of each license can be found in the source code repository:
|
||||
|
||||
GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
|
||||
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
|
||||
*/
|
||||
package gplx.langs.jsons; import gplx.*; import gplx.langs.*;
|
||||
import org.junit.*; import gplx.core.tests.*;
|
||||
public class Json_doc_wtr_tst {
|
||||
private final Json_doc_wtr_fxt fxt = new Json_doc_wtr_fxt();
|
||||
@Test public void Basic() {
|
||||
fxt.Test__Bld_as_str
|
||||
( fxt.Exec__Kv_simple("k1", "v\"1")
|
||||
, fxt.Exec__Concat_apos
|
||||
( "{"
|
||||
, " 'k1':'v\\\"1'"
|
||||
, "}"));
|
||||
}
|
||||
}
|
||||
class Json_doc_wtr_fxt {
|
||||
public Json_doc_wtr Exec__Kv_simple(String key, String val) {
|
||||
Json_doc_wtr doc_wtr = new Json_doc_wtr();
|
||||
doc_wtr.Nde_bgn();
|
||||
doc_wtr.Kv(Bool_.N, Bry_.new_u8(key), Bry_.new_u8(val));
|
||||
doc_wtr.Nde_end();
|
||||
return doc_wtr;
|
||||
}
|
||||
public void Test__Bld_as_str(Json_doc_wtr doc_wtr, String expd) {
|
||||
Gftest.Eq__ary__lines(expd, doc_wtr.Bld_as_str());
|
||||
}
|
||||
public String Exec__Concat_apos(String... ary) {
|
||||
return Json_doc.Make_str_by_apos(ary);
|
||||
}
|
||||
}
|
@ -0,0 +1,57 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012-2017 gnosygnu@gmail.com
|
||||
|
||||
XOWA is licensed under the terms of the General Public License (GPL) Version 3,
|
||||
or alternatively under the terms of the Apache License Version 2.0.
|
||||
|
||||
You may use XOWA according to either of these licenses as is most appropriate
|
||||
for your project on a case-by-case basis.
|
||||
|
||||
The terms of each license can be found in the source code repository:
|
||||
|
||||
GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
|
||||
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
|
||||
*/
|
||||
package gplx.langs.phps; import gplx.*; import gplx.langs.*;
|
||||
public abstract class Php_tkn_base implements Php_tkn {
|
||||
public abstract byte Tkn_tid();
|
||||
public int Src_bgn() {return src_bgn;} private int src_bgn;
|
||||
public int Src_end() {return src_end;} public void Src_end_(int v) {this.src_end = v;} private int src_end;
|
||||
public void Src_rng_(int src_bgn, int src_end) {this.src_bgn = src_bgn; this.src_end = src_end;}
|
||||
}
|
||||
class Php_tkn_generic extends Php_tkn_base {
|
||||
public Php_tkn_generic(int src_bgn, int src_end, byte tid) {this.Src_rng_(src_bgn, src_end); this.tid = tid;}
|
||||
@Override public byte Tkn_tid() {return tid;} private byte tid;
|
||||
}
|
||||
class Php_tkn_txt extends Php_tkn_base {
|
||||
public Php_tkn_txt(int src_bgn, int src_end) {this.Src_rng_(src_bgn, src_end);}
|
||||
@Override public byte Tkn_tid() {return Php_tkn_.Tid_txt;}
|
||||
}
|
||||
class Php_tkn_ws extends Php_tkn_base {
|
||||
public Php_tkn_ws(int src_bgn, int src_end, byte ws_tid) {this.Src_rng_(src_bgn, src_end); this.ws_tid = ws_tid;}
|
||||
@Override public byte Tkn_tid() {return Php_tkn_.Tid_ws;}
|
||||
public byte Ws_tid() {return ws_tid;} private byte ws_tid;
|
||||
public static final byte Tid_space = 0, Tid_nl = 1, Tid_tab = 2, Tid_cr = 3;
|
||||
}
|
||||
class Php_tkn_var extends Php_tkn_base {
|
||||
public Php_tkn_var(int src_bgn, int src_end) {this.Src_rng_(src_bgn, src_end);}
|
||||
@Override public byte Tkn_tid() {return Php_tkn_.Tid_var;}
|
||||
public byte[] Var_name(byte[] src) {return Bry_.Mid(src, this.Src_bgn() + 1, this.Src_end());} // NOTE: assume vars are of form $abc; +1 to skip first $
|
||||
}
|
||||
class Php_tkn_num extends Php_tkn_base {
|
||||
public Php_tkn_num(int src_bgn, int src_end) {this.Src_rng_(src_bgn, src_end);}
|
||||
@Override public byte Tkn_tid() {return Php_tkn_.Tid_num;}
|
||||
public int Num_val_int(byte[] src) {return Bry_.To_int_or(src, this.Src_bgn(), this.Src_end(), Int_.Min_value);}
|
||||
}
|
||||
class Php_tkn_quote extends Php_tkn_base {
|
||||
public Php_tkn_quote(int src_bgn, int src_end, byte quote_tid) {this.Src_rng_(src_bgn, src_end); this.quote_tid = quote_tid;}
|
||||
@Override public byte Tkn_tid() {return Php_tkn_.Tid_quote;}
|
||||
public byte Quote_tid() {return quote_tid;} private byte quote_tid;
|
||||
public byte[] Quote_text(byte[] src) {return Bry_.Mid(src, this.Src_bgn() + 1, this.Src_end() - 1);} // NOTE: assume quote are of form 'abc'; +1, -1 to skip flanking chars
|
||||
public static final byte Tid_null = 0, Tid_mult = 1, Tid_slash = 2, Tid_hash = 3;
|
||||
}
|
||||
class Php_tkn_declaration extends Php_tkn_base {
|
||||
@Override public byte Tkn_tid() {return Php_tkn_.Tid_declaration;}
|
||||
public static final Php_tkn_declaration Instance = new Php_tkn_declaration();
|
||||
}
|
@ -0,0 +1,46 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012-2017 gnosygnu@gmail.com
|
||||
|
||||
XOWA is licensed under the terms of the General Public License (GPL) Version 3,
|
||||
or alternatively under the terms of the Apache License Version 2.0.
|
||||
|
||||
You may use XOWA according to either of these licenses as is most appropriate
|
||||
for your project on a case-by-case basis.
|
||||
|
||||
The terms of each license can be found in the source code repository:
|
||||
|
||||
GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
|
||||
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
|
||||
*/
|
||||
package gplx.langs.phps; import gplx.*; import gplx.langs.*;
|
||||
public class Php_tkn_comment extends Php_tkn_base {
|
||||
public Php_tkn_comment(int src_bgn, int src_end, byte comment_tid) {this.Src_rng_(src_bgn, src_end); this.comment_tid = comment_tid;}
|
||||
@Override public byte Tkn_tid() {return Php_tkn_.Tid_comment;}
|
||||
public byte Comment_tid() {return comment_tid;} private byte comment_tid;
|
||||
|
||||
public void To_bfr(Bry_bfr bfr, byte[] src, boolean trim) {
|
||||
int bgn = this.Src_bgn();
|
||||
int end = this.Src_end();
|
||||
switch (comment_tid) {
|
||||
case Tid_mult: // EX: /* comment */
|
||||
bgn += 2;
|
||||
end -= 2;
|
||||
break;
|
||||
case Tid_slash: // EX: // comment\n
|
||||
bgn += 2;
|
||||
end -= 1;
|
||||
break;
|
||||
case Tid_hash: // EX: # comment\n
|
||||
bgn += 1;
|
||||
end -= 1;
|
||||
break;
|
||||
}
|
||||
if (trim) {
|
||||
bgn = Bry_find_.Find_fwd_while_not_ws(src, bgn, end);
|
||||
end = Bry_find_.Find_bwd__skip_ws(src, end, bgn);
|
||||
}
|
||||
bfr.Add_mid(src, bgn, end);
|
||||
}
|
||||
public static final byte Tid_null = 0, Tid_mult = 1, Tid_slash = 2, Tid_hash = 3;
|
||||
}
|
@ -0,0 +1,60 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012-2017 gnosygnu@gmail.com
|
||||
|
||||
XOWA is licensed under the terms of the General Public License (GPL) Version 3,
|
||||
or alternatively under the terms of the Apache License Version 2.0.
|
||||
|
||||
You may use XOWA according to either of these licenses as is most appropriate
|
||||
for your project on a case-by-case basis.
|
||||
|
||||
The terms of each license can be found in the source code repository:
|
||||
|
||||
GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
|
||||
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
|
||||
*/
|
||||
package gplx.xowa.langs.names; import gplx.*; import gplx.xowa.*; import gplx.xowa.langs.*;
|
||||
import gplx.core.btries.*;
|
||||
import gplx.core.intls.*;
|
||||
class Strcpn {
|
||||
private final Btrie_slim_mgr trie = Btrie_slim_mgr.cs();
|
||||
public Strcpn(byte[][] ary) {
|
||||
for (byte[] val : ary) {
|
||||
trie.Add_bry(val);
|
||||
}
|
||||
}
|
||||
public int Exec(byte[] val) {
|
||||
int val_len = val.length;
|
||||
Btrie_rv trv = new Btrie_rv();
|
||||
int pos_in_chars = 0;
|
||||
int pos_in_bytes = 0;
|
||||
while (pos_in_bytes < val_len) {
|
||||
byte b = val[pos_in_bytes];
|
||||
trie.Match_at_w_b0(trv, b, val, pos_in_bytes, val_len);
|
||||
|
||||
// no match; try next pos_in_bytes
|
||||
if (trv.Pos() == pos_in_bytes) {
|
||||
pos_in_bytes += Utf8_.Len_of_char_by_1st_byte(b);
|
||||
pos_in_chars++;
|
||||
}
|
||||
// match; return pos_in_bytes
|
||||
else {
|
||||
return pos_in_chars;
|
||||
}
|
||||
}
|
||||
return pos_in_chars;
|
||||
}
|
||||
|
||||
public static byte[][] Split_concatenated_ascii(String str) {
|
||||
byte[] bry = Bry_.new_u8(str);
|
||||
int len = bry.length;
|
||||
byte[][] rv = new byte[len][];
|
||||
for (int i = 0; i < len; i++) {
|
||||
rv[i] = new byte[] {bry[i]};
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
public static Strcpn New_by_concatenated_ascii(String s) {
|
||||
return new Strcpn(Split_concatenated_ascii(s));
|
||||
}
|
||||
}
|
@ -0,0 +1,55 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012-2017 gnosygnu@gmail.com
|
||||
|
||||
XOWA is licensed under the terms of the General Public License (GPL) Version 3,
|
||||
or alternatively under the terms of the Apache License Version 2.0.
|
||||
|
||||
You may use XOWA according to either of these licenses as is most appropriate
|
||||
for your project on a case-by-case basis.
|
||||
|
||||
The terms of each license can be found in the source code repository:
|
||||
|
||||
GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
|
||||
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
|
||||
*/
|
||||
package gplx.xowa.langs.names; import gplx.*; import gplx.xowa.*; import gplx.xowa.langs.*;
|
||||
import org.junit.*;
|
||||
import gplx.core.tests.*;
|
||||
public class Strcpn_tst {
|
||||
private final Strcpn_fxt fxt = new Strcpn_fxt();
|
||||
|
||||
@Test public void Single() {
|
||||
fxt.Test__exec(0, "a" , "a__");
|
||||
fxt.Test__exec(1, "a" , "_a_");
|
||||
fxt.Test__exec(2, "a" , "__a");
|
||||
fxt.Test__exec(3, "a" , "___");
|
||||
}
|
||||
@Test public void Multiple() {
|
||||
fxt.Test__exec(2, "ab" , "__a");
|
||||
fxt.Test__exec(2, "ab" , "__b");
|
||||
fxt.Test__exec(1, "ab" , "_ba");
|
||||
fxt.Test__exec(3, "ab" , "___");
|
||||
}
|
||||
@Test public void Utf8() {
|
||||
fxt.Test__exec(3, "d" , "ab¢d"); // len=2
|
||||
fxt.Test__exec(3, "d" , "ab€d"); // len=3
|
||||
fxt.Test__exec(4, "z" , "ab€d"); // len=3
|
||||
}
|
||||
@Test public void Php_doc() { // REF: http://php.net/manual/en/function.strcspn.php
|
||||
fxt.Test__exec_rev(0, "abcd" , "apple");
|
||||
fxt.Test__exec_rev(0, "abcd" , "banana");
|
||||
fxt.Test__exec_rev(2, "hello" , "l");
|
||||
fxt.Test__exec_rev(2, "hello" , "world");
|
||||
fxt.Test__exec_rev(5, "hello" , "na");
|
||||
// fxt.Test__exec(0, "abcdhelloabcd" , "l" , -9);
|
||||
// fxt.Test__exec(0, "abcdhelloabcd" , "l" , -9, -5);
|
||||
}
|
||||
}
|
||||
class Strcpn_fxt {
|
||||
public void Test__exec_rev(int expd, String subject, String mask) {Test__exec(expd, mask, subject);}
|
||||
public void Test__exec(int expd, String mask, String subject) {
|
||||
Strcpn mgr = Strcpn.New_by_concatenated_ascii(mask);
|
||||
Gftest.Eq__int(expd, mgr.Exec(Bry_.new_u8(subject)));
|
||||
}
|
||||
}
|
@ -0,0 +1,52 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012-2017 gnosygnu@gmail.com
|
||||
|
||||
XOWA is licensed under the terms of the General Public License (GPL) Version 3,
|
||||
or alternatively under the terms of the Apache License Version 2.0.
|
||||
|
||||
You may use XOWA according to either of these licenses as is most appropriate
|
||||
for your project on a case-by-case basis.
|
||||
|
||||
The terms of each license can be found in the source code repository:
|
||||
|
||||
GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
|
||||
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
|
||||
*/
|
||||
package gplx.xowa.langs.names; import gplx.*; import gplx.xowa.*; import gplx.xowa.langs.*;
|
||||
/**
|
||||
* Allowed title characters -- regex character class
|
||||
* Don't change this unless you know what you're doing
|
||||
*
|
||||
* Problematic punctuation:
|
||||
* - []{}|# Are needed for link syntax, never enable these
|
||||
* - <> Causes problems with HTML escaping, don't use
|
||||
* - % Enabled by default, minor problems with path to query rewrite rules, see below
|
||||
* - + Enabled by default, but doesn't work with path to query rewrite rules,
|
||||
* corrupted by apache
|
||||
* - ? Enabled by default, but doesn't work with path to PATH_INFO rewrites
|
||||
*
|
||||
* All three of these punctuation problems can be avoided by using an alias,
|
||||
* instead of a rewrite rule of either variety.
|
||||
*
|
||||
* The problem with % is that when using a path to query rewrite rule, URLs are
|
||||
* double-unescaped: once by Apache's path conversion code, and again by PHP. So
|
||||
* %253F, for example, becomes "?". Our code does not double-escape to compensate
|
||||
* for this, indeed double escaping would break if the double-escaped title was
|
||||
* passed in the query String rather than the path. This is a minor security issue
|
||||
* because articles can be created such that they are hard to view or edit.
|
||||
*
|
||||
* In some rare cases you may wish to remove + for compatibility with old links.
|
||||
*
|
||||
* Theoretically 0x80-0x9F of ISO 8859-1 should be disallowed, but
|
||||
* this breaks interlanguage links
|
||||
*/
|
||||
// REF.MW: /includes/DefaultSettings.php
|
||||
class Title_chars_regx {
|
||||
public boolean Matches(byte[] bry) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
// $wgLegalTitleChars = " %!\"$&'()*,\\-.\\/0-9:;=?@A-Z\\\\^_`a-z~\\x80-\\xFF+"; /*
|
||||
|
||||
// */
|
@ -0,0 +1,246 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012-2017 gnosygnu@gmail.com
|
||||
|
||||
XOWA is licensed under the terms of the General Public License (GPL) Version 3,
|
||||
or alternatively under the terms of the Apache License Version 2.0.
|
||||
|
||||
You may use XOWA according to either of these licenses as is most appropriate
|
||||
for your project on a case-by-case basis.
|
||||
|
||||
The terms of each license can be found in the source code repository:
|
||||
|
||||
GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
|
||||
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
|
||||
*/
|
||||
package gplx.xowa.langs.names; import gplx.*; import gplx.xowa.*; import gplx.xowa.langs.*;
|
||||
import gplx.core.primitives.*;
|
||||
import gplx.xowa.xtns.cldrs.*;
|
||||
|
||||
public class Xol_name_mgr {
|
||||
private final Cldr_name_loader cldr_loader;
|
||||
private final Language_name_loader name_loader;
|
||||
private final Io_url root_dir;
|
||||
// private Ordered_hash hash;
|
||||
// private Keyval[] kvs;
|
||||
|
||||
public Xol_name_mgr(Cldr_name_loader cldr_loader, Language_name_loader name_loader, Io_url root_dir) {
|
||||
this.cldr_loader = cldr_loader;
|
||||
this.name_loader = name_loader;
|
||||
this.root_dir = root_dir;
|
||||
}
|
||||
/**
|
||||
* @param String $code The code of the language for which to get the name
|
||||
* @param null|String $inLanguage Code of language in which to return the name (null for autonyms)
|
||||
* @param String $include 'all', 'mw' or 'mwfile'; see fetchLanguageNames()
|
||||
* @return String Language name or empty
|
||||
* @since 1.20
|
||||
*/
|
||||
public String fetchLanguageName(String code, byte[] inLanguage, byte[] include_bry) {
|
||||
code = String_.Lower(code);
|
||||
if (include_bry == null) include_bry = fetchLanguageNamesUncached__all__key;
|
||||
Ordered_hash array = fetchLanguageNames(inLanguage, include_bry);
|
||||
Keyval rv = (Keyval)array.Get_by(code);
|
||||
return rv == null ? "" : rv.Val_to_str_or_null();
|
||||
}
|
||||
/**
|
||||
* Get an array of language names, indexed by code.
|
||||
* @param null|String $inLanguage Code of language in which to return the names
|
||||
* Use null for autonyms (native names)
|
||||
* @param String $include One of:
|
||||
* 'all' all available languages
|
||||
* 'mw' only if the language is defined in MediaWiki or wgExtraLanguageNames (default)
|
||||
* 'mwfile' only if the language is in 'mw' *and* has a message file
|
||||
* @return array Language code => language name
|
||||
* @since 1.20
|
||||
*/
|
||||
// = 'mw
|
||||
private Ordered_hash languageNameCache;
|
||||
private Ordered_hash lang_names_cached;
|
||||
private Ordered_hash lang_files_cached;
|
||||
|
||||
private static final byte[] Null_bry = Bry_.new_a7("null");
|
||||
public Ordered_hash fetchLanguageNames(byte[] inLanguage, byte[] include_bry) {
|
||||
byte[] cacheKey = inLanguage == null ? Null_bry : inLanguage;
|
||||
cacheKey = Bry_.Add(cacheKey, Byte_ascii.Colon_bry, include_bry);
|
||||
if (languageNameCache == null)
|
||||
languageNameCache = Ordered_hash_.New_bry();
|
||||
Ordered_hash ret = (Ordered_hash)languageNameCache.Get_by(cacheKey);
|
||||
if (ret == null) {
|
||||
Byte_obj_val include_byte = (Byte_obj_val)fetchLanguageNamesUncachedEnum.Get_by(include_bry);
|
||||
byte include = include_byte == null ? fetchLanguageNamesUncached__all : include_byte.Val();
|
||||
|
||||
Cldr_name_file cldr_file = cldr_loader.Load(String_.new_u8(inLanguage));
|
||||
|
||||
if (lang_names_cached == null)
|
||||
lang_names_cached = name_loader.Load_as_hash();
|
||||
|
||||
if (lang_files_cached == null) {
|
||||
lang_files_cached = Ordered_hash_.New();
|
||||
Io_url[] urls = Io_mgr.Instance.QueryDir_fils(root_dir.GenSubDir_nest("bin", "any", "xowa", "cfg", "lang", "core"));
|
||||
for (Io_url url : urls) {
|
||||
String code = url.NameOnly();
|
||||
lang_files_cached.Add(code, Keyval_.new_(code, code));
|
||||
}
|
||||
}
|
||||
|
||||
ret = fetchLanguageNamesUncached(inLanguage, include, cldr_file.Language_names(), lang_names_cached, lang_files_cached);
|
||||
languageNameCache.Add(cacheKey, ret);
|
||||
}
|
||||
return ret;
|
||||
/*
|
||||
$ret = self::$languageNameCache->get( $cacheKey );
|
||||
if ( !$ret ) {
|
||||
$ret = self::fetchLanguageNamesUncached( $inLanguage, $include );
|
||||
self::$languageNameCache->set( $cacheKey, $ret );
|
||||
}
|
||||
return $ret;
|
||||
*/
|
||||
}
|
||||
// public Keyval Get_by_or_null(String code) {
|
||||
// if (hash == null) Init();
|
||||
// return (Keyval)hash.Get_by(code);
|
||||
// }
|
||||
// public Keyval[] Get_all() {
|
||||
// if (hash == null) Init();
|
||||
// return kvs;
|
||||
// }
|
||||
/*
|
||||
private static final Hash_adp isValidCode_cache = Hash_adp_bry.cs();
|
||||
private static final Strcpn isValidCode_strcpn = Strcpn.New_by_concatenated_ascii(":/\\\000&<>'\"");
|
||||
private boolean isValidCode(byte[] code) {
|
||||
Bool_obj_val rv = (Bool_obj_val)isValidCode_cache.Get_by(code);
|
||||
if (rv == null) {
|
||||
// People think language codes are html safe, so enforce it.
|
||||
// Ideally we should only allow a-zA-Z0-9-
|
||||
// but, .+ and other chars are often used for {{int:}} hacks
|
||||
// see bugs T39564, T39587, T38938
|
||||
rv =
|
||||
// Protect against path traversal
|
||||
( isValidCode_strcpn.Exec(code) == code.length
|
||||
// && !preg_match( MediaWikiTitleCodec::getTitleInvalidRegex(), $code );
|
||||
)
|
||||
? Bool_obj_val.True
|
||||
: Bool_obj_val.False;
|
||||
isValidCode_cache.Add(code, rv);
|
||||
}
|
||||
return rv.Val();
|
||||
}
|
||||
*/
|
||||
|
||||
// private void Init() {
|
||||
// Cldr_name_file file = cldr_loader.Load(String_.new_u8(host_lang));
|
||||
// Ordered_hash cldr_names = file.Language_names();
|
||||
// Ordered_hash lang_names = name_loader.Load_as_hash();
|
||||
//
|
||||
// Ordered_hash lang_files = Ordered_hash_.New();
|
||||
// Io_url[] urls = Io_mgr.Instance.QueryDir_fils(root_dir.GenSubDir_nest("bin", "any", "xowa", "cfg", "lang", "core"));
|
||||
// foreach (Io_url url in urls) {
|
||||
// String code = url.NameOnly();
|
||||
// lang_files.Add(code, Keyval_.new_(code, code));
|
||||
// }
|
||||
//
|
||||
// hash = fetchLanguageNamesUncached(host_lang, fetchLanguageNamesUncached__mw, cldr_names, lang_names, lang_files);
|
||||
// kvs = (Keyval[])hash.To_ary(typeof(Keyval));
|
||||
// }
|
||||
|
||||
public static final byte
|
||||
fetchLanguageNamesUncached__mw = 0 // cldr + Names.php + *.json|*.php
|
||||
, fetchLanguageNamesUncached__all = 1 // cldr + Names.php
|
||||
, fetchLanguageNamesUncached__mwFile = 2 // *.json|*.php
|
||||
;
|
||||
private static final byte[] fetchLanguageNamesUncached__all__key = Bry_.new_a7("all");
|
||||
private static final Hash_adp_bry fetchLanguageNamesUncachedEnum = Hash_adp_bry.cs()
|
||||
.Add_str_byte("mw", fetchLanguageNamesUncached__mw)
|
||||
.Add_str_byte("all", fetchLanguageNamesUncached__all)
|
||||
.Add_str_byte("mwFile", fetchLanguageNamesUncached__mwFile)
|
||||
;
|
||||
public static Ordered_hash fetchLanguageNamesUncached
|
||||
( byte[] inLanguage, byte include
|
||||
, Ordered_hash cldr_names
|
||||
, Ordered_hash lang_names
|
||||
, Ordered_hash lang_files
|
||||
) {
|
||||
// XOWA: skip this for now
|
||||
// If passed an invalid language code to use, fallback to en
|
||||
// if ( $inLanguage !== null && !Language::isValidCode( $inLanguage ) ) {
|
||||
// $inLanguage = 'en';
|
||||
Ordered_hash names = Ordered_hash_.New();
|
||||
|
||||
// XOWA: 'LanguageGetTranslatedLanguageNames' is only hooked by cldr_names
|
||||
// if ( $inLanguage ) {
|
||||
// # TODO: also include when $inLanguage is null, when this code is more efficient
|
||||
// Hooks::run( 'LanguageGetTranslatedLanguageNames', [ &$names, $inLanguage ] );
|
||||
// }
|
||||
int cldr_names_len = cldr_names.Len();
|
||||
for (int i = 0; i < cldr_names_len; i++) {
|
||||
Keyval kv = (Keyval)cldr_names.Get_at(i);
|
||||
names.Add(kv.Key(), kv);
|
||||
}
|
||||
|
||||
// REF.MW: /includes/DefaultSettings.php
|
||||
// global $wgExtraLanguageNames;
|
||||
|
||||
// REF.MW: /languages/data/Names.php
|
||||
// $mwNames = $wgExtraLanguageNames + MediaWiki\Languages\Data\Names::$names;
|
||||
Ordered_hash mwNames = lang_names;
|
||||
int mwNames_len = mwNames.Len();
|
||||
String inLanguageStr = String_.new_u8(inLanguage);
|
||||
for (int i = 0; i < mwNames_len; i++) {
|
||||
Keyval mw_name = (Keyval)mwNames.Get_at(i);
|
||||
// # - Prefer own MediaWiki native name when not using the hook
|
||||
// # - For other mwNames just add if not added through the hook
|
||||
String code = mw_name.Key();
|
||||
if (String_.Eq(code, inLanguageStr) || !names.Has(code)) {
|
||||
names.Add_if_dupe_use_nth(code, Keyval_.new_(code, mw_name.Val_to_str_or_empty()));
|
||||
}
|
||||
}
|
||||
|
||||
if (include == fetchLanguageNamesUncached__all) {
|
||||
names.Sort_by(Hash_kv_sorter.Instance);
|
||||
return names;
|
||||
}
|
||||
|
||||
Ordered_hash returnMw = Ordered_hash_.New();
|
||||
mwNames_len = mwNames.Len();
|
||||
for (int i = 0; i < mwNames_len; i++) {
|
||||
Keyval coreName = (Keyval)mwNames.Get_at(i);
|
||||
String code = coreName.Key();
|
||||
returnMw.Add(code, (Keyval)names.Get_by(code));
|
||||
}
|
||||
|
||||
// REF.MW: /languages/classes/i18n/*.json
|
||||
// REF.MW: /languages/classes/messages/Messages*.php
|
||||
if (include == fetchLanguageNamesUncached__mwFile) {
|
||||
Ordered_hash namesMwFile = Ordered_hash_.New();
|
||||
// # We do this using a foreach over the codes instead of a directory
|
||||
// # loop so that messages files in extensions will work correctly.
|
||||
int returnMwLen = returnMw.Len();
|
||||
for (int i = 0; i < returnMwLen; i++) {
|
||||
Keyval kv = (Keyval)returnMw.Get_at(i);
|
||||
String code = kv.Key();
|
||||
if (lang_files.Has(code)) {
|
||||
namesMwFile.Add(code, (Keyval)names.Get_by(code));
|
||||
}
|
||||
}
|
||||
|
||||
namesMwFile.Sort_by(Hash_kv_sorter.Instance);
|
||||
return namesMwFile;
|
||||
}
|
||||
|
||||
returnMw .Sort_by(Hash_kv_sorter.Instance);
|
||||
// # 'mw' option; default if it's not one of the other two options (all/mwfile)
|
||||
return returnMw;
|
||||
}
|
||||
public static Xol_name_mgr New(Io_url root_dir) {
|
||||
return new Xol_name_mgr(new Cldr_name_loader(root_dir), new Language_name_loader(root_dir), root_dir);
|
||||
}
|
||||
}
|
||||
class Hash_kv_sorter implements gplx.core.lists.ComparerAble {
|
||||
public int compare(Object lhsObj, Object rhsObj) {
|
||||
Keyval lhs = (Keyval)lhsObj;
|
||||
Keyval rhs = (Keyval)rhsObj;
|
||||
return String_.Compare(lhs.Key(), rhs.Key());
|
||||
}
|
||||
public static final Hash_kv_sorter Instance = new Hash_kv_sorter(); Hash_kv_sorter() {}
|
||||
}
|
@ -0,0 +1,115 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012-2017 gnosygnu@gmail.com
|
||||
|
||||
XOWA is licensed under the terms of the General Public License (GPL) Version 3,
|
||||
or alternatively under the terms of the Apache License Version 2.0.
|
||||
|
||||
You may use XOWA according to either of these licenses as is most appropriate
|
||||
for your project on a case-by-case basis.
|
||||
|
||||
The terms of each license can be found in the source code repository:
|
||||
|
||||
GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
|
||||
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
|
||||
*/
|
||||
package gplx.xowa.langs.names; import gplx.*; import gplx.xowa.*; import gplx.xowa.langs.*;
|
||||
import org.junit.*;
|
||||
import gplx.core.tests.*;
|
||||
import gplx.xowa.xtns.cldrs.*;
|
||||
public class Xol_name_mgr_tst {
|
||||
private final Xol_name_mgr_fxt fxt = new Xol_name_mgr_fxt();
|
||||
|
||||
@Test public void Cldr_only() {
|
||||
fxt.Test__fetchLanguageNamesUncached
|
||||
( "en", Xol_name_mgr.fetchLanguageNamesUncached__all
|
||||
, fxt.Make__cldr_names("en", "de")
|
||||
, fxt.Make__lang_names()
|
||||
, fxt.Make__lang_files()
|
||||
, fxt.Make__expd_langs
|
||||
( "de|de_cldr"
|
||||
, "en|en_cldr"
|
||||
));
|
||||
}
|
||||
@Test public void Lang_name__langs() {
|
||||
fxt.Test__fetchLanguageNamesUncached
|
||||
( "en", Xol_name_mgr.fetchLanguageNamesUncached__all
|
||||
, fxt.Make__cldr_names("en", "de", "es")
|
||||
, fxt.Make__lang_names("en", "de", "fr")
|
||||
, fxt.Make__lang_files()
|
||||
, fxt.Make__expd_langs
|
||||
( "de|de_cldr" // do not use de_lange
|
||||
, "en|en_lang" // use en_lang b/c of "if (String_.Eq(code, inLanguage)"
|
||||
, "es|es_cldr" // make sure es_lang is still there
|
||||
, "fr|fr_lang" // add fr_lang
|
||||
));
|
||||
}
|
||||
@Test public void mwFile() {
|
||||
fxt.Test__fetchLanguageNamesUncached
|
||||
( "en", Xol_name_mgr.fetchLanguageNamesUncached__mwFile
|
||||
, fxt.Make__cldr_names("en", "de")
|
||||
, fxt.Make__lang_names("en", "de", "fr")
|
||||
, fxt.Make__lang_files("en", "de", "es")
|
||||
, fxt.Make__expd_langs
|
||||
( "de|de_cldr"
|
||||
, "en|en_lang"
|
||||
));
|
||||
}
|
||||
@Test public void mw() {
|
||||
fxt.Test__fetchLanguageNamesUncached
|
||||
( "en", Xol_name_mgr.fetchLanguageNamesUncached__mw
|
||||
, fxt.Make__cldr_names("en", "de")
|
||||
, fxt.Make__lang_names("en", "de", "fr")
|
||||
, fxt.Make__lang_files("en", "de", "es")
|
||||
, fxt.Make__expd_langs
|
||||
( "de|de_cldr"
|
||||
, "en|en_lang"
|
||||
, "fr|fr_lang"
|
||||
));
|
||||
}
|
||||
}
|
||||
class Xol_name_mgr_fxt {
|
||||
public Ordered_hash Make__cldr_names(String... vals) {return Fill(Add_suffix(vals, "_cldr"));}
|
||||
public Ordered_hash Make__lang_names(String... vals) {return Fill(Add_suffix(vals, "_lang"));}
|
||||
public Ordered_hash Make__lang_files(String... vals) {return Fill(Add_suffix(vals, "_file"));}
|
||||
public Ordered_hash Make__expd_langs(String... vals) {return Fill(vals);}
|
||||
public void Test__fetchLanguageNamesUncached
|
||||
( String inLanguage, byte include
|
||||
, Ordered_hash cldr_names
|
||||
, Ordered_hash lang_names
|
||||
, Ordered_hash lang_files
|
||||
, Ordered_hash expd_langs) {
|
||||
Ordered_hash actl_langs = Xol_name_mgr.fetchLanguageNamesUncached(Bry_.new_u8(inLanguage), include, cldr_names, lang_names, lang_files);
|
||||
Gftest.Eq__ary(To_str_ary(expd_langs), To_str_ary(actl_langs));
|
||||
}
|
||||
private static String[] Add_suffix(String[] ary, String val_suffix) {
|
||||
int len = ary.length;
|
||||
for (int i = 0; i < len; i++) {
|
||||
String itm = ary[i];
|
||||
ary[i] = itm + "|" + itm + val_suffix;
|
||||
}
|
||||
return ary;
|
||||
}
|
||||
private static String[] To_str_ary(Ordered_hash hash) {
|
||||
int len = hash.Len();
|
||||
String[] rv = new String[len];
|
||||
for (int i = 0; i < len; i++) {
|
||||
Keyval kv = (Keyval)hash.Get_at(i);
|
||||
rv[i] = kv.Key() + "|" + kv.Val();
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
private static Ordered_hash Fill(String... ary) {
|
||||
Ordered_hash hash = Ordered_hash_.New();
|
||||
int len = ary.length;
|
||||
for (int i = 0; i < len; i++) {
|
||||
Keyval kv = Split(ary[i]);
|
||||
hash.Add(kv.Key(), kv);
|
||||
}
|
||||
return hash;
|
||||
}
|
||||
private static Keyval Split(String str) {
|
||||
String[] ary = String_.Split(str, "|");
|
||||
return Keyval_.new_(ary[0], ary.length == 1 ? ary[0] : ary[1]);
|
||||
}
|
||||
}
|
@ -0,0 +1,24 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012-2017 gnosygnu@gmail.com
|
||||
|
||||
XOWA is licensed under the terms of the General Public License (GPL) Version 3,
|
||||
or alternatively under the terms of the Apache License Version 2.0.
|
||||
|
||||
You may use XOWA according to either of these licenses as is most appropriate
|
||||
for your project on a case-by-case basis.
|
||||
|
||||
The terms of each license can be found in the source code repository:
|
||||
|
||||
GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
|
||||
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
|
||||
*/
|
||||
package gplx.xowa.xtns.cldrs; import gplx.*; import gplx.xowa.*; import gplx.xowa.xtns.*;
|
||||
public class Cldr_name_xtn_mgr extends Xox_mgr_base {
|
||||
// private Cldr_name_loader loader;
|
||||
@Override public byte[] Xtn_key() {return XTN_KEY;} public static final byte[] XTN_KEY = Bry_.new_a7("CldrNames");
|
||||
@Override public Xox_mgr Xtn_clone_new() {return new Cldr_name_xtn_mgr();}
|
||||
@Override public void Xtn_init_by_wiki(Xowe_wiki wiki) {
|
||||
// loader = new Cldr_name_loader(wiki.App().Fsys_mgr().Bin_xtns_dir());
|
||||
}
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012-2017 gnosygnu@gmail.com
|
||||
|
||||
XOWA is licensed under the terms of the General Public License (GPL) Version 3,
|
||||
or alternatively under the terms of the Apache License Version 2.0.
|
||||
|
||||
You may use XOWA according to either of these licenses as is most appropriate
|
||||
for your project on a case-by-case basis.
|
||||
|
||||
The terms of each license can be found in the source code repository:
|
||||
|
||||
GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
|
||||
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
|
||||
*/
|
||||
package gplx.xowa.xtns.cldrs; import gplx.*; import gplx.xowa.*; import gplx.xowa.xtns.*;
|
||||
public class Language_name implements gplx.core.brys.Bry_bfr_able {
|
||||
public Language_name(byte[] code, byte[] name, byte[] note) {
|
||||
this.code = code;
|
||||
this.name = name;
|
||||
this.note = note;
|
||||
}
|
||||
public byte[] Code() {return code;} private final byte[] code;
|
||||
public byte[] Name() {return name;} private final byte[] name;
|
||||
public byte[] Note() {return note;} private final byte[] note;
|
||||
public void To_bfr(Bry_bfr bfr) {
|
||||
bfr.Add(code).Add_byte_pipe();
|
||||
bfr.Add(name).Add_byte_pipe();
|
||||
bfr.Add(note);
|
||||
}
|
||||
}
|
@ -0,0 +1,57 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012-2017 gnosygnu@gmail.com
|
||||
|
||||
XOWA is licensed under the terms of the General Public License (GPL) Version 3,
|
||||
or alternatively under the terms of the Apache License Version 2.0.
|
||||
|
||||
You may use XOWA according to either of these licenses as is most appropriate
|
||||
for your project on a case-by-case basis.
|
||||
|
||||
The terms of each license can be found in the source code repository:
|
||||
|
||||
GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
|
||||
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
|
||||
*/
|
||||
package gplx.xowa.xtns.cldrs; import gplx.*; import gplx.xowa.*; import gplx.xowa.xtns.*;
|
||||
import gplx.langs.jsons.*;
|
||||
public class Language_name_loader {
|
||||
private final Json_parser parser = new Json_parser();
|
||||
private final Io_url names_url;
|
||||
|
||||
public Language_name_loader(Io_url root_dir) {
|
||||
names_url = root_dir.GenSubFil_nest("bin", "any", "xowa", "cfg", "lang", "data", "names.json");
|
||||
}
|
||||
public Ordered_hash Load_as_hash() {
|
||||
byte[] json = Io_mgr.Instance.LoadFilBry(names_url);
|
||||
if (json == null) {
|
||||
Gfo_usr_dlg_.Instance.Warn_many("", "", "no names file exists");
|
||||
return Ordered_hash_.New();
|
||||
}
|
||||
Language_name[] ary = Load(json);
|
||||
|
||||
// convert
|
||||
Ordered_hash rv = Ordered_hash_.New();
|
||||
int len = ary.length;
|
||||
for (int i = 0; i < len; i++) {
|
||||
Language_name itm = ary[i];
|
||||
rv.Add(itm.Code(), Keyval_.new_(String_.new_u8(itm.Code()), itm.Name()));
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
public Language_name[] Load(byte[] json) {
|
||||
List_adp list = List_adp_.New();
|
||||
|
||||
Json_doc jdoc = parser.Parse(json);
|
||||
Json_ary root = jdoc.Root_ary();
|
||||
int len = root.Len();
|
||||
for (int i = 0; i < len; i++) {
|
||||
Json_nde node = root.Get_as_nde(i);
|
||||
byte[] code = node.Get_as_bry("code");
|
||||
byte[] name = node.Get_as_bry("name");
|
||||
Language_name itm = new Language_name(code, name, null);
|
||||
list.Add(itm);
|
||||
}
|
||||
return (Language_name[])list.To_ary_and_clear(Language_name.class);
|
||||
}
|
||||
}
|
@ -0,0 +1,93 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012-2017 gnosygnu@gmail.com
|
||||
|
||||
XOWA is licensed under the terms of the General Public License (GPL) Version 3,
|
||||
or alternatively under the terms of the Apache License Version 2.0.
|
||||
|
||||
You may use XOWA according to either of these licenses as is most appropriate
|
||||
for your project on a case-by-case basis.
|
||||
|
||||
The terms of each license can be found in the source code repository:
|
||||
|
||||
GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
|
||||
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
|
||||
*/
|
||||
package gplx.xowa.xtns.cldrs; import gplx.*; import gplx.xowa.*; import gplx.xowa.xtns.*;
|
||||
import gplx.core.primitives.*;
|
||||
import gplx.langs.phps.*;
|
||||
import gplx.langs.jsons.*;
|
||||
|
||||
// REF.MW: /languages/data/Names.php
|
||||
class Language_names_converter {
|
||||
private final Php_parser parser = new Php_parser();
|
||||
private final Php_evaluator eval = new Php_evaluator(new gplx.core.log_msgs.Gfo_msg_log("test")).Comments_for_kv_();
|
||||
private final Php_text_itm_parser text_itm_parser = new Php_text_itm_parser().Quote_is_single_(true);
|
||||
private final List_adp tmp_list = List_adp_.New();
|
||||
private final Byte_obj_ref tmp_result = Byte_obj_ref.zero_();
|
||||
private final Bry_bfr tmp_bfr = Bry_bfr_.New();
|
||||
|
||||
public Language_name[] Parse_fil(Io_url url) {
|
||||
byte[] src = Io_mgr.Instance.LoadFilBry(url);
|
||||
int bgn = Bry_find_.Find_fwd(src, Bry_.new_a7("$names"));
|
||||
int end = Bry_find_.Find_bwd(src, Bry_.new_a7("];"), src.length);
|
||||
return Parse(Bry_.Mid(src, bgn, end));
|
||||
}
|
||||
public Language_name[] Parse(byte[] src) {
|
||||
parser.Parse_tkns(src, eval);
|
||||
Php_line[] lines = (Php_line[])eval.List().To_ary(Php_line.class);
|
||||
|
||||
List_adp rv = List_adp_.New();
|
||||
int lines_len = lines.length;
|
||||
for (int i = 0; i < lines_len; i++) {
|
||||
Php_line line = lines[i];
|
||||
Php_line_assign assign_line = (Php_line_assign)line;
|
||||
|
||||
byte[][][] ary = Parse_ary_kvs(src, assign_line);
|
||||
for (byte[][] itm : ary) {
|
||||
rv.Add(new Language_name(itm[0], itm[1], itm[2]));
|
||||
}
|
||||
}
|
||||
eval.Clear();
|
||||
return (Language_name[])rv.To_ary_and_clear(Language_name.class);
|
||||
}
|
||||
private byte[][][] Parse_ary_kvs(byte[] src, Php_line_assign assign) {
|
||||
List_adp list = List_adp_.New();
|
||||
Php_itm_ary ary = (Php_itm_ary)assign.Val();
|
||||
int ary_len = ary.Subs_len();
|
||||
for (int i = 0; i < ary_len; i++) {
|
||||
Php_itm_kv kv = (Php_itm_kv)ary.Subs_get(i);
|
||||
byte[] key = kv.Key().Val_obj_bry();
|
||||
byte[] val = text_itm_parser.Parse_as_bry(tmp_list, kv.Val().Val_obj_bry(), tmp_result, tmp_bfr);
|
||||
|
||||
int comments_len = kv.Comments__len();
|
||||
for (int j = 0; j < comments_len; j++) {
|
||||
if (j != 0) tmp_bfr.Add_byte_space();
|
||||
Php_tkn_comment tkn = kv.Comments__get_at__or_null(j);
|
||||
tkn.To_bfr(tmp_bfr, src, true);
|
||||
}
|
||||
byte[] comm = tmp_bfr.To_bry_and_clear();
|
||||
list.Add(new byte[][] {key, val, comm});
|
||||
}
|
||||
return (byte[][][])list.To_ary_and_clear(byte[][].class);
|
||||
}
|
||||
public String To_json(Language_name[] ary) {
|
||||
Json_doc_wtr doc_wtr = new Json_doc_wtr();
|
||||
doc_wtr.Ary_bgn();
|
||||
int len = ary.length;
|
||||
byte[] key_code = Bry_.new_a7("code");
|
||||
byte[] key_name = Bry_.new_a7("name");
|
||||
byte[] key_note = Bry_.new_a7("note");
|
||||
for (int i = 0; i < len; i++) {
|
||||
if (i != 0) doc_wtr.Comma();
|
||||
doc_wtr.Nde_bgn();
|
||||
Language_name itm = ary[i];
|
||||
doc_wtr.Kv(Bool_.N, key_code, itm.Code());
|
||||
doc_wtr.Kv(Bool_.Y, key_name, itm.Name());
|
||||
doc_wtr.Kv(Bool_.Y, key_note, itm.Note());
|
||||
doc_wtr.Nde_end();
|
||||
}
|
||||
doc_wtr.Ary_end();
|
||||
return doc_wtr.Bld_as_str();
|
||||
}
|
||||
}
|
@ -0,0 +1,71 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012-2017 gnosygnu@gmail.com
|
||||
|
||||
XOWA is licensed under the terms of the General Public License (GPL) Version 3,
|
||||
or alternatively under the terms of the Apache License Version 2.0.
|
||||
|
||||
You may use XOWA according to either of these licenses as is most appropriate
|
||||
for your project on a case-by-case basis.
|
||||
|
||||
The terms of each license can be found in the source code repository:
|
||||
|
||||
GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
|
||||
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
|
||||
*/
|
||||
package gplx.xowa.xtns.cldrs; import gplx.*; import gplx.xowa.*; import gplx.xowa.xtns.*;
|
||||
import org.junit.*; import gplx.core.tests.*;
|
||||
import gplx.langs.phps.*;
|
||||
import gplx.langs.jsons.*;
|
||||
public class Language_names_converter_tst {
|
||||
private final Language_names_converter_fxt fxt = new Language_names_converter_fxt();
|
||||
@Test public void Parse_fil() {
|
||||
fxt.Exec__Parse(String_.Concat_lines_nl
|
||||
( "/*ignore_bgn*/ $names = ["
|
||||
, " 'aa' => 'Afar', # comment_a "
|
||||
, " 'mic' => 'Mikmaq', # comment_m "
|
||||
, " 'zza' => 'Zaza', # comment_z "
|
||||
, "]; /*ignore_end*/"
|
||||
)
|
||||
, new Language_name[]
|
||||
{ fxt.Make__language_name("aa", "Afar", "comment_a")
|
||||
, fxt.Make__language_name("mic", "Mikmaq", "comment_m")
|
||||
, fxt.Make__language_name("zza", "Zaza", "comment_z")
|
||||
}, Json_doc.Make_str_by_apos
|
||||
( "["
|
||||
, " {"
|
||||
, " 'code':'aa'"
|
||||
, " , 'name':'Afar'"
|
||||
, " , 'note':'comment_a'"
|
||||
, " }"
|
||||
, ","
|
||||
, " {"
|
||||
, " 'code':'mic'"
|
||||
, " , 'name':'Mikmaq'"
|
||||
, " , 'note':'comment_m'"
|
||||
, " }"
|
||||
, ","
|
||||
, " {"
|
||||
, " 'code':'zza'"
|
||||
, " , 'name':'Zaza'"
|
||||
, " , 'note':'comment_z'"
|
||||
, " }"
|
||||
, "]"
|
||||
));
|
||||
}
|
||||
@Test public void Convert() {
|
||||
Language_names_converter converter = new Language_names_converter();
|
||||
Language_name[] names = converter.Parse_fil(Io_url_.new_dir_("C:\\000\\100_bin\\200_server\\200_http\\100_apache\\100_v2.4\\htdocs\\mediawiki\\v1.29.1\\languages\\data\\Names.php"));
|
||||
String json = converter.To_json(names);
|
||||
Io_mgr.Instance.SaveFilStr(Io_url_.new_fil_("C:\\xowa\\bin\\any\\xowa\\cfg\\lang\\data\\names.json"), json);
|
||||
}
|
||||
}
|
||||
class Language_names_converter_fxt {
|
||||
private final Language_names_converter converter = new Language_names_converter();
|
||||
public Language_name Make__language_name(String code, String name, String note) {return new Language_name(Bry_.new_u8(code), Bry_.new_u8(name), Bry_.new_u8(note));}
|
||||
public void Exec__Parse(String src, Language_name[] expd_names, String expd_json) {
|
||||
Language_name[] actl = converter.Parse(Bry_.new_u8(src));
|
||||
Gftest.Eq__ary(expd_names, actl);
|
||||
Gftest.Eq__ary__lines(expd_json, converter.To_json(actl));
|
||||
}
|
||||
}
|
@ -0,0 +1,76 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012-2017 gnosygnu@gmail.com
|
||||
|
||||
XOWA is licensed under the terms of the General Public License (GPL) Version 3,
|
||||
or alternatively under the terms of the Apache License Version 2.0.
|
||||
|
||||
You may use XOWA according to either of these licenses as is most appropriate
|
||||
for your project on a case-by-case basis.
|
||||
|
||||
The terms of each license can be found in the source code repository:
|
||||
|
||||
GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
|
||||
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
|
||||
*/
|
||||
package gplx.xowa.xtns.template_styles; import gplx.*; import gplx.xowa.*; import gplx.xowa.xtns.*;
|
||||
import gplx.xowa.wikis.caches.*;
|
||||
import gplx.xowa.htmls.core.htmls.*; import gplx.xowa.parsers.htmls.*;
|
||||
import gplx.xowa.parsers.*; import gplx.xowa.parsers.xndes.*;
|
||||
import gplx.xowa.wikis.nss.*;
|
||||
public class Template_styles_nde implements Xox_xnde, Mwh_atr_itm_owner2 {
|
||||
private byte[] css_ttl_bry;
|
||||
private byte[] css_src;
|
||||
private Xoa_ttl css_ttl;
|
||||
public void Xatr__set(Xowe_wiki wiki, byte[] src, Mwh_atr_itm xatr, byte xatr_id) {
|
||||
switch (xatr_id) {
|
||||
case Xatr__src: css_ttl_bry = xatr.Val_as_bry(); break;
|
||||
}
|
||||
}
|
||||
public void Xtn_parse(Xowe_wiki wiki, Xop_ctx ctx, Xop_root_tkn root, byte[] src, Xop_xnde_tkn xnde) {
|
||||
ctx.Para().Process_block__xnde(xnde.Tag(), Xop_xnde_tag.Block_bgn);
|
||||
Xox_xnde_.Parse_xatrs(wiki, this, xatrs_hash, src, xnde);
|
||||
|
||||
// get css_ttl
|
||||
css_ttl = wiki.Ttl_parse(css_ttl_bry);
|
||||
if (css_ttl == null) {
|
||||
Gfo_usr_dlg_.Instance.Warn_many("", "", "Template_styles_nde.invalid_ttl: wiki=~{0} page=~{1} css_ttl=~{2}", wiki.Domain_bry(), ctx.Page().Url_bry_safe(), css_ttl_bry);
|
||||
return;
|
||||
}
|
||||
|
||||
// assume "Template:" if no explicit ns and no ":"
|
||||
if (!css_ttl.ForceLiteralLink() // no initial ":"
|
||||
&& css_ttl.Ns().Id_is_main()) {
|
||||
css_ttl = wiki.Ttl_parse(Bry_.Add(Xow_ns_.Bry__template_w_colon, css_ttl_bry));
|
||||
}
|
||||
|
||||
// get page
|
||||
Xow_page_cache_itm page_itm = wiki.Cache_mgr().Page_cache().Get_or_load_as_itm_2(css_ttl);
|
||||
css_src = page_itm == null ? null : page_itm.Wtxt__direct();
|
||||
if (css_src == null) {
|
||||
Gfo_usr_dlg_.Instance.Warn_many("", "", "Template_styles_nde.page_not_found: wiki=~{0} page=~{1} css_ttl=~{2}", wiki.Domain_bry(), ctx.Page().Url_bry_safe(), css_ttl_bry);
|
||||
}
|
||||
}
|
||||
public void Xtn_write(Bry_bfr bfr, Xoae_app app, Xop_ctx ctx, Xoh_html_wtr html_wtr, Xoh_wtr_ctx hctx, Xoae_page wpg, Xop_xnde_tkn xnde, byte[] src) {
|
||||
if (css_ttl == null) {
|
||||
bfr.Add_str_a7(formatTagError("Invalid title for TemplateStyles src attribute."));
|
||||
}
|
||||
else if (css_src == null) {
|
||||
bfr.Add_str_a7(formatTagError("Page " + String_.new_u8(css_ttl_bry) + " has no content."));
|
||||
}
|
||||
else {
|
||||
bfr.Add_str_a7("<style>");
|
||||
bfr.Add(css_src);
|
||||
bfr.Add_str_a7("</style>");
|
||||
}
|
||||
}
|
||||
private static String formatTagError(String msg) {
|
||||
// $parser->addTrackingCategory( 'templatestyles-page-error-category' );
|
||||
return "<strong class=\"error\">"
|
||||
// + call_user_func_array( 'wfMessage', $msg )->inContentLanguage()->parse()
|
||||
+ msg
|
||||
+ "</strong>";
|
||||
}
|
||||
public static final byte Xatr__src = 0;
|
||||
private static final Hash_adp_bry xatrs_hash = Hash_adp_bry.ci_a7().Add_str_byte("src", Xatr__src);
|
||||
}
|
@ -0,0 +1,71 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012-2017 gnosygnu@gmail.com
|
||||
|
||||
XOWA is licensed under the terms of the General Public License (GPL) Version 3,
|
||||
or alternatively under the terms of the Apache License Version 2.0.
|
||||
|
||||
You may use XOWA according to either of these licenses as is most appropriate
|
||||
for your project on a case-by-case basis.
|
||||
|
||||
The terms of each license can be found in the source code repository:
|
||||
|
||||
GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
|
||||
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
|
||||
*/
|
||||
package gplx.xowa.xtns.template_styles; import gplx.*; import gplx.xowa.*; import gplx.xowa.xtns.*;
|
||||
import org.junit.*;
|
||||
public class Template_styles_nde_tst {
|
||||
private final Template_styles_nde_fxt fxt = new Template_styles_nde_fxt();
|
||||
@Before public void init() {
|
||||
fxt.Reset();
|
||||
}
|
||||
private static final String Css_red = ".red{color:red;}";
|
||||
private static final String Style_red = "<style>" + Css_red + "</style>";
|
||||
@Test public void Implicit_template() { // PURPOSE: default to template
|
||||
fxt.Init__page("Template:Test.css", Css_red);
|
||||
fxt.Test__parse
|
||||
( "<templatestyles src='Test.css'/>"
|
||||
, Style_red
|
||||
);
|
||||
}
|
||||
@Test public void Force_main() { // PURPOSE: ":" forces main
|
||||
fxt.Init__page("Test.css", Css_red);
|
||||
fxt.Test__parse
|
||||
( "<templatestyles src=':Test.css'/>"
|
||||
, Style_red
|
||||
);
|
||||
}
|
||||
@Test public void Explicit() { // PURPOSE: explicit ns
|
||||
fxt.Init__page("Module:Test.css", Css_red);
|
||||
fxt.Test__parse
|
||||
( "<templatestyles src='Module:Test.css'/>"
|
||||
, Style_red
|
||||
);
|
||||
}
|
||||
@Test public void Error__invalid_title() {
|
||||
fxt.Test__parse
|
||||
( "<templatestyles src='A|b.css'/>"
|
||||
, "<strong class=\"error\">Invalid title for TemplateStyles src attribute.</strong>"
|
||||
);
|
||||
}
|
||||
@Test public void Error__missing_page() {
|
||||
fxt.Test__parse
|
||||
( "<templatestyles src='Missing.css'/>"
|
||||
, "<strong class=\"error\">Page Missing.css has no content.</strong>"
|
||||
);
|
||||
}
|
||||
}
|
||||
class Template_styles_nde_fxt {
|
||||
private final Xop_fxt parser_fxt = new Xop_fxt();
|
||||
public void Reset() {
|
||||
parser_fxt.Reset();
|
||||
parser_fxt.Wiki().Xtn_mgr().Init_by_wiki(parser_fxt.Wiki());
|
||||
}
|
||||
public void Init__page(String page, String text) {
|
||||
parser_fxt.Init_page_create(page, text);
|
||||
}
|
||||
public void Test__parse(String src, String expd) {
|
||||
parser_fxt.Test__parse__tmpl_to_html(src, expd);
|
||||
}
|
||||
}
|
Loading…
Reference in new issue