Translate extension for MediaWiki
 
Loading...
Searching...
No Matches
MediaWiki\Extension\Translate\Utilities\Utilities Class Reference

Essentially random collection of helper functions, similar to GlobalFunctions.php. More...

Static Public Member Functions

static title (string $message, string $code, int $ns=NS_MEDIAWIKI)
 Does quick normalisation of message name so that in can be looked from the database.
 
static figureMessage (string $text)
 Splits page name into message key and language code.
 
static getMessageContent (string $key, string $language, int $namespace=NS_MEDIAWIKI)
 Loads page content without side effects.
 
static getContents ( $titles, int $namespace)
 Fetches contents for page names in given namespace.
 
static getContentForTitle (Title $title, bool $addFuzzy=false)
 Returns the content for a given title and adds the fuzzy tag if requested.
 
static getLanguageName (string $code, ?string $language='en')
 Returns a localised language name.
 
static getLanguageNames (?string $code)
 Get translated language names for the languages generally supported for translation in the current wiki.
 
static fieldset (string $legend, string $contents, array $attributes=[])
 Constructs a fieldset with contents.
 
static convertWhiteSpaceToHTML (string $message)
 Escapes the message, and does some mangling to whitespace, so that it is preserved when outputted as-is to html page.
 
static cacheFile (string $filename)
 Gets the path for cache files.
 
static getPlaceholder ()
 Returns a random string that can be used as placeholder in strings.
 
static getIcon (MessageGroup $g, int $size)
 Get URLs for icons if available.
 
static getSafeReadDB ()
 Get a DB handle suitable for read and read-for-write cases.
 
static shouldReadFromPrimary ()
 Check whether primary should be used for reads to avoid reading stale data.
 
static getEditorUrl (MessageHandle $handle, string $action_source='', bool $includeLanguageParam=true)
 Get a URL that points to an editor for this message handle.
 
static serialize ( $value)
 Serialize the given value.
 
static deserialize (string $str, array $opts=[ 'allowed_classes'=> false])
 Deserialize the given string.
 
static getVersion ()
 
static allowsSubpages (Title $title)
 Checks if the namespace that the title belongs to allows subpages.
 
static isSupportedLanguageCode (string $code)
 Checks whether a language code is supported for translation at the wiki level.
 
static getTextFromContentOrNull (?Content $content)
 Best-effort extraction of text from content.
 
static getTextFromTextContent (?Content $content)
 Strict extraction of text from content.
 
static getTranslations (MessageHandle $handle)
 Returns all translations of a given message.
 
static getTranslationRevisions (MessageHandle $handle)
 Returns the latest RevisionRecord for each existing translation of a message.
 
static isTranslationPage (MessageHandle $handle)
 
static isValidRegex (string $pattern, ?string &$error=null)
 Validate a regular exception.
 

Detailed Description

Essentially random collection of helper functions, similar to GlobalFunctions.php.

Author
Niklas Laxström @license GPL-2.0-or-later

Definition at line 30 of file Utilities.php.

Member Function Documentation

◆ allowsSubpages()

static MediaWiki\Extension\Translate\Utilities\Utilities::allowsSubpages ( Title $title)
static

Checks if the namespace that the title belongs to allows subpages.

Definition at line 383 of file Utilities.php.

◆ cacheFile()

static MediaWiki\Extension\Translate\Utilities\Utilities::cacheFile ( string $filename)
static

Gets the path for cache files.

The cache directory must be configured to use this method.

Parameters
string$filename
Returns
string Full path.

Definition at line 241 of file Utilities.php.

◆ convertWhiteSpaceToHTML()

static MediaWiki\Extension\Translate\Utilities\Utilities::convertWhiteSpaceToHTML ( string $message)
static

Escapes the message, and does some mangling to whitespace, so that it is preserved when outputted as-is to html page.

Line feeds are converted to <br /> and occurrences of leading and trailing and multiple consecutive spaces to non-breaking spaces.

This is also implemented in JavaScript in ext.translate.quickedit.

