diff options
Diffstat (limited to 'includes/Message.php')
-rw-r--r-- | includes/Message.php | 165 |
1 files changed, 143 insertions, 22 deletions
diff --git a/includes/Message.php b/includes/Message.php index b0147b9a..5719f830 100644 --- a/includes/Message.php +++ b/includes/Message.php @@ -22,11 +22,11 @@ */ /** - * The Message class provides methods which fullfil two basic services: + * The Message class provides methods which fulfil two basic services: * - fetching interface messages * - processing messages into a variety of formats * - * First implemented with MediaWiki 1.17, the Message class is intented to + * First implemented with MediaWiki 1.17, the Message class is intended to * replace the old wfMsg* functions that over time grew unusable. * @see https://www.mediawiki.org/wiki/Manual:Messages_API for equivalences * between old and new functions. @@ -203,6 +203,11 @@ class Message { protected $title = null; /** + * Content object representing the message + */ + protected $content = null; + + /** * @var string */ protected $message; @@ -211,7 +216,7 @@ class Message { * Constructor. * @since 1.17 * @param $key: message key, or array of message keys to try and use the first non-empty message for - * @param $params Array message parameters + * @param array $params message parameters * @return Message: $this */ public function __construct( $key, $params = array() ) { @@ -222,11 +227,44 @@ class Message { } /** + * Returns the message key + * + * @since 1.21 + * + * @return string + */ + public function getKey() { + return $this->key; + } + + /** + * Returns the message parameters + * + * @since 1.21 + * + * @return string[] + */ + public function getParams() { + return $this->parameters; + } + + /** + * Returns the message format + * + * @since 1.21 + * + * @return string + */ + public function getFormat() { + return $this->format; + } + + /** * Factory function that is just wrapper for the real constructor. It is - * intented to be used instead of the real constructor, because it allows + * intended to be used instead of the real constructor, because it allows * chaining method calls, while new objects don't. * @since 1.17 - * @param $key String: message key + * @param string $key message key * @param Varargs: parameters as Strings * @return Message: $this */ @@ -247,9 +285,9 @@ class Message { public static function newFallbackSequence( /*...*/ ) { $keys = func_get_args(); if ( func_num_args() == 1 ) { - if ( is_array($keys[0]) ) { + if ( is_array( $keys[0] ) ) { // Allow an array to be passed as the first argument instead - $keys = array_values($keys[0]); + $keys = array_values( $keys[0] ); } else { // Optimize a single string to not need special fallback handling $keys = $keys[0]; @@ -332,6 +370,7 @@ class Message { * turned off. * @since 1.17 * @param $lang Mixed: language code or Language object. + * @throws MWException * @return Message: $this */ public function inLanguage( $lang ) { @@ -405,6 +444,18 @@ class Message { } /** + * Returns the message as a Content object. + * @return Content + */ + public function content() { + if ( !$this->content ) { + $this->content = new MessageContent( $this ); + } + + return $this->content; + } + + /** * Returns the message parsed from wikitext to HTML. * @since 1.17 * @return String: HTML @@ -413,13 +464,22 @@ class Message { $string = $this->fetchMessage(); if ( $string === false ) { - $key = htmlspecialchars( is_array( $this->key ) ? $this->key[0] : $this->key ); + $key = htmlspecialchars( is_array( $this->key ) ? $this->key[0] : $this->key ); if ( $this->format === 'plain' ) { return '<' . $key . '>'; } return '<' . $key . '>'; } + # Replace $* with a list of parameters for &uselang=qqx. + if ( strpos( $string, '$*' ) !== false ) { + $paramlist = ''; + if ( $this->parameters !== array() ) { + $paramlist = ': $' . implode( ', $', range( 1, count( $this->parameters ) ) ); + } + $string = str_replace( '$*', $paramlist, $string ); + } + # Replace parameters before text parsing $string = $this->replaceParameters( $string, 'before' ); @@ -430,11 +490,11 @@ class Message { if( preg_match( '/^<p>(.*)\n?<\/p>\n?$/sU', $string, $m ) ) { $string = $m[1]; } - } elseif( $this->format === 'block-parse' ){ + } elseif( $this->format === 'block-parse' ) { $string = $this->parseText( $string ); - } elseif( $this->format === 'text' ){ + } elseif( $this->format === 'text' ) { $string = $this->transformText( $string ); - } elseif( $this->format === 'escaped' ){ + } elseif( $this->format === 'escaped' ) { $string = $this->transformText( $string ); $string = htmlspecialchars( $string, ENT_QUOTES, 'UTF-8', false ); } @@ -447,13 +507,30 @@ class Message { /** * Magic method implementation of the above (for PHP >= 5.2.0), so we can do, eg: - * $foo = Message::get($key); + * $foo = Message::get( $key ); * $string = "<abbr>$foo</abbr>"; * @since 1.18 * @return String */ public function __toString() { - return $this->toString(); + // PHP doesn't allow __toString to throw exceptions and will + // trigger a fatal error if it does. So, catch any exceptions. + + try { + return $this->toString(); + } catch ( Exception $ex ) { + try { + trigger_error( "Exception caught in " . __METHOD__ . " (message " . $this->key . "): " + . $ex, E_USER_WARNING ); + } catch ( Exception $ex ) { + // Doh! Cause a fatal error after all? + } + + if ( $this->format === 'plain' ) { + return '<' . $this->key . '>'; + } + return '<' . $this->key . '>'; + } } /** @@ -477,7 +554,7 @@ class Message { } /** - * Returns the message text as-is, only parameters are subsituted. + * Returns the message text as-is, only parameters are substituted. * @since 1.17 * @return String: Unescaped untransformed message text. */ @@ -530,7 +607,7 @@ class Message { /** * Check whether a message does not exist, is an empty string, or is "-" * @since 1.18 - * @return Bool: true if is is and false if not + * @return Bool: true if it is and false if not */ public function isDisabled() { $message = $this->fetchMessage(); @@ -556,10 +633,10 @@ class Message { } /** - * Substitutes any paramaters into the message text. + * Substitutes any parameters into the message text. * @since 1.17 - * @param $message String: the message text - * @param $type String: either before or after + * @param string $message the message text + * @param string $type either before or after * @return String */ protected function replaceParameters( $message, $type = 'before' ) { @@ -577,7 +654,7 @@ class Message { /** * Extracts the parameter type and preprocessed the value if needed. * @since 1.18 - * @param $param String|Array: Parameter as defined in this class. + * @param string|array $param Parameter as defined in this class. * @return Tuple(type, value) */ protected function extractParam( $param ) { @@ -601,17 +678,18 @@ class Message { /** * Wrapper for what ever method we use to parse wikitext. * @since 1.17 - * @param $string String: Wikitext message contents + * @param string $string Wikitext message contents * @return string Wikitext parsed into HTML */ protected function parseText( $string ) { - return MessageCache::singleton()->parse( $string, $this->title, /*linestart*/true, $this->interface, $this->language )->getText(); + $out = MessageCache::singleton()->parse( $string, $this->title, /*linestart*/true, $this->interface, $this->language ); + return is_object( $out ) ? $out->getText() : $out; } /** * Wrapper for what ever method we use to {{-transform wikitext. * @since 1.17 - * @param $string String: Wikitext message contents + * @param string $string Wikitext message contents * @return string Wikitext with {{-constructs replaced with their values. */ protected function transformText( $string ) { @@ -621,6 +699,7 @@ class Message { /** * Wrapper for what ever method we use to get message contents * @since 1.17 + * @throws MWException * @return string */ protected function fetchMessage() { @@ -645,3 +724,45 @@ class Message { } } + +/** + * Variant of the Message class. + * + * Rather than treating the message key as a lookup + * value (which is passed to the MessageCache and + * translated as necessary), a RawMessage key is + * treated as the actual message. + * + * All other functionality (parsing, escaping, etc.) + * is preserved. + * + * @since 1.21 + */ +class RawMessage extends Message { + /** + * Call the parent constructor, then store the key as + * the message. + * + * @param string $key Message to use + * @param array $params Parameters for the message + * @see Message::__construct + */ + public function __construct( $key, $params = array() ) { + parent::__construct( $key, $params ); + // The key is the message. + $this->message = $key; + } + + /** + * Fetch the message (in this case, the key). + * + * @return string + */ + public function fetchMessage() { + // Just in case the message is unset somewhere. + if( !isset( $this->message ) ) { + $this->message = $this->key; + } + return $this->message; + } +} |