Index: trunk/extensions/ExternalData/README |
— | — | @@ -1,7 +1,7 @@ |
2 | 2 | External Data extension |
3 | 3 | |
4 | | - Version 0.6.1 |
5 | | - Yaron Koren and Michael Dale |
| 4 | + Version 0.7 |
| 5 | + Yaron Koren, Michael Dale and David Macdonald |
6 | 6 | |
7 | 7 | This is free software licensed under the GNU General Public License. Please |
8 | 8 | see http://www.gnu.org/copyleft/gpl.html for further details, including the |
— | — | @@ -10,14 +10,21 @@ |
11 | 11 | == Overview == |
12 | 12 | |
13 | 13 | External Data is an extension to MediaWiki that allows for retrieving data |
14 | | -in CSV, JSON and XML formats from both external URLs and local wiki pages. |
| 14 | +from various sources: external URLs and local wiki pages (in CSV, JSON and |
| 15 | +XML formats), database tables, and LDAP servers |
15 | 16 | |
16 | | -The extension defines three parser functions, #get_external_data, |
17 | | -#external_value and #for_external_table: |
| 17 | +The extension defines five parser functions - #get_external_data, |
| 18 | +#get_db_data, #get_ldap_data, #external_value and #for_external_table: |
18 | 19 | |
19 | 20 | #get_external_data retrieves the data from a URL that holds XML, CSV or |
20 | 21 | JSON, and assigns it to local variables or arrays. |
21 | 22 | |
| 23 | +#get_db_data retrieves data from a database, using a SQL query, and assigns |
| 24 | +it to local variables or arrays. |
| 25 | + |
| 26 | +#get_ldap_data retrieves data from an LDAP server and assigns it to |
| 27 | +local variables. |
| 28 | + |
22 | 29 | #external_value displays the value of any retrieved variable, or the |
23 | 30 | first value if it's an array. |
24 | 31 | |
— | — | @@ -49,11 +56,15 @@ |
50 | 57 | |
51 | 58 | $edgCacheTable = 'ed_url_cache'; |
52 | 59 | |
53 | | -Finally, you can also set for string replacements to be done on the URLs you |
54 | | -call, for instance to hide API keys: |
| 60 | +You can also set for string replacements to be done on the URLs you call, |
| 61 | +for instance to hide API keys: |
55 | 62 | |
56 | 63 | $edgStringReplacements['MY_API_KEY'] = 'abcd1324'; |
57 | 64 | |
| 65 | +Finally, to use the database or LDAP retrieval capabilities, you need to |
| 66 | +set connection settings as well - see the online documentation for more |
| 67 | +information. |
| 68 | + |
58 | 69 | == Contact == |
59 | 70 | |
60 | 71 | Comments, questions, suggestions and bug reports are welcome, and can |
Index: trunk/extensions/ExternalData/ExternalData.php |
— | — | @@ -12,8 +12,8 @@ |
13 | 13 | $wgExtensionCredits['parserhook'][]= array( |
14 | 14 | 'path' => __FILE__, |
15 | 15 | 'name' => 'External Data', |
16 | | - 'version' => '0.6.1', |
17 | | - 'author' => array( 'Yaron Koren', 'Michael Dale' ), |
| 16 | + 'version' => '0.7', |
| 17 | + 'author' => array( 'Yaron Koren', 'Michael Dale', 'David Macdonald' ), |
18 | 18 | 'url' => 'http://www.mediawiki.org/wiki/Extension:External_Data', |
19 | 19 | 'description' => 'Allows for retrieving data in CSV, JSON and XML formats from both external URLs and local wiki pages', |
20 | 20 | 'descriptionmsg' => 'externaldata-desc', |
— | — | @@ -49,6 +49,8 @@ |
50 | 50 | |
51 | 51 | function edgRegisterParser(&$parser) { |
52 | 52 | $parser->setFunctionHook( 'get_external_data', array('EDParserFunctions','doGetExternalData') ); |
| 53 | + $parser->setFunctionHook( 'get_ldap_data', array('EDParserFunctions','doGetLDAPData') ); |
| 54 | + $parser->setFunctionHook( 'get_db_data', array('EDParserFunctions','doGetDBData') ); |
53 | 55 | $parser->setFunctionHook( 'external_value', array('EDParserFunctions','doExternalValue') ); |
54 | 56 | $parser->setFunctionHook( 'for_external_table', array('EDParserFunctions','doForExternalTable') ); |
55 | 57 | |
— | — | @@ -59,6 +61,8 @@ |
60 | 62 | switch ( $langCode ) { |
61 | 63 | default: |
62 | 64 | $magicWords['get_external_data'] = array ( 0, 'get_external_data' ); |
| 65 | + $magicWords['get_ldap_data'] = array ( 0, 'get_ldap_data' ); |
| 66 | + $magicWords['get_db_data'] = array ( 0, 'get_db_data' ); |
63 | 67 | $magicWords['external_value'] = array ( 0, 'external_value' ); |
64 | 68 | $magicWords['for_external_table'] = array ( 0, 'for_external_table' ); |
65 | 69 | } |
Index: trunk/extensions/ExternalData/ED_ParserFunctions.php |
— | — | @@ -102,7 +102,70 @@ |
103 | 103 | return; |
104 | 104 | } |
105 | 105 | |
| 106 | + /** |
| 107 | + * Render the #get_ldap_data parser function |
| 108 | + */ |
| 109 | + static function doGetLDAPData( &$parser ) { |
| 110 | + global $wgTitle, $edgCurPageName, $edgValues; |
| 111 | + |
| 112 | + // if we're handling multiple pages, reset $edgValues |
| 113 | + // when we move from one page to another |
| 114 | + $cur_page_name = $wgTitle->getText(); |
| 115 | + if (! isset($edgCurPageName) || $edgCurPageName != $cur_page_name) { |
| 116 | + $edgValues = array(); |
| 117 | + $edgCurPageName = $cur_page_name; |
| 118 | + } |
| 119 | + |
| 120 | + $params = func_get_args(); |
| 121 | + array_shift( $params ); // we already know the $parser ... |
| 122 | + $args = EDUtils::parseParams($params); // parse params into name-value pairs |
| 123 | + $mappings = EDUtils::parseMappings($args['data']); // parse the data arg into mappings |
| 124 | + |
| 125 | + $external_values = EDUtils::getLDAPData( $args['filter'], $args['domain'], array_values($mappings) ); |
| 126 | + |
| 127 | + // Build $edgValues |
| 128 | + foreach ( $mappings as $local_var => $external_var ) { |
| 129 | + $edgValues[$local_var][] = $external_values[0][$external_var][0]; |
| 130 | + } |
| 131 | + return; |
| 132 | + } |
| 133 | + |
106 | 134 | /** |
| 135 | + * Render the #get_db_data parser function |
| 136 | + */ |
| 137 | + static function doGetDBData( &$parser ) { |
| 138 | + global $wgTitle, $edgCurPageName, $edgValues; |
| 139 | + |
| 140 | + // if we're handling multiple pages, reset $edgValues |
| 141 | + // when we move from one page to another |
| 142 | + $cur_page_name = $wgTitle->getText(); |
| 143 | + if (! isset($edgCurPageName) || $edgCurPageName != $cur_page_name) { |
| 144 | + $edgValues = array(); |
| 145 | + $edgCurPageName = $cur_page_name; |
| 146 | + } |
| 147 | + |
| 148 | + $params = func_get_args(); |
| 149 | + array_shift( $params ); // we already know the $parser ... |
| 150 | + $args = EDUtils::parseParams($params); // parse params into name-value pairs |
| 151 | + $mappings = EDUtils::parseMappings($args['data']); // parse the data arg into mappings |
| 152 | + |
| 153 | + $external_values = EDUtils::getDBData( $args['server'], $args['from'], $args['where'], array_values($mappings) ); |
| 154 | + // handle error cases |
| 155 | + if (is_null($external_values)) |
| 156 | + return; |
| 157 | + |
| 158 | + // Build $edgValues |
| 159 | + foreach ( $mappings as $local_var => $external_var ) { |
| 160 | + if ( array_key_exists( $external_var, $external_values ) ) { |
| 161 | + foreach ($external_values[$external_var] as $value) { |
| 162 | + $edgValues[$local_var][] = $value; |
| 163 | + } |
| 164 | + } |
| 165 | + } |
| 166 | + return; |
| 167 | + } |
| 168 | + |
| 169 | + /** |
107 | 170 | * Get the specified index of the array for the specified local |
108 | 171 | * variable retrieved by #get_external_data |
109 | 172 | */ |