MediaWiki REL1_39
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 $result->statusData =& $sv->statusData;
75
76 return $result;
77 }
78
86 public function __get( $name ) {
87 if ( $name === 'ok' ) {
88 return $this->isOK();
89 }
90 if ( $name === 'errors' ) {
91 return $this->getErrors();
92 }
93
94 throw new RuntimeException( "Cannot get '$name' property." );
95 }
96
105 public function __set( $name, $value ) {
106 if ( $name === 'ok' ) {
107 $this->setOK( $value );
108 } elseif ( !property_exists( $this, $name ) ) {
109 // Caller is using undeclared ad-hoc properties
110 $this->$name = $value;
111 } else {
112 throw new RuntimeException( "Cannot set '$name' property." );
113 }
114 }
115
126 public function setMessageLocalizer( MessageLocalizer $messageLocalizer ) {
127 $this->messageLocalizer = $messageLocalizer;
128 }
129
141 public function splitByErrorType() {
142 list( $errorsOnlyStatus, $warningsOnlyStatus ) = parent::splitByErrorType();
143 // phan/phan#2133?
144 '@phan-var Status $errorsOnlyStatus';
145 '@phan-var Status $warningsOnlyStatus';
146
147 if ( $this->messageLocalizer ) {
148 $errorsOnlyStatus->setMessageLocalizer( $this->messageLocalizer );
149 $warningsOnlyStatus->setMessageLocalizer( $this->messageLocalizer );
150 }
151 $errorsOnlyStatus->cleanCallback =
152 $warningsOnlyStatus->cleanCallback = $this->cleanCallback;
153
154 return [ $errorsOnlyStatus, $warningsOnlyStatus ];
155 }
156
162 public function getStatusValue() {
163 return $this;
164 }
165
170 protected function cleanParams( array $params ) {
171 if ( !$this->cleanCallback ) {
172 return $params;
173 }
174 $cleanParams = [];
175 foreach ( $params as $i => $param ) {
176 $cleanParams[$i] = call_user_func( $this->cleanCallback, $param );
177 }
178 return $cleanParams;
179 }
180
190 public function getWikiText( $shortContext = false, $longContext = false, $lang = null ) {
191 $rawErrors = $this->getErrors();
192 if ( count( $rawErrors ) === 0 ) {
193 if ( $this->isOK() ) {
194 $this->fatal( 'internalerror_info',
195 __METHOD__ . " called for a good result, this is incorrect\n" );
196 } else {
197 $this->fatal( 'internalerror_info',
198 __METHOD__ . ": Invalid result object: no error text but not OK\n" );
199 }
200 $rawErrors = $this->getErrors(); // just added a fatal
201 }
202 if ( count( $rawErrors ) === 1 ) {
203 $s = $this->getErrorMessage( $rawErrors[0], $lang )->plain();
204 if ( $shortContext ) {
205 $s = $this->msgInLang( $shortContext, $lang, $s )->plain();
206 } elseif ( $longContext ) {
207 $s = $this->msgInLang( $longContext, $lang, "* $s\n" )->plain();
208 }
209 } else {
210 $errors = $this->getErrorMessageArray( $rawErrors, $lang );
211 foreach ( $errors as &$error ) {
212 $error = $error->plain();
213 }
214 $s = '* ' . implode( "\n* ", $errors ) . "\n";
215 if ( $longContext ) {
216 $s = $this->msgInLang( $longContext, $lang, $s )->plain();
217 } elseif ( $shortContext ) {
218 $s = $this->msgInLang( $shortContext, $lang, "\n$s\n" )->plain();
219 }
220 }
221 return $s;
222 }
223
244 public function getMessage( $shortContext = false, $longContext = false, $lang = null ) {
245 $rawErrors = $this->getErrors();
246 if ( count( $rawErrors ) === 0 ) {
247 if ( $this->isOK() ) {
248 $this->fatal( 'internalerror_info',
249 __METHOD__ . " called for a good result, this is incorrect\n" );
250 } else {
251 $this->fatal( 'internalerror_info',
252 __METHOD__ . ": Invalid result object: no error text but not OK\n" );
253 }
254 $rawErrors = $this->getErrors(); // just added a fatal
255 }
256 if ( count( $rawErrors ) === 1 ) {
257 $s = $this->getErrorMessage( $rawErrors[0], $lang );
258 if ( $shortContext ) {
259 $s = $this->msgInLang( $shortContext, $lang, $s );
260 } elseif ( $longContext ) {
261 $wrapper = new RawMessage( "* \$1\n" );
262 $wrapper->params( $s )->parse();
263 $s = $this->msgInLang( $longContext, $lang, $wrapper );
264 }
265 } else {
266 $msgs = $this->getErrorMessageArray( $rawErrors, $lang );
267 $msgCount = count( $msgs );
268
269 $s = new RawMessage( '* $' . implode( "\n* \$", range( 1, $msgCount ) ) );
270 $s->params( $msgs )->parse();
271
272 if ( $longContext ) {
273 $s = $this->msgInLang( $longContext, $lang, $s );
274 } elseif ( $shortContext ) {
275 $wrapper = new RawMessage( "\n\$1\n", [ $s ] );
276 $wrapper->parse();
277 $s = $this->msgInLang( $shortContext, $lang, $wrapper );
278 }
279 }
280
281 return $s;
282 }
283
294 protected function getErrorMessage( $error, $lang = null ) {
295 if ( is_array( $error ) ) {
296 if ( isset( $error['message'] ) && $error['message'] instanceof Message ) {
297 // Apply context from MessageLocalizer even if we have a Message object already
298 $msg = $this->msg( $error['message'] );
299 } elseif ( isset( $error['message'] ) && isset( $error['params'] ) ) {
300 $msg = $this->msg( $error['message'], array_map( static function ( $param ) {
301 return is_string( $param ) ? wfEscapeWikiText( $param ) : $param;
302 }, $this->cleanParams( $error['params'] ) ) );
303 } else {
304 $msgName = array_shift( $error );
305 $msg = $this->msg( $msgName, array_map( static function ( $param ) {
306 return is_string( $param ) ? wfEscapeWikiText( $param ) : $param;
307 }, $this->cleanParams( $error ) ) );
308 }
309 } elseif ( is_string( $error ) ) {
310 $msg = $this->msg( $error );
311 } else {
312 throw new UnexpectedValueException( 'Got ' . get_class( $error ) . ' for key.' );
313 }
314
315 if ( $lang ) {
316 $msg->inLanguage( $lang );
317 }
318 return $msg;
319 }
320
329 public function getHTML( $shortContext = false, $longContext = false, $lang = null ) {
330 $text = $this->getWikiText( $shortContext, $longContext, $lang );
331 $out = MediaWikiServices::getInstance()->getMessageCache()
332 ->parse( $text, null, true, true, $lang );
333 return $out instanceof ParserOutput
334 ? $out->getText( [ 'enableSectionEditLinks' => false ] )
335 : $out;
336 }
337
344 protected function getErrorMessageArray( $errors, $lang = null ) {
345 return array_map( function ( $e ) use ( $lang ) {
346 return $this->getErrorMessage( $e, $lang );
347 }, $errors );
348 }
349
358 public function getErrorsArray() {
359 return $this->getStatusArray( 'error' );
360 }
361
370 public function getWarningsArray() {
371 return $this->getStatusArray( 'warning' );
372 }
373
381 public function __sleep() {
382 $keys = array_keys( get_object_vars( $this ) );
383 return array_diff( $keys, [ 'cleanCallback', 'messageLocalizer' ] );
384 }
385
389 public function __wakeup() {
390 $this->cleanCallback = false;
391 $this->messageLocalizer = null;
392 }
393
399 private function msg( $key, ...$params ): Message {
400 if ( $this->messageLocalizer ) {
401 return $this->messageLocalizer->msg( $key, ...$params );
402 } else {
403 return wfMessage( $key, ...$params );
404 }
405 }
406
413 private function msgInLang( $key, $lang, ...$params ): Message {
414 $msg = $this->msg( $key, ...$params );
415 if ( $lang ) {
416 $msg->inLanguage( $lang );
417 }
418 return $msg;
419 }
420}
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:82
Service locator for MediaWiki core services.
The Message class deals with fetching and processing of interface message into a variety of formats.
Definition Message.php:140
inLanguage( $lang)
Request the message in any language that is supported.
Definition Message.php:832
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.
getStatusArray( $type=false)
Returns a list of status messages of the given type (or all if false)
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:126
getWikiText( $shortContext=false, $longContext=false, $lang=null)
Get the error list as a wikitext formatted list.
Definition Status.php:190
splitByErrorType()
Splits this Status object into two new Status objects, one which contains only the error messages,...
Definition Status.php:141
getErrorsArray()
Get the list of errors (but not warnings)
Definition Status.php:358
getErrorMessage( $error, $lang=null)
Return the message for a single error.
Definition Status.php:294
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:381
cleanParams(array $params)
Definition Status.php:170
__set( $name, $value)
Change operation result Backwards compatibility logic.
Definition Status.php:105
getErrorMessageArray( $errors, $lang=null)
Return an array with a Message object for each error.
Definition Status.php:344
getWarningsArray()
Get the list of warnings (but not errors)
Definition Status.php:370
getStatusValue()
Returns the wrapped StatusValue object.
Definition Status.php:162
getHTML( $shortContext=false, $longContext=false, $lang=null)
Get the error message as HTML.
Definition Status.php:329
__wakeup()
Sanitize the callback parameter on wakeup, to avoid arbitrary execution.
Definition Status.php:389
__get( $name)
Backwards compatibility logic.
Definition Status.php:86
getMessage( $shortContext=false, $longContext=false, $lang=null)
Get a bullet list of the errors as a Message object.
Definition Status.php:244
Interface for localizing messages in MediaWiki.
foreach( $mmfl['setupFiles'] as $fileName) if($queue) if(empty( $mmfl['quiet'])) $s
if(!isset( $args[0])) $lang