class Wdata_fmtr__json implements gplx.core.brys.Bfr_arg {
  private final Bry_bfr tmp_bfr = Bry_bfr_.Reset(255);
  private Xoapi_toggle_itm toggle_itm;
  private Wdata_toc_data toc_data;
  private Json_doc jdoc;

  public void Init_by_ctor(Wdata_toc_data toc_data, Xoapi_toggle_mgr toggle_mgr) {
    this.toc_data = toc_data.Itms_len_enable_n_();
    this.toggle_itm =
        toggle_mgr.Get_or_new("wikidatawiki-json")
            .Html_toggle_hdr_cls_(Bry_.new_a7("overflow:auto;"));
  }

  public void Init_by_lang(Wdata_hwtr_msgs msgs) {
    toc_data.Orig_(msgs.Json_div_hdr());
    toggle_itm.Init_msgs(msgs.Toggle_title_y(), msgs.Toggle_title_n());
  }

  public void Init_by_wdoc(Json_doc jdoc) {
    this.jdoc = jdoc;
    toc_data.Make(0);
  }

  public void Bfr_arg__add(Bry_bfr bfr) {
    if (jdoc == null) return; // TEST: wdoc doesn't have jdoc
    jdoc.Root_nde().Print_as_json(tmp_bfr, 0);
    fmtr.Bld_bfr_many(
        bfr,
        toc_data.Href(),
        toc_data.Text(),
        toggle_itm.Html_toggle_btn(),
        toggle_itm.Html_toggle_hdr(),
        tmp_bfr.To_bry_and_clear());
  }

  private final Bry_fmtr fmtr =
      Bry_fmtr.new_(
          String_.Concat_lines_nl_skip_last(
              "",
              "  <h2 class='wb-section-heading' id='~{hdr_href}'>~{hdr_text}~{toggle_btn}</h2>",
              "  <div class='visualClear'></div>",
              "  <pre~{toggle_hdr}>~{json}",
              "  </pre>",
              "  </div>"),
          "hdr_href",
          "hdr_text",
          "toggle_btn",
          "toggle_hdr",
          "json");
}
Exemple #2
0
 public static byte[] Eval_val_or(
     Xop_ctx ctx,
     byte[] src,
     Xot_invk caller,
     Xot_invk self,
     int self_args_len,
     int i,
     byte[] or) {
   if (i >= self_args_len) return or;
   Bry_bfr bfr = Bry_bfr_.New();
   Arg_nde_tkn nde = self.Args_get_by_idx(i);
   nde.Val_tkn().Tmpl_evaluate(ctx, src, caller, bfr);
   return bfr.To_bry_and_clear_and_trim();
 }
Exemple #3
0
 public static byte[] Eval_arg_or(
     Xop_ctx ctx,
     byte[] src,
     Xot_invk caller,
     Xot_invk self,
     int self_args_len,
     int i,
     byte[] or) {
   if (i >= self_args_len) return or;
   Arg_nde_tkn nde = self.Args_get_by_idx(i);
   Bry_bfr bfr = Bry_bfr_.New();
   Eval_arg_or(bfr, ctx, src, caller, self, nde, or);
   return bfr.To_bry_and_clear_and_trim();
 }
public class Xoh_page_bfr {
  private byte toc_mode;
  private Bry_bfr head_bfr;
  private final Bry_bfr body_bfr = Bry_bfr_.New();

  public void Init(Bry_bfr head_bfr) {
    this.toc_mode = Xoh_toc_data.Toc_mode__none;
    this.head_bfr = head_bfr;
    body_bfr.Clear();
  }

  public Bry_bfr Split_by_toc(byte toc_mode) {
    this.toc_mode = toc_mode;
    return body_bfr;
  }

