예제 #1
0
	Xof_repo_itm Ini_repo_add(Xof_file_mgr file_mgr, byte[] key, String root, String wiki, boolean trg) {
		Xof_repo_itm repo = file_mgr.Repo_mgr().Set(String_.new_u8(key), root, wiki).Ext_rules_(Xof_rule_grp.Grp_app_default).Dir_depth_(2);
		if (trg) {
			byte[][] ary = repo.Mode_names();
			ary[0] = Bry_.new_a7("raw");
			ary[1] = Bry_.new_a7("fit");
		}
		return repo;
	}
예제 #2
0
 public void Test_category_v2(String ctg_name_str, int... expd) {
   Xowe_wiki wiki = bldr_fxt.Wiki();
   byte[] ctg_name_bry = Bry_.new_a7(ctg_name_str);
   Xoctg_data_ctg ctg = new Xoctg_data_ctg(ctg_name_bry);
   wiki.Db_mgr_as_sql().Load_mgr().Load_ctg_v2(ctg, ctg_name_bry);
   Tfds.Eq_ary(expd, Xto_int_ary(ctg));
 }
예제 #3
0
 public void Test_search(String search_word_str, int... expd) {
   Xowe_wiki wiki = bldr_fxt.Wiki();
   List_adp rv = List_adp_.new_();
   byte[] search_word_bry = Bry_.new_a7(search_word_str);
   wiki.Db_mgr_as_sql().Load_mgr().Load_search(Cancelable_.Never, rv, search_word_bry, 100);
   Tfds.Eq_ary(expd, Xto_int_ary(rv));
 }
예제 #4
0
 public boolean Chk(Xowe_wiki wiki) {
   if (!wiki.App().App_type().Uid_is_gui())
     return true; // NOTE: ignore during Server / Console modes; DATE:2015-04-01
   if (in_progress_hash.Has(wiki.Domain_bry()))
     return true; // NOTE: ignore if currently building; different bldr commands call
                  // wiki.Init_assert() which may lead to fals checks;
   Io_url url = url_(wiki);
   if (!Io_mgr.I.ExistsFil(url)) return true;
   Xoae_app app = wiki.Appe();
   app.Usr_dlg().Log_many("", "", "import.marker: marker found: url=~{0}", url.Raw());
   byte[] incompete_msg_bry =
       app.Usere()
           .Msg_mgr()
           .Val_by_key_args(Bry_.new_a7("api-xowa.import.core.incomplete"), wiki.Domain_str());
   int rslt = app.Gui_mgr().Kit().Ask_yes_no_cancel("", "", String_.new_u8(incompete_msg_bry));
   switch (rslt) {
     case Gfui_dlg_msg_.Btn_yes:
       Xob_cleanup_cmd.Delete_wiki_sql(wiki);
       End(wiki);
       return false; // delete wiki
     case Gfui_dlg_msg_.Btn_no:
       End(wiki);
       return true; // delete marker
     case Gfui_dlg_msg_.Btn_cancel:
       return true; // noop
     default:
       throw Err_.unhandled(rslt);
   }
 }
예제 #5
0
 public void Test_load_ttl(int ns_id, String ttl_str, Xowd_page_itm expd) {
   Xowe_wiki wiki = bldr_fxt.Wiki();
   Xow_ns ns = wiki.Ns_mgr().Ids_get_or_null(ns_id);
   byte[] ttl_bry = Bry_.new_a7(ttl_str);
   wiki.Db_mgr_as_sql().Load_mgr().Load_by_ttl(actl, ns, ttl_bry);
   Tfds.Eq(expd.Id(), actl.Id());
   Tfds.Eq_date(expd.Modified_on(), actl.Modified_on());
   Tfds.Eq(expd.Redirected(), actl.Redirected());
   Tfds.Eq(expd.Text_len(), actl.Text_len());
 }
예제 #6
0
public class Xop_lnki_align_h_ {
  public static final byte Null = 0, None = 1, Left = 2, Center = 3, Right = 4; // SERIALIZED
  public static final byte[][] Html_names =
      new byte[][] {
        Object_.Bry__null,
        Bry_.new_a7("none"),
        Bry_.new_a7("left"),
        Bry_.new_a7("center"),
        Bry_.new_a7("right")
      };
  public static final Hash_adp_bry Hash =
      Hash_adp_bry.ci_a7()
          .Add_str_byte("tnone", None)
          .Add_str_byte("tleft", Left)
          .Add_str_byte("tcenter", Center)
          .Add_str_byte("tright", Right);

  public static byte[] To_bry(int v) {
    return Html_names[v];
  }
}
예제 #7
0
	@gplx.Virtual public void Clear(boolean src_repo_is_wmf) {
		Io_mgr.I.InitEngine_mem();
		if (app == null) {
			app = Xoa_app_fxt.app_();
			en_wiki = Xoa_app_fxt.wiki_(app, Xow_domain_.Domain_str_enwiki);
			commons = Xoa_app_fxt.wiki_(app, Xow_domain_.Domain_str_commons);
			app.Wiki_mgr().Add(commons);
			app.Wiki_mgr().Add(en_wiki);
			
			Xof_file_mgr file_mgr = app.File_mgr();
			file_mgr.Img_mgr().Wkr_resize_img_(Xof_img_wkr_resize_img_mok._);
			file_mgr.Img_mgr().Wkr_query_img_size_(new Xof_img_wkr_query_img_size_test());
			app.Wmf_mgr().Api_mgr().Api_orig_(api_size);

			byte[] src_commons = Bry_.new_a7("src_commons");
			byte[] src_en_wiki = Bry_.new_a7("src_en_wiki");
			byte[] trg_commons = Bry_.new_a7("trg_commons");
			byte[] trg_en_wiki = Bry_.new_a7("trg_en_wiki");
			src_commons_repo = Ini_repo_add(file_mgr, src_commons, "mem/src/commons.wikimedia.org/", Xow_domain_.Domain_str_commons, false);
			src_en_wiki_repo = Ini_repo_add(file_mgr, src_en_wiki, "mem/src/en.wikipedia.org/"		, Xow_domain_.Domain_str_enwiki, false);
			Ini_repo_add(file_mgr, trg_commons, "mem/trg/commons.wikimedia.org/", Xow_domain_.Domain_str_commons, true).Primary_(true);
			Ini_repo_add(file_mgr, trg_en_wiki, "mem/trg/en.wikipedia.org/"		, Xow_domain_.Domain_str_enwiki, true).Primary_(true);
			Xowe_repo_mgr wiki_repo_mgr = en_wiki.File_mgr().Repo_mgr();
			Xof_repo_pair pair = null;
			pair = wiki_repo_mgr.Add_repo(src_commons, trg_commons);
			pair.Src().Fsys_is_wnt_(true).Wmf_fsys_(src_repo_is_wmf).Tarball_(!src_repo_is_wmf);
			pair.Trg().Fsys_is_wnt_(true);

			pair = wiki_repo_mgr.Add_repo(src_en_wiki, trg_en_wiki);
			pair.Src().Fsys_is_wnt_(true).Wmf_fsys_(src_repo_is_wmf);
			pair.Trg().Fsys_is_wnt_(true);
		}
		en_wiki.Clear_for_tests();
		commons.Clear_for_tests();
		src_fils = trg_fils = Io_fil.Ary_empty;
		html_view_str = null;
		html_w = html_h = -1;
	}
