MediaWiki master
RestStatusTrait.php
Go to the documentation of this file.
1<?php
2
4
10use StatusValue;
12
16trait RestStatusTrait {
17
18 private ?Converter $messageValueConverter = null;
19
20 private function getMessageValueConverter(): Converter {
21 if ( !$this->messageValueConverter ) {
22 $this->messageValueConverter = new Converter();
23 }
24 return $this->messageValueConverter;
25 }
26
32 private function convertStatusToMessageValues( StatusValue $status ): array {
33 $conv = $this->getMessageValueConverter();
34 return array_map( static function ( $error ) use ( $conv ) {
35 // TODO: It should be possible to do this without going through a Message object,
36 // but the internal format of parameters is different in MessageValue (T358779)
37 return $conv->convertMessage(
38 Message::newFromSpecifier( [ $error['message'], ...$error['params'], ] )
39 );
40 }, $status->getErrors() );
41 }
42
52 private function throwExceptionForStatus(
53 StatusValue $status,
54 $msg,
55 int $code,
56 array $data = []
57 ) {
58 $data += [ 'error-keys' => $this->getStatusErrorKeys( $status ) ];
59
60 if ( is_string( $msg ) ) {
61 $msg = MessageValue::new( $msg )
62 ->semicolonListParams(
63 $this->convertStatusToMessageValues( $status )
64 );
65 }
66
67 throw new LocalizedHttpException( $msg, $code, $data );
68 }
69
70 private function getStatusErrorKeys( StatusValue $status ) {
71 $keys = [];
72
73 foreach ( $status->getErrors() as [ 'message' => $msg ] ) {
74 if ( is_string( $msg ) ) {
75 $keys[] = $msg;
76 } elseif ( is_array( $msg ) ) {
77 $keys[] = $msg[0];
78 } elseif ( $msg instanceof MessageSpecifier ) {
79 $keys[] = $msg->getKey();
80 }
81 }
82
83 return array_unique( $keys );
84 }
85
86 private function logStatusError( StatusValue $status, string $message, string $channel ) {
87 LoggerFactory::getInstance( $channel )->error(
88 $message,
89 [ 'reason' => $this->getStatusErrorKeys( $status ) ]
90 );
91 }
92
93}
Create PSR-3 logger objects.
Converter between Message and MessageValue.
Definition Converter.php:18
The Message class deals with fetching and processing of interface message into a variety of formats.
Definition Message.php:158
static newFromSpecifier( $value)
Transform a MessageSpecifier or a primitive value used interchangeably with specifiers (a message key...
Definition Message.php:460
Generic operation result class Has warning/error list, boolean status and arbitrary value.
getErrors()
Get the list of errors.
Value object representing a message for i18n.
Copyright (C) 2011-2020 Wikimedia Foundation and others.