Extension:StringUtils

From MediaWiki.org
Jump to navigation Jump to search
MediaWiki extensions manual
Crystal Clear action run.svg
StringUtils
Release status: unmaintained
Implementation Parser function
Description Allows to manipulate strings
Author(s) Andreas Weyer
Latest version 1.0 (2009-04-13)
Database changes No
License Public domain
Download see the code section
Hooks used
LanguageGetMagic
Translate the StringUtils extension if it is available at translatewiki.net
Check usage and version matrix.

The StringUtils extension allows to manipulate strings. It may be used to translate characters (to lower/upper case) or to replace sequences in a string.

Usage[edit]

Character translation[edit]

To lower case
{{#lowercase:''text''}}
converts all chars in text to lower case.
This does not differ from parser function {{lc: ...}} provided by MediaWiki core. Use this one instead.
To upper case
{{#uppercase:''text''}}
converts all chars in text to upper case.
This does not differ from parser function {{uc: ...}} provided by MediaWiki core. Use this one instead.

String replacement[edit]

Replace case sensitive
{{#replace:''search'', ''replace'', ''text''}}
replace search with replace in text where char case in search must match those in text.
A replace function is available in the standard ParserFunctions extension since r50997, which integrated most of StringFunctions.
Replace case insensitive
{{#ireplace:''search'', ''replace'', ''text''}}
replace search with replace in text where char case in search will be ignored.
Case-insensitive replace is not available in the ParserFunctions extension.

Examples[edit]

{{#lowercase:This is a TEST.}}
{{#uppercase:This is a TEST.}}

produces:

this is a test.

THIS IS A TEST.

{{#replace:Bar|ZOO|Foo Bar bar.}}
{{#ireplace:Bar|ZOO|Foo Bar bar.}}

produces:

Foo ZOO bar.

Foo ZOO ZOO.

Installation[edit]

  • Add the following code at the bottom of your LocalSettings.php:
    require_once "$IP/extensions/StringUtils/StringUtils.php";
    
  • Yes Done – Navigate to Special:Version on your wiki to verify that the extension is successfully installed.

Code[edit]

StringUtils.php
<?php
 
if ( !defined( 'MEDIAWIKI' ) ) {
    die( 'This file is a MediaWiki extension, it is not a valid entry point' );
}
 
$wgExtensionFunctions[] = 'wfSetupStringUtils';
 
$wgExtensionCredits['parserhook'][] = array(
    'path' => __FILE__,
    'name' => 'StringUtils',
    'version' => '1.0',
    'url' => 'https://www.mediawiki.org/wiki/Extension:StringUtils',
    'author' => 'Andreas Weyer',
    'description' => 'Allows to manipulate strings',
    'license-name' => 'PD'
);  
        
$wgHooks['LanguageGetMagic'][] = 'wfStringUtilsLanguageGetMagic';

class ExtStringUtils { 
    function toUpperCase( &$parser, $string = '' ) {
        return strtoupper($string);
    }
    function toLowerCase( &$parser, $string = '' ) {
        return strtolower($string);
    }
    function stringReplace( &$parser, $search = '', $replace = '', $subject = '' ) {
        return str_replace($search, $replace, $subject);
    }
    function stringIReplace( &$parser, $search = '', $replace = '', $subject = '' ) {
        return str_ireplace($search, $replace, $subject);
    }
}
 
function wfSetupStringUtils() {
    global $wgParser, $wgMessageCache, $wgExtStringUtils, $wgMessageCache, $wgHooks;
 
    $wgExtStringUtils = new ExtStringUtils;
 
    $wgParser->setFunctionHook( 'uppercase', array( &$wgExtStringUtils, 'toUpperCase' ) );
    $wgParser->setFunctionHook( 'lowercase', array( &$wgExtStringUtils, 'toLowerCase' ) );
    $wgParser->setFunctionHook( 'replace',   array( &$wgExtStringUtils, 'stringReplace' ) );
    $wgParser->setFunctionHook( 'ireplace',  array( &$wgExtStringUtils, 'stringIReplace' ) );
}
 
function wfStringUtilsLanguageGetMagic( &$magicWords, $langCode = 0 ) {
    require_once( dirname( __FILE__ ) . '/StringUtils.i18n.php' );
    foreach( efStringUtilsWords( $langCode ) as $word => $trans )
        $magicWords[$word] = $trans;
    return true;
}
StringUtils.i18n.php
<?php
 
/**
 * Get translated magic words, if available
 *
 * @param string $lang Language code
 * @return array
 */
function efStringUtilsWords( $lang ) {
    $words = array();
    
    /**
     * English
     */
    $words['en'] = array(
        'lowercase' => array( 0, 'lowercase' ),
        'uppercase' => array( 0, 'uppercase' ),
        'replace'   => array( 0, 'replace' ),
        'ireplace'  => array( 0, 'ireplace' )
    );
 
    # English is used as a fallback, and the English synonyms are
    # used if a translation has not been provided for a given word
    return ( $lang == 'en' || !isset( $words[$lang] ) )
        ? $words['en']
        : array_merge( $words['en'], $words[$lang] );
}

See also[edit]