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

Embeddable: Create core dbs in proper subdirectory

This commit is contained in:
gnosygnu
2017-10-23 20:50:22 -04:00
parent dc22c15895
commit 1336d44f34
4537 changed files with 0 additions and 311750 deletions

View File

@@ -13,95 +13,3 @@ 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.wikis.nss; import gplx.*; import gplx.xowa.*; import gplx.xowa.wikis.*;
import gplx.xowa.bldrs.cmds.*; import gplx.xowa.apps.utls.*;
public class Xow_ns implements Gfo_invk {
public Xow_ns(int id, byte case_match, byte[] name, boolean is_alias) {
this.id = id; this.case_match = case_match; this.is_alias = is_alias;
Name_bry_(name);
}
public void Name_bry_(byte[] v) {
if (id == Xow_ns_.Tid__main) { // NOTE: Main will never prefix titles; EX: "Test" vs "Category:Test"
this.name_db_str = "";
this.name_db = Bry_.Empty;
this.name_db_w_colon = Bry_.Empty;
}
else {
this.name_db = v;
this.name_db_w_colon = Bry_.Add(v, Byte_ascii.Colon);
this.name_db_str = String_.new_u8(v);
}
this.num_str = Int_.To_str_pad_bgn_zero(id, 3);
this.num_bry = Bry_.new_a7(num_str);
synchronized (url_encoder) { // LOCK:static-obj
this.name_enc = url_encoder.Encode(name_db);
}
this.name_ui = Bry_.Replace(name_enc, Byte_ascii.Underline, Byte_ascii.Space);
this.name_ui_w_colon = Bry_.Replace(name_db_w_colon, Byte_ascii.Underline, Byte_ascii.Space);
}
public boolean Exists() {return exists;} public Xow_ns Exists_(boolean v) {exists = v; return this;} private boolean exists;
public byte[] Name_db() {return name_db;} private byte[] name_db;
public byte[] Name_db_w_colon() {return name_db_w_colon;} private byte[] name_db_w_colon;
public String Name_db_str() {return name_db_str;} private String name_db_str;
public byte[] Name_ui() {return name_ui;} private byte[] name_ui;
public byte[] Name_ui_w_colon() {return name_ui_w_colon;} private byte[] name_ui_w_colon; // PERF: for Xoa_ttl
public byte[] Name_enc() {return name_enc;} private byte[] name_enc;
public byte[] Name_combo() {return id == Xow_ns_.Tid__main ? Xow_ns_.Bry__main: name_ui;} // for combo boxes; namely "(Main)"
public String Num_str() {return num_str;} private String num_str;
public byte[] Num_bry() {return num_bry;} private byte[] num_bry;
public int Id() {return id;} private int id;
public int Id_subj_id() {if (id < 0) return id; return Id_is_talk() ? id - 1 : id;} // id< 0: special/media return themself; REF.MW: Namespace.php|getSubject
public int Id_talk_id() {if (id < 0) return id; return Id_is_talk() ? id : id + 1;} // REF.MW: Namespace.php|getTalk
public int Id_alt_id() {if (id < 0) return id; return Id_is_talk() ? Id_subj_id() : Id_talk_id();} // REF.MW: Namespace.php|getTalk
public boolean Id_is_subj() {return !Id_is_talk();} // REF.MW: Namespace.php|isMain
public boolean Id_is_talk() {return id > Xow_ns_.Tid__main && id % 2 == 1;} // REF.MW: Namespace.php|isTalk
public boolean Id_is_media() {return id == Xow_ns_.Tid__media;}
public boolean Id_is_special() {return id == Xow_ns_.Tid__special;}
public boolean Id_is_main() {return id == Xow_ns_.Tid__main;}
public boolean Id_is_file() {return id == Xow_ns_.Tid__file;}
public boolean Id_is_file_or_media() {return id == Xow_ns_.Tid__file || id == Xow_ns_.Tid__media;}
public boolean Id_is_tmpl() {return id == Xow_ns_.Tid__template;}
public boolean Id_is_ctg() {return id == Xow_ns_.Tid__category;}
public boolean Id_is_module() {return id == Xow_ns_.Tid__module;}
public int Ord() {return ord;} public void Ord_(int v) {this.ord = v;} private int ord;
public int Ord_subj_id() {if (id < 0) return ord; return Id_is_talk() ? ord - 1 : ord;} // id< 0: special/media returns self
public int Ord_talk_id() {if (id < 0) return ord; return Id_is_talk() ? ord : ord + 1;}
public byte Case_match() {return case_match;} public void Case_match_(byte v) {case_match = v;} private byte case_match;
public boolean Subpages_enabled() {return subpages_enabled;} private boolean subpages_enabled = false;// CHANGED: id > Xow_ns_.Tid__special; only Special, Media does not have subpages; DATE:2013-10-07
public Xow_ns Subpages_enabled_(boolean v) {subpages_enabled = v; return this;}
public boolean Is_gender_aware() {return id == Xow_ns_.Tid__user || id == Xow_ns_.Tid__user_talk;} // ASSUME: only User, User_talk are gender aware
public boolean Is_capitalized() {return false;} // ASSUME: always false (?)
public boolean Is_content() {return id == Xow_ns_.Tid__main;} // ASSUME: only Main
public boolean Is_includable() {return true;} // ASSUME: always true (be transcluded?)
public boolean Is_movable() {return id > Xow_ns_.Tid__special;} // ASSUME: only Special, Media cannot move (be renamed?)
public boolean Is_meta() {return id < Xow_ns_.Tid__main;} // ASSUME: only Special, Media
public boolean Is_alias() {return is_alias;} private boolean is_alias;
public int Count() {return count;} public Xow_ns Count_(int v) {count = v; return this;} private int count;
public byte[] Gen_ttl(byte[] page) {return id == Xow_ns_.Tid__main ? page : Bry_.Add(name_db_w_colon, page);}
public void Aliases_add(String alias) {
if (String_.Eq(alias, name_db_str)) return;
if (aliases == null) aliases = Ordered_hash_.New();
aliases.Add_if_dupe_use_1st(alias, alias);
} private Ordered_hash aliases;
public Keyval[] Aliases_as_scrib_ary() { // NOTE: intended for Scrib_lib_site; DATE:2014-02-15
if (aliases == null) return Keyval_.Ary_empty;
int len = aliases.Count();
Keyval[] rv = new Keyval[len];
for (int i = 0; i < len; i++) {
String alias = (String)aliases.Get_at(i);
rv[i] = Keyval_.int_(i + List_adp_.Base1, alias);
}
return rv;
}
public Xob_ns_file_itm Bldr_data() {return bldr_data;} public void Bldr_data_(Xob_ns_file_itm v) {bldr_data = v;} private Xob_ns_file_itm bldr_data;
public Object Invk(GfsCtx ctx, int ikey, String k, GfoMsg m) {
if (ctx.Match(k, Invk_subpages_enabled_)) this.subpages_enabled = m.ReadYn("v");
else if (ctx.Match(k, Invk_id)) return id;
else if (ctx.Match(k, Invk_name_txt)) return name_ui;
else if (ctx.Match(k, Invk_name_ui)) return Name_combo();
else return Gfo_invk_.Rv_unhandled;
return this;
} private static final String Invk_subpages_enabled_ = "subpages_enabled_", Invk_id = "id", Invk_name_txt = "name_txt", Invk_name_ui = "name_ui";
private static final Xoa_url_encoder url_encoder = new Xoa_url_encoder();
}

