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

'v3.4.3.1'

This commit is contained in:
gnosygnu
2016-04-17 23:47:45 -04:00
parent 14471ca656
commit ad140a93fe
102 changed files with 1278 additions and 382 deletions

View File

@@ -1,25 +0,0 @@
/*
XOWA: the XOWA Offline Wiki Application
Copyright (C) 2012 gnosygnu@gmail.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package gplx.xowa.specials.randoms; import gplx.*; import gplx.xowa.*; import gplx.xowa.specials.*;
public class Rndm_core_row {
public void Load(int uid, String where_sql, int total, int interval) {this.uid = uid; this.where_sql = where_sql; this.total = total; this.interval = interval;}
public int uid;
public String where_sql;
public int total;
public int interval;
}

View File

@@ -1,52 +0,0 @@
/*
XOWA: the XOWA Offline Wiki Application
Copyright (C) 2012 gnosygnu@gmail.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package gplx.xowa.specials.randoms; import gplx.*; import gplx.xowa.*; import gplx.xowa.specials.*;
import gplx.core.ios.*; import gplx.dbs.*; import gplx.dbs.utls.*;
public class Rndm_core_tbl implements Rls_able {
private final String tbl_name = "rndm_core"; private final Dbmeta_fld_list flds = Dbmeta_fld_list.new_();
private final String fld_uid, fld_where_sql, fld_total, fld_interval;
private final Db_conn conn; private Db_stmt stmt_select;
public Rndm_core_tbl(Db_conn conn) {
this.conn = conn;
fld_uid = flds.Add_int_pkey("rndm_uid");
fld_where_sql = flds.Add_str("rndm_where_sql", 1024);
fld_total = flds.Add_int("rndm_total");
fld_interval = flds.Add_int("rndm_interval");
conn.Rls_reg(this);
}
public void Create_tbl() {conn.Meta_tbl_create(Dbmeta_tbl_itm.New(tbl_name, flds, Dbmeta_idx_itm.new_unique_by_name(tbl_name, "core", fld_uid)));}
public void Upsert(int uid, String where_sql, int total, int interval) {
conn.Stmt_delete(tbl_name, fld_uid).Crt_int(fld_uid, uid).Exec_delete();
conn.Stmt_insert(tbl_name, flds).Val_int(fld_uid, uid).Val_str(fld_where_sql, where_sql).Val_int(fld_total, total).Val_int(fld_interval, interval).Exec_insert();
}
public void Select(Rndm_core_row rv, int uid) {
if (stmt_select == null) stmt_select = conn.Stmt_select(tbl_name, flds, fld_uid);
Db_rdr rdr = stmt_select.Clear().Val_int(fld_uid, uid).Exec_select__rls_manual();
try {
if (rdr.Move_next()) {
String where_sql = rdr.Read_str(fld_where_sql);
int total = rdr.Read_int(fld_total);
int interval = rdr.Read_int(fld_interval);
rv.Load(uid, where_sql, total, interval);
}
} finally {rdr.Rls();}
}
public void Rls() {
stmt_select = Db_stmt_.Rls(stmt_select);
}
}

View File

@@ -1,90 +0,0 @@
/*
XOWA: the XOWA Offline Wiki Application
Copyright (C) 2012 gnosygnu@gmail.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package gplx.xowa.specials.randoms; import gplx.*; import gplx.xowa.*; import gplx.xowa.specials.*;
import gplx.core.primitives.*; import gplx.core.brys.fmtrs.*;
import gplx.dbs.*; import gplx.xowa.wikis.data.tbls.*;
public class Rndm_mgr implements Rls_able {
private Rndm_core_tbl core_tbl; private final Rndm_core_row core_row = new Rndm_core_row();
private Rndm_range_tbl rng_tbl;
private Xowd_page_tbl page_tbl; private Db_stmt stmt__page__random;
public void Init(Db_conn conn, Xowd_page_tbl page_tbl) {
core_tbl = new Rndm_core_tbl(conn);
rng_tbl = new Rndm_range_tbl(conn);
this.page_tbl = page_tbl;
conn.Rls_reg(this);
}
public void Rebuild(int uid, String where_sql, int interval) {
int total_count = 0; List_adp rng_list = List_adp_.new_();
Db_conn conn = page_tbl.conn;
Db_stmt stmt = conn.Stmt_select_order(page_tbl.Tbl_name(), String_.Ary(page_tbl.Fld_page_id()), String_.Ary_empty, page_tbl.Fld_page_id());
Db_rdr rdr = stmt.Exec_select__rls_auto();
try {
while (rdr.Move_next()) {
++total_count; // add to total count first so (total_count % interval) is never 0 for total_count = 0
if (total_count % interval == 0) {
int cur_page_id = rdr.Read_int(page_tbl.Fld_page_id());
rng_list.Add(Int_obj_ref.new_(cur_page_id));
}
}
}
finally {rdr.Rls();}
conn.Txn_bgn("rndm_rebuild");
core_tbl.Upsert(uid, where_sql, total_count, interval);
int rng_ary_len = rng_list.Count();
rng_tbl.Insert_bgn(uid);
for (int i = 0; i < rng_ary_len; ++i) {
Int_obj_ref rng_itm = (Int_obj_ref)rng_list.Get_at(i);
rng_tbl.Insert_itm(uid, i + 1, rng_itm.Val());
}
rng_tbl.Insert_end();
conn.Txn_end();
}
public int Get_rndm_page(int uid) { // EX: ns=0;type_is_redirect=0
synchronized (core_row) {core_tbl.Select(core_row, uid);}
int rndm_num = RandomAdp_.new_().Next(core_row.total);
int rng_idx = rndm_num / core_row.interval;
int itm_idx = rndm_num % core_row.interval;
int page_id_bgn = rng_idx == 0 ? 0 : rng_tbl.Select_or_neg_1(uid, rng_idx);
int rv = Select_by_offset(core_row.where_sql, page_id_bgn, itm_idx);
return rv;
}
private int Select_by_offset(String where_sql, int page_id_bgn, int offset) {
Bry_bfr bfr = Bry_bfr.new_();
where_sql = fmt_where.Fmt_(where_sql).Bld_many_to_str(bfr, page_tbl.Fld_page_ns(), page_tbl.Fld_is_redirect());
String sql = fmt_sql.Bld_many_to_str(bfr, page_tbl.Tbl_name(), page_tbl.Fld_page_id(), where_sql, page_id_bgn, offset);
Db_rdr rdr = Db_rdr_.Empty;
try {
rdr = page_tbl.conn.Exec_rdr(sql);
return rdr.Move_next() ? rdr.Read_int(page_tbl.Fld_page_id()) : -1;
}
finally {rdr.Rls();}
}
public void Rls() {
stmt__page__random = Db_stmt_.Rls(stmt__page__random);
}
private static final Bry_fmt fmt_sql = Bry_fmt.New(String_.Concat_lines_nl_skip_last
( "SELECT p.~{page_id}"
, "FROM ~{page} p"
, "WHERE p.~{~page_id} > ~{page_id_bgn}"
, "~{where_sql}"
, "ORDER BY ~{page_id}"
, "LIMIT 1"
, "OFFSET ~{offset};"
), "page", "page_id", "where_sql", "page_id_bgn", "offset");
private static final Bry_fmt fmt_where = Bry_fmt.New("", "page_namespace", "page_is_redirect");
}

View File

@@ -1,52 +0,0 @@
/*
XOWA: the XOWA Offline Wiki Application
Copyright (C) 2012 gnosygnu@gmail.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package gplx.xowa.specials.randoms; import gplx.*; import gplx.xowa.*; import gplx.xowa.specials.*;
import gplx.core.ios.*; import gplx.dbs.*; import gplx.dbs.utls.*;
public class Rndm_range_tbl implements Rls_able {
private final String tbl_name = "rng_range"; private final Dbmeta_fld_list flds = Dbmeta_fld_list.new_();
private final String fld_rng_uid, fld_rng_idx, fld_rng_page_id;
private final Db_conn conn; private Db_stmt stmt_select, stmt_insert;
public Rndm_range_tbl(Db_conn conn) {
this.conn = conn;
fld_rng_uid = flds.Add_int("rng_uid");
fld_rng_idx = flds.Add_int("rng_idx");
fld_rng_page_id = flds.Add_int("rng_page_id");
conn.Rls_reg(this);
}
public void Create_tbl() {conn.Meta_tbl_create(Dbmeta_tbl_itm.New(tbl_name, flds, Dbmeta_idx_itm.new_unique_by_name(tbl_name, "core", fld_rng_uid, fld_rng_idx)));}
public void Insert_bgn(int uid) {
conn.Stmt_delete(tbl_name, fld_rng_uid).Crt_int(fld_rng_uid, uid).Exec_delete();
stmt_insert = conn.Stmt_insert(tbl_name, flds);
}
public void Insert_itm(int uid, int idx, int page_id) {
stmt_insert.Clear().Val_int(fld_rng_uid, uid).Val_int(fld_rng_idx, idx).Val_int(fld_rng_page_id, page_id).Exec_insert();
}
public void Insert_end() {
stmt_insert = Db_stmt_.Rls(stmt_insert);
}
public int Select_or_neg_1(int core_uid, int rng_idx) {
if (stmt_select == null) stmt_select = conn.Stmt_select(tbl_name, flds, fld_rng_uid, fld_rng_idx);
Db_rdr rdr = stmt_select.Clear().Val_int(fld_rng_uid, core_uid).Val_int(fld_rng_idx, rng_idx).Exec_select__rls_manual();
try {return rdr.Move_next() ? rdr.Read_int(fld_rng_page_id) : -1;}
catch (Exception e) {Gfo_usr_dlg_.Instance.Warn_many("", "", "failed to get rndm_idx; url=~{0} core_uid=~{1} rng_idx=~{2} err=~{3}", conn.Conn_info().Db_api(), core_uid, rng_idx, Err_.Message_gplx_log(e)); return -1;}
finally {rdr.Rls();}
}
public void Rls() {
stmt_select = Db_stmt_.Rls(stmt_select);
}
}

View File

@@ -1,41 +0,0 @@
/*
XOWA: the XOWA Offline Wiki Application
Copyright (C) 2012 gnosygnu@gmail.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package gplx.xowa.specials.randoms; import gplx.*; import gplx.xowa.*; import gplx.xowa.specials.*;
import gplx.xowa.bldrs.*; import gplx.xowa.bldrs.wkrs.*;
public class Xob_rnd_cmd implements Xob_cmd {
private final Xowe_wiki wiki;
private int rndm_uid = 0; private String rndm_where_sql = "AND ~{page_namespace} = 0 AND ~{page_is_redirect} = 0"; private int rndm_interval = 1000;
public Xob_rnd_cmd(Xob_bldr bldr, Xowe_wiki wiki) {this.wiki = wiki;}
public String Cmd_key() {return Xob_cmd_keys.Key_util_random;}
public Xob_cmd Cmd_clone(Xob_bldr bldr, Xowe_wiki wiki) {return null;}
public void Cmd_run() {
wiki.Special_mgr().Page_random().Mgr().Rebuild(rndm_uid, rndm_where_sql, rndm_interval);
}
public Object Invk(GfsCtx ctx, int ikey, String k, GfoMsg m) {
if (ctx.Match(k, Invk_rndm_uid_)) rndm_uid = m.ReadInt("v");
else if (ctx.Match(k, Invk_rndm_where_sql_)) rndm_where_sql = m.ReadStr("v");
else if (ctx.Match(k, Invk_rndm_interval_)) rndm_interval = m.ReadInt("v");
else return GfoInvkAble_.Rv_unhandled;
return this;
} private static final String Invk_rndm_uid_ = "rndm_uid_", Invk_rndm_where_sql_ = "rndm_where_sql_", Invk_rndm_interval_ = "rndm_interval_";
public void Cmd_init(Xob_bldr bldr) {}
public void Cmd_bgn(Xob_bldr bldr) {}
public void Cmd_end() {}
public void Cmd_term() {}
}

View File

@@ -19,7 +19,7 @@ package gplx.xowa.specials.randoms; import gplx.*; import gplx.xowa.*; import gp
import gplx.xowa.wikis.nss.*;
public class Xows_page_random implements Xows_page {
public Xows_page_random(Xowe_wiki wiki) {}
public Rndm_mgr Mgr() {return mgr;} private final Rndm_mgr mgr = new Rndm_mgr();
// public Rndm_mgr Mgr() {return mgr;} private final Rndm_mgr mgr = new Rndm_mgr();
public Xows_special_meta Special__meta() {return Xows_special_meta_.Itm__random;}
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;