OpenWetWare:Software/Extensions/StringFunctions

From OpenWetWare
Revision as of 15:34, 17 November 2007 by Bill Flanagan (talk | contribs) (New page: =OpenWetWare:Software/Extensions/StringFunctions= This extension defines an additional set of parser functions that operate on strings. '''Note: These functio...)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search

OpenWetWare:Software/Extensions/StringFunctions

This extension defines an additional set of parser functions that operate on strings.

Note: These functions are currently not installed at the Wikimedia projects.

Functions

This module defines these functions: len, pos, rpos, sub, pad, replace, explode, urlencode, and urldecode.

Note: Some parameters of these functions can be limited through global settings to ensure the functions operate in O(n) time complexity, and are therefore safe against DoS attacks. See section Limits below.

#len:

The #len function returns the length of the given string. The syntax is:

{{#len:string}}

The return value is always a number of characters in the string. If no string is specified, the return value is zero.

Notes:

  • Trailing spaces are not counted. Example: {{#len:Icecream }} returns 8.
  • This function is safe with utf-8 multibyte characters. Example: {{#len:Žmržlina}} returns 8.

#pos:

The #pos function returns the position of a given needle within the string. The syntax is:

{{#pos:string|needle|offset}}

The offset parameter, if specified, tells a starting position where this function should begin searching.

If the needle is found, the return value is a zero-based integer of the first position within the string. If the needle is not found, the function returns an empty string.

Notes:

  • This function is case sensitive.
  • The maximum allowed length of the needle is limited through $wgStringFunctionsLimitSearch global setting.
  • This function is safe with utf-8 multibyte characters. Example: {{#pos:Žmržlina|lina}} returns 4.

#rpos:

The #rpos function returns the last position of a given needle within the string. The syntax is:

 {{#rpos:string|needle}}

If the needle is found, the return value is a zero-based integer of its last position within the string. If the needle is not found, the function returns -1.

Tip: When using this to search for the last delimiter, add +1 to the result to retreive position after the last delimiter. This also works when the delimiter is not found, because "-1 + 1" is zero, which is the beginning of the given value.

Notes:

  • This function is case sensitive.
  • The maximum allowed length of the needle is limited through $wgStringFunctionsLimitSearch global setting.
  • This function is safe with utf-8 multibyte characters. Example: {{#rpos:Žmržlina|lina}} returns 4.
  • When this extension is running on PHP 4, the needle can only be a single character. If a string is used as the needle, only the first character of that string will be used. The needle may be a string of more than one character as of PHP 5.0.0.

#sub:

The #sub function returns a substring from the given string. The syntax is:

{{#sub:string|start|length}}

The start parameter, if positive (or zero), specifies a zero-based index of the first character to be returned. Example: {{#sub:Icecream|3}} returns cream.

If the start parameter is negative, it specifies how many characters from the end should be returned. Example: {{#sub:Icecream|-3}} returns eam.

The length parameter, if present and positive, specifies the maximum length of the returned string. Example: {{#sub:Icecream|3|3}} returns cre.

If the length parameter is negative, it specifies, how many characters will be omitted from the end of the string. Example: {{#sub:Icecream|3|-3}} returns cr.

Notes:

  • If the length parameter is zero, it is not used for truncation at all.
    • Example: {{#sub:Icecream|3|0}} returns cream.
  • If start denotes a position beyond the truncation from the end by negative length parameter, an empty string will be returned.
    • Example: {{#sub:Icecream|3|-6}} returns an empty string.
    • The return value is a substring from the string, or an empty string.
  • This function is safe with utf-8 multibyte characters. Example: {{#sub:Žmržlina|3}} returns žlina.

#pad:

The #pad function returns the given string extended to a given width. The syntax is:

{{#pad:string|length|padstring|direction}}

The length parameter specifies the desired length of the returned string.

The padstring parameter, if specified, specifies a pattern to be used to fill the missing space. It may be a single character, which will be used as many times as necessary; or a string which will be concatenated as many times as necessary and then trimed to the required length. Example: {{#pad:Ice|10|xX}} returns XxXxXxXIce.

If the padstring is not specified, spaces are used for padding.

The direction parameter, if specified, can be one of these values:

  • left - the padding will be on the left side of the string. Example: {{#pad:Ice|5|x|left}} returns xxIce.
  • right - the padding will be on the right side of the string. Example: {{#pad:Ice|5|x|right}} returns Icexx.
  • center - the string will be centered in the returned string. Example: {{#pad:Ice|5|x|center}} returns xIcex.

If the direction is not specified, the padding will be on the left side of the string.

The return value is the given string extended to the length of characters, using the padstring to fill the missing part(s). If the given string is already longer than length, it is not extended, nor truncated.

Notes:

  • The maximum allowed value for the length is limited through $wgStringFunctionsLimitPad global setting.
  • This function is NOT safe with utf-8 multibyte characters (i.e. non-english characters). Example: {{#pad:Žmržlina|12|x}} returns xxŽmržlina (instead of xxxxŽmržlina).

#replace:

The #replace function returns the given string with all occurrences of a needle replaced with product.

{{#replace:string|needle|product}}

If the needle is not specified, or is empty a single space will be searched for.

If the product is not specified, or is empty, all occurrences of the needle will be removed from the string.

Notes:

  • This function is case sensitive.
  • Even if the product is a space, an empty string is used. This is a side-effect of the MediaWiki parser feature. To use a space as the product, put it in a nowiki tags.
    • Example: {{#replace:My_little_home_page|_|<nowiki> </nowiki>}} returns My little home page.
  • The maximum allowed length of the needle is limited through $wgStringFunctionsLimitSearch global setting.
  • The maximum allowed length of the product is limited through $wgStringFunctionsLimitReplace global setting.
  • This function is safe with utf-8 multibyte characters. Example: {{#replace:Žmržlina|ž|z}} returns Žmrzlina.

#explode:

The #explode functions splits the given string into pieces and then returns one of the pieces. The syntax is:

{{#explode:string|delimiter|position}}

The delimiter parameter specifies a string to be used to divide the string into pieces. This delimiter string is then not part of any piece, and when two delimiter strings are next to each other, they create an empty piece between them. If this parameter is not specified, a single space is used.

The position parameter specifies, which piece is to be returned. Pieces are counted from 0. If this parameter is not specified, the first piece is used (piece with number 0). When a negative value is used as position, the pieces are counted from the end. In this case, piece with number -1 means the last piece. Examples:

  • {{#explode:And if you tolerate this| |2}} returns you.
  • {{#explode:String/Functions/Code|/|-1}} returns Code.

The return value is the position-th piece. If there are less pieces than the position specifies, an empty string is returned.

Notes:

  • This function is case sensitive.
  • The maximum allowed length of the delimiter is limited through $wgStringFunctionsLimitSearch global setting.
  • This function is safe with utf-8 multibyte characters. Example: {{#explode:Žmržlina|ž|1}} returns lina.

#urlencode: and #urldecode:

These two functions operate in tandem: #urlencode converts a string into an URL-safe syntax, and #urldecode converts such a string back. The syntax is:

{{#urlencode:value}}
{{#urldecode:value}}

Note: These functions work by directly exposing PHP's urlencode() and urldecode() functions.

Usage tips

Pipes

To use a pipe ( "|" ) in the parameters in these functions, try the &#124; sequence, which is translated to a regular pipe before these functions are executed, but only after the control pipes are established by the parser. This way the pipes will be treated as pieces of string, not like wiki syntax.

Spaces

To use a space in the parameters in these functions (at the beginning or end of the parameters), you might need to put the space in the <nowiki></nowiki> tags. This will prevent the parser from trimming the space off before calling the functions.

Limits

This module defines these global settings: $wgStringFunctionsLimitSearch, $wgStringFunctionsLimitReplace, $wgStringFunctionsLimitPad.

They are used to limit some parameters of some functions to ensure the functions operate in O(n) time complexity, and are therefore safe against DoS attacks.

$wgStringFunctionsLimitSearch

This setting is used by #pos:, #rpos:, #replace:, #explode: functions. All these functions search for a substring in a larger string while they operate, which can run in O(n*m), and therefore they make the software more voulnerable to DoS attacks. By setting this value to a specific small number, the time complexity is decreased to O(n).

This setting limits the maximum allowed length of the string being searched for.

The default value is 30 multibyte characters.

$wgStringFunctionsLimitReplace

This setting is used by #replace: function. This function replaces all occurences of one string for another, which can be used to quickly generate very large amounts of data, and therefore it can make the software more voulnerable to DoS attacks. This setting limits the maximum allowed length of the replacing string.

The default value is 30 multibyte characters.

$wgStringFunctionsLimitPad

This setting is used by #pad: function. This function creates a string by the specified length, which can be used to quickly generate very large amounts of data, and therefore it can make the software more voulnerable to DoS attacks. This setting limits the maximum allowed length of the resulting padded string.

The default value is 100 multibyte characters.

Installation

Requirements

This extension requires MediaWiki 1.6+ and the PHP mbstring extension.

If you do not have mbstring installed, you can do without by deleting all occurrences of "mb_" (without quotes) in the code, which will replace all mbstring functions (mb_strlen, mb_substr, mb_strpos, mb_strrpos) by their non-mbstring equivalents. By doing this, the extension will no longer work correctly with utf-8 multibyte characters (e.g. non-English letters), but it will work.

Instructions

Do the following to install these functions as an extension to MediaWiki.

  1. Copy the source code below to extensions/StringFunctions/StringFunctions.php.
  2. Add the following to LocalSettings.php (near the bottom) in the root of your MediaWiki installation:

<source lang=php>require_once ("$IP/extensions/StringFunctions/StringFunctions.php");</source>


Compatibility with MediaWiki 1.6

All StringFunctions will work on MediaWiki 1.6, but their syntax is without the # character. If you want to use the # character, find this section of the /extensions/StringFunctions/StringFunctions.php: <source lang=php> $wgParser->setFunctionHook ( 'len', array ( &$wgExtStringFunctions, 'runLen' ) ); $wgParser->setFunctionHook ( 'pos', array ( &$wgExtStringFunctions, 'runPos' ) ); $wgParser->setFunctionHook ( 'rpos', array ( &$wgExtStringFunctions, 'runRPos' ) ); $wgParser->setFunctionHook ( 'sub', array ( &$wgExtStringFunctions, 'runSub' ) ); $wgParser->setFunctionHook ( 'pad', array ( &$wgExtStringFunctions, 'runPad' ) ); $wgParser->setFunctionHook ( 'replace', array ( &$wgExtStringFunctions, 'runReplace' ) ); $wgParser->setFunctionHook ( 'explode', array ( &$wgExtStringFunctions, 'runExplode' ) ); $wgParser->setFunctionHook ( 'urlencode', array ( &$wgExtStringFunctions, 'runUrlEncode' ) ); $wgParser->setFunctionHook ( 'urldecode', array ( &$wgExtStringFunctions, 'runUrlDecode' ) ); </source>

Replace the above with this: <source lang=php> $wgParser->setFunctionHook ( '#len', array ( &$wgExtStringFunctions, 'runLen' ) ); $wgParser->setFunctionHook ( '#pos', array ( &$wgExtStringFunctions, 'runPos' ) ); $wgParser->setFunctionHook ( '#rpos', array ( &$wgExtStringFunctions, 'runRPos' ) ); $wgParser->setFunctionHook ( '#sub', array ( &$wgExtStringFunctions, 'runSub' ) ); $wgParser->setFunctionHook ( '#pad', array ( &$wgExtStringFunctions, 'runPad' ) ); $wgParser->setFunctionHook ( '#replace', array ( &$wgExtStringFunctions, 'runReplace' ) ); $wgParser->setFunctionHook ( '#explode', array ( &$wgExtStringFunctions, 'runExplode' ) ); $wgParser->setFunctionHook ( '#urlencode', array ( &$wgExtStringFunctions, 'runUrlEncode' ) ); $wgParser->setFunctionHook ( '#urldecode', array ( &$wgExtStringFunctions, 'runUrlDecode' ) ); </source>

Code

This code has been tested on MediaWiki 1.6.8 and above.

History:

  • Aug 28, 2007 -- v1.10 -- Added negative positions to #explode.
  • Jan 30, 2007 -- v1.9 -- Added limits to #pos, #pad, #replace, #explode.
  • Oct 30, 2006 -- v1.8 -- Fixed for MediaWiki 1.8.
  • Oct 26, 2006 -- v1.6 -- Fixed spaces in #rpos and #replace.
  • Oct 1, 2006 -- v1.5 -- Added #rpos, #pad, #replace, #explode.
  • May 18, 2006 -- v1.2 -- Renamed #toURL and #fromURL to match MediaWiki's {{urlencode:}} function.
  • May 18, 2006 -- v1.1 -- Added #pos.
  • May 15, 2006 -- v1.0 -- First stable release.

Extension:StringFunctions/Code

Applications

See also