r52165 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r52164‎ | r52165 | r52166 >
Date:15:05, 19 June 2009
Author:yaron
Status:deferred
Tags:
Comment:
Version 0.7 - David Macdonald's additions for accessing database and LDAP data
Modified paths:
  • /trunk/extensions/ExternalData/ED_ParserFunctions.php (modified) (history)
  • /trunk/extensions/ExternalData/ExternalData.php (modified) (history)
  • /trunk/extensions/ExternalData/README (modified) (history)

Diff [purge]

Index: trunk/extensions/ExternalData/README
@@ -1,7 +1,7 @@
22 External Data extension
33
4 - Version 0.6.1
5 - Yaron Koren and Michael Dale
 4+ Version 0.7
 5+ Yaron Koren, Michael Dale and David Macdonald
66
77 This is free software licensed under the GNU General Public License. Please
88 see http://www.gnu.org/copyleft/gpl.html for further details, including the
@@ -10,14 +10,21 @@
1111 == Overview ==
1212
1313 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
1516
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:
1819
1920 #get_external_data retrieves the data from a URL that holds XML, CSV or
2021 JSON, and assigns it to local variables or arrays.
2122
 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+
2229 #external_value displays the value of any retrieved variable, or the
2330 first value if it's an array.
2431
@@ -49,11 +56,15 @@
5057
5158 $edgCacheTable = 'ed_url_cache';
5259
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:
5562
5663 $edgStringReplacements['MY_API_KEY'] = 'abcd1324';
5764
 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+
5869 == Contact ==
5970
6071 Comments, questions, suggestions and bug reports are welcome, and can
Index: trunk/extensions/ExternalData/ExternalData.php
@@ -12,8 +12,8 @@
1313 $wgExtensionCredits['parserhook'][]= array(
1414 'path' => __FILE__,
1515 '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' ),
1818 'url' => 'http://www.mediawiki.org/wiki/Extension:External_Data',
1919 'description' => 'Allows for retrieving data in CSV, JSON and XML formats from both external URLs and local wiki pages',
2020 'descriptionmsg' => 'externaldata-desc',
@@ -49,6 +49,8 @@
5050
5151 function edgRegisterParser(&$parser) {
5252 $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') );
5355 $parser->setFunctionHook( 'external_value', array('EDParserFunctions','doExternalValue') );
5456 $parser->setFunctionHook( 'for_external_table', array('EDParserFunctions','doForExternalTable') );
5557
@@ -59,6 +61,8 @@
6062 switch ( $langCode ) {
6163 default:
6264 $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' );
6367 $magicWords['external_value'] = array ( 0, 'external_value' );
6468 $magicWords['for_external_table'] = array ( 0, 'for_external_table' );
6569 }
Index: trunk/extensions/ExternalData/ED_ParserFunctions.php
@@ -102,7 +102,70 @@
103103 return;
104104 }
105105
 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+
106134 /**
 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+ /**
107170 * Get the specified index of the array for the specified local
108171 * variable retrieved by #get_external_data
109172 */

Status & tagging log