r86302 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r86301‎ | r86302 | r86303 >
Date:11:42, 18 April 2011
Author:reedy
Status:ok (Comments)
Tags:
Comment:
* (bug 28586) YAML: strings that are the same as boolean literals
* (bug 28591) Update/replace/supplement spyc (YAML parsing library)
* YAML API output is now 1.2 compliant, using JSON as the formatter

YAML 1.2 spec is a JSON subset - "The primary objective of this revision is to bring YAML into compliance with JSON as an official subset. YAML 1.2 is compatible with 1.1 for most practical applications - this is a minor revision." [1] Per discussion with Tim, switch YAML to use the JSON formatter

Was originally going to delete the ApiFormatYaml per Tim, but class needed to keep nicer (and apparent) output in API help page

Hence made subclass ApiFormatJson, minimal method overriding

spyc.php deleted from libs

[1] http://www.yaml.org/spec/1.2/spec.html#id2803629
Modified paths:
  • /trunk/phase3/RELEASE-NOTES (modified) (history)
  • /trunk/phase3/includes/GlobalFunctions.php (modified) (history)
  • /trunk/phase3/includes/MessageCache.php (modified) (history)
  • /trunk/phase3/includes/OutputPage.php (modified) (history)
  • /trunk/phase3/includes/api/ApiFormatYaml.php (modified) (history)
  • /trunk/phase3/includes/libs/spyc.php (deleted) (history)

Diff [purge]

