r96045 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r96044‎ | r96045 | r96046 >
Date:21:27, 1 September 2011
Author:laner
Status:resolved (Comments)
Tags:
Comment:
Bring back in old YAML class, the JSON class does not work with UserData in EC2-like systems.
Modified paths:
  • /trunk/extensions/OpenStackManager/OpenStackManager.php (modified) (history)
  • /trunk/extensions/OpenStackManager/OpenStackNovaController.php (modified) (history)
  • /trunk/extensions/OpenStackManager/Spyc.php (added) (history)

Diff [purge]

Index: trunk/extensions/OpenStackManager/Spyc.php
@@ -0,0 +1,236 @@
 2+<?php
 3+/**
 4+ * Spyc -- A Simple PHP YAML Class
 5+ * @version 0.2.3 -- 2006-02-04
 6+ * @author Chris Wanstrath <chris@ozmm.org>
 7+ * @see http://spyc.sourceforge.net/
 8+ * @copyright Copyright 2005-2006 Chris Wanstrath
 9+ * @license http://www.opensource.org/licenses/mit-license.php MIT License
 10+ */
 11+
 12+/**
 13+ * The Simple PHP YAML Class.
 14+ *
 15+ * This class can be used to read a YAML file and convert its contents
 16+ * into a PHP array. It currently supports a very limited subsection of
 17+ * the YAML spec.
 18+ *
 19+ * @ingroup API
 20+ */
 21+class Spyc {
 22+
 23+ /**
 24+ * Dump YAML from PHP array statically
 25+ *
 26+ * The dump method, when supplied with an array, will do its best
 27+ * to convert the array into friendly YAML. Pretty simple. Feel free to
 28+ * save the returned string as nothing.yml and pass it around.
 29+ *
 30+ * Oh, and you can decide how big the indent is and what the wordwrap
 31+ * for folding is. Pretty cool -- just pass in 'false' for either if
 32+ * you want to use the default.
 33+ *
 34+ * Indent's default is 2 spaces, wordwrap's default is 40 characters. And
 35+ * you can turn off wordwrap by passing in 0.
 36+ *
 37+ * @return string
 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+ */
 42+ public static function YAMLDump( $array, $indent = false, $wordwrap = false ) {
 43+ $spyc = new Spyc;
 44+ return $spyc->dump( $array, $indent, $wordwrap );
 45+ }
 46+
 47+ /**
 48+ * Dump PHP array to YAML
 49+ *
 50+ * The dump method, when supplied with an array, will do its best
 51+ * to convert the array into friendly YAML. Pretty simple. Feel free to
 52+ * save the returned string as tasteful.yml and pass it around.
 53+ *
 54+ * Oh, and you can decide how big the indent is and what the wordwrap
 55+ * for folding is. Pretty cool -- just pass in 'false' for either if
 56+ * you want to use the default.
 57+ *
 58+ * Indent's default is 2 spaces, wordwrap's default is 40 characters. And
 59+ * you can turn off wordwrap by passing in 0.
 60+ *
 61+ * @public
 62+ * @return string
 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+ */
 67+ 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+ private $_haveRefs;
 97+ private $_allNodes;
 98+ private $_lastIndent;
 99+ private $_lastNode;
 100+ private $_inBlock;
 101+ private $_isInline;
 102+ private $_dumpIndent;
 103+ private $_dumpWordWrap;
 104+
 105+ /**** Private Methods ****/
 106+
 107+ /**
 108+ * Attempts to convert a key / value array item to YAML
 109+ * @return string
 110+ * @param $key The name of the key
 111+ * @param $value The value of the item
 112+ * @param $indent The indent of the current node
 113+ */
 114+ private function _yamlize( $key, $value, $indent ) {
 115+ if ( is_array( $value ) ) {
 116+ // It has children. What to do?
 117+ // Make it the right kind of item
 118+ $string = $this->_dumpNode( $key, null, $indent );
 119+ // Add the indent
 120+ $indent += $this->_dumpIndent;
 121+ // Yamlize the array
 122+ $string .= $this->_yamlizeArray( $value, $indent );
 123+ } elseif ( !is_array( $value ) ) {
 124+ // It doesn't have children. Yip.
 125+ $string = $this->_dumpNode( $key, $value, $indent );
 126+ }
 127+ return $string;
 128+ }
 129+
 130+ /**
 131+ * Attempts to convert an array to YAML
 132+ * @return string
 133+ * @param $array The array you want to convert
 134+ * @param $indent The indent of the current level
 135+ */
 136+ private function _yamlizeArray( $array, $indent ) {
 137+ if ( is_array( $array ) ) {
 138+ $string = '';
 139+ foreach ( $array as $key => $value ) {
 140+ $string .= $this->_yamlize( $key, $value, $indent );
 141+ }
 142+ return $string;
 143+ } else {
 144+ return false;
 145+ }
 146+ }
 147+
 148+ /**
 149+ * Find out whether a string needs to be output as a literal rather than in plain style.
 150+ * Added by Roan Kattouw 13-03-2008
 151+ * @param $value The string to check
 152+ * @return bool
 153+ */
 154+ function _needLiteral( $value ) {
 155+ // Check whether the string contains # or : or begins with any of:
 156+ // [ - ? , [ ] { } ! * & | > ' " % @ ` ]
 157+ // or is a number or contains newlines
 158+ return (bool)( gettype( $value ) == "string" &&
 159+ ( is_numeric( $value ) ||
 160+ strpos( $value, "\n" ) ||
 161+ preg_match( "/[#:]/", $value ) ||
 162+ preg_match( "/^[-?,[\]{}!*&|>'\"%@`]/", $value ) ) );
 163+ }
 164+
 165+ /**
 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
 171+ */
 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 );
 178+ }
 179+
 180+ $spaces = str_repeat( ' ', $indent );
 181+
 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+ if ($key == '*') //bug 21922 - Quote asterix used as keys
 190+ $key = "'*'";
 191+
 192+ // It's mapped
 193+ if ( $value !== '' && !is_null( $value ) )
 194+ $string = $spaces . $key . ': ' . $value . "\n";
 195+ else
 196+ $string = $spaces . $key . ":\n";
 197+ }
 198+ return $string;
 199+ }
 200+
 201+ /**
 202+ * Creates a literal block for dumping
 203+ * @return string
 204+ * @param $value
 205+ * @param $indent int The value of the indent
 206+ */
 207+ private function _doLiteralBlock( $value, $indent ) {
 208+ $exploded = explode( "\n", $value );
 209+ $newValue = '|-';
 210+ $indent += $this->_dumpIndent;
 211+ $spaces = str_repeat( ' ', $indent );
 212+ foreach ( $exploded as $line ) {
 213+ $newValue .= "\n" . $spaces . trim( $line );
 214+ }
 215+ return $newValue;
 216+ }
 217+
 218+ /**
 219+ * Folds a string of text, if necessary
 220+ * @return string
 221+ * @param $value The string you wish to fold
 222+ */
 223+ private function _doFolding( $value, $indent ) {
 224+ // Don't do anything if wordwrap is set to 0
 225+ if ( $this->_dumpWordWrap === 0 ) {
 226+ return $value;
 227+ }
 228+
 229+ if ( strlen( $value ) > $this->_dumpWordWrap ) {
 230+ $indent += $this->_dumpIndent;
 231+ $indent = str_repeat( ' ', $indent );
 232+ $wrapped = wordwrap( $value, $this->_dumpWordWrap, "\n$indent" );
 233+ $value = ">-\n" . $indent . $wrapped;
 234+ }
 235+ return $value;
 236+ }
 237+}