예제 #8
0
public class Wdata_itemByTitle_page implements Xows_page {
  private Xoa_url_arg_hash arg_hash = new Xoa_url_arg_hash();
  private static final byte[] Arg_site = Bry_.new_a7("site"), Arg_page = Bry_.new_a7("page");

  public Bry_fmtr Html_fmtr() {
    return html_fmtr;
  }

  private Wdata_itemByTitle_cfg cfg;

  public Xows_special_meta Special_meta() {
    return Xows_special_meta_.Itm__item_by_title;
  }

  public void Special_gen(Xowe_wiki wiki, Xoae_page page, Xoa_url url, Xoa_ttl ttl) {
    if (cfg == null)
      cfg =
          (Wdata_itemByTitle_cfg) wiki.Appe().Special_mgr().Get_or_null(Wdata_itemByTitle_cfg.Key);
    // Special:ItemByTitle/enwiki/Earth -> www.wikidata.org/wiki/Q2
    Gfo_usr_dlg usr_dlg = wiki.Appe().Usr_dlg();
    byte[] site_bry = cfg.Site_default();
    byte[] page_bry = Bry_.Empty;
    byte[] raw_bry = ttl.Full_txt_wo_qarg(); // EX: enwiki/Earth
    int args_len = url.Args().length;
    if (args_len > 0) {
      arg_hash.Load(url);
      site_bry = arg_hash.Get_val_bry_or(Arg_site, Bry_.Empty);
      page_bry = arg_hash.Get_val_bry_or(Arg_page, Bry_.Empty);
    }
    int site_bgn = Bry_finder.Find_fwd(raw_bry, Xoa_ttl.Subpage_spr);
    if (site_bgn != Bry_.NotFound) { // leaf arg is available
      int page_bgn = Bry_finder.Find_fwd(raw_bry, Xoa_ttl.Subpage_spr, site_bgn + 1);
      int raw_bry_len = raw_bry.length;
      if (page_bgn != Bry_.NotFound
          && page_bgn < raw_bry_len) { // pipe is found and not last char (EX: "enwiki/" is invalid
        site_bry = Bry_.Mid(raw_bry, site_bgn + 1, page_bgn);
        page_bry = Bry_.Mid(raw_bry, page_bgn + 1, raw_bry_len);
      }
    }
    Xoae_app app = wiki.Appe();
    if (Bry_.Len_gt_0(site_bry) && Bry_.Len_gt_0(page_bry))
      if (Navigate(usr_dlg, app, app.Wiki_mgr().Wdata_mgr(), page, site_bry, page_bry)) return;
    Bry_bfr tmp_bfr = wiki.Utl__bfr_mkr().Get_k004();
    html_fmtr.Bld_bfr_many(
        tmp_bfr,
        "Search for items by site and title",
        "Site",
        site_bry,
        "Page",
        page_bry,
        "Search");
    page.Data_raw_(tmp_bfr.To_bry_and_rls());
    page.Html_data().Html_restricted_n_(); // [[Special:]] pages allow all HTML
  }

  private static boolean Navigate(
      Gfo_usr_dlg usr_dlg,
      Xoae_app app,
      Wdata_wiki_mgr wdata_mgr,
      Xoae_page page,
      byte[] site_bry,
      byte[] page_bry) {
    page_bry =
        Xoa_app_.Utl__encoder_mgr()
            .Http_url()
            .Decode(page_bry); // NOTE: space is converted to + on postback to url; decode
    byte[] wiki_domain = Xow_wiki_alias.Parse_wmf_key(site_bry);
    if (wiki_domain == null) {
      usr_dlg.Warn_many("", "", "site_bry parse failed; site_bry:~{0}", String_.new_u8(site_bry));
      return false;
    }
    Xowe_wiki wiki = app.Wiki_mgr().Get_by_key_or_make(wiki_domain);
    if (wiki == null) {
      usr_dlg.Warn_many(
          "", "", "wiki_domain does not exist; wiki_domain:~{0}", String_.new_u8(wiki_domain));
      return false;
    }
    Xoa_ttl wdata_ttl = Xoa_ttl.parse_(wiki, page_bry);
    if (wdata_ttl == null) {
      usr_dlg.Warn_many("", "", "ttl is invalid; ttl:~{0}", String_.new_u8(page_bry));
      return false;
    }
    Wdata_doc doc = wdata_mgr.Pages_get(wiki, wdata_ttl);
    if (doc == null) {
      usr_dlg.Warn_many(
          "", "", "ttl cannot be found in wikidata; ttl:~{0}", String_.new_u8(wdata_ttl.Raw()));
      return false;
    }
    byte[] qid_bry = doc.Qid();
    Xoae_page qid_page = wdata_mgr.Wdata_wiki().Data_mgr().Redirect(page, qid_bry);
    if (qid_page.Missing()) {
      usr_dlg.Warn_many(
          "", "", "qid cannot be found in wikidata; qid:~{0}", String_.new_u8(qid_bry));
      return false;
    }
    return true;
  }

  private static Bry_fmtr html_fmtr =
      Bry_fmtr.new_(
          String_.Concat_lines_nl(
              "<div id=\"mw-content-text\">",
              "<form method=\"get\" action=\"//www.wikidata.org/wiki/Special:ItemByTitle\" name=\"itembytitle\" id=\"wb-itembytitle-form1\">",
              "<fieldset>",
              "<legend>~{legend}</legend>",
              "<label for=\"wb-itembytitle-sitename\">~{site_lbl}:</label>",
              "<input id=\"wb-itembytitle-sitename\" size=\"12\" name=\"site\" value=\"~{site_val}\" accesskey=\"s\" />",
              "",
              "<label for=\"pagename\">~{page_lbl}:</label>",
              "<input id=\"pagename\" size=\"36\" class=\"wb-input-text\" name=\"page\" value=\"~{page_val}\" accesskey=\"p\" />",
              "",
              "<input id=\"wb-itembytitle-submit\" class=\"wb-input-button\" type=\"submit\" value=\"~{search_lbl}\" name=\"submit\" />",
              "</fieldset>",
              "</form>",
              "</div>",
              "<br>To change the default site, see <a href='/site/home/wiki/Help:Options/Wikibase'>Help:Options/Wikibase</a>"),
          "legend",
          "site_lbl",
          "site_val",
          "page_lbl",
          "page_val",
          "search_lbl");
}
예제 #9
0
public class Xop_list_tkn_ {
  public static final byte[] Hook_ul = new byte[] {Byte_ascii.Nl, Byte_ascii.Star},
      Hook_ol = new byte[] {Byte_ascii.Nl, Byte_ascii.Hash},
      Hook_dt = new byte[] {Byte_ascii.Nl, Byte_ascii.Semic},
      Hook_dd = new byte[] {Byte_ascii.Nl, Byte_ascii.Colon};
  public static final byte List_itmTyp_null = 0,
      List_itmTyp_ul = Byte_ascii.Star,
      List_itmTyp_ol = Byte_ascii.Hash,
      List_itmTyp_dt = Byte_ascii.Semic,
      List_itmTyp_dd = Byte_ascii.Colon;
  public static final String Str_li = "li",
      Str_ol = "ol",
      Str_ul = "ul",
      Str_dl = "dl",
      Str_dt = "dt",
      Str_dd = "dd";
  public static final byte[] Byt_li = Bry_.new_a7(Str_li),
      Byt_ol = Bry_.new_a7(Str_ol),
      Byt_ul = Bry_.new_a7(Str_ul),
      Byt_dl = Bry_.new_a7(Str_dl),
      Byt_dt = Bry_.new_a7(Str_dt),
      Byt_dd = Bry_.new_a7(Str_dd);