Parameters
string$messagePlain text string.
Returns
string Text string that is ready for outputting.

Definition at line 226 of file Utilities.php.

◆ deserialize()

static MediaWiki\Extension\Translate\Utilities\Utilities::deserialize ( string $str,
array $opts = [ 'allowed_classes' => false ] )
static

Deserialize the given string.

Returns
mixed

Definition at line 365 of file Utilities.php.

◆ fieldset()

static MediaWiki\Extension\Translate\Utilities\Utilities::fieldset ( string $legend,
string $contents,
array $attributes = [] )
static

Constructs a fieldset with contents.

Parameters
string$legendRaw html.
string$contentsRaw html.
array$attributesHtml attributes for the fieldset.
Returns
string Html.

Definition at line 207 of file Utilities.php.

◆ figureMessage()

static MediaWiki\Extension\Translate\Utilities\Utilities::figureMessage ( string $text)
static

Splits page name into message key and language code.

Parameters
string$text
Returns
array ( string, string ) Key and language code.
Todo
Handle names without slash.

Definition at line 61 of file Utilities.php.

◆ getContentForTitle()

static MediaWiki\Extension\Translate\Utilities\Utilities::getContentForTitle ( Title $title,
bool $addFuzzy = false )
static

Returns the content for a given title and adds the fuzzy tag if requested.

Parameters
Title$title
bool$addFuzzyAdd the fuzzy tag if appropriate.
Returns
string|null

Definition at line 152 of file Utilities.php.

◆ getContents()

static MediaWiki\Extension\Translate\Utilities\Utilities::getContents ( $titles,
int $namespace )
static

Fetches contents for page names in given namespace.

Parameters
string | string[]$titlesDatabase page names.
int$namespaceThe number of the namespace.
Returns
array ( string => array ( string, string ) ) Tuples of page text and last author indexed by page name.

Definition at line 133 of file Utilities.php.

◆ getEditorUrl()

static MediaWiki\Extension\Translate\Utilities\Utilities::getEditorUrl ( MessageHandle $handle,
string $action_source = '',
bool $includeLanguageParam = true )
static

Get a URL that points to an editor for this message handle.

Parameters
MessageHandle$handle
string$action_sourceIf non-empty, defines where the link originates from, for metrics (event logging)
bool$includeLanguageParamWhether the language parameter should be included
Returns
string Domain relative URL

Definition at line 329 of file Utilities.php.

◆ getIcon()

static MediaWiki\Extension\Translate\Utilities\Utilities::getIcon ( MessageGroup $g,
int $size )
static

Get URLs for icons if available.

Parameters
MessageGroup$g
int$sizeLength of the edge of a bounding box to fit the icon.
Returns
null|array

Definition at line 269 of file Utilities.php.

◆ getLanguageName()

static MediaWiki\Extension\Translate\Utilities\Utilities::getLanguageName ( string $code,
?string $language = 'en' )
static

Returns a localised language name.

Parameters
string$codeLanguage code.
null | string$languageLanguage code of the language that the name should be in.
Returns
string Best-effort localisation of wanted language name.

Definition at line 177 of file Utilities.php.

◆ getLanguageNames()

static MediaWiki\Extension\Translate\Utilities\Utilities::getLanguageNames ( ?string $code)
static

Get translated language names for the languages generally supported for translation in the current wiki.

Message groups can have further exclusions.

Parameters
null | string$code
Returns
array<string,string> ( language code => language name )

Definition at line 189 of file Utilities.php.

◆ getMessageContent()

static MediaWiki\Extension\Translate\Utilities\Utilities::getMessageContent ( string $key,
string $language,
int $namespace = NS_MEDIAWIKI )
static

Loads page content without side effects.

Parameters
string$keyMessage key.
string$languageLanguage code.
int$namespaceNamespace number.
Returns
string|null The contents or null.

Definition at line 76 of file Utilities.php.

◆ getPlaceholder()

static MediaWiki\Extension\Translate\Utilities\Utilities::getPlaceholder ( )
static

Returns a random string that can be used as placeholder in strings.

