1
0
mirror of https://github.com/gnosygnu/xowa.git synced 2024-10-27 20:34:16 +00:00

Mw_parse: Add more implementation for thumbnails

This commit is contained in:
gnosygnu 2017-02-06 16:52:40 -05:00
parent b30f10b647
commit 6f9e92afff
23 changed files with 1236 additions and 1071 deletions

View File

@ -20,4 +20,19 @@ public class Php_utl_ {
public static boolean Empty(byte[] v) {return v == null || v.length == 0;} public static boolean Empty(byte[] v) {return v == null || v.length == 0;}
public static boolean Empty(boolean v) {return v == false;} public static boolean Empty(boolean v) {return v == false;}
public static boolean Is_set(byte[] v) {return v != null;} public static boolean Is_set(byte[] v) {return v != null;}
public static boolean isnumeric(byte[] src) {
if (src == null) return false;
int len = src.length;
for (int i = 0; i < len; i++) {
byte b = src[i];
switch (b) {
case Byte_ascii.Num_0: case Byte_ascii.Num_1: case Byte_ascii.Num_2: case Byte_ascii.Num_3: case Byte_ascii.Num_4:
case Byte_ascii.Num_5: case Byte_ascii.Num_6: case Byte_ascii.Num_7: case Byte_ascii.Num_8: case Byte_ascii.Num_9:
break;
default:
return false;
}
}
return true;
}
} }

View File