Index: trunk/phase3/includes/GlobalFunctions.php
@@ -716,10 +716,12 @@
717717 * @return string
718718 */
719719 function wfMsgWikiHtml( $key ) {
720 - global $wgOut;
 720+ global $wgMessageCache;
721721 $args = func_get_args();
722722 array_shift( $args );
723 - return wfMsgReplaceArgs( $wgOut->parse( wfMsgGetKey( $key, true ), /* can't be set to false */ true ), $args );
 723+ return wfMsgReplaceArgs(
 724+ $wgMessageCache->parse( wfMsgGetKey( $key, true ), null, /* can't be set to false */ true ),
 725+ $args );
724726 }
725727
726728 /**
@@ -741,7 +743,7 @@
742744 * Behavior for conflicting options (e.g., parse+parseinline) is undefined.
743745 */
744746 function wfMsgExt( $key, $options ) {
745 - global $wgOut;
 747+ global $wgMessageCache;
746748
747749 $args = func_get_args();
748750 array_shift( $args );
@@ -781,9 +783,9 @@
782784 }
783785
784786 if( in_array( 'parse', $options, true ) ) {
785 - $string = $wgOut->parse( $string, true, !$forContent, $langCodeObj );
 787+ $string = $wgMessageCache->parse( $string, null, true, !$forContent, $langCodeObj );
786788 } elseif ( in_array( 'parseinline', $options, true ) ) {
787 - $string = $wgOut->parse( $string, true, !$forContent, $langCodeObj );
 789+ $string = $wgMessageCache->parse( $string, null, true, !$forContent, $langCodeObj );
788790 $m = array();
789791 if( preg_match( '/^<p>(.*)\n?<\/p>\n?$/sU', $string, $m ) ) {
790792 $string = $m[1];
Index: trunk/phase3/includes/MessageCache.php
@@ -102,6 +102,8 @@
103103
104104 /**
105105 * ParserOptions is lazy initialised.
 106+ *
 107+ * @return ParserOptions
106108 */
107109 function getParserOptions() {
108110 if ( !$this->mParserOptions ) {
@@ -220,6 +222,8 @@
221223
222224 /**
223225 * Set the cache to $cache, if it is valid. Otherwise set the cache to false.
 226+ *
 227+ * @return bool
224228 */
225229 function setCache( $cache, $code ) {
226230 if ( isset( $cache['VERSION'] ) && $cache['VERSION'] == MSG_CACHE_VERSION ) {
@@ -734,6 +738,21 @@
735739 return $message;
736740 }
737741
 742+ $parser = $this->getParser();
 743+ if ( $parser ) {
 744+ $popts = $this->getParserOptions();
 745+ $popts->setInterfaceMessage( $interface );
 746+ $popts->setTargetLanguage( $language );
 747+ $popts->setUserLang( $language );
 748+ $message = $parser->transformMsg( $message, $popts, $title );
 749+ }
 750+ return $message;
 751+ }
 752+
 753+ /**
 754+ * @return Parser
 755+ */
 756+ function getParser() {
738757 global $wgParser, $wgParserConf;
739758 if ( !$this->mParser && isset( $wgParser ) ) {
740759 # Do some initialisation so that we don't have to do it twice
@@ -746,16 +765,28 @@
747766 } else {
748767 $this->mParser = clone $wgParser;
749768 }
750 - #wfDebug( __METHOD__ . ": following contents triggered transform: $message\n" );
751769 }
752 - if ( $this->mParser ) {
753 - $popts = $this->getParserOptions();
754 - $popts->setInterfaceMessage( $interface );
 770+ return $this->mParser;
 771+ }
 772+
 773+ /**
 774+ * @param $text string
 775+ * @param $title
 776+ * @param $linestart bool
 777+ * @return ParserOutput
 778+ */
 779+ public function parse( $text, $title = null, $linestart = true, $interface = false, $language = null ) {
 780+ $parser = $this->getParser();
 781+ $popts = $this->getParserOptions();
 782+
 783+ if ( $interface ) {
 784+ $popts->setInterfaceMessage( true );
 785+ }
 786+ if ( $language !== null ) {
755787 $popts->setTargetLanguage( $language );
756 - $popts->setUserLang( $language );
757 - $message = $this->mParser->transformMsg( $message, $popts, $title );
758788 }
759 - return $message;
 789+
 790+ return $parser->parse( $text, $title, $popts, $linestart );
760791 }
761792
762793 function disable() {
Index: trunk/phase3/includes/OutputPage.php
@@ -792,7 +792,7 @@
793793 * @param $t Title object
794794 */
795795 public function setTitle( $t ) {
796 - $this->getContext()->setTitle($t);
 796+ $this->getContext()->setTitle( $t );
797797 }
798798
799799 /**
Index: trunk/phase3/includes/api/ApiFormatYaml.php
@@ -33,20 +33,12 @@
3434 * API YAML output formatter
3535 * @ingroup API
3636 */
37 -class ApiFormatYaml extends ApiFormatBase {
 37+class ApiFormatYaml extends ApiFormatJson {
3838
39 - public function __construct( $main, $format ) {
40 - parent::__construct( $main, $format );
41 - }
42 -
4339 public function getMimeType() {
4440 return 'application/yaml';
4541 }
4642
47 - public function execute() {
48 - $this->printText( Spyc::YAMLDump( $this->getResultData() ) );
49 - }
50 -
5143 public function getDescription() {
5244 return 'Output data in YAML format' . parent::getDescription();
5345 }
Index: trunk/phase3/includes/libs/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 - } else {
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/RELEASE-NOTES
@@ -67,6 +67,7 @@
6868 require_once "$IP/extensions/Math/Math.php";
6969 * $wgProfiler is now a configuration array, see StartProfiler.sample for details
7070 * $wgProfiling has been removed
 71+* The spyc library is now no longer included in phase3
7172
7273 === New features in 1.18 ===
7374 * (bug 8130) Query pages should limit to content namespaces, not just main
@@ -336,6 +337,9 @@
337338 * (bug 27712) add parent_id to list=deletedrevs
338339 * (bug 28455) Add 'toponly' to recentchanges API module
339340 * (bug 26873) API: Add 'toponly' filter in usercontribs module
 341+* (bug 28586) YAML: strings that are the same as boolean literals
 342+* (bug 28591) Update/replace/supplement spyc (YAML parsing library)
 343+* YAML API output is now 1.2 compliant, using JSON as the formatter
340344
341345 === Languages updated in 1.18 ===
342346

Follow-up revisions

RevisionCommit summaryAuthorDate
r86303Revert unintended changes from r86302reedy12:00, 18 April 2011
r88161HipHop build fixes:...tstarling12:25, 15 May 2011
r96049Followup r96045, r96047...reedy21:47, 1 September 2011

Comments

#Comment by Brion VIBBER (talk | contribs)   17:31, 3 May 2011

s/subset/superset/ :) (any JSON document is a valid YAML document, allegedly)

#Comment by Brion VIBBER (talk | contribs)   17:40, 3 May 2011

Just a note -- there are differences in the output between the previous YAML outputter and the JSON outputter, but I think it's actually a bug in the old YAML generator. :)

yaml

query:
  pages:
    -
      pageid: 15580374
      ns: 0


json

	"query": {
		"pages": {
			"15580374": {
				"pageid": 15580374,
				"ns": 0,

The YAML outputter incorrectly turned query.pages from a map into a vector array, probably because it saw integer keys but didn't actually vet them. (Ah PHP, your frankenstein hashmap+array construct strikes again!) It's possible that YAML-using clients that assumed the broken structure may not always work with the new, HOWEVER the new output will actually be the correct output structure. So that's probably good.

Status & tagging log