  public static byte[] XmlTag_lst(byte b) {
    switch (b) {
      case List_itmTyp_ul:
        return Byt_ul;
      case List_itmTyp_ol:
        return Byt_ol;
      case List_itmTyp_dt:
      case List_itmTyp_dd:
        return Byt_dl;
      default:
        throw Err_.unhandled(b);
    }
  }

  public static byte[] XmlTag_itm(byte b) {
    switch (b) {
      case List_itmTyp_ul:
      case List_itmTyp_ol:
        return Byt_li;
      case List_itmTyp_dt:
        return Byt_dt;
      case List_itmTyp_dd:
        return Byt_dd;
      default:
        throw Err_.unhandled(b);
    }
  }

  public static byte Char_lst(byte b) {
    switch (b) {
      case List_itmTyp_ul:
        return Byte_ascii.Star;
      case List_itmTyp_ol:
        return Byte_ascii.Hash;
      case List_itmTyp_dt:
        return Byte_ascii.Semic;
      case List_itmTyp_dd:
        return Byte_ascii.Colon;
      default:
        throw Err_.unhandled(b);
    }
  }
}
예제 #10
0
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\"");
}
예제 #11
0
class Hiero_html_wtr {
  private Hiero_phoneme_mgr phoneme_mgr;
  private Bry_bfr temp_bfr = Bry_bfr.reset_(255);

  public Hiero_html_wtr(Hiero_html_mgr mgr, Hiero_phoneme_mgr phoneme_mgr) {
    this.phoneme_mgr = phoneme_mgr;
  }

  public void Init_for_write(Xoh_wtr_ctx hctx) {
    this.hiero_img_dir =
        hctx.Mode_is_hdump()
            ? gplx.xowa.html.hdumps.abrvs.Xohd_abrv_.Key_hiero_dir
            : Hiero_xtn_mgr.Img_src_dir;
  }

  private byte[] hiero_img_dir = null;

  public void Hr(Bry_bfr bfr) {
    bfr.Add(Html_tag_.Hr_inl).Add_byte_nl();
  }

  public void Tbl_eol(Bry_bfr bfr) {
    bfr.Add(Tbl_eol_bry);
  }

  public byte[] Td_height(int height) {
    return temp_bfr.Add(Option_bgn_bry)
        .Add_int_variable(height)
        .Add(Option_end_bry)
        .Xto_bry_and_clear();
  }

  private static final byte[] Option_bgn_bry = Bry_.new_a7("height: "),
      Option_end_bry = Bry_.new_a7("px;");

  public void Td(Bry_bfr bfr, byte[] glyph) {
    bfr.Add(Td_bgn_bry).Add(glyph).Add(Td_end_bry);
  }

  private static final byte[] Td_bgn_bry = Bry_.new_a7("\n          <td>"),
      Td_end_bry = Bry_.new_a7("\n          </td>");

  public void Cartouche_bgn(Bry_bfr bfr) {
    bfr.Add(Cartouche_bgn_lhs_bry)
        .Add_int_variable((Hiero_html_mgr.Cartouche_width * Hiero_html_mgr.scale) / 100)
        .Add(Cartouche_bgn_rhs_bry);
  }

  private static final String Tbl_bgn_str = "<table class=\"mw-hiero-table\">";
  private static final byte[]
      Cartouche_bgn_lhs_bry =
          Bry_.new_a7(
              String_.Concat_lines_nl_skip_last(
                  "",
                  "          <td>",
                  "            " + Tbl_bgn_str,
                  "              <tr>",
                  "                <td class='mw-hiero-box' style='height: ")),
      Cartouche_bgn_rhs_bry =
          Bry_.new_a7(
              String_.Concat_lines_nl_skip_last(
                  "px;'>",
                  "                </td>",
                  "              </tr>",
                  "              <tr>",
                  "                <td>",
                  "                  " + Tbl_bgn_str,
                  "                    <tr>"));

  public void Cartouche_end(Bry_bfr bfr) {
    bfr.Add(Cartouche_end_lhs_bry)
        .Add_int_variable((Hiero_html_mgr.Cartouche_width * Hiero_html_mgr.scale) / 100)
        .Add(Cartouche_end_rhs_bry);
  }

  private static final byte[]
      Cartouche_end_lhs_bry =
          Bry_.new_a7(
              String_.Concat_lines_nl_skip_last(
                  "",
                  "                    </tr>",
                  "                  </table>",
                  "                </td>",
                  "              </tr>",
                  "              <tr>",
                  "                <td class='mw-hiero-box' style='height: ")),
      Cartouche_end_rhs_bry =
          Bry_.new_a7(
              String_.Concat_lines_nl_skip_last(
                  "px;'>",
                  "                </td>",
                  "              </tr>",
                  "            </table>",
                  "          </td>"));

  public byte[] Cartouche_img(
      Xoh_wtr_ctx hctx,
      boolean bgn,
      byte[] glyph) { // render open / close cartouche; note that MW has two branches, but they are
    // both the same
    int height = (int) ((Hiero_html_mgr.Max_height * Hiero_html_mgr.scale) / 100);
    Hiero_phoneme_itm phoneme_itm = phoneme_mgr.Get_by_key(glyph);
    if (phoneme_itm == null) throw Err_.new_fmt_("missing phoneme: {0}", String_.new_u8(glyph));
    byte[] code = phoneme_itm.Gardiner_code();
    byte[] title = bgn ? Html_entity_.Lt_bry : Html_entity_.Gt_bry;
    return cartouche_img_fmtr.Bld_bry_many(temp_bfr, hiero_img_dir, code, height, title);
  }

  private static final Bry_fmtr cartouche_img_fmtr =
      Bry_fmtr.new_(
          String_.Concat(
              "\n            <img src='~{path}hiero_~{code}.png'",
              " height='~{height}' title='~{title}'",
              " alt='~{title}' />"),
          "path",
          "code",
          "height",
          "title");

  public void Tbl_inner(Bry_bfr html_bfr, Bry_bfr text_bfr) {
    html_bfr.Add(Tbl_inner_bgn)
        .Add_bfr_and_clear(text_bfr)
        .Add(Tbl_inner_end); // 	$html .= self::TABLE_START . "<tr>\n" . $tableContentHtml .
    // '</tr></table>';
  }

