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 );
27 static function newGood( $repo = false, $value = null ) {
28 $result = new self( $repo );
29 $result->value
= $value;
33 function __construct( $repo = false ) {
35 $this->cleanCallback
= $repo->getErrorCleanupFunction();
39 function setResult( $ok, $value = null ) {
41 $this->value
= $value;
45 return $this->ok
&& !$this->errors
;
52 function warning( $message /*, parameters... */ ) {
53 $params = array_slice( func_get_args(), 1 );
54 $this->errors
[] = array(
56 'message' => $message,
57 'params' => $params );
61 * Add an error, do not set fatal flag
62 * This can be used for non-fatal errors
64 function error( $message /*, parameters... */ ) {
65 $params = array_slice( func_get_args(), 1 );
66 $this->errors
[] = array(
68 'message' => $message,
69 'params' => $params );
73 * Add an error and set OK to false, indicating that the operation as a whole was fatal
75 function fatal( $message /*, parameters... */ ) {
76 $params = array_slice( func_get_args(), 1 );
77 $this->errors
[] = array(
79 'message' => $message,
80 'params' => $params );
84 protected function cleanParams( $params ) {
85 if ( !$this->cleanCallback
) {
88 $cleanParams = array();
89 foreach ( $params as $i => $param ) {
90 $cleanParams[$i] = call_user_func( $this->cleanCallback
, $param );
95 protected function getItemXML( $item ) {
96 $params = $this->cleanParams( $item['params'] );
97 $xml = "<{$item['type']}>\n" .
98 Xml
::element( 'message', null, $item['message'] ) . "\n" .
99 Xml
::element( 'text', null, wfMsgReal( $item['message'], $params ) ) ."\n";
100 foreach ( $params as $param ) {
101 $xml .= Xml
::element( 'param', null, $param );
103 $xml .= "</{$this->type}>\n";
108 * Get the error list as XML
112 foreach ( $this->errors
as $error ) {
113 $xml .= $this->getItemXML( $error );
115 $xml .= "</errors>\n";
120 * Get the error list as a wikitext formatted list
121 * @param string $shortContext A short enclosing context message name, to be used
122 * when there is a single error
123 * @param string $longContext A long enclosing context message name, for a list
125 function getWikiText( $shortContext = false, $longContext = false ) {
126 if ( count( $this->errors
) == 0 ) {
128 $this->fatal( 'internalerror_info',
129 __METHOD__
." called for a good result, this is incorrect\n" );
131 $this->fatal( 'internalerror_info',
132 __METHOD__
.": Invalid result object: no error text but not OK\n" );
135 if ( count( $this->errors
) == 1 ) {
136 $params = array_map( 'wfEscapeWikiText', $this->cleanParams( $this->errors
[0]['params'] ) );
137 $s = wfMsgReal( $this->errors
[0]['message'], $params );
138 if ( $shortContext ) {
139 $s = wfMsg( $shortContext, $s );
140 } elseif ( $longContext ) {
141 $s = wfMsg( $longContext, "* $s\n" );
145 foreach ( $this->errors
as $error ) {
146 $params = array_map( 'wfEscapeWikiText', $this->cleanParams( $error['params'] ) );
147 $s .= '* ' . wfMsgReal( $error['message'], $params ) . "\n";
149 if ( $longContext ) {
150 $s = wfMsg( $longContext, $s );
151 } elseif ( $shortContext ) {
152 $s = wfMsg( $shortContext, "\n* $s\n" );
159 * Merge another status object into this one
161 function merge( $other, $overwriteValue = false ) {
162 $this->errors
= array_merge( $this->errors
, $other->errors
);
163 $this->ok
= $this->ok
&& $other->ok
;
164 if ( $overwriteValue ) {
165 $this->value
= $other->value
;
167 $this->successCount +
= $other->successCount
;
168 $this->failCount +
= $other->failCount
;