MediaWiki  master
Status.php
Go to the documentation of this file.
1 <?php
40 class Status extends StatusValue {
42  public $cleanCallback = false;
43 
45  protected $messageLocalizer;
46 
58  public static function wrap( $sv ) {
59  if ( $sv instanceof static ) {
60  return $sv;
61  }
62 
63  $result = new static();
64  $result->ok =& $sv->ok;
65  $result->errors =& $sv->errors;
66  $result->value =& $sv->value;
67  $result->successCount =& $sv->successCount;
68  $result->failCount =& $sv->failCount;
69  $result->success =& $sv->success;
70 
71  return $result;
72  }
73 
81  public function __get( $name ) {
82  if ( $name === 'ok' ) {
83  return $this->isOK();
84  }
85  if ( $name === 'errors' ) {
86  return $this->getErrors();
87  }
88 
89  throw new RuntimeException( "Cannot get '$name' property." );
90  }
91 
100  public function __set( $name, $value ) {
101  if ( $name === 'ok' ) {
102  $this->setOK( $value );
103  } elseif ( !property_exists( $this, $name ) ) {
104  // Caller is using undeclared ad-hoc properties
105  $this->$name = $value;
106  } else {
107  throw new RuntimeException( "Cannot set '$name' property." );
108  }
109  }
110 
122  $this->messageLocalizer = $messageLocalizer;
123  }
124 
136  public function splitByErrorType() {
137  list( $errorsOnlyStatus, $warningsOnlyStatus ) = parent::splitByErrorType();
138  // phan/phan#2133?
139  '@phan-var Status $errorsOnlyStatus';
140  '@phan-var Status $warningsOnlyStatus';
141 
142  if ( $this->messageLocalizer ) {
143  $errorsOnlyStatus->setMessageLocalizer( $this->messageLocalizer );
144  $warningsOnlyStatus->setMessageLocalizer( $this->messageLocalizer );
145  }
146  $errorsOnlyStatus->cleanCallback =
147  $warningsOnlyStatus->cleanCallback = $this->cleanCallback;
148 
149  return [ $errorsOnlyStatus, $warningsOnlyStatus ];
150  }
151 
157  public function getStatusValue() {
158  return $this;
159  }
160 
165  protected function cleanParams( array $params ) {
166  if ( !$this->cleanCallback ) {
167  return $params;
168  }
169  $cleanParams = [];
170  foreach ( $params as $i => $param ) {
171  $cleanParams[$i] = call_user_func( $this->cleanCallback, $param );
172  }
173  return $cleanParams;
174  }
175 
185  public function getWikiText( $shortContext = false, $longContext = false, $lang = null ) {
186  $rawErrors = $this->getErrors();
187  if ( count( $rawErrors ) === 0 ) {
188  if ( $this->isOK() ) {
189  $this->fatal( 'internalerror_info',
190  __METHOD__ . " called for a good result, this is incorrect\n" );
191  } else {
192  $this->fatal( 'internalerror_info',
193  __METHOD__ . ": Invalid result object: no error text but not OK\n" );
194  }
195  $rawErrors = $this->getErrors(); // just added a fatal
196  }
197  if ( count( $rawErrors ) === 1 ) {
198  $s = $this->getErrorMessage( $rawErrors[0], $lang )->plain();
199  if ( $shortContext ) {
200  $s = $this->msgInLang( $shortContext, $lang, $s )->plain();
201  } elseif ( $longContext ) {
202  $s = $this->msgInLang( $longContext, $lang, "* $s\n" )->plain();
203  }
204  } else {
205  $errors = $this->getErrorMessageArray( $rawErrors, $lang );
206  foreach ( $errors as &$error ) {
207  $error = $error->plain();
208  }
209  $s = '* ' . implode( "\n* ", $errors ) . "\n";
210  if ( $longContext ) {
211  $s = $this->msgInLang( $longContext, $lang, $s )->plain();
212  } elseif ( $shortContext ) {
213  $s = $this->msgInLang( $shortContext, $lang, "\n$s\n" )->plain();
214  }
215  }
216  return $s;
217  }
218 
239  public function getMessage( $shortContext = false, $longContext = false, $lang = null ) {
240  $rawErrors = $this->getErrors();
241  if ( count( $rawErrors ) === 0 ) {
242  if ( $this->isOK() ) {
243  $this->fatal( 'internalerror_info',
244  __METHOD__ . " called for a good result, this is incorrect\n" );
245  } else {
246  $this->fatal( 'internalerror_info',
247  __METHOD__ . ": Invalid result object: no error text but not OK\n" );
248  }
249  $rawErrors = $this->getErrors(); // just added a fatal
250  }
251  if ( count( $rawErrors ) === 1 ) {
252  $s = $this->getErrorMessage( $rawErrors[0], $lang );
253  if ( $shortContext ) {
254  $s = $this->msgInLang( $shortContext, $lang, $s );
255  } elseif ( $longContext ) {
256  $wrapper = new RawMessage( "* \$1\n" );
257  $wrapper->params( $s )->parse();
258  $s = $this->msgInLang( $longContext, $lang, $wrapper );
259  }
260  } else {
261  $msgs = $this->getErrorMessageArray( $rawErrors, $lang );
262  $msgCount = count( $msgs );
263 
264  $s = new RawMessage( '* $' . implode( "\n* \$", range( 1, $msgCount ) ) );
265  $s->params( $msgs )->parse();
266 
267  if ( $longContext ) {
268  $s = $this->msgInLang( $longContext, $lang, $s );
269  } elseif ( $shortContext ) {
270  $wrapper = new RawMessage( "\n\$1\n", [ $s ] );
271  $wrapper->parse();
272  $s = $this->msgInLang( $shortContext, $lang, $wrapper );
273  }
274  }
275 
276  return $s;
277  }
278 
289  protected function getErrorMessage( $error, $lang = null ) {
290  if ( is_array( $error ) ) {
291  if ( isset( $error['message'] ) && $error['message'] instanceof Message ) {
292  $msg = $error['message'];
293  } elseif ( isset( $error['message'] ) && isset( $error['params'] ) ) {
294  $msg = $this->msg( $error['message'], array_map( function ( $param ) {
295  return is_string( $param ) ? wfEscapeWikiText( $param ) : $param;
296  }, $this->cleanParams( $error['params'] ) ) );
297  } else {
298  $msgName = array_shift( $error );
299  $msg = $this->msg( $msgName, array_map( function ( $param ) {
300  return is_string( $param ) ? wfEscapeWikiText( $param ) : $param;
301  }, $this->cleanParams( $error ) ) );
302  }
303  } elseif ( is_string( $error ) ) {
304  $msg = $this->msg( $error );
305  } else {
306  throw new UnexpectedValueException( 'Got ' . get_class( $error ) . ' for key.' );
307  }
308 
309  if ( $lang ) {
310  $msg->inLanguage( $lang );
311  }
312  return $msg;
313  }
314 
323  public function getHTML( $shortContext = false, $longContext = false, $lang = null ) {
324  $text = $this->getWikiText( $shortContext, $longContext, $lang );
325  $out = MessageCache::singleton()->parse( $text, null, true, true, $lang );
326  return $out instanceof ParserOutput
327  ? $out->getText( [ 'enableSectionEditLinks' => false ] )
328  : $out;
329  }
330 
337  protected function getErrorMessageArray( $errors, $lang = null ) {
338  return array_map( function ( $e ) use ( $lang ) {
339  return $this->getErrorMessage( $e, $lang );
340  }, $errors );
341  }
342 
350  public function getErrorsArray() {
351  return $this->getStatusArray( 'error' );
352  }
353 
361  public function getWarningsArray() {
362  return $this->getStatusArray( 'warning' );
363  }
364 
373  protected function getStatusArray( $type = false ) {
374  $result = [];
375 
376  foreach ( $this->getErrors() as $error ) {
377  if ( $type === false || $error['type'] === $type ) {
378  if ( $error['message'] instanceof MessageSpecifier ) {
379  $result[] = array_merge(
380  [ $error['message']->getKey() ],
381  $error['message']->getParams()
382  );
383  } elseif ( $error['params'] ) {
384  $result[] = array_merge( [ $error['message'] ], $error['params'] );
385  } else {
386  $result[] = [ $error['message'] ];
387  }
388  }
389  }
390 
391  return $result;
392  }
393 
401  function __sleep() {
402  $keys = array_keys( get_object_vars( $this ) );
403  return array_diff( $keys, [ 'cleanCallback', 'messageLocalizer' ] );
404  }
405 
409  function __wakeup() {
410  $this->cleanCallback = false;
411  $this->messageLocalizer = null;
412  }
413 
419  private function msg( $key, ...$params ) : Message {
420  if ( $this->messageLocalizer ) {
421  return $this->messageLocalizer->msg( $key, ...$params );
422  } else {
423  return wfMessage( $key, ...$params );
424  }
425  }
426 
433  private function msgInLang( $key, $lang, ...$params ) : Message {
434  $msg = $this->msg( $key, ...$params );
435  if ( $lang ) {
436  $msg->inLanguage( $lang );
437  }
438  return $msg;
439  }
440 }
getStatusArray( $type=false)
Returns a list of status messages of the given type (or all if false)
Definition: Status.php:373
wfEscapeWikiText( $text)
Escapes the given text so that it may be output using addWikiText() without any linking, formatting, etc.
__wakeup()
Sanitize the callback parameter on wakeup, to avoid arbitrary execution.
Definition: Status.php:409
fatal( $message,... $parameters)
Add an error and set OK to false, indicating that the operation as a whole was fatal.
array [] $errors
Definition: StatusValue.php:48
The Message class provides methods which fulfil two basic services:
Definition: Message.php:162
if(!isset( $args[0])) $lang
__sleep()
Don&#39;t save the callback when serializing, because Closures can&#39;t be serialized and we&#39;re going to cle...
Definition: Status.php:401
splitByErrorType()
Splits this Status object into two new Status objects, one which contains only the error messages...
Definition: Status.php:136
getWikiText( $shortContext=false, $longContext=false, $lang=null)
Get the error list as a wikitext formatted list.
Definition: Status.php:185
getWarningsArray()
Get the list of warnings (but not errors)
Definition: Status.php:361
callable false $cleanCallback
Definition: Status.php:42
getMessage( $shortContext=false, $longContext=false, $lang=null)
Get a bullet list of the errors as a Message object.
Definition: Status.php:239
cleanParams(array $params)
Definition: Status.php:165
getStatusValue()
Returns the wrapped StatusValue object.
Definition: Status.php:157
msgInLang( $key, $lang,... $params)
Definition: Status.php:433
msg( $key,... $params)
Definition: Status.php:419
getErrorsArray()
Get the list of errors (but not warnings)
Definition: Status.php:350
setOK( $ok)
Change operation status.
__set( $name, $value)
Change operation result Backwards compatibility logic.
Definition: Status.php:100
isOK()
Returns whether the operation completed.
mixed $value
Definition: StatusValue.php:51
getErrorMessageArray( $errors, $lang=null)
Return an array with a Message object for each error.
Definition: Status.php:337
static wrap( $sv)
Succinct helper method to wrap a StatusValue.
Definition: Status.php:58
__get( $name)
Backwards compatibility logic.
Definition: Status.php:81
Variant of the Message class.
Definition: RawMessage.php:34
getErrors()
Get the list of errors.
MessageLocalizer null $messageLocalizer
Definition: Status.php:45
getHTML( $shortContext=false, $longContext=false, $lang=null)
Get the error message as HTML.
Definition: Status.php:323
setMessageLocalizer(MessageLocalizer $messageLocalizer)
Makes this Status object use the given localizer instead of the global one.
Definition: Status.php:121
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
getErrorMessage( $error, $lang=null)
Return the message for a single error.
Definition: Status.php:289
static singleton()
Get the singleton instance of this class.