diff options
author | Sam Wilson <sam@samwilson.id.au> | 2020-03-13 11:37:14 +0800 |
---|---|---|
committer | Sam Wilson <sam@samwilson.id.au> | 2020-03-27 11:19:43 +0800 |
commit | 02db290c6406ebf1db5eadf308fcf4694b21929a (patch) | |
tree | 8bdd06e9c16341df1fa9e28690b2a6a5bc5f2cbd /includes/xml/XmlSelect.php | |
parent | a91378eef131add9df9ed105284e947af92cd66d (diff) | |
download | mediawikicore-02db290c6406ebf1db5eadf308fcf4694b21929a.tar.gz mediawikicore-02db290c6406ebf1db5eadf308fcf4694b21929a.zip |
Unify handling of options messages
There are a few messages that use a custom format for select
lists. This moves the parsing of these messages into a single
method.
Bug: T245565
Change-Id: I0a24d3458979d7cca2dc3cb38c1b0f700a88490a
Diffstat (limited to 'includes/xml/XmlSelect.php')
-rw-r--r-- | includes/xml/XmlSelect.php | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/includes/xml/XmlSelect.php b/includes/xml/XmlSelect.php index b778b05eb6f0..a3ed381fbd2b 100644 --- a/includes/xml/XmlSelect.php +++ b/includes/xml/XmlSelect.php @@ -133,4 +133,27 @@ class XmlSelect { return Html::rawElement( $this->tagName, $this->attributes, rtrim( $contents ) ); } + + /** + * Parse labels and values out of a comma- and colon-separated list of options, such as is used for + * expiry and duration lists. Documentation of the format is on translatewiki.net. + * @link https://translatewiki.net/wiki/Template:Doc-mediawiki-options-list + * @param string $msg The message to parse. + * @return string[] The options array, where keys are option labels (i.e. translations) + * and values are option values (i.e. untranslated). + */ + public static function parseOptionsMessage( string $msg ): array { + $options = []; + foreach ( explode( ',', $msg ) as $option ) { + // Normalize options that only have one part. + if ( strpos( $option, ':' ) === false ) { + $option = "$option:$option"; + } + // Extract the two parts. + list( $label, $value ) = explode( ':', $option ); + // Escape special chars just to be safe, even though there should never be any. + $options[ trim( $label ) ] = htmlspecialchars( trim( $value ) ); + } + return $options; + } } |