64 public static function wrap( $sv ) {
65 if ( $sv instanceof
static ) {
69 $result =
new static();
70 $result->ok =& $sv->ok;
71 $result->errors =& $sv->errors;
72 $result->value =& $sv->value;
73 $result->successCount =& $sv->successCount;
74 $result->failCount =& $sv->failCount;
75 $result->success =& $sv->success;
87 public function __get( $name ) {
88 if ( $name ===
'ok' ) {
91 if ( $name ===
'errors' ) {
95 throw new RuntimeException(
"Cannot get '$name' property." );
107 if ( $name ===
'ok' ) {
109 } elseif ( !property_exists( $this, $name ) ) {
113 throw new RuntimeException(
"Cannot set '$name' property." );
143 [ $errorsOnlyStatus, $warningsOnlyStatus ] = parent::splitByErrorType();
145 '@phan-var Status $errorsOnlyStatus';
146 '@phan-var Status $warningsOnlyStatus';
148 if ( $this->messageLocalizer ) {
149 $errorsOnlyStatus->setMessageLocalizer( $this->messageLocalizer );
150 $warningsOnlyStatus->setMessageLocalizer( $this->messageLocalizer );
152 $errorsOnlyStatus->cleanCallback =
155 return [ $errorsOnlyStatus, $warningsOnlyStatus ];
172 if ( !$this->cleanCallback ) {
176 foreach ( $params as $i => $param ) {
177 $cleanParams[$i] = call_user_func( $this->cleanCallback, $param );
193 if ( count( $rawErrors ) === 0 ) {
194 if ( $this->
isOK() ) {
195 $this->
fatal(
'internalerror_info',
196 __METHOD__ .
" called for a good result, this is incorrect\n" );
198 $this->
fatal(
'internalerror_info',
199 __METHOD__ .
": Invalid result object: no error text but not OK\n" );
203 if ( count( $rawErrors ) === 1 ) {
205 if ( $shortContext ) {
206 $s = $this->msgInLang( $shortContext,
$lang, $s )->plain();
207 } elseif ( $longContext ) {
208 $s = $this->msgInLang( $longContext,
$lang,
"* $s\n" )->plain();
212 foreach (
$errors as &$error ) {
213 $error = $error->plain();
215 $s =
'* ' . implode(
"\n* ",
$errors ) .
"\n";
216 if ( $longContext ) {
217 $s = $this->msgInLang( $longContext,
$lang, $s )->plain();
218 } elseif ( $shortContext ) {
219 $s = $this->msgInLang( $shortContext,
$lang,
"\n$s\n" )->plain();
245 public function getMessage( $shortContext =
false, $longContext =
false,
$lang =
null ) {
247 if ( count( $rawErrors ) === 0 ) {
248 if ( $this->
isOK() ) {
249 $this->
fatal(
'internalerror_info',
250 __METHOD__ .
" called for a good result, this is incorrect\n" );
252 $this->
fatal(
'internalerror_info',
253 __METHOD__ .
": Invalid result object: no error text but not OK\n" );
257 if ( count( $rawErrors ) === 1 ) {
259 if ( $shortContext ) {
260 $s = $this->msgInLang( $shortContext,
$lang, $s );
261 } elseif ( $longContext ) {
263 $wrapper->params( $s )->parse();
264 $s = $this->msgInLang( $longContext,
$lang, $wrapper );
268 $msgCount = count( $msgs );
270 $s =
new RawMessage(
'* $' . implode(
"\n* \$", range( 1, $msgCount ) ) );
271 $s->params( $msgs )->parse();
273 if ( $longContext ) {
274 $s = $this->msgInLang( $longContext,
$lang, $s );
275 } elseif ( $shortContext ) {
276 $wrapper =
new RawMessage(
"\n\$1\n", [ $s ] );
278 $s = $this->msgInLang( $shortContext,
$lang, $wrapper );
294 if ( count( $this->errors ) === 1 ) {
299 if ( in_array( get_class( $message ), [ Message::class, ApiMessage::class ],
true ) ) {
302 if ( $message->getKey() ===
'rawmessage' ) {
303 return [ $this->
getWikiText(
false,
false,
'en' ), [] ];
306 $text = $this->msgInLang( $message->getKey(),
'en' )->plain();
307 } elseif ( in_array( get_class( $message ), [ RawMessage::class, ApiRawMessage::class ],
true ) ) {
308 $text = $message->getKey();
311 return [ $this->
getWikiText(
false,
false,
'en' ), [] ];
316 foreach ( $message->getParams() as $param ) {
317 if ( is_array( $param ) && count( $param ) === 1 ) {
319 $param = reset( $param );
321 if ( is_int( $param ) || is_float( $param ) || is_string( $param ) ) {
322 $context[
"parameter$i"] = $param;
325 return [ $this->
getWikiText(
false,
false,
'en' ), [] ];
328 $text = str_replace(
"\$$i",
"{parameter$i}", $text );
333 return [ $text, $context ];
336 return [ $this->
getWikiText(
false,
false,
'en' ), [] ];
350 if ( is_array( $error ) ) {
351 if ( isset( $error[
'message'] ) && $error[
'message'] instanceof
Message ) {
353 $msg = $this->msg( $error[
'message'] );
354 } elseif ( isset( $error[
'message'] ) && isset( $error[
'params'] ) ) {
355 $msg = $this->msg( $error[
'message'], array_map(
static function ( $param ) {
357 }, $this->cleanParams( $error[
'params'] ) ) );
359 $msgName = array_shift( $error );
360 $msg = $this->msg( $msgName, array_map(
static function ( $param ) {
362 }, $this->cleanParams( $error ) ) );
364 } elseif ( is_string( $error ) ) {
365 $msg = $this->msg( $error );
367 throw new UnexpectedValueException(
'Got ' . get_class( $error ) .
' for key.' );
371 $msg->inLanguage(
$lang );
384 public function getHTML( $shortContext =
false, $longContext =
false,
$lang =
null ) {
385 $text = $this->getWikiText( $shortContext, $longContext,
$lang );
386 $out = MediaWikiServices::getInstance()->getMessageCache()
387 ->parse( $text,
null,
true,
true,
$lang );
389 ? $out->
getText( [
'enableSectionEditLinks' =>
false ] )
400 return array_map(
function ( $e ) use (
$lang ) {
401 return $this->getErrorMessage( $e,
$lang );
413 return $this->getStatusArray(
'error' );
424 return $this->getStatusArray(
'warning' );
435 $keys = array_keys( get_object_vars( $this ) );
436 return array_diff(
$keys, [
'cleanCallback',
'messageLocalizer' ] );
443 $this->cleanCallback =
false;
444 $this->messageLocalizer =
null;
452 private function msg( $key, ...$params ):
Message {
453 if ( $this->messageLocalizer ) {
454 return $this->messageLocalizer->msg( $key, ...$params );
466 private function msgInLang( $key,
$lang, ...$params ):
Message {
467 $msg = $this->msg( $key, ...$params );
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'))
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.
Generic operation result class Has warning/error list, boolean status and arbitrary value.
static wrap( $sv)
Succinct helper method to wrap a StatusValue.
setMessageLocalizer(MessageLocalizer $messageLocalizer)
Makes this Status object use the given localizer instead of the global one.
getWikiText( $shortContext=false, $longContext=false, $lang=null)
Get the error list as a wikitext formatted list.
splitByErrorType()
Splits this Status object into two new Status objects, one which contains only the error messages,...
getErrorsArray()
Get the list of errors (but not warnings)
getErrorMessage( $error, $lang=null)
Return the message for a single error.
callable false $cleanCallback
MessageLocalizer null $messageLocalizer
getPsr3MessageAndContext()
Try to convert the status to a PSR-3 friendly format.
__sleep()
Don't save the callback when serializing, because Closures can't be serialized and we're going to cle...
cleanParams(array $params)
__set( $name, $value)
Change operation result Backwards compatibility logic.
getErrorMessageArray( $errors, $lang=null)
Return an array with a Message object for each error.
getWarningsArray()
Get the list of warnings (but not errors)
getStatusValue()
Returns the wrapped StatusValue object.
getHTML( $shortContext=false, $longContext=false, $lang=null)
Get the error message as HTML.
__wakeup()
Sanitize the callback parameter on wakeup, to avoid arbitrary execution.
__get( $name)
Backwards compatibility logic.
getMessage( $shortContext=false, $longContext=false, $lang=null)
Get a bullet list of the errors as a Message object.
Interface for localizing messages in MediaWiki.
if(!isset( $args[0])) $lang