inc/utilities.php
Some functions used by code everywhere.
Note that this file must be UTF8 as it has characters, but not have a BOM because this would create a problem with the cookies.
Functions
swSimpleSanitize($s)
Removes greater and lower tag characters on input that shoud not have one (url get parameters query and names, also to be used on public input fields) to prevent XSS.
swLengthSort($a,$b)
Custom compare function to sort 2 strings by length (longer first), then alphabetically. Used by search.php
swUnescape($s)
Handles some special character tags to be able to use them within templates and fields: colon, pipe, rightsquare, leftsquare, rightcurly, leftcurly.
swGetArrayValue($array,$key,$default= ' ' )
Returns the value of an array, if the key exists and default if it does not.
swQuerySplit($s)
Not yet really developped, a stub to better explode queries into words by preserving spaces in fields.
swStrReplace($pattern, $replace, $s)
Functions replaces all lowercase, titlecase and uppercase versions of the pattern with the replace text. Super case insensitiv.e
swGetValue($s, $key, $getArray = false)
Esssential function to retrieve field values.
Fields start with double brackets, have a key, double colon, and then values also separated by double colons and end with double brackets.
[[key::value1::value2]]
You cannot use double brackets for key and value.
You cannot use colons for keys. You should not uses spaces for keys because the query function will not support it. You should not start a field with an underscore, because it is reserved for sofawiki code.
You should not use colons for values, but currently it works.
It makes no difference if the values are in the same brackets or separately. The following is equivalent to the upper.
[[key::value1]] [[key::value2]]
You could even have other fields between them. However, the order of the values may matter.
swGetAllFields($s,$allowinternalvariables=false)
Returns an array of all fields by key and inside the key by number.
$allowinternalvariables returns also the fields with undescore.
swReplaceFields($s,$fields,$options)
Replaces all instances of the provided fields with the new values. Fields are added at the end of the page. Empty lines that hat only field values before are removed.
swGetAllLinks($s)
Returns an array of all internal links. The label after the pipe is ignored.
swValidate($v,$invalidcharacters)
Checks if the string $v has none of the invalid characters in $invalidcharacters.
swNameURL($name)
Creates the URL representation of a name. Pages are only unique on the level of the URL representation.
The goal is to create a readable URL.
Search and relaxed filter use also the URL representation to compare text.
The URL representation is mainly
- lowercase
- german umlauts represented by ae, oe and ue
- accented characters represented by their base character
- spaces and anything that is not a-z 0-9 / . _ replaced by a hyphen.
- all duplicate hyphens are removed
- trailing hyphens are removed, even those before the language subpage extension.
swFileGetContents($url)
Alternative file_get_contents() function.
Note: This functions looks like a duplicate of the wgets() function in inc/special/update.php.
swRowToString($arr)
Converts row of fields to string in wikitext.
Uses rows separated by double hard paranthesises and having only one value by field at a time.
swWriteRow($handle, $arr)
Writes row of fields to file.
swReadField($handle)
Parses a field at a time with a state machine.