From c389765b248da8a27086fc985014fc75726a63e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adrien=20LES=C3=89N=C3=89CHAL?= Date: Tue, 10 Dec 2024 17:41:55 +0100 Subject: [PATCH 1/3] `#ueswitch`: Split arguments before evaluation --- includes/SimpleFunctions.php | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/includes/SimpleFunctions.php b/includes/SimpleFunctions.php index b89c893..d66c22b 100644 --- a/includes/SimpleFunctions.php +++ b/includes/SimpleFunctions.php @@ -360,17 +360,27 @@ public static function ueswitchRender($parser, $frame, $params) { $switchKey = isset($params[0]) ? ParserPower::unescape(trim($frame->expand(array_shift($params)))) : ''; if (count($params) > 0) { - $lastItem = $frame->expand($params[count($params) - 1]); + $lastBits = $params[count($params) - 1]->splitArg(); + $frame->expand($lastBits['name']); + $lastValue = $frame->expand($lastBits['value']); + $default = ''; $mwDefaultFound = false; $mwDefault = $parser->getMagicWordFactory()->get('default'); $keyFound = false; foreach ($params as $param) { - $pair = explode('=', $frame->expand($param), 2); + $bits = $param->splitArg(); + if ($bits['index'] === '') { + $key = $frame->expand($bits['name']); + $value = $frame->expand($bits['value']); + } else { + $key = $frame->expand($bits['value']); + $value = null; + } if (!$keyFound) { - $key = ParserPower::unescape(trim($pair[0])); + $key = ParserPower::unescape(trim($key)); if ($key === $switchKey) { $keyFound = true; } elseif ($mwDefault->matchStartToEnd($key)) { @@ -378,18 +388,18 @@ public static function ueswitchRender($parser, $frame, $params) { } } - if (count($pair) > 1) { + if ($value) { if ($keyFound) { - return [ParserPower::unescape(trim($pair[1])), 'noparse' => false]; + return [ParserPower::unescape(trim($value)), 'noparse' => false]; } elseif ($mwDefaultFound) { - $default = $pair[1]; + $default = $value; $mwDefaultFound = false; } } } - if (strpos($lastItem, '=') === false) { - $default = $lastItem; + if ($lastBits['index'] !== '') { + $default = $lastValue; } return [ParserPower::unescape(trim($default)), 'noparse' => false]; } else { From 0da33019f1e1b1ae004b47ee7be44bead672ddc1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adrien=20LES=C3=89N=C3=89CHAL?= Date: Tue, 10 Dec 2024 18:04:45 +0100 Subject: [PATCH 2/3] `arrangeParams`: Split arguments before evaluation The 1st argument is always evaluated, so we still have to check whether arguments are strings or PPNodes. --- includes/ParserPower.php | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/includes/ParserPower.php b/includes/ParserPower.php index b089f44..8857bca 100644 --- a/includes/ParserPower.php +++ b/includes/ParserPower.php @@ -27,11 +27,20 @@ class ParserPower { public static function arrangeParams($frame, $unexpandedParams) { $params = []; foreach ($unexpandedParams as $unexpandedParam) { - $param = explode('=', $frame->expand($unexpandedParam), 2); - if (count($param) == 2) { - $params[trim($param[0])] = trim($param[1]); + if (is_string($unexpandedParam)) { + $param = explode('=', $unexpandedParam, 2); + if (count($param) === 2) { + $params[trim($param[0])] = $param[1]; + } else { + $params[] = $param[0]; + } } else { - $params[] = trim($param[0]); + $param = $unexpandedParam->splitArg(); + if ($param['index'] === '') { + $params[trim($frame->expand($param['name']))] = trim($frame->expand($param['value'])); + } else { + $params[] = trim($frame->expand($param['value'])); + } } } From 36c1cb92749f7c640b4961500a98e7e23243b4f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adrien=20LES=C3=89N=C3=89CHAL?= Date: Wed, 11 Dec 2024 10:30:22 +0100 Subject: [PATCH 3/3] Simplify `arrangeParams` Only the 1st argument may be already expanded, so no need to check for the type of every argument --- includes/ParserPower.php | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/includes/ParserPower.php b/includes/ParserPower.php index 8857bca..c4f5f98 100644 --- a/includes/ParserPower.php +++ b/includes/ParserPower.php @@ -26,21 +26,22 @@ class ParserPower { */ public static function arrangeParams($frame, $unexpandedParams) { $params = []; + + if (isset($unexpandedParams[0]) && is_string($unexpandedParams[0])) { + $param = explode('=', array_shift($unexpandedParams), 2); + if (count($param) === 2) { + $params[trim($param[0])] = trim($param[1]); + } else { + $params[] = trim($param[0]); + } + } + foreach ($unexpandedParams as $unexpandedParam) { - if (is_string($unexpandedParam)) { - $param = explode('=', $unexpandedParam, 2); - if (count($param) === 2) { - $params[trim($param[0])] = $param[1]; - } else { - $params[] = $param[0]; - } + $param = $unexpandedParam->splitArg(); + if ($param['index'] === '') { + $params[trim($frame->expand($param['name']))] = trim($frame->expand($param['value'])); } else { - $param = $unexpandedParam->splitArg(); - if ($param['index'] === '') { - $params[trim($frame->expand($param['name']))] = trim($frame->expand($param['value'])); - } else { - $params[] = trim($frame->expand($param['value'])); - } + $params[] = trim($frame->expand($param['value'])); } }