Fix for r47482: put the RELEASE-NOTES entry in the "bug fixes" section and not the...
[mediawiki.git] / includes / api / ApiFormatYaml_spyc.php
blobf16b2c8a665d0949f3803ef3cbd342aba7fbade7
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 */
11 /**
12 * The Simple PHP YAML Class.
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.
18 * @ingroup API
20 class Spyc {
22 /**
23 * Dump YAML from PHP array statically
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.
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.
33 * Indent's default is 2 spaces, wordwrap's default is 40 characters. And
34 * you can turn off wordwrap by passing in 0.
36 * @return string
37 * @param $array Array: PHP array
38 * @param $indent Integer: Pass in false to use the default, which is 2
39 * @param $wordwrap Integer: Pass in 0 for no wordwrap, false for default (40)
41 public static function YAMLDump($array,$indent = false,$wordwrap = false) {
42 $spyc = new Spyc;
43 return $spyc->dump($array,$indent,$wordwrap);
46 /**
47 * Dump PHP array to YAML
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.
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.
57 * Indent's default is 2 spaces, wordwrap's default is 40 characters. And
58 * you can turn off wordwrap by passing in 0.
60 * @public
61 * @return string
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)
66 function dump($array,$indent = false,$wordwrap = false) {
67 // Dumps to some very clean YAML. We'll have to add some more features
68 // and options soon. And better support for folding.
70 // New features and options.
71 if ($indent === false or !is_numeric($indent)) {
72 $this->_dumpIndent = 2;
73 } else {
74 $this->_dumpIndent = $indent;
77 if ($wordwrap === false or !is_numeric($wordwrap)) {
78 $this->_dumpWordWrap = 40;
79 } else {
80 $this->_dumpWordWrap = $wordwrap;
83 // New YAML document
84 $string = "---\n";
86 // Start at the base of the array and move through it.
87 foreach ($array as $key => $value) {
88 $string .= $this->_yamlize($key,$value,0);
90 return $string;
93 /**** Private Properties ****/
95 private $_haveRefs;
96 private $_allNodes;
97 private $_lastIndent;
98 private $_lastNode;
99 private $_inBlock;
100 private $_isInline;
101 private $_dumpIndent;
102 private $_dumpWordWrap;
104 /**** Private Methods ****/
107 * Attempts to convert a key / value array item to YAML
108 * @return string
109 * @param $key The name of the key
110 * @param $value The value of the item
111 * @param $indent The indent of the current node
113 private function _yamlize($key,$value,$indent) {
114 if (is_array($value)) {
115 // It has children. What to do?
116 // Make it the right kind of item
117 $string = $this->_dumpNode($key,NULL,$indent);
118 // Add the indent
119 $indent += $this->_dumpIndent;
120 // Yamlize the array
121 $string .= $this->_yamlizeArray($value,$indent);
122 } elseif (!is_array($value)) {
123 // It doesn't have children. Yip.
124 $string = $this->_dumpNode($key,$value,$indent);
126 return $string;
130 * Attempts to convert an array to YAML
131 * @return string
132 * @param $array The array you want to convert
133 * @param $indent The indent of the current level
135 private function _yamlizeArray($array,$indent) {
136 if (is_array($array)) {
137 $string = '';
138 foreach ($array as $key => $value) {
139 $string .= $this->_yamlize($key,$value,$indent);
141 return $string;
142 } else {
143 return false;
148 * Find out whether a string needs to be output as a literal rather than in plain style.
149 * Added by Roan Kattouw 13-03-2008
150 * @param $value The string to check
151 * @return bool
153 function _needLiteral($value) {
154 # Check whether the string contains # or : or begins with any of:
155 # [ - ? , [ ] { } ! * & | > ' " % @ ` ]
156 # or is a number or contains newlines
157 return (bool)(gettype($value) == "string" &&
158 (is_numeric($value) ||
159 strpos($value, "\n") ||
160 preg_match("/[#:]/", $value) ||
161 preg_match("/^[-?,[\]{}!*&|>'\"%@`]/", $value)));
166 * Returns YAML from a key and a value
167 * @return string
168 * @param $key The name of the key
169 * @param $value The value of the item
170 * @param $indent The indent of the current node
172 private function _dumpNode($key,$value,$indent) {
173 // do some folding here, for blocks
174 if ($this->_needLiteral($value)) {
175 $value = $this->_doLiteralBlock($value,$indent);
176 } else {
177 $value = $this->_doFolding($value,$indent);
180 $spaces = str_repeat(' ',$indent);
182 if (is_int($key)) {
183 // It's a sequence
184 if ($value !== '' && !is_null($value))
185 $string = $spaces.'- '.$value."\n";
186 else
187 $string = $spaces . "-\n";
188 } else {
189 // It's mapped
190 if ($value !== '' && !is_null($value))
191 $string = $spaces . $key . ': ' . $value . "\n";
192 else
193 $string = $spaces . $key . ":\n";
195 return $string;
199 * Creates a literal block for dumping
200 * @return string
201 * @param $value
202 * @param $indent int The value of the indent
204 private function _doLiteralBlock($value,$indent) {
205 $exploded = explode("\n",$value);
206 $newValue = '|';
207 $indent += $this->_dumpIndent;
208 $spaces = str_repeat(' ',$indent);
209 foreach ($exploded as $line) {
210 $newValue .= "\n" . $spaces . trim($line);
212 return $newValue;
216 * Folds a string of text, if necessary
217 * @return string
218 * @param $value The string you wish to fold
220 private function _doFolding($value,$indent) {
221 // Don't do anything if wordwrap is set to 0
222 if ($this->_dumpWordWrap === 0) {
223 return $value;
226 if (strlen($value) > $this->_dumpWordWrap) {
227 $indent += $this->_dumpIndent;
228 $indent = str_repeat(' ',$indent);
229 $wrapped = wordwrap($value,$this->_dumpWordWrap,"\n$indent");
230 $value = ">\n".$indent.$wrapped;
232 return $value;