View File

@@ -13,61 +13,3 @@ 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.wikis.nss; import gplx.*; import gplx.xowa.*; import gplx.xowa.wikis.*;
public class Xow_ns_ {
public static final int // PAGE:en.w:http://www.mediawiki.org/wiki/Help:Namespaces
Tid__media = -2
, Tid__special = -1
, Tid__main = 0 , Tid__talk = 1
, Tid__user = 2 , Tid__user_talk = 3
, Tid__project = 4 , Tid__project_talk = 5
, Tid__file = 6 , Tid__file_talk = 7
, Tid__mediawiki = 8 , Tid__mediawiki_talk = 9
, Tid__template = 10 , Tid__template_talk = 11
, Tid__help = 12 , Tid__help_talk = 13
, Tid__category = 14 , Tid__category_talk = 15
, Tid__portal = 100 , Tid__portal_talk = 101
, Tid__module = 828 , Tid__module_talk = 829
, Tid__null = Int_.Min_value
;
public static final String
Key__media = "Media"
, Key__special = "Special"
, Key__main = "(Main)" , Key__talk = "Talk"
, Key__user = "User" , Key__user_talk = "User_talk"
, Key__project = "Project" , Key__project_talk = "Project_talk"
, Key__file = "File" , Key__file_talk = "File_talk"
, Key__mediawiki = "MediaWiki" , Key__mediawiki_talk = "MediaWiki_talk"
, Key__template = "Template" , Key__template_talk = "Template_talk"
, Key__help = "Help" , Key__help_talk = "Help_talk"
, Key__category = "Category" , Key__category_talk = "Category_talk"
, Key__portal = "Portal" , Key__portal_talk = "Portal_talk"
, Key__module = "Module" , Key__module_talk = "Module_talk"
, Key__null = "null"
, Key__wikipedia = "Wikipedia"
;
public static final byte[]
Bry__media = Bry_.new_a7(Key__media)
, Bry__special = Bry_.new_a7(Key__special)
, Bry__main = Bry_.new_a7(Key__main) , Bry__talk = Bry_.new_a7(Key__talk)
, Bry__user = Bry_.new_a7(Key__user) , Bry__user_talk = Bry_.new_a7(Key__user_talk)
, Bry__project = Bry_.new_a7(Key__project) , Bry__project_talk = Bry_.new_a7(Key__project_talk)
, Bry__file = Bry_.new_a7(Key__file) , Bry__file_talk = Bry_.new_a7(Key__file_talk)
, Bry__mediawiki = Bry_.new_a7(Key__mediawiki) , Bry__mediawiki_talk = Bry_.new_a7(Key__mediawiki_talk)
, Bry__template = Bry_.new_a7(Key__template) , Bry__template_talk = Bry_.new_a7(Key__template_talk)
, Bry__help = Bry_.new_a7(Key__help) , Bry__help_talk = Bry_.new_a7(Key__help_talk)
, Bry__category = Bry_.new_a7(Key__category) , Bry__category_talk = Bry_.new_a7(Key__category_talk)
, Bry__portal = Bry_.new_a7(Key__portal) , Bry__portal_talk = Bry_.new_a7(Key__portal_talk)
, Bry__module = Bry_.new_a7(Key__module) , Bry__module_talk = Bry_.new_a7(Key__module_talk)
, Bry__null = Bry_.new_a7(Key__null)
;
public static final String
Alias__wikipedia = "Wikipedia"
, Alias__image = "Image"
;
public static final byte[] Alias__image__bry = Bry_.new_a7(Alias__image);
public static final byte[]
Bry__template_w_colon = Bry_.new_a7(Key__template + ":")
, Bry__module_w_colon = Bry_.new_a7(Key__module + ":")
;
}

View File

