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

Xomw: Add message param utility classes

This commit is contained in:
gnosygnu 2017-03-31 09:36:47 -04:00
parent f57fd74e13
commit 145635b6c7
5 changed files with 401 additions and 299 deletions

View File

@ -54,8 +54,9 @@ public class Byte_ {
public static byte[] Ary_by_ints(int... ary) { public static byte[] Ary_by_ints(int... ary) {
int ary_len = ary.length; int ary_len = ary.length;
byte[] rv = new byte[ary_len]; byte[] rv = new byte[ary_len];
for (int i = 0; i < ary_len; i++) for (int i = 0; i < ary_len; i++) {
rv[i] = By_int(ary[i]); rv[i] = By_int(ary[i]);
}
return rv; return rv;
} }
} }

View File

@ -47,6 +47,22 @@ public class Hex_utl_ {
} }
return rv; return rv;
} }
public static byte[] Parse_hex_to_bry(String src) {return Parse_hex_to_bry(Bry_.new_u8(src));}
public static byte[] Parse_hex_to_bry(byte[] src) {
int src_len = src.length;
if ((src_len % 2) != 0) throw Err_.new_wo_type("hex_utl: hex_string must have an even length; src=~{0}", src);
int ary_len = src_len / 2;
byte[] rv = new byte[ary_len];
int rv_idx = 0;
for (int i = 0; i < src_len; i += 2) {
int val = Parse_or(src, i, i + 2, -1);
if (val == -1) throw Err_.new_wo_type("hex_utl: hex_string has invalid char; src=~{0}", src);
rv[rv_idx] = (byte)val;
rv_idx++;
}
return rv;
}
public static byte[] Encode_bry(byte[] src) { public static byte[] Encode_bry(byte[] src) {
int src_len = src.length; int src_len = src.length;
byte[] trg = new byte[src_len * 2]; byte[] trg = new byte[src_len * 2];

View File

@ -53,6 +53,9 @@ public class Hex_utl__tst {
fxt.Test__write_bfr(Bool_.Y, 256, "100"); fxt.Test__write_bfr(Bool_.Y, 256, "100");
fxt.Test__write_bfr(Bool_.Y, Int_.Max_value, "7fffffff"); fxt.Test__write_bfr(Bool_.Y, Int_.Max_value, "7fffffff");
} }
@Test public void Encode() {
fxt.Test__parse_hex_to_bry("E2A7BC", 226, 167, 188);
}
} }
class Hex_utl__fxt { class Hex_utl__fxt {
public void Test__write(String s, int bgn, int end, int val, String expd) { public void Test__write(String s, int bgn, int end, int val, String expd) {
@ -75,8 +78,12 @@ class Hex_utl__fxt {
Hex_utl_.Write_bfr(bfr, lcase, val); Hex_utl_.Write_bfr(bfr, lcase, val);
Gftest.Eq__str(expd, bfr.To_str_and_clear()); Gftest.Eq__str(expd, bfr.To_str_and_clear());
} }
// public void Test__encode_bry(int val, int pad, String expd) { public void Test__encode_bry(String val, int... expd) {
// String actl = Hex_utl_.To_str(val, pad); byte[] actl = Hex_utl_.Encode_bry(Bry_.new_u8(val));
// Tfds.Eq(expd, actl); Gftest.Eq__ary(Byte_.Ary_by_ints(expd), actl, "encode");
// } }
public void Test__parse_hex_to_bry(String val, int... expd) {
byte[] actl = Hex_utl_.Parse_hex_to_bry(Bry_.new_u8(val));
Gftest.Eq__ary(Byte_.Ary_by_ints(expd), actl, "encode");
}
} }

View File

@ -20,6 +20,7 @@ public class Gftest {
public static void Eq__ary(boolean[] expd, boolean[] actl, String msg_fmt, Object... msg_args) {Eq__array(Type_adp_.Tid__bool, expd, actl, msg_fmt, msg_args);} public static void Eq__ary(boolean[] expd, boolean[] actl, String msg_fmt, Object... msg_args) {Eq__array(Type_adp_.Tid__bool, expd, actl, msg_fmt, msg_args);}
public static void Eq__ary(int[] expd, int[] actl, String msg_fmt, Object... msg_args) {Eq__array(Type_adp_.Tid__int, expd, actl, msg_fmt, msg_args);} public static void Eq__ary(int[] expd, int[] actl, String msg_fmt, Object... msg_args) {Eq__array(Type_adp_.Tid__int, expd, actl, msg_fmt, msg_args);}
public static void Eq__ary(long[] expd, long[] actl, String msg_fmt, Object... msg_args) {Eq__array(Type_adp_.Tid__long, expd, actl, msg_fmt, msg_args);} public static void Eq__ary(long[] expd, long[] actl, String msg_fmt, Object... msg_args) {Eq__array(Type_adp_.Tid__long, expd, actl, msg_fmt, msg_args);}
public static void Eq__ary(byte[] expd, byte[] actl, String msg_fmt, Object... msg_args) {Eq__array(Type_adp_.Tid__byte, expd, actl, msg_fmt, msg_args);}
public static void Eq__ary__lines(String expd, byte[] actl) {Eq__ary__lines(expd, String_.new_u8(actl), "no_msg");} public static void Eq__ary__lines(String expd, byte[] actl) {Eq__ary__lines(expd, String_.new_u8(actl), "no_msg");}
public static void Eq__ary__lines(String expd, byte[] actl, String msg_fmt, Object... msg_args) {Eq__ary__lines(expd, String_.new_u8(actl), msg_fmt, msg_args);} public static void Eq__ary__lines(String expd, byte[] actl, String msg_fmt, Object... msg_args) {Eq__ary__lines(expd, String_.new_u8(actl), msg_fmt, msg_args);}
public static void Eq__ary__lines(String expd, String actl, String msg_fmt, Object... msg_args) {Eq__array(Type_adp_.Tid__bry, Bry_split_.Split_lines(Bry_.new_u8_safe(expd)), Bry_split_.Split_lines(Bry_.new_u8_safe(actl)), msg_fmt, msg_args);} public static void Eq__ary__lines(String expd, String actl, String msg_fmt, Object... msg_args) {Eq__array(Type_adp_.Tid__bry, Bry_split_.Split_lines(Bry_.new_u8_safe(expd)), Bry_split_.Split_lines(Bry_.new_u8_safe(actl)), msg_fmt, msg_args);}
@ -154,6 +155,7 @@ public class Gftest {
case Type_adp_.Tid__bry: bfr.Add_safe((byte[])Array_.Get_at(ary, idx)); break; case Type_adp_.Tid__bry: bfr.Add_safe((byte[])Array_.Get_at(ary, idx)); break;
case Type_adp_.Tid__long: bfr.Add_long_variable(Long_.cast(Array_.Get_at(ary, idx))); break; case Type_adp_.Tid__long: bfr.Add_long_variable(Long_.cast(Array_.Get_at(ary, idx))); break;
case Type_adp_.Tid__int: bfr.Add_int_variable(Int_.cast(Array_.Get_at(ary, idx))); break; case Type_adp_.Tid__int: bfr.Add_int_variable(Int_.cast(Array_.Get_at(ary, idx))); break;
case Type_adp_.Tid__byte: bfr.Add_int_variable((int)(Byte_.cast(Array_.Get_at(ary, idx)))); break;
default: throw Err_.new_unhandled_default(type_id); default: throw Err_.new_unhandled_default(type_id);
} }
} }
@ -178,6 +180,7 @@ public class Gftest {
case Type_adp_.Tid__bry: eq = Bry_.Eq((byte[])expd_obj, (byte[])actl_obj); break; case Type_adp_.Tid__bry: eq = Bry_.Eq((byte[])expd_obj, (byte[])actl_obj); break;
case Type_adp_.Tid__long: eq = Long_.cast(expd_obj) == Long_.cast(actl_obj); break; case Type_adp_.Tid__long: eq = Long_.cast(expd_obj) == Long_.cast(actl_obj); break;
case Type_adp_.Tid__int: eq = Int_.cast(expd_obj) == Int_.cast(actl_obj); break; case Type_adp_.Tid__int: eq = Int_.cast(expd_obj) == Int_.cast(actl_obj); break;
case Type_adp_.Tid__byte: eq = Byte_.cast(expd_obj) == Byte_.cast(actl_obj); break;
} }
} }
if (!eq) { if (!eq) {

View File

@ -165,6 +165,9 @@ public class XomwMessage {
private static final int FORMAT_NULL = 5; private static final int FORMAT_NULL = 5;
// private static final int PRM_TID_BEFORE = 1; // "before"
// private static final int PRM_TID_AFTER = 2; // "after"
/** /**
* Mapping from Message::listParam() types to Language methods. * Mapping from Message::listParam() types to Language methods.
* @var array * @var array
@ -268,20 +271,20 @@ public class XomwMessage {
// throw new InvalidArgumentException('key must be a String or an array'); // throw new InvalidArgumentException('key must be a String or an array');
// } // }
// //
// $this->keysToTry = (array)key; // this.keysToTry = (array)key;
// //
// if ( empty( $this->keysToTry ) ) { // if (empty(this.keysToTry)) {
// throw new InvalidArgumentException('key must not be an empty list'); // throw new InvalidArgumentException('key must not be an empty list');
// } // }
// //
// $this->key = reset( $this->keysToTry ); // this.key = reset(this.keysToTry);
// //
// $this->parameters = array_values( $prmsVar ); // this.parameters = array_values($prmsVar);
// // User language is only resolved in getLanguage(). This helps preserve the // // User language is only resolved in getLanguage(). This helps preserve the
// // semantic intent of "user language" across serialize() and unserialize(). // // semantic intent of "user language" across serialize() and unserialize().
// $this->language = $language ?: false; // this.language = $language ?: false;
// } // }
//
// /** // /**
// * @see Serializable::serialize() // * @see Serializable::serialize()
// * @since 1.26 // * @since 1.26
@ -289,14 +292,14 @@ public class XomwMessage {
// */ // */
// public function serialize() { // public function serialize() {
// return serialize([ // return serialize([
// 'interfaceIsUserLang' => $this->interfaceIsUserLang, // 'interfaceIsUserLang' => this.interfaceIsUserLang,
// 'language' => $this->language ? $this->language->getCode() : false, // 'language' => this.language ? this.language->getCode() : false,
// 'key' => $this->key, // 'key' => this.key,
// 'keysToTry' => $this->keysToTry, // 'keysToTry' => this.keysToTry,
// 'parameters' => $this->parameters, // 'parameters' => this.parameters,
// 'format' => $this->format, // 'format' => this.format,
// 'useDatabase' => $this->useDatabase, // 'useDatabase' => this.useDatabase,
// 'title' => $this->title, // 'title' => this.title,
// ]); // ]);
// } // }
// //
@ -307,16 +310,16 @@ public class XomwMessage {
// */ // */
// public function unserialize($serialized) { // public function unserialize($serialized) {
// $data = unserialize($serialized); // $data = unserialize($serialized);
// $this->interfaceIsUserLang = $data['interfaceIsUserLang']; // this.interfaceIsUserLang = $data['interfaceIsUserLang'];
// $this->key = $data['key']; // this.key = $data['key'];
// $this->keysToTry = $data['keysToTry']; // this.keysToTry = $data['keysToTry'];
// $this->parameters = $data['parameters']; // this.parameters = $data['parameters'];
// $this->format = $data['format']; // this.format = $data['format'];
// $this->useDatabase = $data['useDatabase']; // this.useDatabase = $data['useDatabase'];
// $this->language = $data['language'] ? Language::factory( $data['language'] ) : false; // this.language = $data['language'] ? Language::factory($data['language']) : false;
// $this->title = $data['title']; // this.title = $data['title'];
// } // }
//
// /** // /**
// * @since 1.24 // * @since 1.24
// * // *
@ -324,7 +327,7 @@ public class XomwMessage {
// * constructor was a fallback list of keys to try. // * constructor was a fallback list of keys to try.
// */ // */
// public function isMultiKey() { // public function isMultiKey() {
// return count( $this->keysToTry ) > 1; // return count(this.keysToTry) > 1;
// } // }
// //
// /** // /**
@ -334,7 +337,7 @@ public class XomwMessage {
// * in order of preference. // * in order of preference.
// */ // */
// public function getKeysToTry() { // public function getKeysToTry() {
// return $this->keysToTry; // return this.keysToTry;
// } // }
// //
// /** // /**
@ -349,7 +352,7 @@ public class XomwMessage {
// * @return String // * @return String
// */ // */
// public function getKey() { // public function getKey() {
// return $this->key; // return this.key;
// } // }
// //
// /** // /**
@ -360,7 +363,7 @@ public class XomwMessage {
// * @return array // * @return array
// */ // */
// public function getParams() { // public function getParams() {
// return $this->parameters; // return this.parameters;
// } // }
// //
// /** // /**
@ -373,7 +376,7 @@ public class XomwMessage {
// */ // */
// public function getFormat() { // public function getFormat() {
// wfDeprecated(__METHOD__, '1.29'); // wfDeprecated(__METHOD__, '1.29');
// return $this->format; // return this.format;
// } // }
// //
// /** // /**
@ -385,7 +388,7 @@ public class XomwMessage {
// */ // */
// public function getLanguage() { // public function getLanguage() {
// // Defaults to false which means current user language // // Defaults to false which means current user language
// return $this->language ?: RequestContext::getMain()->getLanguage(); // return this.language ?: RequestContext::getMain()->getLanguage();
// } // }
// //
// /** // /**
@ -479,12 +482,12 @@ public class XomwMessage {
// public function getTitle() { // public function getTitle() {
// global $wgContLang, $wgForceUIMsgAsContentMsg; // global $wgContLang, $wgForceUIMsgAsContentMsg;
// //
// title = $this->key; // title = this.key;
// if ( // if (
// !$this->language->equals( $wgContLang ) // !this.language->equals($wgContLang)
// && in_array( $this->key, (array)$wgForceUIMsgAsContentMsg ) // && in_array(this.key, (array)$wgForceUIMsgAsContentMsg)
// ) { // ) {
// $code = $this->language->getCode(); // $code = this.language->getCode();
// title .= '/' . $code; // title .= '/' . $code;
// } // }
// //
@ -521,7 +524,7 @@ public class XomwMessage {
// } // }
// } // }
// //
// $this->parameters = array_merge( $this->parameters, array_values( $args ) ); // this.parameters = array_merge(this.parameters, array_values($args));
// return $this; // return $this;
// } // }
// //
@ -544,7 +547,7 @@ public class XomwMessage {
// $prmsVar = $prmsVar[0]; // $prmsVar = $prmsVar[0];
// } // }
// foreach ($prmsVar as $param) { // foreach ($prmsVar as $param) {
// $this->parameters[] = self::rawParam( $param ); // this.parameters[] = self::rawParam($param);
// } // }
// return $this; // return $this;
// } // }
@ -566,7 +569,7 @@ public class XomwMessage {
// $prmsVar = $prmsVar[0]; // $prmsVar = $prmsVar[0];
// } // }
// foreach ($prmsVar as $param) { // foreach ($prmsVar as $param) {
// $this->parameters[] = self::numParam( $param ); // this.parameters[] = self::numParam($param);
// } // }
// return $this; // return $this;
// } // }
@ -588,7 +591,7 @@ public class XomwMessage {
// $prmsVar = $prmsVar[0]; // $prmsVar = $prmsVar[0];
// } // }
// foreach ($prmsVar as $param) { // foreach ($prmsVar as $param) {
// $this->parameters[] = self::durationParam( $param ); // this.parameters[] = self::durationParam($param);
// } // }
// return $this; // return $this;
// } // }
@ -610,7 +613,7 @@ public class XomwMessage {
// $prmsVar = $prmsVar[0]; // $prmsVar = $prmsVar[0];
// } // }
// foreach ($prmsVar as $param) { // foreach ($prmsVar as $param) {
// $this->parameters[] = self::expiryParam( $param ); // this.parameters[] = self::expiryParam($param);
// } // }
// return $this; // return $this;
// } // }
@ -632,7 +635,7 @@ public class XomwMessage {
// $prmsVar = $prmsVar[0]; // $prmsVar = $prmsVar[0];
// } // }
// foreach ($prmsVar as $param) { // foreach ($prmsVar as $param) {
// $this->parameters[] = self::timeperiodParam( $param ); // this.parameters[] = self::timeperiodParam($param);
// } // }
// return $this; // return $this;
// } // }
@ -654,7 +657,7 @@ public class XomwMessage {
// $prmsVar = $prmsVar[0]; // $prmsVar = $prmsVar[0];
// } // }
// foreach ($prmsVar as $param) { // foreach ($prmsVar as $param) {
// $this->parameters[] = self::sizeParam( $param ); // this.parameters[] = self::sizeParam($param);
// } // }
// return $this; // return $this;
// } // }
@ -676,7 +679,7 @@ public class XomwMessage {
// $prmsVar = $prmsVar[0]; // $prmsVar = $prmsVar[0];
// } // }
// foreach ($prmsVar as $param) { // foreach ($prmsVar as $param) {
// $this->parameters[] = self::bitrateParam( $param ); // this.parameters[] = self::bitrateParam($param);
// } // }
// return $this; // return $this;
// } // }
@ -700,7 +703,7 @@ public class XomwMessage {
// $prmsVar = $prmsVar[0]; // $prmsVar = $prmsVar[0];
// } // }
// foreach ($prmsVar as $param) { // foreach ($prmsVar as $param) {
// $this->parameters[] = self::plaintextParam( $param ); // this.parameters[] = self::plaintextParam($param);
// } // }
// return $this; // return $this;
// } // }
@ -715,9 +718,9 @@ public class XomwMessage {
// * @return Message $this // * @return Message $this
// */ // */
// public function setContext(IContextSource $context) { // public function setContext(IContextSource $context) {
// $this->inLanguage( $context->getLanguage() ); // this.inLanguage($context->getLanguage());
// $this->title( $context->getTitle() ); // this.title($context->getTitle());
// $this->interfaceIsUserLang = true; // this.interfaceIsUserLang = true;
// //
// return $this; // return $this;
// } // }
@ -735,21 +738,21 @@ public class XomwMessage {
// */ // */
// public function inLanguage($lang) { // public function inLanguage($lang) {
// if ($lang instanceof Language) { // if ($lang instanceof Language) {
// $this->language = $lang; // this.language = $lang;
// } elseif (is_string($lang)) { // } elseif (is_string($lang)) {
// if ( !$this->language instanceof Language || $this->language->getCode() != $lang ) { // if (!this.language instanceof Language || this.language->getCode() != $lang) {
// $this->language = Language::factory( $lang ); // this.language = Language::factory($lang);
// } // }
// } elseif ($lang instanceof StubUserLang) { // } elseif ($lang instanceof StubUserLang) {
// $this->language = false; // this.language = false;
// } else { // } else {
// $type = gettype($lang); // $type = gettype($lang);
// throw new MWException(__METHOD__ . " must be " // throw new MWException(__METHOD__ . " must be "
// . "passed a String or Language Object; $type given" // . "passed a String or Language Object; $type given"
// ); // );
// } // }
// $this->message = null; // this.message = null;
// $this->interfaceIsUserLang = false; // this.interfaceIsUserLang = false;
// return $this; // return $this;
// } // }
@ -764,12 +767,12 @@ public class XomwMessage {
*/ */
public XomwMessage inContentLanguage() { public XomwMessage inContentLanguage() {
// global $wgForceUIMsgAsContentMsg; // global $wgForceUIMsgAsContentMsg;
// if ( in_array( $this->key, (array)$wgForceUIMsgAsContentMsg ) ) { // if (in_array(this.key, (array)$wgForceUIMsgAsContentMsg)) {
// return $this; // return $this;
// } // }
// //
// global $wgContLang; // global $wgContLang;
// $this->inLanguage( $wgContLang ); // this.inLanguage($wgContLang);
// return $this; // return $this;
return this; return this;
} }
@ -785,7 +788,7 @@ public class XomwMessage {
// * @return Message $this // * @return Message $this
// */ // */
// public function setInterfaceMessageFlag($interfaceIsUserLang) { // public function setInterfaceMessageFlag($interfaceIsUserLang) {
// $this->interfaceIsUserLang = (boolean)$interfaceIsUserLang; // this.interfaceIsUserLang = (boolean)$interfaceIsUserLang;
// return $this; // return $this;
// } // }
// //
@ -799,8 +802,8 @@ public class XomwMessage {
// * @return Message $this // * @return Message $this
// */ // */
// public function useDatabase($useDatabase) { // public function useDatabase($useDatabase) {
// $this->useDatabase = (boolean)$useDatabase; // this.useDatabase = (boolean)$useDatabase;
// $this->message = null; // this.message = null;
// return $this; // return $this;
// } // }
// //
@ -814,7 +817,7 @@ public class XomwMessage {
// * @return Message $this // * @return Message $this
// */ // */
// public function title(title) { // public function title(title) {
// $this->title = title; // this.title = title;
// return $this; // return $this;
// } // }
@ -844,7 +847,7 @@ public class XomwMessage {
*/ */
// NOTE: causes issues in C# source; E2A7BC; http://www.fileformat.info/info/unicode/char/29fc/index.htm // NOTE: causes issues in C# source; E2A7BC; http://www.fileformat.info/info/unicode/char/29fc/index.htm
private static final byte[] LeftPointingCurvedAngleBracket = Bry_.New_by_ints(226, 167, 188); private static final byte[] LeftPointingCurvedAngleBracket = gplx.core.encoders.Hex_utl_.Parse_hex_to_bry("E2A7BC");
public byte[] toString(int format) { public byte[] toString(int format) {
if (format == FORMAT_NULL) { if (format == FORMAT_NULL) {
Gfo_usr_dlg_.Instance.Warn_many("", "", "toString import implicit.*; format=~{0} key=~{1}", format, key); Gfo_usr_dlg_.Instance.Warn_many("", "", "toString import implicit.*; format=~{0} key=~{1}", format, key);
@ -864,15 +867,15 @@ public class XomwMessage {
return Bry_.Escape_html(Bry_.Add(LeftPointingCurvedAngleBracket, key, LeftPointingCurvedAngleBracket)); return Bry_.Escape_html(Bry_.Add(LeftPointingCurvedAngleBracket, key, LeftPointingCurvedAngleBracket));
} }
// // Replace $* with a list of parameters for &uselang=qqx. // Replace $* with a list of parameters for &uselang=qqx.
// if (strpos(s, "$*") != false) { // if (XophpString.strpos(s, "$*") != false) {
// String paramlist = ""; // String paramlist = "";
// if (this.parameters != []) { // if (this.parameters != []) {
// paramlist = ": $" . implode(", $", range(1, count(this.parameters))); // paramlist = ": $" . implode(", $", range(1, count(this.parameters)));
// } // }
// s = str_replace("$*", paramlist, s); // s = str_replace("$*", paramlist, s);
// } // }
//
// // Replace parameters before text parsing // // Replace parameters before text parsing
// s = this.replaceParameters(s, "before", format); // s = this.replaceParameters(s, "before", format);
// //
@ -909,16 +912,16 @@ public class XomwMessage {
// // trigger a fatal error if it does. So, catch any exceptions. // // trigger a fatal error if it does. So, catch any exceptions.
// //
// try { // try {
// return $this->toString( self::FORMAT_PARSE ); // return this.toString(self::FORMAT_PARSE);
// } catch (Exception $ex) { // } catch (Exception $ex) {
// try { // try {
// trigger_error( "Exception caught in " . __METHOD__ . " (message " . $this->key . "): " // trigger_error("Exception caught in " . __METHOD__ . " (message " . this.key . "): "
// . $ex, E_USER_WARNING); // . $ex, E_USER_WARNING);
// } catch (Exception $ex) { // } catch (Exception $ex) {
// // Doh! Cause a fatal error after all? // // Doh! Cause a fatal error after all?
// } // }
// //
// return '⧼' . htmlspecialchars( $this->key ) . '⧽'; // return '⧼' . htmlspecialchars(this.key) . '⧽';
// } // }
// } // }
// //
@ -930,8 +933,8 @@ public class XomwMessage {
// * @return String Parsed HTML. // * @return String Parsed HTML.
// */ // */
// public function parse() { // public function parse() {
// $this->format = self::FORMAT_PARSE; // this.format = self::FORMAT_PARSE;
// return $this->toString( self::FORMAT_PARSE ); // return this.toString(self::FORMAT_PARSE);
// } // }
/** /**
@ -954,8 +957,8 @@ public class XomwMessage {
// * @return String Unescaped untransformed message text. // * @return String Unescaped untransformed message text.
// */ // */
// public function plain() { // public function plain() {
// $this->format = self::FORMAT_PLAIN; // this.format = self::FORMAT_PLAIN;
// return $this->toString( self::FORMAT_PLAIN ); // return this.toString(self::FORMAT_PLAIN);
// } // }
// //
// /** // /**
@ -966,8 +969,8 @@ public class XomwMessage {
// * @return String HTML // * @return String HTML
// */ // */
// public function parseAsBlock() { // public function parseAsBlock() {
// $this->format = self::FORMAT_BLOCK_PARSE; // this.format = self::FORMAT_BLOCK_PARSE;
// return $this->toString( self::FORMAT_BLOCK_PARSE ); // return this.toString(self::FORMAT_BLOCK_PARSE);
// } // }
// //
// /** // /**
@ -979,8 +982,8 @@ public class XomwMessage {
// * @return String Escaped message text. // * @return String Escaped message text.
// */ // */
// public function escaped() { // public function escaped() {
// $this->format = self::FORMAT_ESCAPED; // this.format = self::FORMAT_ESCAPED;
// return $this->toString( self::FORMAT_ESCAPED ); // return this.toString(self::FORMAT_ESCAPED);
// } // }
// //
// /** // /**
@ -991,7 +994,7 @@ public class XomwMessage {
// * @return boolean // * @return boolean
// */ // */
// public function exists() { // public function exists() {
// return $this->fetchMessage() !== false; // return this.fetchMessage() !== false;
// } // }
// //
// /** // /**
@ -1003,7 +1006,7 @@ public class XomwMessage {
// * @return boolean // * @return boolean
// */ // */
// public function isBlank() { // public function isBlank() {
// $message = $this->fetchMessage(); // $message = this.fetchMessage();
// return $message === false || $message === ''; // return $message === false || $message === '';
// } // }
// //
@ -1015,7 +1018,7 @@ public class XomwMessage {
// * @return boolean // * @return boolean
// */ // */
// public function isDisabled() { // public function isDisabled() {
// $message = $this->fetchMessage(); // $message = this.fetchMessage();
// return $message === false || $message === '' || $message === '-'; // return $message === false || $message === '' || $message === '-';
// } // }
// //
@ -1122,64 +1125,66 @@ public class XomwMessage {
// } // }
// return [ 'list' => $list, 'type' => $type ]; // return [ 'list' => $list, 'type' => $type ];
// } // }
//
// /** /**
// * Substitutes any parameters into the message text. * Substitutes any parameters into the message text.
// * *
// * @since 1.17 * @since 1.17
// * *
// * @param String $message The message text. * @param String $message The message text.
// * @param String $type Either "before" or "after". * @param String $type Either "before" or "after".
// * @param String $format One of the FORMAT_* constants. * @param String $format One of the FORMAT_* constants.
// * *
// * @return String * @return String
// */ */
// protected function replaceParameters( $message, $type = 'before', $format ) { // DFLT:type="before"
// private byte[] replaceParameters(byte[] message, int prm_tid, int format) {
// $replacementKeys = []; // $replacementKeys = [];
// foreach ( $this->parameters as $n => $param ) { // foreach (this.parameters as $n => $param) {
// list( $paramType, $value ) = $this->extractParam( $param, $format ); // list($paramType, $value) = this.extractParam($param, $format);
// if ($type === $paramType) { // if ($type === $paramType) {
// $replacementKeys['$' . ($n + 1)] = $value; // $replacementKeys['$' . ($n + 1)] = $value;
// } // }
// } // }
// $message = strtr($message, $replacementKeys); // $message = strtr($message, $replacementKeys);
// return $message; // return $message;
// return null;
// } // }
//
// /** /**
// * Extracts the parameter type and preprocessed the value if needed. * Extracts the parameter type and preprocessed the value if needed.
// * *
// * @since 1.18 * @since 1.18
// * *
// * @param mixed $param Parameter as defined in this cls. * @param mixed $param Parameter as defined in this cls.
// * @param String $format One of the FORMAT_* constants. * @param String $format One of the FORMAT_* constants.
// * *
// * @return array Array with the parameter type (either "before" or "after") and the value. * @return array Array with the parameter type (either "before" or "after") and the value.
// */ */
// protected function extractParam( $param, $format ) { // private void extractParam(XomwMessageVal rv, Object param, int format) {
// if (is_array($param)) { // if (is_array($param)) {
// if (isset($param['raw'])) { // if (isset($param['raw'])) {
// return [ 'after', $param['raw'] ]; // return [ 'after', $param['raw'] ];
// } elseif (isset($param['num'])) { // } elseif (isset($param['num'])) {
// // Replace number prmsVar always in before step for now. // // Replace number prmsVar always in before step for now.
// // No support for combined raw and num prmsVar // // No support for combined raw and num prmsVar
// return [ 'before', $this->getLanguage()->formatNum( $param['num'] ) ]; // return [ 'before', this.getLanguage()->formatNum($param['num']) ];
// } elseif (isset($param['duration'])) { // } elseif (isset($param['duration'])) {
// return [ 'before', $this->getLanguage()->formatDuration( $param['duration'] ) ]; // return [ 'before', this.getLanguage()->formatDuration($param['duration']) ];
// } elseif (isset($param['expiry'])) { // } elseif (isset($param['expiry'])) {
// return [ 'before', $this->getLanguage()->formatExpiry( $param['expiry'] ) ]; // return [ 'before', this.getLanguage()->formatExpiry($param['expiry']) ];
// } elseif (isset($param['period'])) { // } elseif (isset($param['period'])) {
// return [ 'before', $this->getLanguage()->formatTimePeriod( $param['period'] ) ]; // return [ 'before', this.getLanguage()->formatTimePeriod($param['period']) ];
// } elseif (isset($param['size'])) { // } elseif (isset($param['size'])) {
// return [ 'before', $this->getLanguage()->formatSize( $param['size'] ) ]; // return [ 'before', this.getLanguage()->formatSize($param['size']) ];
// } elseif (isset($param['bitrate'])) { // } elseif (isset($param['bitrate'])) {
// return [ 'before', $this->getLanguage()->formatBitrate( $param['bitrate'] ) ]; // return [ 'before', this.getLanguage()->formatBitrate($param['bitrate']) ];
// } elseif (isset($param['plaintext'])) { // } elseif (isset($param['plaintext'])) {
// return [ 'after', $this->formatPlaintext( $param['plaintext'], $format ) ]; // return [ 'after', this.formatPlaintext($param['plaintext'], $format) ];
// } elseif (isset($param['list'])) { // } elseif (isset($param['list'])) {
// return $this->formatListParam( $param['list'], $param['type'], $format ); // return this.formatListParam($param['list'], $param['type'], $format);
// } else { // } else {
// $warning = 'Invalid parameter for message "' . $this->getKey() . '": ' . // $warning = 'Invalid parameter for message "' . this.getKey() . '": ' .
// htmlspecialchars(serialize($param)); // htmlspecialchars(serialize($param));
// trigger_error($warning, E_USER_WARNING); // trigger_error($warning, E_USER_WARNING);
// $e = new Exception; // $e = new Exception;
@ -1187,17 +1192,18 @@ public class XomwMessage {
// //
// return [ 'before', '[INVALID]' ]; // return [ 'before', '[INVALID]' ];
// } // }
// } elseif ( $param instanceof Message ) { // }
// else if ($param instanceof Message) {
// // Match language, flags, etc. to the current message. // // Match language, flags, etc. to the current message.
// $msg = clone $param; // $msg = clone $param;
// if ( $msg->language !== $this->language || $msg->useDatabase !== $this->useDatabase ) { // if ($msg->language !== this.language || $msg->useDatabase !== this.useDatabase) {
// // Cache depends on these parameters // // Cache depends on these parameters
// $msg->message = null; // $msg->message = null;
// } // }
// $msg->interfaceIsUserLang = $this->interfaceIsUserLang; // $msg->interfaceIsUserLang = this.interfaceIsUserLang;
// $msg->language = $this->language; // $msg->language = this.language;
// $msg->useDatabase = $this->useDatabase; // $msg->useDatabase = this.useDatabase;
// $msg->title = $this->title; // $msg->title = this.title;
// //
// // DWIM // // DWIM
// if ($format === 'block-parse') { // if ($format === 'block-parse') {
@ -1209,11 +1215,12 @@ public class XomwMessage {
// // then they'll get double escaped. If the message needs to be // // then they'll get double escaped. If the message needs to be
// // escaped, it'll happen right here when we call toString(). // // escaped, it'll happen right here when we call toString().
// return [ 'after', $msg->toString($format) ]; // return [ 'after', $msg->toString($format) ];
// } else { // }
// return [ 'before', $param ]; // else {
// rv.Set(PRM_TID_BEFORE, param);
// } // }
// } // }
//
// /** // /**
// * Wrapper for what ever method we use to parse wikitext. // * Wrapper for what ever method we use to parse wikitext.
// * // *
@ -1226,10 +1233,10 @@ public class XomwMessage {
// protected function parseText($String) { // protected function parseText($String) {
// $out = MessageCache::singleton()->parse( // $out = MessageCache::singleton()->parse(
// $String, // $String,
// $this->title, // this.title,
// /*linestart*/true, // /*linestart*/true,
// $this->interfaceIsUserLang, // this.interfaceIsUserLang,
// $this->getLanguage() // this.getLanguage()
// ); // );
// //
// return $out instanceof ParserOutput ? $out->getText() : $out; // return $out instanceof ParserOutput ? $out->getText() : $out;
@ -1247,9 +1254,9 @@ public class XomwMessage {
// protected function transformText($String) { // protected function transformText($String) {
// return MessageCache::singleton()->transform( // return MessageCache::singleton()->transform(
// $String, // $String,
// $this->interfaceIsUserLang, // this.interfaceIsUserLang,
// $this->getLanguage(), // this.getLanguage(),
// $this->title // this.title
// ); // );
// } // }
// //
@ -1262,11 +1269,11 @@ public class XomwMessage {
// * @throws MWException If message key array is empty. // * @throws MWException If message key array is empty.
// */ // */
// protected function fetchMessage() { // protected function fetchMessage() {
// if ( $this->message === null ) { // if (this.message === null) {
// $cache = MessageCache::singleton(); // $cache = MessageCache::singleton();
// //
// foreach ( $this->keysToTry as key ) { // foreach (this.keysToTry as key) {
// $message = $cache->get( key, $this->useDatabase, $this->getLanguage() ); // $message = $cache->get(key, this.useDatabase, this.getLanguage());
// if ($message !== false && $message !== '') { // if ($message !== false && $message !== '') {
// break; // break;
// } // }
@ -1274,10 +1281,10 @@ public class XomwMessage {
// //
// // NOTE: The constructor makes sure keysToTry isn't empty, // // NOTE: The constructor makes sure keysToTry isn't empty,
// // so we know that key and $message are initialized. // // so we know that key and $message are initialized.
// $this->key = key; // this.key = key;
// $this->message = $message; // this.message = $message;
// } // }
// return $this->message; // return this.message;
// } // }
// //
// /** // /**
@ -1317,7 +1324,7 @@ public class XomwMessage {
// */ // */
// protected function formatListParam(array $prmsVar, $listType, $format) { // protected function formatListParam(array $prmsVar, $listType, $format) {
// if (!isset(self::$listTypeMap[$listType])) { // if (!isset(self::$listTypeMap[$listType])) {
// $warning = 'Invalid list type for message "' . $this->getKey() . '": ' // $warning = 'Invalid list type for message "' . this.getKey() . '": '
// . htmlspecialchars($listType) // . htmlspecialchars($listType)
// . ' (prmsVar are ' . htmlspecialchars(serialize($prmsVar)) . ')'; // . ' (prmsVar are ' . htmlspecialchars(serialize($prmsVar)) . ')';
// trigger_error($warning, E_USER_WARNING); // trigger_error($warning, E_USER_WARNING);
@ -1329,7 +1336,7 @@ public class XomwMessage {
// //
// // Handle an empty list sensibly // // Handle an empty list sensibly
// if (!$prmsVar) { // if (!$prmsVar) {
// return [ 'before', $this->getLanguage()->$func( [] ) ]; // return [ 'before', this.getLanguage()->$func([]) ];
// } // }
// //
// // First, determine what kinds of list items we have // // First, determine what kinds of list items we have
@ -1337,7 +1344,7 @@ public class XomwMessage {
// $vars = []; // $vars = [];
// $list = []; // $list = [];
// foreach ($prmsVar as $n => $p) { // foreach ($prmsVar as $n => $p) {
// list( $type, $value ) = $this->extractParam( $p, $format ); // list($type, $value) = this.extractParam($p, $format);
// $types[$type] = true; // $types[$type] = true;
// $list[] = $value; // $list[] = $value;
// $vars[] = '$' . ($n + 1); // $vars[] = '$' . ($n + 1);
@ -1346,13 +1353,81 @@ public class XomwMessage {
// // Easy case: all are 'before' or 'after', so just join the // // Easy case: all are 'before' or 'after', so just join the
// // values and use the same type. // // values and use the same type.
// if (count($types) === 1) { // if (count($types) === 1) {
// return [ key( $types ), $this->getLanguage()->$func( $list ) ]; // return [ key($types), this.getLanguage()->$func($list) ];
// } // }
// //
// // Hard case: We need to process each value per its type, then // // Hard case: We need to process each value per its type, then
// // return the concatenated values as 'after'. We handle this by turning // // return the concatenated values as 'after'. We handle this by turning
// // the list into a RawMessage and processing that as a parameter. // // the list into a RawMessage and processing that as a parameter.
// $vars = $this->getLanguage()->$func( $vars ); // $vars = this.getLanguage()->$func($vars);
// return $this->extractParam( new RawMessage( $vars, $prmsVar ), $format ); // return this.extractParam(new RawMessage($vars, $prmsVar), $format);
// } // }
} }
class XomwMessageVal {
public int tid;
public Object val;
public void Set(int tid, Object val) {
this.tid = tid;
this.val = val;
}
}
abstract class XomwMessagePrm {
public XomwMessagePrm(int tid) {this.tid = tid;}
public int Tid() {return tid;} private int tid;
public static final int
Tid__raw = 0
, Tid__num = 1
, Tid__duration = 2
, Tid__expiry = 3
, Tid__period = 4
, Tid__size = 5
, Tid__bitrate = 6
, Tid__plaintext = 7
, Tid__list = 8
;
}
class XomwMessagePrm_raw extends XomwMessagePrm { public byte[] raw;
public XomwMessagePrm_raw(byte[] raw) {super(Tid__raw);
this.raw = raw;
}
}
class XomwMessagePrm_num extends XomwMessagePrm { public int num;
public XomwMessagePrm_num(int num) {super(Tid__num);
this.num = num;
}
}
class XomwMessagePrm_duration extends XomwMessagePrm { public int duration;
public XomwMessagePrm_duration(int duration) {super(Tid__duration);
this.duration = duration;
}
}
class XomwMessagePrm_expiry extends XomwMessagePrm { public byte[] expiry;
public XomwMessagePrm_expiry(byte[] expiry) {super(Tid__expiry);
this.expiry = expiry;
}
}
class XomwMessagePrm_period extends XomwMessagePrm { public int period;
public XomwMessagePrm_period(int period) {super(Tid__period);
this.period = period;
}
}
class XomwMessagePrm_size extends XomwMessagePrm { public int size;
public XomwMessagePrm_size(int size) {super(Tid__size);
this.size = size;
}
}
class XomwMessagePrm_bitrate extends XomwMessagePrm { public int bitrate;
public XomwMessagePrm_bitrate(int bitrate) {super(Tid__bitrate);
this.bitrate = bitrate;
}
}
class XomwMessagePrm_plaintext extends XomwMessagePrm { public byte[] plaintext;
public XomwMessagePrm_plaintext(byte[] plaintext) {super(Tid__plaintext);
this.plaintext = plaintext;
}
}
class XomwMessagePrm_list extends XomwMessagePrm { public byte[][] list;
public XomwMessagePrm_list(byte[][] list) {super(Tid__list);
this.list = list;
}
}