[SECURITY] Restore ability to suppress pages while deleting
[lhc/web/wiklou.git] / includes / Message.php
index c1a12aa..be6b0af 100644 (file)
  * @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';
+
+       /**
+        * Mapping from Message::listParam() types to Language methods.
+        * @var array
+        */
+       protected static $listTypeMap = [
+               'comma' => 'commaList',
+               'semicolon' => 'semicolonList',
+               'pipe' => 'pipeList',
+               'text' => 'listToText',
+       ];
 
        /**
         * In which language to get this message. True, which is the default,
@@ -190,15 +211,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 +361,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;
        }
 
@@ -403,7 +419,7 @@ class Message implements MessageSpecifier, Serializable {
                }
 
                if ( $value instanceof Message ) { // Message, RawMessage, ApiMessage, etc
-                       $message = clone( $value );
+                       $message = clone $value;
                } elseif ( $value instanceof MessageSpecifier ) {
                        $message = new Message( $value );
                } elseif ( is_string( $value ) ) {
@@ -472,18 +488,32 @@ class Message implements MessageSpecifier, Serializable {
         *
         * @since 1.17
         *
-        * @param mixed ... Parameters as strings, or a single argument that is
-        * an array of strings.
+        * @param mixed ... Parameters as strings or arrays from
+        *  Message::numParam() and the like, or a single array of parameters.
         *
         * @return Message $this
         */
        public function params( /*...*/ ) {
                $args = func_get_args();
-               if ( isset( $args[0] ) && is_array( $args[0] ) ) {
-                       $args = $args[0];
+
+               // If $args has only one entry and it's an array, then it's either a
+               // non-varargs call or it happens to be a call with just a single
+               // "special" parameter. Since the "special" parameters don't have any
+               // numeric keys, we'll test that to differentiate the cases.
+               if ( count( $args ) === 1 && isset( $args[0] ) && is_array( $args[0] ) ) {
+                       if ( $args[0] === [] ) {
+                               $args = [];
+                       } else {
+                               foreach ( $args[0] as $key => $value ) {
+                                       if ( is_int( $key ) ) {
+                                               $args = $args[0];
+                                               break;
+                                       }
+                               }
+                       }
                }
-               $args_values = array_values( $args );
-               $this->parameters = array_merge( $this->parameters, $args_values );
+
+               $this->parameters = array_merge( $this->parameters, array_values( $args ) );
                return $this;
        }
 
@@ -761,6 +791,7 @@ class Message implements MessageSpecifier, Serializable {
         */
        public function useDatabase( $useDatabase ) {
                $this->useDatabase = (bool)$useDatabase;
+               $this->message = null;
                return $this;
        }
 
@@ -796,9 +827,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 +848,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 +862,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;
        }
@@ -852,17 +893,11 @@ class Message implements MessageSpecifier, Serializable {
         * @return string
         */
        public function __toString() {
-               if ( $this->format !== 'parse' ) {
-                       $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 ] );
-               }
-
                // 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();
+                       return $this->toString( self::FORMAT_PARSE );
                } catch ( Exception $ex ) {
                        try {
                                trigger_error( "Exception caught in " . __METHOD__ . " (message " . $this->key . "): "
@@ -871,10 +906,7 @@ class Message implements MessageSpecifier, Serializable {
                                // Doh! Cause a fatal error after all?
                        }
 
-                       if ( $this->format === 'plain' || $this->format === 'text' ) {
-                               return '<' . $this->key . '>';
-                       }
-                       return '&lt;' . htmlspecialchars( $this->key ) . '&gt;';
+                       return '⧼' . htmlspecialchars( $this->key ) . '⧽';
                }
        }
 
@@ -886,8 +918,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 );
        }
 
        /**
@@ -898,8 +930,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 );
        }
 
        /**
@@ -910,8 +942,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 );
        }
 
        /**
@@ -922,8 +954,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 );
        }
 
        /**
@@ -935,8 +967,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 );
        }
 
        /**
@@ -1022,9 +1054,9 @@ class Message implements MessageSpecifier, Serializable {
        /**
         * @since 1.22
         *
-        * @param number $period
+        * @param int $period
         *
-        * @return number[] Array with a single "period" key.
+        * @return int[] Array with a single "period" key.
         */
        public static function timeperiodParam( $period ) {
                return [ 'period' => $period ];
@@ -1063,6 +1095,22 @@ class Message implements MessageSpecifier, Serializable {
                return [ 'plaintext' => $plaintext ];
        }
 
+       /**
+        * @since 1.29
+        *
+        * @param array $list
+        * @param string $type 'comma', 'semicolon', 'pipe', 'text'
+        * @return array Array with "list" and "type" keys.
+        */
+       public static function listParam( array $list, $type = 'text' ) {
+               if ( !isset( self::$listTypeMap[$type] ) ) {
+                       throw new InvalidArgumentException(
+                               "Invalid type '$type'. Known types are: " . join( ', ', array_keys( self::$listTypeMap ) )
+                       );
+               }
+               return [ 'list' => $list, 'type' => $type ];
+       }
+
        /**
         * Substitutes any parameters into the message text.
         *
@@ -1070,13 +1118,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;
                        }
@@ -1091,10 +1140,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'] ];
@@ -1113,7 +1163,9 @@ 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 ) ];
+                       } elseif ( isset( $param['list'] ) ) {
+                               return $this->formatListParam( $param['list'], $param['type'], $format );
                        } else {
                                $warning = 'Invalid parameter for message "' . $this->getKey() . '": ' .
                                        htmlspecialchars( serialize( $param ) );
@@ -1124,10 +1176,27 @@ class Message implements MessageSpecifier, Serializable {
                                return [ 'before', '[INVALID]' ];
                        }
                } elseif ( $param instanceof Message ) {
+                       // Match language, flags, etc. to the current message.
+                       $msg = clone $param;
+                       if ( $msg->language !== $this->language || $msg->useDatabase !== $this->useDatabase ) {
+                               // Cache depends on these parameters
+                               $msg->message = null;
+                       }
+                       $msg->interface = $this->interface;
+                       $msg->language = $this->language;
+                       $msg->useDatabase = $this->useDatabase;
+                       $msg->title = $this->title;
+
+                       // DWIM
+                       if ( $format === 'block-parse' ) {
+                               $format = 'parse';
+                       }
+                       $msg->format = $format;
+
                        // 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', $msg->toString( $format ) ];
                } else {
                        return [ 'before', $param ];
                }
@@ -1207,23 +1276,73 @@ 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 );
 
                }
        }
+
+       /**
+        * Formats a list of parameters as a concatenated string.
+        * @since 1.29
+        * @param array $params
+        * @param string $listType
+        * @param string $format One of the FORMAT_* constants.
+        * @return array Array with the parameter type (either "before" or "after") and the value.
+        */
+       protected function formatListParam( array $params, $listType, $format ) {
+               if ( !isset( self::$listTypeMap[$listType] ) ) {
+                       $warning = 'Invalid list type for message "' . $this->getKey() . '": '
+                               . htmlspecialchars( $listType )
+                               . ' (params are ' . htmlspecialchars( serialize( $params ) ) . ')';
+                       trigger_error( $warning, E_USER_WARNING );
+                       $e = new Exception;
+                       wfDebugLog( 'Bug58676', $warning . "\n" . $e->getTraceAsString() );
+                       return [ 'before', '[INVALID]' ];
+               }
+               $func = self::$listTypeMap[$listType];
+
+               // Handle an empty list sensibly
+               if ( !$params ) {
+                       return [ 'before', $this->getLanguage()->$func( [] ) ];
+               }
+
+               // First, determine what kinds of list items we have
+               $types = [];
+               $vars = [];
+               $list = [];
+               foreach ( $params as $n => $p ) {
+                       list( $type, $value ) = $this->extractParam( $p, $format );
+                       $types[$type] = true;
+                       $list[] = $value;
+                       $vars[] = '$' . ( $n + 1 );
+               }
+
+               // Easy case: all are 'before' or 'after', so just join the
+               // values and use the same type.
+               if ( count( $types ) === 1 ) {
+                       return [ key( $types ), $this->getLanguage()->$func( $list ) ];
+               }
+
+               // Hard case: We need to process each value per its type, then
+               // return the concatenated values as 'after'. We handle this by turning
+               // the list into a RawMessage and processing that as a parameter.
+               $vars = $this->getLanguage()->$func( $vars );
+               return $this->extractParam( new RawMessage( $vars, $params ), $format );
+       }
 }
 
 /**