@@ -13,116 +13,3 @@ 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.wikis.nss; import gplx.*; import gplx.xowa.*; import gplx.xowa.wikis.*;
import gplx.core.primitives.*;
public class Xow_ns_canonical_ {
public static final Xow_ns[] Ary = new Xow_ns[] // REF.MW: Namespace.php|$wgCanonicalNamespaceNames
{ New_itm(Xow_ns_.Tid__media , Xow_ns_.Key__media)
, New_itm(Xow_ns_.Tid__special , Xow_ns_.Key__special)
, New_itm(Xow_ns_.Tid__talk , Xow_ns_.Key__talk)
, New_itm(Xow_ns_.Tid__user , Xow_ns_.Key__user)
, New_itm(Xow_ns_.Tid__user_talk , Xow_ns_.Key__user_talk)
, New_itm(Xow_ns_.Tid__project , Xow_ns_.Key__project)
, New_itm(Xow_ns_.Tid__project_talk , Xow_ns_.Key__project_talk)
, New_itm(Xow_ns_.Tid__file , Xow_ns_.Key__file)
, New_itm(Xow_ns_.Tid__file_talk , Xow_ns_.Key__file_talk)
, New_itm(Xow_ns_.Tid__mediawiki , Xow_ns_.Key__mediawiki)
, New_itm(Xow_ns_.Tid__mediawiki_talk , Xow_ns_.Key__mediawiki_talk)
, New_itm(Xow_ns_.Tid__template , Xow_ns_.Key__template)
, New_itm(Xow_ns_.Tid__template_talk , Xow_ns_.Key__template_talk)
, New_itm(Xow_ns_.Tid__help , Xow_ns_.Key__help)
, New_itm(Xow_ns_.Tid__help_talk , Xow_ns_.Key__help_talk)
, New_itm(Xow_ns_.Tid__category , Xow_ns_.Key__category)
, New_itm(Xow_ns_.Tid__category_talk , Xow_ns_.Key__category_talk)
, New_itm(Xow_ns_.Tid__module , Xow_ns_.Key__module)
, New_itm(Xow_ns_.Tid__module_talk , Xow_ns_.Key__module_talk)
};
private static Xow_ns New_itm(int id, String name) {return new Xow_ns(id, Xow_ns_case_.Tid__1st, Bry_.new_a7(name), false);} // NOTE: for id/name reference only; case_match and alias does not matter;
private static Ordered_hash id_hash;
public static int To_id(byte[] key) {
if (id_hash == null) {
id_hash = Ordered_hash_.New_bry();
int len = Ary.length;
for (int i = 0; i < len; ++i) {
Xow_ns ns = Ary[i];
id_hash.Add(ns.Name_db(), new Int_obj_val(ns.Id()));
}
}
Object rv_obj = id_hash.Get_by(key);
return rv_obj == null ? Xow_ns_.Tid__null : ((Int_obj_val)rv_obj).Val();
}
public static String To_canonical_or_local_as_str(Xow_ns ns) { // NOTE: prefer canonical names if they exist; otherwise use local; PAGE:sh.w:Koprno; DATE:2015-11-08
switch (ns.Id()) {
case Xow_ns_.Tid__media: return Xow_ns_.Key__media;
case Xow_ns_.Tid__special: return Xow_ns_.Key__special;
case Xow_ns_.Tid__talk: return Xow_ns_.Key__talk;
case Xow_ns_.Tid__user: return Xow_ns_.Key__user;
case Xow_ns_.Tid__user_talk: return Xow_ns_.Key__user_talk;
case Xow_ns_.Tid__project: return Xow_ns_.Key__project;
case Xow_ns_.Tid__project_talk: return Xow_ns_.Key__project_talk;
case Xow_ns_.Tid__file: return Xow_ns_.Key__file;
case Xow_ns_.Tid__file_talk: return Xow_ns_.Key__file_talk;
case Xow_ns_.Tid__mediawiki: return Xow_ns_.Key__mediawiki;
case Xow_ns_.Tid__mediawiki_talk: return Xow_ns_.Key__mediawiki_talk;
case Xow_ns_.Tid__template: return Xow_ns_.Key__template;
case Xow_ns_.Tid__template_talk: return Xow_ns_.Key__template_talk;
case Xow_ns_.Tid__help: return Xow_ns_.Key__help;
case Xow_ns_.Tid__help_talk: return Xow_ns_.Key__help_talk;
case Xow_ns_.Tid__category: return Xow_ns_.Key__category;
case Xow_ns_.Tid__category_talk: return Xow_ns_.Key__category_talk;
case Xow_ns_.Tid__module: return Xow_ns_.Key__module;
case Xow_ns_.Tid__module_talk: return Xow_ns_.Key__module_talk;
default: return String_.new_u8(ns.Name_ui());
}
}
public static byte[] To_canonical_or_local_as_bry(Xow_ns ns) {
switch (ns.Id()) {
case Xow_ns_.Tid__media: return Xow_ns_.Bry__media;
case Xow_ns_.Tid__special: return Xow_ns_.Bry__special;
case Xow_ns_.Tid__talk: return Xow_ns_.Bry__talk;
case Xow_ns_.Tid__user: return Xow_ns_.Bry__user;
case Xow_ns_.Tid__user_talk: return Xow_ns_.Bry__user_talk;
case Xow_ns_.Tid__project: return Xow_ns_.Bry__project;
case Xow_ns_.Tid__project_talk: return Xow_ns_.Bry__project_talk;
case Xow_ns_.Tid__file: return Xow_ns_.Bry__file;
case Xow_ns_.Tid__file_talk: return Xow_ns_.Bry__file_talk;
case Xow_ns_.Tid__mediawiki: return Xow_ns_.Bry__mediawiki;
case Xow_ns_.Tid__mediawiki_talk: return Xow_ns_.Bry__mediawiki_talk;
case Xow_ns_.Tid__template: return Xow_ns_.Bry__template;
case Xow_ns_.Tid__template_talk: return Xow_ns_.Bry__template_talk;
case Xow_ns_.Tid__help: return Xow_ns_.Bry__help;
case Xow_ns_.Tid__help_talk: return Xow_ns_.Bry__help_talk;
case Xow_ns_.Tid__category: return Xow_ns_.Bry__category;
case Xow_ns_.Tid__category_talk: return Xow_ns_.Bry__category_talk;
case Xow_ns_.Tid__module: return Xow_ns_.Bry__module;
case Xow_ns_.Tid__module_talk: return Xow_ns_.Bry__module_talk;
default: return ns.Name_ui();
}
}
public static byte[] To_canonical_or_local_as_bry_w_colon(Xow_ns ns) {
byte[] rv = null;
switch (ns.Id()) {
case Xow_ns_.Tid__media: rv = Xow_ns_.Bry__media; break;
case Xow_ns_.Tid__special: rv = Xow_ns_.Bry__special; break;
case Xow_ns_.Tid__talk: rv = Xow_ns_.Bry__talk; break;
case Xow_ns_.Tid__user: rv = Xow_ns_.Bry__user; break;
case Xow_ns_.Tid__user_talk: rv = Xow_ns_.Bry__user_talk; break;
case Xow_ns_.Tid__project: rv = Xow_ns_.Bry__project; break;
case Xow_ns_.Tid__project_talk: rv = Xow_ns_.Bry__project_talk; break;
case Xow_ns_.Tid__file: rv = Xow_ns_.Bry__file; break;
case Xow_ns_.Tid__file_talk: rv = Xow_ns_.Bry__file_talk; break;
case Xow_ns_.Tid__mediawiki: rv = Xow_ns_.Bry__mediawiki; break;
case Xow_ns_.Tid__mediawiki_talk: rv = Xow_ns_.Bry__mediawiki_talk; break;
case Xow_ns_.Tid__template: rv = Xow_ns_.Bry__template; break;
case Xow_ns_.Tid__template_talk: rv = Xow_ns_.Bry__template_talk; break;
case Xow_ns_.Tid__help: rv = Xow_ns_.Bry__help; break;
case Xow_ns_.Tid__help_talk: rv = Xow_ns_.Bry__help_talk; break;
case Xow_ns_.Tid__category: rv = Xow_ns_.Bry__category; break;
case Xow_ns_.Tid__category_talk: rv = Xow_ns_.Bry__category_talk; break;
case Xow_ns_.Tid__module: rv = Xow_ns_.Bry__module; break;
case Xow_ns_.Tid__module_talk: rv = Xow_ns_.Bry__module_talk; break;
default: return ns.Name_db_w_colon();
}
return Bry_.Add(rv, Byte_ascii.Colon_bry);
}
}

