MediaWiki  1.34.0
Status.php
Go to the documentation of this file.
1 <?php
40 class Status extends StatusValue {
42  public $cleanCallback = false;
43 
55  public static function wrap( $sv ) {
56  if ( $sv instanceof static ) {
57  return $sv;
58  }
59 
60  $result = new static();
61  $result->ok =& $sv->ok;
62  $result->errors =& $sv->errors;
63  $result->value =& $sv->value;
64  $result->successCount =& $sv->successCount;
65  $result->failCount =& $sv->failCount;
66  $result->success =& $sv->success;
67 
68  return $result;
69  }
70 
78  function __get( $name ) {
79  if ( $name === 'ok' ) {
80  return $this->isOK();
81  }
82  if ( $name === 'errors' ) {
83  return $this->getErrors();
84  }
85 
86  throw new RuntimeException( "Cannot get '$name' property." );
87  }
88 
97  function __set( $name, $value ) {
98  if ( $name === 'ok' ) {
99  $this->setOK( $value );
100  } elseif ( !property_exists( $this, $name ) ) {
101  // Caller is using undeclared ad-hoc properties
102  $this->$name = $value;
103  } else {
104  throw new RuntimeException( "Cannot set '$name' property." );
105  }
106  }
107 
120  public function splitByErrorType() {
121  list( $errorsOnlyStatus, $warningsOnlyStatus ) = parent::splitByErrorType();
122  $errorsOnlyStatus->cleanCallback =
123  $warningsOnlyStatus->cleanCallback = $this->cleanCallback;
124 
125  return [ $errorsOnlyStatus, $warningsOnlyStatus ];
126  }
127 
133  public function getStatusValue() {
134  return $this;
135  }
136 
141  protected function cleanParams( array $params ) {
142  if ( !$this->cleanCallback ) {
143  return $params;
144  }
145  $cleanParams = [];
146  foreach ( $params as $i => $param ) {
147  $cleanParams[$i] = call_user_func( $this->cleanCallback, $param );
148  }
149  return $cleanParams;
150  }
151 
157  protected function languageFromParam( $lang ) {
158  if ( $lang === null ) {
159  return RequestContext::getMain()->getLanguage();
160  }
161  if ( $lang instanceof Language || $lang instanceof StubUserLang ) {
162  return $lang;
163  }
164  return Language::factory( $lang );
165  }
166 
176  public function getWikiText( $shortContext = false, $longContext = false, $lang = null ) {
177  $lang = $this->languageFromParam( $lang );
178 
179  $rawErrors = $this->getErrors();
180  if ( count( $rawErrors ) === 0 ) {
181  if ( $this->isOK() ) {
182  $this->fatal( 'internalerror_info',
183  __METHOD__ . " called for a good result, this is incorrect\n" );
184  } else {
185  $this->fatal( 'internalerror_info',
186  __METHOD__ . ": Invalid result object: no error text but not OK\n" );
187  }
188  $rawErrors = $this->getErrors(); // just added a fatal
189  }
190  if ( count( $rawErrors ) === 1 ) {
191  $s = $this->getErrorMessage( $rawErrors[0], $lang )->plain();
192  if ( $shortContext ) {
193  $s = wfMessage( $shortContext, $s )->inLanguage( $lang )->plain();
194  } elseif ( $longContext ) {
195  $s = wfMessage( $longContext, "* $s\n" )->inLanguage( $lang )->plain();
196  }
197  } else {
198  $errors = $this->getErrorMessageArray( $rawErrors, $lang );
199  foreach ( $errors as &$error ) {
200  $error = $error->plain();
201  }
202  $s = '* ' . implode( "\n* ", $errors ) . "\n";
203  if ( $longContext ) {
204  $s = wfMessage( $longContext, $s )->inLanguage( $lang )->plain();
205  } elseif ( $shortContext ) {
206  $s = wfMessage( $shortContext, "\n$s\n" )->inLanguage( $lang )->plain();
207  }
208  }
209  return $s;
210  }
211 
232  public function getMessage( $shortContext = false, $longContext = false, $lang = null ) {
233  $lang = $this->languageFromParam( $lang );
234 
235  $rawErrors = $this->getErrors();
236  if ( count( $rawErrors ) === 0 ) {
237  if ( $this->isOK() ) {
238  $this->fatal( 'internalerror_info',
239  __METHOD__ . " called for a good result, this is incorrect\n" );
240  } else {
241  $this->fatal( 'internalerror_info',
242  __METHOD__ . ": Invalid result object: no error text but not OK\n" );
243  }
244  $rawErrors = $this->getErrors(); // just added a fatal
245  }
246  if ( count( $rawErrors ) === 1 ) {
247  $s = $this->getErrorMessage( $rawErrors[0], $lang );
248  if ( $shortContext ) {
249  $s = wfMessage( $shortContext, $s )->inLanguage( $lang );
250  } elseif ( $longContext ) {
251  $wrapper = new RawMessage( "* \$1\n" );
252  $wrapper->params( $s )->parse();
253  $s = wfMessage( $longContext, $wrapper )->inLanguage( $lang );
254  }
255  } else {
256  $msgs = $this->getErrorMessageArray( $rawErrors, $lang );
257  $msgCount = count( $msgs );
258 
259  $s = new RawMessage( '* $' . implode( "\n* \$", range( 1, $msgCount ) ) );
260  $s->params( $msgs )->parse();
261 
262  if ( $longContext ) {
263  $s = wfMessage( $longContext, $s )->inLanguage( $lang );
264  } elseif ( $shortContext ) {
265  $wrapper = new RawMessage( "\n\$1\n", [ $s ] );
266  $wrapper->parse();
267  $s = wfMessage( $shortContext, $wrapper )->inLanguage( $lang );
268  }
269  }
270 
271  return $s;
272  }
273 
284  protected function getErrorMessage( $error, $lang = null ) {
285  if ( is_array( $error ) ) {
286  if ( isset( $error['message'] ) && $error['message'] instanceof Message ) {
287  $msg = $error['message'];
288  } elseif ( isset( $error['message'] ) && isset( $error['params'] ) ) {
289  $msg = wfMessage( $error['message'],
290  array_map( 'wfEscapeWikiText', $this->cleanParams( $error['params'] ) ) );
291  } else {
292  $msgName = array_shift( $error );
293  $msg = wfMessage( $msgName,
294  array_map( 'wfEscapeWikiText', $this->cleanParams( $error ) ) );
295  }
296  } elseif ( is_string( $error ) ) {
297  $msg = wfMessage( $error );
298  } else {
299  throw new UnexpectedValueException( 'Got ' . get_class( $error ) . ' for key.' );
300  }
301 
302  $msg->inLanguage( $this->languageFromParam( $lang ) );
303  return $msg;
304  }
305 
314  public function getHTML( $shortContext = false, $longContext = false, $lang = null ) {
315  $lang = $this->languageFromParam( $lang );
316  $text = $this->getWikiText( $shortContext, $longContext, $lang );
317  $out = MessageCache::singleton()->parse( $text, null, true, true, $lang );
318  return $out instanceof ParserOutput
319  ? $out->getText( [ 'enableSectionEditLinks' => false ] )
320  : $out;
321  }
322 
329  protected function getErrorMessageArray( $errors, $lang = null ) {
330  $lang = $this->languageFromParam( $lang );
331  return array_map( function ( $e ) use ( $lang ) {
332  return $this->getErrorMessage( $e, $lang );
333  }, $errors );
334  }
335 
343  public function getErrorsArray() {
344  return $this->getStatusArray( 'error' );
345  }
346 
354  public function getWarningsArray() {
355  return $this->getStatusArray( 'warning' );
356  }
357 
366  protected function getStatusArray( $type = false ) {
367  $result = [];
368 
369  foreach ( $this->getErrors() as $error ) {
370  if ( $type === false || $error['type'] === $type ) {
371  if ( $error['message'] instanceof MessageSpecifier ) {
372  $result[] = array_merge(
373  [ $error['message']->getKey() ],
374  $error['message']->getParams()
375  );
376  } elseif ( $error['params'] ) {
377  $result[] = array_merge( [ $error['message'] ], $error['params'] );
378  } else {
379  $result[] = [ $error['message'] ];
380  }
381  }
382  }
383 
384  return $result;
385  }
386 
392  function __sleep() {
393  $keys = array_keys( get_object_vars( $this ) );
394  return array_diff( $keys, [ 'cleanCallback' ] );
395  }
396 
400  function __wakeup() {
401  $this->cleanCallback = false;
402  }
403 }
StatusValue
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Definition: StatusValue.php:42
ParserOutput
Definition: ParserOutput.php:25
$lang
if(!isset( $args[0])) $lang
Definition: testCompression.php:33
Status\languageFromParam
languageFromParam( $lang)
Definition: Status.php:157
Status\getErrorMessage
getErrorMessage( $error, $lang=null)
Return the message for a single error.
Definition: Status.php:284
Status\getStatusValue
getStatusValue()
Returns the wrapped StatusValue object.
Definition: Status.php:133
StatusValue\$errors
array[] $errors
Definition: StatusValue.php:48
Status\getMessage
getMessage( $shortContext=false, $longContext=false, $lang=null)
Get a bullet list of the errors as a Message object.
Definition: Status.php:232
MessageSpecifier
Definition: MessageSpecifier.php:21
StatusValue\fatal
fatal( $message,... $parameters)
Add an error and set OK to false, indicating that the operation as a whole was fatal.
Definition: StatusValue.php:208
wfMessage
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
Definition: GlobalFunctions.php:1264
$s
$s
Definition: mergeMessageFileList.php:185
Status\__get
__get( $name)
Backwards compatibility logic.
Definition: Status.php:78
Message
StatusValue\setOK
setOK( $ok)
Change operation status.
Definition: StatusValue.php:157
Status\__set
__set( $name, $value)
Change operation result Backwards compatibility logic.
Definition: Status.php:97
Status
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Definition: Status.php:40
Status\getWikiText
getWikiText( $shortContext=false, $longContext=false, $lang=null)
Get the error list as a wikitext formatted list.
Definition: Status.php:176
Status\getHTML
getHTML( $shortContext=false, $longContext=false, $lang=null)
Get the error message as HTML.
Definition: Status.php:314
Status\wrap
static wrap( $sv)
Succinct helper method to wrap a StatusValue.
Definition: Status.php:55
StatusValue\isOK
isOK()
Returns whether the operation completed.
Definition: StatusValue.php:130
Status\getErrorMessageArray
getErrorMessageArray( $errors, $lang=null)
Return an array with a Message object for each error.
Definition: Status.php:329
MessageCache\singleton
static singleton()
Get the singleton instance of this class.
Definition: MessageCache.php:114
Status\__wakeup
__wakeup()
Sanitize the callback parameter on wakeup, to avoid arbitrary execution.
Definition: Status.php:400
StatusValue\getErrors
getErrors()
Get the list of errors.
Definition: StatusValue.php:148
StubUserLang
Stub object for the user language.
Definition: StubUserLang.php:24
Status\__sleep
__sleep()
Don't save the callback when serializing, because Closures can't be serialized and we're going to cle...
Definition: Status.php:392
RequestContext\getMain
static getMain()
Get the RequestContext object associated with the main request.
Definition: RequestContext.php:431
Status\$cleanCallback
callable false $cleanCallback
Definition: Status.php:42
StatusValue\$value
mixed $value
Definition: StatusValue.php:51
Status\getWarningsArray
getWarningsArray()
Get the list of warnings (but not errors)
Definition: Status.php:354
Status\getStatusArray
getStatusArray( $type=false)
Returns a list of status messages of the given type (or all if false)
Definition: Status.php:366
Status\cleanParams
cleanParams(array $params)
Definition: Status.php:141
$keys
$keys
Definition: testCompression.php:67
Language\factory
static factory( $code)
Get a cached or new language object for a given language code.
Definition: Language.php:217
ParserOutput\getText
getText( $options=[])
Get the output HTML.
Definition: ParserOutput.php:323
RawMessage
Variant of the Message class.
Definition: RawMessage.php:34
Status\getErrorsArray
getErrorsArray()
Get the list of errors (but not warnings)
Definition: Status.php:343
Language
Internationalisation code.
Definition: Language.php:37
Status\splitByErrorType
splitByErrorType()
Splits this Status object into two new Status objects, one which contains only the error messages,...
Definition: Status.php:120
$type
$type
Definition: testCompression.php:48