Index: trunk/extensions/ExternalData/ED_ParserFunctions.php |
— | — | @@ -37,7 +37,7 @@ |
38 | 38 | xml_set_element_handler( $xml_parser, "EDParserFunctions::startElement", "EDParserFunctions::endElement" ); |
39 | 39 | xml_set_character_data_handler( $xml_parser, "EDParserFunctions::getContent" ); |
40 | 40 | 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", |
42 | 42 | xml_error_string(xml_get_error_code($xml_parser)), |
43 | 43 | xml_get_current_line_number($xml_parser))); |
44 | 44 | } |
— | — | @@ -45,17 +45,39 @@ |
46 | 46 | return $edgXMLValues; |
47 | 47 | } |
48 | 48 | |
49 | | - static function getCSVData ( $csv ) { |
| 49 | + static function getCSVData( $csv ) { |
50 | 50 | // regular expression copied from http://us.php.net/fgetcsv |
51 | 51 | $csv_vals = preg_split('/,(?=(?:[^\"]*\"[^\"]*\")*(?![^\"]*\"))/', $csv); |
52 | 52 | // start with a null value so that the real values start with |
53 | 53 | // an index of 1 instead of 0 |
54 | | - $values = array(null); |
| 54 | + $values = array( null ); |
55 | 55 | foreach ( $csv_vals as $csv_val ) { |
56 | 56 | $values[] = trim( $csv_val, '"' ); |
57 | 57 | } |
58 | 58 | return $values; |
59 | 59 | } |
| 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 | + } |
60 | 82 | |
61 | 83 | /** |
62 | 84 | * Render the #get_external_data parser function |
— | — | @@ -72,6 +94,8 @@ |
73 | 95 | $external_values = self::getXMLData( $url_contents ); |
74 | 96 | } elseif ($format == 'csv') { |
75 | 97 | $external_values = self::getCSVData( $url_contents ); |
| 98 | + } elseif ($format == 'json') { |
| 99 | + $external_values = self::getJSONData( $url_contents ); |
76 | 100 | } |
77 | 101 | // for each external variable name specified in the function |
78 | 102 | // call, get its value (if one exists), and attach it to the |
Index: trunk/extensions/ExternalData/README |
— | — | @@ -1,6 +1,6 @@ |
2 | 2 | External Data extension |
3 | 3 | |
4 | | - Version 0.1 |
| 4 | + Version 0.2 |
5 | 5 | Yaron Koren |
6 | 6 | |
7 | 7 | This is free software licensed under the GNU General Public License. Please |
— | — | @@ -10,11 +10,11 @@ |
11 | 11 | == Overview == |
12 | 12 | |
13 | 13 | 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: |
16 | 16 | |
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. |
19 | 19 | |
20 | 20 | #external_value displays the value of any such variable. |
21 | 21 | |
Index: trunk/extensions/ExternalData/ED_Settings.php |
— | — | @@ -11,10 +11,10 @@ |
12 | 12 | |
13 | 13 | $wgExtensionCredits['parserhook'][]= array( |
14 | 14 | 'name' => 'External Data', |
15 | | - 'version' => '0.1', |
| 15 | + 'version' => '0.2', |
16 | 16 | 'author' => 'Yaron Koren', |
17 | 17 | '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', |
19 | 19 | ); |
20 | 20 | |
21 | 21 | $wgExtensionFunctions[] = 'edgParserFunctions'; |