X-Git-Url: http://git.cyclocoop.org/?a=blobdiff_plain;f=includes%2FMessage.php;h=7d86d07517f566e8d83cadee54180b7df2f02854;hb=bee07f1578675a1ba8fd3248f406495b1b6b5016;hp=c2c954ab628f6f073c7fe2c31061a1a0ce718285;hpb=366b6bb86ba4d20c0f4bfa3b0e9d2de84ffe848b;p=lhc%2Fweb%2Fwiklou.git diff --git a/includes/Message.php b/includes/Message.php index c2c954ab62..7d86d07517 100644 --- a/includes/Message.php +++ b/includes/Message.php @@ -157,6 +157,16 @@ * @since 1.17 */ class Message implements MessageSpecifier, Serializable { + /** Use message text as-is */ + const FORMAT_PLAIN = 'plain'; + /** Use normal wikitext -> HTML parsing (the result will be wrapped in a block-level HTML tag) */ + const FORMAT_BLOCK_PARSE = 'block-parse'; + /** Use normal wikitext -> HTML parsing but strip the block-level wrapper */ + const FORMAT_PARSE = 'parse'; + /** Transform {{..}} constructs but don't transform to HTML */ + const FORMAT_TEXT = 'text'; + /** Transform {{..}} constructs, HTML-escape the result */ + const FORMAT_ESCAPED = 'escaped'; /** * In which language to get this message. True, which is the default, @@ -190,15 +200,8 @@ class Message implements MessageSpecifier, Serializable { protected $parameters = []; /** - * Format for the message. - * Supported formats are: - * * text (transform) - * * escaped (transform+htmlspecialchars) - * * block-parse - * * parse (default) - * * plain - * * @var string + * @deprecated */ protected $format = 'parse'; @@ -347,8 +350,10 @@ class Message implements MessageSpecifier, Serializable { * @since 1.21 * * @return string + * @deprecated since 1.29 formatting is not stateful */ public function getFormat() { + wfDeprecated( __METHOD__, '1.29' ); return $this->format; } @@ -796,9 +801,18 @@ class Message implements MessageSpecifier, Serializable { * * @since 1.17 * + * @param string|null $format One of the FORMAT_* constants. Null means use whatever was used + * the last time (this is for B/C and should be avoided). + * * @return string HTML */ - public function toString() { + public function toString( $format = null ) { + if ( $format === null ) { + $ex = new LogicException( __METHOD__ . ' using implicit format: ' . $this->format ); + \MediaWiki\Logger\LoggerFactory::getInstance( 'message-format' )->warning( + $ex->getMessage(), [ 'exception' => $ex, 'format' => $this->format, 'key' => $this->key ] ); + $format = $this->format; + } $string = $this->fetchMessage(); if ( $string === false ) { @@ -808,6 +822,7 @@ class Message implements MessageSpecifier, Serializable { // message key is user-controlled. // '⧼' is used instead of '<' to side-step any // double-escaping issues. + // (Keep synchronised with mw.Message#toString in JS.) return '⧼' . htmlspecialchars( $this->key ) . '⧽'; } @@ -821,23 +836,23 @@ class Message implements MessageSpecifier, Serializable { } # Replace parameters before text parsing - $string = $this->replaceParameters( $string, 'before' ); + $string = $this->replaceParameters( $string, 'before', $format ); # Maybe transform using the full parser - if ( $this->format === 'parse' ) { + if ( $format === self::FORMAT_PARSE ) { $string = $this->parseText( $string ); $string = Parser::stripOuterParagraph( $string ); - } elseif ( $this->format === 'block-parse' ) { + } elseif ( $format === self::FORMAT_BLOCK_PARSE ) { $string = $this->parseText( $string ); - } elseif ( $this->format === 'text' ) { + } elseif ( $format === self::FORMAT_TEXT ) { $string = $this->transformText( $string ); - } elseif ( $this->format === 'escaped' ) { + } elseif ( $format === self::FORMAT_ESCAPED ) { $string = $this->transformText( $string ); $string = htmlspecialchars( $string, ENT_QUOTES, 'UTF-8', false ); } # Raw parameter replacement - $string = $this->replaceParameters( $string, 'after' ); + $string = $this->replaceParameters( $string, 'after', $format ); return $string; } @@ -856,7 +871,7 @@ class Message implements MessageSpecifier, Serializable { // trigger a fatal error if it does. So, catch any exceptions. try { - return $this->toString(); + return $this->toString( self::FORMAT_PARSE ); } catch ( Exception $ex ) { try { trigger_error( "Exception caught in " . __METHOD__ . " (message " . $this->key . "): " @@ -865,10 +880,7 @@ class Message implements MessageSpecifier, Serializable { // Doh! Cause a fatal error after all? } - if ( $this->format === 'plain' || $this->format === 'text' ) { - return '<' . $this->key . '>'; - } - return '<' . htmlspecialchars( $this->key ) . '>'; + return '⧼' . htmlspecialchars( $this->key ) . '⧽'; } } @@ -880,8 +892,8 @@ class Message implements MessageSpecifier, Serializable { * @return string Parsed HTML. */ public function parse() { - $this->format = 'parse'; - return $this->toString(); + $this->format = self::FORMAT_PARSE; + return $this->toString( self::FORMAT_PARSE ); } /** @@ -892,8 +904,8 @@ class Message implements MessageSpecifier, Serializable { * @return string Unescaped message text. */ public function text() { - $this->format = 'text'; - return $this->toString(); + $this->format = self::FORMAT_TEXT; + return $this->toString( self::FORMAT_TEXT ); } /** @@ -904,8 +916,8 @@ class Message implements MessageSpecifier, Serializable { * @return string Unescaped untransformed message text. */ public function plain() { - $this->format = 'plain'; - return $this->toString(); + $this->format = self::FORMAT_PLAIN; + return $this->toString( self::FORMAT_PLAIN ); } /** @@ -916,8 +928,8 @@ class Message implements MessageSpecifier, Serializable { * @return string HTML */ public function parseAsBlock() { - $this->format = 'block-parse'; - return $this->toString(); + $this->format = self::FORMAT_BLOCK_PARSE; + return $this->toString( self::FORMAT_BLOCK_PARSE ); } /** @@ -929,8 +941,8 @@ class Message implements MessageSpecifier, Serializable { * @return string Escaped message text. */ public function escaped() { - $this->format = 'escaped'; - return $this->toString(); + $this->format = self::FORMAT_ESCAPED; + return $this->toString( self::FORMAT_ESCAPED ); } /** @@ -1064,13 +1076,14 @@ class Message implements MessageSpecifier, Serializable { * * @param string $message The message text. * @param string $type Either "before" or "after". + * @param string $format One of the FORMAT_* constants. * * @return string */ - protected function replaceParameters( $message, $type = 'before' ) { + protected function replaceParameters( $message, $type = 'before', $format ) { $replacementKeys = []; foreach ( $this->parameters as $n => $param ) { - list( $paramType, $value ) = $this->extractParam( $param ); + list( $paramType, $value ) = $this->extractParam( $param, $format ); if ( $type === $paramType ) { $replacementKeys['$' . ( $n + 1 )] = $value; } @@ -1085,10 +1098,11 @@ class Message implements MessageSpecifier, Serializable { * @since 1.18 * * @param mixed $param Parameter as defined in this class. + * @param string $format One of the FORMAT_* constants. * * @return array Array with the parameter type (either "before" or "after") and the value. */ - protected function extractParam( $param ) { + protected function extractParam( $param, $format ) { if ( is_array( $param ) ) { if ( isset( $param['raw'] ) ) { return [ 'after', $param['raw'] ]; @@ -1107,7 +1121,7 @@ class Message implements MessageSpecifier, Serializable { } elseif ( isset( $param['bitrate'] ) ) { return [ 'before', $this->getLanguage()->formatBitrate( $param['bitrate'] ) ]; } elseif ( isset( $param['plaintext'] ) ) { - return [ 'after', $this->formatPlaintext( $param['plaintext'] ) ]; + return [ 'after', $this->formatPlaintext( $param['plaintext'], $format ) ]; } else { $warning = 'Invalid parameter for message "' . $this->getKey() . '": ' . htmlspecialchars( serialize( $param ) ); @@ -1121,7 +1135,7 @@ class Message implements MessageSpecifier, Serializable { // Message objects should not be before parameters because // then they'll get double escaped. If the message needs to be // escaped, it'll happen right here when we call toString(). - return [ 'after', $param->toString() ]; + return [ 'after', $param->toString( $format ) ]; } else { return [ 'before', $param ]; } @@ -1201,18 +1215,19 @@ class Message implements MessageSpecifier, Serializable { * @since 1.25 * * @param string $plaintext String to ensure plaintext output of + * @param string $format One of the FORMAT_* constants. * - * @return string Input plaintext encoded for output to $this->format + * @return string Input plaintext encoded for output to $format */ - protected function formatPlaintext( $plaintext ) { - switch ( $this->format ) { - case 'text': - case 'plain': + protected function formatPlaintext( $plaintext, $format ) { + switch ( $format ) { + case self::FORMAT_TEXT: + case self::FORMAT_PLAIN: return $plaintext; - case 'parse': - case 'block-parse': - case 'escaped': + case self::FORMAT_PARSE: + case self::FORMAT_BLOCK_PARSE: + case self::FORMAT_ESCAPED: default: return htmlspecialchars( $plaintext, ENT_QUOTES );