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.
19 /** Counters for batch operations */
20 var $successCount = 0, $failCount = 0;
22 /*semi-private*/ var $errors = array();
23 /*semi-private*/ var $cleanCallback = false;
26 * Factory function for fatal errors
28 static function newFatal( $message /*, parameters...*/ ) {
29 $params = func_get_args();
31 call_user_func_array( array( &$result, 'error' ), $params );
36 static function newGood( $value = null ) {
38 $result->value
= $value;
42 function setResult( $ok, $value = null ) {
44 $this->value
= $value;
48 return $this->ok
&& !$this->errors
;
55 function warning( $message /*, parameters... */ ) {
56 $params = array_slice( func_get_args(), 1 );
57 $this->errors
[] = array(
59 'message' => $message,
60 'params' => $params );
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(
71 'message' => $message,
72 'params' => $params );
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(
82 'message' => $message,
83 'params' => $params );
87 protected function cleanParams( $params ) {
88 if ( !$this->cleanCallback
) {
91 $cleanParams = array();
92 foreach ( $params as $i => $param ) {
93 $cleanParams[$i] = call_user_func( $this->cleanCallback
, $param );
98 protected function getItemXML( $item ) {
99 $params = $this->cleanParams( $item['params'] );
100 $xml = "<{$item['type']}>\n" .
101 Xml
::element( 'message', null, $item['message'] ) . "\n" .
102 Xml
::element( 'text', null, wfMsgReal( $item['message'], $params ) ) ."\n";
103 foreach ( $params as $param ) {
104 $xml .= Xml
::element( 'param', null, $param );
106 $xml .= "</{$this->type}>\n";
111 * Get the error list as XML
115 foreach ( $this->errors
as $error ) {
116 $xml .= $this->getItemXML( $error );
118 $xml .= "</errors>\n";
123 * Get the error list as a wikitext formatted list
124 * @param string $shortContext A short enclosing context message name, to be used
125 * when there is a single error
126 * @param string $longContext A long enclosing context message name, for a list
128 function getWikiText( $shortContext = false, $longContext = false ) {
129 if ( count( $this->errors
) == 0 ) {
131 $this->fatal( 'internalerror_info',
132 __METHOD__
." called for a good result, this is incorrect\n" );
134 $this->fatal( 'internalerror_info',
135 __METHOD__
.": Invalid result object: no error text but not OK\n" );
138 if ( count( $this->errors
) == 1 ) {
139 $params = array_map( 'wfEscapeWikiText', $this->cleanParams( $this->errors
[0]['params'] ) );
140 $s = wfMsgReal( $this->errors
[0]['message'], $params, true, false, false );
141 if ( $shortContext ) {
142 $s = wfMsgNoTrans( $shortContext, $s );
143 } elseif ( $longContext ) {
144 $s = wfMsgNoTrans( $longContext, "* $s\n" );
148 foreach ( $this->errors
as $error ) {
149 $params = array_map( 'wfEscapeWikiText', $this->cleanParams( $error['params'] ) );
150 $s .= '* ' . wfMsgReal( $error['message'], $params, true, false, false ) . "\n";
152 if ( $longContext ) {
153 $s = wfMsgNoTrans( $longContext, $s );
154 } elseif ( $shortContext ) {
155 $s = wfMsgNoTrans( $shortContext, "\n* $s\n" );
162 * Merge another status object into this one
164 function merge( $other, $overwriteValue = false ) {
165 $this->errors
= array_merge( $this->errors
, $other->errors
);
166 $this->ok
= $this->ok
&& $other->ok
;
167 if ( $overwriteValue ) {
168 $this->value
= $other->value
;
170 $this->successCount +
= $other->successCount
;
171 $this->failCount +
= $other->failCount
;
174 function getErrorsArray() {
176 foreach ( $this->errors
as $error ) {
177 if ( $error['type'] == 'error' )
178 $result[] = $error['message'];
184 * Returns true if the specified message is present as a warning or error
186 function hasMessage( $msg ) {
187 foreach ( $this->errors
as $error ) {
188 if ( $error['message'] === $msg ) {