*
* @since 1.17
*/
-class Message implements MessageSpecifier {
+class Message implements MessageSpecifier, Serializable {
/**
* In which language to get this message. True, which is the default,
/**
* @var array List of parameters which will be substituted into the message.
*/
- protected $parameters = array();
+ protected $parameters = [];
/**
* Format for the message.
/**
* @since 1.17
*
- * @param string|string[] $key Message key or array of message keys to try and use the first
- * non-empty message for.
+ * @param string|string[]|MessageSpecifier $key Message key, or array of
+ * message keys to try and use the first non-empty message for, or a
+ * MessageSpecifier to copy from.
* @param array $params Message parameters.
* @param Language $language Optional language of the message, defaults to $wgLang.
*
* @throws InvalidArgumentException
*/
- public function __construct( $key, $params = array(), Language $language = null ) {
+ public function __construct( $key, $params = [], Language $language = null ) {
global $wgLang;
+ if ( $key instanceof MessageSpecifier ) {
+ if ( $params ) {
+ throw new InvalidArgumentException(
+ '$params must be empty if $key is a MessageSpecifier'
+ );
+ }
+ $params = $key->getParams();
+ $key = $key->getKey();
+ }
+
if ( !is_string( $key ) && !is_array( $key ) ) {
throw new InvalidArgumentException( '$key must be a string or an array' );
}
$this->language = $language ?: $wgLang;
}
+ /**
+ * @see Serializable::serialize()
+ * @since 1.26
+ * @return string
+ */
+ public function serialize() {
+ return serialize( [
+ 'interface' => $this->interface,
+ 'language' => $this->language instanceof StubUserLang ? false : $this->language->getCode(),
+ 'key' => $this->key,
+ 'keysToTry' => $this->keysToTry,
+ 'parameters' => $this->parameters,
+ 'format' => $this->format,
+ 'useDatabase' => $this->useDatabase,
+ 'title' => $this->title,
+ ] );
+ }
+
+ /**
+ * @see Serializable::unserialize()
+ * @since 1.26
+ * @param string $serialized
+ */
+ public function unserialize( $serialized ) {
+ global $wgLang;
+
+ $data = unserialize( $serialized );
+ $this->interface = $data['interface'];
+ $this->key = $data['key'];
+ $this->keysToTry = $data['keysToTry'];
+ $this->parameters = $data['parameters'];
+ $this->format = $data['format'];
+ $this->useDatabase = $data['useDatabase'];
+ $this->language = $data['language'] ? Language::factory( $data['language'] ) : $wgLang;
+ $this->title = $data['title'];
+ }
+
/**
* @since 1.24
*
*
* @since 1.17
*
- * @param string|string[] $key Message key or array of keys.
+ * @param string|string[]|MessageSpecifier $key
* @param mixed $param,... Parameters as strings.
*
* @return Message
return new self( $keys );
}
+ /**
+ * Get a title object for a mediawiki message, where it can be found in the mediawiki namespace.
+ * The title will be for the current language, if the message key is in
+ * $wgForceUIMsgAsContentMsg it will be append with the language code (except content
+ * language), because Message::inContentLanguage will also return in user language.
+ *
+ * @see $wgForceUIMsgAsContentMsg
+ * @return Title
+ * @since 1.26
+ */
+ public function getTitle() {
+ global $wgContLang, $wgForceUIMsgAsContentMsg;
+
+ $code = $this->language->getCode();
+ $title = $this->key;
+ if (
+ $wgContLang->getCode() !== $code
+ && in_array( $this->key, (array)$wgForceUIMsgAsContentMsg )
+ ) {
+ $title .= '/' . $code;
+ }
+
+ return Title::makeTitle( NS_MEDIAWIKI, $wgContLang->ucfirst( strtr( $title, ' ', '_' ) ) );
+ }
+
/**
* Adds parameters to the parameter list of this message.
*
* @since 1.17
*
- * @param mixed $params,... Parameters as strings, or a single argument that is
+ * @param mixed ... Parameters as strings, or a single argument that is
* an array of strings.
*
* @return Message $this
# Replace $* with a list of parameters for &uselang=qqx.
if ( strpos( $string, '$*' ) !== false ) {
$paramlist = '';
- if ( $this->parameters !== array() ) {
+ if ( $this->parameters !== [] ) {
$paramlist = ': $' . implode( ', $', range( 1, count( $this->parameters ) ) );
}
$string = str_replace( '$*', $paramlist, $string );
* @return array Array with a single "raw" key.
*/
public static function rawParam( $raw ) {
- return array( 'raw' => $raw );
+ return [ 'raw' => $raw ];
}
/**
* @return array Array with a single "num" key.
*/
public static function numParam( $num ) {
- return array( 'num' => $num );
+ return [ 'num' => $num ];
}
/**
* @return int[] Array with a single "duration" key.
*/
public static function durationParam( $duration ) {
- return array( 'duration' => $duration );
+ return [ 'duration' => $duration ];
}
/**
* @return string[] Array with a single "expiry" key.
*/
public static function expiryParam( $expiry ) {
- return array( 'expiry' => $expiry );
+ return [ 'expiry' => $expiry ];
}
/**
* @return number[] Array with a single "period" key.
*/
public static function timeperiodParam( $period ) {
- return array( 'period' => $period );
+ return [ 'period' => $period ];
}
/**
* @return int[] Array with a single "size" key.
*/
public static function sizeParam( $size ) {
- return array( 'size' => $size );
+ return [ 'size' => $size ];
}
/**
* @return int[] Array with a single "bitrate" key.
*/
public static function bitrateParam( $bitrate ) {
- return array( 'bitrate' => $bitrate );
+ return [ 'bitrate' => $bitrate ];
}
/**
* @return string[] Array with a single "plaintext" key.
*/
public static function plaintextParam( $plaintext ) {
- return array( 'plaintext' => $plaintext );
+ return [ 'plaintext' => $plaintext ];
}
/**
* @return string
*/
protected function replaceParameters( $message, $type = 'before' ) {
- $replacementKeys = array();
+ $replacementKeys = [];
foreach ( $this->parameters as $n => $param ) {
list( $paramType, $value ) = $this->extractParam( $param );
if ( $type === $paramType ) {
protected function extractParam( $param ) {
if ( is_array( $param ) ) {
if ( isset( $param['raw'] ) ) {
- return array( 'after', $param['raw'] );
+ return [ 'after', $param['raw'] ];
} elseif ( isset( $param['num'] ) ) {
// Replace number params always in before step for now.
// No support for combined raw and num params
- return array( 'before', $this->language->formatNum( $param['num'] ) );
+ return [ 'before', $this->language->formatNum( $param['num'] ) ];
} elseif ( isset( $param['duration'] ) ) {
- return array( 'before', $this->language->formatDuration( $param['duration'] ) );
+ return [ 'before', $this->language->formatDuration( $param['duration'] ) ];
} elseif ( isset( $param['expiry'] ) ) {
- return array( 'before', $this->language->formatExpiry( $param['expiry'] ) );
+ return [ 'before', $this->language->formatExpiry( $param['expiry'] ) ];
} elseif ( isset( $param['period'] ) ) {
- return array( 'before', $this->language->formatTimePeriod( $param['period'] ) );
+ return [ 'before', $this->language->formatTimePeriod( $param['period'] ) ];
} elseif ( isset( $param['size'] ) ) {
- return array( 'before', $this->language->formatSize( $param['size'] ) );
+ return [ 'before', $this->language->formatSize( $param['size'] ) ];
} elseif ( isset( $param['bitrate'] ) ) {
- return array( 'before', $this->language->formatBitrate( $param['bitrate'] ) );
+ return [ 'before', $this->language->formatBitrate( $param['bitrate'] ) ];
} elseif ( isset( $param['plaintext'] ) ) {
- return array( 'after', $this->formatPlaintext( $param['plaintext'] ) );
+ return [ 'after', $this->formatPlaintext( $param['plaintext'] ) ];
} else {
$warning = 'Invalid parameter for message "' . $this->getKey() . '": ' .
htmlspecialchars( serialize( $param ) );
$e = new Exception;
wfDebugLog( 'Bug58676', $warning . "\n" . $e->getTraceAsString() );
- return array( 'before', '[INVALID]' );
+ return [ 'before', '[INVALID]' ];
}
} elseif ( $param instanceof Message ) {
// 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 array( 'after', $param->toString() );
+ return [ 'after', $param->toString() ];
} else {
- return array( 'before', $param );
+ return [ 'before', $param ];
}
}
*
* @throws InvalidArgumentException
*/
- public function __construct( $text, $params = array() ) {
+ public function __construct( $text, $params = [] ) {
if ( !is_string( $text ) ) {
throw new InvalidArgumentException( '$text must be a string' );
}