  private static final byte[]
      Tbl_inner_bgn =
          Bry_.new_u8(
              String_.Concat_lines_nl_skip_last(
                  "      <table class=\"mw-hiero-table\">", "        <tr>")),
      Tbl_inner_end =
          Bry_.new_u8(String_.Concat_lines_nl_skip_last("", "        </tr>", "      </table>"));

  public void Tbl_outer(Bry_bfr bfr, Bry_bfr html_bfr) {
    bfr.Add(Outer_tbl_bgn);
    bfr.Add_bfr_and_clear(html_bfr);
    bfr.Add(Outer_tbl_end);
  }

  private static final byte[]
      Outer_tbl_bgn =
          Bry_.new_a7(
              String_.Concat_lines_nl_skip_last(
                  "<table class='mw-hiero-table mw-hiero-outer' dir='ltr'>",
                  "  <tr>",
                  "    <td>",
                  "")),
      Outer_tbl_end =
          Bry_.new_a7(
              String_.Concat_lines_nl_skip_last("", "    </td>", "  </tr>", "</table>", ""));

  public byte[] Img_phoneme(byte[] img_cls, byte[] td_height, byte[] glyph_esc, byte[] code) {
    byte[] code_esc = Html_utl.Escape_html_as_bry(temp_bfr, code);
    byte[] img_title =
        temp_bfr.Add(code_esc)
            .Add_byte_space()
            .Add_byte(Byte_ascii.Brack_bgn)
            .Add(glyph_esc)
            .Add_byte(Byte_ascii.Brack_end)
            .Xto_bry_and_clear(); // "~{code} [~{glyph}]"
    return Img(img_cls, td_height, glyph_esc, code_esc, img_title);
  }

  public byte[] Img_file(byte[] img_cls, byte[] td_height, byte[] glyph_esc) {
    return Img(img_cls, td_height, glyph_esc, glyph_esc, glyph_esc);
  }

  private byte[] Img(
      byte[] img_cls, byte[] td_height, byte[] glyph, byte[] img_src_name, byte[] img_title) {
    byte[] img_src = Bld_img_src(hiero_img_dir, img_src_name);
    return glyph_img_fmtr.Bld_bry_many(
        temp_bfr, img_cls, Hiero_html_mgr.Image_margin, td_height, img_src, img_title, glyph);
  }

  private static final byte[] Tbl_eol_bry =
      Bry_.new_a7(
          String_.Concat_lines_nl_skip_last(
              "", "        </tr>", "      </table>", "      " + Tbl_bgn_str, "        <tr>"));
  private static final Bry_fmtr glyph_img_fmtr =
      Bry_fmtr.new_(
          "\n            <img ~{img_cls}style='margin: ~{img_margin}px; ~{option}' src='~{img_src}' title='~{img_title}' alt='~{glyph}' />",
          "img_cls",
          "img_margin",
          "option",
          "img_src",
          "img_title",
          "glyph");

  public byte[] Void(boolean half) { // render void
    int width = Hiero_html_mgr.Max_height;
    if (half) width /= 2;
    return void_fmtr.Bld_bry_many(temp_bfr, width);
  }

  private static final Bry_fmtr void_fmtr =
      Bry_fmtr.new_(
          String_.Concat_lines_nl_skip_last(
              "",
              "            <table class=\"mw-hiero-table\" style=\"width: ~{width}px;\">",
              "              <tr>",
              "                <td>&#160;",
              "                </td>",
              "              </tr>",
              "            </table>"),
          "width");

  private static byte[] Bld_img_src(byte[] hiero_img_dir, byte[] name) {
    return Bry_.Add(hiero_img_dir, Img_src_prefix, name, Img_src_ext);
  }

  private static final byte[] Img_src_prefix = Bry_.new_a7("hiero_"),
      Img_src_ext = Bry_.new_a7(".png");
}
public class Xoa_css_img_downloader {
  private byte[] wiki_domain;

  public Xoa_css_img_downloader Ctor(
      Gfo_usr_dlg usr_dlg, Xof_download_wkr download_wkr, byte[] stylesheet_prefix) {
    this.usr_dlg = usr_dlg;
    this.download_wkr = download_wkr;
    this.stylesheet_prefix = stylesheet_prefix;
    return this;
  }

  private Gfo_usr_dlg usr_dlg;
  private Xof_download_wkr download_wkr;

  public Xoa_css_img_downloader Stylesheet_prefix_(byte[] v) {
    stylesheet_prefix = v;
    return this;
  }

  private byte[]
      stylesheet_prefix; // TEST: setter exposed b/c tests can handle "mem/" but not "//mem"

  public void Chk(byte[] wiki_domain, Io_url css_fil) {
    this.wiki_domain = wiki_domain;
    List_adp img_list = List_adp_.New();
    byte[] old_bry = Io_mgr.Instance.LoadFilBry(css_fil);
    byte[] rel_url_prefix = Bry_.Add(Bry_fwd_slashes, wiki_domain);
    byte[] new_bry = Convert_to_local_urls(rel_url_prefix, old_bry, img_list);
    Io_url img_dir = css_fil.OwnerDir();
    Download_fils(img_dir, img_list.To_str_ary());
    Io_mgr.Instance.SaveFilBry(css_fil, new_bry);
  }

  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 static byte[] Import_url_build(
      byte[] stylesheet_prefix, byte[] rel_url_prefix, byte[] css_url) {
    return Bry_.Has_at_bgn(
            css_url,
            Bry_http_protocol) // css_url already starts with "http"; return self;
                               // PAGE:tr.n:Main_Page; DATE:2014-06-04
        ? css_url
        : Bry_.Add(stylesheet_prefix, css_url);
  }

