37 use UnexpectedValueException;
76 public static function wrap( $sv ) {
77 if ( $sv instanceof
static ) {
81 $result =
new static();
82 $result->ok =& $sv->ok;
83 $result->errors =& $sv->errors;
84 $result->value =& $sv->value;
85 $result->successCount =& $sv->successCount;
86 $result->failCount =& $sv->failCount;
87 $result->success =& $sv->success;
99 public function __get( $name ) {
100 if ( $name ===
'ok' ) {
101 return $this->
isOK();
103 if ( $name ===
'errors' ) {
107 throw new RuntimeException(
"Cannot get '$name' property." );
119 if ( $name ===
'ok' ) {
121 } elseif ( !property_exists( $this, $name ) ) {
125 throw new RuntimeException(
"Cannot set '$name' property." );
155 [ $errorsOnlyStatus, $warningsOnlyStatus ] = parent::splitByErrorType();
157 '@phan-var Status $errorsOnlyStatus';
158 '@phan-var Status $warningsOnlyStatus';
160 if ( $this->messageLocalizer ) {
161 $errorsOnlyStatus->setMessageLocalizer( $this->messageLocalizer );
162 $warningsOnlyStatus->setMessageLocalizer( $this->messageLocalizer );
166 return [ $errorsOnlyStatus, $warningsOnlyStatus ];
183 if ( !$this->cleanCallback ) {
187 foreach ( $params as $i => $param ) {
188 $cleanParams[$i] = call_user_func( $this->cleanCallback, $param );
202 public function getWikiText( $shortContext =
false, $longContext =
false, $lang =
null ) {
204 if ( count( $rawErrors ) === 0 ) {
205 if ( $this->
isOK() ) {
207 'internalerror_info',
208 __METHOD__ .
" called for a good result, this is incorrect\n"
212 'internalerror_info',
213 __METHOD__ .
": Invalid result object: no error text but not OK\n"
218 if ( count( $rawErrors ) === 1 ) {
220 if ( $shortContext ) {
221 $s = $this->msgInLang( $shortContext, $lang, $s )->plain();
222 } elseif ( $longContext ) {
223 $s = $this->msgInLang( $longContext, $lang,
"* $s\n" )->plain();
227 foreach (
$errors as &$error ) {
228 $error = $error->plain();
230 $s =
'* ' . implode(
"\n* ",
$errors ) .
"\n";
231 if ( $longContext ) {
232 $s = $this->msgInLang( $longContext, $lang, $s )->plain();
233 } elseif ( $shortContext ) {
234 $s = $this->msgInLang( $shortContext, $lang,
"\n$s\n" )->plain();
260 public function getMessage( $shortContext =
false, $longContext =
false, $lang =
null ) {
262 if ( count( $rawErrors ) === 0 ) {
263 if ( $this->
isOK() ) {
265 'internalerror_info',
266 __METHOD__ .
" called for a good result, this is incorrect\n"
270 'internalerror_info',
271 __METHOD__ .
": Invalid result object: no error text but not OK\n"
276 if ( count( $rawErrors ) === 1 ) {
278 if ( $shortContext ) {
279 $s = $this->msgInLang( $shortContext, $lang, $s );
280 } elseif ( $longContext ) {
282 $wrapper->params( $s )->parse();
283 $s = $this->msgInLang( $longContext, $lang, $wrapper );
287 $msgCount = count( $msgs );
289 $s =
new RawMessage(
'* $' . implode(
"\n* \$", range( 1, $msgCount ) ) );
290 $s->params( $msgs )->parse();
292 if ( $longContext ) {
293 $s = $this->msgInLang( $longContext, $lang, $s );
294 } elseif ( $shortContext ) {
295 $wrapper =
new RawMessage(
"\n\$1\n", [ $s ] );
297 $s = $this->msgInLang( $shortContext, $lang, $wrapper );
313 if ( count( $this->errors ) === 1 ) {
318 if ( in_array( get_class( $message ), [ Message::class, ApiMessage::class ],
true ) ) {
321 if ( $message->getKey() ===
'rawmessage' ) {
322 return [ $this->
getWikiText(
false,
false,
'en' ), [] ];
325 $text = $this->msgInLang( $message->getKey(),
'en' )->plain();
326 } elseif ( in_array( get_class( $message ), [ RawMessage::class, ApiRawMessage::class ],
true ) ) {
327 $text = $message->getKey();
330 return [ $this->
getWikiText(
false,
false,
'en' ), [] ];
335 foreach ( $message->getParams() as $param ) {
336 if ( is_array( $param ) && count( $param ) === 1 ) {
338 $param = reset( $param );
340 if ( is_int( $param ) || is_float( $param ) || is_string( $param ) ) {
341 $context[
"parameter$i"] = $param;
344 return [ $this->
getWikiText(
false,
false,
'en' ), [] ];
347 $text = str_replace(
"\$$i",
"{parameter$i}", $text );
352 return [ $text, $context ];
355 return [ $this->
getWikiText(
false,
false,
'en' ), [] ];
369 if ( is_array( $error ) ) {
370 if ( isset( $error[
'message'] ) && $error[
'message'] instanceof
Message ) {
372 $msg = $this->msg( $error[
'message'] );
373 } elseif ( isset( $error[
'message'] ) && isset( $error[
'params'] ) ) {
376 array_map(
static function ( $param ) {
378 }, $this->cleanParams( $error[
'params'] ) )
381 $msgName = array_shift( $error );
384 array_map(
static function ( $param ) {
386 }, $this->cleanParams( $error ) )
389 } elseif ( is_string( $error ) ) {
390 $msg = $this->msg( $error );
392 throw new UnexpectedValueException(
'Got ' . get_class( $error ) .
' for key.' );
396 $msg->inLanguage( $lang );
409 public function getHTML( $shortContext =
false, $longContext =
false, $lang =
null ) {
410 $text = $this->getWikiText( $shortContext, $longContext, $lang );
412 ->parse( $text,
null,
true,
true, $lang );
414 ? $out->
getText( [
'enableSectionEditLinks' =>
false ] )
425 return array_map(
function ( $e ) use ( $lang ) {
426 return $this->getErrorMessage( $e, $lang );
438 return $this->getStatusArray(
'error' );
449 return $this->getStatusArray(
'warning' );
460 $keys = array_keys( get_object_vars( $this ) );
461 return array_diff( $keys, [
'cleanCallback',
'messageLocalizer' ] );
468 $this->cleanCallback =
false;
469 $this->messageLocalizer =
null;
477 private function msg( $key, ...$params ):
Message {
478 if ( $this->messageLocalizer ) {
479 return $this->messageLocalizer->msg( $key, ...$params );
491 private function msgInLang( $key, $lang, ...$params ):
Message {
492 $msg = $this->msg( $key, ...$params );
503 class_alias( Status::class,
'Status' );
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
wfEscapeWikiText( $text)
Escapes the given text so that it may be output using addWikiText() without any linking,...
if(!defined('MW_SETUP_CALLBACK'))
Extension of Message implementing IApiMessage.
Extension of RawMessage implementing IApiMessage.
Base class for language-specific code.
The Message class deals with fetching and processing of interface message into a variety of formats.
inLanguage( $lang)
Request the message in any language that is supported.
getText( $options=[])
Get the output HTML.
Generic operation result class Has warning/error list, boolean status and arbitrary value.
getErrors()
Get the list of errors.
setOK( $ok)
Change operation status.
isOK()
Returns whether the operation completed.
fatal( $message,... $parameters)
Add an error and set OK to false, indicating that the operation as a whole was fatal.
Interface for localizing messages in MediaWiki.