Committing some work in progress -- abstraction of handler-specific metadata formatti...
[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 }
26
27 static function newGood( $repo = false, $value = null ) {
28 $result = new self( $repo );
29 $result->value = $value;
30 return $result;
31 }
32
33 function __construct( $repo = false ) {
34 if ( $repo ) {
35 $this->cleanCallback = $repo->getErrorCleanupFunction();
36 }
37 }
38
39 function setResult( $ok, $value = null ) {
40 $this->ok = $ok;
41 $this->value = $value;
42 }
43
44 function isGood() {
45 return $this->ok && !$this->errors;
46 }
47
48 function isOK() {
49 return $this->ok;
50 }
51
52 function warning( $message /*, parameters... */ ) {
53 $params = array_slice( func_get_args(), 1 );
54 $this->errors[] = array(
55 'type' => 'warning',
56 'message' => $message,
57 'params' => $params );
58 }
59
60 /**
61 * Add an error, do not set fatal flag
62 * This can be used for non-fatal errors
63 */
64 function error( $message /*, parameters... */ ) {
65 $params = array_slice( func_get_args(), 1 );
66 $this->errors[] = array(
67 'type' => 'error',
68 'message' => $message,
69 'params' => $params );
70 }
71
72 /**
73 * Add an error and set OK to false, indicating that the operation as a whole was fatal
74 */
75 function fatal( $message /*, parameters... */ ) {
76 $params = array_slice( func_get_args(), 1 );
77 $this->errors[] = array(
78 'type' => 'error',
79 'message' => $message,
80 'params' => $params );
81 $this->ok = false;
82 }
83
84 protected function cleanParams( $params ) {
85 if ( !$this->cleanCallback ) {
86 return $params;
87 }
88 $cleanParams = array();
89 foreach ( $params as $i => $param ) {
90 $cleanParams[$i] = call_user_func( $this->cleanCallback, $param );
91 }
92 return $cleanParams;
93 }
94
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 );
102 }
103 $xml .= "</{$this->type}>\n";
104 return $xml;
105 }
106
107 /**
108 * Get the error list as XML
109 */
110 function getXML() {
111 $xml = "<errors>\n";
112 foreach ( $this->errors as $error ) {
113 $xml .= $this->getItemXML( $error );
114 }
115 $xml .= "</errors>\n";
116 return $xml;
117 }
118
119 /**
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
124 */
125 function getWikiText( $shortContext = false, $longContext = false ) {
126 if ( count( $this->errors ) == 0 ) {
127 if ( $this->ok ) {
128 $this->fatal( 'internalerror_info',
129 __METHOD__." called for a good result, this is incorrect\n" );
130 } else {
131 $this->fatal( 'internalerror_info',
132 __METHOD__.": Invalid result object: no error text but not OK\n" );
133 }
134 }
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" );
142 }
143 } else {
144 $s = '';
145 foreach ( $this->errors as $error ) {
146 $params = array_map( 'wfEscapeWikiText', $this->cleanParams( $error['params'] ) );
147 $s .= '* ' . wfMsgReal( $error['message'], $params ) . "\n";
148 }
149 if ( $longContext ) {
150 $s = wfMsg( $longContext, $s );
151 } elseif ( $shortContext ) {
152 $s = wfMsg( $shortContext, "\n* $s\n" );
153 }
154 }
155 return $s;
156 }
157
158 /**
159 * Merge another status object into this one
160 */
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;
166 }
167 $this->successCount += $other->successCount;
168 $this->failCount += $other->failCount;
169 }
170 }