  private int Import_url_chk(
      byte[] rel_url_prefix,
      byte[] src,
      int src_len,
      int old_pos,
      int find_bgn,
      byte[] url_raw,
      Bry_bfr bfr) {
    if (find_bgn < Bry_import_len) return Bry_find_.Not_found;
    if (!Bry_.Match(src, find_bgn - Bry_import_len, find_bgn, Bry_import))
      return Bry_find_.Not_found;
    byte[] css_url = url_raw;
    int css_url_len = css_url.length;
    if (css_url_len > 0
        && css_url[0]
            == Byte_ascii
                .Slash) { // css_url starts with "/"; EX: "/page" or "//site/page" DATE:2014-02-03
      if (css_url_len > 1
          && css_url[1] != Byte_ascii.Slash) // skip if css_url starts with "//"; EX: "//site/page"
      css_url = Bry_.Add(rel_url_prefix, css_url); // "/w/a.css" -> "//en.wikipedia.org/w/a.css"
    }
    css_url =
        Bry_.Replace(
            css_url,
            Byte_ascii.Space,
            Byte_ascii
                .Underline); // NOTE: must replace spaces with underlines else download will fail;
                             // EX:https://it.wikivoyage.org/w/index.php?title=MediaWiki:Container e
                             // Infobox.css&action=raw&ctype=text/css; DATE:2015-03-08
    byte[] css_src_bry = Import_url_build(stylesheet_prefix, rel_url_prefix, css_url);
    String css_src_str = String_.new_u8(css_src_bry);
    download_wkr.Download_xrg()
        .Prog_fmt_hdr_(
            usr_dlg.Log_many(
                GRP_KEY, "logo.download", "downloading import for '~{0}'", css_src_str));
    byte[] css_trg_bry = download_wkr.Download_xrg().Exec_as_bry(css_src_str);
    if (css_trg_bry == null) {
      usr_dlg.Warn_many("", "", "could not import css: url=~{0}", css_src_str);
      return Bry_find_.Not_found; // css not found
    }
    bfr.Add_mid(src, old_pos, find_bgn - Bry_import_len).Add_byte_nl();
    bfr.Add(Bry_comment_bgn).Add(css_url).Add(Bry_comment_end).Add_byte_nl();
    if (Bry_find_.Find_fwd(css_url, Wikisource_dynimg_ttl) != -1)
      css_trg_bry =
          Bry_.Replace(
              css_trg_bry,
              Wikisource_dynimg_find,
              Wikisource_dynimg_repl); // FreedImg hack;
                                       // PAGE:en.s:Page:Notes_on_Osteology_of_Baptanodon._With_a_Description_of_a_New_Species.pdf/3 DATE:2014-09-06
    bfr.Add(css_trg_bry).Add_byte_nl();
    bfr.Add_byte_nl();
    int semic_pos = Bry_find_.Find_fwd(src, Byte_ascii.Semic, find_bgn + url_raw.length, src_len);
    return semic_pos + Int_.Const_dlm_len;
  }

  private static final byte[]
      Wikisource_dynimg_ttl =
          Bry_.new_a7("en.wikisource.org/w/index.php?title=MediaWiki:Dynimg.css"),
      Wikisource_dynimg_find =
          Bry_.new_a7(
              ".freedImg img[src*=\"wikipedia\"], .freedImg img[src*=\"wikisource\"], .freedImg img[src*=\"score\"], .freedImg img[src*=\"math\"] {"),
      Wikisource_dynimg_repl =
          Bry_.new_a7(
              ".freedImg img[src*=\"wikipedia\"], .freedImg img[src*=\"wikisource\"], /*XOWA:handle file:// paths which will have /commons.wikimedia.org/ but not /wikipedia/ */ .freedImg img[src*=\"wikimedia\"], .freedImg img[src*=\"score\"], .freedImg img[src*=\"math\"] {");

  public byte[] Clean_img_url(byte[] raw, int raw_len) {
    int pos_bgn = 0;
    if (Bry_.Has_at_bgn(raw, Bry_fwd_slashes, 0, raw_len)) pos_bgn = Bry_fwd_slashes.length;
    if (Bry_.Has_at_bgn(raw, Bry_http, 0, raw_len)) pos_bgn = Bry_http.length;
    int pos_slash = Bry_find_.Find_fwd(raw, Byte_ascii.Slash, pos_bgn, raw_len);
    if (pos_slash == Bry_find_.Not_found)
      return null; // first segment is site_name; at least one slash must be present for image name;
                   // EX: site.org/img_name.jpg
    if (pos_slash == raw_len - 1) return null; // "site.org/" is invalid
    int pos_end = raw_len;
    int pos_question = Bry_find_.Find_bwd(raw, Byte_ascii.Question);
    if (pos_question != Bry_find_.Not_found)
      pos_end = pos_question; // remove query params; EX: img_name?key=val
    return Bry_.Mid(raw, pos_bgn, pos_end);
  }

  private void Download_fils(Io_url css_dir, String[] ary) {
    int ary_len = ary.length;
    for (int i = 0; i < ary_len; i++) {
      String src = ary[i];
      Io_url trg =
          css_dir.GenSubFil_nest(
              Op_sys.Cur().Fsys_http_frag_to_url_str(Replace_invalid_chars_str(src)));
      if (Io_mgr.Instance.ExistsFil(trg)) continue;
      download_wkr.Download(true, "https://" + src, trg, "download: " + src); // ILN
      if (Io_mgr.Instance.QueryFil(trg).Size()
          == 0) { // warn if 0 byte files downloaded; DATE:2015-07-06
        Xoa_app_.Usr_dlg()
            .Warn_many("", "", "css.download; 0 byte file downloaded; file=~{0}", trg.Raw());
      }
    }
  }

  String Replace_invalid_chars_str(String raw_str) {
    return String_.new_u8(Replace_invalid_chars(Bry_.new_u8(raw_str)));
  }

  byte[] Replace_invalid_chars(byte[] raw_bry) {
    int raw_len = raw_bry.length;
    for (int i = 0; i < raw_len; i++) { // convert invalid wnt chars to underscores
      byte b = raw_bry[i];
      switch (b) {
          // case Byte_ascii.Slash:
        case Byte_ascii.Backslash:
        case Byte_ascii.Colon:
        case Byte_ascii.Star:
        case Byte_ascii.Question:
        case Byte_ascii.Quote:
        case Byte_ascii.Lt:
        case Byte_ascii.Gt:
        case Byte_ascii.Pipe:
          raw_bry[i] = Byte_ascii.Underline;
          break;
      }
    }
    return raw_bry;
  }

  private static final byte[] Bry_url = Bry_.new_a7("url("),
      Bry_data_image = Bry_.new_a7("data:image/"),
      Bry_http = Bry_.new_a7("http://"),
      Bry_fwd_slashes = Bry_.new_a7("//"),
      Bry_import = Bry_.new_a7("@import "),
      Bry_http_protocol = Bry_.new_a7("http");
  public static final byte[] Bry_comment_bgn = Bry_.new_a7("/*XOWA:"),
      Bry_comment_end = Bry_.new_a7("*/");
  private static final int Bry_url_len = Bry_url.length, Bry_import_len = Bry_import.length;
  static final String GRP_KEY = "xowa.wikis.init.css";
}
예제 #13
0
public class Xoh_img_data implements Xoh_data_itm {
  public int Tid() {
    return Xoh_hzip_dict_.Tid__img;
  }

  public int Src_bgn() {
    return src_bgn;
  }

  private int src_bgn;

  public int Src_end() {
    return src_end;
  }

  private int src_end;

  public Xoh_anch_href_data Anch_href() {
    return anch_href;
  }

  private Xoh_anch_href_data anch_href = new Xoh_anch_href_data();

  public Xoh_anch_cls_data Anch_cls() {
    return anch_cls;
  }

  private Xoh_anch_cls_data anch_cls = new Xoh_anch_cls_data();

  public boolean Anch_rel_nofollow_exists() {
    return anch_rel_is_nofollow;
  }

  private boolean anch_rel_is_nofollow;

  public int Anch_title_bgn() {
    return anch_title_bgn;
  }

  private int anch_title_bgn;

  public int Anch_title_end() {
    return anch_title_end;
  }

  private int anch_title_end;

  public boolean Anch_title_exists() {
    return anch_title_end != -1;
  }

  public Bry_obj_ref Anch_xo_ttl() {
    return anch_xo_ttl;
  }

