/** mediawiki-extensions-Arrays-REL1_37 ExtArrays.php
* print the value of an array (identified by arrayid) by the index, invalid index results in the default value being printed. note the index is 0-based.
* usage:
* {{#arrayindex:arrayid|index}}
**/
public static function pfObj_arrayindex( Parser &$parser, PPFrame $frame, $args ) {
global $egArraysCompatibilityMode;
// Get Parameters
$arrayId = isset( $args[0] ) ? trim( $frame->expand( $args[0] ) ) : '';
$rawOptions = isset( $args[2] ) ? $args[2] : '';
if ( !isset( $args[1] ) ) {
return '';
}
$index = trim( $frame->expand( $args[1] ) );
// get value or null if it doesn't exist. Takes care of negative index as well
$val = self::get( $parser )->getArrayValue( $arrayId, $index );
if ( $val === null || ( $val === '' && !$egArraysCompatibilityMode ) ) {
// index doesn't exist, return default (parameter 3)!
// without compatibility, also return default in case of empty string ''
// only expand default when needed
$defaultOrOptions = trim( $frame->expand( $rawOptions ) );
if ( $egArraysCompatibilityMode ) {
// COMPATIBILITY-MODE
// now parse the options, and do posterior process on the created array
$options = self::parse_options( $defaultOrOptions );
$default = self::array_value( $options, 'default' );
} else {
$default = $defaultOrOptions;
}
return $default;
}
return $val;
}
/**
* Returns a value within an array. If key or array do not exist, this will return null
* or another predefined default. $index can also be a negative value, in this case the
* value that far from the end of the array will be returned.
*
* @since 2.0
*
* @param string $arrayId
* @param string $index
* @param mixed|null $default value to return in case the value doesn't exist. null by default.
*
* @return string|null
*/
private function getArrayValue( $arrayId, $index, $default = null ) {
$arrayId = trim( $arrayId );
if ( $this->arrayExists( $arrayId )
&& $this->validate_array_index( $arrayId, $index, true )
&& array_key_exists( $index, $this->mArrays[ $arrayId ] )
) {
return $this->mArrays[ $arrayId ][ $index ];
} else {
return $default;
}
}