Translate extension for MediaWiki
 
Loading...
Searching...
No Matches
TranslateUtils Class Reference

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

Static Public Member Functions

static title ( $message, $code, $ns=NS_MEDIAWIKI)
 Does quick normalisation of message name so that in can be looked from the database.
 
static figureMessage ( $text)
 Splits page name into message key and language code.
 
static getMessageContent ( $key, $language, $namespace=NS_MEDIAWIKI)
 Loads page content without side effects.
 
static getContents ( $titles, $namespace)
 Fetches contents for pagenames in given namespace without side effects.
 
static getContentForTitle (Title $title, $addFuzzy=false)
 Returns the content for a given title and adds the fuzzy tag if requested.
 
static translationChanges ( $hours=24, $bots=false, $ns=null, array $extraFields=[])
 Fetches recent changes for titles in given namespaces.
 
static getLanguageName ( $code, $language='en')
 Returns a localised language name.
 
static languageSelector ( $language, $selectedId)
 Returns a language selector.
 
static getLanguageSelector ( $language, $labelOption=false)
 Standard language selector in Translate extension.
 
static getLanguageNames ( $code)
 Get translated language names for the languages generally supported for translation in the current wiki.
 
static messageKeyToGroup ( $namespace, $key)
 Returns the primary group message belongs to.
 
static messageKeyToGroups ( $namespace, $key)
 Returns the all the groups message belongs to.
 
static normaliseKey ( $namespace, $key)
 Converts page name and namespace to message index format.
 
static fieldset ( $legend, $contents, array $attributes=[])
 Constructs a fieldset with contents.
 
static convertWhiteSpaceToHTML ( $message)
 Escapes the message, and does some mangling to whitespace, so that it is preserved when outputted as-is to html page.
 
static assetPath ( $path)
 Construct the web address to given asset.
 
static cacheFile ( $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, $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)
 Get an URL that points to an editor for this message handle.
 
static serialize ( $value)
 Serialize the given value.
 
