37use 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;
88 $result->statusData =& $sv->statusData;
101 if ( $name ===
'ok' ) {
102 return $this->
isOK();
104 if ( $name ===
'errors' ) {
108 throw new RuntimeException(
"Cannot get '$name' property." );
120 if ( $name ===
'ok' ) {
122 } elseif ( !property_exists( $this, $name ) ) {
126 throw new RuntimeException(
"Cannot set '$name' property." );
156 [ $errorsOnlyStatus, $warningsOnlyStatus ] = parent::splitByErrorType();
158 '@phan-var Status $errorsOnlyStatus';
159 '@phan-var Status $warningsOnlyStatus';
161 if ( $this->messageLocalizer ) {
162 $errorsOnlyStatus->setMessageLocalizer( $this->messageLocalizer );
163 $warningsOnlyStatus->setMessageLocalizer( $this->messageLocalizer );
167 return [ $errorsOnlyStatus, $warningsOnlyStatus ];
184 if ( !$this->cleanCallback ) {
188 foreach ( $params as $i => $param ) {
189 $cleanParams[$i] = call_user_func( $this->cleanCallback, $param );
203 public function getWikiText( $shortContext =
false, $longContext =
false, $lang =
null ) {
205 if ( count( $rawErrors ) === 0 ) {
206 if ( $this->
isOK() ) {
208 'internalerror_info',
209 __METHOD__ .
" called for a good result, this is incorrect\n"
213 'internalerror_info',
214 __METHOD__ .
": Invalid result object: no error text but not OK\n"
219 if ( count( $rawErrors ) === 1 ) {
221 if ( $shortContext ) {
222 $s = $this->msgInLang( $shortContext, $lang, $s )->plain();
223 } elseif ( $longContext ) {
224 $s = $this->msgInLang( $longContext, $lang,
"* $s\n" )->plain();
228 foreach (
$errors as &$error ) {
229 $error = $error->plain();
231 $s =
'* ' . implode(
"\n* ",
$errors ) .
"\n";
232 if ( $longContext ) {
233 $s = $this->msgInLang( $longContext, $lang, $s )->plain();
234 } elseif ( $shortContext ) {
235 $s = $this->msgInLang( $shortContext, $lang,
"\n$s\n" )->plain();
261 public function getMessage( $shortContext =
false, $longContext =
false, $lang =
null ) {
263 if ( count( $rawErrors ) === 0 ) {
264 if ( $this->
isOK() ) {
266 'internalerror_info',
267 __METHOD__ .
" called for a good result, this is incorrect\n"
271 'internalerror_info',
272 __METHOD__ .
": Invalid result object: no error text but not OK\n"
277 if ( count( $rawErrors ) === 1 ) {
279 if ( $shortContext ) {
280 $s = $this->msgInLang( $shortContext, $lang, $s );
281 } elseif ( $longContext ) {
283 $wrapper->params( $s )->parse();
284 $s = $this->msgInLang( $longContext, $lang, $wrapper );
288 $msgCount = count( $msgs );
290 $s =
new RawMessage(
'* $' . implode(
"\n* \$", range( 1, $msgCount ) ) );
291 $s->params( $msgs )->parse();
293 if ( $longContext ) {
294 $s = $this->msgInLang( $longContext, $lang, $s );
295 } elseif ( $shortContext ) {
296 $wrapper =
new RawMessage(
"\n\$1\n", [ $s ] );
298 $s = $this->msgInLang( $shortContext, $lang, $wrapper );
314 if ( count( $this->errors ) === 1 ) {
319 if ( in_array( get_class( $message ), [ Message::class, ApiMessage::class ],
true ) ) {
322 if ( $message->getKey() ===
'rawmessage' ) {
323 return [ $this->
getWikiText(
false,
false,
'en' ), [] ];
326 $text = $this->msgInLang( $message->getKey(),
'en' )->plain();
327 } elseif ( in_array( get_class( $message ), [ RawMessage::class, ApiRawMessage::class ],
true ) ) {
328 $text = $message->getKey();
331 return [ $this->
getWikiText(
false,
false,
'en' ), [] ];
336 foreach ( $message->getParams() as $param ) {
337 if ( is_array( $param ) && count( $param ) === 1 ) {
339 $param = reset( $param );
341 if ( is_int( $param ) || is_float( $param ) || is_string( $param ) ) {
342 $context[
"parameter$i"] = $param;
345 return [ $this->
getWikiText(
false,
false,
'en' ), [] ];
348 $text = str_replace(
"\$$i",
"{parameter$i}", $text );
353 return [ $text, $context ];
356 return [ $this->
getWikiText(
false,
false,
'en' ), [] ];
370 if ( is_array( $error ) ) {
371 if ( isset( $error[
'message'] ) && $error[
'message'] instanceof
Message ) {
373 $msg = $this->msg( $error[
'message'] );
374 } elseif ( isset( $error[
'message'] ) && isset( $error[
'params'] ) ) {
377 array_map(
static function ( $param ) {
379 }, $this->cleanParams( $error[
'params'] ) )
382 $msgName = array_shift( $error );
385 array_map(
static function ( $param ) {
387 }, $this->cleanParams( $error ) )
390 } elseif ( is_string( $error ) ) {
391 $msg = $this->msg( $error );
393 throw new UnexpectedValueException(
'Got ' . get_class( $error ) .
' for key.' );
397 $msg->inLanguage( $lang );
410 public function getHTML( $shortContext =
false, $longContext =
false, $lang =
null ) {
411 $text = $this->getWikiText( $shortContext, $longContext, $lang );
413 ->parse( $text,
null,
true,
true, $lang );
415 ? $out->
getText( [
'enableSectionEditLinks' =>
false ] )
426 return array_map(
function ( $e ) use ( $lang ) {
427 return $this->getErrorMessage( $e, $lang );
439 return $this->getStatusArray(
'error' );
450 return $this->getStatusArray(
'warning' );
461 $keys = array_keys( get_object_vars( $this ) );
462 return array_diff( $keys, [
'cleanCallback',
'messageLocalizer' ] );
469 $this->cleanCallback =
false;
470 $this->messageLocalizer =
null;
478 private function msg( $key, ...$params ):
Message {
479 if ( $this->messageLocalizer ) {
480 return $this->messageLocalizer->msg( $key, ...$params );
492 private function msgInLang( $key, $lang, ...$params ):
Message {
493 $msg = $this->msg( $key, ...$params );
504class_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.
Rendered output of a wiki page, as parsed from wikitext.
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.