aboutsummaryrefslogtreecommitdiffstats
path: root/includes/xml/XmlSelect.php
diff options
context:
space:
mode:
Diffstat (limited to 'includes/xml/XmlSelect.php')
-rw-r--r--includes/xml/XmlSelect.php23
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;
+ }
}