]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - includes/Status.php
MediaWiki 1.15.0
[autoinstalls/mediawiki.git] / includes / Status.php
1 <?php
2
3 /**
4  * Generic operation result class
5  * Has warning/error list, boolean status and arbitrary value
6  *
7  * "Good" means the operation was completed with no warnings or errors.
8  *
9  * "OK" means the operation was partially or wholly completed.
10  *
11  * An operation which is not OK should have errors so that the user can be
12  * informed as to what went wrong. Calling the fatal() function sets an error
13  * message and simultaneously switches off the OK flag.
14  */
15 class Status {
16         var $ok = true;
17         var $value;
18
19         /** Counters for batch operations */
20         var $successCount = 0, $failCount = 0;
21
22         /*semi-private*/ var $errors = array();
23         /*semi-private*/ var $cleanCallback = false;
24
25         /**
26          * Factory function for fatal errors
27          */
28         static function newFatal( $message /*, parameters...*/ ) {
29                 $params = func_get_args();
30                 $result = new self;
31                 call_user_func_array( array( &$result, 'error' ), $params );
32                 $result->ok = false;
33                 return $result;
34         }
35
36         static function newGood( $value = null ) {
37                 $result = new self;
38                 $result->value = $value;
39                 return $result;
40         }
41
42         function setResult( $ok, $value = null ) {
43                 $this->ok = $ok;
44                 $this->value = $value;
45         }
46
47         function isGood() {
48                 return $this->ok && !$this->errors;
49         }
50
51         function isOK() {
52                 return $this->ok;
53         }
54
55         function warning( $message /*, parameters... */ ) {
56                 $params = array_slice( func_get_args(), 1 );
57                 $this->errors[] = array(
58                         'type' => 'warning',
59                         'message' => $message,
60                         'params' => $params );
61         }
62
63         /**
64          * Add an error, do not set fatal flag
65          * This can be used for non-fatal errors
66          */
67         function error( $message /*, parameters... */ ) {
68                 $params = array_slice( func_get_args(), 1 );
69                 $this->errors[] = array(
70                         'type' => 'error',
71                         'message' => $message,
72                         'params' => $params );
73         }
74
75         /**
76          * Add an error and set OK to false, indicating that the operation as a whole was fatal
77          */
78         function fatal( $message /*, parameters... */ ) {
79                 $params = array_slice( func_get_args(), 1 );
80                 $this->errors[] = array(
81                         'type' => 'error',
82                         'message' => $message,
83                         'params' => $params );
84                 $this->ok = false;
85         }
86
87         protected function cleanParams( $params ) {
88                 if ( !$this->cleanCallback ) {
89                         return $params;
90                 }
91                 $cleanParams = array();
92                 foreach ( $params as $i => $param ) {
93                         $cleanParams[$i] = call_user_func( $this->cleanCallback, $param );
94                 }
95                 return $cleanParams;
96         }
97
98         protected function getItemXML( $item ) {
99                 $params = $this->cleanParams( $item['params'] );
100                 $xml = "<{$item['type']}>\n" .
101                         Xml::element( 'message', null, $item['message'] ) . "\n" .
102                         Xml::element( 'text', null, wfMsgReal( $item['message'], $params ) ) ."\n";
103                 foreach ( $params as $param ) {
104                         $xml .= Xml::element( 'param', null, $param );
105                 }
106                 $xml .= "</{$this->type}>\n";
107                 return $xml;
108         }
109
110         /**
111          * Get the error list as XML
112          */
113         function getXML() {
114                 $xml = "<errors>\n";
115                 foreach ( $this->errors as $error ) {
116                         $xml .= $this->getItemXML( $error );
117                 }
118                 $xml .= "</errors>\n";
119                 return $xml;
120         }
121
122         /**
123          * Get the error list as a wikitext formatted list
124          * @param string $shortContext A short enclosing context message name, to be used
125          *     when there is a single error
126          * @param string $longContext A long enclosing context message name, for a list
127          */
128         function getWikiText( $shortContext = false, $longContext = false ) {
129                 if ( count( $this->errors ) == 0 ) {
130                         if ( $this->ok ) {
131                                 $this->fatal( 'internalerror_info',
132                                         __METHOD__." called for a good result, this is incorrect\n" );
133                         } else {
134                                 $this->fatal( 'internalerror_info',
135                                         __METHOD__.": Invalid result object: no error text but not OK\n" );
136                         }
137                 }
138                 if ( count( $this->errors ) == 1 ) {
139                         $params = array_map( 'wfEscapeWikiText', $this->cleanParams( $this->errors[0]['params'] ) );
140                         $s = wfMsgReal( $this->errors[0]['message'], $params, true, false, false );
141                         if ( $shortContext ) {
142                                 $s = wfMsgNoTrans( $shortContext, $s );
143                         } elseif ( $longContext ) {
144                                 $s = wfMsgNoTrans( $longContext, "* $s\n" );
145                         }
146                 } else {
147                         $s = '';
148                         foreach ( $this->errors as $error ) {
149                                 $params = array_map( 'wfEscapeWikiText', $this->cleanParams( $error['params'] ) );
150                                 $s .= '* ' . wfMsgReal( $error['message'], $params, true, false, false ) . "\n";
151                         }
152                         if ( $longContext ) {
153                                 $s = wfMsgNoTrans( $longContext, $s );
154                         } elseif ( $shortContext ) {
155                                 $s = wfMsgNoTrans( $shortContext, "\n* $s\n" );
156                         }
157                 }
158                 return $s;
159         }
160
161         /**
162          * Merge another status object into this one
163          */
164         function merge( $other, $overwriteValue = false ) {
165                 $this->errors = array_merge( $this->errors, $other->errors );
166                 $this->ok = $this->ok && $other->ok;
167                 if ( $overwriteValue ) {
168                         $this->value = $other->value;
169                 }
170                 $this->successCount += $other->successCount;
171                 $this->failCount += $other->failCount;
172         }
173         
174         function getErrorsArray() {
175                 $result = array();
176                 foreach ( $this->errors as $error ) {
177                         if ( $error['type'] == 'error' )
178                                 $result[] = $error['message'];
179                 }
180                 return $result;
181         }
182
183         /**
184          * Returns true if the specified message is present as a warning or error
185          */
186         function hasMessage( $msg ) {
187                 foreach ( $this->errors as $error ) {
188                         if ( $error['message'] === $msg ) {
189                                 return true;
190                         }
191                 }
192                 return false;
193         }
194 }