/** mediawiki-extensions-Arrays-REL1_37 ExtArrays.php
* print an array.
* foreach element of the array, print 'subject' where all occurrences of 'search' is replaced with the element,
* and each element print-out is deliminated by 'delimiter'
* The subject can embed parser functions; wiki links; and templates.
* usage:
* {{#arrayprint:arrayid|delimiter|search|subject|options}}
* examples:
* {{#arrayprint:b}} -- simple
* {{#arrayprint:b|<br/>}} -- add change line
* {{#arrayprint:b|<br/>|@@@|[[@@@]]}} -- embed wiki links
* {{#arrayprint:b|<br/>|@@@|{{#set:prop=@@@}} }} -- embed parser function
* {{#arrayprint:b|<br/>|@@@|{{f.tag{{f.print.vbar}}prop{{f.print.vbar}}@@@}} }} -- embed template function
* {{#arrayprint:b|<br/>|@@@|[[name::@@@]]}} -- make SMW links
*/
public static function pfObj_arrayprint( Parser &$parser, PPFrame $frame, $args ) {
global $egArraysCompatibilityMode, $egArraysExpansionEscapeTemplates;
// Get Parameters
$arrayId = isset( $args[0] ) ? trim( $frame->expand( $args[0] ) ) : '';
$delimiter = isset( $args[1] ) ? trim( $frame->expand( $args[1] ) ) : self::$mDefaultSep;
/*
* PPFrame::NO_ARGS and PPFrame::NO_TEMPLATES for expansion make a lot of sense here since the patterns getting replaced
* in $subject before $subject is being parsed. So any template or argument influence in the patterns wouldn't make any
* sense in any sane scenario.
*/
$search = isset( $args[2] ) ? trim( $frame->expand( $args[2], PPFrame::NO_ARGS | PPFrame::NO_TEMPLATES ) ) : null;
$subject = isset( $args[3] ) ? trim( $frame->expand( $args[3], PPFrame::NO_ARGS | PPFrame::NO_TEMPLATES ) ) : null;
// options array:
$options = isset( $args[4] )
? self::parse_options( $frame->expand( $args[4] ) )
: [];
// get array, null if non-existant:
$array = self::get( $parser )->getArray( $arrayId );
if ( $array === null ) {
// array we want to print doesn't exist!
if ( !$egArraysCompatibilityMode ) {
return '';
} else {
// COMPATIBILITY-MODE
return "undefined array: $arrayId";
}
}
// if there is no subject, there is no point in expanding. Faster!
if ( $subject === null ) {
if ( !$egArraysCompatibilityMode && $egArraysExpansionEscapeTemplates !== null ) {
// we can ignore options here, since if subject is null, options won't be set as well!
return trim( implode( $delimiter, $array ) );
} else {
// COMPATIBILITY-MODE
// set search and subject so the old routine can be done
$search = $subject = '@@@@';
}
}
$rendered_values = [];
foreach ( $array as $val ) {
if ( !$egArraysCompatibilityMode ) {
// NO COMPATIBILITY-MODE
/**
* escape the array value so it won't destroy the users wiki markup expression.
*/
$val = self::escapeForExpansion( $val );
}
// replace place holder with current value:
$rawResult = str_replace( $search, $val, $subject );
/*
* $subjectd still is un-expanded (this allows to use some parser functions like
* {{FULLPAGENAME:@@@@}} directly without getting parsed before @@@@ is replaced.
* Expand it so we replace templates like {{!}} which we need for the final parse.
*/
$rawResult = $parser->preprocessToDom( $rawResult, $frame->isTemplate() ? Parser::PTD_FOR_INCLUSION : 0 );
$rawResult = trim( $frame->expand( $rawResult ) );
$rendered_values[] = $rawResult;
}
// follow special print options:
switch ( self::array_value( $options, 'print' ) ) {
case 'pretty':
// pretty list print with ' and ' connecting the last two items
if ( $delimiter === '' ) {
// '' as delimiter isn't pretty, so in this case we take the (languages) default
$output = self::arrayToText( $rendered_values );
} else {
$output = self::arrayToText( $rendered_values, $delimiter );
}
break;
default:
// normal print with one delimiter, might be the languages default
$output = implode( $delimiter, $rendered_values );
break;
}
if ( $egArraysCompatibilityMode || $egArraysExpansionEscapeTemplates === null ) {
// COMPATIBILITY-MODE:
/*
* don't leave the final parse to Parser::braceSubstitution() since there are some special cases where it
* would produce unexpected output (it uses a new child frame and ignores whether the frame is a template!)
*/
$output = $parser->preprocessToDom( $output, $frame->isTemplate() ? Parser::PTD_FOR_INCLUSION : 0 );
$output = $frame->expand( $output );
}
return trim( $output );
}