  private Bry_obj_ref anch_xo_ttl = Bry_obj_ref.New_empty();

  public Xoh_img_src_data Img_src() {
    return img_src;
  }

  private final Xoh_img_src_data img_src = new Xoh_img_src_data();

  public Xoh_img_cls_data Img_cls() {
    return img_cls;
  }

  private final Xoh_img_cls_data img_cls = new Xoh_img_cls_data();

  public Xoh_img_xoimg_data Img_xoimg() {
    return img_xoimg;
  }

  private final Xoh_img_xoimg_data img_xoimg = new Xoh_img_xoimg_data();

  public int Img_alt_bgn() {
    return img_alt_bgn;
  }

  private int img_alt_bgn;

  public int Img_alt_end() {
    return img_alt_end;
  }

  private int img_alt_end;

  public boolean Img_alt__diff__anch_title() {
    return img_alt__diff_anch_title;
  }

  private boolean img_alt__diff_anch_title;

  public int Img_w() {
    return img_w;
  }

  private int img_w;

  public int Img_h() {
    return img_h;
  }

  private int img_h;

  public boolean Img_w__diff__file_w() {
    return !img_src.File_is_orig() && img_w != img_src.File_w();
  }

  public boolean Img_is_vid() {
    return img_is_vid;
  }

  private boolean img_is_vid;

  public boolean Img_wo_anch() {
    return img_wo_anch;
  }

  private boolean img_wo_anch;

  public int Img_imap_idx() {
    return img_imap_idx;
  }

  private int img_imap_idx;

  public boolean Img_is_gallery() {
    return img_is_gallery;
  }

  private boolean img_is_gallery;

  public void Img_is_gallery_(boolean v) {
    this.img_is_gallery = v;
  }

  public Pgbnr_itm Img_pgbnr() {
    return img_pgbnr;
  }

  private final Pgbnr_itm img_pgbnr = new Pgbnr_itm();

  public void Clear() {
    this.img_alt__diff_anch_title =
        anch_rel_is_nofollow = img_is_vid = img_wo_anch = img_is_gallery = false;
    this.src_bgn =
        src_end = anch_title_bgn = anch_title_end = img_w = img_h = img_alt_bgn = img_alt_end = -1;
    this.img_imap_idx = -1;
    anch_href.Clear();
    anch_cls.Clear();
    img_src.Clear();
    img_cls.Clear();
    img_xoimg.Clear();
    img_pgbnr.Clear_by_hdump();
  }

  public boolean Init_by_parse(
      Xoh_hdoc_wkr hdoc_wkr,
      Xoh_hdoc_ctx hctx,
      Gfh_tag_rdr tag_rdr,
      byte[] src,
      Gfh_tag anch_head,
      Gfh_tag unused) {
    Gfh_tag img_tag = anch_head;
    Bry_err_wkr err_wkr = tag_rdr.Err_wkr();
    this.img_wo_anch = anch_head.Name_id() == Gfh_tag_.Id__img;
    if (img_wo_anch) {
      Gfh_atr xowa_title =
          anch_head.Atrs__get_by_or_empty(
              Xoh_img_xoimg_data.Bry__data_xowa_title); // data-xowa-title='A.png'
      anch_xo_ttl.Val_(xowa_title.Val());
    } else {
      if (anch_head.Name_id() == Gfh_tag_.Id__div) { // video / audio
        if (!anch_head.Atrs__cls_eq(
            gplx.xowa.htmls.core.wkrs.thms.Xoh_thm_data.Atr__id__xowa_media_div))
          return false; // handle fake-thumbs created through en.w:Template:Image_label_begin;
                        // PAGE:en.w:Blackburnshire; DATE:2016-01-04
        img_is_vid = true;
        tag_rdr.Tag__move_fwd_head(); // next <div>
        anch_head = tag_rdr.Tag__move_fwd_head(); // next <div>
      }
      this.src_bgn = anch_head.Src_bgn(); // <a
      if (!anch_href.Parse(err_wkr, hctx, src, anch_head)) return false; // href='/wiki/File:A.png'
      if (!anch_cls.Parse(err_wkr, src, anch_head)) return false; // class='image'
      Gfh_atr anch_title = anch_head.Atrs__get_by_or_empty(Gfh_atr_.Bry__title); // title='abc'
      anch_title_bgn = anch_title.Val_bgn();
      anch_title_end = anch_title.Val_end();
      Gfh_atr xowa_title =
          anch_head.Atrs__get_by_or_empty(Bry__atr__xowa_title); // xowa_title='A.png'
      if (xowa_title.Val_dat_exists()) anch_xo_ttl.Val_(xowa_title.Val());
      img_tag = tag_rdr.Tag__move_fwd_head();
    }
    img_tag.Chk_name_or_fail(Gfh_tag_.Id__img); // <img
    if (img_tag.Atrs__cls_has(
        gplx.xowa.xtns.pagebanners.Pgbnr_xtn_mgr
            .Bry__cls__wpb_banner_image)) { // handle pagebanner; EX: <img class="wpb-banner-image">
      img_pgbnr.Init_by_parse(img_tag);
    }
    img_xoimg.Parse(err_wkr, src, img_tag); // data-xoimg='...'
    this.img_w =
        img_tag.Atrs__get_as_int_or(Gfh_atr_.Bry__width, Xof_img_size.Size__neg1); // width='220'
    this.img_h =
        img_tag.Atrs__get_as_int_or(Gfh_atr_.Bry__height, Xof_img_size.Size__neg1); // height='110'
    Gfh_atr img_alt = img_tag.Atrs__get_by_or_empty(Gfh_atr_.Bry__alt); // alt='File:A.png'
    img_alt_bgn = img_alt.Val_bgn();
    img_alt_end = img_alt.Val_end();
    img_cls.Init_by_parse(err_wkr, src, img_tag); // class='thumbborder'
    img_alt__diff_anch_title =
        !Bry_.Match(src, img_alt_bgn, img_alt_end, src, anch_title_bgn, anch_title_end);
    if (!img_src.Parse(err_wkr, hctx, hctx.Wiki__domain_bry(), img_tag)) return false; // src='...'
    if (anch_xo_ttl.Val_is_empty()) {
      anch_xo_ttl.Val_(img_src.File_ttl_bry());
      if (anch_xo_ttl.Val_is_empty()) anch_xo_ttl.Val_(anch_href.Ttl_page_db());
    }
    this.img_imap_idx = Get_imap_idx(tag_rdr.Err_wkr(), img_tag);
    if (img_wo_anch) {
      src_end = img_tag.Src_end();
      return true;
    }
    Gfh_tag anch_tail = tag_rdr.Tag__move_fwd_tail(Gfh_tag_.Id__a); // </a>
    this.src_end = anch_tail.Src_end();
    if (anch_href.Site_exists()) {
      Xow_domain_itm itm =
          Xow_domain_itm_.parse(Bry_.Mid(src, anch_href.Site_bgn(), anch_href.Site_end()));
      anch_rel_is_nofollow = itm.Domain_type_id() == Xow_domain_tid_.Tid__other;
    }
    if (img_is_vid) {
      tag_rdr.Tag__move_fwd_tail(Gfh_tag_.Id__div);
      anch_head = tag_rdr.Tag__move_fwd_tail(Gfh_tag_.Id__div);
      src_end = anch_head.Src_end();
    }
    return true;
  }

