DynamicPageList3/includes/Variables.php

186 lines
3.3 KiB
PHP
Raw Normal View History

2020-11-22 20:00:48 +00:00
<?php
2021-10-01 22:52:30 +00:00
namespace MediaWiki\Extension\DynamicPageList3;
2020-11-22 20:00:48 +00:00
class Variables {
/**
* Memory storage for variables.
*
2021-02-22 23:48:01 +00:00
* @var array
2020-11-22 20:00:48 +00:00
*/
2021-02-22 23:48:01 +00:00
public static $memoryVar = [];
2020-11-22 20:00:48 +00:00
/**
* Memory storage for arrays of variables.
*
2021-02-22 23:48:01 +00:00
* @var array
2020-11-22 20:00:48 +00:00
*/
2021-02-22 23:48:01 +00:00
public static $memoryArray = [];
2020-11-22 20:00:48 +00:00
/**
* Expects pairs of 'variable name' and 'value'
* if the first parameter is empty it will be ignored {{#vardefine:|a|b}} is the same as {{#vardefine:a|b}}
*
* @param array $arg
* @return string
*/
2021-02-22 23:48:01 +00:00
public static function setVar( $arg ) {
$numargs = count( $arg );
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
if ( $numargs >= 3 && $arg[2] == '' ) {
2020-11-22 20:00:48 +00:00
$start = 3;
} else {
$start = 2;
}
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
for ( $i = $start; $i < $numargs; $i++ ) {
2020-11-22 20:00:48 +00:00
$var = $arg[$i];
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
if ( ++$i <= $numargs - 1 ) {
2020-11-22 20:00:48 +00:00
self::$memoryVar[$var] = $arg[$i];
} else {
self::$memoryVar[$var] = '';
}
}
2021-10-01 22:52:30 +00:00
2020-11-22 20:00:48 +00:00
return '';
}
/**
* @param array $arg
* @return string
*/
2021-02-22 23:48:01 +00:00
public static function setVarDefault( $arg ) {
$numargs = count( $arg );
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
if ( $numargs > 3 ) {
2020-11-22 20:00:48 +00:00
$value = $arg[3];
} else {
return '';
}
2021-10-01 22:52:30 +00:00
2020-11-22 20:00:48 +00:00
$var = $arg[2];
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
if ( !array_key_exists( $var, self::$memoryVar ) || self::$memoryVar[$var] == '' ) {
2020-11-22 20:00:48 +00:00
self::$memoryVar[$var] = $value;
}
2021-10-01 22:52:30 +00:00
2020-11-22 20:00:48 +00:00
return '';
}
/**
* @param string $var
* @return string
*/
2021-02-22 23:48:01 +00:00
public static function getVar( $var ) {
if ( array_key_exists( $var, self::$memoryVar ) ) {
2020-11-22 20:00:48 +00:00
return self::$memoryVar[$var];
}
2021-10-01 22:52:30 +00:00
2020-11-22 20:00:48 +00:00
return '';
}
/**
* @param array $arg
* @return string
*/
2021-02-22 23:48:01 +00:00
public static function setArray( $arg ) {
$numargs = count( $arg );
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
if ( $numargs < 5 ) {
2020-11-22 20:00:48 +00:00
return '';
}
2021-10-01 22:52:30 +00:00
$var = trim( $arg[2] );
$value = $arg[3];
2020-11-22 20:00:48 +00:00
$delimiter = $arg[4];
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
if ( $var == '' ) {
2020-11-22 20:00:48 +00:00
return '';
}
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
if ( $value == '' ) {
2020-11-22 20:00:48 +00:00
self::$memoryArray[$var] = [];
return '';
2020-11-22 20:00:48 +00:00
}
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
if ( $delimiter == '' ) {
2020-11-22 20:00:48 +00:00
self::$memoryArray[$var] = [
$value
];
2021-10-01 22:52:30 +00:00
return '';
2020-11-22 20:00:48 +00:00
}
2021-10-01 22:52:30 +00:00
if ( strpos( $delimiter, '/' ) !== 0 || ( strlen( $delimiter ) - 1 ) !== strrpos( $delimiter, '/' ) ) {
2020-11-22 20:00:48 +00:00
$delimiter = '/\s*' . $delimiter . '\s*/';
}
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
self::$memoryArray[$var] = preg_split( $delimiter, $value );
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
return "value={$value}, delimiter={$delimiter}," . count( self::$memoryArray[$var] );
2020-11-22 20:00:48 +00:00
}
/**
* @param array $arg
* @return string
*/
2021-02-22 23:48:01 +00:00
public static function dumpArray( $arg ) {
$numargs = count( $arg );
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
if ( $numargs < 3 ) {
2020-11-22 20:00:48 +00:00
return '';
}
2021-10-01 22:52:30 +00:00
$var = trim( $arg[2] );
2020-11-22 20:00:48 +00:00
$text = " array {$var} = {";
2021-10-01 22:52:30 +00:00
$n = 0;
2021-02-22 23:48:01 +00:00
if ( array_key_exists( $var, self::$memoryArray ) ) {
foreach ( self::$memoryArray[$var] as $value ) {
if ( $n++ > 0 ) {
2020-11-22 20:00:48 +00:00
$text .= ', ';
}
2021-10-01 22:52:30 +00:00
2020-11-22 20:00:48 +00:00
$text .= "{$value}";
}
}
2021-10-01 22:52:30 +00:00
2020-11-22 20:00:48 +00:00
return $text . "}\n";
}
/**
* @param string $var
* @param string $delimiter
* @param string $search
* @param string $subject
* @return array|string
*/
2021-02-22 23:48:01 +00:00
public static function printArray( $var, $delimiter, $search, $subject ) {
$var = trim( $var );
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
if ( $var == '' ) {
2020-11-22 20:00:48 +00:00
return '';
}
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
if ( !array_key_exists( $var, self::$memoryArray ) ) {
2020-11-22 20:00:48 +00:00
return '';
}
2021-10-01 22:52:30 +00:00
$values = self::$memoryArray[$var];
2020-11-22 20:00:48 +00:00
$rendered_values = [];
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
foreach ( $values as $v ) {
$temp_result_value = str_replace( $search, $v, $subject );
2020-11-22 20:00:48 +00:00
$rendered_values[] = $temp_result_value;
}
2021-10-01 22:52:30 +00:00
2020-11-22 20:00:48 +00:00
return [
2021-10-01 22:52:30 +00:00
// @phan-suppress-next-line PhanPluginMixedKeyNoKey
2021-02-22 23:48:01 +00:00
implode( $delimiter, $rendered_values ),
2021-10-01 22:52:30 +00:00
'noparse' => false,
'isHTML' => false
2020-11-22 20:00:48 +00:00
];
}
}