r45731 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r45730‎ | r45731 | r45732 >
Date:16:55, 14 January 2009
Author:yaron
Status:deferred
Tags:
Comment:
Version 0.2 - handling of JSON files added
Modified paths:
  • /trunk/extensions/ExternalData/ED_ParserFunctions.php (modified) (history)
  • /trunk/extensions/ExternalData/ED_Settings.php (modified) (history)
  • /trunk/extensions/ExternalData/README (modified) (history)

Diff [purge]

Index: trunk/extensions/ExternalData/ED_ParserFunctions.php
@@ -37,7 +37,7 @@
3838 xml_set_element_handler( $xml_parser, "EDParserFunctions::startElement", "EDParserFunctions::endElement" );
3939 xml_set_character_data_handler( $xml_parser, "EDParserFunctions::getContent" );
4040 if (!xml_parse($xml_parser, $xml, true)) {
41 - die(sprintf("XML error: %s at line %d",
 41+ echo(sprintf("XML error: %s at line %d",
4242 xml_error_string(xml_get_error_code($xml_parser)),
4343 xml_get_current_line_number($xml_parser)));
4444 }
@@ -45,17 +45,39 @@
4646 return $edgXMLValues;
4747 }
4848
49 - static function getCSVData ( $csv ) {
 49+ static function getCSVData( $csv ) {
5050 // regular expression copied from http://us.php.net/fgetcsv
5151 $csv_vals = preg_split('/,(?=(?:[^\"]*\"[^\"]*\")*(?![^\"]*\"))/', $csv);
5252 // start with a null value so that the real values start with
5353 // an index of 1 instead of 0
54 - $values = array(null);
 54+ $values = array( null );
5555 foreach ( $csv_vals as $csv_val ) {
5656 $values[] = trim( $csv_val, '"' );
5757 }
5858 return $values;
5959 }
 60+
 61+ /**
 62+ * Recursive function for use by getJSONData()
 63+ */
 64+ static function parseTree( $tree, &$retrieved_values ) {
 65+ foreach ($tree as $key => $val) {
 66+ if (is_array( $val )) {
 67+ self::parseTree( $val, &$retrieved_values );
 68+ } else {
 69+ $retrieved_values[$key] = $val;
 70+ }
 71+ }
 72+ }
 73+
 74+ static function getJSONData( $json ) {
 75+ $json_tree = json_decode($json, true);
 76+ $values = array();
 77+ if ( is_array( $json_tree ) ) {
 78+ self::parseTree( $json_tree, &$values );
 79+ }
 80+ return $values;
 81+ }
6082
6183 /**
6284 * Render the #get_external_data parser function
@@ -72,6 +94,8 @@
7395 $external_values = self::getXMLData( $url_contents );
7496 } elseif ($format == 'csv') {
7597 $external_values = self::getCSVData( $url_contents );
 98+ } elseif ($format == 'json') {
 99+ $external_values = self::getJSONData( $url_contents );
76100 }
77101 // for each external variable name specified in the function
78102 // call, get its value (if one exists), and attach it to the
Index: trunk/extensions/ExternalData/README
@@ -1,6 +1,6 @@
22 External Data extension
33
4 - Version 0.1
 4+ Version 0.2
55 Yaron Koren
66
77 This is free software licensed under the GNU General Public License. Please
@@ -10,11 +10,11 @@
1111 == Overview ==
1212
1313 External Data is an extension to MediaWiki that allows for creating
14 -variables from an external XML or CSV file. It defines two parser functions,
15 -#get_external_data and #external_value:
 14+variables from an external XML, CSV or JSON file. It defines two parser
 15+functions, #get_external_data and #external_value:
1616
17 -#get_external_data retrieves the data from a URL that holds XML or CSV,
18 -and assigns it to variables on the page.
 17+#get_external_data retrieves the data from a URL that holds XML, CSV or
 18+JSON, and assigns it to variables on the page.
1919
2020 #external_value displays the value of any such variable.
2121
Index: trunk/extensions/ExternalData/ED_Settings.php
@@ -11,10 +11,10 @@
1212
1313 $wgExtensionCredits['parserhook'][]= array(
1414 'name' => 'External Data',
15 - 'version' => '0.1',
 15+ 'version' => '0.2',
1616 'author' => 'Yaron Koren',
1717 'url' => 'http://www.mediawiki.org/wiki/Extension:External_Data',
18 - 'description' => 'Allows creating variables from an external XML or CSV file',
 18+ 'description' => 'Allows creating variables from an external XML, CSV or JSON file',
1919 );
2020
2121 $wgExtensionFunctions[] = 'edgParserFunctions';

Status & tagging log