I guess it should actually return the result (anyway, there is a fatal PHP error...
[lhc/web/wiklou.git] / includes / filerepo / FileRepoStatus.php
1 <?php
2
3 /**
4 * Generic operation result class
5 * Has warning/error list, boolean status and arbitrary value
6 */
7 class FileRepoStatus {
8 var $ok = true;
9 var $value;
10
11 /** Counters for batch operations */
12 var $successCount = 0, $failCount = 0;
13
14 /*semi-private*/ var $errors = array();
15 /*semi-private*/ var $cleanCallback = false;
16
17 /**
18 * Factory function for fatal errors
19 */
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 );
24 $result->ok = false;
25 return $result;
26 }
27
28 static function newGood( $repo = false, $value = null ) {
29 $result = new self( $repo );
30 $result->value = $value;
31 return $result;
32 }
33
34 function __construct( $repo = false ) {
35 if ( $repo ) {
36 $this->cleanCallback = $repo->getErrorCleanupFunction();
37 }
38 }
39
40 function setResult( $ok, $value = null ) {
41 $this->ok = $ok;
42 $this->value = $value;
43 }
44
45 function isGood() {
46 return $this->ok && !$this->errors;
47 }
48
49 function isOK() {
50 return $this->ok;
51 }
52
53 function warning( $message /*, parameters... */ ) {
54 $params = array_slice( func_get_args(), 1 );
55 $this->errors[] = array(
56 'type' => 'warning',
57 'message' => $message,
58 'params' => $params );
59 }
60
61 /**
62 * Add an error, do not set fatal flag
63 * This can be used for non-fatal errors
64 */
65 function error( $message /*, parameters... */ ) {
66 $params = array_slice( func_get_args(), 1 );
67 $this->errors[] = array(
68 'type' => 'error',
69 'message' => $message,
70 'params' => $params );
71 }
72
73 /**
74 * Add an error and set OK to false, indicating that the operation as a whole was fatal
75 */
76 function fatal( $message /*, parameters... */ ) {
77 $params = array_slice( func_get_args(), 1 );
78 $this->errors[] = array(
79 'type' => 'error',
80 'message' => $message,
81 'params' => $params );
82 $this->ok = false;
83 }
84
85 protected function cleanParams( $params ) {
86 if ( !$this->cleanCallback ) {
87 return $params;
88 }
89 $cleanParams = array();
90 foreach ( $params as $i => $param ) {
91 $cleanParams[$i] = call_user_func( $this->cleanCallback, $param );
92 }
93 return $cleanParams;
94 }
95
96 protected function getItemXML( $item ) {
97 $params = $this->cleanParams( $item['params'] );
98 $xml = "<{$item['type']}>\n" .
99 Xml::element( 'message', null, $item['message'] ) . "\n" .
100 Xml::element( 'text', null, wfMsgReal( $item['message'], $params ) ) ."\n";
101 foreach ( $params as $param ) {
102 $xml .= Xml::element( 'param', null, $param );
103 }
104 $xml .= "</{$this->type}>\n";
105 return $xml;
106 }
107
108 /**
109 * Get the error list as XML
110 */
111 function getXML() {
112 $xml = "<errors>\n";
113 foreach ( $this->errors as $error ) {
114 $xml .= $this->getItemXML( $error );
115 }
116 $xml .= "</errors>\n";
117 return $xml;
118 }
119
120 /**
121 * Get the error list as a wikitext formatted list
122 * @param string $shortContext A short enclosing context message name, to be used
123 * when there is a single error
124 * @param string $longContext A long enclosing context message name, for a list
125 */
126 function getWikiText( $shortContext = false, $longContext = false ) {
127 if ( count( $this->errors ) == 0 ) {
128 if ( $this->ok ) {
129 $this->fatal( 'internalerror_info',
130 __METHOD__." called for a good result, this is incorrect\n" );
131 } else {
132 $this->fatal( 'internalerror_info',
133 __METHOD__.": Invalid result object: no error text but not OK\n" );
134 }
135 }
136 if ( count( $this->errors ) == 1 ) {
137 $params = array_map( 'wfEscapeWikiText', $this->cleanParams( $this->errors[0]['params'] ) );
138 $s = wfMsgReal( $this->errors[0]['message'], $params );
139 if ( $shortContext ) {
140 $s = wfMsg( $shortContext, $s );
141 } elseif ( $longContext ) {
142 $s = wfMsg( $longContext, "* $s\n" );
143 }
144 } else {
145 $s = '';
146 foreach ( $this->errors as $error ) {
147 $params = array_map( 'wfEscapeWikiText', $this->cleanParams( $error['params'] ) );
148 $s .= '* ' . wfMsgReal( $error['message'], $params ) . "\n";
149 }
150 if ( $longContext ) {
151 $s = wfMsg( $longContext, $s );
152 } elseif ( $shortContext ) {
153 $s = wfMsg( $shortContext, "\n* $s\n" );
154 }
155 }
156 return $s;
157 }
158
159 /**
160 * Merge another status object into this one
161 */
162 function merge( $other, $overwriteValue = false ) {
163 $this->errors = array_merge( $this->errors, $other->errors );
164 $this->ok = $this->ok && $other->ok;
165 if ( $overwriteValue ) {
166 $this->value = $other->value;
167 }
168 $this->successCount += $other->successCount;
169 $this->failCount += $other->failCount;
170 }
171 }