  public void Commit(Xoa_page pg) {
    boolean toc_mode_enabled = true,
        toc_mode_is_pgbnr = false; // default to Xoh_toc_data_.Toc_mode__basic
    switch (toc_mode) {
      case Xoh_toc_data.Toc_mode__none:
        toc_mode_enabled = false;
        break;
      case Xoh_toc_data.Toc_mode__pgbnr:
        toc_mode_is_pgbnr = true;
        break;
    }

    // set flags
    pg.Html_data().Toc_mgr().Exists_y_();
    pg.Html_data().Head_mgr().Itm__pgbnr().Enabled_(toc_mode_is_pgbnr);

    // build bfr by add bfr_0, toc, body_bfr
    if (toc_mode_enabled) {
      pg.Html_data()
          .Toc_mgr()
          .To_html(head_bfr, gplx.xowa.htmls.core.htmls.Xoh_wtr_ctx.Basic, toc_mode_is_pgbnr);
      head_bfr.Add_bfr_and_clear(body_bfr);
    }
  }
}
 public byte[] Convert_to_local_urls(byte[] rel_url_prefix, byte[] src, List_adp list) {
   try {
     int src_len = src.length;
     int prv_pos = 0;
     Bry_bfr bfr = Bry_bfr_.New_w_size(src_len);
     Hash_adp img_hash = Hash_adp_bry.cs();
     while (true) {
       int url_pos = Bry_find_.Find_fwd(src, Bry_url, prv_pos);
       if (url_pos == Bry_find_.Not_found) {
         bfr.Add_mid(src, prv_pos, src_len);
         break;
       } // no more "url("; exit;
       int bgn_pos = url_pos + Bry_url_len; // set bgn_pos after "url("
       byte bgn_byte = src[bgn_pos];
       byte end_byte = Byte_ascii.Null;
       boolean quoted = true;
       switch (bgn_byte) { // find end_byte
         case Byte_ascii.Quote:
         case Byte_ascii.Apos: // quoted; end_byte is ' or "
           end_byte = bgn_byte;
           ++bgn_pos;
           break;
         default: // not quoted; end byte is ")"
           end_byte = Byte_ascii.Paren_end;
           quoted = false;
           break;
       }
       int end_pos = Bry_find_.Find_fwd(src, end_byte, bgn_pos, src_len);
       if (end_pos
           == Bry_find_.Not_found) { // unclosed "url("; exit since nothing else will be found
         usr_dlg.Warn_many(
             GRP_KEY,
             "parse.invalid_url.end_missing",
             "could not find end_sequence for 'url(': bgn='~{0}' end='~{1}'",
             prv_pos,
             String_.new_u8__by_len(src, prv_pos, prv_pos + 25));
         bfr.Add_mid(src, prv_pos, src_len);
         break;
       }
       if (end_pos - bgn_pos == 0) { // empty; "url()"; ignore
         usr_dlg.Warn_many(
             GRP_KEY,
             "parse.invalid_url.empty",
             "'url(' is empty: bgn='~{0}' end='~{1}'",
             prv_pos,
             String_.new_u8__by_len(src, prv_pos, prv_pos + 25));
         bfr.Add_mid(src, prv_pos, bgn_pos);
         prv_pos = bgn_pos;
         continue;
       }
       byte[] img_raw = Bry_.Mid(src, bgn_pos, end_pos);
       int img_raw_len = img_raw.length;
       if (Bry_.Has_at_bgn(img_raw, Bry_data_image, 0, img_raw_len)) { // base64
         bfr.Add_mid(src, prv_pos, end_pos); // nothing to download; just add entire String
         prv_pos = end_pos;
         continue;
       }
       int import_url_end =
           Import_url_chk(
               rel_url_prefix,
               src,
               src_len,
               prv_pos,
               url_pos,
               img_raw,
               bfr); // check for embedded stylesheets via @import tag
       if (import_url_end != Bry_find_.Not_found) {
         prv_pos = import_url_end;
         continue;
       }
       byte[] img_cleaned = Xob_url_fixer.Fix(wiki_domain, img_raw, img_raw_len);
       if (img_cleaned == null) { // could not clean img
         usr_dlg.Warn_many(
             GRP_KEY,
             "parse.invalid_url.clean_failed",
             "could not extract valid http src: bgn='~{0}' end='~{1}'",
             prv_pos,
             String_.new_u8(img_raw));
         bfr.Add_mid(src, prv_pos, bgn_pos);
         prv_pos = bgn_pos;
         continue;
       }
       if (!img_hash.Has(img_cleaned)) { // only add unique items for download;
         img_hash.Add_as_key_and_val(img_cleaned);
         list.Add(String_.new_u8(img_cleaned));
       }
       img_cleaned =
           Replace_invalid_chars(
               Bry_.Copy(
                   img_cleaned)); // NOTE: must call ByteAry.Copy else img_cleaned will change
                                  // *inside* hash
       bfr.Add_mid(src, prv_pos, bgn_pos);
       if (!quoted) bfr.Add_byte(Byte_ascii.Quote);
       bfr.Add(img_cleaned);
       if (!quoted) bfr.Add_byte(Byte_ascii.Quote);
       prv_pos = end_pos;
     }
     return bfr.To_bry_and_clear();
   } catch (Exception e) {
     usr_dlg.Warn_many(
         "",
         "",
         "failed to convert local_urls: ~{0} ~{1}",
         String_.new_u8(rel_url_prefix),
         Err_.Message_gplx_full(e));
     return src;
   }
 }
public class Split_wkr__rndm implements Split_wkr {
  private final Split_rslt_wkr__rndm rslt_wkr = new Split_rslt_wkr__rndm();
  private final Bry_bfr tmp_bfr = Bry_bfr_.New();
  private final Db_attach_mgr attach_mgr = new Db_attach_mgr();
  private Rndm_bldr_wkr bldr;
  private int cur_ns_id = -1;

  public void Split__init(Split_ctx ctx, Xow_wiki wiki, Db_conn wkr_conn) {
    this.bldr = Rndm_addon.Get(wiki).Mgr().New_bldr();
    ctx.Rslt_mgr().Reg_wkr(rslt_wkr);
  }

  public void Split__term(Split_ctx ctx) {
    bldr.Exec_qry_end();
  }

  public void Split__trg__1st__new(Split_ctx ctx, Db_conn trg_conn) {
    Rndm_qry_tbl mgr_tbl = new Rndm_qry_tbl(trg_conn);
    mgr_tbl.Create_tbl();
  }

  public void Split__trg__nth__new(Split_ctx ctx, Db_conn trg_conn) {
    if (cur_ns_id != ctx.Trg_ns()) {
      if (cur_ns_id != -1) bldr.Exec_qry_end();
      cur_ns_id = ctx.Trg_ns();
      bldr.Exec_qry_bgn(Rndm_qry_itm.New_by_ns(ctx.Wiki(), cur_ns_id));
    }
    bldr.Exec_rng_bgn();
    bldr.Conn().Txn_bgn("rndm");
  }

