Index: trunk/extensions/ExternalData/ED_ParserFunctions.php |
— | — | @@ -0,0 +1,100 @@ |
| 2 | +<?php |
| 3 | + |
| 4 | +if ( !defined( 'MEDIAWIKI' ) ) { |
| 5 | + die( 'This file is a MediaWiki extension; it is not a valid entry point' ); |
| 6 | +} |
| 7 | + |
| 8 | +/** |
| 9 | + * Class for handling the parser functions for External Data |
| 10 | + * |
| 11 | + * @author Yaron Koren |
| 12 | + */ |
| 13 | +class EDParserFunctions { |
| 14 | + |
| 15 | + // XML-handling functions based on code found at |
| 16 | + // http://us.php.net/xml_set_element_handler |
| 17 | + static function startElement( $parser, $name, $attrs ) { |
| 18 | + global $edgCurrentXMLTag; |
| 19 | + // set to all lowercase to avoid casing issues |
| 20 | + $edgCurrentXMLTag = strtolower($name); |
| 21 | + } |
| 22 | + |
| 23 | + static function endElement( $parser, $name ) { |
| 24 | + global $edgCurrentXMLTag; |
| 25 | + $edgCurrentXMLTag = ""; |
| 26 | + } |
| 27 | + |
| 28 | + static function getContent ( $parser, $content ) { |
| 29 | + global $edgCurrentXMLTag, $edgXMLValues; |
| 30 | + $edgXMLValues[$edgCurrentXMLTag] = $content; |
| 31 | + } |
| 32 | + |
| 33 | + static function getXMLData ( $xml ) { |
| 34 | + global $edgXMLValues; |
| 35 | + $edgXMLValues = array(); |
| 36 | + |
| 37 | + $xml_parser = xml_parser_create(); |
| 38 | + xml_set_element_handler( $xml_parser, "EDParserFunctions::startElement", "EDParserFunctions::endElement" ); |
| 39 | + xml_set_character_data_handler( $xml_parser, "EDParserFunctions::getContent" ); |
| 40 | + if (!xml_parse($xml_parser, $xml, true)) { |
| 41 | + die(sprintf("XML error: %s at line %d", |
| 42 | + xml_error_string(xml_get_error_code($xml_parser)), |
| 43 | + xml_get_current_line_number($xml_parser))); |
| 44 | + } |
| 45 | + xml_parser_free( $xml_parser ); |
| 46 | + return $edgXMLValues; |
| 47 | + } |
| 48 | + |
| 49 | + static function getCSVData ( $csv ) { |
| 50 | + // regular expression copied from http://us.php.net/fgetcsv |
| 51 | + $csv_vals = preg_split('/,(?=(?:[^\"]*\"[^\"]*\")*(?![^\"]*\"))/', $csv); |
| 52 | + // start with a null value so that the real values start with |
| 53 | + // an index of 1 instead of 0 |
| 54 | + $values = array(null); |
| 55 | + foreach ( $csv_vals as $csv_val ) { |
| 56 | + $values[] = trim( $csv_val, '"' ); |
| 57 | + } |
| 58 | + return $values; |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Render the #get_external_data parser function |
| 63 | + */ |
| 64 | + static function doGetExternalData( &$parser ) { |
| 65 | + global $edgValues; |
| 66 | + $params = func_get_args(); |
| 67 | + array_shift( $params ); // we already know the $parser ... |
| 68 | + $url = array_shift( $params ); |
| 69 | + $url_contents = file_get_contents( $url ); |
| 70 | + $format = array_shift( $params ); |
| 71 | + $external_values = array(); |
| 72 | + if ($format == 'xml') { |
| 73 | + $external_values = self::getXMLData( $url_contents ); |
| 74 | + } elseif ($format == 'csv') { |
| 75 | + $external_values = self::getCSVData( $url_contents ); |
| 76 | + } |
| 77 | + // for each external variable name specified in the function |
| 78 | + // call, get its value (if one exists), and attach it to the |
| 79 | + // local variable name |
| 80 | + foreach ($params as $param) { |
| 81 | + list( $local_var, $external_var ) = explode( '=', $param ); |
| 82 | + // set to all lowercase to avoid casing issues |
| 83 | + $external_var = strtolower( $external_var ); |
| 84 | + if ( array_key_exists( $external_var, $external_values ) ) |
| 85 | + $edgValues[$local_var] = $external_values[$external_var]; |
| 86 | + } |
| 87 | + |
| 88 | + return ''; |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Render the #external_value parser function |
| 93 | + */ |
| 94 | + static function doExternalValue( &$parser, $local_var = '' ) { |
| 95 | + global $edgValues; |
| 96 | + if ( array_key_exists( $local_var, $edgValues) ) |
| 97 | + return $edgValues[$local_var]; |
| 98 | + else |
| 99 | + return ''; |
| 100 | + } |
| 101 | +} |
Index: trunk/extensions/ExternalData/README |
— | — | @@ -0,0 +1,40 @@ |
| 2 | +External Data extension |
| 3 | + |
| 4 | + Version 0.1 |
| 5 | + Yaron Koren |
| 6 | + |
| 7 | +This is free software licensed under the GNU General Public License. Please |
| 8 | +see http://www.gnu.org/copyleft/gpl.html for further details, including the |
| 9 | +full text and terms of the license. |
| 10 | + |
| 11 | +== Overview == |
| 12 | + |
| 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: |
| 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. |
| 19 | + |
| 20 | +#external_value displays the value of any such variable. |
| 21 | + |
| 22 | +For more information, see the extension homepage at: |
| 23 | +http://www.mediawiki.org/wiki/Extension:External_Data |
| 24 | + |
| 25 | +== Requirements == |
| 26 | + |
| 27 | +This version of the External Data extension requires MediaWiki 1.8 or higher. |
| 28 | + |
| 29 | +== Installation == |
| 30 | + |
| 31 | +To install the extension, place the entire 'ExternalData' directory |
| 32 | +within your MediaWiki 'extensions' directory, then add the following |
| 33 | +line to your 'LocalSettings.php' file: |
| 34 | + |
| 35 | + require_once( "$IP/extensions/ExternalData/ED_Settings.php" ); |
| 36 | + |
| 37 | +== Contact == |
| 38 | + |
| 39 | +Comments, questions, suggestions and bug reports are welcome, and can |
| 40 | +be placed on the Talk page for the extension, or sent to Yaron at |
| 41 | +yaron57@gmail.com. |
Index: trunk/extensions/ExternalData/ED_Settings.php |
— | — | @@ -0,0 +1,53 @@ |
| 2 | +<?php |
| 3 | +/** |
| 4 | + * Initialization file for the External Data extension |
| 5 | + * |
| 6 | + * @file |
| 7 | + * @ingroup ExternalData |
| 8 | + * @author Yaron Koren |
| 9 | + */ |
| 10 | + |
| 11 | +if (!defined('MEDIAWIKI')) die(); |
| 12 | + |
| 13 | +$wgExtensionCredits['parserhook'][]= array( |
| 14 | + 'name' => 'External Data', |
| 15 | + 'version' => '0.1', |
| 16 | + 'author' => 'Yaron Koren', |
| 17 | + 'url' => 'http://www.mediawiki.org/wiki/Extension:External_Data', |
| 18 | + 'description' => 'Allows creating variables from an external XML or CSV file', |
| 19 | +); |
| 20 | + |
| 21 | +$wgExtensionFunctions[] = 'edgParserFunctions'; |
| 22 | +$wgHooks['LanguageGetMagic'][] = 'edgLanguageGetMagic'; |
| 23 | + |
| 24 | +$edgIP = $IP . '/extensions/ExternalData'; |
| 25 | +$wgAutoloadClasses['EDParserFunctions'] = $edgIP . '/ED_ParserFunctions.php'; |
| 26 | + |
| 27 | +$edgValues = array(); |
| 28 | + |
| 29 | +function edgParserFunctions() { |
| 30 | + global $wgHooks, $wgParser; |
| 31 | + if( defined( 'MW_SUPPORTS_PARSERFIRSTCALLINIT' ) ) { |
| 32 | + $wgHooks['ParserFirstCallInit'][] = 'edgRegisterParser'; |
| 33 | + } else { |
| 34 | + if ( class_exists( 'StubObject' ) && !StubObject::isRealObject( $wgParser ) ) { |
| 35 | + $wgParser->_unstub(); |
| 36 | + } |
| 37 | + edgRegisterParser( $wgParser ); |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +function edgRegisterParser(&$parser) { |
| 42 | + $parser->setFunctionHook( 'get_external_data', array('EDParserFunctions','doGetExternalData') ); |
| 43 | + $parser->setFunctionHook( 'external_value', array('EDParserFunctions','doExternalValue') ); |
| 44 | + return true; // always return true, in order not to stop MW's hook processing! |
| 45 | +} |
| 46 | + |
| 47 | +function edgLanguageGetMagic( &$magicWords, $langCode = "en" ) { |
| 48 | + switch ( $langCode ) { |
| 49 | + default: |
| 50 | + $magicWords['get_external_data'] = array ( 0, 'get_external_data' ); |
| 51 | + $magicWords['external_value'] = array ( 0, 'external_value' ); |
| 52 | + } |
| 53 | + return true; |
| 54 | +} |