Debian Codegraph @

includes/Status/Status.php

raw symbols SCIP
1 <?php
2 /**
3 * Generic operation result.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 */
22
23 namespace MediaWiki\Status;
24
25 use MediaWiki\Context\IContextSource;
26 use MediaWiki\Context\RequestContext;
27 use MediaWiki\Language\Language;
28 use MediaWiki\MediaWikiServices;
29 use MediaWiki\Message\Message;
30 use MediaWiki\StubObject\StubUserLang;
31 use MessageLocalizer;
32 use RuntimeException;
33 use StatusValue;
34
35 /**
36 * Generic operation result class
37 * Has warning/error list, boolean status and arbitrary value
38 *
39 * "Good" means the operation was completed with no warnings or errors.
40 *
41 * "OK" means the operation was partially or wholly completed.
42 *
43 * An operation which is not OK should have errors so that the user can be
44 * informed as to what went wrong. Calling the fatal() function sets an error
45 * message and simultaneously switches off the OK flag.
46 *
47 * The recommended pattern for Status objects is to return a Status object
48 * unconditionally, i.e. both on success and on failure -- so that the
49 * developer of the calling code is reminded that the function can fail, and
50 * so that a lack of error-handling will be explicit.
51 *
52 * @newable
53 */
54 class Status extends StatusValue {
55 /** @var callable|false */
56 public $cleanCallback = false;
57
58 /** @var MessageLocalizer|null */
59 protected $messageLocalizer;
60
61 private ?StatusFormatter $formatter = null;
62
63 /**
64 * Succinct helper method to wrap a StatusValue
65 *
66 * This is useful when formatting StatusValue objects:
67 * @code
68 * $this->getOutput()->addHtml( Status::wrap( $sv )->getHTML() );
69 * @endcode
70 *
71 * @param StatusValue|Status $sv
72 * @return Status
73 */
74 public static function wrap( $sv ) {
75 if ( $sv instanceof static ) {
76 return $sv;
77 }
78
79 $result = new static();
80 $result->ok =& $sv->ok;
81 $result->errors =& $sv->errors;
82 $result->value =& $sv->value;
83 $result->successCount =& $sv->successCount;
84 $result->failCount =& $sv->failCount;
85 $result->success =& $sv->success;
86 $result->statusData =& $sv->statusData;
87
88 return $result;
89 }
90
91 /**
92 * Backwards compatibility logic
93 *
94 * @param string $name
95 * @return mixed
96 * @throws RuntimeException
97 */
98 public function __get( $name ) {
99 if ( $name === 'ok' ) {
100 return $this->isOK();
101 }
102 if ( $name === 'errors' ) {
103 return $this->getErrors();
104 }
105
106 throw new RuntimeException( "Cannot get '$name' property." );
107 }
108
109 /**
110 * Change operation result
111 * Backwards compatibility logic
112 *
113 * @param string $name
114 * @param mixed $value
115 * @throws RuntimeException
116 */
117 public function __set( $name, $value ) {
118 if ( $name === 'ok' ) {
119 $this->setOK( $value );
120 } else {
121 throw new RuntimeException( "Cannot set '$name' property." );
122 }
123 }
124
125 /**
126 * Makes this Status object use the given localizer instead of the global one.
127 * If it is an IContextSource or a ResourceLoader Context, it will also be used to
128 * determine the interface language.
129 * @note This setting does not survive serialization. That's usually for the best
130 * (there's no guarantee we'll still have the same localization settings after
131 * unserialization); it is the caller's responsibility to set the localizer again
132 * if needed.
133 * @param MessageLocalizer $messageLocalizer
134 *
135 * @deprecated since 1.42, use FormatterFactory::getStatusFormatter instead.
136 */
137 public function setMessageLocalizer( MessageLocalizer $messageLocalizer ) {
138 // TODO: hard deprecate after switching callers to StatusFormatter
139 $this->messageLocalizer = $messageLocalizer;
140 $this->formatter = null;
141 }
142
143 private function getFormatter(): StatusFormatter {
144 if ( !$this->formatter ) {
145 $context = RequestContext::getMain();
146
147 // HACK: only works for IContextSource objects.
148 if ( $this->messageLocalizer && $this->messageLocalizer instanceof IContextSource ) {
149 $context = $this->messageLocalizer;
150 }
151
152 $formatterFactory = MediaWikiServices::getInstance()->getFormatterFactory();
153 $this->formatter = $formatterFactory->getStatusFormatter( $context );
154 }
155
156 return $this->formatter;
157 }
158
159 /**
160 * Splits this Status object into two new Status objects, one which contains only
161 * the error messages, and one that contains the warnings, only. The returned array is
162 * defined as:
163 * [
164 * 0 => object(Status) # The Status with error messages, only
165 * 1 => object(Status) # The Status with warning messages, only
166 * ]
167 *
168 * @return Status[]
169 */
170 public function splitByErrorType() {
171 [ $errorsOnlyStatus, $warningsOnlyStatus ] = parent::splitByErrorType();
172 // phan/phan#2133?
173 '@phan-var Status $errorsOnlyStatus';
174 '@phan-var Status $warningsOnlyStatus';
175
176 if ( $this->messageLocalizer ) {
177 $errorsOnlyStatus->setMessageLocalizer = $this->messageLocalizer;
178 $warningsOnlyStatus->setMessageLocalizer = $this->messageLocalizer;
179 }
180
181 if ( $this->formatter ) {
182 $errorsOnlyStatus->formatter = $this->formatter;
183 $warningsOnlyStatus->formatter = $this->formatter;
184 }
185
186 $errorsOnlyStatus->cleanCallback = $warningsOnlyStatus->cleanCallback = $this->cleanCallback;
187
188 return [ $errorsOnlyStatus, $warningsOnlyStatus ];
189 }
190
191 /**
192 * Returns the wrapped StatusValue object
193 * @return StatusValue
194 * @since 1.27
195 */
196 public function getStatusValue() {
197 return $this;
198 }
199
200 /**
201 * Get the error list as a wikitext formatted list
202 *
203 * All message parameters that were provided as strings will be escaped with wfEscapeWikiText.
204 * This is mostly a historical accident and often undesirable (T368821).
205 * - To avoid this behavior when producing the Status, pass MessageSpecifier objects to methods
206 * such as `$status->fatal()`, instead of separate key and params parameters.
207 * - To avoid this behavior when consuming the Status, use the `$status->getMessages()` method
208 * instead, and display each message separately (or combine then with `Message::listParams()`).
209 *
210 * @deprecated since 1.42, use StatusFormatter instead.
211 *
212 * @param string|false $shortContext A short enclosing context message name, to
213 * be used when there is a single error
214 * @param string|false $longContext A long enclosing context message name, for a list
215 * @param string|Language|StubUserLang|null $lang Language to use for processing messages
216 * @return string
217 */
218 public function getWikiText( $shortContext = false, $longContext = false, $lang = null ) {
219 return $this->getFormatter()->getWikiText( $this, [
220 'shortContext' => $shortContext,
221 'longContext' => $longContext,
222 'lang' => $lang,
223 'cleanCallback' => $this->cleanCallback
224 ] );
225 }
226
227 /**
228 * Get a bullet list of the errors as a Message object.
229 *
230 * All message parameters that were provided as strings will be escaped with wfEscapeWikiText.
231 * This is mostly a historical accident and often undesirable (T368821).
232 * - To avoid this behavior when producing the Status, pass MessageSpecifier objects to methods
233 * such as `$status->fatal()`, instead of separate key and params parameters.
234 * - To avoid this behavior when consuming the Status, use the `$status->getMessages()` method
235 * instead, and display each message separately (or combine then with `Message::listParams()`).
236 *
237 * $shortContext and $longContext can be used to wrap the error list in some text.
238 * $shortContext will be preferred when there is a single error; $longContext will be
239 * preferred when there are multiple ones. In either case, $1 will be replaced with
240 * the list of errors.
241 *
242 * $shortContext is assumed to use $1 as an inline parameter: if there is a single item,
243 * it will not be made into a list; if there are multiple items, newlines will be inserted
244 * around the list.
245 * $longContext is assumed to use $1 as a standalone parameter; it will always receive a list.
246 *
247 * If both parameters are missing, and there is only one error, no bullet will be added.
248 *
249 * @deprecated since 1.42, use StatusFormatter instead.
250 *
251 * @param string|string[]|false $shortContext A message name or an array of message names.
252 * @param string|string[]|false $longContext A message name or an array of message names.
253 * @param string|Language|StubUserLang|null $lang Language to use for processing messages
254 * @return Message
255 */
256 public function getMessage( $shortContext = false, $longContext = false, $lang = null ) {
257 return $this->getFormatter()->getMessage( $this, [
258 'shortContext' => $shortContext,
259 'longContext' => $longContext,
260 'lang' => $lang,
261 'cleanCallback' => $this->cleanCallback
262 ] );
263 }
264
265 /**
266 * Try to convert the status to a PSR-3 friendly format. The output will be similar to
267 * getWikiText( false, false, 'en' ), but message parameters will be extracted into the
268 * context array with parameter names 'parameter1' etc. when possible.
269 *
270 * @deprecated since 1.42, use StatusFormatter instead.
271 *
272 * @return array A pair of (message, context) suitable for passing to a PSR-3 logger.
273 * @phan-return array{0:string,1:(int|float|string)[]}
274 */
275 public function getPsr3MessageAndContext(): array {
276 return $this->getFormatter()->getPsr3MessageAndContext( $this );
277 }
278
279 /**
280 * Get the error message as HTML. This is done by parsing the wikitext error message
281 *
282 * All message parameters that were provided as strings will be escaped with wfEscapeWikiText.
283 * This is mostly a historical accident and often undesirable (T368821).
284 * - To avoid this behavior when producing the Status, pass MessageSpecifier objects to methods
285 * such as `$status->fatal()`, instead of separate key and params parameters.
286 * - To avoid this behavior when consuming the Status, use the `$status->getMessages()` method
287 * instead, and display each message separately (or combine then with `Message::listParams()`).
288 *
289 * @deprecated since 1.42, use StatusFormatter instead.
290 *
291 * @param string|false $shortContext A short enclosing context message name, to
292 * be used when there is a single error
293 * @param string|false $longContext A long enclosing context message name, for a list
294 * @param string|Language|StubUserLang|null $lang Language to use for processing messages
295 * @return string
296 */
297 public function getHTML( $shortContext = false, $longContext = false, $lang = null ) {
298 return $this->getFormatter()->getHTML( $this, [
299 'shortContext' => $shortContext,
300 'longContext' => $longContext,
301 'lang' => $lang,
302 'cleanCallback' => $this->cleanCallback
303 ] );
304 }
305
306 /**
307 * Get the list of errors (but not warnings)
308 *
309 * @deprecated since 1.43 Use `->getMessages( 'error' )` instead
310 * @return array[] A list in which each entry is an array with a message key as its first element.
311 * The remaining array elements are the message parameters.
312 * @phan-return non-empty-array[]
313 */
314 public function getErrorsArray() {
315 return $this->getStatusArray( 'error' );
316 }
317
318 /**
319 * Get the list of warnings (but not errors)
320 *
321 * @deprecated since 1.43 Use `->getMessages( 'warning' )` instead
322 * @return array[] A list in which each entry is an array with a message key as its first element.
323 * The remaining array elements are the message parameters.
324 * @phan-return non-empty-array[]
325 */
326 public function getWarningsArray() {
327 return $this->getStatusArray( 'warning' );
328 }
329
330 /**
331 * Don't save the callback when serializing, because Closures can't be
332 * serialized and we're going to clear it in __wakeup anyway.
333 * Don't save the localizer, because it can be pretty much anything. Restoring it is
334 * the caller's responsibility (otherwise it will just fall back to the global request context).
335 * Same for the formatter, which is likely to contain a localizer.
336 * @return array
337 */
338 public function __sleep() {
339 $keys = array_keys( get_object_vars( $this ) );
340 return array_diff( $keys, [ 'cleanCallback', 'messageLocalizer', 'formatter' ] );
341 }
342
343 /**
344 * Sanitize the callback parameter on wakeup, to avoid arbitrary execution.
345 */
346 public function __wakeup() {
347 $this->cleanCallback = false;
348 $this->messageLocalizer = null;
349 $this->formatter = null;
350 }
351
352 }
353
354 /** @deprecated class alias since 1.41 */
355 class_alias( Status::class, 'Status' );