diff options
author | jenkins-bot <jenkins-bot@gerrit.wikimedia.org> | 2024-11-13 17:16:45 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@wikimedia.org> | 2024-11-13 17:16:45 +0000 |
commit | e9a77c7d2c0523b70650ddffc37079e04272796f (patch) | |
tree | b6d91d68cd4e3686e7066a92b5e7f7677f8e7136 /includes/Status | |
parent | 2c2a8c0052cd22076c01db0d2168af768c1bdb9e (diff) | |
parent | fa7153d2432b51f4d48e982dfef76df34da8ce10 (diff) | |
download | mediawikicore-e9a77c7d2c0523b70650ddffc37079e04272796f.tar.gz mediawikicore-e9a77c7d2c0523b70650ddffc37079e04272796f.zip |
Merge "StatusFormatter: Allow passing context to getPsr3MessageAndContext()"
Diffstat (limited to 'includes/Status')
-rw-r--r-- | includes/Status/Status.php | 5 | ||||
-rw-r--r-- | includes/Status/StatusFormatter.php | 17 |
2 files changed, 12 insertions, 10 deletions
diff --git a/includes/Status/Status.php b/includes/Status/Status.php index e592660cebe0..fbba4b52a5be 100644 --- a/includes/Status/Status.php +++ b/includes/Status/Status.php @@ -266,14 +266,15 @@ class Status extends StatusValue { * Try to convert the status to a PSR-3 friendly format. The output will be similar to * getWikiText( false, false, 'en' ), but message parameters will be extracted into the * context array with parameter names 'parameter1' etc. when possible. + * A predefined context array may be passed for convenience. * * @deprecated since 1.42, use StatusFormatter instead. * * @return array A pair of (message, context) suitable for passing to a PSR-3 logger. * @phan-return array{0:string,1:(int|float|string)[]} */ - public function getPsr3MessageAndContext(): array { - return $this->getFormatter()->getPsr3MessageAndContext( $this ); + public function getPsr3MessageAndContext( array $context = [] ): array { + return $this->getFormatter()->getPsr3MessageAndContext( $this, $context ); } /** diff --git a/includes/Status/StatusFormatter.php b/includes/Status/StatusFormatter.php index c069b0dc6154..75beb33ca208 100644 --- a/includes/Status/StatusFormatter.php +++ b/includes/Status/StatusFormatter.php @@ -228,11 +228,12 @@ class StatusFormatter { * Try to convert the status to a PSR-3 friendly format. The output will be similar to * getWikiText( false, false, 'en' ), but message parameters will be extracted into the * context array with parameter names 'parameter1' etc. when possible. + * A predefined context array may be passed for convenience. * * @return array A pair of (message, context) suitable for passing to a PSR-3 logger. * @phan-return array{0:string,1:(int|float|string)[]} */ - public function getPsr3MessageAndContext( StatusValue $status ): array { + public function getPsr3MessageAndContext( StatusValue $status, array $context = [] ): array { $options = [ 'lang' => 'en' ]; $errors = $status->getErrors(); @@ -244,7 +245,7 @@ class StatusFormatter { // Fall back to getWikiText for rawmessage, which is just a placeholder for non-translated text. // Turning the entire message into a context parameter wouldn't be useful. if ( $message->getKey() === 'rawmessage' ) { - return [ $this->getWikiText( $status, $options ), [] ]; + return [ $this->getWikiText( $status, $options ), $context ]; } // $1,$2... will be left as-is when no parameters are provided. $text = $this->msgInLang( $message->getKey(), 'en' )->plain(); @@ -254,20 +255,20 @@ class StatusFormatter { $params = $message->getParamsOfRawMessage(); } else { // Unknown Message subclass, we can't be sure how it marks parameters. Fall back to getWikiText. - return [ $this->getWikiText( $status, $options ), [] ]; + return [ $this->getWikiText( $status, $options ), $context ]; } - $context = []; + $contextParams = []; $i = 1; foreach ( $params as $param ) { if ( $param instanceof MessageParam ) { $param = $param->getValue(); } if ( is_int( $param ) || is_float( $param ) || is_string( $param ) ) { - $context["parameter$i"] = $param; + $contextParams["parameter$i"] = $param; } else { // Parameter is not of a safe type, fall back to getWikiText. - return [ $this->getWikiText( $status, $options ), [] ]; + return [ $this->getWikiText( $status, $options ), $context ]; } $text = str_replace( "\$$i", "{parameter$i}", $text ); @@ -275,10 +276,10 @@ class StatusFormatter { $i++; } - return [ $text, $context ]; + return [ $text, $context + $contextParams ]; } // Parameters cannot be easily extracted, fall back to getWikiText, - return [ $this->getWikiText( $status, $options ), [] ]; + return [ $this->getWikiText( $status, $options ), $context ]; } /** |