  public void Split__trg__nth__rls(Split_ctx ctx, Db_conn trg_conn) {
    // make rndm_rng and add
    Rndm_rng_itm rng_itm = bldr.Exec_rng_end_or_null();
    if (rng_itm == null) return;
    Rndm_rng_tbl rng_tbl = new Rndm_rng_tbl(trg_conn);
    rng_tbl.Create_tbl();
    Db_stmt rng_stmt = rng_tbl.Insert_stmt();
    rng_tbl.Insert(
        rng_stmt, rng_itm.Mgr_idx(), rng_itm.Rng_idx(), rng_itm.Seq_bgn(), rng_itm.Seq_end());
    rng_stmt.Rls();
    bldr.Conn().Txn_end();

    // make rndm_seq and bulk copy
    Rndm_seq_tbl seq_tbl = new Rndm_seq_tbl(trg_conn);
    seq_tbl.Create_tbl();
    Split_tbl_.Bld_insert_by_select(tmp_bfr, seq_tbl.Tbl_name(), seq_tbl.Flds());
    tmp_bfr.Add_str_u8_fmt(
        "WHERE {0} = {1} AND {2} = {3}",
        seq_tbl.Fld__qry_idx(), bldr.Qry_idx(), seq_tbl.Fld__rng_idx(), bldr.Rng_idx());
    attach_mgr.Conn_main_(trg_conn).Conn_links_(new Db_attach_itm("src_db", bldr.Conn()));
    attach_mgr.Exec_sql(tmp_bfr.To_str_and_clear());
    //			bldr.Conn().Txn_bgn("rndm");
  }

  public void Split__exec(Split_ctx ctx, Split_rslt_mgr rslt_mgr, Xowd_page_itm page, int page_id) {
    if (page.Ns_id() == gplx.xowa.wikis.nss.Xow_ns_.Tid__main && !page.Redirected()) {
      bldr.Exec_seq_itm(page.Id());
      rslt_wkr.On__nth__itm(
          Rndm_seq_tbl.Db_row_size_fixed, bldr.Qry_idx(), bldr.Rng_idx(), bldr.Seq_in_qry());
    }
  }

  public void Split__pages_loaded(
      Split_ctx ctx, int ns_id, int score_bgn, int score_end) {} // rndm_wkr has no caching
}
public class Xoh_page_wtr_wkr {
  private final Object thread_lock_1 = new Object(), thread_lock_2 = new Object();
  private final Bry_bfr tmp_bfr = Bry_bfr_.Reset(255);
  private final Xoh_page_wtr_mgr mgr;
  private final byte page_mode;
  private final Wdata_xwiki_link_wtr wdata_lang_wtr =
      new Wdata_xwiki_link_wtr(); // In other languages
  private Xoae_app app;
  private Xowe_wiki wiki;
  private Xoae_page page;
  private byte[] root_dir_bry;

  public Xoh_page_wtr_wkr(Xoh_page_wtr_mgr mgr, byte page_mode) {
    this.mgr = mgr;
    this.page_mode = page_mode;
  }

  public Xoh_page_wtr_wkr Ctgs_enabled_(boolean v) {
    ctgs_enabled = v;
    return this;
  }

  private boolean ctgs_enabled = true;

  public void Write_page(Bry_bfr rv, Xoae_page page, Xop_ctx ctx) {
    synchronized (thread_lock_1) {
      this.page = page;
      this.wiki = page.Wikie();
      this.app = wiki.Appe();
      ctx.Page_(page); // HACK: must update page for toc_mgr; WHEN: Xoae_page rewrite
      Bry_fmtr fmtr = null;
      if (mgr.Html_capable()) {
        wdata_lang_wtr.Page_(page);
        byte view_mode = page_mode;
        switch (page_mode) {
          case Xopg_page_.Tid_edit:
            fmtr = mgr.Page_edit_fmtr();
            break;
          case Xopg_page_.Tid_html:
            fmtr = mgr.Page_read_fmtr();
            view_mode = Xopg_page_.Tid_read;
            break; // set view_mode to read, so that "read" is highlighted in HTML
          case Xopg_page_.Tid_read:
            fmtr = mgr.Page_read_fmtr();
            // ctx.Page().Redlink_list().Clear();	// not sure if this is the best place to put it,
            // but redlinks (a) must only fire once; (b) must fire before html generation; (c)
            // cannot fire during edit (preview will handle separately); NOTE: probably put in to
            // handle reusable redlink lists; redlink lists are now instantiated per page, so clear
            // is not useful
            break;
        }
        Bry_bfr page_bfr =
            wiki.Utl__bfr_mkr()
                .Get_m001(); // NOTE: get separate page rv to output page; do not reuse tmp_bfr b/c
                             // it will be used inside Fmt_do
        Xoh_wtr_ctx hctx = null;
        if (page_mode == Xopg_page_.Tid_html
            && wiki.App().Api_root().Wiki().Hdump().Html_mode().Tid_is_hdump_save()) {
          hctx = Xoh_wtr_ctx.Hdump;
          Write_body(page_bfr, ctx, hctx, page);
          Write_page_by_tid(
              ctx,
              hctx,
              page_mode,
              rv,
              mgr.Page_html_fmtr(),
              Gfh_utl.Escape_html_as_bry(page_bfr.To_bry_and_clear()));
        } else {
          hctx = Xoh_wtr_ctx.Basic;
          Write_body(page_bfr, ctx, hctx, page);
          Write_page_by_tid(ctx, hctx, view_mode, rv, fmtr, page_bfr.To_bry_and_rls());
          new gplx.xowa.addons.apps.scripts.Xoscript_mgr().Write(rv, wiki, page);
          if (page_mode
              == Xopg_page_
                  .Tid_html) // if html, write page again, but wrap it in html skin this time
          Write_page_by_tid(
                ctx,
                hctx,
                page_mode,
                rv,
                mgr.Page_html_fmtr(),
                Gfh_utl.Escape_html_as_bry(rv.To_bry_and_clear()));
          wdata_lang_wtr.Page_(null);
        }
      } else Write_body(rv, ctx, Xoh_wtr_ctx.Basic, page);
      this.page = null;
    }
  }

