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
00087 protected function cleanParams( $params ) {
00088 if ( !$this->cleanCallback ) {
00089 return $params;
00090 }
00091 $cleanParams = array();
00092 foreach ( $params as $i => $param ) {
00093 $cleanParams[$i] = call_user_func( $this->cleanCallback, $param );
00094 }
00095 return $cleanParams;
00096 }
00097
00098 protected function getItemXML( $item ) {
00099 $params = $this->cleanParams( $item['params'] );
00100 $xml = "<{$item['type']}>\n" .
00101 Xml::element( 'message', null, $item['message'] ) . "\n" .
00102 Xml::element( 'text', null, wfMsgReal( $item['message'], $params ) ) ."\n";
00103 foreach ( $params as $param ) {
00104 $xml .= Xml::element( 'param', null, $param );
00105 }
00106 $xml .= "</{$this->type}>\n";
00107 return $xml;
00108 }
00109
00113 function getXML() {
00114 $xml = "<errors>\n";
00115 foreach ( $this->errors as $error ) {
00116 $xml .= $this->getItemXML( $error );
00117 }
00118 $xml .= "</errors>\n";
00119 return $xml;
00120 }
00121
00128 function getWikiText( $shortContext = false, $longContext = false ) {
00129 if ( count( $this->errors ) == 0 ) {
00130 if ( $this->ok ) {
00131 $this->fatal( 'internalerror_info',
00132 __METHOD__." called for a good result, this is incorrect\n" );
00133 } else {
00134 $this->fatal( 'internalerror_info',
00135 __METHOD__.": Invalid result object: no error text but not OK\n" );
00136 }
00137 }
00138 if ( count( $this->errors ) == 1 ) {
00139 $params = array_map( 'wfEscapeWikiText', $this->cleanParams( $this->errors[0]['params'] ) );
00140 $s = wfMsgReal( $this->errors[0]['message'], $params, true, false, false );
00141 if ( $shortContext ) {
00142 $s = wfMsgNoTrans( $shortContext, $s );
00143 } elseif ( $longContext ) {
00144 $s = wfMsgNoTrans( $longContext, "* $s\n" );
00145 }
00146 } else {
00147 $s = '';
00148 foreach ( $this->errors as $error ) {
00149 $params = array_map( 'wfEscapeWikiText', $this->cleanParams( $error['params'] ) );
00150 $s .= '* ' . wfMsgReal( $error['message'], $params, true, false, false ) . "\n";
00151 }
00152 if ( $longContext ) {
00153 $s = wfMsgNoTrans( $longContext, $s );
00154 } elseif ( $shortContext ) {
00155 $s = wfMsgNoTrans( $shortContext, "\n* $s\n" );
00156 }
00157 }
00158 return $s;
00159 }
00160
00164 function merge( $other, $overwriteValue = false ) {
00165 $this->errors = array_merge( $this->errors, $other->errors );
00166 $this->ok = $this->ok && $other->ok;
00167 if ( $overwriteValue ) {
00168 $this->value = $other->value;
00169 }
00170 $this->successCount += $other->successCount;
00171 $this->failCount += $other->failCount;
00172 }
00173
00174 function getErrorsArray() {
00175 $result = array();
00176 foreach ( $this->errors as $error ) {
00177 if ( $error['type'] == 'error' )
00178 $result[] = $error['message'];
00179 }
00180 return $result;
00181 }
00182
00186 function hasMessage( $msg ) {
00187 foreach ( $this->errors as $error ) {
00188 if ( $error['message'] === $msg ) {
00189 return true;
00190 }
00191 }
00192 return false;
00193 }
00194 }