(bug 19441) Updated date formatting for Lithuanian
[mediawiki.git] / includes / Status.php
blob4bceb2e46ffba6c7dde6bf1623816cb64c3fb359
1 <?php
3 /**
4 * Generic operation result class
5 * Has warning/error list, boolean status and arbitrary value
7 * "Good" means the operation was completed with no warnings or errors.
9 * "OK" means the operation was partially or wholly completed.
11 * An operation which is not OK should have errors so that the user can be
12 * informed as to what went wrong. Calling the fatal() function sets an error
13 * message and simultaneously switches off the OK flag.
15 class Status {
16 var $ok = true;
17 var $value;
19 /** Counters for batch operations */
20 var $successCount = 0, $failCount = 0;
22 /*semi-private*/ var $errors = array();
23 /*semi-private*/ var $cleanCallback = false;
25 /**
26 * Factory function for fatal errors
28 static function newFatal( $message /*, parameters...*/ ) {
29 $params = func_get_args();
30 $result = new self;
31 call_user_func_array( array( &$result, 'error' ), $params );
32 $result->ok = false;
33 return $result;
36 static function newGood( $value = null ) {
37 $result = new self;
38 $result->value = $value;
39 return $result;
42 function setResult( $ok, $value = null ) {
43 $this->ok = $ok;
44 $this->value = $value;
47 function isGood() {
48 return $this->ok && !$this->errors;
51 function isOK() {
52 return $this->ok;
55 function warning( $message /*, parameters... */ ) {
56 $params = array_slice( func_get_args(), 1 );
57 $this->errors[] = array(
58 'type' => 'warning',
59 'message' => $message,
60 'params' => $params );
63 /**
64 * Add an error, do not set fatal flag
65 * This can be used for non-fatal errors
67 function error( $message /*, parameters... */ ) {
68 $params = array_slice( func_get_args(), 1 );
69 $this->errors[] = array(
70 'type' => 'error',
71 'message' => $message,
72 'params' => $params );
75 /**
76 * Add an error and set OK to false, indicating that the operation as a whole was fatal
78 function fatal( $message /*, parameters... */ ) {
79 $params = array_slice( func_get_args(), 1 );
80 $this->errors[] = array(
81 'type' => 'error',
82 'message' => $message,
83 'params' => $params );
84 $this->ok = false;
87 /**
88 * Sanitize the callback parameter on wakeup, to avoid arbitrary execution.
90 function __wakeup() {
91 $this->cleanCallback = false;
94 protected function cleanParams( $params ) {
95 if ( !$this->cleanCallback ) {
96 return $params;
98 $cleanParams = array();
99 foreach ( $params as $i => $param ) {
100 $cleanParams[$i] = call_user_func( $this->cleanCallback, $param );
102 return $cleanParams;
105 protected function getItemXML( $item ) {
106 $params = $this->cleanParams( $item['params'] );
107 $xml = "<{$item['type']}>\n" .
108 Xml::element( 'message', null, $item['message'] ) . "\n" .
109 Xml::element( 'text', null, wfMsgReal( $item['message'], $params ) ) ."\n";
110 foreach ( $params as $param ) {
111 $xml .= Xml::element( 'param', null, $param );
113 $xml .= "</{$this->type}>\n";
114 return $xml;
118 * Get the error list as XML
120 function getXML() {
121 $xml = "<errors>\n";
122 foreach ( $this->errors as $error ) {
123 $xml .= $this->getItemXML( $error );
125 $xml .= "</errors>\n";
126 return $xml;
130 * Get the error list as a wikitext formatted list
131 * @param string $shortContext A short enclosing context message name, to be used
132 * when there is a single error
133 * @param string $longContext A long enclosing context message name, for a list
135 function getWikiText( $shortContext = false, $longContext = false ) {
136 if ( count( $this->errors ) == 0 ) {
137 if ( $this->ok ) {
138 $this->fatal( 'internalerror_info',
139 __METHOD__." called for a good result, this is incorrect\n" );
140 } else {
141 $this->fatal( 'internalerror_info',
142 __METHOD__.": Invalid result object: no error text but not OK\n" );
145 if ( count( $this->errors ) == 1 ) {
146 $params = array_map( 'wfEscapeWikiText', $this->cleanParams( $this->errors[0]['params'] ) );
147 $s = wfMsgReal( $this->errors[0]['message'], $params, true, false, false );
148 if ( $shortContext ) {
149 $s = wfMsgNoTrans( $shortContext, $s );
150 } elseif ( $longContext ) {
151 $s = wfMsgNoTrans( $longContext, "* $s\n" );
153 } else {
154 $s = '';
155 foreach ( $this->errors as $error ) {
156 $params = array_map( 'wfEscapeWikiText', $this->cleanParams( $error['params'] ) );
157 $s .= '* ' . wfMsgReal( $error['message'], $params, true, false, false ) . "\n";
159 if ( $longContext ) {
160 $s = wfMsgNoTrans( $longContext, $s );
161 } elseif ( $shortContext ) {
162 $s = wfMsgNoTrans( $shortContext, "\n$s\n" );
165 return $s;
169 * Merge another status object into this one
171 function merge( $other, $overwriteValue = false ) {
172 $this->errors = array_merge( $this->errors, $other->errors );
173 $this->ok = $this->ok && $other->ok;
174 if ( $overwriteValue ) {
175 $this->value = $other->value;
177 $this->successCount += $other->successCount;
178 $this->failCount += $other->failCount;
181 function getErrorsArray() {
182 $result = array();
183 foreach ( $this->errors as $error ) {
184 if ( $error['type'] == 'error' )
185 if( $error['params'] )
186 $result[] = array_merge( array( $error['message'] ), $error['params'] );
187 else
188 $result[] = $error['message'];
190 return $result;
194 * Returns true if the specified message is present as a warning or error
196 function hasMessage( $msg ) {
197 foreach ( $this->errors as $error ) {
198 if ( $error['message'] === $msg ) {
199 return true;
202 return false;