View File

@@ -13,21 +13,3 @@ 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.wikis.nss; import gplx.*; import gplx.xowa.*; import gplx.xowa.wikis.*;
public class Xow_ns_case_ {
public static final byte Tid__all = 0, Tid__1st = 1;
public static final String Key__all = "case-sensitive", Key__1st = "first-letter";
public static final byte[] Bry__all = Bry_.new_a7(Key__all), Bry__1st = Bry_.new_a7(Key__1st);
public static byte To_tid(String s) {
if (String_.Eq(s, Key__1st)) return Tid__1st;
else if (String_.Eq(s, Key__all)) return Tid__all;
else throw Err_.new_unhandled(s);
}
public static String To_str(byte tid) {
switch (tid) {
case Tid__all: return Key__all;
case Tid__1st: return Key__1st;
default: throw Err_.new_unhandled(tid);
}
}
}

View File

@@ -13,253 +13,3 @@ 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.wikis.nss; import gplx.*; import gplx.xowa.*; import gplx.xowa.wikis.*;
import gplx.core.primitives.*; import gplx.core.btries.*; import gplx.xowa.langs.cases.*;
import gplx.xowa.bldrs.installs.*;
import gplx.xowa.xtns.scribunto.*;
public class Xow_ns_mgr implements Gfo_invk, gplx.core.lists.ComparerAble {
private Ordered_hash id_hash = Ordered_hash_.New(); // hash for retrieval by id
private Hash_adp_bry name_hash; // hash for retrieval by name; note that ns names are case-insensitive "File:" == "fILe:"
private Hash_adp_bry tmpl_hash; // hash for retrieval by name; PERF for templates
private Ordered_hash aliases = Ordered_hash_.New(); // hash to store aliases; used to populate name_hash;
private final Int_obj_ref id_hash_ref = Int_obj_ref.New_zero();
public Xow_ns_mgr(Xol_case_mgr case_mgr) {
name_hash = Hash_adp_bry.ci_u8(case_mgr);
tmpl_hash = Hash_adp_bry.ci_u8(case_mgr);
}
public Xow_ns_mgr Clear() {
name_hash.Clear();
id_hash.Clear();
tmpl_hash.Clear();
for (int i = 0; i < ords_len; i++)
ords[i] = null;
ords_len = 0;
ns_count = 0;
ns_file = null;
return this;
}
public Btrie_slim_mgr Category_trie() {return category_trie;} private Btrie_slim_mgr category_trie;
public Xow_ns Ns_main() {return ns_main;} private Xow_ns ns_main;
public Xow_ns Ns_template() {return ns_template;} private Xow_ns ns_template;
public Xow_ns Ns_file() {return ns_file;} private Xow_ns ns_file;
public Xow_ns Ns_category() {return ns_category;} private Xow_ns ns_category;
public Xow_ns Ns_portal() {return ns_portal;} private Xow_ns ns_portal;
public Xow_ns Ns_project() {return ns_project;} private Xow_ns ns_project;
public Xow_ns Ns_module() {return ns_module;} private Xow_ns ns_module;
public Xow_ns Ns_mediawiki() {return ns_mediawiki;} private Xow_ns ns_mediawiki;
public int Ns_page_id() {return ns_page_id;} public void Ns_page_id_(int v) {ns_page_id = v;} private int ns_page_id = Int_.Min_value;
public int Count() {return ns_count;} private int ns_count = 0;
public Xow_ns[] Ords_ary() {return ords;} private Xow_ns[] ords = new Xow_ns[Xow_ns_mgr_.Ordinal_max];
public int Ords_len() {return ords_len;} private int ords_len;
public Xow_ns Ords_get_at(int ord) {return ords[ord];}
public int Ids_len() {return id_hash.Count();}
public Xow_ns Ids_get_at(int idx) {return (Xow_ns)id_hash.Get_at(idx);}
public Xow_ns Ids_get_or_null(int id) {synchronized (id_hash_ref) {return (Xow_ns)id_hash.Get_by(id_hash_ref.Val_(id));}} // LOCK:hash-key; DATE:2016-07-06
private Xow_ns Ids_get_or_empty(int id) {
Xow_ns rv = Ids_get_or_null(id);
return rv == null ? Ns__empty : rv;
} private static final Xow_ns Ns__empty = new Xow_ns(Int_.Max_value, Byte_.Zero, Bry_.Empty, false);
public Xow_ns Names_get_or_null(byte[] name_bry) {return this.Names_get_or_null(name_bry, 0, name_bry.length);}
public Xow_ns Names_get_or_null(byte[] src, int bgn, int end) {
Object rv = name_hash.Get_by_mid(src, bgn, end);
return rv == null ? null : ((Xow_ns_mgr_name_itm)rv).Ns();
}
public Xow_ns Names_get_or_main(byte[] name_bry) {
Xow_ns rv = this.Names_get_or_null(name_bry, 0, name_bry.length);
return rv == null ? this.Ns_main() : rv;
}
public Object Names_get_w_colon(byte[] src, int bgn, int end) { // NOTE: get ns for a name with a ":"; EX: "Template:A" should return "Template" ns
int colon_pos = Bry_find_.Find_fwd(src, Byte_ascii.Colon, bgn, end);
if (colon_pos == Bry_find_.Not_found) return null; // name does not have ":"; return;
Object rv = name_hash.Get_by_mid(src, bgn, colon_pos);
return rv == null ? null : ((Xow_ns_mgr_name_itm)rv).Ns();
}
public int Tmpls_get_w_colon(byte[] src, int bgn, int end) { // NOTE: get length of template name with a ":"; EX: "Template:A" returns 10; PERF
int colon_pos = Bry_find_.Find_fwd(src, Byte_ascii.Colon, bgn, end);
if (colon_pos == Bry_find_.Not_found) return Bry_find_.Not_found;
Object o = tmpl_hash.Get_by_mid(src, bgn, colon_pos + 1); // +1 to include colon_pos
return o == null ? Bry_find_.Not_found : ((byte[])o).length;
}
public void Aliases_clear() {aliases.Clear();}
public Xow_ns_mgr Aliases_add(int ns_id, String name) {
Keyval kv = Keyval_.new_(name, new Int_obj_val(ns_id));
aliases.Add_if_dupe_use_nth(name, kv);
return this;
}
public void Aliases_del(String name) {aliases.Del(name);}
public Xow_ns_mgr Init() {
this.Ords_sort();
this.Rebuild_hashes();
return this;
}
public void Init_w_defaults() {
this.Add_defaults();
this.Init();
}
private void Rebuild_hashes() {
name_hash.Clear(); tmpl_hash.Clear();
for (int i = 0; i < ords_len; i++) {
Xow_ns ns = ords[i];
if (ns == null) continue; // TEST: allow gaps in ns numbers; see Talk_skip test and related
if (ns.Id() == Xow_ns_.Tid__project_talk) Fix_project_talk(ns); // NOTE: handle $1 talk as per Language.php!fixVariableInNamespace; placement is important as it must go before key registration but after ord sort
Rebuild_hashes__add(name_hash, ns, ns.Name_db());
if (ns.Id_is_tmpl()) tmpl_hash.Add(ns.Name_db_w_colon(), ns.Name_db_w_colon());
}
int aliases_len = aliases.Count();
for (int i = 0; i < aliases_len; i++) {
Keyval kv = (Keyval)aliases.Get_at(i);
int ns_id = ((Int_obj_val)kv.Val()).Val();
Xow_ns ns = Ids_get_or_null(ns_id); if (ns == null) continue; // happens when alias exists, but not ns; EX: test has Image alias, but not File alias; should not happen "live" but don't want to fail
ns.Aliases_add(kv.Key()); // register alias with official ns; EX: "Image" will be placed in "File"'s .Aliases
byte[] alias_bry = Bry_.new_u8(kv.Key());
Rebuild_hashes__add(name_hash, ns, alias_bry);
if (ns.Id_is_tmpl()) {
byte[] alias_name = Bry_.new_u8(kv.Key());
alias_name = Bry_.Add(alias_name, Byte_ascii.Colon);
tmpl_hash.Add_if_dupe_use_nth(alias_name, alias_name);
}
}
}
private void Fix_project_talk(Xow_ns ns) {
byte[] ns_name = ns.Name_db();
if (Bry_find_.Find_fwd(ns.Name_db(), Project_talk_fmt_arg)== Bry_find_.Not_found) return; // no $1 found; exit
Xow_ns project_ns = ords[ns.Ord_subj_id()];
if (project_ns == null) return; // should warn or throw error; for now just exit
ns.Name_bry_(Bry_.Replace(ns_name, Project_talk_fmt_arg, project_ns.Name_db()));
} private static final byte[] Project_talk_fmt_arg = Bry_.new_a7("$1");
private void Rebuild_hashes__add(Hash_adp_bry hash, Xow_ns ns, byte[] key) {
Xow_ns_mgr_name_itm ns_itm = new Xow_ns_mgr_name_itm(key, ns);
hash.Add_if_dupe_use_nth(key, ns_itm);
if (Bry_find_.Find_fwd(key, Byte_ascii.Underline) != Bry_find_.Not_found) // ns has _; add another entry for space; EX: Help_talk -> Help talk
hash.Add_if_dupe_use_nth(Bry_.Replace(key, Byte_ascii.Underline, Byte_ascii.Space), ns_itm);
}
public Xow_ns_mgr Add_defaults() { // NOTE: needs to happen after File ns is added; i.e.: cannot be put in Xow_ns_mgr() {} ctor
Aliases_add(Xow_ns_.Tid__file , "Image"); // REF.MW: Setup.php; add "Image", "Image talk" for backward compatibility; note that MW hardcodes Image ns as well
Aliases_add(Xow_ns_.Tid__file_talk , "Image_talk");
Aliases_add(Xow_ns_.Tid__project , "Project"); // always add "Project" ns (EX: Wikipedia is name for en.wikipedia.org; not sure if MW hardcodes, but it is in messages
Aliases_add(Xow_ns_.Tid__module , "Module"); // always add "Module" ns; de.wikipedia.org has "Modul" defined in siteinfo.xml, but also uses Module
return this;
}
public Xow_ns_mgr Add_new(int nsId, String name) {return Add_new(nsId, Bry_.new_u8(name), Xow_ns_case_.Tid__1st, false);} // for tst_ constructor
public Xow_ns_mgr Add_new(int ns_id, byte[] name, byte caseMatchId, boolean alias) {
Bry_.Replace_all_direct(name, Byte_ascii.Space, Byte_ascii.Underline); // standardize on _; EX: User talk -> User_talk; DATE:2013-04-21
Xow_ns ns = new Xow_ns(ns_id, caseMatchId, name, alias);
switch (ns_id) {
case Xow_ns_.Tid__main: ns_main = ns; break;
case Xow_ns_.Tid__template: ns_template = ns; break;
case Xow_ns_.Tid__portal: ns_portal = ns; break;
case Xow_ns_.Tid__project: ns_project = ns; break;
case Xow_ns_.Tid__mediawiki: ns_mediawiki = ns; break;
case Xow_ns_.Tid__module: ns_module = ns; break;
case Xow_ns_.Tid__file: if (ns_file == null) ns_file = ns; break; // NOTE: if needed, else Image will become the official ns_file
case Xow_ns_.Tid__category:
ns_category = ns;
if (category_trie == null)
category_trie = Btrie_slim_mgr.new_(ns.Case_match() == Xow_ns_case_.Tid__all);
category_trie.Add_obj(ns.Name_db(), this);
break;
}
++ns_count;
synchronized (id_hash_ref) { // LOCK:hash-key; DATE:2016-07-06
if (!id_hash.Has(id_hash_ref.Val_(ns_id))) // NOTE: do not add if already exists; avoids alias
id_hash.Add(Int_obj_ref.New(ns.Id()), ns);
}
name_hash.Add_if_dupe_use_nth(ns.Name_db(), new Xow_ns_mgr_name_itm(ns.Name_db(), ns));
return this;
}
public int compare(Object lhsObj, Object rhsObj) {
Xow_ns lhs = (Xow_ns)lhsObj;
Xow_ns rhs = (Xow_ns)rhsObj;
return Int_.Compare(lhs.Id(), rhs.Id());
}
private void Ords_sort() {
int ords_cur = 0;
int ns_len = id_hash.Count();
id_hash.Sort_by(this);
// assert that all items are grouped in pairs of subj, talk; note that subj is even and talk is odd
int nxt_ns_id = Int_.Min_value;
int prv_ns_id = Int_.Min_value;
for (int i = 0; i < ns_len; i++) {
Xow_ns ns = (Xow_ns)id_hash.Get_at(i);
int ns_id = ns.Id();
if (ns_id < 0 // ignore negative ns (which don't have subj/talk pairing)
|| ns.Is_alias() // ignore alias
) continue;
if (nxt_ns_id != Int_.Min_value) { // nxt_ns_id is set
if (nxt_ns_id != ns_id) // prv was subj, but cur does not match expected talk_id; create talk for prv subj
Ords_sort_add(nxt_ns_id);
nxt_ns_id = Int_.Min_value; // always reset value
}
if (ns_id % 2 == 0) // subj
nxt_ns_id = ns_id + 1; // anticipate nxt_ns_id
else { // talk
if (prv_ns_id != ns_id - 1) // prv was not subj for cur; create subj for current talk
Ords_sort_add(ns_id - 1);
}
prv_ns_id = ns_id;
}
if (nxt_ns_id != Int_.Min_value) // handle trailing ns_id; EX: 0, 1, 2; need to make 3
Ords_sort_add(nxt_ns_id);
// sort again b/c new ns may have been added
id_hash.Sort_by(this);
ns_len = id_hash.Count();
// assign ords; assert that subj has even ordinal index
ords_len = 0;
for (int i = 0; i < ns_len; i++) {
Xow_ns ns = (Xow_ns)id_hash.Get_at(i);
int ns_id = ns.Id();
if (ns.Is_alias()) continue; // ignore alias
if (ns_id < 0) {}
else {
if (ns_id % 2 == 0) { // subj
if (ords_cur % 2 != 0) { // current ordinal is not even; skip
++ords_len;
++ords_cur;
}
}
}
ns.Ord_(ords_cur);
ords[ords_cur++] = ns;
++ords_len;
}
}
private void Ords_sort_add(int ns_id) {
this.Add_new(ns_id, Int_.To_bry(ns_id), Xow_ns_case_.Tid__1st, false); // NOTE: name and case_match are mostly useless defaults; note that in theory this proc should not be called (all siteInfos should be well-formed) but just in case, create items now so that Get_by_ord() does not fail
}
public byte[] Bld_ttl_w_ns(Bry_bfr bfr, boolean text_form, boolean literalize, int ns_id, byte[] ttl) {
if (ns_id == Xow_ns_.Tid__main) return ttl;
Xow_ns ns = Ids_get_or_null(ns_id); if (ns == null) {Xoa_app_.Usr_dlg().Warn_many("", "", "ns_mgr:uknown ns_id; ns_id=~{0} ttl=~{1}", ns_id, ttl); return ttl;}
if (literalize) bfr.Add_byte(Byte_ascii.Colon); // NOTE: add : to literalize ns; EX: [[Category:A]] will get thrown into category list; [[:Category:A]] will print
bfr.Add(text_form ? ns.Name_ui_w_colon() : ns.Name_db_w_colon());
bfr.Add(ttl);
return bfr.To_bry_and_clear();
}
class Xow_ns_mgr_name_itm {
public Xow_ns_mgr_name_itm(byte[] name, Xow_ns ns) {this.name = name; this.name_len = name.length; this.ns = ns;}
public byte[] Name() {return name;} private byte[] name;
public int Name_len() {return name_len;} private int name_len;
public Xow_ns Ns() {return ns;} private Xow_ns ns;
}
public Object Invk(GfsCtx ctx, int ikey, String k, GfoMsg m) {
if (ctx.Match(k, Invk_clear)) this.Clear();
// NOTE: called by /xowa/bin/any/xowa/cfg/wiki/core/*.gfs for (a) aliases; (b) Subpages_enabled
else if (ctx.Match(k, Invk_add_alias_bulk)) Exec_add_alias_bulk(m.ReadBry("v"));
else if (ctx.Match(k, Invk_get_by_id_or_new)) return this.Ids_get_or_empty(m.ReadInt("v")); // NOTE: if ns doesn't exist, returning empty is fine; DATE:2014-02-15
else return Gfo_invk_.Rv_unhandled;
return this;
} private static final String Invk_add_alias_bulk = "add_alias_bulk", Invk_get_by_id_or_new = "get_by_id_or_new";
public static final String Invk_load = "load", Invk_clear = "clear";
private void Exec_add_alias_bulk(byte[] raw) {
byte[][] lines = Bry_split_.Split(raw, Byte_ascii.Nl);
int lines_len = lines.length;
for (int i = 0; i < lines_len; i++) {
byte[] line = lines[i];
if (line.length == 0) continue;
byte[][] flds = Bry_split_.Split(line, Byte_ascii.Pipe);
int cur_id = Bry_.To_int_or(flds[0], Int_.Min_value);
this.Aliases_add(cur_id, String_.new_u8(flds[1]));
}
Ords_sort();
}
}

