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 );
88 * Sanitize the callback parameter on wakeup, to avoid arbitrary execution.
91 $this->cleanCallback
= false;
94 protected function cleanParams( $params ) {
95 if ( !$this->cleanCallback
) {
98 $cleanParams = array();
99 foreach ( $params as $i => $param ) {
100 $cleanParams[$i] = call_user_func( $this->cleanCallback
, $param );
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";
118 * Get the error list as XML
122 foreach ( $this->errors
as $error ) {
123 $xml .= $this->getItemXML( $error );
125 $xml .= "</errors>\n";
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 ) {
138 $this->fatal( 'internalerror_info',
139 __METHOD__
." called for a good result, this is incorrect\n" );
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" );
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" );
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() {
183 foreach ( $this->errors
as $error ) {
184 if ( $error['type'] == 'error' )
185 if( $error['params'] )
186 $result[] = array_merge( array( $error['message'] ), $error['params'] );
188 $result[] = $error['message'];
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 ) {