4 * Generic operation result class
5 * Has warning/error list, boolean status and arbitrary value
11 /** Counters for batch operations */
12 var $successCount = 0, $failCount = 0;
14 /*semi-private*/ var $errors = array();
15 /*semi-private*/ var $cleanCallback = false;
18 * Factory function for fatal errors
20 static function newFatal( $repo, $message /*, parameters...*/ ) {
21 $params = array_slice( func_get_args(), 1 );
22 $result = new self( $repo );
23 call_user_func_array( array( &$result, 'error' ), $params );
28 static function newGood( $repo = false, $value = null ) {
29 $result = new self( $repo );
30 $result->value
= $value;
34 function __construct( $repo = false ) {
36 $this->cleanCallback
= $repo->getErrorCleanupFunction();
40 function setResult( $ok, $value = null ) {
42 $this->value
= $value;
46 return $this->ok
&& !$this->errors
;
53 function warning( $message /*, parameters... */ ) {
54 $params = array_slice( func_get_args(), 1 );
55 $this->errors
[] = array(
57 'message' => $message,
58 'params' => $params );
62 * Add an error, do not set fatal flag
63 * This can be used for non-fatal errors
65 function error( $message /*, parameters... */ ) {
66 $params = array_slice( func_get_args(), 1 );
67 $this->errors
[] = array(
69 'message' => $message,
70 'params' => $params );
74 * Add an error and set OK to false, indicating that the operation as a whole was fatal
76 function fatal( $message /*, parameters... */ ) {
77 $params = array_slice( func_get_args(), 1 );
78 $this->errors
[] = array(
80 'message' => $message,
81 'params' => $params );
85 protected function cleanParams( $params ) {
86 if ( !$this->cleanCallback
) {
89 $cleanParams = array();
90 foreach ( $params as $i => $param ) {
91 $cleanParams[$i] = call_user_func( $this->cleanCallback
, $param );
96 protected function getItemXML( $item ) {
97 $params = $this->cleanParams( $item['params'] );
98 $xml = "<{$item['type']}>\n" .
99 Xml
::element( 'message', null, $item['message'] ) . "\n" .
100 Xml
::element( 'text', null, wfMsgReal( $item['message'], $params ) ) ."\n";
101 foreach ( $params as $param ) {
102 $xml .= Xml
::element( 'param', null, $param );
104 $xml .= "</{$this->type}>\n";
109 * Get the error list as XML
113 foreach ( $this->errors
as $error ) {
114 $xml .= $this->getItemXML( $error );
116 $xml .= "</errors>\n";
121 * Get the error list as a wikitext formatted list
122 * @param string $shortContext A short enclosing context message name, to be used
123 * when there is a single error
124 * @param string $longContext A long enclosing context message name, for a list
126 function getWikiText( $shortContext = false, $longContext = false ) {
127 if ( count( $this->errors
) == 0 ) {
129 $this->fatal( 'internalerror_info',
130 __METHOD__
." called for a good result, this is incorrect\n" );
132 $this->fatal( 'internalerror_info',
133 __METHOD__
.": Invalid result object: no error text but not OK\n" );
136 if ( count( $this->errors
) == 1 ) {
137 $params = array_map( 'wfEscapeWikiText', $this->cleanParams( $this->errors
[0]['params'] ) );
138 $s = wfMsgReal( $this->errors
[0]['message'], $params );
139 if ( $shortContext ) {
140 $s = wfMsg( $shortContext, $s );
141 } elseif ( $longContext ) {
142 $s = wfMsg( $longContext, "* $s\n" );
146 foreach ( $this->errors
as $error ) {
147 $params = array_map( 'wfEscapeWikiText', $this->cleanParams( $error['params'] ) );
148 $s .= '* ' . wfMsgReal( $error['message'], $params ) . "\n";
150 if ( $longContext ) {
151 $s = wfMsg( $longContext, $s );
152 } elseif ( $shortContext ) {
153 $s = wfMsg( $shortContext, "\n* $s\n" );
160 * Merge another status object into this one
162 function merge( $other, $overwriteValue = false ) {
163 $this->errors
= array_merge( $this->errors
, $other->errors
);
164 $this->ok
= $this->ok
&& $other->ok
;
165 if ( $overwriteValue ) {
166 $this->value
= $other->value
;
168 $this->successCount +
= $other->successCount
;
169 $this->failCount +
= $other->failCount
;