3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
22 * Generic operation result class
23 * Has warning/error list, boolean status and arbitrary value
25 * "Good" means the operation was completed with no warnings or errors.
27 * "OK" means the operation was partially or wholly completed.
29 * An operation which is not OK should have errors so that the user can be
30 * informed as to what went wrong. Calling the fatal() function sets an error
31 * message and simultaneously switches off the OK flag.
33 * The recommended pattern for Status objects is to return a StatusValue
34 * unconditionally, i.e. both on success and on failure -- so that the
35 * developer of the calling code is reminded that the function can fail, and
36 * so that a lack of error-handling will be explicit.
38 * The use of Message objects should be avoided when serializability is needed.
46 protected $errors = array();
50 /** @var array Map of (key => bool) to indicate success of each part of batch operations */
51 public $success = array();
52 /** @var int Counter for batch operations */
53 public $successCount = 0;
54 /** @var int Counter for batch operations */
55 public $failCount = 0;
58 * Factory function for fatal errors
60 * @param string|MessageSpecifier $message Message key or object
63 public static function newFatal( $message /*, parameters...*/ ) {
64 $params = func_get_args();
65 $result = new static();
66 call_user_func_array( array( &$result, 'fatal' ), $params );
71 * Factory function for good results
76 public static function newGood( $value = null ) {
77 $result = new static();
78 $result->value
= $value;
83 * Returns whether the operation completed and didn't have any error or
88 public function isGood() {
89 return $this->ok
&& !$this->errors
;
93 * Returns whether the operation completed
97 public function isOK() {
104 public function getValue() {
109 * Get the list of errors
111 * Each error is a (message:string or MessageSpecifier,params:array) map
115 public function getErrors() {
116 return $this->errors
;
120 * Change operation status
124 public function setOK( $ok ) {
129 * Change operation resuklt
131 * @param bool $ok Whether the operation completed
132 * @param mixed $value
134 public function setResult( $ok, $value = null ) {
136 $this->value
= $value;
142 * @param string|MessageSpecifier $message Message key or object
144 public function warning( $message /*, parameters... */ ) {
145 $this->errors
[] = array(
147 'message' => $message,
148 'params' => array_slice( func_get_args(), 1 )
153 * Add an error, do not set fatal flag
154 * This can be used for non-fatal errors
156 * @param string|MessageSpecifier $message Message key or object
158 public function error( $message /*, parameters... */ ) {
159 $this->errors
[] = array(
161 'message' => $message,
162 'params' => array_slice( func_get_args(), 1 )
167 * Add an error and set OK to false, indicating that the operation
168 * as a whole was fatal
170 * @param string|MessageSpecifier $message Message key or object
172 public function fatal( $message /*, parameters... */ ) {
173 $this->errors
[] = array(
175 'message' => $message,
176 'params' => array_slice( func_get_args(), 1 )
182 * Merge another status object into this one
184 * @param Status $other Other Status object
185 * @param bool $overwriteValue Whether to override the "value" member
187 public function merge( $other, $overwriteValue = false ) {
188 $this->errors
= array_merge( $this->errors
, $other->errors
);
189 $this->ok
= $this->ok
&& $other->ok
;
190 if ( $overwriteValue ) {
191 $this->value
= $other->value
;
193 $this->successCount +
= $other->successCount
;
194 $this->failCount +
= $other->failCount
;
198 * Returns a list of status messages of the given type
200 * Each entry is a map of (message:string or MessageSpecifier,params:array))
202 * @param string $type
205 public function getErrorsByType( $type ) {
207 foreach ( $this->errors
as $error ) {
208 if ( $error['type'] === $type ) {
217 * Returns true if the specified message is present as a warning or error
219 * @param string|MessageSpecifier $message Message key or object to search for
223 public function hasMessage( $message ) {
224 if ( $message instanceof MessageSpecifier
) {
225 $message = $message->getKey();
227 foreach ( $this->errors
as $error ) {
228 if ( $error['message'] instanceof MessageSpecifier
229 && $error['message']->getKey() === $message
232 } elseif ( $error['message'] === $message ) {
241 * If the specified source message exists, replace it with the specified
242 * destination message, but keep the same parameters as in the original error.
244 * Note, due to the lack of tools for comparing IStatusMessage objects, this
245 * function will not work when using such an object as the search parameter.
247 * @param IStatusMessage|string $source Message key or object to search for
248 * @param IStatusMessage|string $dest Replacement message key or object
249 * @return bool Return true if the replacement was done, false otherwise.
251 public function replaceMessage( $source, $dest ) {
254 foreach ( $this->errors
as $index => $error ) {
255 if ( $error['message'] === $source ) {
256 $this->errors
[$index]['message'] = $dest;
267 public function __toString() {
268 $status = $this->isOK() ?
"OK" : "Error";
269 if ( count( $this->errors
) ) {
270 $errorcount = "collected " . ( count( $this->errors
) ) . " error(s) on the way";
272 $errorcount = "no errors detected";
274 if ( isset( $this->value
) ) {
275 $valstr = gettype( $this->value
) . " value set";
276 if ( is_object( $this->value
) ) {
277 $valstr .= "\"" . get_class( $this->value
) . "\" instance";
280 $valstr = "no value set";
282 $out = sprintf( "<%s, %s, %s>",
287 if ( count( $this->errors
) > 0 ) {
288 $hdr = sprintf( "+-%'-4s-+-%'-25s-+-%'-40s-+\n", "", "", "" );
292 foreach ( $this->errors
as $error ) {
293 if ( $error['message'] instanceof MessageSpecifier
) {
294 $key = $error['message']->getKey();
295 $params = $error['message']->getParams();
296 } elseif ( $error['params'] ) {
297 $key = $error['message'];
298 $params = $error['params'];
300 $key = $error['message'];
304 $out .= sprintf( "| %4d | %-25.25s | %-40.40s |\n",
307 implode( " ", $params )