  private void Write_page_by_tid(
      Xop_ctx ctx,
      Xoh_wtr_ctx hctx,
      byte html_gen_tid,
      Bry_bfr bfr,
      Bry_fmtr fmtr,
      byte[] page_data) {
    // if custom_html, use it and exit; needed for Default_tab
    byte[] custom_html = page.Html_data().Custom_html();
    if (custom_html != null) {
      bfr.Add(custom_html);
      return;
    }
    // temp variables
    if (root_dir_bry == null) this.root_dir_bry = app.Fsys_mgr().Root_dir().To_http_file_bry();
    Xoa_ttl page_ttl = page.Ttl();
    int page_ns_id = page_ttl.Ns().Id();
    byte page_tid = Xow_page_tid.Identify(wiki.Domain_tid(), page_ns_id, page_ttl.Page_db());
    DateAdp modified_on = page.Db().Page().Modified_on();
    byte[] modified_on_msg =
        wiki.Msg_mgr()
            .Val_by_id_args(
                Xol_msg_itm_.Id_portal_lastmodified,
                modified_on.XtoStr_fmt_yyyy_MM_dd(),
                modified_on.XtoStr_fmt_HHmm());
    byte[] page_body_class = Xoh_page_body_cls.Calc(tmp_bfr, page_ttl, page_tid);
    byte[] html_content_editable =
        wiki.Gui_mgr().Cfg_browser().Content_editable() ? Content_editable_bry : Bry_.Empty;
    byte[] page_content_sub = Xoh_page_wtr_wkr_.Bld_page_content_sub(app, wiki, page, tmp_bfr);
    byte[] js_edit_toolbar_bry =
        html_gen_tid == Xopg_page_.Tid_edit
            ? wiki.Fragment_mgr().Html_js_edit_toolbar()
            : Bry_.Empty;
    Xol_vnt_mgr vnt_mgr = wiki.Lang().Vnt_mgr();
    if (vnt_mgr.Enabled()) {
      byte[] converted_title = vnt_mgr.Convert_lang().Converted_title(); // prefer converted title
      if (converted_title
          == null) // converted title does not exist; use regular page title and convert it
      converted_title = vnt_mgr.Convert_lang().Auto_convert(vnt_mgr.Cur_itm(), page_ttl.Page_txt());
      page_ttl = Xoa_ttl.Parse(wiki, page_ttl.Ns().Id(), converted_title);
    }
    byte[] page_name =
        Xoh_page_wtr_wkr_.Bld_page_name(
            tmp_bfr, page_ttl,
            null); // NOTE: page_name does not show display_title (<i>). always pass in null
    byte[] page_display_title =
        Xoh_page_wtr_wkr_.Bld_page_name(tmp_bfr, page_ttl, page.Html_data().Display_ttl());
    page.Html_data()
        .Custom_tab_name_(
            page_name); // set tab_name to page_name; note that if null, gui code will ignore and
                        // use Ttl.Page_txt; PAGE: zh.w:釣魚臺列嶼主權問題 DATE:2015-10-05
    Xow_portal_mgr portal_mgr = wiki.Html_mgr().Portal_mgr().Init_assert();
    fmtr.Bld_bfr_many(
        bfr,
        root_dir_bry,
        Xoa_app_.Version,
        Xoa_app_.Build_date,
        app.Tcp_server().Running_str(),
        page.Db().Page().Id(),
        page.Ttl().Full_db(),
        page_name,
        page.Html_data().Page_heading().Init(page.Html_data(), page_display_title),
        modified_on_msg,
        mgr.Css_common_bry(),
        mgr.Css_wiki_bry(),
        page.Html_data().Head_mgr().Init(app, wiki, page).Init_dflts(),
        page.Lang().Dir_ltr_bry(),
        page.Html_data().Indicators(),
        page_content_sub,
        wiki.Html_mgr().Portal_mgr().Div_jump_to(),
        wiki.Xtn_mgr().Xtn_pgbnr().Write_html(page, ctx, hctx),
        page_body_class,
        html_content_editable,
        page_data,
        wdata_lang_wtr

        // sidebar divs
        ,
        portal_mgr.Div_personal_bry(),
        portal_mgr.Div_ns_bry(wiki.Utl__bfr_mkr(), page_ttl, wiki.Ns_mgr()),
        portal_mgr.Div_view_bry(
            wiki.Utl__bfr_mkr(), html_gen_tid, page.Html_data().Xtn_search_text()),
        portal_mgr.Div_logo_bry(),
        portal_mgr.Div_home_bry(),
        new Xopg_xtn_skin_fmtr_arg(page, Xopg_xtn_skin_itm_tid.Tid_sidebar),
        portal_mgr.Div_sync_bry(
            tmp_bfr, app.Api_root().Addon().Bldr().Sync().Manual_enabled(), wiki, page),
        portal_mgr.Div_wikis_bry(wiki.Utl__bfr_mkr()),
        portal_mgr.Sidebar_mgr().Html_bry(),
        mgr.Edit_rename_div_bry(page_ttl),
        page.Html_data().Edit_preview_w_dbg(),
        js_edit_toolbar_bry);
    Xoh_page_wtr_wkr_.Bld_head_end(bfr, tmp_bfr, page); // add after </head>
    Xoh_page_wtr_wkr_.Bld_html_end(bfr, tmp_bfr, page); // add after </html>		
  }

