]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - includes/Status.php
MediaWiki 1.16.0
[autoinstallsdev/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         /**
88          * Sanitize the callback parameter on wakeup, to avoid arbitrary execution.
89          */
90         function __wakeup() {
91                 $this->cleanCallback = false;
92         }
93
94         protected function cleanParams( $params ) {
95                 if ( !$this->cleanCallback ) {
96                         return $params;
97                 }
98                 $cleanParams = array();
99                 foreach ( $params as $i => $param ) {
100                         $cleanParams[$i] = call_user_func( $this->cleanCallback, $param );
101                 }
102                 return $cleanParams;
103         }
104
105         protected function getItemXML( $item ) {
106                 $params = $this->cleanParams( $item['params'] );
107                 $xml = "<{$item['type']}>\n" .
108                         Xml::element( 'message', null, $item['message'] ) . "\n" .
109                         Xml::element( 'text', null, wfMsgReal( $item['message'], $params ) ) ."\n";
110                 foreach ( $params as $param ) {
111                         $xml .= Xml::element( 'param', null, $param );
112                 }
113                 $xml .= "</{$this->type}>\n";
114                 return $xml;
115         }
116
117         /**
118          * Get the error list as XML
119          */
120         function getXML() {
121                 $xml = "<errors>\n";
122                 foreach ( $this->errors as $error ) {
123                         $xml .= $this->getItemXML( $error );
124                 }
125                 $xml .= "</errors>\n";
126                 return $xml;
127         }
128
129         /**
130          * Get the error list as a wikitext formatted list
131          * @param string $shortContext A short enclosing context message name, to be used
132          *     when there is a single error
133          * @param string $longContext A long enclosing context message name, for a list
134          */
135         function getWikiText( $shortContext = false, $longContext = false ) {
136                 if ( count( $this->errors ) == 0 ) {
137                         if ( $this->ok ) {
138                                 $this->fatal( 'internalerror_info',
139                                         __METHOD__." called for a good result, this is incorrect\n" );
140                         } else {
141                                 $this->fatal( 'internalerror_info',
142                                         __METHOD__.": Invalid result object: no error text but not OK\n" );
143                         }
144                 }
145                 if ( count( $this->errors ) == 1 ) {
146                         $params = array_map( 'wfEscapeWikiText', $this->cleanParams( $this->errors[0]['params'] ) );
147                         $s = wfMsgReal( $this->errors[0]['message'], $params, true, false, false );
148                         if ( $shortContext ) {
149                                 $s = wfMsgNoTrans( $shortContext, $s );
150                         } elseif ( $longContext ) {
151                                 $s = wfMsgNoTrans( $longContext, "* $s\n" );
152                         }
153                 } else {
154                         $s = '';
155                         foreach ( $this->errors as $error ) {
156                                 $params = array_map( 'wfEscapeWikiText', $this->cleanParams( $error['params'] ) );
157                                 $s .= '* ' . wfMsgReal( $error['message'], $params, true, false, false ) . "\n";
158                         }
159                         if ( $longContext ) {
160                                 $s = wfMsgNoTrans( $longContext, $s );
161                         } elseif ( $shortContext ) {
162                                 $s = wfMsgNoTrans( $shortContext, "\n$s\n" );
163                         }
164                 }
165                 return $s;
166         }
167
168         /**
169          * Merge another status object into this one
170          */
171         function merge( $other, $overwriteValue = false ) {
172                 $this->errors = array_merge( $this->errors, $other->errors );
173                 $this->ok = $this->ok && $other->ok;
174                 if ( $overwriteValue ) {
175                         $this->value = $other->value;
176                 }
177                 $this->successCount += $other->successCount;
178                 $this->failCount += $other->failCount;
179         }
180
181         function getErrorsArray() {
182                 $result = array();
183                 foreach ( $this->errors as $error ) {
184                         if ( $error['type'] == 'error' )
185                                 if( $error['params'] )
186                                         $result[] = array_merge( array( $error['message'] ), $error['params'] );
187                                 else
188                                         $result[] = $error['message'];
189                 }
190                 return $result;
191         }
192
193         /**
194          * Returns true if the specified message is present as a warning or error
195          */
196         function hasMessage( $msg ) {
197                 foreach ( $this->errors as $error ) {
198                         if ( $error['message'] === $msg ) {
199                                 return true;
200                         }
201                 }
202                 return false;
203         }
204 }