MediaWiki REL1_35
Status.php
Go to the documentation of this file.
1<?php
24
44class Status extends StatusValue {
46 public $cleanCallback = false;
47
50
62 public static function wrap( $sv ) {
63 if ( $sv instanceof static ) {
64 return $sv;
65 }
66
67 $result = new static();
68 $result->ok =& $sv->ok;
69 $result->errors =& $sv->errors;
70 $result->value =& $sv->value;
71 $result->successCount =& $sv->successCount;
72 $result->failCount =& $sv->failCount;
73 $result->success =& $sv->success;
74
75 return $result;
76 }
77
85 public function __get( $name ) {
86 if ( $name === 'ok' ) {
87 return $this->isOK();
88 }
89 if ( $name === 'errors' ) {
90 return $this->getErrors();
91 }
92
93 throw new RuntimeException( "Cannot get '$name' property." );
94 }
95
104 public function __set( $name, $value ) {
105 if ( $name === 'ok' ) {
106 $this->setOK( $value );
107 } elseif ( !property_exists( $this, $name ) ) {
108 // Caller is using undeclared ad-hoc properties
109 $this->$name = $value;
110 } else {
111 throw new RuntimeException( "Cannot set '$name' property." );
112 }
113 }
114
125 public function setMessageLocalizer( MessageLocalizer $messageLocalizer ) {
126 $this->messageLocalizer = $messageLocalizer;
127 }
128
140 public function splitByErrorType() {
141 list( $errorsOnlyStatus, $warningsOnlyStatus ) = parent::splitByErrorType();
142 // phan/phan#2133?
143 '@phan-var Status $errorsOnlyStatus';
144 '@phan-var Status $warningsOnlyStatus';
145
146 if ( $this->messageLocalizer ) {
147 $errorsOnlyStatus->setMessageLocalizer( $this->messageLocalizer );
148 $warningsOnlyStatus->setMessageLocalizer( $this->messageLocalizer );
149 }
150 $errorsOnlyStatus->cleanCallback =
151 $warningsOnlyStatus->cleanCallback = $this->cleanCallback;
152
153 return [ $errorsOnlyStatus, $warningsOnlyStatus ];
154 }
155
161 public function getStatusValue() {
162 return $this;
163 }
164
169 protected function cleanParams( array $params ) {
170 if ( !$this->cleanCallback ) {
171 return $params;
172 }
173 $cleanParams = [];
174 foreach ( $params as $i => $param ) {
175 $cleanParams[$i] = call_user_func( $this->cleanCallback, $param );
176 }
177 return $cleanParams;
178 }
179
189 public function getWikiText( $shortContext = false, $longContext = false, $lang = null ) {
190 $rawErrors = $this->getErrors();
191 if ( count( $rawErrors ) === 0 ) {
192 if ( $this->isOK() ) {
193 $this->fatal( 'internalerror_info',
194 __METHOD__ . " called for a good result, this is incorrect\n" );
195 } else {
196 $this->fatal( 'internalerror_info',
197 __METHOD__ . ": Invalid result object: no error text but not OK\n" );
198 }
199 $rawErrors = $this->getErrors(); // just added a fatal
200 }
201 if ( count( $rawErrors ) === 1 ) {
202 $s = $this->getErrorMessage( $rawErrors[0], $lang )->plain();
203 if ( $shortContext ) {
204 $s = $this->msgInLang( $shortContext, $lang, $s )->plain();
205 } elseif ( $longContext ) {
206 $s = $this->msgInLang( $longContext, $lang, "* $s\n" )->plain();
207 }
208 } else {
209 $errors = $this->getErrorMessageArray( $rawErrors, $lang );
210 foreach ( $errors as &$error ) {
211 $error = $error->plain();
212 }
213 $s = '* ' . implode( "\n* ", $errors ) . "\n";
214 if ( $longContext ) {
215 $s = $this->msgInLang( $longContext, $lang, $s )->plain();
216 } elseif ( $shortContext ) {
217 $s = $this->msgInLang( $shortContext, $lang, "\n$s\n" )->plain();
218 }
219 }
220 return $s;
221 }
222
243 public function getMessage( $shortContext = false, $longContext = false, $lang = null ) {
244 $rawErrors = $this->getErrors();
245 if ( count( $rawErrors ) === 0 ) {
246 if ( $this->isOK() ) {
247 $this->fatal( 'internalerror_info',
248 __METHOD__ . " called for a good result, this is incorrect\n" );
249 } else {
250 $this->fatal( 'internalerror_info',
251 __METHOD__ . ": Invalid result object: no error text but not OK\n" );
252 }
253 $rawErrors = $this->getErrors(); // just added a fatal
254 }
255 if ( count( $rawErrors ) === 1 ) {
256 $s = $this->getErrorMessage( $rawErrors[0], $lang );
257 if ( $shortContext ) {
258 $s = $this->msgInLang( $shortContext, $lang, $s );
259 } elseif ( $longContext ) {
260 $wrapper = new RawMessage( "* \$1\n" );
261 $wrapper->params( $s )->parse();
262 $s = $this->msgInLang( $longContext, $lang, $wrapper );
263 }
264 } else {
265 $msgs = $this->getErrorMessageArray( $rawErrors, $lang );
266 $msgCount = count( $msgs );
267
268 $s = new RawMessage( '* $' . implode( "\n* \$", range( 1, $msgCount ) ) );
269 $s->params( $msgs )->parse();
270
271 if ( $longContext ) {
272 $s = $this->msgInLang( $longContext, $lang, $s );
273 } elseif ( $shortContext ) {
274 $wrapper = new RawMessage( "\n\$1\n", [ $s ] );
275 $wrapper->parse();
276 $s = $this->msgInLang( $shortContext, $lang, $wrapper );
277 }
278 }
279
280 return $s;
281 }
282
293 protected function getErrorMessage( $error, $lang = null ) {
294 if ( is_array( $error ) ) {
295 if ( isset( $error['message'] ) && $error['message'] instanceof Message ) {
296 $msg = $error['message'];
297 } elseif ( isset( $error['message'] ) && isset( $error['params'] ) ) {
298 $msg = $this->msg( $error['message'], array_map( function ( $param ) {
299 return is_string( $param ) ? wfEscapeWikiText( $param ) : $param;
300 }, $this->cleanParams( $error['params'] ) ) );
301 } else {
302 $msgName = array_shift( $error );
303 $msg = $this->msg( $msgName, array_map( function ( $param ) {
304 return is_string( $param ) ? wfEscapeWikiText( $param ) : $param;
305 }, $this->cleanParams( $error ) ) );
306 }
307 } elseif ( is_string( $error ) ) {
308 $msg = $this->msg( $error );
309 } else {
310 throw new UnexpectedValueException( 'Got ' . get_class( $error ) . ' for key.' );
311 }
312
313 if ( $lang ) {
314 $msg->inLanguage( $lang );
315 }
316 return $msg;
317 }
318
327 public function getHTML( $shortContext = false, $longContext = false, $lang = null ) {
328 $text = $this->getWikiText( $shortContext, $longContext, $lang );
329 $out = MediaWikiServices::getInstance()->getMessageCache()
330 ->parse( $text, null, true, true, $lang );
331 return $out instanceof ParserOutput
332 ? $out->getText( [ 'enableSectionEditLinks' => false ] )
333 : $out;
334 }
335
342 protected function getErrorMessageArray( $errors, $lang = null ) {
343 return array_map( function ( $e ) use ( $lang ) {
344 return $this->getErrorMessage( $e, $lang );
345 }, $errors );
346 }
347
355 public function getErrorsArray() {
356 return $this->getStatusArray( 'error' );
357 }
358
366 public function getWarningsArray() {
367 return $this->getStatusArray( 'warning' );
368 }
369
378 protected function getStatusArray( $type = false ) {
379 $result = [];
380
381 foreach ( $this->getErrors() as $error ) {
382 if ( $type === false || $error['type'] === $type ) {
383 if ( $error['message'] instanceof MessageSpecifier ) {
384 $result[] = array_merge(
385 [ $error['message']->getKey() ],
386 $error['message']->getParams()
387 );
388 } elseif ( $error['params'] ) {
389 $result[] = array_merge( [ $error['message'] ], $error['params'] );
390 } else {
391 $result[] = [ $error['message'] ];
392 }
393 }
394 }
395
396 return $result;
397 }
398
406 public function __sleep() {
407 $keys = array_keys( get_object_vars( $this ) );
408 return array_diff( $keys, [ 'cleanCallback', 'messageLocalizer' ] );
409 }
410
414 public function __wakeup() {
415 $this->cleanCallback = false;
416 $this->messageLocalizer = null;
417 }
418
424 private function msg( $key, ...$params ) : Message {
425 if ( $this->messageLocalizer ) {
426 return $this->messageLocalizer->msg( $key, ...$params );
427 } else {
428 return wfMessage( $key, ...$params );
429 }
430 }
431
438 private function msgInLang( $key, $lang, ...$params ) : Message {
439 $msg = $this->msg( $key, ...$params );
440 if ( $lang ) {
441 $msg->inLanguage( $lang );
442 }
443 return $msg;
444 }
445}
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(ini_get('mbstring.func_overload')) if(!defined('MW_ENTRY_POINT'))
Pre-config setup: Before loading LocalSettings.php.
Definition Setup.php:85
MediaWikiServices is the service locator for the application scope of MediaWiki.
The Message class deals with fetching and processing of interface message into a variety of formats.
Definition Message.php:161
inLanguage( $lang)
Request the message in any language that is supported.
Definition Message.php:757
getText( $options=[])
Get the output HTML.
Variant of the Message class.
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:44
static wrap( $sv)
Succinct helper method to wrap a StatusValue.
Definition Status.php:62
setMessageLocalizer(MessageLocalizer $messageLocalizer)
Makes this Status object use the given localizer instead of the global one.
Definition Status.php:125
getWikiText( $shortContext=false, $longContext=false, $lang=null)
Get the error list as a wikitext formatted list.
Definition Status.php:189
splitByErrorType()
Splits this Status object into two new Status objects, one which contains only the error messages,...
Definition Status.php:140
msg( $key,... $params)
Definition Status.php:424
getErrorsArray()
Get the list of errors (but not warnings)
Definition Status.php:355
getErrorMessage( $error, $lang=null)
Return the message for a single error.
Definition Status.php:293
callable false $cleanCallback
Definition Status.php:46
MessageLocalizer null $messageLocalizer
Definition Status.php:49
__sleep()
Don't save the callback when serializing, because Closures can't be serialized and we're going to cle...
Definition Status.php:406
cleanParams(array $params)
Definition Status.php:169
__set( $name, $value)
Change operation result Backwards compatibility logic.
Definition Status.php:104
msgInLang( $key, $lang,... $params)
Definition Status.php:438
getErrorMessageArray( $errors, $lang=null)
Return an array with a Message object for each error.
Definition Status.php:342
getWarningsArray()
Get the list of warnings (but not errors)
Definition Status.php:366
getStatusValue()
Returns the wrapped StatusValue object.
Definition Status.php:161
getStatusArray( $type=false)
Returns a list of status messages of the given type (or all if false)
Definition Status.php:378
getHTML( $shortContext=false, $longContext=false, $lang=null)
Get the error message as HTML.
Definition Status.php:327
__wakeup()
Sanitize the callback parameter on wakeup, to avoid arbitrary execution.
Definition Status.php:414
__get( $name)
Backwards compatibility logic.
Definition Status.php:85
getMessage( $shortContext=false, $longContext=false, $lang=null)
Get a bullet list of the errors as a Message object.
Definition Status.php:243
Interface for localizing messages in MediaWiki.
Stable for implementing.
if(!isset( $args[0])) $lang