X-Git-Url: https://git.cyclocoop.org/%27.WWW_URL.%27admin/?a=blobdiff_plain;f=includes%2FMessage.php;h=c2c954ab628f6f073c7fe2c31061a1a0ce718285;hb=eb2f439768d34254964e798d9523211ba8ea2f5f;hp=fd016fcb371de8368b7509248cfbf8ec092a88ee;hpb=a0803c9bc551631c8a35bf2d99705a724cb62039;p=lhc%2Fweb%2Fwiklou.git diff --git a/includes/Message.php b/includes/Message.php index fd016fcb37..c2c954ab62 100644 --- a/includes/Message.php +++ b/includes/Message.php @@ -120,7 +120,7 @@ * * @code * // old style: - * wfMsgExt( 'key', array( 'parseinline' ), 'apple' ); + * wfMsgExt( 'key', [ 'parseinline' ], 'apple' ); * // new style: * wfMessage( 'key', 'apple' )->parse(); * @endcode @@ -131,7 +131,7 @@ * Places where HTML cannot be used. {{-transformation is done. * @code * // old style: - * wfMsgExt( 'key', array( 'parsemag' ), 'apple', 'pear' ); + * wfMsgExt( 'key', [ 'parsemag' ], 'apple', 'pear' ); * // new style: * wfMessage( 'key', 'apple', 'pear' )->text(); * @endcode @@ -382,6 +382,40 @@ class Message implements MessageSpecifier, Serializable { return new self( $key, $params ); } + /** + * Transform a MessageSpecifier or a primitive value used interchangeably with + * specifiers (a message key string, or a key + params array) into a proper Message. + * + * Also accepts a MessageSpecifier inside an array: that's not considered a valid format + * but is an easy error to make due to how StatusValue stores messages internally. + * Further array elements are ignored in that case. + * + * @param string|array|MessageSpecifier $value + * @return Message + * @throws InvalidArgumentException + * @since 1.27 + */ + public static function newFromSpecifier( $value ) { + $params = []; + if ( is_array( $value ) ) { + $params = $value; + $value = array_shift( $params ); + } + + if ( $value instanceof Message ) { // Message, RawMessage, ApiMessage, etc + $message = clone( $value ); + } elseif ( $value instanceof MessageSpecifier ) { + $message = new Message( $value ); + } elseif ( is_string( $value ) ) { + $message = new Message( $value, $params ); + } else { + throw new InvalidArgumentException( __METHOD__ . ': invalid argument type ' + . gettype( $value ) ); + } + + return $message; + } + /** * Factory function accepting multiple message keys and returning a message instance * for the first message which is non-empty. If all messages are empty then an @@ -421,12 +455,12 @@ class Message implements MessageSpecifier, Serializable { public function getTitle() { global $wgContLang, $wgForceUIMsgAsContentMsg; - $code = $this->getLanguage()->getCode(); $title = $this->key; if ( - $wgContLang->getCode() !== $code + !$this->language->equals( $wgContLang ) && in_array( $this->key, (array)$wgForceUIMsgAsContentMsg ) ) { + $code = $this->language->getCode(); $title .= '/' . $code; } @@ -768,10 +802,13 @@ class Message implements MessageSpecifier, Serializable { $string = $this->fetchMessage(); if ( $string === false ) { - if ( $this->format === 'plain' || $this->format === 'text' ) { - return '<' . $this->key . '>'; - } - return '<' . htmlspecialchars( $this->key ) . '>'; + // Err on the side of safety, ensure that the output + // is always html safe in the event the message key is + // missing, since in that case its highly likely the + // message key is user-controlled. + // '⧼' is used instead of '<' to side-step any + // double-escaping issues. + return '⧼' . htmlspecialchars( $this->key ) . '⧽'; } # Replace $* with a list of parameters for &uselang=qqx.