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

Source: Restore broken commit

This commit is contained in:
gnosygnu
2017-02-06 22:14:55 -05:00
parent 938beac9f9
commit 3bfeb94b43
4380 changed files with 328018 additions and 0 deletions

View File

@@ -0,0 +1,49 @@
/*
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.addons.wikis.pages.randoms; import gplx.*; import gplx.xowa.*; import gplx.xowa.addons.*; import gplx.xowa.addons.wikis.*; import gplx.xowa.addons.wikis.pages.*;
import gplx.xowa.bldrs.wkrs.*; import gplx.xowa.addons.wikis.pages.randoms.bldrs.*;
import gplx.xowa.specials.*; import gplx.xowa.addons.wikis.pages.randoms.specials.*;
import gplx.xowa.addons.wikis.pages.randoms.mgrs.*;
public class Rndm_addon implements Xoax_addon_itm, Xoax_addon_itm__bldr, Xoax_addon_itm__special {
public Rndm_addon() {this.mgr = null;} // prototype
public Rndm_addon(Xow_wiki wiki) {this.mgr = new Rndm_mgr(wiki);}
public Rndm_mgr Mgr() {return mgr;} private final Rndm_mgr mgr;
public Xob_cmd[] Bldr_cmds() {
return new Xob_cmd[]
{ Rndm_bldr_cmd.Prototype
};
}
public Xow_special_page[] Special_pages() {
return new Xow_special_page[]
{ Rndm_root_special.Prototype
, Rndm_page_special.Prototype
};
}
public static Rndm_addon Get(Xow_wiki wiki) {
Rndm_addon rv = (Rndm_addon)wiki.Addon_mgr().Itms__get_or_null(ADDON_KEY);
if (rv == null) {
rv = new Rndm_addon(wiki);
wiki.Addon_mgr().Itms__add(rv);
}
return rv;
}
public String Addon__key() {return ADDON_KEY;} private static final String ADDON_KEY = "xowa.builds.randoms";
}

View File

@@ -0,0 +1,37 @@
/*
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.addons.wikis.pages.randoms.bldrs; import gplx.*; import gplx.xowa.*; import gplx.xowa.addons.*; import gplx.xowa.addons.wikis.*; import gplx.xowa.addons.wikis.pages.*; import gplx.xowa.addons.wikis.pages.randoms.*;
import gplx.xowa.bldrs.*; import gplx.xowa.bldrs.wkrs.*;
public class Rndm_bldr_cmd extends Xob_cmd__base {
private int rndm_interval = 1000;
public Rndm_bldr_cmd(Xob_bldr bldr, Xowe_wiki wiki) {super(bldr, wiki);}
@Override public void Cmd_run() {
wiki.Init_assert();
new Rndm_ns_rebuilder().Exec(wiki, rndm_interval);
}
@Override public Object Invk(GfsCtx ctx, int ikey, String k, GfoMsg m) {
if (ctx.Match(k, Invk_rndm_interval_)) rndm_interval = m.ReadInt("v");
else return Gfo_invk_.Rv_unhandled;
return this;
} private static final String Invk_rndm_interval_ = "rndm_interval_";
public static final String BLDR_CMD_KEY = "wiki.random";
@Override public String Cmd_key() {return BLDR_CMD_KEY;}
public static final Xob_cmd Prototype = new Rndm_bldr_cmd(null, null);
@Override public Xob_cmd Cmd_clone(Xob_bldr bldr, Xowe_wiki wiki) {return new Rndm_bldr_cmd(bldr, wiki);}
}

View File

@@ -0,0 +1,72 @@
/*
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.addons.wikis.pages.randoms.bldrs; import gplx.*; import gplx.xowa.*; import gplx.xowa.addons.*; import gplx.xowa.addons.wikis.*; import gplx.xowa.addons.wikis.pages.*; import gplx.xowa.addons.wikis.pages.randoms.*;
import gplx.dbs.*; import gplx.xowa.addons.wikis.pages.randoms.dbs.*;
public class Rndm_bldr_wkr {
private final Rndm_qry_tbl qry_tbl; private final Rndm_rng_tbl rng_tbl; private final Rndm_seq_tbl seq_tbl;
private Rndm_qry_itm qry_itm;
private Db_stmt rng_stmt, seq_stmt;
private int rng_seq_bgn, seq_in_rng;
private int qry_idx_max = 0;
public Rndm_bldr_wkr(Db_conn conn, Rndm_qry_tbl qry_tbl, Rndm_rng_tbl rng_tbl, Rndm_seq_tbl seq_tbl) {
this.conn = conn;
this.qry_tbl = qry_tbl; this.rng_tbl = rng_tbl; this.seq_tbl = seq_tbl;
qry_idx_max = qry_tbl.Select_qry_max();
}
public Db_conn Conn() {return conn;} private final Db_conn conn;
public int Qry_idx() {return qry_idx;} private int qry_idx;
public int Rng_idx() {return rng_idx;} private int rng_idx;
public int Seq_in_qry() {return seq_in_qry;} private int seq_in_qry;
public void Exec_qry_bgn(Rndm_qry_itm qry_itm) {
this.qry_itm = qry_itm;
qry_idx = qry_tbl.Select_by_key(qry_itm.Qry_key());
if (qry_idx == -1)
qry_idx = ++qry_idx_max;
else {
// delete all
qry_tbl.Delete_by_qry_idx(qry_idx);
rng_tbl.Delete_by_qry_idx(qry_idx);
seq_tbl.Delete_by_qry_idx(qry_idx);
}
rng_idx = seq_in_rng = seq_in_qry = 0;
rng_stmt = rng_tbl.Insert_stmt();
seq_stmt = seq_tbl.Insert_stmt();
}
public void Exec_qry_end() {
if (seq_in_qry == 0) return; // no sequences added
qry_tbl.Insert(qry_idx, rng_idx, qry_itm.Qry_key(), qry_itm.Qry_data(), qry_itm.Qry_name());
}
public void Exec_rng_bgn() {
rng_seq_bgn = seq_in_qry;
++rng_idx;
seq_in_rng = 0;
}
public Rndm_rng_itm Exec_rng_end_or_null() {
if (seq_in_rng == 0) return null; // no sequences added; return null;
Rndm_rng_itm rv = new Rndm_rng_itm(qry_idx, rng_idx, rng_seq_bgn, seq_in_qry);
rng_tbl.Insert(rng_stmt, qry_idx, rng_idx, rng_seq_bgn, seq_in_qry);
return rv;
}
public void Exec_seq_itm(int page_id) {
seq_tbl.Insert(seq_stmt, qry_idx, rng_idx, seq_in_rng, page_id);
++seq_in_qry;
++seq_in_rng;
}
}

View File

@@ -0,0 +1,70 @@
/*
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.addons.wikis.pages.randoms.bldrs; import gplx.*; import gplx.xowa.*; import gplx.xowa.addons.*; import gplx.xowa.addons.wikis.*; import gplx.xowa.addons.wikis.pages.*; import gplx.xowa.addons.wikis.pages.randoms.*;
import gplx.dbs.*; import gplx.xowa.wikis.data.tbls.*;
import gplx.xowa.wikis.nss.*;
import gplx.xowa.addons.wikis.pages.randoms.dbs.*;
class Rndm_ns_rebuilder {
public void Exec(Xow_wiki wiki, int rndm_interval) {
// get wkr; page_tbl
Rndm_addon addon = Rndm_addon.Get(wiki);
Rndm_bldr_wkr wkr = addon.Mgr().New_bldr();
Xowd_page_tbl page_tbl = wiki.Data__core_mgr().Db__core().Tbl__page();
String fld_page_id = page_tbl.Fld_page_id();
Db_conn page_conn = page_tbl.Conn();
page_conn.Meta_idx_assert(page_tbl.Tbl_name(), "rndm_rebuild", page_tbl.Fld_page_ns(), fld_page_id);
// loop over ns
Xow_ns_mgr ns_mgr = wiki.Ns_mgr();
int len = ns_mgr.Ids_len();
wkr.Conn().Txn_bgn("rndm");
for (int i = 0; i < len; ++i) {
Xow_ns ns = ns_mgr.Ids_get_at(i);
Gfo_log_.Instance.Prog("reading ns", "ns", ns.Id());
int page_id_cur = -1;
wkr.Exec_qry_bgn(Rndm_qry_itm.New_by_ns(wiki, ns.Id()));
// read pages in ns where page_id > last_page_id
while (true) {
Gfo_log_.Instance.Prog("reading pages", "page_id", page_id_cur);
String sql = String_.Format("SELECT * FROM page WHERE page_namespace = {0} AND page_id > {1} ORDER BY page_id", ns.Id(), page_id_cur); // ANSI.Y
int rdr_count = 0;
wkr.Exec_rng_bgn();
Db_rdr rdr = page_conn.Stmt_sql(sql).Exec_select__rls_auto();
try {
// read pages until rndm_interval
while (rdr.Move_next()) {
int page_id = rdr.Read_int(fld_page_id);
wkr.Exec_seq_itm(page_id);
if (++rdr_count == rndm_interval) {
page_id_cur = page_id;
break;
}
}
}
finally {rdr.Rls();}
wkr.Exec_rng_end_or_null();
if (rdr_count != rndm_interval)
break;
}
wkr.Exec_qry_end();
}
wkr.Conn().Txn_end();
page_conn.Meta_idx_delete("page", "rndm_rebuild");
}
}

View File

@@ -0,0 +1,54 @@
/*
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.addons.wikis.pages.randoms.dbs; import gplx.*; import gplx.xowa.*; import gplx.xowa.addons.*; import gplx.xowa.addons.wikis.*; import gplx.xowa.addons.wikis.pages.*; import gplx.xowa.addons.wikis.pages.randoms.*;
import gplx.dbs.*;
import gplx.xowa.wikis.data.*;
public class Rndm_db_mgr {
private final Xow_wiki wiki;
public Rndm_db_mgr(Xow_wiki wiki) {
this.wiki = wiki;
}
public Db_conn Conn() {return conn;} private Db_conn conn;
public Rndm_qry_tbl Tbl__qry() {return tbl__qry;} private Rndm_qry_tbl tbl__qry;
public Rndm_rng_tbl Tbl__rng() {return tbl__rng;} private Rndm_rng_tbl tbl__rng;
public Rndm_seq_tbl Tbl__seq() {return tbl__seq;} private Rndm_seq_tbl tbl__seq;
public Rndm_db_mgr Init() {
this.conn = Get_or_new(wiki);
this.tbl__qry = new Rndm_qry_tbl(conn);
this.tbl__rng = new Rndm_rng_tbl(conn);
this.tbl__seq = new Rndm_seq_tbl(conn);
if (!conn.Meta_tbl_exists(tbl__qry.Tbl_name())) {
tbl__qry.Create_tbl();
tbl__rng.Create_tbl();
tbl__seq.Create_tbl();
}
return this;
}
private static Db_conn Get_or_new(Xow_wiki wiki) {
int layout_text = wiki.Data__core_mgr().Db__core().Db_props().Layout_text().Tid();
Io_url url = null;
switch (layout_text) {
case Xow_db_layout.Tid__all: url = wiki.Data__core_mgr().Db__core().Url(); break;
case Xow_db_layout.Tid__few: url = wiki.Fsys_mgr().Root_dir().GenSubFil(String_.Format("{0}-data.xowa", wiki.Domain_str())); break;
case Xow_db_layout.Tid__lot: url = wiki.Fsys_mgr().Root_dir().GenSubFil(String_.Format("{0}-xtn.random.core.xowa", wiki.Domain_str())); break;
default: throw Err_.new_unhandled(layout_text);
}
return Db_conn_bldr.Instance.Get_or_autocreate(true, url);
}
}

View File

@@ -0,0 +1,41 @@
/*
XOWA: the XOWA Offline Wiki Application
Copyright (C) 2012 gnosygnu@gmail.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package gplx.xowa.addons.wikis.pages.randoms.dbs; import gplx.*; import gplx.xowa.*; import gplx.xowa.addons.*; import gplx.xowa.addons.wikis.*; import gplx.xowa.addons.wikis.pages.*; import gplx.xowa.addons.wikis.pages.randoms.*;
public class Rndm_qry_itm {
public Rndm_qry_itm(int qry_idx, int rng_end, String qry_key, String qry_data, String qry_name) {
this.qry_idx = qry_idx;
this.rng_end = rng_end;
this.qry_key = qry_key;
this.qry_data = qry_data;
this.qry_name = qry_name;
}
public int Qry_idx() {return qry_idx;} private final int qry_idx;
public int Rng_end() {return rng_end;} private final int rng_end;
public String Qry_key() {return qry_key;} private final String qry_key;
public String Qry_data() {return qry_data;} private final String qry_data;
public String Qry_name() {return qry_name;} private final String qry_name;
public int Seq_max() {return seq_max;} private int seq_max; public void Seq_max_(int v) {this.seq_max = v;}
public static Rndm_qry_itm New_by_ns(Xow_wiki wiki, int ns_id) {
String ns_str = Int_.To_str(ns_id);
String qry_key = "xowa.ns." + ns_str; // xowa.ns.0
String qry_data = "type=ns;ns=" + ns_str; // type=ns;ns=0
String qry_name = "Namespace " + ns_str + " - All";
return new Rndm_qry_itm(-1, -1, qry_key, qry_data, qry_name);
}
}

View File

@@ -0,0 +1,56 @@
/*
XOWA: the XOWA Offline Wiki Application
Copyright (C) 2012 gnosygnu@gmail.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package gplx.xowa.addons.wikis.pages.randoms.dbs; import gplx.*; import gplx.xowa.*; import gplx.xowa.addons.*; import gplx.xowa.addons.wikis.*; import gplx.xowa.addons.wikis.pages.*; import gplx.xowa.addons.wikis.pages.randoms.*;
import gplx.core.ios.*; import gplx.dbs.*; import gplx.dbs.utls.*;
public class Rndm_qry_tbl implements Rls_able {
private final String tbl_name = "rndm_qry"; private final Dbmeta_fld_list flds = new Dbmeta_fld_list();
private final String fld_qry_idx, fld_rng_end, fld_qry_key, fld_qry_data, fld_qry_name;
private final Db_conn conn;
public Rndm_qry_tbl(Db_conn conn) {
this.conn = conn;
fld_qry_idx = flds.Add_int_pkey("qry_idx"); // EX: 0
fld_rng_end = flds.Add_int("rng_end"); // EX: 123
fld_qry_key = flds.Add_str("qry_key", 255); // EX: xowa.ns.0
fld_qry_data = flds.Add_str("qry_data", 255); // EX: type=ns;ns=0
fld_qry_name = flds.Add_str("qry_name", 255); // EX: Main Namespace - All
conn.Rls_reg(this);
}
public String Tbl_name() {return tbl_name;}
public void Create_tbl() {conn.Meta_tbl_create(Dbmeta_tbl_itm.New(tbl_name, flds));}
public int Select_rng_end(int qry_idx) {
Db_rdr rdr = conn.Stmt_select(tbl_name, flds, fld_qry_idx).Exec_select__rls_auto();
try {return rdr.Move_next() ? rdr.Read_int(fld_rng_end) : 0;}
finally {rdr.Rls();}
}
public int Select_qry_max() {
Db_rdr rdr = conn.Stmt_sql("SELECT Coalesce(Max(qry_idx), 0) AS qry_idx FROM rndm_qry").Exec_select__rls_auto(); // ANSI.Y
try {return rdr.Move_next() ? rdr.Read_int(fld_qry_idx) : 0;}
finally {rdr.Rls();}
}
public int Select_by_key(String key) {
Db_rdr rdr = conn.Stmt_select(tbl_name, flds, fld_qry_key).Crt_str(fld_qry_key, key).Exec_select__rls_auto();
try {return rdr.Move_next() ? rdr.Read_int(fld_qry_idx) : -1;}
finally {rdr.Rls();}
}
public void Insert(int qry_idx, int rng_end, String qry_key, String qry_data, String qry_name) {
conn.Stmt_insert(tbl_name, flds).Val_int(fld_qry_idx, qry_idx).Val_int(fld_rng_end, rng_end)
.Val_str(fld_qry_key, qry_key).Val_str(fld_qry_data, qry_data).Val_str(fld_qry_name, qry_name).Exec_insert();
}
public void Delete_by_qry_idx(int qry_idx) {conn.Stmt_delete(tbl_name, fld_qry_idx).Crt_int(fld_qry_idx, qry_idx).Exec_delete();}
public void Rls() {}
}

View File

@@ -0,0 +1,30 @@
/*
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.addons.wikis.pages.randoms.dbs; import gplx.*; import gplx.xowa.*; import gplx.xowa.addons.*; import gplx.xowa.addons.wikis.*; import gplx.xowa.addons.wikis.pages.*; import gplx.xowa.addons.wikis.pages.randoms.*;
public class Rndm_rng_itm {
public Rndm_rng_itm(int mgr_idx, int rng_idx, int seq_bgn, int seq_end) {
this.mgr_idx = mgr_idx; this.rng_idx = rng_idx;
this.seq_bgn = seq_bgn; this.seq_end = seq_end;
}
public int Mgr_idx() {return mgr_idx;} private final int mgr_idx;
public int Rng_idx() {return rng_idx;} private final int rng_idx;
public int Seq_bgn() {return seq_bgn;} private final int seq_bgn;
public int Seq_end() {return seq_end;} private final int seq_end;
public static Rndm_rng_itm Noop() {return new Rndm_rng_itm(-1, -1, 0, 0);}
}

View File

@@ -0,0 +1,55 @@
/*
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.addons.wikis.pages.randoms.dbs; import gplx.*; import gplx.xowa.*; import gplx.xowa.addons.*; import gplx.xowa.addons.wikis.*; import gplx.xowa.addons.wikis.pages.*; import gplx.xowa.addons.wikis.pages.randoms.*;
import gplx.core.ios.*; import gplx.dbs.*; import gplx.dbs.utls.*;
public class Rndm_rng_tbl implements Rls_able {
private final String tbl_name = "rndm_rng"; private final Dbmeta_fld_list flds = new Dbmeta_fld_list();
private final String fld_qry_idx, fld_rng_idx, fld_seq_bgn, fld_seq_end;
private final Db_conn conn;
public Rndm_rng_tbl(Db_conn conn) {
this.conn = conn;
fld_qry_idx = flds.Add_int("qry_idx");
fld_rng_idx = flds.Add_int("rng_idx");
fld_seq_bgn = flds.Add_int("seq_bgn");
fld_seq_end = flds.Add_int("seq_end");
conn.Rls_reg(this);
}
public void Create_tbl() {conn.Meta_tbl_create(Dbmeta_tbl_itm.New(tbl_name, flds));}
public void Create_idx() {conn.Meta_idx_create(tbl_name, "core", fld_qry_idx, fld_seq_bgn, fld_seq_end);}
public Rndm_rng_itm Select_by_rng_idx_or_noop(int qry_idx, int rng_idx) {
Db_rdr rdr = conn.Stmt_select(tbl_name, flds, fld_rng_idx).Crt_int(fld_rng_idx, rng_idx).Exec_select__rls_auto();
try {return Load_or_noop(rdr);}
finally {rdr.Rls();}
}
public Rndm_rng_itm Select_by_rndm_num_or_noop(int qry_idx, int rndm_num) {
Db_rdr rdr = conn.Stmt_sql(String_.Format("SELECT * FROM rndm_rng WHERE qry_idx = {0} AND seq_bgn <= {1} AND seq_end > {2}", qry_idx, rndm_num)).Exec_select__rls_auto(); // ANSI.Y
try {return Load_or_noop(rdr);}
finally {rdr.Rls();}
}
private Rndm_rng_itm Load_or_noop(Db_rdr rdr) {
return (rdr.Move_next())
? new Rndm_rng_itm(rdr.Read_int(fld_qry_idx), rdr.Read_int(fld_rng_idx), rdr.Read_int(fld_seq_bgn), rdr.Read_int(fld_seq_end))
: Rndm_rng_itm.Noop();
}
public Db_stmt Insert_stmt() {return conn.Stmt_insert(tbl_name, flds);}
public void Insert(Db_stmt stmt, int qry_idx, int rng_idx, int seq_bgn, int seq_end) {
stmt.Clear().Val_int(fld_qry_idx, qry_idx).Val_int(fld_rng_idx, rng_idx).Val_int(fld_seq_bgn, seq_bgn).Val_int(fld_seq_end, seq_end).Exec_insert();
}
public void Delete_by_qry_idx(int qry_idx) {conn.Stmt_delete(tbl_name, fld_qry_idx).Crt_int(fld_qry_idx, qry_idx).Exec_delete();}
public void Rls() {}
}

View File

@@ -0,0 +1,52 @@
/*
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.addons.wikis.pages.randoms.dbs; import gplx.*; import gplx.xowa.*; import gplx.xowa.addons.*; import gplx.xowa.addons.wikis.*; import gplx.xowa.addons.wikis.pages.*; import gplx.xowa.addons.wikis.pages.randoms.*;
import gplx.core.ios.*; import gplx.dbs.*; import gplx.dbs.utls.*;
public class Rndm_seq_tbl implements Rls_able { // list of page_ids w/ random_idx; EX: 0,123|1,23|2,31|...
private final String fld_qry_idx, fld_rng_idx, fld_seq_idx, fld_page_id;
private final Db_conn conn;
public Rndm_seq_tbl(Db_conn conn) {
this.conn = conn;
fld_qry_idx = flds.Add_int("qry_idx");
fld_rng_idx = flds.Add_int("rng_idx");
fld_seq_idx = flds.Add_int("seq_idx");
fld_page_id = flds.Add_int("page_id");
}
public Db_conn Conn() {return conn;}
public String Tbl_name() {return tbl_name;} private final String tbl_name = "rndm_seq";
public Dbmeta_fld_list Flds() {return flds;} private final Dbmeta_fld_list flds = new Dbmeta_fld_list();
public String Fld__qry_idx() {return fld_qry_idx;}
public String Fld__rng_idx() {return fld_rng_idx;}
public void Create_tbl() {conn.Meta_tbl_create(Dbmeta_tbl_itm.New(tbl_name, flds));}
public void Create_idx() {conn.Meta_idx_create(tbl_name, "core", fld_qry_idx, fld_rng_idx, fld_seq_idx);}
public int Select_or_neg_1(int qry_idx, int rng_idx, int seq_idx) {
Db_stmt stmt = conn.Stmt_select(tbl_name, flds, fld_qry_idx, fld_rng_idx, fld_seq_idx);
Db_rdr rdr = stmt.Clear().Crt_int(fld_qry_idx, qry_idx).Crt_int(fld_rng_idx, rng_idx).Val_int(fld_seq_idx, seq_idx).Exec_select__rls_auto();
try {return rdr.Move_next() ? rdr.Read_int(fld_page_id) : -1;}
catch (Exception e) {Gfo_usr_dlg_.Instance.Warn_many("", "", "failed to get seq_idx; url=~{0} qry_idx=~{1} rng_idx=~{2} seq_idx=~{3} err=~{4}", conn.Conn_info().Db_api(), qry_idx, rng_idx, seq_idx, Err_.Message_gplx_log(e)); return -1;}
finally {rdr.Rls();}
}
public Db_stmt Insert_stmt() {return conn.Stmt_insert(tbl_name, flds);}
public void Insert(Db_stmt stmt, int qry_idx, int rng_idx, int seq_idx, int page_id) {
stmt.Clear().Val_int(fld_qry_idx, qry_idx).Val_int(fld_rng_idx, rng_idx).Val_int(fld_seq_idx, seq_idx).Val_int(fld_page_id, page_id).Exec_insert();
}
public void Delete_by_qry_idx(int qry_idx) {conn.Stmt_delete(tbl_name, fld_qry_idx).Crt_int(fld_qry_idx, qry_idx).Exec_delete();}
public void Rls() {}
public static final int Db_row_size_fixed = 4 * 4;
}

View File

@@ -0,0 +1,41 @@
/*
XOWA: the XOWA Offline Wiki Application
Copyright (C) 2012 gnosygnu@gmail.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package gplx.xowa.addons.wikis.pages.randoms.mgrs; import gplx.*; import gplx.xowa.*; import gplx.xowa.addons.*; import gplx.xowa.addons.wikis.*; import gplx.xowa.addons.wikis.pages.*; import gplx.xowa.addons.wikis.pages.randoms.*;
import gplx.dbs.*; import gplx.xowa.wikis.data.tbls.*;
import gplx.xowa.addons.wikis.pages.randoms.dbs.*; import gplx.xowa.addons.wikis.pages.randoms.bldrs.*;
public class Rndm_mgr {
public Rndm_mgr(Xow_wiki wiki) {
this.db_mgr = new Rndm_db_mgr(wiki).Init();
}
public Rndm_db_mgr Db_mgr() {return db_mgr;} private Rndm_db_mgr db_mgr;
public int Get_rndm_page(int qry_idx) {
// 0|type:ns,ns_id:123,text:123|123
int rng_end = db_mgr.Tbl__qry().Select_rng_end(qry_idx);
Rndm_rng_itm rng_end_itm = db_mgr.Tbl__rng().Select_by_rng_idx_or_noop(qry_idx, rng_end);
int rndm_num = RandomAdp_.new_().Next(rng_end_itm.Seq_end());
Rndm_rng_itm rng_itm = db_mgr.Tbl__rng().Select_by_rndm_num_or_noop(qry_idx, rndm_num);
int seq_idx = rndm_num - rng_itm.Seq_bgn();
int page_id = db_mgr.Tbl__seq().Select_or_neg_1(qry_idx, rng_itm.Rng_idx(), seq_idx);
Gfo_log_.Instance.Info("get_random_page", "qry_idx", qry_idx, "rng_end", rng_end, "rndm_num", rndm_num, "rng_idx", rng_itm.Rng_idx(), "seq_idx", seq_idx, "page_id", page_id);
return page_id;
}
public Rndm_bldr_wkr New_bldr() {return new Rndm_bldr_wkr(db_mgr.Conn(), db_mgr.Tbl__qry(), db_mgr.Tbl__rng(), db_mgr.Tbl__seq());}
public static final int Qry_idx__main = 0; // all
}

View File

@@ -0,0 +1,29 @@
/*
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.addons.wikis.pages.randoms.mgrs; import gplx.*; import gplx.xowa.*; import gplx.xowa.addons.*; import gplx.xowa.addons.wikis.*; import gplx.xowa.addons.wikis.pages.*; import gplx.xowa.addons.wikis.pages.randoms.*;
import gplx.core.lists.hashs.*;
class Rndm_ns_mgr {
// private final Hash_adp__int hash = new Hash_adp__int();
// public Rndm_ns_itm Get_by_ns(int ns_id) {return (Rndm_ns_itm)hash.Get_by_or_null(ns_id);}
public void Add() { // rndm;
/*
for (mgr : rndm_mgr)
hash.Add(mgr,);
*/
}
}

