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
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 // Apply context from MessageLocalizer even if we have a Message object already
297 $msg = $this->msg( $error['message'] );
298 } elseif ( isset( $error['message'] ) && isset( $error['params'] ) ) {
299 $msg = $this->msg( $error['message'], array_map( static function ( $param ) {
300 return is_string( $param ) ? wfEscapeWikiText( $param ) : $param;
301 }, $this->cleanParams( $error['params'] ) ) );
302 } else {
303 $msgName = array_shift( $error );
304 $msg = $this->msg( $msgName, array_map( static function ( $param ) {
305 return is_string( $param ) ? wfEscapeWikiText( $param ) : $param;
306 }, $this->cleanParams( $error ) ) );
307 }
308 } elseif ( is_string( $error ) ) {
309 $msg = $this->msg( $error );
310 } else {
311 throw new UnexpectedValueException( 'Got ' . get_class( $error ) . ' for key.' );
312 }
313
314 if ( $lang ) {
315 $msg->inLanguage( $lang );
316 }
317 return $msg;
318 }
319
328 public function getHTML( $shortContext = false, $longContext = false, $lang = null ) {
329 $text = $this->getWikiText( $shortContext, $longContext, $lang );
330 $out = MediaWikiServices::getInstance()->getMessageCache()
331 ->parse( $text, null, true, true, $lang );
332 return $out instanceof ParserOutput
333 ? $out->getText( [ 'enableSectionEditLinks' => false ] )
334 : $out;
335 }
336
343 protected function getErrorMessageArray( $errors, $lang = null ) {
344 return array_map( function ( $e ) use ( $lang ) {
345 return $this->getErrorMessage( $e, $lang );
346 }, $errors );
347 }
348
357 public function getErrorsArray() {
358 return $this->getStatusArray( 'error' );
359 }
360
369 public function getWarningsArray() {
370 return $this->getStatusArray( 'warning' );
371 }
372
380 public function __sleep() {
381 $keys = array_keys( get_object_vars( $this ) );
382 return array_diff( $keys, [ 'cleanCallback', 'messageLocalizer' ] );
383 }
384
388 public function __wakeup() {
389 $this->cleanCallback = false;
390 $this->messageLocalizer = null;
391 }
392
398 private function msg( $key, ...$params ): Message {
399 if ( $this->messageLocalizer ) {
400 return $this->messageLocalizer->msg( $key, ...$params );
401 } else {
402 return wfMessage( $key, ...$params );
403 }
404 }
405
412 private function msgInLang( $key, $lang, ...$params ): Message {
413 $msg = $this->msg( $key, ...$params );
414 if ( $lang ) {
415 $msg->inLanguage( $lang );
416 }
417 return $msg;
418 }
419}
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: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
getErrorsArray()
Get the list of errors (but not warnings)
Definition Status.php:357
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:380
cleanParams(array $params)
Definition Status.php:169
__set( $name, $value)
Change operation result Backwards compatibility logic.
Definition Status.php:104
getErrorMessageArray( $errors, $lang=null)
Return an array with a Message object for each error.
Definition Status.php:343
getWarningsArray()
Get the list of warnings (but not errors)
Definition Status.php:369
getStatusValue()
Returns the wrapped StatusValue object.
Definition Status.php:161
getHTML( $shortContext=false, $longContext=false, $lang=null)
Get the error message as HTML.
Definition Status.php:328
__wakeup()
Sanitize the callback parameter on wakeup, to avoid arbitrary execution.
Definition Status.php:388
__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.
foreach( $mmfl['setupFiles'] as $fileName) if($queue) if(empty( $mmfl['quiet'])) $s
if(!isset( $args[0])) $lang