Remove double globals.
[lhc/web/wiklou.git] / includes / api / ApiFormatYaml_spyc.php
1 <?php
2 /**
3 * Spyc -- A Simple PHP YAML Class
4 * @version 0.2.3 -- 2006-02-04
5 * @author Chris Wanstrath <chris@ozmm.org>
6 * @see http://spyc.sourceforge.net/
7 * @copyright Copyright 2005-2006 Chris Wanstrath
8 * @license http://www.opensource.org/licenses/mit-license.php MIT License
9 */
10
11 /**
12 * The Simple PHP YAML Class.
13 *
14 * This class can be used to read a YAML file and convert its contents
15 * into a PHP array. It currently supports a very limited subsection of
16 * the YAML spec.
17 *
18 * @ingroup API
19 */
20 class Spyc {
21
22 /**
23 * Dump YAML from PHP array statically
24 *
25 * The dump method, when supplied with an array, will do its best
26 * to convert the array into friendly YAML. Pretty simple. Feel free to
27 * save the returned string as nothing.yml and pass it around.
28 *
29 * Oh, and you can decide how big the indent is and what the wordwrap
30 * for folding is. Pretty cool -- just pass in 'false' for either if
31 * you want to use the default.
32 *
33 * Indent's default is 2 spaces, wordwrap's default is 40 characters. And
34 * you can turn off wordwrap by passing in 0.
35 *
36 * @param $array Array: PHP array
37 * @param $indent Integer: Pass in false to use the default, which is 2
38 * @param $wordwrap Integer: Pass in 0 for no wordwrap, false for default (40)
39 * @return String
40 */
41 public static function YAMLDump( $array, $indent = false, $wordwrap = false ) {
42 $spyc = new Spyc;
43 return $spyc->dump( $array, $indent, $wordwrap );
44 }
45
46 /**
47 * Dump PHP array to YAML
48 *
49 * The dump method, when supplied with an array, will do its best
50 * to convert the array into friendly YAML. Pretty simple. Feel free to
51 * save the returned string as tasteful.yml and pass it around.
52 *
53 * Oh, and you can decide how big the indent is and what the wordwrap
54 * for folding is. Pretty cool -- just pass in 'false' for either if
55 * you want to use the default.
56 *
57 * Indent's default is 2 spaces, wordwrap's default is 40 characters. And
58 * you can turn off wordwrap by passing in 0.
59 *
60 * @param $array Array: PHP array
61 * @param $indent Integer: Pass in false to use the default, which is 2
62 * @param $wordwrap Integer: Pass in 0 for no wordwrap, false for default (40)
63 * @return String
64 */
65 public function dump( $array, $indent = false, $wordwrap = false ) {
66 // Dumps to some very clean YAML. We'll have to add some more features
67 // and options soon. And better support for folding.
68
69 // New features and options.
70 if ( $indent === false or !is_numeric( $indent ) ) {
71 $this->_dumpIndent = 2;
72 } else {
73 $this->_dumpIndent = $indent;
74 }
75
76 if ( $wordwrap === false or !is_numeric( $wordwrap ) ) {
77 $this->_dumpWordWrap = 40;
78 } else {
79 $this->_dumpWordWrap = $wordwrap;
80 }
81
82 // New YAML document
83 $string = "---\n";
84
85 // Start at the base of the array and move through it.
86 foreach ( $array as $key => $value ) {
87 $string .= $this->_yamlize( $key, $value, 0 );
88 }
89 return $string;
90 }
91
92 /**** Private Properties ****/
93
94 /**
95 * Unused variables, but just commented rather than deleting
96 * to save altering the library
97 private $_haveRefs;
98 private $_allNodes;
99 private $_lastIndent;
100 private $_lastNode;
101 private $_inBlock;
102 private $_isInline;
103 **/
104 private $_dumpIndent;
105 private $_dumpWordWrap;
106
107 /**** Private Methods ****/
108
109 /**
110 * Attempts to convert a key / value array item to YAML
111 *
112 * @param $key Mixed: the name of the key
113 * @param $value Mixed: the value of the item
114 * @param $indent Integer: the indent of the current node
115 * @return String
116 */
117 private function _yamlize( $key, $value, $indent ) {
118 if ( is_array( $value ) ) {
119 // It has children. What to do?
120 // Make it the right kind of item
121 $string = $this->_dumpNode( $key, null, $indent );
122 // Add the indent
123 $indent += $this->_dumpIndent;
124 // Yamlize the array
125 $string .= $this->_yamlizeArray( $value, $indent );
126 } elseif ( !is_array( $value ) ) {
127 // It doesn't have children. Yip.
128 $string = $this->_dumpNode( $key, $value, $indent );
129 }
130 return $string;
131 }
132
133 /**
134 * Attempts to convert an array to YAML
135 *
136 * @param $array Array: the array you want to convert
137 * @param $indent Integer: the indent of the current level
138 * @return String
139 */
140 private function _yamlizeArray( $array, $indent ) {
141 if ( is_array( $array ) ) {
142 $string = '';
143 foreach ( $array as $key => $value ) {
144 $string .= $this->_yamlize( $key, $value, $indent );
145 }
146 return $string;
147 } else {
148 return false;
149 }
150 }
151
152 /**
153 * Find out whether a string needs to be output as a literal rather than in plain style.
154 * Added by Roan Kattouw 13-03-2008
155 *
156 * @param $value String: the string to check
157 * @return Boolean
158 */
159 function _needLiteral( $value ) {
160 // Check whether the string contains # or : or begins with any of:
161 // [ - ? , [ ] { } ! * & | > ' " % @ ` ]
162 // or is a number or contains newlines
163 return (bool)( gettype( $value ) == "string" &&
164 ( is_numeric( $value ) ||
165 strpos( $value, "\n" ) ||
166 preg_match( "/[#:]/", $value ) ||
167 preg_match( "/^[-?,[\]{}!*&|>'\"%@`]/", $value ) ) );
168 }
169
170 /**
171 * Returns YAML from a key and a value
172 *
173 * @param $key Mixed: the name of the key
174 * @param $value Mixed: the value of the item
175 * @param $indent Integer: the indent of the current node
176 * @return String
177 */
178 private function _dumpNode( $key, $value, $indent ) {
179 // do some folding here, for blocks
180 if ( $this->_needLiteral( $value ) ) {
181 $value = $this->_doLiteralBlock( $value, $indent );
182 } else {
183 $value = $this->_doFolding( $value, $indent );
184 }
185
186 $spaces = str_repeat( ' ', $indent );
187
188 if ( is_int( $key ) ) {
189 // It's a sequence
190 if ( $value !== '' && !is_null( $value ) )
191 $string = $spaces . '- ' . $value . "\n";
192 else
193 $string = $spaces . "-\n";
194 } else {
195 if ( $key == '*' ) // bug 21922 - Quote asterix used as keys
196 $key = "'*'";
197
198 // It's mapped
199 if ( $value !== '' && !is_null( $value ) )
200 $string = $spaces . $key . ': ' . $value . "\n";
201 else
202 $string = $spaces . $key . ":\n";
203 }
204 return $string;
205 }
206
207 /**
208 * Creates a literal block for dumping
209 *
210 * @param $value String
211 * @param $indent Integer: the value of the indent
212 * @return String
213 */
214 private function _doLiteralBlock( $value, $indent ) {
215 $exploded = explode( "\n", $value );
216 $newValue = '|-';
217 $indent += $this->_dumpIndent;
218 $spaces = str_repeat( ' ', $indent );
219 foreach ( $exploded as $line ) {
220 $newValue .= "\n" . $spaces . trim( $line );
221 }
222 return $newValue;
223 }
224
225 /**
226 * Folds a string of text, if necessary
227 *
228 * @param $value String: the string you wish to fold
229 * @param $indent Integer: the indent of the current node
230 * @return String
231 */
232 private function _doFolding( $value, $indent ) {
233 // Don't do anything if wordwrap is set to 0
234 if ( $this->_dumpWordWrap === 0 ) {
235 return $value;
236 }
237
238 if ( strlen( $value ) > $this->_dumpWordWrap ) {
239 $indent += $this->_dumpIndent;
240 $indent = str_repeat( ' ', $indent );
241 $wrapped = wordwrap( $value, $this->_dumpWordWrap, "\n$indent" );
242 $value = ">-\n" . $indent . $wrapped;
243 }
244 return $value;
245 }
246 }