View File

@@ -0,0 +1,35 @@
/*
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.addons.wikis.pages.randoms.specials; import gplx.*; import gplx.xowa.*; import gplx.xowa.addons.*; import gplx.xowa.addons.wikis.*; import gplx.xowa.addons.wikis.pages.*; import gplx.xowa.addons.wikis.pages.randoms.*;
import gplx.xowa.specials.*; import gplx.xowa.wikis.nss.*;
public class Rndm_page_special implements Xow_special_page {
public void Special__gen(Xow_wiki wikii, Xoa_page pagei, Xoa_url url, Xoa_ttl ttl) {
Xowe_wiki wiki = (Xowe_wiki)wikii; Xoae_page page = (Xoae_page)pagei;
Xow_ns ns = wiki.Ns_mgr().Names_get_or_main(ttl.Rest_txt());
// Rndm_addon.Get(wiki).Mgr().Get_rndm_page_by_ns(ns);
byte[] random_ttl_bry = wiki.Db_mgr().Load_mgr().Find_random_ttl(ns);
wiki.Data_mgr().Redirect(page, ns.Gen_ttl(random_ttl_bry));
}
public static final String SPECIAL_KEY = "Randompage"; // NOTE: needs to match lang.gfs
public static final byte[] Display_ttl = Bry_.new_a7("Random Page");
public Xow_special_meta Special__meta() {return new Xow_special_meta(Xow_special_meta_.Src__mw, SPECIAL_KEY, "random");}
public static final Xow_special_page Prototype = new Rndm_page_special();
public Xow_special_page Special__clone() {return this;}
}

View File

@@ -0,0 +1,35 @@
/*
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.addons.wikis.pages.randoms.specials; import gplx.*; import gplx.xowa.*; import gplx.xowa.addons.*; import gplx.xowa.addons.wikis.*; import gplx.xowa.addons.wikis.pages.*; import gplx.xowa.addons.wikis.pages.randoms.*;
import gplx.xowa.wikis.nss.*; import gplx.xowa.specials.*;
public class Rndm_root_special implements Xow_special_page {
public void Special__gen(Xow_wiki wikii, Xoa_page pagei, Xoa_url url, Xoa_ttl ttl) {
Xowe_wiki wiki = (Xowe_wiki)wikii; Xoae_page page = (Xoae_page)pagei;
Xow_ns ns = wiki.Ns_mgr().Names_get_or_main(ttl.Rest_txt());
// Rndm_addon.Get(wiki).Mgr().Regy().Get_rndm_page_by_ns(ns);
byte[] random_ttl_bry = wiki.Db_mgr().Load_mgr().Find_random_ttl(ns);
byte[] root_bry = Xoa_ttl.Parse(wiki, random_ttl_bry).Root_txt();
wiki.Data_mgr().Redirect(page, ns.Gen_ttl(root_bry));
}
public static final String SPECIAL_KEY = "RandomRootPage";
public static final byte[] Display_ttl = Bry_.new_a7("Random Root Page");
public Xow_special_meta Special__meta() {return new Xow_special_meta(Xow_special_meta_.Src__mw, SPECIAL_KEY);}
public static final Xow_special_page Prototype = new Rndm_root_special();
public Xow_special_page Special__clone() {return this;}
}

View File

@@ -0,0 +1,56 @@
/*
XOWA: the XOWA Offline Wiki Application
Copyright (C) 2012 gnosygnu@gmail.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package gplx.xowa.addons.wikis.pages.randoms.specials; import gplx.*; import gplx.xowa.*; import gplx.xowa.addons.*; import gplx.xowa.addons.wikis.*; import gplx.xowa.addons.wikis.pages.*; import gplx.xowa.addons.wikis.pages.randoms.*;
import org.junit.*; import gplx.xowa.specials.*;
public class Rndm_root_special_tst {
@Before public void init() {fxt.Clear();} private Rndm_root_special_fxt fxt = new Rndm_root_special_fxt();
@Test public void Ns_main() {
fxt.Init_create_page("A");
fxt.Init_create_page("A/B/C");
fxt.Test_open("Special:RandomRootPage/Main", "A"); // NOTE: result will always be "A"; "A" -> "A"; "A/B/C" -> "A"
}
@Test public void Ns_help() {
fxt.Init_create_page("Help:A");
fxt.Init_create_page("Help:A/B/C");
fxt.Test_open("Special:RandomRootPage/Help", "Help:A");
}
}
class Rndm_root_special_fxt {
private Xop_fxt parser_fxt; private Rndm_root_special special_page; private Xowe_wiki wiki;
public void Clear() {
parser_fxt = new Xop_fxt();
parser_fxt.Reset();
wiki = parser_fxt.Wiki();
special_page = new gplx.xowa.addons.wikis.pages.randoms.specials.Rndm_root_special();
}
public void Init_create_page(String page) {parser_fxt.Init_page_create(page, page);}
public void Test_open(String special_url, String expd) {
Xoae_page page = Test_special_open(wiki, special_page, special_url);
Tfds.Eq(expd, String_.new_a7(page.Url().Page_bry()));
Tfds.Eq(expd, String_.new_a7(page.Db().Text().Text_bry()));
}
public static Xoae_page Test_special_open(Xowe_wiki wiki, Xow_special_page special_page, String special_url) {
Xoae_page page = wiki.Parser_mgr().Ctx().Page();
Xoa_url url = wiki.Utl__url_parser().Parse(Bry_.new_u8(special_url));
page.Url_(url);
Xoa_ttl ttl = Xoa_ttl.Parse(wiki, Bry_.new_a7(special_url));
page.Ttl_(ttl);
special_page.Special__gen(wiki, page, url, ttl);
return page;
}
}

View File

@@ -0,0 +1,39 @@
/*
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.addons.wikis.pages.syncs; import gplx.*; import gplx.xowa.*; import gplx.xowa.addons.*; import gplx.xowa.addons.wikis.*; import gplx.xowa.addons.wikis.pages.*;
import gplx.xowa.bldrs.wkrs.*;
import gplx.xowa.specials.*;
import gplx.xowa.addons.wikis.pages.syncs.specials.*;
public class Xosync_addon implements Xoax_addon_itm, Xoax_addon_itm__special {
public Xow_special_page[] Special_pages() {
return new Xow_special_page[]
{ Sync_html_special.Prototype
};
}
public static Xosync_addon Get(Xow_wiki wiki) {
Xosync_addon rv = (Xosync_addon)wiki.Addon_mgr().Itms__get_or_null(ADDON_KEY);
if (rv == null) {
rv = new Xosync_addon();
wiki.Addon_mgr().Itms__add(rv);
}
return rv;
}
public String Addon__key() {return ADDON_KEY;} private static final String ADDON_KEY = "xowa.pages.syncs";
}

View File

@@ -0,0 +1,80 @@
/*
XOWA: the XOWA Offline Wiki Application
Copyright (C) 2012 gnosygnu@gmail.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package gplx.xowa.addons.wikis.pages.syncs.core; import gplx.*; import gplx.xowa.*; import gplx.xowa.addons.*; import gplx.xowa.addons.wikis.*; import gplx.xowa.addons.wikis.pages.*; import gplx.xowa.addons.wikis.pages.syncs.*;
import gplx.dbs.*;
import gplx.xowa.htmls.*;
import gplx.xowa.addons.wikis.pages.syncs.dbs.*;
import gplx.xowa.apps.apis.xowa.addons.bldrs.*;
import gplx.xowa.wikis.data.tbls.*;
public class Xosync_read_mgr implements Gfo_invk {
private boolean auto_enabled = false;
private int auto_interval = 60 * 24; // in minutes
private final Xowd_page_itm tmp_dbpg = new Xowd_page_itm();
private Db_conn sync_conn; private Xosync_sync_tbl sync_tbl;
private final Xopg_match_mgr auto_page_matcher = new Xopg_match_mgr();
private final Xosync_update_mgr update_mgr = new Xosync_update_mgr();
public void Init_by_wiki(Xow_wiki wiki) {
this.Auto_scope_("*:Main_Page");
wiki.App().Cfg().Bind_many_wiki(this, wiki, Cfg__manual__enabled, Cfg__auto__enabled, Cfg__auto__interval, Cfg__auto__scope);
}
public boolean Manual_enabled() {return manual_enabled;} private boolean manual_enabled;
public void Auto_update(Xow_wiki wiki, Xoa_page page, Xoa_ttl page_ttl) {
if (wiki.Domain_itm().Domain_type_id() == gplx.xowa.wikis.domains.Xow_domain_tid_.Tid__home) return;
if (wiki.Domain_itm().Domain_type_id() == gplx.xowa.wikis.domains.Xow_domain_tid_.Tid__other) return;
if (page_ttl.Ns().Id_is_special()) return;
if (!auto_enabled) return;
if (!auto_page_matcher.Match(wiki, page_ttl.Full_db())) return;
wiki.Data__core_mgr().Db__core().Tbl__page().Select_by_ttl(tmp_dbpg, page_ttl.Ns(), page_ttl.Page_db());
if (sync_conn == null) {
Io_url sync_db_url = wiki.Fsys_mgr().Root_dir().GenSubFil(wiki.Domain_str() + "-sync.xowa");
sync_conn = Db_conn_bldr.Instance.Get_or_autocreate(true, sync_db_url);
sync_tbl = new Xosync_sync_tbl(sync_conn);
sync_conn.Meta_tbl_assert(sync_tbl);
}
DateAdp sync_date = sync_tbl.Select_sync_date_or_min(tmp_dbpg.Id());
if (Datetime_now.Get().Diff(sync_date).Total_mins().To_int() <= auto_interval) return;
Xoa_app app = wiki.App();
Xoh_page hpg = new Xoh_page();
update_mgr.Init_by_app(app);
update_mgr.Init_by_page(wiki, hpg);
update_mgr.Update(app.Wmf_mgr().Download_wkr(), wiki, page_ttl);
sync_tbl.Upsert(tmp_dbpg.Id(), Datetime_now.Get());
}
private void Auto_scope_(String v) {
auto_page_matcher.Set(v);
}
public Object Invk(GfsCtx ctx, int ikey, String k, GfoMsg m) {
if (ctx.Match(k, Cfg__manual__enabled)) this.manual_enabled = m.ReadYn("v");
else if (ctx.Match(k, Cfg__auto__enabled)) this.auto_enabled = m.ReadYn("v");
else if (ctx.Match(k, Cfg__auto__interval)) this.auto_interval = m.ReadInt("v");
else if (ctx.Match(k, Cfg__auto__scope)) Auto_scope_(m.ReadStr("v"));
else return Gfo_invk_.Rv_unhandled;
return this;
}
private static final String
Cfg__manual__enabled = "xowa.bldr.page_sync.manual.enabled"
, Cfg__auto__enabled = "xowa.bldr.page_sync.auto.enabled"
, Cfg__auto__interval = "xowa.bldr.page_sync.auto.interval"
, Cfg__auto__scope = "xowa.bldr.page_sync.auto.scope"
;
}

View File

@@ -0,0 +1,81 @@
/*
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.addons.wikis.pages.syncs.core; import gplx.*; import gplx.xowa.*; import gplx.xowa.addons.*; import gplx.xowa.addons.wikis.*; import gplx.xowa.addons.wikis.pages.*; import gplx.xowa.addons.wikis.pages.syncs.*;
import gplx.xowa.files.downloads.*;
import gplx.xowa.wikis.data.*;
import gplx.xowa.htmls.*; import gplx.langs.htmls.docs.*;
import gplx.xowa.htmls.core.wkrs.*; import gplx.xowa.htmls.core.wkrs.txts.*; import gplx.xowa.htmls.core.hzips.*;
import gplx.xowa.htmls.core.dbs.*;
import gplx.xowa.addons.wikis.pages.syncs.wmapis.*;
import gplx.xowa.addons.wikis.pages.syncs.core.parsers.*;
public class Xosync_update_mgr {
private final Xoh_hzip_bfr bfr = new Xoh_hzip_bfr(Io_mgr.Len_kb, Bool_.N, Byte_.Max_value_127);
private final Gfh_doc_parser hdoc_parser_mgr;
private final Xoh_hdoc_ctx hctx = new Xoh_hdoc_ctx();
private final Xosync_hdoc_wtr hdoc_bldr = new Xosync_hdoc_wtr();
private final Xosync_hdoc_parser hdoc_parser_wkr;
private final Xowd_html_tbl_mgr html_tbl_mgr = new Xowd_html_tbl_mgr();
public Xosync_update_mgr() {
hdoc_parser_wkr = new Xosync_hdoc_parser(hdoc_bldr);
hdoc_parser_mgr = new Gfh_doc_parser(new Xoh_txt_parser(hdoc_bldr), hdoc_parser_wkr);
}
public void Init_by_app(Xoa_app app) {
hctx.Init_by_app(app);
}
public void Init_by_page(Xow_wiki wiki, Xoa_page page) {
hctx.Init_by_page(wiki, page);
page.Hdump_mgr().Clear();
}
public void Update(Xof_download_wkr download_wkr, Xow_wiki wiki, Xoa_ttl page_ttl) {
Xoh_page hpg = (Xoh_page)hctx.Page();
// call wmf api
Xowm_parse_wmf parse_wkr = new Xowm_parse_wmf();
Xowm_parse_data data = parse_wkr.Get_parse_or_null(download_wkr, wiki, page_ttl);
if (data == null) return;
// parse
Parse(hpg, wiki, hctx.Page__url(), data.Revn_html());
// get existing html_tbl
Xow_db_file html_db = html_tbl_mgr.Get_html_db(wiki);
html_tbl_mgr.Save_html(wiki, html_db, data.Page_id(), data.Revn_id(), hpg.Db().Html().Html_bry());
// download files
hpg.Ctor_by_hview(wiki, wiki.Utl__url_parser().Parse(page_ttl.Full_db()), page_ttl, data.Page_id());
gplx.xowa.files.Xof_file_wkr file_thread = new gplx.xowa.files.Xof_file_wkr
( wiki.File__orig_mgr(), wiki.File__bin_mgr(), wiki.File__mnt_mgr()
, wiki.App().User().User_db_mgr().Cache_mgr(), wiki.File__repo_mgr(), gplx.xowa.guis.cbks.js.Xog_js_wkr_.Noop, hpg, hpg.Hdump_mgr().Imgs()
);
gplx.core.threads.Gfo_thread_pool thread_pool = new gplx.core.threads.Gfo_thread_pool();
thread_pool.Add_at_end(file_thread);
thread_pool.Run();
}
public void Parse(Xoh_page hpg, Xow_wiki wiki, byte[] page_url, byte[] src) {
int src_len = src.length;
// init_by_page for bldr, parser, hdoc
hctx.Init_by_page(wiki, hpg);
hdoc_bldr.Init_by_page(bfr, hpg, hctx, src, 0, src_len);
hdoc_parser_wkr.Init_by_page(hctx, src, 0, src_len);
// parse
hdoc_parser_mgr.Parse(page_url, src, 0, src_len);
hpg.Db().Html().Html_bry_(bfr.To_bry_and_clear());
}
}

View File

@@ -0,0 +1,111 @@
/*
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.addons.wikis.pages.syncs.core.loaders; import gplx.*; import gplx.xowa.*; import gplx.xowa.addons.*; import gplx.xowa.addons.wikis.*; import gplx.xowa.addons.wikis.pages.*; import gplx.xowa.addons.wikis.pages.syncs.*; import gplx.xowa.addons.wikis.pages.syncs.core.*;
import gplx.core.brys.*; import gplx.core.btries.*;
import gplx.langs.htmls.*; import gplx.xowa.htmls.*; import gplx.langs.htmls.docs.*; import gplx.xowa.htmls.core.wkrs.*; import gplx.xowa.htmls.core.wkrs.imgs.atrs.*;
import gplx.xowa.files.*; import gplx.xowa.files.repos.*; import gplx.xowa.files.imgs.*;
import gplx.xowa.wikis.domains.*;
import gplx.xowa.addons.wikis.pages.syncs.core.parsers.*;
public class Xosync_page_loader {
private final Xoh_hdoc_ctx hctx = new Xoh_hdoc_ctx();
private final Gfh_tag_rdr tag_rdr = Gfh_tag_rdr.New__html();
private final Bry_err_wkr err_wkr = new Bry_err_wkr();
private final Xoh_img_src_data img_src_parser = new Xoh_img_src_data();
private final Bry_bfr tmp_bfr = Bry_bfr_.New();
public byte[] Parse(Xow_wiki wiki, Xoh_page hpg, byte[] src) {
// init hctx, tag_rdr, err_wkr
int src_len = src.length;
hctx.Init_by_page(wiki, hpg);
tag_rdr.Init(hpg.Url_bry_safe(), src, 0, src_len);
err_wkr.Init_by_page(String_.new_u8(hpg.Url_bry_safe()), src);
// loop for all <img>
int pos = 0;
Btrie_rv trv = new Btrie_rv();
while (true) {
// get next "<img>"
Gfh_tag img_tag = tag_rdr.Tag__find_fwd_head(pos, src_len, Gfh_tag_.Id__img);
// none found; add and exit
if (img_tag.Name_id() == Gfh_tag_.Id__eos) {
tmp_bfr.Add_mid(src, pos, src_len); // add bytes between img_end and prv_pos
break;
}
// add bytes between prv_pos and img_bgn
tmp_bfr.Add_mid(src, pos, img_tag.Src_bgn());
// do simple replace for @src
Gfh_atr img_src_atr = img_tag.Atrs__get_by_or_fail(Gfh_atr_.Bry__src);
byte[] img_src_val = img_src_atr.Val();
byte path_tid = Xosync_img_src_parser.Src_xo_trie.Match_byte_or(trv, img_src_val, Xosync_img_src_parser.Path__unknown);
switch (path_tid) {
case Xosync_img_src_parser.Path__file:
Add_img(wiki, hpg, img_tag, img_src_atr, img_src_val, path_tid, Xosync_img_src_parser.Bry__xowa_file, wiki.App().Fsys_mgr().File_dir().To_http_file_bry());
break;
case Xosync_img_src_parser.Path__math:
Add_img(wiki, hpg, img_tag, img_src_atr, img_src_val, path_tid, Xosync_img_src_parser.Bry__xowa_math, wiki.App().Fsys_mgr().File_dir().GenSubDir_nest("math").To_http_file_bry());
break;
}
pos = img_tag.Src_end();
}
// overwrite html
src = tmp_bfr.To_bry_and_clear();
hpg.Db().Html().Html_bry_(src);
return src;
}
private Xof_fsdb_itm Add_img(Xow_wiki wiki, Xoh_page hpg, Gfh_tag img_tag, Gfh_atr img_src_atr, byte[] img_src_val, byte path_tid, byte[] src_find, byte[] src_repl) {
// replace "xowa:/file" with "file:////xowa/file/"
img_src_val = Bry_.Replace(img_src_val, src_find, src_repl);
// parse src
img_src_parser.Parse(err_wkr, hctx, wiki.Domain_bry(), img_src_atr.Val_bgn(), img_src_atr.Val_end());
if (img_src_parser.File_ttl_bry() == null) return null; // skip images that don't follow format of "commons.wikimedia.org/thumb/7/70/A.png"; for example, enlarge buttons
// create img
Xof_fsdb_itm img = hpg.Img_mgr().Make_img(false);
// use repo_tid to get fsys_root, orig_repo_name
byte repo_tid = img_src_parser.Repo_tid();
byte[] orig_repo_name = null, fsys_root = null;
switch (repo_tid) {
case Xof_repo_tid_.Tid__remote: fsys_root = hctx.Fsys__file__comm(); orig_repo_name = Xow_domain_itm_.Bry__commons; break;
case Xof_repo_tid_.Tid__local: fsys_root = hctx.Fsys__file__wiki(); orig_repo_name = wiki.Domain_bry(); break;
case Xof_repo_tid_.Tid__math: fsys_root = hctx.Fsys__file__math(); orig_repo_name = Xof_repo_tid_.Bry__math; break;
}
// set vals
img.Orig_repo_name_(orig_repo_name);
byte[] file_ttl_bry = gplx.langs.htmls.encoders.Gfo_url_encoder_.Http_url.Decode(img_src_parser.File_ttl_bry());
Xof_ext file_ext = Xosync_img_src_parser.Ext_by_ttl(file_ttl_bry, repo_tid);
img.Init_by_wm_parse(hctx.Wiki__domain_itm().Abrv_xo(), img_src_parser.Repo_is_commons(), img_src_parser.File_is_orig(), file_ttl_bry, file_ext, img_src_parser.File_w(), img_src_parser.File_time(), img_src_parser.File_page());
// recalc src based on "file:////xowa/file/"
hctx.File__url_bldr().Init_by_repo(repo_tid, fsys_root, Bool_.N, Byte_ascii.Slash, Bool_.N, Bool_.N, 4);
hctx.File__url_bldr().Init_by_itm(img_src_parser.File_is_orig() ? Xof_img_mode_.Tid__orig : Xof_img_mode_.Tid__thumb, file_ttl_bry, Xof_file_wkr_.Md5(file_ttl_bry), Xof_ext_.new_by_ttl_(file_ttl_bry), img_src_parser.File_w(), img_src_parser.File_time(), img_src_parser.File_page());
Io_url html_view_url = hctx.File__url_bldr().Xto_url_by_http();
// if (path_tid == Xosync_img_src_parser.Path__file)
img.Init_at_gallery_end(img_tag.Atrs__get_as_int_or(Gfh_atr_.Bry__width,0), img_tag.Atrs__get_as_int_or(Gfh_atr_.Bry__height, 0), html_view_url, html_view_url);
Xosync_hdoc_parser.Write_img_tag(tmp_bfr, img_tag, img_src_val, img.Html_uid());
return img;
}
}

View File

@@ -0,0 +1,52 @@
/*
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.addons.wikis.pages.syncs.core.loaders; import gplx.*; import gplx.xowa.*; import gplx.xowa.addons.*; import gplx.xowa.addons.wikis.*; import gplx.xowa.addons.wikis.pages.*; import gplx.xowa.addons.wikis.pages.syncs.*; import gplx.xowa.addons.wikis.pages.syncs.core.*;
import gplx.core.tests.*;
import gplx.langs.htmls.*; import gplx.xowa.htmls.*;
import gplx.xowa.files.*; import gplx.xowa.files.repos.*;
import gplx.xowa.addons.wikis.pages.syncs.core.parsers.*;
public class Xosync_page_loader__fxt {
private final Xosync_page_loader mgr = new Xosync_page_loader();
private final Bry_bfr tmp_bfr = Bry_bfr_.New();
private final Xoh_page hpg = new Xoh_page();
private Xowe_wiki wiki;
public void Clear() {
Xoae_app app = Xoa_app_fxt.Make__app__edit();
this.wiki = Xoa_app_fxt.Make__wiki__edit(app);
hpg.Clear();
}
public Xosync_page_loader__fxt Exec__parse(String raw) {
mgr.Parse(wiki, hpg, Bry_.new_u8(raw));
return this;
}
public Xosync_page_loader__fxt Test__html(String expd) {
Gftest.Eq__ary__lines(expd, hpg.Db().Html().Html_bry(), "converted html");
return this;
}
public Xof_fsdb_itm Make__fsdb(boolean repo_is_commons, boolean file_is_orig, String file_ttl, String orig_ext_str, int file_w, double file_time, int file_page) {
Xof_fsdb_itm itm = new Xof_fsdb_itm();
Xof_ext orig_ext = Xof_ext_.new_by_ext_(Bry_.new_u8(orig_ext_str));
itm.Init_by_wm_parse(wiki.Domain_itm().Abrv_xo(), repo_is_commons, file_is_orig, Bry_.new_u8(file_ttl), orig_ext, file_w, file_time, file_page);
return itm;
}
public Xosync_page_loader__fxt Test__fsdb(Xof_fsdb_itm expd) {
Xof_fsdb_itm actl = (Xof_fsdb_itm)hpg.Img_mgr().Get_at(0);
Gftest.Eq__str(Xosync_hdoc_parser__fxt.To_str(tmp_bfr, expd), Xosync_hdoc_parser__fxt.To_str(tmp_bfr, actl));
return this;
}
}

View File

@@ -0,0 +1,41 @@
/*
XOWA: the XOWA Offline Wiki Application
Copyright (C) 2012 gnosygnu@gmail.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package gplx.xowa.addons.wikis.pages.syncs.core.loaders; import gplx.*; import gplx.xowa.*; import gplx.xowa.addons.*; import gplx.xowa.addons.wikis.*; import gplx.xowa.addons.wikis.pages.*; import gplx.xowa.addons.wikis.pages.syncs.*; import gplx.xowa.addons.wikis.pages.syncs.core.*;
import org.junit.*;
import gplx.langs.htmls.*;
public class Xosync_page_loader__tst {
@Before public void init() {fxt.Clear();} private final Xosync_page_loader__fxt fxt = new Xosync_page_loader__fxt();
@Test public void File() {
fxt.Exec__parse(Gfh_utl.Replace_apos("a<img src='xowa:/file/commons.wikimedia.org/thumb/4/a/6/9/Commons-logo.svg/12px.png' width='12' height='20'>b"))
.Test__html(Gfh_utl.Replace_apos("a<img id='xoimg_0' src='file:///mem/xowa/file/commons.wikimedia.org/thumb/4/a/6/9/Commons-logo.svg/12px.png' width='12' height='20'>b"))
.Test__fsdb(fxt.Make__fsdb(Bool_.Y, Bool_.N, "Commons-logo.svg", "svg", 12, -1, -1))
;
}
@Test public void Math() {
fxt.Exec__parse(Gfh_utl.Replace_apos("a<img src='xowa:/math/596f8baf206a81478afd4194b44138715dc1a05c' width='12' height='20'>b"))
.Test__html(Gfh_utl.Replace_apos("a<img id='xoimg_0' src='file:///mem/xowa/file/math/596f8baf206a81478afd4194b44138715dc1a05c' width='12' height='20'>b"))
.Test__fsdb(fxt.Make__fsdb(Bool_.Y, Bool_.Y, "596f8baf206a81478afd4194b44138715dc1a05c", "svg", -1, -1, -1))
;
}
@Test public void Ogg() {
fxt.Exec__parse(Gfh_utl.Replace_apos("a<img src='xowa:/file/commons.wikimedia.org/thumb/4/2/7/e/A.ogg/320px.jpg'>b"))
.Test__html(Gfh_utl.Replace_apos("a<img id='xoimg_0' src='file:///mem/xowa/file/commons.wikimedia.org/thumb/4/2/7/e/A.ogg/320px.jpg'>b"))
.Test__fsdb(fxt.Make__fsdb(Bool_.Y, Bool_.N, "A.ogg", "ogv", 320, -1, -1))
;
}
}

View File

@@ -0,0 +1,106 @@
/*
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.addons.wikis.pages.syncs.core.parsers; import gplx.*; import gplx.xowa.*; import gplx.xowa.addons.*; import gplx.xowa.addons.wikis.*; import gplx.xowa.addons.wikis.pages.*; import gplx.xowa.addons.wikis.pages.syncs.*; import gplx.xowa.addons.wikis.pages.syncs.core.*;
import gplx.langs.htmls.*; import gplx.langs.htmls.docs.*; import gplx.xowa.htmls.core.wkrs.*;
public class Xosync_hdoc_parser implements Gfh_doc_wkr {
private final Xosync_hdoc_wtr hdoc_wtr;
private final Gfh_tag_rdr tag_rdr = Gfh_tag_rdr.New__html();
private final Bry_bfr tmp_bfr = Bry_bfr_.New();
private final Xosync_img_src_parser img_src_parser = new Xosync_img_src_parser();
public Xosync_hdoc_parser(Xosync_hdoc_wtr hdoc_wtr) {this.hdoc_wtr = hdoc_wtr;}
public byte[] Hook() {return Byte_ascii.Angle_bgn_bry;}
public void Init_by_page(Xoh_hdoc_ctx hctx, byte[] src, int src_bgn, int src_end) {
tag_rdr.Init(hctx.Page__url(), src, src_bgn, src_end);
img_src_parser.Init_by_page(hctx);
}
public int Parse(byte[] src, int src_bgn, int src_end, int pos) {
// note that entry point is at "<"
tag_rdr.Pos_(pos);
int nxt_pos = tag_rdr.Pos() + 1; if (nxt_pos == src_end) return src_end;
// check if head or tail; EX: "<a>" vs "</a>"
byte nxt_byte = src[nxt_pos];
// skip comment; needed else comment may gobble up rest of text; see test; DATE:2016-09-10
if (nxt_byte == Byte_ascii.Bang) { // assume comment; EX:"<!--"
int end_comm = Bry_find_.Move_fwd(src, Gfh_tag_.Comm_end, nxt_pos);
if (end_comm == Bry_find_.Not_found) {
Gfo_usr_dlg_.Instance.Warn_many("", "", "end comment not found; src=~{0}", String_.new_u8(src));
end_comm = src_end;
}
return end_comm;
}
Gfh_tag cur = nxt_byte == Byte_ascii.Slash ? tag_rdr.Tag__move_fwd_tail(Gfh_tag_.Id__any) : tag_rdr.Tag__move_fwd_head();
if (cur.Tag_is_tail()) {}
else {
int cur_name_id = cur.Name_id();
switch (cur_name_id) {
case Gfh_tag_.Id__span:
if (cur.Atrs__cls_has(Bry__span__edit_section)) { // remove edit-section
tag_rdr.Tag__move_fwd_tail(cur_name_id);
return tag_rdr.Pos();
}
break;
case Gfh_tag_.Id__img: // rewrite src for XOWA; especially necessary for relative protocol; EX: "//upload.wikimedia.org"; note do not use <super> tag b/c of issues with anchors like "href=#section"
return Parse_img_src(cur);
default:
break;
}
}
hdoc_wtr.On_txt(cur.Src_bgn(), cur.Src_end());
return cur.Src_end();
}
private int Parse_img_src(Gfh_tag img_tag) {
// get @src and parse it
Gfh_atr src_atr = img_tag.Atrs__get_by_or_empty(Gfh_atr_.Bry__src);
img_src_parser.Parse(src_atr.Val());
// if error, write comment; EX: <!--error--><img ...>
String err_msg = img_src_parser.Err_msg();
if (err_msg != null) {
hdoc_wtr.Add_bry(Gfh_tag_.Comm_bgn);
hdoc_wtr.Add_str(img_src_parser.Err_msg());
hdoc_wtr.Add_bry(Gfh_tag_.Comm_end);
}
// get img_src; use img_src_parser if no error, else use original value
byte[] img_src_val = err_msg == null ? img_src_parser.To_bry() : src_atr.Val();
// write html
Write_img_tag(tmp_bfr, img_tag, img_src_val, -1);
hdoc_wtr.Add_bfr(tmp_bfr);
return img_tag.Src_end();
}
public static void Write_img_tag(Bry_bfr bfr, Gfh_tag img_tag, byte[] img_src_val, int uid) {
// rewrite <img> tag with custom img_src_val
int atrs_len = img_tag.Atrs__len();
bfr.Add(Byte_ascii.Angle_bgn_bry);
bfr.Add(Gfh_tag_.Bry__img);
if (uid != -1) {
Gfh_atr_.Add(bfr, Gfh_atr_.Bry__id, Bry_.new_a7("xoimg_" + Int_.To_str(uid)));
}
for (int i = 0; i < atrs_len; ++i) {
Gfh_atr atr = img_tag.Atrs__get_at(i);
// if atr is src use img_src_val; EX: ' src="//upload.wikimedia.org/..."' -> ' src="xowa:/file/..."
Gfh_atr_.Add(bfr, atr.Key(), Bry_.Eq(atr.Key(), Gfh_atr_.Bry__src) ? img_src_val : atr.Val());
}
bfr.Add(Byte_ascii.Angle_end_bry);
}
private static final byte[] Bry__span__edit_section = Bry_.new_a7("mw-editsection");
}

View File

@@ -0,0 +1,47 @@
/*
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.addons.wikis.pages.syncs.core.parsers; import gplx.*; import gplx.xowa.*; import gplx.xowa.addons.*; import gplx.xowa.addons.wikis.*; import gplx.xowa.addons.wikis.pages.*; import gplx.xowa.addons.wikis.pages.syncs.*; import gplx.xowa.addons.wikis.pages.syncs.core.*;
import org.junit.*;
import gplx.langs.htmls.*;
public class Xosync_hdoc_parser__err__tst {
@Before public void init() {fxt.Clear();} private final Xosync_hdoc_parser__fxt fxt = new Xosync_hdoc_parser__fxt();
@Test public void Url_does_not_start_with_upload_wikimedia_org() {
fxt.Exec__parse(Gfh_utl.Replace_apos("<img src='//fail/wikipedia/commons/thumb/7/70/A.png/220px-A.png'>"))
.Test__html(Gfh_utl.Replace_apos("<!--wm.parse:img src does not start with known sequence--><img src='//fail/wikipedia/commons/thumb/7/70/A.png/220px-A.png'>"));
}
@Test public void Unknown_repo() {
fxt.Exec__parse(Gfh_utl.Replace_apos("<img src='//upload.wikimedia.org/wiktionary/fr/thumb/7/70/A.png/220px-A.png'>"))
.Test__html(Gfh_utl.Replace_apos("<!--wm.parse:unknown repo--><img src='//upload.wikimedia.org/wiktionary/fr/thumb/7/70/A.png/220px-A.png'>"));
}
@Test public void Bad_md5() {
fxt.Exec__parse(Gfh_utl.Replace_apos("<img src='//upload.wikimedia.org/wikipedia/commons/thumb/fail/A.png/220px-A.png'>"))
.Test__html(Gfh_utl.Replace_apos("<!--wm.parse:invalid md5--><img src='//upload.wikimedia.org/wikipedia/commons/thumb/fail/A.png/220px-A.png'>"));
}
@Test public void Missing_px() {
fxt.Exec__parse(Gfh_utl.Replace_apos("<img src='//upload.wikimedia.org/wikipedia/commons/thumb/7/70/A.png/220-A.png'>"))
.Test__html(Gfh_utl.Replace_apos("<!--wm.parse:missing px--><img src='//upload.wikimedia.org/wikipedia/commons/thumb/7/70/A.png/220-A.png'>"));
}
@Test public void Bad_file_w() {
fxt.Exec__parse(Gfh_utl.Replace_apos("<img src='//upload.wikimedia.org/wikipedia/commons/thumb/7/70/A.png/220_fail_px-A.png'>"))
.Test__html(Gfh_utl.Replace_apos("<!--wm.parse:invalid file_w--><img src='//upload.wikimedia.org/wikipedia/commons/thumb/7/70/A.png/220_fail_px-A.png'>"));
}
@Test public void Comment() {
fxt.Exec__parse(Gfh_utl.Replace_apos("<i>a<!-- - --></i><b>b</b><i>c</i>"))
.Test__html(Gfh_utl.Replace_apos("<i>a</i><b>b</b><i>c</i>"));
}
}

View File

@@ -0,0 +1,58 @@
/*
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.addons.wikis.pages.syncs.core.parsers; import gplx.*; import gplx.xowa.*; import gplx.xowa.addons.*; import gplx.xowa.addons.wikis.*; import gplx.xowa.addons.wikis.pages.*; import gplx.xowa.addons.wikis.pages.syncs.*; import gplx.xowa.addons.wikis.pages.syncs.core.*;
import org.junit.*;
import gplx.langs.htmls.*;
public class Xosync_hdoc_parser__file__tst {
@Before public void init() {fxt.Clear();} private final Xosync_hdoc_parser__fxt fxt = new Xosync_hdoc_parser__fxt();
@Test public void Commons__thumb() {
fxt.Exec__parse(Gfh_utl.Replace_apos("<img src='//upload.wikimedia.org/wikipedia/commons/thumb/7/70/A.png/320px-A.png'>"))
.Test__html(Gfh_utl.Replace_apos("<img src='xowa:/file/commons.wikimedia.org/thumb/7/0/1/c/A.png/320px.png'>"))
.Test__fsdb(fxt.Make__fsdb(Bool_.Y, Bool_.N, "A.png", 320, -1, -1));
}
@Test public void Url_encoded() {
fxt.Exec__parse(Gfh_utl.Replace_apos("<img src='//upload.wikimedia.org/wikipedia/commons/thumb/9/91/A%2CB.png/320px-A%2CB.png'>"))
.Test__html(Gfh_utl.Replace_apos("<img src='xowa:/file/commons.wikimedia.org/thumb/9/1/0/8/A%2CB.png/320px.png'>"))
.Test__fsdb(fxt.Make__fsdb(Bool_.Y, Bool_.N, "A,B.png", 320, -1, -1));
}
@Test public void Local__orig() {
fxt.Exec__parse(Gfh_utl.Replace_apos("<img src='//upload.wikimedia.org/wikipedia/en/7/70/A.png'>"))
.Test__html(Gfh_utl.Replace_apos("<img src='xowa:/file/en.wikipedia.org/orig/7/0/1/c/A.png'>"))
.Test__fsdb(fxt.Make__fsdb(Bool_.N, Bool_.Y, "A.png", -1, -1, -1));
}
@Test public void Svg() {
fxt.Exec__parse(Gfh_utl.Replace_apos("<img src='//upload.wikimedia.org/wikipedia/commons/thumb/7/75/A.svg/12px-A.svg.png'>"))
.Test__html(Gfh_utl.Replace_apos("<img src='xowa:/file/commons.wikimedia.org/thumb/7/5/9/a/A.svg/12px.png'>"))
.Test__fsdb(fxt.Make__fsdb(Bool_.Y, Bool_.N, "A.svg", 12, -1, -1));
}
@Test public void Ogg() {
fxt.Exec__parse(Gfh_utl.Replace_apos("<img src='//upload.wikimedia.org/wikipedia/commons/thumb/4/42/A.ogg/320px--A.ogg.jpg'>"))
.Test__html(Gfh_utl.Replace_apos("<img src='xowa:/file/commons.wikimedia.org/thumb/4/2/7/e/A.ogg/320px.jpg'>"))
.Test__fsdb(fxt.Make__fsdb(Bool_.Y, Bool_.N, "A.ogg", "ogv", 320, -1, -1));
}
@Test public void Ogg__time() {
fxt.Exec__parse(Gfh_utl.Replace_apos("<img src='//upload.wikimedia.org/wikipedia/commons/thumb/4/42/A.ogg/320px-seek%3D1.2-A.ogg.jpg'>"))
.Test__html(Gfh_utl.Replace_apos("<img src='xowa:/file/commons.wikimedia.org/thumb/4/2/7/e/A.ogg/320px-1.2.jpg'>"))
.Test__fsdb(fxt.Make__fsdb(Bool_.Y, Bool_.N, "A.ogg", "ogv", 320, 1.2, -1));
}
@Test public void Pdf__page() {
fxt.Exec__parse(Gfh_utl.Replace_apos("<img src='//upload.wikimedia.org/wikipedia/commons/thumb/7/76/A.djvu/page1-320px-A.djvu.jpg'>"))
.Test__html(Gfh_utl.Replace_apos("<img src='xowa:/file/commons.wikimedia.org/thumb/7/6/9/a/A.djvu/320px-1.jpg'>"))
.Test__fsdb(fxt.Make__fsdb(Bool_.Y, Bool_.N, "A.djvu", 320, -1, 1));
}
}

View File

@@ -0,0 +1,71 @@
/*
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.addons.wikis.pages.syncs.core.parsers; import gplx.*; import gplx.xowa.*; import gplx.xowa.addons.*; import gplx.xowa.addons.wikis.*; import gplx.xowa.addons.wikis.pages.*; import gplx.xowa.addons.wikis.pages.syncs.*; import gplx.xowa.addons.wikis.pages.syncs.core.*;
import gplx.core.tests.*;
import gplx.langs.htmls.*; import gplx.xowa.htmls.*;
import gplx.xowa.files.*; import gplx.xowa.files.repos.*;
public class Xosync_hdoc_parser__fxt {
private final Xosync_update_mgr mgr = new Xosync_update_mgr();
private final Bry_bfr tmp_bfr = Bry_bfr_.New();
private final Xoh_page hpg = new Xoh_page();
private Xowe_wiki wiki;
public void Clear() {
Xoae_app app = Xoa_app_fxt.Make__app__edit();
this.wiki = Xoa_app_fxt.Make__wiki__edit(app);
Xoa_app_fxt.repo2_(app, wiki);
mgr.Init_by_app(app);
mgr.Init_by_page(wiki, hpg);
}
public Xosync_hdoc_parser__fxt Exec__parse(String raw) {
mgr.Parse(hpg, wiki, Bry_.Empty, Bry_.new_u8(raw));
return this;
}
public Xosync_hdoc_parser__fxt Test__html(String expd) {
Gftest.Eq__ary__lines(expd, hpg.Db().Html().Html_bry(), "converted html");
return this;
}
public Xof_fsdb_itm Make__fsdb(boolean repo_is_commons, boolean file_is_orig, String file_ttl, int file_w, double file_time, int file_page) {
return Make__fsdb(repo_is_commons, file_is_orig, file_ttl, Xof_ext_.new_by_ttl_(Bry_.new_u8(file_ttl)), file_w, file_time, file_page);
}
public Xof_fsdb_itm Make__fsdb(boolean repo_is_commons, boolean file_is_orig, String file_ttl, String file_ext, int file_w, double file_time, int file_page) {
return Make__fsdb(repo_is_commons, file_is_orig, file_ttl, Xof_ext_.new_by_ext_(Bry_.new_u8(file_ext)), file_w, file_time, file_page);
}
public Xof_fsdb_itm Make__fsdb(boolean repo_is_commons, boolean file_is_orig, String file_ttl, Xof_ext file_ext, int file_w, double file_time, int file_page) {
Xof_fsdb_itm itm = new Xof_fsdb_itm();
itm.Init_by_wm_parse(wiki.Domain_itm().Abrv_xo(), repo_is_commons, file_is_orig, Bry_.new_u8(file_ttl), file_ext, file_w, file_time, file_page);
return itm;
}
public Xosync_hdoc_parser__fxt Test__fsdb(Xof_fsdb_itm expd) {
Xof_fsdb_itm actl = (Xof_fsdb_itm)hpg.Hdump_mgr().Imgs().Get_at(0);
Gftest.Eq__str(To_str(tmp_bfr, expd), To_str(tmp_bfr, actl));
return this;
}
public static String To_str(Bry_bfr tmp_bfr, Xof_fsdb_itm itm) {
To_bfr(tmp_bfr, itm);
return tmp_bfr.To_str_and_clear();
}
private static void To_bfr(Bry_bfr bfr, Xof_fsdb_itm itm) {
bfr.Add_str_a7(itm.Orig_repo_id() == Xof_repo_tid_.Tid__remote ? "remote" : "local").Add_byte_pipe();
bfr.Add_str_a7(itm.File_is_orig() ? "orig" : "thumb").Add_byte_pipe();
bfr.Add(itm.Orig_ttl()).Add_byte_pipe();
bfr.Add(itm.Orig_ext().Ext()).Add_byte_pipe();
bfr.Add_int_variable(itm.File_w()).Add_byte_pipe();
bfr.Add_double(itm.Lnki_time()).Add_byte_pipe();
bfr.Add_int_variable(itm.Lnki_page()).Add_byte_pipe();
}
}

View File

@@ -0,0 +1,28 @@
/*
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.addons.wikis.pages.syncs.core.parsers; import gplx.*; import gplx.xowa.*; import gplx.xowa.addons.*; import gplx.xowa.addons.wikis.*; import gplx.xowa.addons.wikis.pages.*; import gplx.xowa.addons.wikis.pages.syncs.*; import gplx.xowa.addons.wikis.pages.syncs.core.*;
import org.junit.*;
import gplx.langs.htmls.*;
public class Xosync_hdoc_parser__misc__tst {
@Before public void init() {fxt.Clear();} private final Xosync_hdoc_parser__fxt fxt = new Xosync_hdoc_parser__fxt();
@Test public void Math() {
fxt.Exec__parse(Gfh_utl.Replace_apos("<img src='https://wikimedia.org/api/rest_v1/media/math/render/svg/596f8baf206a81478afd4194b44138715dc1a05c' class='mwe-math-fallback-image-inline' aria-hidden='true' style='vertical-align: -2.005ex; width:16.822ex; height:6.176ex;' alt='R_{H}=a\\left({\\frac {m}{3M}}\\right)^{\\frac {1}{3}}'>"))
.Test__html(Gfh_utl.Replace_apos("<img src='xowa:/math/596f8baf206a81478afd4194b44138715dc1a05c.svg' class='mwe-math-fallback-image-inline' aria-hidden='true' style='vertical-align: -2.005ex; width:16.822ex; height:6.176ex;' alt='R_{H}=a\\left({\\frac {m}{3M}}\\right)^{\\frac {1}{3}}'>"))
.Test__fsdb(fxt.Make__fsdb(Bool_.Y, Bool_.Y, "596f8baf206a81478afd4194b44138715dc1a05c.svg", -1, -1, -1));
}
}

View File

@@ -0,0 +1,41 @@
/*
XOWA: the XOWA Offline Wiki Application
Copyright (C) 2012 gnosygnu@gmail.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package gplx.xowa.addons.wikis.pages.syncs.core.parsers; import gplx.*; import gplx.xowa.*; import gplx.xowa.addons.*; import gplx.xowa.addons.wikis.*; import gplx.xowa.addons.wikis.pages.*; import gplx.xowa.addons.wikis.pages.syncs.*; import gplx.xowa.addons.wikis.pages.syncs.core.*;
import org.junit.*;
import gplx.langs.htmls.*;
public class Xosync_hdoc_parser__tst {
@Before public void init() {fxt.Clear();} private final Xosync_hdoc_parser__fxt fxt = new Xosync_hdoc_parser__fxt();
@Test public void Remove_edit() {
fxt.Exec__parse(Gfh_utl.Replace_apos_concat_lines
( "<h2><span class='mw-headline' id='Section_1'>Section_1</span>"
, "<span class='mw-editsection'>"
, "<span class='mw-editsection-bracket'>[</span><a href='/w/index.php?title=Page_1&amp;action=edit&amp;section=1' title='Edit section: Section_1'>edit</a>"
, "<span class='mw-editsection-bracket'>]</span>"
, "</span>"
, "</h2>"
)).Test__html(Gfh_utl.Replace_apos_concat_lines
( "<h2><span class='mw-headline' id='Section_1'>Section_1</span>"
, ""
, "</h2>"
));
}
// @Test public void Smoke() {
// fxt.Exec__parse(Io_mgr.Instance.LoadFilStr("C:\\xowa\\dev\\wm.updater.src.html"));
// Io_mgr.Instance.SaveFilBry("C:\\xowa\\dev\\wm.updater.trg.html", fxt.Hdoc().Converted());
// }
}

View File

@@ -0,0 +1,43 @@
/*
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.addons.wikis.pages.syncs.core.parsers; import gplx.*; import gplx.xowa.*; import gplx.xowa.addons.*; import gplx.xowa.addons.wikis.*; import gplx.xowa.addons.wikis.pages.*; import gplx.xowa.addons.wikis.pages.syncs.*; import gplx.xowa.addons.wikis.pages.syncs.core.*;
import gplx.xowa.htmls.*; import gplx.xowa.htmls.core.wkrs.*; import gplx.xowa.htmls.core.hzips.*;
public class Xosync_hdoc_wtr implements Xoh_hdoc_wkr {
private Xoh_hzip_bfr bfr;
private byte[] src;
public void Init_by_page(Xoh_hzip_bfr bfr, Xoh_page hpg, Xoh_hdoc_ctx hctx, byte[] src, int src_bgn, int src_end) {
this.On_new_page(bfr, hpg, hctx, src, src_bgn, src_end);
}
public void On_new_page(Xoh_hzip_bfr bfr, Xoh_page hpg, Xoh_hdoc_ctx hctx, byte[] src, int src_bgn, int src_end) {
this.bfr = bfr;
this.src = src;
}
public void On_txt (int rng_bgn, int rng_end) {bfr.Add_mid(src, rng_bgn, rng_end);}
public void Add_bfr (Bry_bfr v) {bfr.Add_bfr_and_clear(v);}
public void Add_str (String v) {bfr.Add_str_u8(v);}
public void Add_bry (byte[] v) {bfr.Add(v);}
// not used
public void On_escape (gplx.xowa.htmls.core.wkrs.escapes.Xoh_escape_data data) {}
public void On_xnde (gplx.xowa.htmls.core.wkrs.xndes.Xoh_xnde_parser parser) {}
public void On_lnki (gplx.xowa.htmls.core.wkrs.lnkis.Xoh_lnki_data parser) {}
public void On_thm (gplx.xowa.htmls.core.wkrs.thms.Xoh_thm_data parser) {}
public void On_gly (gplx.xowa.htmls.core.wkrs.glys.Xoh_gly_grp_data parser) {}
public boolean Process_parse(Xoh_data_itm data) {return false;}
}

View File

@@ -0,0 +1,234 @@
/*
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.addons.wikis.pages.syncs.core.parsers; import gplx.*; import gplx.xowa.*; import gplx.xowa.addons.*; import gplx.xowa.addons.wikis.*; import gplx.xowa.addons.wikis.pages.*; import gplx.xowa.addons.wikis.pages.syncs.*; import gplx.xowa.addons.wikis.pages.syncs.core.*;
import gplx.core.brys.*; import gplx.core.btries.*;
import gplx.xowa.files.*; import gplx.xowa.files.repos.*; import gplx.xowa.files.imgs.*;
import gplx.langs.htmls.*; import gplx.xowa.htmls.core.wkrs.*;
import gplx.xowa.wikis.domains.*;
public class Xosync_img_src_parser {
private final Bry_rdr rdr = new Bry_rdr().Dflt_dlm_(Byte_ascii.Slash);
private final Xof_url_bldr url_bldr = Xof_url_bldr.new_v2();
private final Bry_bfr tmp_bfr = Bry_bfr_.New();
private final byte[] wiki_abrv_commons;
private Xoh_hdoc_ctx hctx;
private byte path_tid;
private byte[] img_src_bgn_local, img_src_bgn_remote;
private byte[] page_url, repo_local;
private byte[] raw;
public boolean Repo_is_commons() {return repo_is_commons;} private boolean repo_is_commons;
public byte[] File_ttl_bry() {return file_ttl_bry;} private byte[] file_ttl_bry;
public boolean File_is_orig() {return file_is_orig;} private boolean file_is_orig;
public Xof_ext File_ext() {return file_ext;} private Xof_ext file_ext;
public int File_w() {return file_w;} private int file_w;
public double File_time() {return file_time;} private double file_time;
public int File_page() {return file_page;} private int file_page;
public String Err_msg() {return err_msg;} private String err_msg;
public Xosync_img_src_parser() {
rdr.Err_wkr().Fail_throws_err_(false);
img_src_bgn_remote = tmp_bfr.Add(Bry__xowa_file).Add(Xow_domain_itm_.Bry__commons).Add_byte_slash().To_bry_and_clear();
wiki_abrv_commons = Xow_abrv_xo_.To_bry(Xow_domain_itm_.Bry__commons);
}
public void Init_by_page(Xoh_hdoc_ctx hctx) {
this.hctx = hctx;
this.page_url = hctx.Page__url();
this.path_tid = Path__unknown;
this.repo_local = To_wmf_repo_or_null(tmp_bfr, hctx.Wiki__domain_itm());
if (repo_local == null) Gfo_usr_dlg_.Instance.Warn_many("", "", "unsupported wmf repo; domain=~{0}", hctx.Wiki__domain_itm().Domain_bry());
img_src_bgn_local = tmp_bfr.Add(Bry__xowa_file).Add(hctx.Wiki__domain_bry()).Add_byte_slash().To_bry_and_clear(); // EX: "xowa:/file/en.wikipedia.org/"
}
public boolean Parse(byte[] raw) {
// init
this.Clear();
// set raw, raw_len; exit if empty
this.raw = raw;
int raw_len = raw.length;
if (raw_len == 0) return Fail("empty img_src");
rdr.Init_by_src(raw);
// check "//upload.wikimedia.org/" at bgn
this.path_tid = rdr.Chk_or(path_trie, Path__unknown);
switch (path_tid) {
case Path__file: return Parse_file(raw_len);
case Path__math: return Parse_math(raw_len);
default: return Fail("img src does not start with known sequence");
}
}
private boolean Parse_file(int raw_len) {
// get repo: either "wikipedia/commons/" or "wiki_type/wiki_lang/"; EX:"wiktionary/fr"
if (rdr.Is(Bry__repo_remote))
this.repo_is_commons = true;
else {
if (!rdr.Is(repo_local)) return Fail("unknown repo");
}
// get file_is_orig; note omitting "else" b/c default is file_is_orig == false
if (!rdr.Is(Bry__thumb)) file_is_orig = true; // no "/thumb";
// check md5
if (!Check_md5()) return Fail("invalid md5");
// get file_ttl
int file_ttl_bgn = rdr.Pos();
int file_ttl_end = rdr.Find_fwd_lr_or(Byte_ascii.Slash, raw_len);
file_ttl_bry = Bry_.Mid(raw, file_ttl_bgn, file_ttl_end);
file_ttl_bry = gplx.langs.htmls.encoders.Gfo_url_encoder_.Http_url.Decode(file_ttl_bry); // NOTE: @src is always url-encoded; file_ttl_bry is un-encoded (for MD5, database lookups, etc.)
this.file_ext = Xof_ext_.new_by_ttl_(file_ttl_bry);
if (file_ext.Id_is_ogg()) file_ext = Xof_ext_.new_by_id_(Xof_ext_.Id_ogv);
// if thumb, get file_w, file_time, file_page
if (!file_is_orig) {
// if "page", then file_page exists; EX: // "page1-320px"
if (rdr.Is(Bry__page)) {
int file_page_bgn = rdr.Pos();
int file_page_end = rdr.Find_fwd_lr(Byte_ascii.Dash);
file_page = Bry_.To_int_or_fail(raw, file_page_bgn, file_page_end);
}
// get file_w; EX: "320px-"
int file_w_bgn = rdr.Pos();
int file_w_end = rdr.Find_fwd_lr(Bry__px);
if (file_w_end == -1) return Fail("missing px");
file_w = Bry_.To_int_or(raw, file_w_bgn, file_w_end, -1);
if (file_w == -1) return Fail("invalid file_w");
// get time via "-seek%3D"; EX: "320px-seek%3D67-"
int seek_end = rdr.Find_fwd_rr(Bry__seek);
if (seek_end != Bry_find_.Not_found) {
int file_time_bgn = rdr.Pos();
int file_time_end = rdr.Find_fwd_lr(Byte_ascii.Dash);
file_time = Bry_.To_double(raw, file_time_bgn, file_time_end);
}
}
// make image
Add_img(hctx.Wiki__domain_itm().Abrv_xo());
return true;
}
private boolean Parse_math(int raw_len) {
// set file_ttl_bry to rest of src + ".svg"; EX: "https://wikimedia.org/api/rest_v1/media/math/render/svg/596f8baf206a81478afd4194b44138715dc1a05c" -> "596f8baf206a81478afd4194b44138715dc1a05c.svg"
this.file_ttl_bry = Bry_.Add(Bry_.Mid(raw, rdr.Pos(), raw_len), Byte_ascii.Dot_bry, Xof_ext_.Bry_svg);
this.repo_is_commons = true;
this.file_is_orig = true;
this.file_ext = Xof_ext_.new_by_id_(Xof_ext_.Id_svg);
Add_img(wiki_abrv_commons);
return true;
}
private void Add_img(byte[] wiki_abrv) {
Xof_fsdb_itm itm = new Xof_fsdb_itm();
hctx.Page().Hdump_mgr().Imgs().Add(itm);
itm.Init_by_wm_parse(wiki_abrv, repo_is_commons, file_is_orig, file_ttl_bry, file_ext, file_w, file_time, file_page);
}
public byte[] To_bry() {
switch (path_tid) {
case Path__file: To_bfr_file(tmp_bfr); break;
case Path__math: To_bfr_math(tmp_bfr); break;
}
return tmp_bfr.To_bry_and_clear();
}
private void To_bfr_file(Bry_bfr bfr) { // EX:'xowa:/file/commons.wikimedia.org/thumb/7/0/1/c/A.png/220px.png'
// init repo; either "xowa:/file/commons.wikimedia.org" or "xowa:/file/en.wikipedia.org"
byte repo_tid = repo_is_commons ? Xof_repo_tid_.Tid__remote : Xof_repo_tid_.Tid__local;
byte[] fsys_root = repo_is_commons ? img_src_bgn_remote : img_src_bgn_local;
url_bldr.Init_by_repo(repo_tid, fsys_root, Bool_.N, Byte_ascii.Slash, Bool_.N, Bool_.N, 4);
// set other props and generate url;
url_bldr.Init_by_itm(file_is_orig ? Xof_img_mode_.Tid__orig : Xof_img_mode_.Tid__thumb, gplx.langs.htmls.encoders.Gfo_url_encoder_.Http_url.Encode(file_ttl_bry), Xof_file_wkr_.Md5(file_ttl_bry), Xof_ext_.new_by_ttl_(file_ttl_bry), file_w, file_time, file_page);
bfr.Add(url_bldr.Xto_bry());
}
private void To_bfr_math(Bry_bfr bfr) { // EX:'xowa:/math/596f8baf206a81478afd4194b44138715dc1a05c
bfr.Add(Bry__xowa_math).Add(file_ttl_bry);
}
private void Clear() {
this.file_ttl_bry = null;
this.repo_is_commons = false;
this.file_is_orig = false;
this.file_w = -1;
this.file_time = -1;
this.file_page = -1;
this.err_msg = null;
this.raw = null;
}
private boolean Fail(String fmt) {
this.err_msg = "wm.parse:" + fmt;
String msg = String_.Format("", err_msg + "; page={0} raw={1}", page_url, raw);
Gfo_usr_dlg_.Instance.Warn_many("", "", msg);
return false;
}
private boolean Check_md5() { // check if md5; also, skip past md5; EX: "a/a0/"
int pos = rdr.Pos();
if (!Byte_.Eq_many(Byte_ascii.Slash, raw[pos + 1], raw[pos + 4])) return false; // check slashes
byte b_0 = raw[pos + 0], b_2 = raw[pos + 2];
if (b_0 != b_2) return false; // WM repeats 1st MD5 char; EX: "a" in "a/a0"
if (!gplx.core.encoders.Hex_utl_.Is_hex_many(b_0, b_2, raw[pos + 3])) return false; // check rest is hex
rdr.Move_to(pos + 5);
return true;
}
private static final byte[]
Bry__repo_remote = Bry_.new_a7("wikipedia/commons/")
, Bry__thumb = Bry_.new_a7("thumb/")
, Bry__px = Bry_.new_a7("px")
, Bry__seek = Bry_.new_a7("-seek%3D")
, Bry__page = Bry_.new_a7("page")
;
public static final byte Path__unknown = 0, Path__file = 1, Path__math = 2;
private final Btrie_slim_mgr path_trie = Btrie_slim_mgr.cs()
.Add_str_byte("//upload.wikimedia.org/", Path__file)
.Add_str_byte("https://wikimedia.org/api/rest_v1/media/math/render/svg/", Path__math)
;
public static final byte[] Bry__xowa_file = Bry_.new_a7("xowa:/file/"), Bry__xowa_math = Bry_.new_a7("xowa:/math/");
public static Btrie_slim_mgr Src_xo_trie = Btrie_slim_mgr.cs()
.Add_bry_byte(Bry__xowa_file, Path__file)
.Add_bry_byte(Bry__xowa_math, Path__math)
;
private static byte[] To_wmf_repo_or_null(Bry_bfr bfr, Xow_domain_itm domain_itm) {
// add type; EX: "fr.wiktionary.org" -> "wiktionary/"
switch (domain_itm.Domain_type_id()) {
case Xow_domain_tid_.Tid__wikipedia:
case Xow_domain_tid_.Tid__wiktionary:
case Xow_domain_tid_.Tid__wikisource:
case Xow_domain_tid_.Tid__wikivoyage:
case Xow_domain_tid_.Tid__wikiquote:
case Xow_domain_tid_.Tid__wikibooks:
case Xow_domain_tid_.Tid__wikiversity:
case Xow_domain_tid_.Tid__wikinews:
bfr.Add(domain_itm.Domain_type().Key_bry()).Add_byte_slash();
break;
default:
return null;
}
// add lang; EX: "fr.wiktionary.org" -> "fr/"
bfr.Add(domain_itm.Lang_orig_key()).Add_byte_slash();
return bfr.To_bry_and_clear();
}
public static Xof_ext Ext_by_ttl(byte[] file_ttl_bry, byte repo_tid) {
Xof_ext rv = Xof_ext_.new_by_ttl_(file_ttl_bry);
if (rv.Id_is_ogg()) rv = Xof_ext_.new_by_id_(Xof_ext_.Id_ogv);
if (repo_tid == Xof_repo_tid_.Tid__math) rv = Xof_ext_.new_by_id_(Xof_ext_.Id_svg);
return rv;
}
}

View File

@@ -0,0 +1,38 @@
/*
XOWA: the XOWA Offline Wiki Application
Copyright (C) 2012 gnosygnu@gmail.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package gplx.xowa.addons.wikis.pages.syncs.core.parsers; import gplx.*; import gplx.xowa.*; import gplx.xowa.addons.*; import gplx.xowa.addons.wikis.*; import gplx.xowa.addons.wikis.pages.*; import gplx.xowa.addons.wikis.pages.syncs.*; import gplx.xowa.addons.wikis.pages.syncs.core.*;
import gplx.core.ios.streams.*;
import gplx.xowa.htmls.core.dbs.*; import gplx.xowa.htmls.core.hzips.*;
import gplx.xowa.wikis.data.*; import gplx.xowa.wikis.data.tbls.*; import gplx.xowa.wikis.pages.*;
public class Xowd_html_tbl_mgr {
public Xow_db_file Get_html_db(Xow_wiki wiki) {
return wiki.Data__core_mgr().Dbs__assert_by_tid(Xow_db_file_.Tid__html_user);
}
public void Save_html(Xow_wiki wiki, Xow_db_file db, int page_id, int revn_id, byte[] src) {
Xowd_html_tbl tbl = new Xowd_html_tbl(db.Conn());
// set other html props to null; TODO_FUTURE:need to parse html to get these
byte[] display_ttl = null;
byte[] content_sub = null;
byte[] sidebar_div = null;
db.Conn().Meta_tbl_assert(tbl);
tbl.Upsert(page_id, Xopg_module_mgr.Tid_null, Io_stream_tid_.Tid__raw, Xoh_hzip_dict_.Hzip__plain, display_ttl, content_sub, sidebar_div, src);
wiki.Data__core_mgr().Db__core().Tbl__page().Update__html_db_id(page_id, db.Id());
}
}

View File

@@ -0,0 +1,41 @@
/*
XOWA: the XOWA Offline Wiki Application
Copyright (C) 2012 gnosygnu@gmail.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package gplx.xowa.addons.wikis.pages.syncs.dbs; import gplx.*; import gplx.xowa.*; import gplx.xowa.addons.*; import gplx.xowa.addons.wikis.*; import gplx.xowa.addons.wikis.pages.*; import gplx.xowa.addons.wikis.pages.syncs.*;
import gplx.dbs.*; import gplx.dbs.utls.*;
public class Xosync_sync_tbl implements Db_tbl {
private final Dbmeta_fld_list flds = new Dbmeta_fld_list();
private final String fld_page_id, fld_sync_date;
private final Db_conn conn;
public Xosync_sync_tbl(Db_conn conn) {
this.conn = conn;
this.fld_page_id = flds.Add_int_pkey("page_id");
this.fld_sync_date = flds.Add_str("sync_date", 32);
conn.Rls_reg(this);
}
public String Tbl_name() {return tbl_name;} private final String tbl_name = "sync";
public void Create_tbl() {conn.Meta_tbl_create(Dbmeta_tbl_itm.New(tbl_name, flds));}
public DateAdp Select_sync_date_or_min(int page_id) {
Db_rdr rdr = conn.Stmt_select(tbl_name, flds, fld_page_id).Crt_int(fld_page_id, page_id).Exec_select__rls_auto();
try {return rdr.Move_next() ? rdr.Read_date_by_str(fld_sync_date) : DateAdp_.MinValue;}
finally {rdr.Rls();}
}
public void Upsert(int page_id, DateAdp sync_date) {
Db_tbl__crud_.Upsert(conn, tbl_name, flds, String_.Ary(fld_page_id), page_id, sync_date.XtoStr_fmt_yyyyMMdd_HHmmss());
}
public void Rls() {}
}

View File

@@ -0,0 +1,47 @@
/*
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.addons.wikis.pages.syncs.specials; import gplx.*; import gplx.xowa.*; import gplx.xowa.addons.*; import gplx.xowa.addons.wikis.*; import gplx.xowa.addons.wikis.pages.*; import gplx.xowa.addons.wikis.pages.syncs.*;
import gplx.core.net.qargs.*;
import gplx.xowa.specials.*; import gplx.xowa.wikis.nss.*;
import gplx.xowa.htmls.*;
import gplx.xowa.addons.wikis.pages.syncs.core.*;
public class Sync_html_special implements Xow_special_page {
public void Special__gen(Xow_wiki wiki, Xoa_page page, Xoa_url url, Xoa_ttl ttl) {
Gfo_qarg_mgr url_args = new Gfo_qarg_mgr().Init(url.Qargs_ary());
// get args
byte[] redirect_bry = url_args.Read_bry_or(Bry_.new_a7("page"), null);
Xoa_ttl redirect_ttl = wiki.Ttl_parse(redirect_bry);
// Xoa_url redirect_url = wiki.Utl__url_parser().Parse(redirect_bry);
// update
Xosync_update_mgr updater = new Xosync_update_mgr();
updater.Init_by_app(wiki.App());
Xoh_page hpg = new Xoh_page();
updater.Init_by_page(wiki, hpg);
updater.Update(wiki.App().Wmf_mgr().Download_wkr(), wiki, redirect_ttl);
((Xowe_wiki)wiki).Data_mgr().Redirect((Xoae_page)page, redirect_bry); // HACK: should call page.Redirect_trail() below, but need to handle Display_ttl
// page.Redirect_trail().Itms__add__article(redirect_url, redirect_ttl, null);
}
public static final String SPECIAL_KEY = "XowaSyncHtml"; // NOTE: needs to match lang.gfs
public static final byte[] Display_ttl = Bry_.new_a7("Sync HTML");
public Xow_special_meta Special__meta() {return new Xow_special_meta(Xow_special_meta_.Src__mw, SPECIAL_KEY);}
public static final Xow_special_page Prototype = new Sync_html_special();
public Xow_special_page Special__clone() {return this;}
}

View File

@@ -0,0 +1,38 @@
/*
XOWA: the XOWA Offline Wiki Application
Copyright (C) 2012 gnosygnu@gmail.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package gplx.xowa.addons.wikis.pages.syncs.wmapis; import gplx.*; import gplx.xowa.*; import gplx.xowa.addons.*; import gplx.xowa.addons.wikis.*; import gplx.xowa.addons.wikis.pages.*; import gplx.xowa.addons.wikis.pages.syncs.*;
public class Xowm_parse_data {
public Xowm_parse_data(byte[] wiki_domain
, int page_id, byte[] page_ttl, byte[] redirect_src
, int revn_id, byte[] revn_html) {
this.wiki_domain = wiki_domain;
this.page_id = page_id;
this.page_ttl = page_ttl;
this.redirect_src = redirect_src;
this.revn_id = revn_id;
this.revn_html = revn_html;
}
public byte[] Wiki_domain() {return wiki_domain;} private final byte[] wiki_domain;
public int Page_id() {return page_id;} private final int page_id;
public byte[] Page_ttl() {return page_ttl;} private final byte[] page_ttl;
public byte[] Redirect_src() {return redirect_src;} private final byte[] redirect_src;
public int Revn_id() {return revn_id;} private final int revn_id;
public byte[] Revn_html() {return revn_html;} private final byte[] revn_html;
}

View File

@@ -0,0 +1,55 @@
/*
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.addons.wikis.pages.syncs.wmapis; import gplx.*; import gplx.xowa.*; import gplx.xowa.addons.*; import gplx.xowa.addons.wikis.*; import gplx.xowa.addons.wikis.pages.*; import gplx.xowa.addons.wikis.pages.syncs.*;
import gplx.langs.jsons.*;
import gplx.xowa.files.downloads.*;
import gplx.xowa.apps.wms.apis.*;
public class Xowm_parse_wmf {
private final Bry_bfr bfr = Bry_bfr_.New();
private final Json_parser json_parser = new Json_parser();
public Xowm_parse_data Get_parse_or_null(Xof_download_wkr download_wkr, Xow_wiki wiki, Xoa_ttl page_ttl) {
if (!gplx.core.ios.IoEngine_system.Web_access_enabled) return null;
byte[] wiki_domain = wiki.Domain_bry();
byte[] page_full_db = page_ttl.Full_db();
byte[] json = Download(bfr, download_wkr, wiki_domain, page_full_db);
return Parse(json_parser, wiki_domain, page_full_db, json);
}
private static byte[] Download(Bry_bfr bfr, Xof_download_wkr download_wkr, byte[] wiki_domain, byte[] page_full_db) {
// build url; EX: "https://en.wikipedia.org/w/api.php?action=parse&format=json&redirects=1&page=Wikipedia:Main%20Page"
Xowm_api_bldr.Bld_bgn(bfr, wiki_domain);
bfr.Add_str_a7("action=parse&format=json&redirects=1&page="); // NOTE:redirects=1 needed to resolve redirects
bfr.Add(page_full_db);
// download
return download_wkr.Download_xrg().Exec_as_bry(bfr.To_str_and_clear());
}
private static Xowm_parse_data Parse(Json_parser json_parser, byte[] wiki_domain, byte[] page_full_db, byte[] json) {
Json_doc jdoc = json_parser.Parse(json);
// get data
Json_nde parse_nde = jdoc.Root_nde().Get_as_nde("parse");
if (parse_nde == null) return null; // handle pages that don't exist such as s.w:File:AnyFile.png; DATE:2016-11-15
int page_id = parse_nde.Get_as_int("pageid");
int revn_id = parse_nde.Get_as_int("revid");
byte[] page_ttl = Xoa_ttl.Replace_spaces(parse_nde.Get_as_bry("title"));
byte[] redirect_src = Bry_.Eq(page_ttl, page_full_db) ? null : page_full_db;
byte[] revn_html = parse_nde.Get_as_nde("text").Get_as_bry("*");
return new Xowm_parse_data(wiki_domain, page_id, page_ttl, redirect_src, revn_id, revn_html);
}
}