  public void Write_hdump(Bry_bfr bfr, Xop_ctx ctx, Xoh_wtr_ctx hctx, Xoae_page wpg) {
    if (wpg.Html_data().Xtn_pgbnr() != null) {
      ctx.Wiki()
          .Xtn_mgr()
          .Xtn_pgbnr()
          .Write_html(wpg, ctx, hctx)
          .Bfr_arg__add(bfr); // if pgbnr exists, write to top of html
    }
    this.Write_body(bfr, ctx, hctx, wpg);
  }

  public void Write_body(Bry_bfr bfr, Xop_ctx ctx, Xoh_wtr_ctx hctx, Xoae_page page) {
    synchronized (thread_lock_2) {
      this.page = page;
      this.wiki = page.Wikie();
      this.app = wiki.Appe();
      Xoa_ttl page_ttl = page.Ttl();
      int page_ns_id = page_ttl.Ns().Id();
      byte page_tid =
          Xow_page_tid.Identify(
              wiki.Domain_tid(),
              page_ns_id,
              page_ttl
                  .Page_db()); // NOTE: can't cache page_tid b/c Write_body is called directly;
                               // DATE:2014-10-02
      byte[] data_raw = page.Db().Text().Text_bry();
      int bfr_page_bgn = bfr.Len();
      boolean page_tid_uses_pre = false;
      if (page_mode == Xopg_page_.Tid_edit) Write_body_edit(bfr, data_raw, page_ns_id, page_tid);
      else {
        switch (page_tid) {
          case Xow_page_tid.Tid_msg:
          case Xow_page_tid.Tid_js:
          case Xow_page_tid.Tid_css:
          case Xow_page_tid.Tid_lua:
            Write_body_pre(bfr, app, wiki, hctx, data_raw, tmp_bfr);
            page_tid_uses_pre = true;
            break;
          case Xow_page_tid.Tid_json:
            app.Wiki_mgr().Wdata_mgr().Write_json_as_html(bfr, page_ttl.Full_db(), data_raw);
            break;
          case Xow_page_tid.Tid_wikitext:
            Write_body_wikitext(bfr, app, wiki, data_raw, ctx, hctx, page, page_tid, page_ns_id);
            break;
        }
      }
      if (wiki.Domain_tid() != Xow_domain_tid_.Tid__home // allow home wiki to use javascript
          && !page_tid_uses_pre) { // if .js, .css or .lua, skip test; may have js fragments, but
                                   // entire text is escaped and put in pre; don't show spurious
                                   // warning; DATE:2013-11-21
        wiki.Html_mgr().Js_cleaner().Clean_bfr(wiki, page_ttl, bfr, bfr_page_bgn);
      }
    }
  }

