00001 <?php
00002
00015 class Status {
00016 var $ok = true;
00017 var $value;
00018
00020 var $successCount = 0, $failCount = 0;
00021
00022 var $errors = array();
00023 var $cleanCallback = false;
00024
00028 static function newFatal( $message ) {
00029 $params = func_get_args();
00030 $result = new self;
00031 call_user_func_array( array( &$result, 'error' ), $params );
00032 $result->ok = false;
00033 return $result;
00034 }
00035
00036 static function newGood( $value = null ) {
00037 $result = new self;
00038 $result->value = $value;
00039 return $result;
00040 }
00041
00042 function setResult( $ok, $value = null ) {
00043 $this->ok = $ok;
00044 $this->value = $value;
00045 }
00046
00047 function isGood() {
00048 return $this->ok && !$this->errors;
00049 }
00050
00051 function isOK() {
00052 return $this->ok;
00053 }
00054
00055 function warning( $message ) {
00056 $params = array_slice( func_get_args(), 1 );
00057 $this->errors[] = array(
00058 'type' => 'warning',
00059 'message' => $message,
00060 'params' => $params );
00061 }
00062
00067 function error( $message ) {
00068 $params = array_slice( func_get_args(), 1 );
00069 $this->errors[] = array(
00070 'type' => 'error',
00071 'message' => $message,
00072 'params' => $params );
00073 }
00074
00078 function fatal( $message ) {
00079 $params = array_slice( func_get_args(), 1 );
00080 $this->errors[] = array(
00081 'type' => 'error',
00082 'message' => $message,
00083 'params' => $params );
00084 $this->ok = false;
00085 }
00086
00090 function __wakeup() {
00091 $this->cleanCallback = false;
00092 }
00093
00094 protected function cleanParams( $params ) {
00095 if ( !$this->cleanCallback ) {
00096 return $params;
00097 }
00098 $cleanParams = array();
00099 foreach ( $params as $i => $param ) {
00100 $cleanParams[$i] = call_user_func( $this->cleanCallback, $param );
00101 }
00102 return $cleanParams;
00103 }
00104
00105 protected function getItemXML( $item ) {
00106 $params = $this->cleanParams( $item['params'] );
00107 $xml = "<{$item['type']}>\n" .
00108 Xml::element( 'message', null, $item['message'] ) . "\n" .
00109 Xml::element( 'text', null, wfMsgReal( $item['message'], $params ) ) ."\n";
00110 foreach ( $params as $param ) {
00111 $xml .= Xml::element( 'param', null, $param );
00112 }
00113 $xml .= "</{$this->type}>\n";
00114 return $xml;
00115 }
00116
00120 function getXML() {
00121 $xml = "<errors>\n";
00122 foreach ( $this->errors as $error ) {
00123 $xml .= $this->getItemXML( $error );
00124 }
00125 $xml .= "</errors>\n";
00126 return $xml;
00127 }
00128
00135 function getWikiText( $shortContext = false, $longContext = false ) {
00136 if ( count( $this->errors ) == 0 ) {
00137 if ( $this->ok ) {
00138 $this->fatal( 'internalerror_info',
00139 __METHOD__." called for a good result, this is incorrect\n" );
00140 } else {
00141 $this->fatal( 'internalerror_info',
00142 __METHOD__.": Invalid result object: no error text but not OK\n" );
00143 }
00144 }
00145 if ( count( $this->errors ) == 1 ) {
00146 $params = array_map( 'wfEscapeWikiText', $this->cleanParams( $this->errors[0]['params'] ) );
00147 $s = wfMsgReal( $this->errors[0]['message'], $params, true, false, false );
00148 if ( $shortContext ) {
00149 $s = wfMsgNoTrans( $shortContext, $s );
00150 } elseif ( $longContext ) {
00151 $s = wfMsgNoTrans( $longContext, "* $s\n" );
00152 }
00153 } else {
00154 $s = '';
00155 foreach ( $this->errors as $error ) {
00156 $params = array_map( 'wfEscapeWikiText', $this->cleanParams( $error['params'] ) );
00157 $s .= '* ' . wfMsgReal( $error['message'], $params, true, false, false ) . "\n";
00158 }
00159 if ( $longContext ) {
00160 $s = wfMsgNoTrans( $longContext, $s );
00161 } elseif ( $shortContext ) {
00162 $s = wfMsgNoTrans( $shortContext, "\n$s\n" );
00163 }
00164 }
00165 return $s;
00166 }
00167
00171 function merge( $other, $overwriteValue = false ) {
00172 $this->errors = array_merge( $this->errors, $other->errors );
00173 $this->ok = $this->ok && $other->ok;
00174 if ( $overwriteValue ) {
00175 $this->value = $other->value;
00176 }
00177 $this->successCount += $other->successCount;
00178 $this->failCount += $other->failCount;
00179 }
00180
00181 function getErrorsArray() {
00182 $result = array();
00183 foreach ( $this->errors as $error ) {
00184 if ( $error['type'] == 'error' )
00185 if( $error['params'] )
00186 $result[] = array_merge( array( $error['message'] ), $error['params'] );
00187 else
00188 $result[] = $error['message'];
00189 }
00190 return $result;
00191 }
00192
00196 function hasMessage( $msg ) {
00197 foreach ( $this->errors as $error ) {
00198 if ( $error['message'] === $msg ) {
00199 return true;
00200 }
00201 }
00202 return false;
00203 }
00204 }