View File

@@ -13,38 +13,3 @@ 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.wikis.nss; import gplx.*; import gplx.xowa.*; import gplx.xowa.wikis.*;
import gplx.xowa.langs.*; import gplx.xowa.langs.cases.*; import gplx.xowa.langs.bldrs.*;
public class Xow_ns_mgr_ {
public static final int Ordinal_max = 255; // ASSUME: no more than 255 ns in a wiki; choosing 255 to align with byte (no particular reason why it needs to be a byte, but better than 500, 1000, etc)
public static Xow_ns_mgr default_(Xol_case_mgr case_mgr) { // NOTE: same as en.wikipedia.org's ns circa 2012-01 (currently omitting ns:446,447,710,711)
Xow_ns_mgr rv = new Xow_ns_mgr(case_mgr);
rv = rv.Add_new(-2, "Media").Add_new(-1, "Special").Add_new(0, "").Add_new(1, "Talk").Add_new(2, "User").Add_new(3, "User_talk").Add_new(4, "Wikipedia").Add_new(5, "Wikipedia_talk")
.Add_new(6, "File").Add_new(7, "File_talk").Add_new(8, "MediaWiki").Add_new(9, "MediaWiki_talk").Add_new(10, "Template").Add_new(11, "Template_talk")
.Add_new(12, "Help").Add_new(13, "Help_talk").Add_new(14, "Category").Add_new(15, "Category_talk").Add_new(100, "Portal").Add_new(101, "Portal_talk").Add_new(108, "Book").Add_new(109, "Book_talk")
.Add_new(Xow_ns_.Tid__module, Xow_ns_.Key__module).Add_new(Xow_ns_.Tid__module_talk, Xow_ns_.Key__module_talk)
.Add_defaults()
;
rv.Init();
return rv;
}
public static void rebuild_(Xol_lang_itm lang, Xow_ns_mgr ns_mgr) {
Xol_ns_grp ns_names = lang.Ns_names();
int ns_names_len = ns_names.Len();
for (int i = 0; i < ns_names_len; i++) {
Xow_ns ns_name = ns_names.Get_at(i);
int ns_id = ns_name.Id();
Xow_ns ns = ns_mgr.Ids_get_or_null(ns_id);
if (ns == null) continue; // ns_id of -2 will not be found in site_ns
ns.Name_bry_(ns_name.Name_db());
}
ns_names = lang.Ns_aliases();
ns_names_len = ns_names.Len();
for (int i = 0; i < ns_names_len; i++) {
Xow_ns ns_name = ns_names.Get_at(i);
int ns_id = ns_name.Id();
ns_mgr.Aliases_add(ns_id, ns_name.Name_db_str());
}
ns_mgr.Init();
}
}