@ -239,14 +239,31 @@ public class Xomw_MagicWordArray {
* *
* @return array * @return array
*/ */
public byte[] matchVariableStartToEnd(byte[] src) { public void matchVariableStartToEnd(byte[][] rv, byte[] src) {
int src_end = src.length; int src_end = src.length;
if (src_end == 0) {
rv[0] = rv[1] = null;
return;
}
byte[] name = null;
int val_bgn = -1, val_end = -1;
// check fwd; EX: "thumb=$1" // check fwd; EX: "thumb=$1"
if (fwd_trie != null) { if (fwd_trie != null) {
Object o = fwd_trie.Match_at(trv, src, 0, src_end); Object o = fwd_trie.Match_at(trv, src, 0, src_end);
if (o != null) { if (o != null) {
return ((Xomw_MagicWordSynonym)o).magic_name; Xomw_MagicWordSynonym syn = ((Xomw_MagicWordSynonym)o);
name = syn.magic_name;
val_bgn = trv.Pos();
val_end = src_end;
// if "nil", then must be full match; EX: "thumbx" does not match "thumb"
if (syn.arg1_tid == Xomw_MagicWordSynonym.Arg1__nil
&& syn.text_wo_arg1.length != src_end) {
rv[0] = rv[1] = null;
return;
}
} }
} }
@ -254,20 +271,15 @@ public class Xomw_MagicWordArray {
if (bwd_trie != null) { if (bwd_trie != null) {
Object o = bwd_trie.Match_at(trv, src, src_end - 1, -1); Object o = bwd_trie.Match_at(trv, src, src_end - 1, -1);
if (o != null) { if (o != null) {
return ((Xomw_MagicWordSynonym)o).magic_name; Xomw_MagicWordSynonym syn = ((Xomw_MagicWordSynonym)o);
name = syn.magic_name;
val_bgn = 0;
val_end = src_end - syn.text_wo_arg1.length;
} }
} }
return null;
// regexes = this->getVariableStartToEndRegex(); rv[0] = name;
// foreach (regexes as regex) { rv[1] = val_end - val_bgn == 0 ? Bry_.Empty : Bry_.Mid(src, val_bgn, val_end);
// if (regex !== '') {
// m = [];
// if (preg_match(regex, text, m)) {
// return this->parseMatch(m);
// }
// }
// }
// return [ false, false ];
} }
// /** // /**

View File

@ -20,27 +20,30 @@ import org.junit.*; import gplx.core.tests.*;
public class Xomw_MagicWordArray__tst { public class Xomw_MagicWordArray__tst {
private final Xomw_MagicWordArray__fxt fxt = new Xomw_MagicWordArray__fxt(); private final Xomw_MagicWordArray__fxt fxt = new Xomw_MagicWordArray__fxt();
@Test public void Nil() { @Test public void Nil() {
fxt.Init__word(Bool_.Y, "nil", "nil"); fxt.Init__word(Bool_.Y, "img_nil", "nil");
fxt.Init__ary("nil"); fxt.Init__ary("img_nil");
fxt.Test__matchVariableStartToEnd("nil", "nil", "nila"); fxt.Test__matchVariableStartToEnd("nil", "img_nil", "");
fxt.Test__matchVariableStartToEnd("nila", null, null);
} }
@Test public void Bgn() { @Test public void Bgn() {
fxt.Init__word(Bool_.Y, "bgn", "bgn$1"); fxt.Init__word(Bool_.Y, "img_bgn", "bgn$1");
fxt.Init__ary("bgn"); fxt.Init__ary("img_bgn");
fxt.Test__matchVariableStartToEnd("bgn", "bgn", "bgna"); fxt.Test__matchVariableStartToEnd("bgna", "img_bgn", "a");
fxt.Test__matchVariableStartToEnd("bgn", "img_bgn", "");
} }
@Test public void End() { @Test public void End() {
fxt.Init__word(Bool_.Y, "end", "$1end"); fxt.Init__word(Bool_.Y, "img_end", "$1end");
fxt.Init__ary("end"); fxt.Init__ary("img_end");
fxt.Test__matchVariableStartToEnd("end", "end", "aend"); fxt.Test__matchVariableStartToEnd("aend", "img_end", "a");
fxt.Test__matchVariableStartToEnd("end", "img_end", "");
} }
@Test public void Smoke() { @Test public void Smoke() {
fxt.Init__word(Bool_.Y, "img_upright", "upright", "upright=$1", "upright $1"); fxt.Init__word(Bool_.Y, "img_upright", "upright", "upright=$1", "upright $1");
fxt.Init__word(Bool_.Y, "img_width", "$1px"); fxt.Init__word(Bool_.Y, "img_width", "$1px");
fxt.Init__ary("img_upright", "img_width"); fxt.Init__ary("img_upright", "img_width");
fxt.Test__matchVariableStartToEnd("img_upright", "upright", "upright=123", "upright 123"); fxt.Test__matchVariableStartToEnd("upright=123", "img_upright", "123");
fxt.Test__matchVariableStartToEnd("img_width", "123px"); fxt.Test__matchVariableStartToEnd("123px", "img_width", "123");
} }
} }
class Xomw_MagicWordArray__fxt { class Xomw_MagicWordArray__fxt {
@ -52,11 +55,10 @@ class Xomw_MagicWordArray__fxt {
public void Init__ary(String... words) { public void Init__ary(String... words) {
magic_word_ary = new Xomw_MagicWordArray(magic_word_mgr, Bry_.Ary(words)); magic_word_ary = new Xomw_MagicWordArray(magic_word_mgr, Bry_.Ary(words));
} }
public void Test__matchVariableStartToEnd(String expd_str, String... vals) { public void Test__matchVariableStartToEnd(String src, String expd_name, String expd_val) {
byte[] expd = Bry_.new_u8(expd_str); byte[][] rv = new byte[2][];
for (String val : vals) { magic_word_ary.matchVariableStartToEnd(rv, Bry_.new_u8(src));
byte[] actl = magic_word_ary.matchVariableStartToEnd(Bry_.new_u8(val)); Gftest.Eq__str(expd_name, rv[0], expd_name);
Gftest.Eq__bry(expd, actl, val); Gftest.Eq__str(expd_val , rv[1], expd_val);
}
} }
} }

View File

@ -20,6 +20,7 @@ import gplx.core.btries.*;
import gplx.langs.htmls.*; import gplx.langs.htmls.*;
import gplx.xowa.mws.htmls.*; import gplx.xowa.mws.linkers.*; import gplx.xowa.mws.parsers.*; import gplx.xowa.mws.htmls.*; import gplx.xowa.mws.linkers.*; import gplx.xowa.mws.parsers.*;
import gplx.xowa.mws.filerepo.file.*; import gplx.xowa.mws.media.*; import gplx.xowa.mws.filerepo.file.*; import gplx.xowa.mws.media.*;
import gplx.xowa.mws.parsers.lnkis.*;
import gplx.langs.phps.utls.*; import gplx.langs.phps.utls.*;
/* TODO.XO /* TODO.XO
* P8: wfMessage * P8: wfMessage
@ -92,7 +93,7 @@ public class Xomw_linker {
// @param int|null width_option Used by the parser to remember the user preference thumbnailsize // @param int|null width_option Used by the parser to remember the user preference thumbnailsize
// @since 1.20 // @since 1.20
// @return String HTML for an image, with links, wrappers, etc. // @return String HTML for an image, with links, wrappers, etc.
public void Make_image_link(Bry_bfr bfr, Xomw_parser parser, Xoa_ttl title, Xomw_File file, Xomw_img_prms frame_params, Xomw_mda_prms handler_params, Object time, byte[] query, Object widthOption) { public void makeImageLink(Bry_bfr bfr, Xomw_parser parser, Xoa_ttl title, Xomw_File file, Xomw_params_frame frame_params, Xomw_params_handler handler_params, Object time, byte[] query, Object widthOption) {
// XO.MW.HOOK:ImageBeforeProduceHTML // XO.MW.HOOK:ImageBeforeProduceHTML
if (file != null && !file.allowInlineDisplay()) { if (file != null && !file.allowInlineDisplay()) {
@ -176,11 +177,12 @@ public class Xomw_linker {
// If a thumbnail width has not been provided, it is set // If a thumbnail width has not been provided, it is set
// to the default user option as specified in Language*.php // to the default user option as specified in Language*.php
if (frame_params.align == Bry_.Empty) { if (frame_params.align == Bry_.Empty) {
// frame_params.align = $parser->getTargetLanguage()->alignEnd(); frame_params.align = parser.Env().Lang__align_end;
} }
bfr.Add(prefix); bfr.Add(prefix);
this.Make_thumb_link2(bfr, title, file, frame_params, handler_params, time, query); this.Make_thumb_link2(bfr, title, file, frame_params, handler_params, time, query);
bfr.Add(postfix); bfr.Add(postfix);
return;
} }
if (file != null && frame_params.frameless != null) { if (file != null && frame_params.frameless != null) {
@ -197,7 +199,7 @@ public class Xomw_linker {
if (file != null && handler_params.width != -1) { if (file != null && handler_params.width != -1) {
// Create a resized image, without the additional thumbnail features // Create a resized image, without the additional thumbnail features
// $thumb = $file->transform(handler_params); // $thumb = $file->transform(handler_params);
thumb = new Xomw_mto(file.getUrl()); thumb = new Xomw_mto(file.getUrl(), file.getWidth(), file.getHeight());
} }
else { else {
thumb = null; thumb = null;
@ -216,7 +218,7 @@ public class Xomw_linker {
params_list.valign = frame_params.valign; params_list.valign = frame_params.valign;
params_list.img_cls = frame_params.cls; params_list.img_cls = frame_params.cls;
if (frame_params.border != null) { if (frame_params.border != null) {
params_list.img_cls = Xomw_img_prms.Cls_add(params_list.img_cls, Img_class__thumbborder); params_list.img_cls = Xomw_params_frame.Cls_add(params_list.img_cls, Img_class__thumbborder);
} }
// $params = self::getImageLinkMTOParams(frame_params, $query, $parser) + $params; // $params = self::getImageLinkMTOParams(frame_params, $query, $parser) + $params;
@ -245,7 +247,7 @@ public class Xomw_linker {
// @param Parser|null $parser // @param Parser|null $parser
// @return array // @return array
// XO.MW:SYNC:1.29; DATE:2017-02-03 // XO.MW:SYNC:1.29; DATE:2017-02-03
public void Get_image_link_mto_params(Xomw_mto_params mto_params, Xomw_img_prms frame_params, byte[] query, Xomw_parser parser) { public void Get_image_link_mto_params(Xomw_mto_params mto_params, Xomw_params_frame frame_params, byte[] query, Xomw_parser parser) {
if (Php_utl_.Is_set(frame_params.link_url) && frame_params.link_url != Bry_.Empty) { if (Php_utl_.Is_set(frame_params.link_url) && frame_params.link_url != Bry_.Empty) {
mto_params.custom_url_link = frame_params.link_url; mto_params.custom_url_link = frame_params.link_url;
if (Php_utl_.Is_set(frame_params.link_target)) { if (Php_utl_.Is_set(frame_params.link_target)) {
@ -271,8 +273,8 @@ public class Xomw_linker {
} }
} }
public void Make_thumb_link2(Bry_bfr bfr, Xoa_ttl title, Xomw_File file, Xomw_img_prms frame_params, Xomw_mda_prms handler_params, Object time, byte[] query) { public void Make_thumb_link2(Bry_bfr bfr, Xoa_ttl title, Xomw_File file, Xomw_params_frame frame_params, Xomw_params_handler handler_params, Object time, byte[] query) {
boolean exists = false; // = $file && $file->exists(); boolean exists = true; // = $file && $file->exists();
int page = handler_params.page; int page = handler_params.page;
if (frame_params.align == null) { if (frame_params.align == null) {
@ -317,7 +319,7 @@ public class Xomw_linker {
else if (frame_params.framed != null) { else if (frame_params.framed != null) {
// Use image dimensions, don't scale // Use image dimensions, don't scale
// thumb = $file->getUnscaledThumb(handler_params); // thumb = $file->getUnscaledThumb(handler_params);
thumb = new Xomw_mto(file.getUrl()); thumb = new Xomw_mto(file.getUrl(), file.getWidth(), file.getHeight());
no_scale = true; no_scale = true;
} }
else { else {
@ -328,10 +330,11 @@ public class Xomw_linker {
handler_params.width = src_width; handler_params.width = src_width;
} }
// thumb = $file->transform(handler_params); // thumb = $file->transform(handler_params);
thumb = new Xomw_mto(file.getUrl(), file.getWidth(), file.getHeight());
} }
if (thumb != null) { if (thumb != null) {
// outer_width = thumb->getWidth() + 2; outer_width = thumb.getWidth() + 2;
} }
else { else {
outer_width = handler_params.width + 2; outer_width = handler_params.width + 2;
@ -373,7 +376,7 @@ public class Xomw_linker {
mto_params.Clear(); mto_params.Clear();
mto_params.alt = frame_params.alt; mto_params.alt = frame_params.alt;
mto_params.title = frame_params.title; mto_params.title = frame_params.title;
mto_params.img_cls = Xomw_img_prms.Cls_add(frame_params.cls, Img_class__thumbimage); mto_params.img_cls = Xomw_params_frame.Cls_add(frame_params.cls, Img_class__thumbimage);
Get_image_link_mto_params(mto_params, frame_params, query, null); Get_image_link_mto_params(mto_params, frame_params, query, null);
thumb.To_html(bfr, tmp, mto_params); thumb.To_html(bfr, tmp, mto_params);
if (frame_params.framed != null) { if (frame_params.framed != null) {
@ -388,7 +391,8 @@ public class Xomw_linker {
, Bry_.Empty); , Bry_.Empty);
// env.Message_mgr().Get_by_str("thumbnail-more").text(); // env.Message_mgr().Get_by_str("thumbnail-more").text();
byte[] zoom_anch = tmp.To_bry_and_clear(); byte[] zoom_anch = tmp.To_bry_and_clear();
html_utl.Raw_element(bfr, Gfh_tag_.Bry__div, tmp_attribs.Clear().Add(Gfh_atr_.Bry__class, Class__magnify), zoom_anch); html_utl.Raw_element(tmp, Gfh_tag_.Bry__div, tmp_attribs.Clear().Add(Gfh_atr_.Bry__class, Class__magnify), zoom_anch);
zoom_icon = tmp.To_bry_and_clear();
} }
} }
bfr.Add_str_a7(" <div class=\"thumbcaption\">").Add(zoom_icon).Add(frame_params.caption).Add_str_a7("</div></div></div>"); bfr.Add_str_a7(" <div class=\"thumbcaption\">").Add(zoom_icon).Add(frame_params.caption).Add_str_a7("</div></div></div>");

View File

@ -18,6 +18,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
package gplx.xowa.mws.filerepo.file; import gplx.*; import gplx.xowa.*; import gplx.xowa.mws.*; import gplx.xowa.mws.filerepo.*; package gplx.xowa.mws.filerepo.file; import gplx.*; import gplx.xowa.*; import gplx.xowa.mws.*; import gplx.xowa.mws.filerepo.*;
import gplx.xowa.mws.media.*; import gplx.xowa.mws.media.*;
import gplx.langs.phps.utls.*; import gplx.langs.phps.utls.*;
import gplx.xowa.mws.parsers.*;
/* TODO.XO: /* TODO.XO:
* P8: normalizeExtension * P8: normalizeExtension
* P8: normalizeTitle * P8: normalizeTitle
@ -442,9 +443,10 @@ public class Xomw_File {
* @return int|boolean * @return int|boolean
*/ */
// @dflt: page = 1 // @dflt: page = 1
public int getWidth(int page) { @gplx.Virtual public int getWidth(int page) {
return -1; return -1;
} }
public int getWidth() {return this.getWidth(1);}
/** /**
* Return the height of the image. Returns false if the height is unknown * Return the height of the image. Returns false if the height is unknown
@ -460,6 +462,7 @@ public class Xomw_File {
public int getHeight(int page) { public int getHeight(int page) {
return -1; return -1;
} }
public int getHeight() {return this.getHeight(1);}
// /** // /**
// * Return the smallest bucket from wgThumbnailBuckets which is at least // * Return the smallest bucket from wgThumbnailBuckets which is at least
@ -680,18 +683,19 @@ public class Xomw_File {
// public function getSize() { // public function getSize() {
// return false; // return false;
// } // }
//
// /** /**
// * Returns the MIME type of the file. * Returns the MIME type of the file.
// * Overridden by LocalFile, UnregisteredLocalFile * Overridden by LocalFile, UnregisteredLocalFile
// * STUB * STUB
// * *
// * @return String * @return String
// */ */
// function getMimeType() { @gplx.Virtual public byte[] getMimeType() {
// return 'unknown/unknown'; return Mime_type__unknown;
// } }
// private static final byte[] Mime_type__unknown = Bry_.new_a7("unknown/unknown");
// /** // /**
// * Return the type of the media in the file. // * Return the type of the media in the file.
// * Use the value returned by this function with the MEDIATYPE_xxx constants. // * Use the value returned by this function with the MEDIATYPE_xxx constants.
@ -1347,9 +1351,9 @@ public class Xomw_File {
* @return MediaHandler|boolean Registered MediaHandler for file's MIME type * @return MediaHandler|boolean Registered MediaHandler for file's MIME type
* or false if none found * or false if none found
*/ */
public Xomw_MediaHandler getHandler() { public Xomw_MediaHandler getHandler(Xomw_parser_env env) {
if (this.handler == null) { if (this.handler == null) {
// this.handler = MediaHandler::getHandler(this.getMimeType()); this.handler = env.MediaHandlerFactory().getHandler(this.getMimeType());
} }
return this.handler; return this.handler;

File diff suppressed because it is too large Load Diff

View File

@ -21,8 +21,8 @@ public class Xomw_file_finder__mock implements Xomw_file_finder {
public Xomw_File Find_file(Xoa_ttl ttl) { public Xomw_File Find_file(Xoa_ttl ttl) {
return (Xomw_File)hash.Get_by(ttl.Page_db_as_str()); return (Xomw_File)hash.Get_by(ttl.Page_db_as_str());
} }
public void Add(String title, Xomw_FileRepo repo, int w, int h) { public void Add(String title, Xomw_FileRepo repo, int w, int h, byte[] mime) {
Xomw_LocalFile file = new Xomw_LocalFile(Bry_.new_u8(title), repo, w, h); Xomw_LocalFile file = new Xomw_LocalFile(Bry_.new_u8(title), repo, w, h, mime);
hash.Add(title, file); hash.Add_if_dupe_use_nth(title, file);
} }
} }

View File

@ -16,8 +16,12 @@ You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
package gplx.xowa.mws.media; import gplx.*; import gplx.xowa.*; import gplx.xowa.mws.*; package gplx.xowa.mws.media; import gplx.*; import gplx.xowa.*; import gplx.xowa.mws.*;
import gplx.xowa.mws.filerepo.file.*; import gplx.xowa.mws.filerepo.file.*; import gplx.xowa.mws.parsers.lnkis.*;
public class Xomw_ImageHandler extends Xomw_MediaHandler { public Xomw_ImageHandler(byte[] key) {super(key);} // MEMORY:only one instance per wiki
public class Xomw_ImageHandler extends Xomw_MediaHandler { private final Xomw_param_map paramMap = new Xomw_param_map();
public Xomw_ImageHandler(byte[] key) {super(key);
paramMap.Add(Xomw_param_itm.Mw__img_width, Xomw_param_map.Type__handler, Xomw_param_itm.Name_bry__width);
}
/** /**
* @param File file * @param File file
* @return boolean * @return boolean
@ -26,22 +30,24 @@ public class Xomw_ImageHandler extends Xomw_MediaHandler { public Xomw_ImageHand
return (file.getWidth(1) != -1 && file.getHeight(1) != -1); return (file.getWidth(1) != -1 && file.getHeight(1) != -1);
} }
// public function getParamMap() { @Override public Xomw_param_map getParamMap() {
// return [ 'img_width' => 'width' ]; return paramMap;
// } }
//
// public function validateParam(name, value) { @Override public boolean validateParam(int name_uid, byte[] val_bry, int val_int) {
// if (in_array(name, [ 'width', 'height' ])) { if (name_uid == Xomw_param_itm.Name__width || name_uid == Xomw_param_itm.Name__height) {
// if (value <= 0) { if (val_int <= 0) {
// return false; return false;
// } else { }
// return true; else {
// } return true;
// } else { }
// return false; }
// } else {
// } return false;
// }
}
// public function makeParamString(params) { // public function makeParamString(params) {
// if (isset(params['physicalWidth'])) { // if (isset(params['physicalWidth'])) {
// width = params['physicalWidth']; // width = params['physicalWidth'];

View File

@ -17,7 +17,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
package gplx.xowa.mws.media; import gplx.*; import gplx.xowa.*; import gplx.xowa.mws.*; package gplx.xowa.mws.media; import gplx.*; import gplx.xowa.*; import gplx.xowa.mws.*;
import gplx.xowa.mws.filerepo.file.*; import gplx.xowa.mws.filerepo.file.*;
public class Xomw_MediaHandler { import gplx.xowa.mws.parsers.lnkis.*;
public abstract class Xomw_MediaHandler {
public byte[] Key() {return key;} private byte[] key; public byte[] Key() {return key;} private byte[] key;
public Xomw_MediaHandler(byte[] key) { public Xomw_MediaHandler(byte[] key) {
this.key = key; this.key = key;
@ -42,23 +43,23 @@ public class Xomw_MediaHandler {
// return MediaWikiServices::getInstance() // return MediaWikiServices::getInstance()
// ->getMediaHandlerFactory()->getHandler($type); // ->getMediaHandlerFactory()->getHandler($type);
// } // }
//
// /** /**
// * Get an associative array mapping magic word IDs to parameter names. * Get an associative array mapping magic word IDs to parameter names.
// * Will be used by the parser to identify parameters. * Will be used by the parser to identify parameters.
// */ */
// abstract public function getParamMap(); public abstract Xomw_param_map getParamMap();
//
// /** /**
// * Validate a thumbnail parameter at parse time. * Validate a thumbnail parameter at parse time.
// * Return true to accept the parameter, and false to reject it. * Return true to accept the parameter, and false to reject it.
// * If you return false, the parser will do something quiet and forgiving. * If you return false, the parser will do something quiet and forgiving.
// * *
// * @param String $name * @param String $name
// * @param mixed $value * @param mixed $value
// */ */
// abstract public function validateParam($name, $value); public abstract boolean validateParam(int name_uid, byte[] val_bry, int val_int);
//
// /** // /**
// * Merge a parameter array into a String appropriate for inclusion in filenames // * Merge a parameter array into a String appropriate for inclusion in filenames
// * // *

View File

@ -16,15 +16,16 @@ You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
package gplx.xowa.mws.media; import gplx.*; import gplx.xowa.*; import gplx.xowa.mws.*; package gplx.xowa.mws.media; import gplx.*; import gplx.xowa.*; import gplx.xowa.mws.*;
// XO.MW:MW has registry and instance cache; XO only has instance
// XO.MW:SYNC:1.29; DATE:2017-02-05
public class Xomw_MediaHandlerFactory { public class Xomw_MediaHandlerFactory {
// /** private final Hash_adp_bry handlers = Hash_adp_bry.cs();
// * Default, MediaWiki core media handlers
// * // XO.MW:SYNC:1.29; DATE:2017-02-05
// * @var array public Xomw_MediaHandlerFactory() {
// */ // Default, MediaWiki core media handlers
// private static $coreHandlers = [
// 'image/jpeg' => JpegHandler::class, // 'image/jpeg' => JpegHandler::class,
// 'image/png' => PNGHandler::class, handlers.Add(Mime__image__png, new Xomw_ImageHandler(Mime__image__png)); // PngHandler
// 'image/gif' => GIFHandler::class, // 'image/gif' => GIFHandler::class,
// 'image/tiff' => TiffHandler::class, // 'image/tiff' => TiffHandler::class,
// 'image/webp' => WebPHandler::class, // 'image/webp' => WebPHandler::class,
@ -36,55 +37,27 @@ public class Xomw_MediaHandlerFactory {
// 'image/vnd.djvu' => DjVuHandler::class, // official // 'image/vnd.djvu' => DjVuHandler::class, // official
// 'image/x.djvu' => DjVuHandler::class, // compat // 'image/x.djvu' => DjVuHandler::class, // compat
// 'image/x-djvu' => DjVuHandler::class, // compat // 'image/x-djvu' => DjVuHandler::class, // compat
// ];
// }
// /**
// * @var array // XO.MW:SYNC:1.29; DATE:2017-02-05
// */ public Xomw_MediaHandler getHandler(byte[] type) {
// private $registry; return (Xomw_MediaHandler)handlers.Get_by(type);
// }
// /**
// * Instance cache of MediaHandler objects by mimetype public static byte[]
// * Mime__image__jpeg = Bry_.new_a7("image/jpeg")
// * @var MediaHandler[] , Mime__image__png = Bry_.new_a7("image/png")
// */ , Mime__image__gif = Bry_.new_a7("image/gif")
// private $handlers; , Mime__image__tiff = Bry_.new_a7("image/tiff")
// , Mime__image__webp = Bry_.new_a7("image/webp")
// public function __construct( array $registry ) { , Mime__image__x_ms_bmp = Bry_.new_a7("image/x-ms-bmp")
// $this->registry = $registry + self::$coreHandlers; , Mime__image__x_bmp = Bry_.new_a7("image/x-bmp")
// } , Mime__image__x_xcf = Bry_.new_a7("image/x-xcf")
// , Mime__image__svg_xml = Bry_.new_a7("image/svg+xml")
// protected function getHandlerClass( $type ) { , Mime__image__svg = Bry_.new_a7("image/svg")
// if ( isset( $this->registry[$type] ) ) { , Mime__image__vnd_djvu = Bry_.new_a7("image/vnd.djvu")
// return $this->registry[$type]; , Mime__image__x_djvu_dot = Bry_.new_a7("image/x.djvu")
// } else { , Mime__image__x_djvu_dash = Bry_.new_a7("image/x-djvu")
// return false; ;
// }
// }
//
// /**
// * @param String $type mimetype
// * @return boolean|MediaHandler
// */
// public function getHandler( $type ) {
// if ( isset( $this->handlers[$type] ) ) {
// return $this->handlers[$type];
// }
//
// $class = $this->getHandlerClass( $type );
// if ( $class !== false ) {
// /** @var MediaHandler $handler */
// $handler = new $class;
// if ( !$handler->isEnabled() ) {
// wfDebug( __METHOD__ . ": $class is not enabled\n" );
// $handler = false;
// }
// } else {
// wfDebug( __METHOD__ . ": no handler found for $type.\n" );
// $handler = false;
// }
//
// $this->handlers[$type] = $handler;
// return $handler;
// }
} }

View File

@ -22,8 +22,23 @@ public class Xomw_mto {
private int width = -1, height = -1; private int width = -1, height = -1;
public byte[] url; public byte[] url;
private final List_adp attribs = List_adp_.New(), link_attribs = List_adp_.New(); private final List_adp attribs = List_adp_.New(), link_attribs = List_adp_.New();
public Xomw_mto(byte[] url) { public Xomw_mto(byte[] url, int width, int height) {
this.url = url; this.url = url;
this.width = width;
this.height = height;
}
/**
* @return int Width of the output box
*/
public int getWidth() {
return this.width;
}
/**
* @return int Height of the output box
*/
public int getHeight() {
return this.height;
} }
// Return HTML <img ... /> tag for the thumbnail, will include // Return HTML <img ... /> tag for the thumbnail, will include
@ -87,7 +102,7 @@ public class Xomw_mto {
// link_attribs.Add_many(Gfh_atr_.Bry__title, Php_utl_.Empty(options_title) ? title.Get_full_text() : options_title); // link_attribs.Add_many(Gfh_atr_.Bry__title, Php_utl_.Empty(options_title) ? title.Get_full_text() : options_title);
} }
else if (!Php_utl_.Empty(options.desc_link)) { else if (!Php_utl_.Empty(options.desc_link)) {
// link_attribs = $this->getDescLinkAttribs( // link_attribs = this.getDescLinkAttribs(
// empty(options['title']) ? null : options['title'], // empty(options['title']) ? null : options['title'],
// $query // $query
// ); // );
@ -122,7 +137,7 @@ public class Xomw_mto {
// Additional densities for responsive images, if specified. // Additional densities for responsive images, if specified.
// If any of these urls is the same as src url, it'll be excluded. // If any of these urls is the same as src url, it'll be excluded.
// $responsiveUrls = array_diff($this->responsiveUrls, [ $this->url ]); // $responsiveUrls = array_diff(this.responsiveUrls, [ this.url ]);
// if (!Php_utl_.Empty($responsiveUrls)) { // if (!Php_utl_.Empty($responsiveUrls)) {
// $attribs['srcset'] = Html::srcSet($responsiveUrls); // $attribs['srcset'] = Html::srcSet($responsiveUrls);
// } // }

View File

@ -16,8 +16,12 @@ You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
package gplx.xowa.mws.parsers; import gplx.*; import gplx.xowa.*; import gplx.xowa.mws.*; package gplx.xowa.mws.parsers; import gplx.*; import gplx.xowa.*; import gplx.xowa.mws.*;
import gplx.xowa.mws.parsers.lnkis.*;
public class Xomw_parser_ctx { public class Xomw_parser_ctx {
public Xoa_ttl Page_title() {return page_title;} private Xoa_ttl page_title; public Xoa_ttl Page_title() {return page_title;} private Xoa_ttl page_title;
public Xomw_image_params Lnki_wkr__make_image__img_params = new Xomw_image_params();
public byte[][] Lnki_wkr__make_image__match_magic_word = new byte[2][];
public int[] Lnki_wkr__make_image__img_size = new int[2];
public void Init_by_page(Xoa_ttl page_title) { public void Init_by_page(Xoa_ttl page_title) {
this.page_title = page_title; this.page_title = page_title;

View File

@ -16,11 +16,13 @@ You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
package gplx.xowa.mws.parsers; import gplx.*; import gplx.xowa.*; import gplx.xowa.mws.*; package gplx.xowa.mws.parsers; import gplx.*; import gplx.xowa.*; import gplx.xowa.mws.*;
import gplx.xowa.mws.filerepo.file.*; import gplx.xowa.mws.filerepo.file.*; import gplx.xowa.mws.media.*;
public class Xomw_parser_env { public class Xomw_parser_env {
public byte[] Lang__align_end = Bry_.new_a7("right");
public Xomw_MagicWordMgr Magic_word_mgr() {return magic_word_mgr;} private final Xomw_MagicWordMgr magic_word_mgr = new Xomw_MagicWordMgr(); public Xomw_MagicWordMgr Magic_word_mgr() {return magic_word_mgr;} private final Xomw_MagicWordMgr magic_word_mgr = new Xomw_MagicWordMgr();
public Xomw_message_mgr Message_mgr() {return message_mgr;} private final Xomw_message_mgr message_mgr = new Xomw_message_mgr(); public Xomw_message_mgr Message_mgr() {return message_mgr;} private final Xomw_message_mgr message_mgr = new Xomw_message_mgr();
public Xomw_file_finder File_finder() {return file_finder;} private Xomw_file_finder file_finder = new Xomw_file_finder__noop(); public Xomw_file_finder File_finder() {return file_finder;} private Xomw_file_finder file_finder = new Xomw_file_finder__noop();
public Xomw_MediaHandlerFactory MediaHandlerFactory() {return mediaHandlerFactory;} private final Xomw_MediaHandlerFactory mediaHandlerFactory = new Xomw_MediaHandlerFactory();
public Xomw_parser_env File_finder_(Xomw_file_finder v) {file_finder = v; return this;} public Xomw_parser_env File_finder_(Xomw_file_finder v) {file_finder = v; return this;}
} }

View File

@ -15,24 +15,8 @@ GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
package gplx.xowa.mws.htmls; import gplx.*; import gplx.xowa.*; import gplx.xowa.mws.*; package gplx.xowa.mws.parsers.lnkis; import gplx.*; import gplx.xowa.*; import gplx.xowa.mws.*; import gplx.xowa.mws.parsers.*;
public class Xomw_prm_itm { public class Xomw_image_params {
public int type = 0; public Xomw_param_map paramMap = null;
public int name_type = 1; public Xomw_MagicWordArray mwArray = null;
public byte[] name = null;
public byte[] val = null;
public static final int
Type__handler = 0
;
public static final int
Name__width = 0
, Name__manual_thumb = 0
, Name__alt = 1
, Name__class = 2
, Name__link = 3
, Name__frameless = 4
, Name__framed = 5
, Name__thumbnail = 6
;
} }

View File

@ -377,7 +377,7 @@ public class Xomw_lnki_wkr {// THREAD.UNSAFE: caching for repeated calls
// cloak any absolute URLs inside the image markup, so replaceExternalLinks() won't touch them // cloak any absolute URLs inside the image markup, so replaceExternalLinks() won't touch them
bfr.Add(prefix); bfr.Add(prefix);
// Armor_links(Make_image(bfr, nt, text, holders)) // Armor_links(Make_image(bfr, nt, text, holders))
Make_image(pctx, bfr, nt, text, holders); this.makeImage(pctx, bfr, nt, text, holders);
bfr.Add(trail); bfr.Add(trail);
continue; continue;
} }
@ -443,7 +443,7 @@ public class Xomw_lnki_wkr {// THREAD.UNSAFE: caching for repeated calls
} }
} }
} }
public void Make_image(Xomw_parser_ctx pctx, Bry_bfr bfr, Xoa_ttl title, byte[] link_args, Xomw_link_holders holders) { public void makeImage(Xomw_parser_ctx pctx, Bry_bfr bfr, Xoa_ttl title, byte[] options_at_link, Xomw_link_holders holders) {
// Check if the options text is of the form "options|alt text" // Check if the options text is of the form "options|alt text"
// Options are: // Options are:
// * thumbnail make a thumbnail with enlarge-icon and caption, alignment depends on lang // * thumbnail make a thumbnail with enlarge-icon and caption, alignment depends on lang
@ -471,78 +471,78 @@ public class Xomw_lnki_wkr {// THREAD.UNSAFE: caching for repeated calls
// * text-bottom // * text-bottom
// Protect LanguageConverter markup when splitting into parts // Protect LanguageConverter markup when splitting into parts
byte[][] parts = Xomw_string_utils.Delimiter_explode(tmp_list, trv, link_args); byte[][] parts = Xomw_string_utils.Delimiter_explode(tmp_list, trv, options_at_link);
// Give extensions a chance to select the file revision for us // Give extensions a chance to select the file revision for us
// $options = []; // $options = [];
byte[] desc_query = null; byte[] desc_query = null;
// MW.HOOK:BeforeParserFetchFileAndTitle // XO.MW.HOOK:BeforeParserFetchFileAndTitle
// Fetch and register the file (file title may be different via hooks) // Fetch and register the file (file title may be different via hooks)
Xomw_File file = fetchFileAndTitle(title, null);
// list($file, $title) = $this->fetchFileAndTitle($title, $options); // list($file, $title) = $this->fetchFileAndTitle($title, $options);
Xomw_File file = fetchFileAndTitle(title, null);
// Get parameter map // Get parameter map
Xomw_MediaHandler handler2 = file == null ? null : file.getHandler(); Xomw_MediaHandler handler = file == null ? null : file.getHandler(env);
Xomw_image_params tmp_img_params = new Xomw_image_params(); Xomw_image_params tmp_img_params = pctx.Lnki_wkr__make_image__img_params;
this.getImageParams(tmp_img_params, handler2); this.getImageParams(tmp_img_params, handler);
Xomw_param_map paramMap = tmp_img_params.paramMap; Xomw_param_map paramMap = tmp_img_params.paramMap;
Xomw_MagicWordArray mwArray = tmp_img_params.mwArray; Xomw_MagicWordArray mwArray = tmp_img_params.mwArray;
// XO.MW.UNSUPPORTED.TrackingCategory: // XO.MW.UNSUPPORTED.TrackingCategory: if (!$file) $this->addTrackingCategory('broken-file-category');
//if (!$file) {
// $this->addTrackingCategory('broken-file-category');
//}
// Process the input parameters // Process the input parameters
byte[] caption = Bry_.Empty; byte[] caption = Bry_.Empty;
// XO.MW: $params = [ 'frame' => [], 'handler' => [], 'horizAlign' => [], 'vertAlign' => [] ]; // XO.MW: $params = [ 'frame' => [], 'handler' => [], 'horizAlign' => [], 'vertAlign' => [] ];
// Xomw_prm_mgr param_map = new Xomw_prm_mgr(); Xomw_params_frame frameParams = paramMap.Frame.Clear();
// Xomw_prm_mgr param_mgr = new Xomw_prm_mgr(); Xomw_params_handler handlerParams = paramMap.Handler.Clear();
Xomw_img_prms frame = new Xomw_img_prms(); // Xomw_params_horizAlign horizAlignParams = paramMap.HorizAlign.Clear();
Xomw_mda_prms handler = new Xomw_mda_prms(); // Xomw_params_vertAlign vertAlignParams = paramMap.VertAlign.Clear();
boolean seen_format = false; boolean seen_format = false;
int parts_len = parts.length; int parts_len = parts.length;
for (int i = 0; i < parts_len; i++) { for (int i = 0; i < parts_len; i++) {
byte[] part = parts[i]; byte[] part = parts[i];
part = Bry_.Trim(part); part = Bry_.Trim(part);
byte[] magic_name = mwArray.matchVariableStartToEnd(part); byte[][] tmp_match_word = pctx.Lnki_wkr__make_image__match_magic_word;
mwArray.matchVariableStartToEnd(tmp_match_word, part);
byte[] magic_name = tmp_match_word[0];
byte[] val = tmp_match_word[1];
boolean validated = false; boolean validated = false;
Xomw_param_itm prm_itm = paramMap.Get_by(magic_name); Xomw_param_itm param_item = paramMap.Get_by(magic_name);
if (prm_itm != null) { if (param_item != null) {
int prm_type = -1; int typeUid = param_item.type_uid;
int paramNameType = prm_itm.name_type; int paramNameUid = param_item.name_uid;
// Special case; width and height come in one variable together // Special case; width and height come in one variable together
if (prm_type == Xomw_prm_itm.Type__handler && paramNameType == Xomw_prm_itm.Name__width) { if (typeUid == Xomw_param_map.Type__handler && paramNameUid == Xomw_param_itm.Name__width) {
// $parsedWidthParam = $this->parseWidthParam($value); int[] tmp_img_size = pctx.Lnki_wkr__make_image__img_size;
// if (isset($parsedWidthParam['width'])) { this.parseWidthParam(tmp_img_size, val);
// $width = $parsedWidthParam['width']; int parsedW = tmp_img_size[0];
// if ($handler->validateParam('width', $width)) { int parsedH = tmp_img_size[1];
// $params[$type]['width'] = $width; if (parsedW != 0) {
// validated = true; if (handler.validateParam(Xomw_param_itm.Name__width, null, parsedW)) {
// } paramMap.Set(typeUid, Xomw_param_itm.Name__width, null, parsedW);
// } validated = true;
// if (isset($parsedWidthParam['height'])) { }
// $height = $parsedWidthParam['height']; }
// if ($handler->validateParam('height', $height)) { if (parsedH != 0) {
// $params[$type]['height'] = $height; if (handler.validateParam(Xomw_param_itm.Name__height, null, parsedH)) {
// validated = true; paramMap.Set(typeUid, Xomw_param_itm.Name__height, null, parsedH);
// } validated = true;
// } }
}
// else no validation -- T15436 // else no validation -- T15436
} }
else { else {
if (prm_type == Xomw_prm_itm.Type__handler) { if (typeUid == Xomw_param_map.Type__handler) {
// Validate handler parameter // Validate handler parameter
// validated = $handler->validateParam($paramName, $value); // validated = $handler->validateParam($paramName, $value);
} }
else { else {
// Validate @gplx.Internal protected parameters // Validate @gplx.Internal protected parameters
switch (paramNameType) { switch (paramNameUid) {
case Xomw_param_itm.Name__manual_thumb: case Xomw_param_itm.Name__manual_thumb:
case Xomw_param_itm.Name__alt: case Xomw_param_itm.Name__alt:
case Xomw_param_itm.Name__class: case Xomw_param_itm.Name__class:
@ -586,17 +586,16 @@ public class Xomw_lnki_wkr {// THREAD.UNSAFE: caching for repeated calls
// use first appearing option, discard others. // use first appearing option, discard others.
validated = !seen_format; validated = !seen_format;
seen_format = true; seen_format = true;
frame.thumbnail = Bry_.Empty;
break; break;
default: default:
// Most other things appear to be empty or numeric... // Most other things appear to be empty or numeric...
// validated = ($value === false || is_numeric(trim($value))); validated = (val == null || Php_utl_.isnumeric(Bry_.Trim(val)));
break; break;
} }
} }
} if (validated) {
if (validated) { paramMap.Set(typeUid, paramNameUid, val, -1);
// $params[$type][$paramName] = $value; }
} }
} }
if (!validated) { if (!validated) {
@ -605,22 +604,22 @@ public class Xomw_lnki_wkr {// THREAD.UNSAFE: caching for repeated calls
} }
// Process alignment parameters // Process alignment parameters
Xomw_param_itm tmp = paramMap.Get_by(Xomw_prm_mgr.Name__horiz_align); Xomw_param_itm tmp = paramMap.Get_by(Xomw_param_map.Type__horizAlign);
if (tmp != null) { if (tmp != null) {
// frame.align = tmp.val; // frameParams.align = tmp.val;
} }
tmp = paramMap.Get_by(Xomw_prm_mgr.Name__vert_align); tmp = paramMap.Get_by(Xomw_param_map.Type__vertAlign);
if (tmp != null) { if (tmp != null) {
// frame.valign = tmp.val; // frameParams.valign = tmp.val;
} }
frame.caption = caption; frameParams.caption = caption;
boolean image_is_framed boolean image_is_framed
= frame.frame != null = frameParams.frame != null
|| frame.framed != null || frameParams.framed != null
|| frame.thumbnail != null || frameParams.thumbnail != null
|| frame.manual_thumb != null || frameParams.manual_thumb != null
; ;
// Will the image be presented in a frame, with the caption below? // Will the image be presented in a frame, with the caption below?
@ -639,28 +638,28 @@ public class Xomw_lnki_wkr {// THREAD.UNSAFE: caching for repeated calls
// plicit caption= parameter and preserving the old magic unnamed para- // plicit caption= parameter and preserving the old magic unnamed para-
// meter for BC; ... // meter for BC; ...
if (image_is_framed) { // Framed image if (image_is_framed) { // Framed image
if (caption == Bry_.Empty && frame.alt == null) { if (caption == Bry_.Empty && frameParams.alt == null) {
// No caption or alt text, add the filename as the alt text so // No caption or alt text, add the filename as the alt text so
// that screen readers at least get some description of the image // that screen readers at least get some description of the image
frame.alt = title.Get_text(); frameParams.alt = title.Get_text();
} }
// Do not set $params['frame']['title'] because tooltips don't make sense // Do not set $params['frame']['title'] because tooltips don't make sense
// for framed images // for framed images
} }
else { // Inline image else { // Inline image
if (frame.alt == null) { if (frameParams.alt == null) {
// No alt text, use the "caption" for the alt text // No alt text, use the "caption" for the alt text
if (caption != Bry_.Empty) { if (caption != Bry_.Empty) {
// frame.alt = $this->stripAltText(caption, $holders); // frameParams.alt = $this->stripAltText(caption, $holders);
} }
else { else {
// No caption, fall back to using the filename for the // No caption, fall back to using the filename for the
// alt text // alt text
frame.alt = title.Get_text(); frameParams.alt = title.Get_text();
} }
} }
// Use the "caption" for the tooltip text // Use the "caption" for the tooltip text
// frame.title = $this->stripAltText(caption, $holders); // frameParams.title = $this->stripAltText(caption, $holders);
} }
// MW.HOOK:ParserMakeImageParams // MW.HOOK:ParserMakeImageParams
@ -669,7 +668,7 @@ public class Xomw_lnki_wkr {// THREAD.UNSAFE: caching for repeated calls
// byte[] time = options.time; // byte[] time = options.time;
Object time = null; Object time = null;
// options = $this->mOptions->getThumbSize() // options = $this->mOptions->getThumbSize()
linker.Make_image_link(bfr, parser, title, file, frame, handler, time, desc_query, null); linker.makeImageLink(bfr, parser, title, file, frameParams, handlerParams, time, desc_query, null);
// Give the handler a chance to modify the parser Object // Give the handler a chance to modify the parser Object
// if (handler != null) { // if (handler != null) {
@ -697,25 +696,26 @@ public class Xomw_lnki_wkr {// THREAD.UNSAFE: caching for repeated calls
private static Xomw_param_list[] internalParamNames; private static Xomw_param_list[] internalParamNames;
private static Xomw_param_map internalParamMap; private static Xomw_param_map internalParamMap;
private void getImageParams(Xomw_image_params rv, Xomw_MediaHandler handler) { private void getImageParams(Xomw_image_params rv, Xomw_MediaHandler handler) {
byte[] handlerClass = handler == null ? Bry_.Empty : handler.Key(); byte[] handlerClass = handler == null ? Bry_.Empty : handler.Key();
rv.paramMap = (Xomw_param_map)mImageParams.Get_by(handlerClass); rv.paramMap = (Xomw_param_map)mImageParams.Get_by(handlerClass);
// NOTE: lazy-init; code below can be inefficent
if (rv.paramMap == null) { if (rv.paramMap == null) {
// Initialise static lists // Initialise static lists
if (internalParamNames == null) { if (internalParamNames == null) {
internalParamNames = new Xomw_param_list[] internalParamNames = new Xomw_param_list[]
{ Xomw_param_list.New("horizAlign", "left", "right", "center", "none") { Xomw_param_list.New(Xomw_param_map.Type__horizAlign, "horizAlign", "left", "right", "center", "none")
, Xomw_param_list.New("vertAlign", "baseline", "sub", "super", "top", "text-top", "middle", "bottom", "text-bottom") , Xomw_param_list.New(Xomw_param_map.Type__vertAlign , "vertAlign", "baseline", "sub", "super", "top", "text-top", "middle", "bottom", "text-bottom")
, Xomw_param_list.New("frame", "thumbnail", "manual_thumb", "framed", "frameless", "upright", "border", "link", "alt", "class") , Xomw_param_list.New(Xomw_param_map.Type__frame , "frame", "thumbnail", "manual_thumb", "framed", "frameless", "upright", "border", "link", "alt", "class")
}; };
internalParamMap = new Xomw_param_map(); internalParamMap = new Xomw_param_map();
byte[] bry_img = Bry_.new_a7("img_"); byte[] bry_img = Bry_.new_a7("img_");
for (Xomw_param_list param_list : internalParamNames) { for (Xomw_param_list param_list : internalParamNames) {
byte[] type = param_list.type;
for (byte[] name : param_list.names) { for (byte[] name : param_list.names) {
byte[] magic_name = Bry_.Add(bry_img, Bry_.Replace(name, Byte_ascii.Dash, Byte_ascii.Underline)); byte[] magic_name = Bry_.Add(bry_img, Bry_.Replace(name, Byte_ascii.Dash, Byte_ascii.Underline));
internalParamMap.Add(magic_name, type, name); internalParamMap.Add(magic_name, param_list.type_uid, name);
} }
} }
} }
@ -723,10 +723,12 @@ public class Xomw_lnki_wkr {// THREAD.UNSAFE: caching for repeated calls
// Add handler params // Add handler params
Xomw_param_map paramMap = internalParamMap.Clone(); Xomw_param_map paramMap = internalParamMap.Clone();
if (handler != null) { if (handler != null) {
// $handlerParamMap = $handler->getParamMap(); Xomw_param_map handlerParamMap = handler.getParamMap();
// foreach ($handlerParamMap as $magic => $paramName) { int handlerParamMapLen = handlerParamMap.Len();
// $paramMap[$magic] = [ 'handler', $paramName ]; for (int i = 0; i < handlerParamMapLen; i++) {
// } Xomw_param_itm itm = (Xomw_param_itm)handlerParamMap.Get_at(i);
paramMap.Add(itm.magic, itm.type_uid, itm.name);
}
} }
this.mImageParams.Add(handlerClass, paramMap); this.mImageParams.Add(handlerClass, paramMap);
rv.paramMap = paramMap; rv.paramMap = paramMap;
@ -738,6 +740,35 @@ public class Xomw_lnki_wkr {// THREAD.UNSAFE: caching for repeated calls
rv.mwArray = (Xomw_MagicWordArray)mImageParamsMagicArray.Get_by(handlerClass); rv.mwArray = (Xomw_MagicWordArray)mImageParamsMagicArray.Get_by(handlerClass);
} }
} }
// Parsed a width param of imagelink like 300px or 200x300px
// XO.MW.NOTE: for MW, "" -> null, null while "AxB" -> 0x0
public void parseWidthParam(int[] img_size, byte[] src) {
img_size[0] = img_size[1] = -1;
if (src == Bry_.Empty) {
return;
}
// (T15500) In both cases (width/height and width only),
// permit trailing "px" for backward compatibility.
int src_bgn = 0;
int src_end = src.length;
// XO: "px" is optional; if exists at end, ignore it
if (Bry_.Has_at_end(src, Bry__px)) {
src_end -= 2;
}
// XO.MW: if ( preg_match( '/^([0-9]*)x([0-9]*)\s*(?:px)?\s*$/', $value, $m ) ) {
int w_bgn = 0;
int w_end = Bry_find_.Find_fwd_while_num(src, src_bgn, src_end);
int h_bgn = -1;
int h_end = -1;
if (w_end < src_end && src[w_end] == Byte_ascii.Ltr_x) {
h_bgn = w_end + 1;
h_end = Bry_find_.Find_fwd_while_num(src, h_bgn, src_end);
}
img_size[0] = Bry_.To_int_or(src, w_bgn, w_end, 0);
img_size[1] = Bry_.To_int_or(src, h_bgn, h_end, 0);
}
private static final byte[] Bry__px = Bry_.new_a7("px");
/** /**
* Fetch a file and its title and register a reference to it. * Fetch a file and its title and register a reference to it.
@ -826,7 +857,3 @@ public class Xomw_lnki_wkr {// THREAD.UNSAFE: caching for repeated calls
// pipe -> \\| // pipe -> \\|
// other chars... -> (.*) // other chars... -> (.*)
} }
class Xomw_image_params {
public Xomw_param_map paramMap = null;
public Xomw_MagicWordArray mwArray = null;
}

View File

@ -16,7 +16,7 @@ You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
package gplx.xowa.mws.parsers.lnkis; import gplx.*; import gplx.xowa.*; import gplx.xowa.mws.*; import gplx.xowa.mws.parsers.*; package gplx.xowa.mws.parsers.lnkis; import gplx.*; import gplx.xowa.*; import gplx.xowa.mws.*; import gplx.xowa.mws.parsers.*;
import org.junit.*; import org.junit.*; import gplx.core.tests.*; import gplx.xowa.mws.filerepo.*; import gplx.xowa.mws.filerepo.file.*;
public class Xomw_lnki_wkr__file__tst { public class Xomw_lnki_wkr__file__tst {
private final Xomw_lnki_wkr__fxt fxt = new Xomw_lnki_wkr__fxt(); private final Xomw_lnki_wkr__fxt fxt = new Xomw_lnki_wkr__fxt();
@Before public void init() { @Before public void init() {
@ -27,6 +27,72 @@ public class Xomw_lnki_wkr__file__tst {
fxt.Test__to_html("[[File:A.png]]", "<img alt='A.png' src='/orig/7/70/A.png' />"); fxt.Test__to_html("[[File:A.png]]", "<img alt='A.png' src='/orig/7/70/A.png' />");
} }
@Test public void Thumb() { @Test public void Thumb() {
fxt.Test__to_html("[[File:A.png|thumb]]", "<div class='thumb t'><div class='thumbinner' style='width:2px;'> <div class='thumbcaption'></div></div></div><img alt='A.png' src='/orig/7/70/A.png' />"); fxt.Test__to_html("[[File:A.png|thumb]]", "<div class='thumb tright'><div class='thumbinner' style='width:302px;'><a><img alt='A.png' src='/orig/7/70/A.png' class='thumbimage' /></a> <div class='thumbcaption'><div class='magnify'><a href='' class='internal'></a></div></div></div></div>");
}
@Test public void Size() {
fxt.Test__to_html("[[File:A.png|123x456px]]", "<img alt='A.png' src='/orig/7/70/A.png' />");
}
@Test public void Test__parseWidthParam() {
int[] img_size = new int[2];
// WxHpx
fxt.Test__parseWidthParam(img_size, "12x34px" , 12, 34);
// WxH
fxt.Test__parseWidthParam(img_size, "12x34" , 12, 34);
// Wpx
fxt.Test__parseWidthParam(img_size, "12px" , 12, 0);
// W
fxt.Test__parseWidthParam(img_size, "12" , 12, 0);
// 12x
fxt.Test__parseWidthParam(img_size, "12x" , 12, 0);
// x34
fxt.Test__parseWidthParam(img_size, "x34" , 0, 34);
}
}
class Xomw_lnki_wkr__fxt {
private final Xomw_lnki_wkr wkr;
private final Xomw_parser_ctx pctx;
private final Xomw_parser_bfr pbfr = new Xomw_parser_bfr();
private final Xomw_file_finder__mock file_finder = new Xomw_file_finder__mock();
private final Xomw_FileRepo repo = new Xomw_FileRepo(Bry_.new_a7("/orig"), Bry_.new_a7("/thumb"));
private boolean apos = true;
public Xomw_lnki_wkr__fxt() {
Xoae_app app = Xoa_app_fxt.Make__app__edit();
Xowe_wiki wiki = Xoa_app_fxt.Make__wiki__edit(app);
Xomw_parser parser = new Xomw_parser();
wkr = parser.Lnki_wkr();
// env
parser.Env().File_finder_(file_finder);
parser.Env().Magic_word_mgr().Add(Bry_.new_u8("img_thumbnail"), Bool_.Y, Bry_.Ary("thumb"));
parser.Env().Magic_word_mgr().Add(Bry_.new_u8("img_width"), Bool_.Y, Bry_.Ary("$1px"));
parser.Init_by_wiki(wiki);
// ctx
pctx = new Xomw_parser_ctx();
pctx.Init_by_page(wiki.Ttl_parse(Bry_.new_a7("Page_1")));
}
public void Clear() {
wkr.Clear_state();
}
public void Init__file(String title, int w, int h) {
file_finder.Add(title, repo, w, h, gplx.xowa.files.Xof_ext_.Mime_type__ary[gplx.xowa.files.Xof_ext_.Id_png]);
}
public void Test__parse(String src_str, String expd) {
byte[] src_bry = Bry_.new_u8(src_str);
wkr.Replace_internal_links(pctx, pbfr.Init(src_bry));
if (apos) expd = gplx.langs.htmls.Gfh_utl.Replace_apos(expd);
Gftest.Eq__ary__lines(expd, pbfr.Rslt().To_str_and_clear(), src_str);
}
public void Test__to_html(String src_str, String expd) {
byte[] src_bry = Bry_.new_u8(src_str);
wkr.Replace_internal_links(pctx, pbfr.Init(src_bry));
wkr.Replace_link_holders(pctx, pbfr);
if (apos) expd = gplx.langs.htmls.Gfh_utl.Replace_apos(expd);
Gftest.Eq__ary__lines(expd, pbfr.Rslt().To_str_and_clear(), src_str);
}
public void Test__parseWidthParam(int[] img_size, String src_str, int expd_w, int expd_h) {
wkr.parseWidthParam(img_size, Bry_.new_u8(src_str));
Gftest.Eq__int(expd_w, img_size[0], "w");
Gftest.Eq__int(expd_h, img_size[1], "h");
} }
} }

View File

@ -27,45 +27,3 @@ public class Xomw_lnki_wkr__text__tst {
@Test public void Html__text() {fxt.Test__to_html("[[A]]" , "<a href='/wiki/A' title='A'>A</a>");} @Test public void Html__text() {fxt.Test__to_html("[[A]]" , "<a href='/wiki/A' title='A'>A</a>");}
@Test public void Html__capt() {fxt.Test__to_html("[[A|a]]" , "<a href='/wiki/A' title='A'>a</a>");} @Test public void Html__capt() {fxt.Test__to_html("[[A|a]]" , "<a href='/wiki/A' title='A'>a</a>");}
} }
class Xomw_lnki_wkr__fxt {
private final Xomw_lnki_wkr wkr;
private final Xomw_parser_ctx pctx;
private final Xomw_parser_bfr pbfr = new Xomw_parser_bfr();
private final Xomw_file_finder__mock file_finder = new Xomw_file_finder__mock();
private final Xomw_FileRepo repo = new Xomw_FileRepo(Bry_.new_a7("/orig"), Bry_.new_a7("/thumb"));
private boolean apos = true;
public Xomw_lnki_wkr__fxt() {
Xoae_app app = Xoa_app_fxt.Make__app__edit();
Xowe_wiki wiki = Xoa_app_fxt.Make__wiki__edit(app);
Xomw_parser parser = new Xomw_parser();
wkr = parser.Lnki_wkr();
// env
parser.Env().File_finder_(file_finder);
parser.Env().Magic_word_mgr().Add(Bry_.new_u8("img_thumbnail"), Bool_.Y, Bry_.Ary("thumb"));
parser.Init_by_wiki(wiki);
// ctx
pctx = new Xomw_parser_ctx();
pctx.Init_by_page(wiki.Ttl_parse(Bry_.new_a7("Page_1")));
}
public void Clear() {
wkr.Clear_state();
}
public void Init__file(String title, int w, int h) {
file_finder.Add(title, repo, w, h);
}
public void Test__parse(String src_str, String expd) {
byte[] src_bry = Bry_.new_u8(src_str);
wkr.Replace_internal_links(pctx, pbfr.Init(src_bry));
if (apos) expd = gplx.langs.htmls.Gfh_utl.Replace_apos(expd);
Tfds.Eq_str_lines(expd, pbfr.Rslt().To_str_and_clear(), src_str);
}
public void Test__to_html(String src_str, String expd) {
byte[] src_bry = Bry_.new_u8(src_str);
wkr.Replace_internal_links(pctx, pbfr.Init(src_bry));
wkr.Replace_link_holders(pctx, pbfr);
if (apos) expd = gplx.langs.htmls.Gfh_utl.Replace_apos(expd);
Tfds.Eq_str_lines(expd, pbfr.Rslt().To_str_and_clear(), src_str);
}
}

View File

@ -18,25 +18,40 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
package gplx.xowa.mws.parsers.lnkis; import gplx.*; import gplx.xowa.*; import gplx.xowa.mws.*; import gplx.xowa.mws.parsers.*; package gplx.xowa.mws.parsers.lnkis; import gplx.*; import gplx.xowa.*; import gplx.xowa.mws.*; import gplx.xowa.mws.parsers.*;
public class Xomw_param_itm { public class Xomw_param_itm {
public final byte[] magic; public final byte[] magic;
public final byte[] type; public final int type_uid;
public final byte[] name; public final byte[] name;
public final int name_type; public final int name_uid;
public Xomw_param_itm(byte[] magic, byte[] type, byte[] name) { public Xomw_param_itm(byte[] magic, int type_uid, byte[] name) {
this.magic = magic; this.magic = magic;
this.type = type; this.type_uid = type_uid;
this.name = name; this.name = name;
this.name_type = name_types.Get_as_int_or(name, -1); this.name_uid = name_uids.Get_as_int_or(name, -1);
} }
public static final int public static final int
Name__width = 0 Name__width = 0
, Name__manual_thumb = 1 , Name__height = 1
, Name__alt = 2 , Name__manual_thumb = 2
, Name__class = 3 , Name__alt = 3
, Name__link = 4 , Name__class = 4
, Name__frameless = 5 , Name__link = 5
, Name__framed = 6 , Name__frameless = 6
, Name__thumbnail = 7 , Name__framed = 7
, Name__thumbnail = 8
;
private static final Hash_adp_bry name_uids = Hash_adp_bry.cs()
.Add_str_int("width" , Name__width)
.Add_str_int("manual_thumb" , Name__manual_thumb)
.Add_str_int("alt" , Name__alt)
.Add_str_int("class" , Name__class)
.Add_str_int("link" , Name__link)
.Add_str_int("frameless" , Name__frameless)
.Add_str_int("framed" , Name__framed)
.Add_str_int("thumbnail" , Name__thumbnail)
;
public static final byte[]
Mw__img_width = Bry_.new_a7("img_width")
;
public static final byte[]
Name_bry__width = Bry_.new_a7("width")
; ;
private static final Hash_adp_bry name_types = Hash_adp_bry.cs()
.Add_str_int("thumbnail", Name__thumbnail);
} }

View File

@ -18,12 +18,24 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
package gplx.xowa.mws.parsers.lnkis; import gplx.*; import gplx.xowa.*; import gplx.xowa.mws.*; import gplx.xowa.mws.parsers.*; package gplx.xowa.mws.parsers.lnkis; import gplx.*; import gplx.xowa.*; import gplx.xowa.mws.*; import gplx.xowa.mws.parsers.*;
public class Xomw_param_map { public class Xomw_param_map {
private final Ordered_hash hash = Ordered_hash_.New_bry(); private final Ordered_hash hash = Ordered_hash_.New_bry();
public final Xomw_params_frame Frame = new Xomw_params_frame();
public final Xomw_params_handler Handler = new Xomw_params_handler();
public final Xomw_params_horizAlign HorizAlign = new Xomw_params_horizAlign();
public final Xomw_params_vertAlign VertAlign = new Xomw_params_vertAlign();
public int Len() {return hash.Len();}
public Xomw_param_itm Get_at(int i) {return (Xomw_param_itm)hash.Get_at(i);}
public Xomw_param_itm Get_by(byte[] name) { public Xomw_param_itm Get_by(byte[] name) {
return (Xomw_param_itm)hash.Get_by(name); return (Xomw_param_itm)hash.Get_by(name);
} }
public Xomw_param_itm Get_by(int name_type) { public Xomw_param_itm Get_by(int name_type) {
return null; return null;
} }
public void Set(int type, int paramNameUid, byte[] paramBry, int paramInt) {
switch (type) {
case Type__frame: Frame.Set(paramNameUid, paramBry, paramInt); break;
case Type__handler: Handler.Set(paramNameUid, paramBry, paramInt); break;
}
}
public byte[][] Keys() { public byte[][] Keys() {
int len = hash.Len(); int len = hash.Len();
byte[][] rv = new byte[len][]; byte[][] rv = new byte[len][];
@ -32,8 +44,8 @@ public class Xomw_param_map {
} }
return rv; return rv;
} }
public void Add(byte[] magic, byte[] type, byte[] name) { public void Add(byte[] magic, int type_uid, byte[] name) {
Xomw_param_itm itm = new Xomw_param_itm(magic, type, name); Xomw_param_itm itm = new Xomw_param_itm(magic, type_uid, name);
hash.Add(magic, itm); hash.Add(magic, itm);
} }
public Xomw_param_map Clone() { public Xomw_param_map Clone() {
@ -41,17 +53,23 @@ public class Xomw_param_map {
int len = hash.Len(); int len = hash.Len();
for (int i = 0; i < len; i++) { for (int i = 0; i < len; i++) {
Xomw_param_itm itm = (Xomw_param_itm)hash.Get_at(i); Xomw_param_itm itm = (Xomw_param_itm)hash.Get_at(i);
rv.Add(itm.magic, itm.type, itm.name); rv.Add(itm.magic, itm.type_uid, itm.name);
} }
rv.Frame.Copy_to(this.Frame);
rv.Handler.Copy_to(this.Handler);
return rv; return rv;
} }
public static final int Type__horizAlign = 0, Type__vertAlign = 1, Type__frame = 2, Type__handler = 3;
} }
class Xomw_param_list { class Xomw_param_list {
public int type_uid;
public byte[] type; public byte[] type;
public byte[][] names; public byte[][] names;
public static Xomw_param_list New(String type, String... names) { public static Xomw_param_list New(int type_uid, String type, String... names) {
Xomw_param_list rv = new Xomw_param_list(); Xomw_param_list rv = new Xomw_param_list();
rv.type_uid = type_uid;
rv.type = Bry_.new_u8(type); rv.type = Bry_.new_u8(type);
rv.names = Bry_.Ary(names); rv.names = Bry_.Ary(names);
return rv; return rv;

View File

@ -15,8 +15,8 @@ GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
package gplx.xowa.mws.htmls; import gplx.*; import gplx.xowa.*; import gplx.xowa.mws.*; package gplx.xowa.mws.parsers.lnkis; import gplx.*; import gplx.xowa.*; import gplx.xowa.mws.*; import gplx.xowa.mws.parsers.*;
public class Xomw_img_prms { public class Xomw_params_frame {
public byte[] align = null; public byte[] align = null;
public byte[] valign = null; public byte[] valign = null;
public byte[] caption = null; public byte[] caption = null;
@ -35,11 +35,37 @@ public class Xomw_img_prms {
public byte[] no_link = null; public byte[] no_link = null;
public byte[] border = null; public byte[] border = null;
public double upright = -1; public double upright = -1;
public void Clear() { public void Set(int uid, byte[] val_bry, int val_int) {
switch (uid) {
case Xomw_param_itm.Name__thumbnail: thumbnail = val_bry; break;
}
}
public Xomw_params_frame Clear() {
align = valign = caption = frame = framed = frameless align = valign = caption = frame = framed = frameless
= thumbnail = manual_thumb = alt = title = cls = img_cls = thumbnail = manual_thumb = alt = title = cls = img_cls
= link_title = link_url = link_target = no_link = null; = link_title = link_url = link_target = no_link = null;
upright = -1; upright = -1;
return this;
}
public void Copy_to(Xomw_params_frame src) {
this.align = src.align;
this.valign = src.valign;
this.caption = src.caption;
this.frame = src.frame;
this.framed = src.framed;
this.frameless = src.frameless;
this.thumbnail = src.thumbnail;
this.manual_thumb = src.manual_thumb;
this.alt = src.alt;
this.title = src.title;
this.cls = src.cls;
this.img_cls = src.img_cls;
this.link_title = src.link_title;
this.link_url = src.link_url;
this.link_target = src.link_target;
this.no_link = src.no_link;
this.border = src.border;
this.upright = src.upright;
} }
public static byte[] Cls_add(byte[] lhs, byte[] rhs) { public static byte[] Cls_add(byte[] lhs, byte[] rhs) {
return Bry_.Len_eq_0(lhs) ? rhs : Bry_.Add(lhs, Byte_ascii.Space_bry, rhs); return Bry_.Len_eq_0(lhs) ? rhs : Bry_.Add(lhs, Byte_ascii.Space_bry, rhs);

View File

@ -0,0 +1,39 @@
/*
XOWA: the XOWA Offline Wiki Application
Copyright (C) 2012 gnosygnu@gmail.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package gplx.xowa.mws.parsers.lnkis; import gplx.*; import gplx.xowa.*; import gplx.xowa.mws.*; import gplx.xowa.mws.parsers.*;
public class Xomw_params_handler {
public int width = -1;
public int height = -1;
public int page = -1;
public Xomw_params_handler Clear() {
width = height = page = -1;
return this;
}
public void Copy_to(Xomw_params_handler src) {
this.width = src.width;
this.height = src.height;
this.page = src.page;
}
public void Set(int uid, byte[] val_bry, int val_int) {
switch (uid) {
case Xomw_param_itm.Name__width: width = val_int; break;
case Xomw_param_itm.Name__height: height = val_int; break;
default: throw Err_.new_unhandled_default(uid);
}
}
}

View File

@ -15,9 +15,9 @@ GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
package gplx.xowa.mws.htmls; import gplx.*; import gplx.xowa.*; import gplx.xowa.mws.*; package gplx.xowa.mws.parsers.lnkis; import gplx.*; import gplx.xowa.*; import gplx.xowa.mws.*; import gplx.xowa.mws.parsers.*;
public class Xomw_mda_prms { public class Xomw_params_horizAlign {
public int width = -1; public Xomw_params_horizAlign Clear() {
public int height = -1; return this;
public int page = -1; }
} }

View File

@ -15,16 +15,9 @@ GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
package gplx.xowa.mws.htmls; import gplx.*; import gplx.xowa.*; import gplx.xowa.mws.*; package gplx.xowa.mws.parsers.lnkis; import gplx.*; import gplx.xowa.*; import gplx.xowa.mws.*; import gplx.xowa.mws.parsers.*;
public class Xomw_prm_mgr { public class Xomw_params_vertAlign {
public Xomw_prm_itm Get_or_null(int name_type) { public Xomw_params_vertAlign Clear() {
return null; return this;
} }
public Xomw_prm_itm Get_or_null(byte[] key) {
return null;
}
public static final int
Name__horiz_align = 0
, Name__vert_align = 1
;
} }