MediaWiki REL1_34
Status.php
Go to the documentation of this file.
1<?php
40class 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}
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
Internationalisation code.
Definition Language.php:37
The Message class provides methods which fulfil two basic services:
Definition Message.php:162
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:40
static wrap( $sv)
Succinct helper method to wrap a StatusValue.
Definition Status.php:55
getWikiText( $shortContext=false, $longContext=false, $lang=null)
Get the error list as a wikitext formatted list.
Definition Status.php:176
splitByErrorType()
Splits this Status object into two new Status objects, one which contains only the error messages,...
Definition Status.php:120
getErrorsArray()
Get the list of errors (but not warnings)
Definition Status.php:343
getErrorMessage( $error, $lang=null)
Return the message for a single error.
Definition Status.php:284
callable false $cleanCallback
Definition Status.php:42
__sleep()
Don't save the callback when serializing, because Closures can't be serialized and we're going to cle...
Definition Status.php:392
cleanParams(array $params)
Definition Status.php:141
__set( $name, $value)
Change operation result Backwards compatibility logic.
Definition Status.php:97
getErrorMessageArray( $errors, $lang=null)
Return an array with a Message object for each error.
Definition Status.php:329
getWarningsArray()
Get the list of warnings (but not errors)
Definition Status.php:354
getStatusValue()
Returns the wrapped StatusValue object.
Definition Status.php:133
languageFromParam( $lang)
Definition Status.php:157
getStatusArray( $type=false)
Returns a list of status messages of the given type (or all if false)
Definition Status.php:366
getHTML( $shortContext=false, $longContext=false, $lang=null)
Get the error message as HTML.
Definition Status.php:314
__wakeup()
Sanitize the callback parameter on wakeup, to avoid arbitrary execution.
Definition Status.php:400
__get( $name)
Backwards compatibility logic.
Definition Status.php:78
getMessage( $shortContext=false, $longContext=false, $lang=null)
Get a bullet list of the errors as a Message object.
Definition Status.php:232
Stub object for the user language.
if(!isset( $args[0])) $lang