Definition at line 256 of file Utilities.php.

◆ getSafeReadDB()

static MediaWiki\Extension\Translate\Utilities\Utilities::getSafeReadDB ( )
static

Get a DB handle suitable for read and read-for-write cases.

Returns
IReadableDatabase Primary for HTTP POST, CLI, DB already changed; replica otherwise

Definition at line 300 of file Utilities.php.

◆ getTextFromContentOrNull()

static MediaWiki\Extension\Translate\Utilities\Utilities::getTextFromContentOrNull ( ?Content $content)
static

Best-effort extraction of text from content.

Returns null when the content is missing, hidden or not text-based.

Definition at line 405 of file Utilities.php.

◆ getTextFromTextContent()

static MediaWiki\Extension\Translate\Utilities\Utilities::getTextFromTextContent ( ?Content $content)
static

Strict extraction of text from content.

Throws when the content is missing, hidden or not text-based.

Definition at line 414 of file Utilities.php.

◆ getTranslationRevisions()

static MediaWiki\Extension\Translate\Utilities\Utilities::getTranslationRevisions ( MessageHandle $handle)
static

Returns the latest RevisionRecord for each existing translation of a message.

This is the typed counterpart of {

See also
getTranslations()}: callers that need more than page text and author name (e.g. revision timestamp) should use this method and read whatever fields they need from the RevisionRecord objects directly.

Results are ordered by page title (ascending), matching the order that {

See also
getTranslations()} returns.
Parameters
MessageHandle$handleLanguage code is ignored.
Returns
array<string, RevisionRecord> RevisionRecords indexed by page title.

Definition at line 458 of file Utilities.php.

◆ getTranslations()

static MediaWiki\Extension\Translate\Utilities\Utilities::getTranslations ( MessageHandle $handle)
static

Returns all translations of a given message.

Parameters
MessageHandle$handleLanguage code is ignored.
Returns
array ( string => array ( string, string ) ) Tuples of page text and last author indexed by page name.

Definition at line 432 of file Utilities.php.

◆ getVersion()

static MediaWiki\Extension\Translate\Utilities\Utilities::getVersion ( )
static

Definition at line 369 of file Utilities.php.

◆ isSupportedLanguageCode()

static MediaWiki\Extension\Translate\Utilities\Utilities::isSupportedLanguageCode ( string $code)
static

Checks whether a language code is supported for translation at the wiki level.

Note that it is possible that message groups define other language codes which are not supported by the wiki, in which case this function would return false for those.

Definition at line 395 of file Utilities.php.

◆ isTranslationPage()

static MediaWiki\Extension\Translate\Utilities\Utilities::isTranslationPage ( MessageHandle $handle)
static

Definition at line 491 of file Utilities.php.

◆ isValidRegex()

static MediaWiki\Extension\Translate\Utilities\Utilities::isValidRegex ( string $pattern,
?string & $error = null )
static

Validate a regular exception.

Parameters
string$patternThe regular expression to validate.
string | null&$errorOutput parameter containing the error message, if any.
Returns
bool Whether the regular exception is valid.

Definition at line 520 of file Utilities.php.

◆ serialize()

static MediaWiki\Extension\Translate\Utilities\Utilities::serialize ( $value)
static

Serialize the given value.

Parameters
mixed$value

Definition at line 357 of file Utilities.php.

◆ shouldReadFromPrimary()

static MediaWiki\Extension\Translate\Utilities\Utilities::shouldReadFromPrimary ( )
static

Check whether primary should be used for reads to avoid reading stale data.

Definition at line 308 of file Utilities.php.

◆ title()

static MediaWiki\Extension\Translate\Utilities\Utilities::title ( string $message,
string $code,
int $ns = NS_MEDIAWIKI )
static

Does quick normalisation of message name so that in can be looked from the database.

Parameters
string$messageName of the message
string$codeLanguage code in lower case and with dash as delimiter
int$nsNamespace constant
Returns
string The normalised title as a string.

Definition at line 39 of file Utilities.php.


The documentation for this class was generated from the following file: