r71769 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r71768‎ | r71769 | r71770 >
Date:23:37, 26 August 2010
Author:reedy
Status:ok (Comments)
Tags:
Comment:
Move ApiFormatYaml_spyc.php to spyc.php as per r71763
Modified paths:
  • /trunk/phase3/includes/AutoLoader.php (modified) (history)
  • /trunk/phase3/includes/api/ApiFormatYaml_spyc.php (deleted) (history)
  • /trunk/phase3/includes/libs/spyc.php (added) (history)

Diff [purge]

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

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r71763Per wikitech-l discussion, it would be kind of cool if we collected our non-M...demon23:22, 26 August 2010

Comments

#Comment by Nikerabbit (talk | contribs)   06:23, 27 August 2010

Is this really good candidate for libs? This version is many years old, not done by us, but modified by us into something different from what it was originally.

Status & tagging log