static deserialize ( $str, $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 getTextFromTextContent (?Content $content)
 
static getTranslations (MessageHandle $handle)
 Returns all translations of a given message.
 

Detailed Description

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

Definition at line 18 of file TranslateUtils.php.

Member Function Documentation

◆ allowsSubpages()

static TranslateUtils::allowsSubpages ( Title $title)
static

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

Definition at line 527 of file TranslateUtils.php.

◆ assetPath()

static TranslateUtils::assetPath ( $path)
static

Construct the web address to given asset.

Parameters
string$pathPath to the resource relative to extensions root directory.
Returns
string Full or partial web path.

Definition at line 368 of file TranslateUtils.php.

◆ cacheFile()

static TranslateUtils::cacheFile ( $filename)
static

Gets the path for cache files.

Parameters
string$filename
Returns
string Full path.
Exceptions
MWExceptionIf cache directory is not configured.

Definition at line 379 of file TranslateUtils.php.

◆ convertWhiteSpaceToHTML()

static TranslateUtils::convertWhiteSpaceToHTML ( $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 353 of file TranslateUtils.php.

◆ deserialize()

static TranslateUtils::deserialize ( $str,
$opts = [ 'allowed_classes' => false ] )
static

Deserialize the given string.

Parameters
string$str
array | null$opts
Returns
mixed

Definition at line 503 of file TranslateUtils.php.

◆ fieldset()

static TranslateUtils::fieldset ( $legend,
$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 336 of file TranslateUtils.php.

◆ figureMessage()

static TranslateUtils::figureMessage ( $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 49 of file TranslateUtils.php.

◆ getContentForTitle()

static TranslateUtils::getContentForTitle ( Title $title,
$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 129 of file TranslateUtils.php.

◆ getContents()

static TranslateUtils::getContents ( $titles,
$namespace )
static

Fetches contents for pagenames in given namespace without side effects.

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 79 of file TranslateUtils.php.

◆ getEditorUrl()

static TranslateUtils::getEditorUrl ( MessageHandle $handle)
static

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

Parameters
MessageHandle$handle
Returns
string Domain relative URL
Since
2017.10

Definition at line 474 of file TranslateUtils.php.

◆ getIcon()

static TranslateUtils::getIcon ( MessageGroup $g,
$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
Since
2013-04-01

Definition at line 412 of file TranslateUtils.php.

◆ getLanguageName()

static TranslateUtils::getLanguageName ( $code,
$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 226 of file TranslateUtils.php.

◆ getLanguageNames()

static TranslateUtils::getLanguageNames ( $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 ( language code => language name )

Definition at line 275 of file TranslateUtils.php.

◆ getLanguageSelector()

static TranslateUtils::getLanguageSelector ( $language,
$labelOption = false )
static

Standard language selector in Translate extension.

Parameters
string$languageLanguage code of the language the names should be localised to.
bool$labelOption
Returns
XmlSelect

Definition at line 252 of file TranslateUtils.php.

◆ getMessageContent()

static TranslateUtils::getMessageContent ( $key,
$language,
$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 64 of file TranslateUtils.php.

◆ getPlaceholder()

static TranslateUtils::getPlaceholder ( )
static

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

Returns
string
Since
2012-07-31

Definition at line 398 of file TranslateUtils.php.

◆ getSafeReadDB()

static TranslateUtils::getSafeReadDB ( )
static

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

Returns
\Wikimedia\Rdbms\IDatabase Primary for HTTP POST, CLI, DB already changed; replica otherwise

Definition at line 443 of file TranslateUtils.php.

◆ getTextFromTextContent()

static TranslateUtils::getTextFromTextContent ( ?Content $content)
static

Definition at line 547 of file TranslateUtils.php.

◆ getTranslations()

static TranslateUtils::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.
Since
2012-12-18

Definition at line 566 of file TranslateUtils.php.

◆ getVersion()

static TranslateUtils::getVersion ( )
static
Returns
string
Since
2020.05

Definition at line 511 of file TranslateUtils.php.

◆ isSupportedLanguageCode()

static TranslateUtils::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.

Parameters
string$code
Returns
bool

Definition at line 542 of file TranslateUtils.php.

◆ languageSelector()

static TranslateUtils::languageSelector ( $language,
$selectedId )
static

Returns a language selector.

Parameters
string$languageLanguage code of the language the names should be localised to.
string$selectedIdThe language code that is selected by default.
Returns
string

Definition at line 237 of file TranslateUtils.php.

◆ messageKeyToGroup()

static TranslateUtils::messageKeyToGroup ( $namespace,
$key )
static

Returns the primary group message belongs to.

Parameters
int$namespace
string$key
Returns
string|null Group id or null.

Definition at line 294 of file TranslateUtils.php.

◆ messageKeyToGroups()

static TranslateUtils::messageKeyToGroups ( $namespace,
$key )
static

Returns the all the groups message belongs to.

Parameters
int$namespace
string$key
Returns
string[] Possibly empty list of group ids.

Definition at line 306 of file TranslateUtils.php.

◆ normaliseKey()

static TranslateUtils::normaliseKey ( $namespace,
$key )
static

Converts page name and namespace to message index format.

Parameters
int$namespace
string$key
Returns
string

Definition at line 323 of file TranslateUtils.php.

◆ serialize()

static TranslateUtils::serialize ( $value)
static

Serialize the given value.

Parameters
mixed$value
Returns
string

Definition at line 493 of file TranslateUtils.php.

◆ shouldReadFromPrimary()

static TranslateUtils::shouldReadFromPrimary ( )
static

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

Returns
bool

Definition at line 455 of file TranslateUtils.php.

◆ title()

static TranslateUtils::title ( $message,
$code,
$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 27 of file TranslateUtils.php.

◆ translationChanges()

static TranslateUtils::translationChanges ( $hours = 24,
$bots = false,
$ns = null,
array $extraFields = [] )
static

Fetches recent changes for titles in given namespaces.

Parameters
int$hoursNumber of hours.
bool$botsShould bot edits be included.
null | int[]$nsList of namespace IDs.
string[]$extraFieldsList of extra columns to fetch.
Returns
array List of recent changes.

Definition at line 164 of file TranslateUtils.php.


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