Index: trunk/extensions/OpenStackManager/OpenStackNovaController.php
@@ -241,8 +241,6 @@
242242 function createInstance( $instanceName, $image, $key, $instanceType, $availabilityZone, $groups ) {
243243 global $wgOpenStackManagerInstanceUserData;
244244
245 - # 1, 1 is min and max number of instances to create.
246 - # We never want to make more than one at a time.
247245 $options = array();
248246 if ( $key ) {
249247 $options['KeyName'] = $key;
@@ -260,7 +258,7 @@
261259 $userdata .= $endl;
262260 $userdata .= $boundary;
263261 if ( $wgOpenStackManagerInstanceUserData['cloud-config'] ) {
264 - $userdata .= $endl . $this->getAttachmentMime( FormatJson::encode( $wgOpenStackManagerInstanceUserData['cloud-config'] ), 'text/cloud-config', 'cloud-config.txt' );
 262+ $userdata .= $endl . $this->getAttachmentMime( Spyc::YAMLDump( $wgOpenStackManagerInstanceUserData['cloud-config'] ), 'text/cloud-config', 'cloud-config.txt' );
265263 $userdata .= $endl . $boundary;
266264 }
267265 if ( $wgOpenStackManagerInstanceUserData['scripts'] ) {
@@ -268,7 +266,7 @@
269267 wfSuppressWarnings();
270268 $stat = stat( $script );
271269 wfRestoreWarnings();
272 - if ( $stat ) {
 270+ if ( ! $stat ) {
273271 continue;
274272 }
275273 $scripttext = file_get_contents( $script );
@@ -297,6 +295,8 @@
298296 } elseif ( count( $groups ) == 1 ) {
299297 $options['SecurityGroup'] = $groups[0];
300298 }
 299+ # 1, 1 is min and max number of instances to create.
 300+ # We never want to make more than one at a time.
301301 $response = $this->novaConnection->run_instances( $image, 1, 1, $options );
302302 if ( ! $response->isOK() ) {
303303 return null;
Index: trunk/extensions/OpenStackManager/OpenStackManager.php
@@ -109,6 +109,7 @@
110110 $wgAutoloadClasses['SpecialNova'] = $dir . 'special/SpecialNova.php';
111111 $wgAutoloadClasses['OpenStackNovaHostJob'] = $dir . 'OpenStackNovaHostJob.php';
112112 $wgAutoloadClasses['AmazonEC2'] = $dir . 'aws-sdk/sdk.class.php';
 113+$wgAutoloadClasses['Spyc'] = $dir . 'Spyc.php';
113114 $wgSpecialPages['NovaInstance'] = 'SpecialNovaInstance';
114115 $wgSpecialPageGroups['NovaInstance'] = 'nova';
115116 $wgSpecialPages['NovaKey'] = 'SpecialNovaKey';

Follow-up revisions

RevisionCommit summaryAuthorDate
r96047Kill Spyc.php from r96045...reedy21:44, 1 September 2011
r96049Followup r96045, r96047...reedy21:47, 1 September 2011

Comments

#Comment by Nikerabbit (talk | contribs)   06:32, 2 September 2011

Wow 2006-02-04

Status & tagging log