  private void Write_body_wikitext(
      Bry_bfr bfr,
      Xoae_app app,
      Xowe_wiki wiki,
      byte[] data_raw,
      Xop_ctx ctx,
      Xoh_wtr_ctx hctx,
      Xoae_page page,
      byte page_tid,
      int ns_id) {
    // dump and exit if pre-generated html from html dumps
    byte[] hdump_data = page.Db().Html().Html_bry();
    if (Bry_.Len_gt_0(hdump_data)) {
      bfr.Add(hdump_data);
      return;
    }

    // dump and exit if MediaWiki message;
    if (ns_id
        == Xow_ns_
            .Tid__mediawiki) { // if MediaWiki and wikitext, must be a message; convert args back to
                               // php; DATE:2014-06-13
      bfr.Add(Gfs_php_converter.Xto_php(tmp_bfr, Bool_.N, data_raw));
      return;
    }

    // if [[File]], add boilerplate header; note that html is XOWA-generated so does not need to be
    // tidied
    if (ns_id == Xow_ns_.Tid__file)
      app.Ns_file_page_mgr()
          .Bld_html(wiki, ctx, page, bfr, page.Ttl(), wiki.Cfg_file_page(), page.File_queue());

    // get separate bfr; note that bfr already has <html> and <head> written to it, so this can't be
    // passed to tidy; DATE:2014-06-11
    Bry_bfr tidy_bfr = wiki.Utl__bfr_mkr().Get_m001();

    // write wikitext
    if (page.Html_data().Skip_parse()) {
      tidy_bfr.Add(page.Html_data().Custom_body());
    } else {
      if (page.Root()
          != null) { // NOTE: will be null if blank; occurs for one test:
                     // Logo_has_correct_main_page; DATE:2015-09-29
        page.Html_data()
            .Toc_mgr()
            .Clear(); // NOTE: always clear tocs before writing html; toc_itms added when writing
                      // html_hdr; DATE:2016-07-17
        wiki.Html_mgr()
            .Html_wtr()
            .Write_doc(tidy_bfr, ctx, hctx, page.Root().Data_mid(), page.Root());
        if (wiki.Html_mgr().Html_wtr().Cfg().Toc__show())
          gplx.xowa.htmls.core.wkrs.tocs.Xoh_toc_wtr.Write_toc(tidy_bfr, page, hctx);
      }
    }

    // if [[Category]], add catpage data
    if (ns_id == Xow_ns_.Tid__category) tidy_bfr.Add_safe(page.Html_data().Catpage_data());
    // if (ns_id == Xow_ns_.Tid__category) wiki.Ctg__catpage_mgr().Write_catpage(tidy_bfr, page,
    // hctx);

    // tidy html
    wiki.Html_mgr().Tidy_mgr().Exec_tidy(tidy_bfr, !hctx.Mode_is_hdump(), page.Url_bry_safe());

    // add back to main bfr
    bfr.Add_bfr_and_clear(tidy_bfr);
    tidy_bfr.Mkr_rls();

    // handle Categories at bottom of page; note that html is XOWA-generated so does not need to be
    // tidied
    int ctgs_len = page.Wtxt().Ctgs__len();
    if (ctgs_enabled
        && ctgs_len > 0 // skip if no categories found while parsing wikitext
        && !wiki.Html_mgr()
            .Importing_ctgs() // do not show categories if importing categories, page will wait for
                              // category import to be done; DATE:2014-10-15
        && !hctx.Mode_is_hdump() // do not dump categories during hdump; DATE:2016-10-12
    ) {
      if (app.Mode().Tid_is_gui())
        app.Usr_dlg().Prog_many("", "", "loading categories: count=~{0}", ctgs_len);
      Xoctg_pagebox_itm[] pagebox_itms = wiki.Ctg__pagebox_wtr().Get_catlinks_by_page(wiki, page);
      boolean hidden_enabled = wiki.App().Api_root().Addon().Wikis__ctgs__hidden_enabled();
      wiki.Ctg__pagebox_wtr().Write_pagebox(hidden_enabled, bfr, wiki, page, pagebox_itms);
    }

    // translate if variants are enabled
    Xol_vnt_mgr vnt_mgr = wiki.Lang().Vnt_mgr();
    if (vnt_mgr.Enabled())
      bfr.Add(
          vnt_mgr.Convert_lang()
              .Parse_page(vnt_mgr.Cur_itm(), page.Db().Page().Id(), bfr.To_bry_and_clear()));
  }

  private void Write_body_pre(
      Bry_bfr bfr,
      Xoae_app app,
      Xowe_wiki wiki,
      Xoh_wtr_ctx hctx,
      byte[] data_raw,
      Bry_bfr tmp_bfr) {
    Xoh_html_wtr_escaper.Escape(
        app.Parser_amp_mgr(), tmp_bfr, data_raw, 0, data_raw.length, false, false);
    if (hctx.Mode_is_hdump()) bfr.Add(data_raw);
    else app.Html_mgr().Page_mgr().Content_code_fmtr().Bld_bfr_many(bfr, tmp_bfr);
    tmp_bfr.Clear();
  }

  private void Write_body_edit(Bry_bfr bfr, byte[] data_raw, int ns_id, byte page_tid) {
    if (ns_id
            == Xow_ns_
                .Tid__mediawiki // if MediaWiki and wikitext, must be a message; convert args back
                                // to php; DATE:2014-06-13
        && page_tid == Xow_page_tid.Tid_wikitext)
      data_raw = Gfs_php_converter.Xto_php(tmp_bfr, Bool_.N, data_raw);
    int data_raw_len = data_raw.length;
    if (mgr.Html_capable())
      Xoh_html_wtr_escaper.Escape(
          page.Wikie().Appe().Parser_amp_mgr(),
          bfr,
          data_raw,
          0,
          data_raw_len,
          false,
          false); // NOTE: must escape; assume that browser will automatically escape (&lt;) (which
                  // Mozilla does)
    else bfr.Add(data_raw);
    if (data_raw_len > 0) // do not add nl if empty String
    bfr
          .Add_byte_nl(); // per MW:EditPage.php: "Ensure there's a newline at the end, otherwise
                          // adding lines is awkward."
  }

  private static final byte[] Content_editable_bry = Bry_.new_a7(" contenteditable=\"true\"");
}
Exemple #8
0
 public void Xtn_write(
     Bry_bfr bfr,
     Xoae_app app,
     Xop_ctx ctx,
     Xoh_html_wtr html_wtr,
     Xoh_wtr_ctx hctx,
     Xoae_page wpg,
     Xop_xnde_tkn xnde,
     byte[] src) {
   Xowe_wiki wiki = ctx.Wiki();
   Dpl_html_data html_mode = Dpl_html_data.new_(Dpl_itm_keys.Key_unordered);
   int itms_len = pages.Count();
   if (itms_len == 0) {
     if (!itm.Suppress_errors()) bfr.Add_str_a7("No pages meet these criteria.");
     return;
   }
   int itms_bgn = 0;
   if (itm.Offset() != Int_.Min_value) {
     itms_bgn = itm.Offset();
   }
   if (itm.Count() != Int_.Min_value && itms_bgn + itm.Count() < itms_len) {
     itms_len = itms_bgn + itm.Count();
   }
   boolean show_ns = itm.Show_ns();
   Bry_bfr tmp_bfr = Bry_bfr_.Get();
   Xop_amp_mgr amp_mgr = wiki.Appe().Parser_amp_mgr();
   try {
     bfr.Add(html_mode.Grp_bgn()).Add_byte_nl();
     for (int i = itms_bgn; i < itms_len; i++) {
       Xowd_page_itm page = (Xowd_page_itm) pages.Get_at(i);
       Xoa_ttl ttl = Xoa_ttl.Parse(wiki, page.Ns_id(), page.Ttl_page_db());
       byte[] ttl_page_txt = show_ns ? ttl.Full_txt_w_ttl_case() : ttl.Page_txt();
       if (ttl_page_txt == null)
         continue; // NOTE: apparently DynamicPageList allows null pages; DATE:2013-07-22
       switch (html_mode.Tid()) {
         case Dpl_html_data.Tid_list_ul:
         case Dpl_html_data.Tid_list_ol:
           bfr.Add(Xoh_consts.Space_2).Add(html_mode.Itm_bgn()).Add(Gfh_bldr_.Bry__a_lhs_w_href);
           bfr.Add_str_a7("/wiki/")
               .Add(Gfo_url_encoder_.Href.Encode(ttl.Full_db()))
               .Add_byte_quote(); // NOTE: Full_db to encode spaces as underscores;
                                  // PAGE:en.q:Wikiquote:Speedy_deletions DATE:2016-01-19
           Gfh_atr_.Add(
               bfr,
               Gfh_atr_.Bry__title,
               Xoh_html_wtr_escaper.Escape(
                   amp_mgr,
                   tmp_bfr,
                   ttl
                       .Full_txt_w_ttl_case())); // NOTE: Full_txt b/c title always includes ns,
                                                 // even if show_ns is off;
                                                 // PAGE:en.b:Wikibooks:WikiProject DATE:2016-01-20
           if (itm.No_follow()) bfr.Add(Bry_nofollow);
           bfr.Add_byte(Byte_ascii.Gt);
           Xoh_html_wtr_escaper.Escape(
               amp_mgr, bfr, ttl_page_txt, 0, ttl_page_txt.length, false, false);
           bfr.Add(Gfh_bldr_.Bry__a_rhs).Add(html_mode.Itm_end()).Add_byte_nl();
           // TODO_OLD:
           // lnki_wtr.Clear().Href_wiki_(ttl).Title_(ttl).Nofollow_().Write_head(bfr).Write_text(bfr).Write_tail(bfr)
           break;
         default:
           break;
       }
     }
     bfr.Add(html_mode.Grp_end()).Add_byte_nl();
   } finally {
     tmp_bfr.Mkr_rls();
   }
 }
public class Xol_num_mgr implements Gfo_invk {
  private boolean digits_translate;
  protected Bry_bfr tmp_bfr = Bry_bfr_.Reset(32);
  private static final byte[] Comma_bry = Bry_.new_a7(",");

  public Xol_num_grp_fmtr Num_grp_fmtr() {
    return num_grp_fmtr;
  }

  private Xol_num_grp_fmtr num_grp_fmtr = new Xol_num_grp_fmtr();

  public Xol_transform_mgr Separators_mgr() {
    return separators_mgr;
  }

  private Xol_transform_mgr separators_mgr = new Xol_transform_mgr();

  public Xol_transform_mgr Digits_mgr() {
    return digits_mgr;
  }

  private Xol_transform_mgr digits_mgr = new Xol_transform_mgr();

  public byte[] Raw(byte[] num) {
    if (digits_translate) num = digits_mgr.Replace(tmp_bfr, num, false);
    num = separators_mgr.Replace(tmp_bfr, num, false);
    num = Bry_.Replace_safe(tmp_bfr, num, Comma_bry, Bry_.Empty);
    return num;
  }

  public byte[] Format_num_no_separators(byte[] num) {
    return Format_num(num, true);
  }

  public byte[] Format_num_by_long(long val) {
    return Format_num(Bry_.new_a7(Long_.To_str(val)));
  }

  public byte[] Format_num(int val) {
    return Format_num(Bry_.new_a7(Int_.To_str(val)));
  }

  public byte[] Format_num(byte[] num) {
    return Format_num(num, false);
  }

  public byte[] Format_num(byte[] num, boolean skip_commafy) {
    if (!skip_commafy) {
      num = Commafy(num);
      num = separators_mgr.Replace(tmp_bfr, num, true);
    }
    if (digits_translate) num = digits_mgr.Replace(tmp_bfr, num, true);
    return num;
  }

  @gplx.Virtual
  public byte[] Commafy(byte[] num_bry) {
    if (num_bry == null) return Bry_.Empty; // MW: if ( $number === null ) return '';
    if (num_grp_fmtr.Mode_is_regx()) return num_grp_fmtr.Fmt_regx(tmp_bfr, num_bry);
    else // NOTE: for now, return same as ###,###,###; only affects 12 languages; current
         // implementation is bad; https://bugzilla.wikimedia.org/show_bug.cgi?id=63977
    return num_grp_fmtr.Fmt_regx(tmp_bfr, num_bry);
  }

  public Xol_num_mgr Clear() {
    digits_mgr.Clear();
    separators_mgr.Clear();
    num_grp_fmtr.Clear();
    return this;
  }

