MediaWiki REL1_40
Status.php
Go to the documentation of this file.
1<?php
26
46class Status extends StatusValue {
48 public $cleanCallback = false;
49
52
64 public static function wrap( $sv ) {
65 if ( $sv instanceof static ) {
66 return $sv;
67 }
68
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;
76 $result->statusData =& $sv->statusData;
77
78 return $result;
79 }
80
88 public function __get( $name ) {
89 if ( $name === 'ok' ) {
90 return $this->isOK();
91 }
92 if ( $name === 'errors' ) {
93 return $this->getErrors();
94 }
95
96 throw new RuntimeException( "Cannot get '$name' property." );
97 }
98
107 public function __set( $name, $value ) {
108 if ( $name === 'ok' ) {
109 $this->setOK( $value );
110 } elseif ( !property_exists( $this, $name ) ) {
111 // Caller is using undeclared ad-hoc properties
112 $this->$name = $value;
113 } else {
114 throw new RuntimeException( "Cannot set '$name' property." );
115 }
116 }
117
128 public function setMessageLocalizer( MessageLocalizer $messageLocalizer ) {
129 $this->messageLocalizer = $messageLocalizer;
130 }
131
143 public function splitByErrorType() {
144 [ $errorsOnlyStatus, $warningsOnlyStatus ] = parent::splitByErrorType();
145 // phan/phan#2133?
146 '@phan-var Status $errorsOnlyStatus';
147 '@phan-var Status $warningsOnlyStatus';
148
149 if ( $this->messageLocalizer ) {
150 $errorsOnlyStatus->setMessageLocalizer( $this->messageLocalizer );
151 $warningsOnlyStatus->setMessageLocalizer( $this->messageLocalizer );
152 }
153 $errorsOnlyStatus->cleanCallback =
154 $warningsOnlyStatus->cleanCallback = $this->cleanCallback;
155
156 return [ $errorsOnlyStatus, $warningsOnlyStatus ];
157 }
158
164 public function getStatusValue() {
165 return $this;
166 }
167
172 protected function cleanParams( array $params ) {
173 if ( !$this->cleanCallback ) {
174 return $params;
175 }
176 $cleanParams = [];
177 foreach ( $params as $i => $param ) {
178 $cleanParams[$i] = call_user_func( $this->cleanCallback, $param );
179 }
180 return $cleanParams;
181 }
182
192 public function getWikiText( $shortContext = false, $longContext = false, $lang = null ) {
193 $rawErrors = $this->getErrors();
194 if ( count( $rawErrors ) === 0 ) {
195 if ( $this->isOK() ) {
196 $this->fatal( 'internalerror_info',
197 __METHOD__ . " called for a good result, this is incorrect\n" );
198 } else {
199 $this->fatal( 'internalerror_info',
200 __METHOD__ . ": Invalid result object: no error text but not OK\n" );
201 }
202 $rawErrors = $this->getErrors(); // just added a fatal
203 }
204 if ( count( $rawErrors ) === 1 ) {
205 $s = $this->getErrorMessage( $rawErrors[0], $lang )->plain();
206 if ( $shortContext ) {
207 $s = $this->msgInLang( $shortContext, $lang, $s )->plain();
208 } elseif ( $longContext ) {
209 $s = $this->msgInLang( $longContext, $lang, "* $s\n" )->plain();
210 }
211 } else {
212 $errors = $this->getErrorMessageArray( $rawErrors, $lang );
213 foreach ( $errors as &$error ) {
214 $error = $error->plain();
215 }
216 $s = '* ' . implode( "\n* ", $errors ) . "\n";
217 if ( $longContext ) {
218 $s = $this->msgInLang( $longContext, $lang, $s )->plain();
219 } elseif ( $shortContext ) {
220 $s = $this->msgInLang( $shortContext, $lang, "\n$s\n" )->plain();
221 }
222 }
223 return $s;
224 }
225
246 public function getMessage( $shortContext = false, $longContext = false, $lang = null ) {
247 $rawErrors = $this->getErrors();
248 if ( count( $rawErrors ) === 0 ) {
249 if ( $this->isOK() ) {
250 $this->fatal( 'internalerror_info',
251 __METHOD__ . " called for a good result, this is incorrect\n" );
252 } else {
253 $this->fatal( 'internalerror_info',
254 __METHOD__ . ": Invalid result object: no error text but not OK\n" );
255 }
256 $rawErrors = $this->getErrors(); // just added a fatal
257 }
258 if ( count( $rawErrors ) === 1 ) {
259 $s = $this->getErrorMessage( $rawErrors[0], $lang );
260 if ( $shortContext ) {
261 $s = $this->msgInLang( $shortContext, $lang, $s );
262 } elseif ( $longContext ) {
263 $wrapper = new RawMessage( "* \$1\n" );
264 $wrapper->params( $s )->parse();
265 $s = $this->msgInLang( $longContext, $lang, $wrapper );
266 }
267 } else {
268 $msgs = $this->getErrorMessageArray( $rawErrors, $lang );
269 $msgCount = count( $msgs );
270
271 $s = new RawMessage( '* $' . implode( "\n* \$", range( 1, $msgCount ) ) );
272 $s->params( $msgs )->parse();
273
274 if ( $longContext ) {
275 $s = $this->msgInLang( $longContext, $lang, $s );
276 } elseif ( $shortContext ) {
277 $wrapper = new RawMessage( "\n\$1\n", [ $s ] );
278 $wrapper->parse();
279 $s = $this->msgInLang( $shortContext, $lang, $wrapper );
280 }
281 }
282
283 return $s;
284 }
285
294 public function getPsr3MessageAndContext(): array {
295 if ( count( $this->errors ) === 1 ) {
296 // identical to getMessage( false, false, 'en' ) when there's just one error
297 $message = $this->getErrorMessage( $this->errors[0], 'en' );
298
299 $text = null;
300 if ( in_array( get_class( $message ), [ Message::class, ApiMessage::class ], true ) ) {
301 // $1,$2... will be left as-is when no parameters are provided.
302 $text = $this->msgInLang( $message->getKey(), 'en' )->plain();
303 } elseif ( in_array( get_class( $message ), [ RawMessage::class, ApiRawMessage::class ], true ) ) {
304 $text = $message->getKey();
305 } else {
306 // Unknown Message subclass, we can't be sure how it marks parameters. Fall back to getWikiText.
307 return [ $this->getWikiText( false, false, 'en' ), [] ];
308 }
309
310 $context = [];
311 $i = 1;
312 foreach ( $message->getParams() as $param ) {
313 if ( is_array( $param ) && count( $param ) === 1 ) {
314 // probably Message::numParam() or similar
315 $param = reset( $param );
316 }
317 if ( is_int( $param ) || is_float( $param ) || is_string( $param ) ) {
318 $context["parameter$i"] = $param;
319 } else {
320 // Parameter is not of a safe type, fall back to getWikiText.
321 return [ $this->getWikiText( false, false, 'en' ), [] ];
322 }
323
324 $text = str_replace( "\$$i", "{parameter$i}", $text );
325
326 $i++;
327 }
328
329 return [ $text, $context ];
330 }
331 // Parameters cannot be easily extracted, fall back to getWikiText,
332 return [ $this->getWikiText( false, false, 'en' ), [] ];
333 }
334
345 protected function getErrorMessage( $error, $lang = null ) {
346 if ( is_array( $error ) ) {
347 if ( isset( $error['message'] ) && $error['message'] instanceof Message ) {
348 // Apply context from MessageLocalizer even if we have a Message object already
349 $msg = $this->msg( $error['message'] );
350 } elseif ( isset( $error['message'] ) && isset( $error['params'] ) ) {
351 $msg = $this->msg( $error['message'], array_map( static function ( $param ) {
352 return is_string( $param ) ? wfEscapeWikiText( $param ) : $param;
353 }, $this->cleanParams( $error['params'] ) ) );
354 } else {
355 $msgName = array_shift( $error );
356 $msg = $this->msg( $msgName, array_map( static function ( $param ) {
357 return is_string( $param ) ? wfEscapeWikiText( $param ) : $param;
358 }, $this->cleanParams( $error ) ) );
359 }
360 } elseif ( is_string( $error ) ) {
361 $msg = $this->msg( $error );
362 } else {
363 throw new UnexpectedValueException( 'Got ' . get_class( $error ) . ' for key.' );
364 }
365
366 if ( $lang ) {
367 $msg->inLanguage( $lang );
368 }
369 return $msg;
370 }
371
380 public function getHTML( $shortContext = false, $longContext = false, $lang = null ) {
381 $text = $this->getWikiText( $shortContext, $longContext, $lang );
382 $out = MediaWikiServices::getInstance()->getMessageCache()
383 ->parse( $text, null, true, true, $lang );
384 return $out instanceof ParserOutput
385 ? $out->getText( [ 'enableSectionEditLinks' => false ] )
386 : $out;
387 }
388
395 protected function getErrorMessageArray( $errors, $lang = null ) {
396 return array_map( function ( $e ) use ( $lang ) {
397 return $this->getErrorMessage( $e, $lang );
398 }, $errors );
399 }
400
409 public function getErrorsArray() {
410 return $this->getStatusArray( 'error' );
411 }
412
421 public function getWarningsArray() {
422 return $this->getStatusArray( 'warning' );
423 }
424
432 public function __sleep() {
433 $keys = array_keys( get_object_vars( $this ) );
434 return array_diff( $keys, [ 'cleanCallback', 'messageLocalizer' ] );
435 }
436
440 public function __wakeup() {
441 $this->cleanCallback = false;
442 $this->messageLocalizer = null;
443 }
444
450 private function msg( $key, ...$params ): Message {
451 if ( $this->messageLocalizer ) {
452 return $this->messageLocalizer->msg( $key, ...$params );
453 } else {
454 return wfMessage( $key, ...$params );
455 }
456 }
457
464 private function msgInLang( $key, $lang, ...$params ): Message {
465 $msg = $this->msg( $key, ...$params );
466 if ( $lang ) {
467 $msg->inLanguage( $lang );
468 }
469 return $msg;
470 }
471}
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 persistent session ID (if any) loaded at startup.
Definition WebStart.php:88
Variant of the Message class.
Service locator for MediaWiki core services.
Stub object for the user language.
The Message class deals with fetching and processing of interface message into a variety of formats.
Definition Message.php:144
inLanguage( $lang)
Request the message in any language that is supported.
Definition Message.php:835
getText( $options=[])
Get the output HTML.
Generic operation result class Has warning/error list, boolean status and arbitrary value.
array[] $errors
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.
Definition Status.php:46
static wrap( $sv)
Succinct helper method to wrap a StatusValue.
Definition Status.php:64
setMessageLocalizer(MessageLocalizer $messageLocalizer)
Makes this Status object use the given localizer instead of the global one.
Definition Status.php:128
getWikiText( $shortContext=false, $longContext=false, $lang=null)
Get the error list as a wikitext formatted list.
Definition Status.php:192
splitByErrorType()
Splits this Status object into two new Status objects, one which contains only the error messages,...
Definition Status.php:143
getErrorsArray()
Get the list of errors (but not warnings)
Definition Status.php:409
getErrorMessage( $error, $lang=null)
Return the message for a single error.
Definition Status.php:345
callable false $cleanCallback
Definition Status.php:48
MessageLocalizer null $messageLocalizer
Definition Status.php:51
getPsr3MessageAndContext()
Try to convert the status to a PSR-3 friendly format.
Definition Status.php:294
__sleep()
Don't save the callback when serializing, because Closures can't be serialized and we're going to cle...
Definition Status.php:432
cleanParams(array $params)
Definition Status.php:172
__set( $name, $value)
Change operation result Backwards compatibility logic.
Definition Status.php:107
getErrorMessageArray( $errors, $lang=null)
Return an array with a Message object for each error.
Definition Status.php:395
getWarningsArray()
Get the list of warnings (but not errors)
Definition Status.php:421
getStatusValue()
Returns the wrapped StatusValue object.
Definition Status.php:164
getHTML( $shortContext=false, $longContext=false, $lang=null)
Get the error message as HTML.
Definition Status.php:380
__wakeup()
Sanitize the callback parameter on wakeup, to avoid arbitrary execution.
Definition Status.php:440
__get( $name)
Backwards compatibility logic.
Definition Status.php:88
getMessage( $shortContext=false, $longContext=false, $lang=null)
Get a bullet list of the errors as a Message object.
Definition Status.php:246
Interface for localizing messages in MediaWiki.
if(!isset( $args[0])) $lang