mirror of
https://github.com/gnosygnu/xowa.git
synced 2025-05-31 22:44:34 +00:00
XOMW: More XomwHooks implementation [#632]
This commit is contained in:
parent
0c3cb1ba3d
commit
a6b128422e
@ -247,4 +247,29 @@ public class XophpArray_ {
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// REF.PHP: https://www.php.net/manual/en/function.array-shift.php
|
||||
// Returns the shifted value, or NULL if array is empty or is not an array.
|
||||
public static Object array_shift(XophpArray array) {
|
||||
if (array == null) {
|
||||
return null;
|
||||
}
|
||||
int len = array.Len();
|
||||
if (len == 0) {
|
||||
return null;
|
||||
}
|
||||
XophpArrayItm[] itms = array.To_ary();
|
||||
array.Clear();
|
||||
int idx = 0;
|
||||
for (int i = 1; i < len; i++) {
|
||||
XophpArrayItm itm = itms[i];
|
||||
if (itm.Key_is_int()) {
|
||||
array.Add(idx++, itm.Val());
|
||||
}
|
||||
else {
|
||||
array.Add(itm.Key(), itm.Val());
|
||||
}
|
||||
}
|
||||
return itms[0].Val();
|
||||
}
|
||||
}
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -19,9 +19,8 @@ package gplx.xowa.mediawiki.includes;
|
||||
import gplx.xowa.mediawiki.*;
|
||||
/*
|
||||
TODO:
|
||||
* $wgHooks
|
||||
* array_shift
|
||||
* Closure?
|
||||
* class XophpClosure: https://www.php.net/manual/en/class.closure.php
|
||||
* array_filter: https://www.php.net/manual/en/function.array-filter.php
|
||||
*/
|
||||
|
||||
/**
|
||||
@ -78,9 +77,7 @@ public class XomwHooks {
|
||||
* @return bool True if the hook has a function registered to it
|
||||
*/
|
||||
public static boolean isRegistered(String name) {
|
||||
// global $wgHooks;
|
||||
// return !XophpArray_.empty($wgHooks[$name]) || !XophpArray_.empty(handlers[$name]);
|
||||
return false;
|
||||
return !XophpObject_.empty_obj(XomwDefaultSettings.wgHooks.Get_by(name)) || !XophpObject_.empty_obj(handlers.Get_by(name));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -93,17 +90,14 @@ public class XomwHooks {
|
||||
* @return array
|
||||
*/
|
||||
public static XophpArray getHandlers(String name) {
|
||||
// global $wgHooks;
|
||||
|
||||
if (!isRegistered(name)) {
|
||||
return XophpArray.New();
|
||||
// } else if (!XophpArray_.isset(handlers, name)) {
|
||||
// return $wgHooks[name];
|
||||
// } else if (!isset($wgHooks[name])) {
|
||||
// return handlers[name];
|
||||
} else if (!XophpArray_.isset(handlers, name)) {
|
||||
return XomwDefaultSettings.wgHooks.Get_by_ary(name);
|
||||
} else if (!XophpArray_.isset(XomwDefaultSettings.wgHooks, name)) {
|
||||
return handlers.Get_by_ary(name);
|
||||
} else {
|
||||
// return XophpArray_.array_merge(handlers.Get_by_ary(name), $wgHooks[name]);
|
||||
return XophpArray_.array_merge(handlers.Get_by_ary(name));
|
||||
return XophpArray_.array_merge(handlers.Get_by_ary(name), XomwDefaultSettings.wgHooks.Get_by_ary(name));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012-2017 gnosygnu@gmail.com
|
||||
Copyright (C) 2012-2020 gnosygnu@gmail.com
|
||||
|
||||
XOWA is licensed under the terms of the General Public License (GPL) Version 3,
|
||||
or alternatively under the terms of the Apache License Version 2.0.
|
||||
@ -13,60 +13,65 @@ The terms of each license can be found in the source code repository:
|
||||
GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
|
||||
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
|
||||
*/
|
||||
package gplx.xowa.mediawiki; import gplx.*; import gplx.xowa.*;
|
||||
import org.junit.*; import gplx.core.tests.*;
|
||||
public class XophpArray__tst { // REF:https://www.php.net/manual/en/function.array-merge.php
|
||||
package gplx.xowa.mediawiki;
|
||||
|
||||
import gplx.Bool_;
|
||||
import gplx.core.tests.Gftest;
|
||||
import org.junit.Test;
|
||||
|
||||
public class XophpArray__tst {
|
||||
private final XophpArray__fxt fxt = new XophpArray__fxt();
|
||||
@Test public void array_merge__basic() {
|
||||
// REF.PHP:https://www.php.net/manual/en/function.array-merge.php
|
||||
@Test public void array_merge__basic() {
|
||||
XophpArray ary1 = fxt.Make().Add("key1", "val1").Add("a");
|
||||
XophpArray ary2 = fxt.Make().Add("key2", "val2").Add("b");
|
||||
fxt.Test__eq
|
||||
( fxt.Make().Add("key1", "val1").Add("a").Add("key2", "val2").Add("b")
|
||||
, XophpArray_.array_merge(ary1, ary2));
|
||||
}
|
||||
@Test public void array_merge__same_key() {
|
||||
@Test public void array_merge__same_key() {
|
||||
XophpArray ary1 = fxt.Make().Add("key", "val1");
|
||||
XophpArray ary2 = fxt.Make().Add("key", "val2");
|
||||
fxt.Test__eq
|
||||
( fxt.Make().Add("key", "val2")
|
||||
, XophpArray_.array_merge(ary1, ary2));
|
||||
}
|
||||
@Test public void array_merge__same_idx() {
|
||||
@Test public void array_merge__same_idx() {
|
||||
XophpArray ary1 = fxt.Make().Add(0, "a");
|
||||
XophpArray ary2 = fxt.Make().Add(0, "b");
|
||||
fxt.Test__eq
|
||||
( fxt.Make().Add(0, "a").Add(1, "b")
|
||||
, XophpArray_.array_merge(ary1, ary2));
|
||||
}
|
||||
@Test public void array_merge__renumber() {
|
||||
@Test public void array_merge__renumber() {
|
||||
XophpArray ary1 = fxt.Make().Add(3, "a");
|
||||
XophpArray ary2 = fxt.Make().Add(2, "b");
|
||||
fxt.Test__eq
|
||||
( fxt.Make().Add(0, "a").Add(1, "b")
|
||||
, XophpArray_.array_merge(ary1, ary2));
|
||||
}
|
||||
@Test public void array_merge__example_1() {
|
||||
@Test public void array_merge__example_1() {
|
||||
XophpArray ary1 = fxt.Make().Add("color", "red").Add_many(2, 4);
|
||||
XophpArray ary2 = fxt.Make().Add_many("a", "b").Add("color", "green").Add("shape", "trapezoid").Add(4);
|
||||
fxt.Test__eq
|
||||
( fxt.Make().Add("color", "green").Add_many(2, 4, "a", "b").Add("shape", "trapezoid").Add(4)
|
||||
, XophpArray_.array_merge(ary1, ary2));
|
||||
}
|
||||
@Test public void array_merge__example_2() {
|
||||
@Test public void array_merge__example_2() {
|
||||
XophpArray ary1 = fxt.Make();
|
||||
XophpArray ary2 = fxt.Make().Add(1, "data");
|
||||
fxt.Test__eq
|
||||
( fxt.Make().Add(0, "data")
|
||||
, XophpArray_.array_merge(ary1, ary2));
|
||||
}
|
||||
@Test public void array_add() {
|
||||
@Test public void array_add() {
|
||||
XophpArray ary1 = fxt.Make().Add(0, "zero_a").Add(2, "two_a").Add(3, "three_a");
|
||||
XophpArray ary2 = fxt.Make().Add(1, "one_b").Add(3, "three_b").Add(4, "four_b");
|
||||
fxt.Test__eq
|
||||
( fxt.Make().Add(0, "zero_a").Add(2, "two_a").Add(3, "three_a").Add(1, "one_b").Add(4, "four_b")
|
||||
, XophpArray_.array_add(ary1, ary2));
|
||||
}
|
||||
@Test public void array_splice__bgn_is_positive() {
|
||||
@Test public void array_splice__bgn_is_positive() {
|
||||
XophpArray src = fxt.Make().Add_many("a", "b", "c", "d");
|
||||
XophpArray del = XophpArray_.array_splice(src, 1);
|
||||
fxt.Test__eq
|
||||
@ -78,7 +83,7 @@ public class XophpArray__tst { // REF:https://www.php.net/manual/en/function.arr
|
||||
, del
|
||||
);
|
||||
}
|
||||
@Test public void array_splice__bgn_is_positive_large() {
|
||||
@Test public void array_splice__bgn_is_positive_large() {
|
||||
XophpArray src = fxt.Make().Add_many("a", "b", "c", "d");
|
||||
XophpArray del = XophpArray_.array_splice(src, 99);
|
||||
fxt.Test__eq
|
||||
@ -90,7 +95,7 @@ public class XophpArray__tst { // REF:https://www.php.net/manual/en/function.arr
|
||||
, del
|
||||
);
|
||||
}
|
||||
@Test public void array_splice__bgn_is_negative() {
|
||||
@Test public void array_splice__bgn_is_negative() {
|
||||
XophpArray src = fxt.Make().Add_many("a", "b", "c", "d");
|
||||
XophpArray del = XophpArray_.array_splice(src, -3);
|
||||
fxt.Test__eq
|
||||
@ -102,7 +107,7 @@ public class XophpArray__tst { // REF:https://www.php.net/manual/en/function.arr
|
||||
, del
|
||||
);
|
||||
}
|
||||
@Test public void array_splice__bgn_is_negative_large() {
|
||||
@Test public void array_splice__bgn_is_negative_large() {
|
||||
XophpArray src = fxt.Make().Add_many("a", "b", "c", "d");
|
||||
XophpArray del = XophpArray_.array_splice(src, -99);
|
||||
fxt.Test__eq
|
||||
@ -114,7 +119,7 @@ public class XophpArray__tst { // REF:https://www.php.net/manual/en/function.arr
|
||||
, del
|
||||
);
|
||||
}
|
||||
@Test public void array_splice__len_is_positive() {
|
||||
@Test public void array_splice__len_is_positive() {
|
||||
XophpArray src = fxt.Make().Add_many("a", "b", "c", "d");
|
||||
XophpArray del = XophpArray_.array_splice(src, 1, 2);
|
||||
fxt.Test__eq
|
||||
@ -126,7 +131,7 @@ public class XophpArray__tst { // REF:https://www.php.net/manual/en/function.arr
|
||||
, del
|
||||
);
|
||||
}
|
||||
@Test public void array_splice__len_is_positive_large() {
|
||||
@Test public void array_splice__len_is_positive_large() {
|
||||
XophpArray src = fxt.Make().Add_many("a", "b", "c", "d");
|
||||
XophpArray del = XophpArray_.array_splice(src, 1, 99);
|
||||
fxt.Test__eq
|
||||
@ -138,7 +143,7 @@ public class XophpArray__tst { // REF:https://www.php.net/manual/en/function.arr
|
||||
, del
|
||||
);
|
||||
}
|
||||
@Test public void array_splice__len_is_negative() {
|
||||
@Test public void array_splice__len_is_negative() {
|
||||
XophpArray src = fxt.Make().Add_many("a", "b", "c", "d");
|
||||
XophpArray del = XophpArray_.array_splice(src, 1, -2);
|
||||
fxt.Test__eq
|
||||
@ -150,7 +155,7 @@ public class XophpArray__tst { // REF:https://www.php.net/manual/en/function.arr
|
||||
, del
|
||||
);
|
||||
}
|
||||
@Test public void array_splice__len_is_negative_large() {
|
||||
@Test public void array_splice__len_is_negative_large() {
|
||||
XophpArray src = fxt.Make().Add_many("a", "b", "c", "d");
|
||||
XophpArray del = XophpArray_.array_splice(src, 1, -99);
|
||||
fxt.Test__eq
|
||||
@ -162,7 +167,7 @@ public class XophpArray__tst { // REF:https://www.php.net/manual/en/function.arr
|
||||
, del
|
||||
);
|
||||
}
|
||||
@Test public void array_splice__repl() {
|
||||
@Test public void array_splice__repl() {
|
||||
XophpArray src = fxt.Make().Add(0, "a").Add(1, "b").Add(2, "c").Add(3, "d");
|
||||
XophpArray del = XophpArray_.array_splice(src, 1, 2, fxt.Make().Add(0, "x"));
|
||||
fxt.Test__eq
|
||||
@ -174,7 +179,7 @@ public class XophpArray__tst { // REF:https://www.php.net/manual/en/function.arr
|
||||
, del
|
||||
);
|
||||
}
|
||||
@Test public void array_splice__example_1a() {
|
||||
@Test public void array_splice__example_1a() {
|
||||
XophpArray src = fxt.Make().Add_many("red", "green", "blue", "yellow");
|
||||
XophpArray del = XophpArray_.array_splice(src, 2);
|
||||
fxt.Test__eq
|
||||
@ -186,7 +191,7 @@ public class XophpArray__tst { // REF:https://www.php.net/manual/en/function.arr
|
||||
, del
|
||||
);
|
||||
}
|
||||
@Test public void array_splice__example_1b() {
|
||||
@Test public void array_splice__example_1b() {
|
||||
XophpArray src = fxt.Make().Add_many("red", "green", "blue", "yellow");
|
||||
XophpArray del = XophpArray_.array_splice(src, 1, -1);
|
||||
fxt.Test__eq
|
||||
@ -198,7 +203,7 @@ public class XophpArray__tst { // REF:https://www.php.net/manual/en/function.arr
|
||||
, del
|
||||
);
|
||||
}
|
||||
@Test public void array_splice__example_1c() {
|
||||
@Test public void array_splice__example_1c() {
|
||||
XophpArray src = fxt.Make().Add_many("red", "green", "blue", "yellow");
|
||||
XophpArray del = XophpArray_.array_splice(src, 1, 4, XophpArray.New("orange"));
|
||||
fxt.Test__eq
|
||||
@ -210,7 +215,7 @@ public class XophpArray__tst { // REF:https://www.php.net/manual/en/function.arr
|
||||
, del
|
||||
);
|
||||
}
|
||||
@Test public void array_splice__example_1d() {
|
||||
@Test public void array_splice__example_1d() {
|
||||
XophpArray src = fxt.Make().Add_many("red", "green", "blue", "yellow");
|
||||
XophpArray del = XophpArray_.array_splice(src, -1, 1, XophpArray.New("black", "maroon"));
|
||||
fxt.Test__eq
|
||||
@ -222,42 +227,42 @@ public class XophpArray__tst { // REF:https://www.php.net/manual/en/function.arr
|
||||
, del
|
||||
);
|
||||
}
|
||||
@Test public void values() {
|
||||
@Test public void values() {
|
||||
XophpArray orig = fxt.Make().Add("size", "XL").Add("color", "gold");
|
||||
fxt.Test__eq
|
||||
( fxt.Make().Add(0, "XL").Add(1, "gold")
|
||||
, orig.values()
|
||||
);
|
||||
}
|
||||
@Test public void array_map() {
|
||||
@Test public void array_map() {
|
||||
XophpArray orig = fxt.Make().Add_many("a", "b", "c");
|
||||
fxt.Test__eq
|
||||
( fxt.Make().Add_many("A", "B", "C")
|
||||
, XophpArray_.array_map(XophpString_.Callback_owner, "strtoupper", orig)
|
||||
);
|
||||
}
|
||||
@Test public void array_flip__basic() {
|
||||
@Test public void array_flip__basic() {
|
||||
XophpArray orig = fxt.Make().Add_many("oranges", "apples", "pears");
|
||||
fxt.Test__eq
|
||||
( fxt.Make().Add("oranges", 0).Add("apples", 1).Add("pears", 2)
|
||||
, XophpArray_.array_flip(orig)
|
||||
);
|
||||
}
|
||||
@Test public void array_flip__collision() {
|
||||
@Test public void array_flip__collision() {
|
||||
XophpArray orig = fxt.Make().Add("a", 1).Add("b", 1).Add("c", 2);
|
||||
fxt.Test__eq
|
||||
( fxt.Make().Add("1", "b").Add("2", "c")
|
||||
, XophpArray_.array_flip(orig)
|
||||
);
|
||||
}
|
||||
@Test public void implode() {
|
||||
@Test public void implode() {
|
||||
XophpArray orig = fxt.Make().Add_many("a", "b", "c");
|
||||
Gftest.Eq__str
|
||||
( "a b c"
|
||||
, XophpArray_.implode(" ", orig)
|
||||
);
|
||||
}
|
||||
@Test public void in_array() {
|
||||
@Test public void in_array() {
|
||||
// PHP samples
|
||||
XophpArray array;
|
||||
// Example #1
|
||||
@ -276,6 +281,58 @@ public class XophpArray__tst { // REF:https://www.php.net/manual/en/function.arr
|
||||
Gftest.Eq__bool(Bool_.N, XophpArray_.in_array(XophpArray.New('f', 'i'), array));
|
||||
Gftest.Eq__bool(Bool_.Y, XophpArray_.in_array('o', array));
|
||||
}
|
||||
@Test public void array_shift() {
|
||||
XophpArray array;
|
||||
String shifted;
|
||||
|
||||
// key is int
|
||||
array = XophpArray.New("a", "b");
|
||||
shifted = (String)XophpArray_.array_shift(array);
|
||||
|
||||
Gftest.Eq__str("a", shifted);
|
||||
fxt.Test__eq
|
||||
( XophpArray.New().Add(0, "b")
|
||||
, array
|
||||
);
|
||||
|
||||
// key is str and int
|
||||
array = XophpArray.New().Add("a", "a").Add(2, "b").Add(5, "c");
|
||||
shifted = (String)XophpArray_.array_shift(array);
|
||||
|
||||
Gftest.Eq__str("a", shifted);
|
||||
fxt.Test__eq
|
||||
( XophpArray.New().Add(0, "b").Add(1, "c")
|
||||
, array
|
||||
);
|
||||
|
||||
// empty
|
||||
array = XophpArray.New();
|
||||
shifted = (String)XophpArray_.array_shift(array);
|
||||
|
||||
Gftest.Eq__bool_y(shifted == null);
|
||||
fxt.Test__eq
|
||||
( XophpArray.New()
|
||||
, array
|
||||
);
|
||||
|
||||
// null
|
||||
array = null;
|
||||
shifted = (String)XophpArray_.array_shift(array);
|
||||
|
||||
Gftest.Eq__bool_y(shifted == null);
|
||||
Gftest.Eq__bool_y(array == null);
|
||||
|
||||
// PHP samples
|
||||
// Example #1
|
||||
array = XophpArray.New("orange", "banana", "apple", "strawberry");
|
||||
shifted = (String)XophpArray_.array_shift(array);
|
||||
|
||||
Gftest.Eq__str("orange", shifted);
|
||||
fxt.Test__eq
|
||||
( XophpArray.New().Add(0, "banana").Add(1, "apple").Add(2, "strawberry")
|
||||
, array
|
||||
);
|
||||
}
|
||||
}
|
||||
class XophpArray__fxt {
|
||||
public XophpArray Make() {return new XophpArray();}
|
Loading…
Reference in New Issue
Block a user