View File

@@ -13,65 +13,3 @@ 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.wikis.nss; import gplx.*; import gplx.xowa.*; import gplx.xowa.wikis.*;
import org.junit.*;
import gplx.xowa.langs.cases.*;
public class Xow_ns_mgr_tst {
@Before public void init() {fxt.Clear();} private Xow_ns_mgr_fxt fxt = new Xow_ns_mgr_fxt();
@Test public void Basic() {fxt.ini_ns_(-2, 0, 1).run_Ords_sort().tst_Ords(-2, -100, 0, 1);}
@Test public void Talk_skip() {fxt.ini_ns_(-2, 0, 2, 3).run_Ords_sort().tst_Ords(-2, -100, 0, 1, 2, 3);}
@Test public void Subj_skip() {fxt.ini_ns_(-2, 1, 2, 3).run_Ords_sort().tst_Ords(-2, -100, 0, 1, 2, 3);}
@Test public void Out_of_order() {fxt.ini_ns_(3, 1, 2, -2).run_Ords_sort().tst_Ords(-2, -100, 0, 1, 2, 3);}
@Test public void Skip_odd() {fxt.ini_ns_(-2, 1, 3).run_Ords_sort().tst_Ords(-2, -100, 0, 1, 2, 3);}
@Test public void Skip_even() {fxt.ini_ns_(-2, 2, 4).run_Ords_sort().tst_Ords(-2, -100, 2, 3, 4, 5);}
@Test public void Ns_alias() {
fxt.Ns_mgr().Aliases_clear();
fxt.Ns_mgr().Add_new(Xow_ns_.Tid__template, "Template");
fxt.Ns_mgr().Aliases_add(Xow_ns_.Tid__template, "Templatex");
fxt.Ns_mgr().Init();
byte[] name = Bry_.new_a7("Templatex:Abc");
Tfds.Eq(10, fxt.Ns_mgr().Tmpls_get_w_colon(name, 0, name.length));
}
@Test public void Utf8() {// PURPOSE: handle different casings for ns_names; PAGE:ru.w:Портрет_итальянского_Ренессанса DATE:2014-07-04
Xow_ns_mgr ns_mgr = new Xow_ns_mgr(Xol_case_mgr_.U8());
ns_mgr.Add_new(1234, "Test");
ns_mgr.Add_new(1235, "файл");
fxt.Ns_mgr_(ns_mgr);
fxt.Test_ns_name(1234, "Test", "test", "TEST", "tesT");
fxt.Test_ns_name(1235, "файл", "Файл");
}
}
class Xow_ns_mgr_fxt {
private Xow_ns_mgr ns_mgr = new Xow_ns_mgr(Xol_case_mgr_.A7());
public Xow_ns_mgr Ns_mgr() {return ns_mgr;}
public void Ns_mgr_(Xow_ns_mgr v) {this.ns_mgr = v;}
public void Clear() {ns_mgr.Clear();}
public Xow_ns_mgr_fxt ini_ns_(int... ids) {
int ids_len = ids.length;
for (int i = 0; i < ids_len; i++) {
int id = ids[i];
ns_mgr.Add_new(id, Int_.To_str(id));
}
return this;
}
public Xow_ns_mgr_fxt run_Ords_sort() {ns_mgr.Init(); return this;}
public Xow_ns_mgr_fxt tst_Ords(int... expd) {
int actl_len = ns_mgr.Ords_len();
int[] actl = new int[actl_len];
for (int i = 0; i < actl_len; i++) {
Xow_ns ns_itm = ns_mgr.Ords_ary()[i];
actl[i] = ns_itm == null ? -100 : ns_itm.Id();
}
Tfds.Eq_ary(expd, actl);
return this;
}
public void Test_ns_name(int expd_id, String... ns_names) {
int ns_names_len = ns_names.length;
for (int i = 0; i < ns_names_len; ++i) {
String ns_name = ns_names[i];
Xow_ns actl_ns = ns_mgr.Names_get_or_null(Bry_.new_u8(ns_name));
int actl_id = actl_ns == null ? Int_.Min_value : actl_ns.Id();
Tfds.Eq(expd_id, actl_id, ns_name);
}
}
}