  public void Init_by_decode(
      boolean anch_rel_is_nofollow,
      int anch_title_bgn,
      int anch_title_end,
      boolean img_wo_anch,
      boolean img_is_vid,
      int img_w,
      int img_h,
      int img_alt_bgn,
      int img_alt_end,
      int img_imap_idx) {
    this.anch_rel_is_nofollow = anch_rel_is_nofollow;
    this.anch_title_bgn = anch_title_bgn;
    this.anch_title_end = anch_title_end;
    this.img_wo_anch = img_wo_anch;
    this.img_is_vid = img_is_vid;
    this.img_w = img_w;
    this.img_h = img_h;
    this.img_alt_bgn = img_alt_bgn;
    this.img_alt_end = img_alt_end;
    this.img_imap_idx = img_imap_idx;
  }

  public void Pool__rls() {
    pool_mgr.Rls_fast(pool_idx);
  }

  private Gfo_poolable_mgr pool_mgr;
  private int pool_idx;

  public Gfo_poolable_itm Pool__make(Gfo_poolable_mgr mgr, int idx, Object[] args) {
    Xoh_img_data rv = new Xoh_img_data();
    rv.pool_mgr = mgr;
    rv.pool_idx = idx;
    return rv;
  }

  private static int Get_imap_idx(Bry_err_wkr err_wkr, Gfh_tag img_tag) {
    byte[] val = img_tag.Atrs__get_as_bry(Imap_xtn_mgr.Bry__usemap__name);
    if (Bry_.Len_eq_0(val)) return -1;
    int rv = Bry_.To_int_or(val, Imap_xtn_mgr.Bry__usemap__prefix.length, val.length, -1);
    if (rv == -1) {
      err_wkr.Warn("invalid imap_idx", "val", val);
      return rv;
    }
    return rv;
  }

  public static final byte[] Bry__cls__anch__image = Bry_.new_a7("image"),
      Bry__cls__img__thumbimage = Bry_.new_a7("thumbimage"),
      Bry__atr__xowa_title = Bry_.new_a7("xowa_title");
}
예제 #14
0
 public byte[] Format_num_by_long(long val) {
   return Format_num(Bry_.new_a7(Long_.To_str(val)));
 }
예제 #15
0
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();
    }
  }
}
예제 #16
0
 public byte[] Format_num(int val) {
   return Format_num(Bry_.new_a7(Int_.To_str(val)));
 }
예제 #17
0
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};
}
예제 #18
0
class Xow_search_scanner {
  private final List_adp tkns = List_adp_.new_();
  private byte[] src;
  private int src_len, pos, txt_bgn;
  private final Ordered_hash tmp_list = Ordered_hash_.new_();
  private final Bry_bfr tmp_bfr = Bry_bfr.new_();

  public Xow_search_tkn[] Scan(byte[] src) {
    this.src = src;
    this.src_len = src.length;
    tkns.Clear();
    pos = 0;
    txt_bgn = -1;
    while (pos < src_len) {
      byte cur_b = src[pos];
      Object cur_obj = trie.Match_bgn_w_byte(cur_b, src, pos, src_len);
      if (cur_obj == null) { // text character
        if (txt_bgn == -1) txt_bgn = pos; // 1st character not set; set it
        ++pos;
      } else { // AND, OR, (, ), -, \s, "
        int pos_end = trie.Match_pos();
        byte cur_tid = ((Byte_obj_val) cur_obj).Val();
        if (Cur_join_is_word(cur_tid, pos_end))
          continue; // ignore words containing "and", "or"; EX: "random"; "for"
        if (txt_bgn != -1) { // pending word; create
          Tkns_add_word(Xow_search_tkn.Tid_word, txt_bgn, pos);
          txt_bgn = -1;
        }
        switch (cur_tid) {
          case Xow_search_tkn.Tid_space: // discard spaces
            pos = Bry_finder.Find_fwd_while(src, pos, src_len, Byte_ascii.Space);
            break;
          case Xow_search_tkn.Tid_quote: // find end quote and add as word
            int quote_bgn = pos + 1;
            int quote_end = Bry_finder.Find_fwd(src, Byte_ascii.Quote, quote_bgn, src_len);
            if (quote_end == Bry_.NotFound)
              throw Err_.new_fmt_("could not find end quote: {0}", String_.new_u8(src));
            Tkns_add_word(Xow_search_tkn.Tid_word_quoted, quote_bgn, quote_end);
            pos = quote_end + 1; // +1 to place after quote
            break;
          case Xow_search_tkn.Tid_not:
            Tkns_add_word(Xow_search_tkn.Tid_not, pos, pos_end);
            pos = pos_end;
            break;
          case Xow_search_tkn.Tid_paren_bgn:
          case Xow_search_tkn.Tid_paren_end:
          case Xow_search_tkn.Tid_and:
          case Xow_search_tkn.Tid_or:
            tkns.Add(new_tkn(cur_tid, pos, pos_end));
            pos = pos_end;
            break;
          default:
            throw Err_.unhandled(cur_tid);
        }
      }
    }
    if (txt_bgn != -1) { // pending word; create
      Tkns_add_word(Xow_search_tkn.Tid_word, txt_bgn, pos);
      txt_bgn = -1;
    }
    return (Xow_search_tkn[]) tkns.To_ary_and_clear(Xow_search_tkn.class);
  }

  private boolean Cur_join_is_word(
      byte cur_tid,
      int
          pos_end) { // extra logic to handle and / or occuring in unquoted strings; EX: "random";
                     // "for"
    switch (cur_tid) {
      default:
        return false; // only look at AND, OR, -
      case Xow_search_tkn.Tid_and:
      case Xow_search_tkn.Tid_or:
      case Xow_search_tkn.Tid_not:
        break;
    }
    boolean join_is_word = true;
    if (txt_bgn == -1) { // no pending word;
      if (cur_tid == Xow_search_tkn.Tid_not)
        return false; // NOT is only operator if no pending tkn; EX: -abc -> NOT abc; a-b -> a-b
      byte nxt_b = pos_end < src_len ? src[pos_end] : Byte_ascii.Nil;
      Object nxt_obj = trie.Match_bgn_w_byte(nxt_b, src, pos_end, src_len);
      if (nxt_obj == null) // next tkn is text; join must be word
      join_is_word = true;
      else { // next tkn is tkn
        byte nxt_tid = ((Byte_obj_val) nxt_obj).Val();
        switch (nxt_tid) {
          case Xow_search_tkn.Tid_space:
          case Xow_search_tkn.Tid_quote:
          case Xow_search_tkn.Tid_paren_bgn:
          case Xow_search_tkn.Tid_paren_end:
            join_is_word =
                false; // next tkn is sym; and/or is not word; EX: a AND ; a AND"b"; a AND(b)
            break;
          case Xow_search_tkn.Tid_not:
          case Xow_search_tkn.Tid_and:
          case Xow_search_tkn.Tid_or:
            join_is_word = true; // next tkn is and or not; and/or is word; EX: andor; oror; or-abc;
            break;
          default:
            throw Err_.unhandled(cur_tid);
        }
      }
    } else { // pending word; cur join must be word; EX: "grand": "and" invoked and "gr" pending
      join_is_word = true;
    }
    if (join_is_word) {
      if (txt_bgn == -1) txt_bgn = pos; // 1st character not set; set it
      pos = pos_end;
      return true;
    }
    if (txt_bgn != -1) {
      Tkns_add_word(Xow_search_tkn.Tid_word, txt_bgn, pos); // create word
      txt_bgn = -1;
    }
    return false;
  }