  public Object Invk(GfsCtx ctx, int ikey, String k, GfoMsg m) {
    if (ctx.Match(k, Invk_clear)) this.Clear();
    else if (ctx.Match(k, Invk_separators)) return separators_mgr;
    else if (ctx.Match(k, Invk_digits)) {
      digits_translate = true;
      return digits_mgr;
    } // NOTE: only langes with a digit_transform_table will call digits; DATE:2014-05-28
    else if (ctx.Match(k, Invk_digit_grouping_pattern))
      return String_.new_u8(num_grp_fmtr.Digit_grouping_pattern());
    else if (ctx.Match(k, Invk_digit_grouping_pattern_))
      num_grp_fmtr.Digit_grouping_pattern_(m.ReadBry("v"));
    else return Gfo_invk_.Rv_unhandled;
    return this;
  }

  public static final String Invk_clear = "clear",
      Invk_separators = "separators",
      Invk_digits = "digits",
      Invk_digit_grouping_pattern = "digit_grouping_pattern",
      Invk_digit_grouping_pattern_ = "digit_grouping_pattern_";
  public static final byte[] Separators_key__grp = new byte[] {Byte_ascii.Comma},
      Separators_key__dec = new byte[] {Byte_ascii.Dot};
}
public class Xol_duration_mgr {
  private Xol_msg_itm[] interval_msgs = null;
  private final Bry_bfr tmp_bfr = Bry_bfr_.Reset(255);
  private final Bry_fmtr tmp_fmtr = Bry_fmtr.New__tmp();

  public Xol_duration_mgr(Xol_lang_itm lang) {
    this.lang = lang;
  }

  private Xol_lang_itm lang;

  public Xol_interval_itm[] Get_duration_intervals(long seconds, Xol_duration_itm[] intervals) {
    if (intervals == null) intervals = Xol_duration_itm_.Ary_default;
    Array_.Sort(intervals, Xol_duration_itm_sorter.Instance);
    int intervals_len = intervals.length;
    long val = seconds;
    List_adp rv = List_adp_.New();
    for (int i = 0; i < intervals_len; i++) {
      Xol_duration_itm itm = intervals[i];
      long itm_seconds = itm.Seconds();
      val = seconds / itm_seconds;
      if (val > 0
          || (i == intervals_len - 1
              && rv.Count()
                  == 0) // always add one seg; EX: 40 seconds, but minutes requested -> 0 minutes;
                        // DATE:2014-05-10
      ) {
        seconds -= val * itm_seconds;
        rv.Add(new Xol_interval_itm(itm, val));
      }
    }
    return (Xol_interval_itm[]) rv.To_ary(Xol_interval_itm.class);
  }

  public byte[] Format_durations(Xop_ctx ctx, long seconds, Xol_duration_itm[] ary) {
    if (interval_msgs == null) Format_durations_init();
    Xol_interval_itm[] intervals = Get_duration_intervals(seconds, ary);
    int intervals_len = intervals.length;
    byte[][] msgs_ary = new byte[intervals_len][];
    for (int i = 0; i < intervals_len; i++) {
      Xol_interval_itm interval = intervals[i];
      Xol_msg_itm msg_itm = interval_msgs[interval.Duration_itm().Tid()];
      byte[] msg_bry = msg_itm.Fmt(tmp_bfr, tmp_fmtr, interval.Val());
      msg_bry = ctx.Wiki().Parser_mgr().Main().Parse_text_to_html(ctx, msg_bry);
      msgs_ary[i] = msg_bry;
    }
    return List_to_str(msgs_ary);
  }

  private byte[] Msg_and, Msg_word_separator, Msg_comma_separator;

  private void Format_durations_init() {
    Xol_msg_mgr msg_mgr = lang.Msg_mgr();
    int len = Xol_duration_itm_.Ary_default.length;
    interval_msgs = new Xol_msg_itm[len];
    for (int i = 0; i < len; i++) {
      Xol_duration_itm itm = Xol_duration_itm_.Ary_default[i];
      byte[] msg_key = Bry_.Add(Bry_duration, itm.Name_bry());
      interval_msgs[i] = msg_mgr.Itm_by_key_or_new(msg_key);
    }
  }

  private static final byte[] Bry_duration = Bry_.new_a7("duration-");

  private void List_to_str_init() {
    Xol_msg_mgr msg_mgr = lang.Msg_mgr();
    Msg_and = msg_mgr.Val_by_str_or_empty("and");
    Msg_word_separator = msg_mgr.Val_by_str_or_empty("word-separator");
    Msg_comma_separator = msg_mgr.Val_by_str_or_empty("comma-separator");
  }

  public byte[] List_to_str(byte[][] segs_ary) {
    int len = segs_ary.length;
    switch (len) {
      case 0:
        return Bry_.Empty;
      case 1:
        return segs_ary[0];
      default:
        if (Msg_and == null) List_to_str_init();
        int last_idx = len - 1;
        for (int i = 0; i < last_idx; i++) {
          if (i != 0) tmp_bfr.Add(Msg_comma_separator);
          tmp_bfr.Add(segs_ary[i]);
        }
        tmp_bfr.Add(Msg_and).Add(Msg_word_separator).Add(segs_ary[last_idx]);
        return tmp_bfr.To_bry_and_clear();
    }
  }
}