View File

@@ -13,49 +13,3 @@ 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.wikis.nss; import gplx.*; import gplx.xowa.*; import gplx.xowa.wikis.*;
import org.junit.*;
public class Xow_ns_tst {
@Before public void init() {fxt.Clear();} private Xow_ns_fxt fxt = new Xow_ns_fxt();
@Test public void Category() {
fxt .Expd_id_subjId_(Xow_ns_.Tid__category)
.Expd_id_talkId_(Xow_ns_.Tid__category_talk)
.Expd_id_subj_(Bool_.Y)
.Expd_id_talk_(Bool_.N)
.Test(Xow_ns_.Tid__category)
;
}
@Test public void Category_talk() {
fxt .Expd_id_subjId_(Xow_ns_.Tid__category)
.Expd_id_talkId_(Xow_ns_.Tid__category_talk)
.Expd_id_subj_(Bool_.N)
.Expd_id_talk_(Bool_.Y)
.Test(Xow_ns_.Tid__category_talk)
;
}
@Test public void Special() {
fxt .Expd_id_subjId_(Xow_ns_.Tid__special)
.Expd_id_talkId_(Xow_ns_.Tid__special)
.Expd_id_subj_(Bool_.Y)
.Expd_id_talk_(Bool_.N)
.Test(Xow_ns_.Tid__special)
;
}
}
class Xow_ns_fxt {
public void Clear() {
expd_id_subjId = expd_id_talkId = Int_.Max_value;
expd_id_subj = expd_id_talk = false;
}
public Xow_ns_fxt Expd_id_subjId_(int v) {expd_id_subjId = v; return this;} private int expd_id_subjId;
public Xow_ns_fxt Expd_id_talkId_(int v) {expd_id_talkId = v; return this;} private int expd_id_talkId;
public Xow_ns_fxt Expd_id_subj_(boolean v) {expd_id_subj = v; return this;} private boolean expd_id_subj;
public Xow_ns_fxt Expd_id_talk_(boolean v) {expd_id_talk = v; return this;} private boolean expd_id_talk;
public void Test(int nsId) {
Xow_ns actl = new Xow_ns(nsId, Xow_ns_case_.Tid__1st, Bry_.Empty, false);
Tfds.Eq(expd_id_subjId, actl.Id_subj_id());
Tfds.Eq(expd_id_talkId, actl.Id_talk_id());
Tfds.Eq(expd_id_subj, actl.Id_is_subj());
Tfds.Eq(expd_id_talk, actl.Id_is_talk());
}
}