  private void Tkns_add_word(byte tid, int src_bgn, int src_end) {
    if (tkns.Count() > 0) { // at least 1 tkn; check for "auto-and"
      Xow_search_tkn last_tkn = (Xow_search_tkn) tkns.Get_at_last();
      if (last_tkn.Tid()
          == Xow_search_tkn.Tid_word) // previous tkn is word; auto "AND" words; EX: A B -> A AND B
      tkns.Add(Xow_search_tkn.new_bry(Xow_search_tkn.Tid_and, Bry_and));
    }
    if (tid
        == Xow_search_tkn
            .Tid_word) { // if word has symbol, convert to quoted; EX: a-b should become "a-b";
                         // otherwise searcher would search for a single word a-b
      byte[] cur_word = Bry_.Mid(src, src_bgn, src_end);
      byte[][] words =
          gplx.xowa.bldrs.cmds.texts.Xob_search_base.Split_ttl_into_words(
              null, tmp_list, tmp_bfr, cur_word);
      int words_len = words.length;
      if (words_len == 1 // only one word
          && !Bry_.Eq(words[0], cur_word) // split word not same as raw
          && Bry_finder.Find_fwd(cur_word, Byte_ascii.Star) == -1 // no asterisk
      ) {
        tkns.Add(Xow_search_tkn.new_bry(tid, words[0]));
        return;
      }
      if (words.length > 1) // multiple words; add as quoted-term; EX: "a-b"
      tid = Xow_search_tkn.Tid_word_quoted;
    }
    tkns.Add(new_tkn(tid, src_bgn, src_end));
  }

  private Xow_search_tkn new_tkn(byte tid, int val_bgn, int val_end) {
    return Xow_search_tkn.new_pos(tid, val_bgn, val_end);
  }

  private static final byte[] Bry_and = Bry_.new_a7("AND");
  private static final Btrie_slim_mgr trie =
      Btrie_slim_mgr.ci_ascii_() // NOTE:ci.ascii:OR / AND only
          .Add_str_byte(" ", Xow_search_tkn.Tid_space)
          .Add_str_byte("\"", Xow_search_tkn.Tid_quote)
          .Add_str_byte("-", Xow_search_tkn.Tid_not)
          .Add_str_byte("(", Xow_search_tkn.Tid_paren_bgn)
          .Add_str_byte(")", Xow_search_tkn.Tid_paren_end)
          .Add_str_byte("or", Xow_search_tkn.Tid_or)
          .Add_str_byte("and", Xow_search_tkn.Tid_and);
}
예제 #19
0
public class Dpl_xnde implements Xox_xnde {
  private Dpl_itm itm = new Dpl_itm();
  private List_adp pages = List_adp_.New();

  public void Xatr__set(
      Xowe_wiki wiki,
      byte[] src,
      Mwh_atr_itm xatr,
      Object xatr_id_obj) {} // NOTE: <dynamicPageList> has no attributes

  public void Xtn_parse(
      Xowe_wiki wiki, Xop_ctx ctx, Xop_root_tkn root, byte[] src, Xop_xnde_tkn xnde) {
    itm.Parse(wiki, ctx, ctx.Page().Ttl().Full_txt_w_ttl_case(), src, xnde);
    Dpl_page_finder.Find_pages(pages, wiki, itm);
    if (itm.Sort_ascending() != Bool_.__byte) pages.Sort_by(new Dpl_page_sorter(itm));
  }

  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();
    }
  }

  private static byte[] Bry_nofollow = Bry_.new_a7(" rel=\"nofollow\"");
}
예제 #20
0
 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;"));
 }
예제 #21
0
public class Xoa_app_mode {
  Xoa_app_mode(int tid) {
    this.tid = tid;
  }

  public int Tid() {
    return tid;
  }

  private final int tid;

  public boolean Tid_is_gui() {
    return tid == Tid_gui;
  }

  public boolean Tid_is_tcp() {
    return tid == Tid_tcp;
  }

  public boolean Tid_is_cmd() {
    return tid == Tid_cmd;
  }

  public boolean Tid_is_http() {
    return tid == Tid_http;
  }

  public boolean Tid_supports_js() {
    switch (tid) {
      case Tid_gui:
      case Tid_tcp:
        return true;
      default:
        return false;
    }
  }

  public byte[] Name() {
    switch (tid) {
      case Tid_cmd:
        return Key_cmd;
      case Tid_gui:
        return Key_gui;
      case Tid_http:
        return Key_http;
      case Tid_tcp:
        return Key_tcp;
      case Tid_file:
        return Key_file;
      default:
        return Key_cmd; // see parse
    }
  }

  private static final int Tid_cmd = 1, Tid_gui = 2, Tid_tcp = 3, Tid_http = 4, Tid_file = 5;
  private static final byte[] Key_cmd = Bry_.new_a7("cmd"),
      Key_gui = Bry_.new_a7("gui"),
      Key_tcp = Bry_.new_a7("server"),
      Key_http = Bry_.new_a7("http_server"),
      Key_file = Bry_.new_a7("file");
  public static final Xoa_app_mode Itm_cmd = new Xoa_app_mode(Tid_cmd),
      Itm_gui = new Xoa_app_mode(Tid_gui),
      Itm_tcp = new Xoa_app_mode(Tid_tcp),
      Itm_http = new Xoa_app_mode(Tid_http),
      Itm_file = new Xoa_app_mode(Tid_file);
  private static final Hash_adp_bry type_hash =
      Hash_adp_bry.cs()
          .Add_bry_int(Key_cmd, Tid_cmd)
          .Add_bry_int(Key_gui, Tid_gui)
          .Add_bry_int(Key_http, Tid_http)
          .Add_bry_int(Key_tcp, Tid_tcp)
          .Add_bry_int(Key_file, Tid_file);

  public static Xoa_app_mode parse(String s) {
    Object o = type_hash.Get_by(Bry_.new_u8(s));
    if (o == null)
      return Itm_cmd; // default to cmd as per early behaviour; handles mistaken "--app_mode wrong"
    int tid = ((Int_obj_val) o).Val();
    switch (tid) {
      case Tid_cmd:
        return Itm_cmd;
      case Tid_gui:
        return Itm_gui;
      case Tid_http:
        return Itm_http;
      case Tid_tcp:
        return Itm_tcp;
      case Tid_file:
        return Itm_file;
      default:
        throw Err_.new_unhandled(tid);
    }
  }
}