Extension:StringLoop

From MediaWiki.org
Jump to navigation Jump to search
MediaWiki extensions manual
OOjs UI icon advanced.svg

Release status: beta
Implementation Parser function
Description This extensions implements for loops in the style of the Bourne shell.
Author(s) Robert Paciorek (berciktalk)
Latest version 1.0 (2010-08-14)
MediaWiki 1.15
License GNU GPL v3.0
Download below
Translate the StringLoop extension if it is available at translatewiki.net
Check usage and version matrix.

What can this extension do?[edit]

This extensions implements for loops in the style of the Bourne shell. For any words in "input list" (separated by "input_list_separator" - default space) this word is substituted for variable "variable_name" and is inserting "wiki code". "wiki code" can use variable value by:

{{ #var: variable_name }}

Usage[edit]

{{#string_loop:variable_name|input list|wiki code|input_list_separator}}

input_list_separator is optional and default is space.

Example[edit]

{{#string_loop:v|a b c|[[{{ #var: v }}]] <nowiki/>}}

generated:

[[a]] [[b]] [[c]]

Download instructions[edit]

Please cut and paste the code found below and place it in $IP/extensions/StringLoop/StringLoop.php. Note: $IP stands for the root directory of your MediaWiki installation, the same directory that holds LocalSettings.php.

Installation[edit]

This extension require VariablesExtension to be installed. To install this extension, add the following to LocalSettings.php:

require_once("$IP/extensions/StringLoop/StringLoop.php");

Code[edit]

<?php

/**
 * This extensions implements for loops in the style of the Bourne shell.
 * For any words in "input list" (separated by "input_list_separator" - default space)
 * this word is substituted for variable "variable_name" and is inserting "wiki code".
 * "wiki code" can use variable value by "{{ #var: variable_name }}"
 *
 * Extensions can by used to parse a template argument which is a list.
 *
 * INSTALATION:
 *	1. install VariablesExtension - http://www.mediawiki.org/wiki/Extension:VariablesExtension
 *	2. put this file (StringLoop.php) to PATH_TO_YOUR_WIKI//extensions/StringLoop/
 *	3. add to LocalSettings.php
 *		require_once("$IP/extensions/StringLoop/StringLoop.php");
 *
 * USAGE:
 *	{{#string_loop:variable_name|input list|wiki code|input_list_separator}}
 *		input_list_separator is optional and default is space
 *
 * EXAMPLE:
 *	{{#string_loop:v|a b c|[[{{ #var: v }}]] <nowiki/>}}
 *	generated: [[a]] [[b]] [[c]]
 *
 *
 * COPYRIGHT (c) 2010, Robert Paciorek (http://www.opcode.eu.org/), GNU GPL v 3.0
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * some code fragmets inspired by Loops extensions by David M. Sledge
 *  (http://www.mediawiki.org/wiki/Extension:Loops)
 *
**/


$wgExtensionFunctions[] = 'StringLoop::register';
$wgExtensionCredits['parserhook'][] = array(
    'name' => 'StringLoop',
    'author' => 'Robert paciorek',
    'url' => 'http://opcode.eu.org/',
);
$wgHooks['LanguageGetMagic'][]       = "StringLoop::setMagicWords";

class StringLoop {
	public static function register() {
		global $wgParser;
		$wgParser->setFunctionHook( 'string_loop', array( 'StringLoop', 'string_loop' ), SFH_OBJECT_ARGS );
		return true;
	}

	public static function setMagicWords( &$magicWords, $langCode ) {
		$magicWords['string_loop'] = array( 0, 'string_loop' );
		return true;
	}

	public function string_loop(&$parser, $frame, $args) {
		global $wgExtVariables;

		$varName=array_shift( $args );
        	$varName = $varName === null ? '' : trim( $frame->expand( $varName ) );
		$input_str=array_shift( $args );
        	$input_str = $input_str === null ? '' : trim( $frame->expand( $input_str ) );
		$loopStatement=array_shift( $args );
		$delimiter=array_shift( $args );
		$delimiter = $delimiter === null ? ' ' : $delimiter;
		$output='';

		$words = explode($delimiter, $input_str);
		foreach ( $words as $word ) {
			$wgExtVariables->vardefine( $parser, $varName, "$word" );
			
			$output .= isset( $loopStatement ) ?
				trim( $frame->expand( $loopStatement ) ) : '';
		}
		
		return $output;
	}
}