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