"fixed not working plugin cache (on tree level): callback has to be public, as it's get called static by an other class (egw_cache)"

This commit is contained in:
Ralf Becker 2010-04-03 10:31:57 +00:00
parent b47b6ed7ff
commit 80a1fb5a53

View File

@ -15,9 +15,11 @@
* use importexport_helper_functions::method * use importexport_helper_functions::method
*/ */
class importexport_helper_functions { class importexport_helper_functions {
// Plugins are scanned and cached for all instances using this source path /**
const CACHE_EXPIRATION = 86400; * Plugins are scanned and cached for all instances using this source path for given time (in seconds)
*/
const CACHE_EXPIRATION = 3600;
/** /**
* Files known to cause problems, and will be skipped in a plugin scan * Files known to cause problems, and will be skipped in a plugin scan
@ -31,10 +33,10 @@ class importexport_helper_functions {
/** /**
* nothing to construct here, only static functions! * nothing to construct here, only static functions!
*/ */
/** /**
* Converts a custom time string to to unix timestamp * Converts a custom time string to to unix timestamp
* The format of the time string is given by the argument $_format * The format of the time string is given by the argument $_format
* which takes the same parameters as the php date() function. * which takes the same parameters as the php date() function.
* *
* @abstract supportet formatstrings: d,m,y,Y,H,h,i,s,O,a,A * @abstract supportet formatstrings: d,m,y,Y,H,h,i,s,O,a,A
@ -48,7 +50,7 @@ class importexport_helper_functions {
$fparams = explode( ',', chunk_split( $_format, 1, ',' ) ); $fparams = explode( ',', chunk_split( $_format, 1, ',' ) );
$spos = 0; $spos = 0;
foreach ( $fparams as $fparam ) { foreach ( $fparams as $fparam ) {
switch ( $fparam ) { switch ( $fparam ) {
case 'd': (int)$day = substr( $_string, $spos, 2 ); $spos += 2; break; case 'd': (int)$day = substr( $_string, $spos, 2 ); $spos += 2; break;
case 'm': (int)$mon = substr( $_string, $spos, 2 ); $spos += 2; break; case 'm': (int)$mon = substr( $_string, $spos, 2 ); $spos += 2; break;
@ -64,10 +66,10 @@ class importexport_helper_functions {
default: $spos++; // seperator default: $spos++; // seperator
} }
} }
print_debug("hour:$hour; min:$min; sec:$sec; mon:$mon; day:$day; year:$year;\n"); print_debug("hour:$hour; min:$min; sec:$sec; mon:$mon; day:$day; year:$year;\n");
$timestamp = mktime($hour, $min, $sec, $mon, $day, $year, $_is_dst); $timestamp = mktime($hour, $min, $sec, $mon, $day, $year, $_is_dst);
// offset given? // offset given?
if ( isset( $offset ) && strlen( $offset == 5 ) ) { if ( isset( $offset ) && strlen( $offset == 5 ) ) {
$operator = $offset{0}; $operator = $offset{0};
@ -92,7 +94,7 @@ class importexport_helper_functions {
} }
} }
return is_array( $_account_lids ) ? $account_ids : implode( ',', (array)$account_ids ); return is_array( $_account_lids ) ? $account_ids : implode( ',', (array)$account_ids );
} // end of member function account_lid2id } // end of member function account_lid2id
/** /**
@ -110,19 +112,17 @@ class importexport_helper_functions {
} }
return is_array( $_account_id ) ? $account_lids : implode( ',', (array)$account_lids ); return is_array( $_account_id ) ? $account_lids : implode( ',', (array)$account_lids );
} // end of member function account_id2lid } // end of member function account_id2lid
/** /**
* converts cat_id to a cat_name * converts cat_id to a cat_name
* *
* @param mixed _cat_ids comma seperated list or array * @param mixed _cat_ids comma seperated list or array
* @return mixed comma seperated list or array with cat_names * @return mixed comma seperated list or array with cat_names
*/ */
public static function cat_id2name( $_cat_ids ) { public static function cat_id2name( $_cat_ids ) {
$cats = &CreateObject( 'phpgwapi.categories' );
$cat_ids = is_array( $_cat_ids ) ? $_cat_ids : explode( ',', $_cat_ids ); $cat_ids = is_array( $_cat_ids ) ? $_cat_ids : explode( ',', $_cat_ids );
foreach ( $cat_ids as $cat_id ) { foreach ( $cat_ids as $cat_id ) {
$cat_names[] = $cats->id2name( (int)$cat_id ); $cat_names[] = categories::id2name( (int)$cat_id );
} }
return is_array( $_cat_ids ) ? $cat_names : implode(',',(array)$cat_names); return is_array( $_cat_ids ) ? $cat_names : implode(',',(array)$cat_names);
} // end of member function category_id2name } // end of member function category_id2name
@ -135,52 +135,51 @@ class importexport_helper_functions {
* @return mixed comma seperated list or array with cat_ids * @return mixed comma seperated list or array with cat_ids
*/ */
public static function cat_name2id( $_cat_names ) { public static function cat_name2id( $_cat_names ) {
$cats = CreateObject( 'phpgwapi.categories' ); $cats = new categories(); // uses current user and app (egw_info[flags][currentapp])
$cats->app_name = $GLOBALS['egw_info']['flags']['currentapp'];
$cat_names = is_array( $_cat_names ) ? $_cat_names : explode( ',', $_cat_names ); $cat_names = is_array( $_cat_names ) ? $_cat_names : explode( ',', $_cat_names );
foreach ( $cat_names as $cat_name ) { foreach ( $cat_names as $cat_name ) {
if ( $cat_name == '' ) continue; if ( $cat_name == '' ) continue;
if ( ( $cat_id = $cats->name2id( $cat_name ) ) == 0 ) { if ( ( $cat_id = $cats->name2id( $cat_name ) ) == 0 ) {
$cat_id = $cats->add( array( $cat_id = $cats->add( array(
'name' => $cat_name, 'name' => $cat_name,
'access' => 'public', 'access' => 'public',
'descr' => $cat_name. ' ('. lang('Automatically created by importexport'). ')' 'descr' => $cat_name. ' ('. lang('Automatically created by importexport'). ')'
)); ));
} }
$cat_ids[] = $cat_id; $cat_ids[] = $cat_id;
} }
return is_array( $_cat_names ) ? $cat_ids : implode( ',', (array)$cat_ids ); return is_array( $_cat_names ) ? $cat_ids : implode( ',', (array)$cat_ids );
} // end of member function category_name2id } // end of member function category_name2id
/** /**
* conversion * conversion
* *
* Conversions enable you to change / adapt the content of each _record field for your needs. * Conversions enable you to change / adapt the content of each _record field for your needs.
* General syntax is: pattern1 |> replacement1 || ... || patternN |> replacementN * General syntax is: pattern1 |> replacement1 || ... || patternN |> replacementN
* If the pattern-part of a pair is ommited it will match everything ('^.*$'), which * If the pattern-part of a pair is ommited it will match everything ('^.*$'), which
* is only usefull for the last pair, as they are worked from left to right. * is only usefull for the last pair, as they are worked from left to right.
* Example: 1|>private||public * Example: 1|>private||public
* This will translate a '1' in the _record field to 'privat' and everything else to 'public'. * This will translate a '1' in the _record field to 'privat' and everything else to 'public'.
* *
* In addintion to the fields assign by the pattern of the reg.exp. * In addintion to the fields assign by the pattern of the reg.exp.
* you can use all other _record fields, with the syntax |[FIELDINDEX]. * you can use all other _record fields, with the syntax |[FIELDINDEX].
* Example: * Example:
* Your record is: * Your record is:
* array( 0 => Company, 1 => NFamily, 2 => NGiven * array( 0 => Company, 1 => NFamily, 2 => NGiven
* Your conversion string for field 0 (Company): * Your conversion string for field 0 (Company):
* .+|>|[0]: |[1], |[2]|||[1], |[2] * .+|>|[0]: |[1], |[2]|||[1], |[2]
* This constructs something like * This constructs something like
* Company: FamilyName, GivenName or FamilyName, GivenName if 'Company' is empty. * Company: FamilyName, GivenName or FamilyName, GivenName if 'Company' is empty.
* *
* Moreover the two helper function cat() and account() can be used. * Moreover the two helper function cat() and account() can be used.
* cat(Cat1,...,CatN) returns a (','-separated) list with the cat_id's. If a * cat(Cat1,...,CatN) returns a (','-separated) list with the cat_id's. If a
* category isn't found, it will be automaticaly added. * category isn't found, it will be automaticaly added.
* *
* Patterns as well as the replacement can be regular expressions (the replacement is done * Patterns as well as the replacement can be regular expressions (the replacement is done
* via ereg_replace). * via ereg_replace).
* *
* @param array _record reference with record to do the conversion with * @param array _record reference with record to do the conversion with
* @param array _conversion array with conversion description * @param array _conversion array with conversion description
* @param object &$cclass calling class to process the '@ evals' (not impelmeted yet) * @param object &$cclass calling class to process the '@ evals' (not impelmeted yet)
@ -188,7 +187,7 @@ class importexport_helper_functions {
*/ */
public static function conversion( $_record, $_conversion, &$_cclass = null ) { public static function conversion( $_record, $_conversion, &$_cclass = null ) {
if (empty( $_conversion ) ) return $_record; if (empty( $_conversion ) ) return $_record;
$PSep = '||'; // Pattern-Separator, separats the pattern-replacement-pairs in conversion $PSep = '||'; // Pattern-Separator, separats the pattern-replacement-pairs in conversion
$ASep = '|>'; // Assignment-Separator, separats pattern and replacesment $ASep = '|>'; // Assignment-Separator, separats pattern and replacesment
$CPre = '|['; $CPos = ']'; // |[_record-idx] is expanded to the corespondig value $CPre = '|['; $CPos = ']'; // |[_record-idx] is expanded to the corespondig value
@ -199,7 +198,7 @@ class importexport_helper_functions {
foreach ( $_conversion as $idx => $conversion_string ) { foreach ( $_conversion as $idx => $conversion_string ) {
if ( empty( $conversion_string ) ) continue; if ( empty( $conversion_string ) ) continue;
// fetch patterns ($rvalues) // fetch patterns ($rvalues)
$pat_reps = explode( $PSep, stripslashes( $conversion_string ) ); $pat_reps = explode( $PSep, stripslashes( $conversion_string ) );
foreach( $pat_reps as $k => $pat_rep ) { foreach( $pat_reps as $k => $pat_rep ) {
@ -209,25 +208,25 @@ class importexport_helper_functions {
} }
$rvalues[$pattern] = $replace; // replace two with only one, added by the form $rvalues[$pattern] = $replace; // replace two with only one, added by the form
} }
// conversion list may be longer than $_record aka (no_csv) // conversion list may be longer than $_record aka (no_csv)
$val = array_key_exists( $idx, $_record ) ? $_record[$idx] : ''; $val = array_key_exists( $idx, $_record ) ? $_record[$idx] : '';
foreach ( $rvalues as $pattern => $replace ) { foreach ( $rvalues as $pattern => $replace ) {
if( ereg( (string)$pattern, $val) ) { if( ereg( (string)$pattern, $val) ) {
$val = ereg_replace( (string)$pattern, $replace, (string)$val ); $val = ereg_replace( (string)$pattern, $replace, (string)$val );
$reg = '\|\[([a-zA-Z_0-9]+)\]'; $reg = '\|\[([a-zA-Z_0-9]+)\]';
while( ereg( $reg, $val, $vars ) ) { while( ereg( $reg, $val, $vars ) ) {
// expand all _record fields // expand all _record fields
$val = str_replace( $val = str_replace(
$CPre . $vars[1] . $CPos, $CPre . $vars[1] . $CPos,
$_record[array_search($vars[1], array_keys($_record))], $_record[array_search($vars[1], array_keys($_record))],
$val $val
); );
} }
$val = preg_replace_callback( "/(cat|account|strtotime)\(([^)]*)\)/i", array( self, 'c2_dispatcher') , $val ); $val = preg_replace_callback( "/(cat|account|strtotime)\(([^)]*)\)/i", array( self, 'c2_dispatcher') , $val );
} }
} }
@ -238,10 +237,10 @@ class importexport_helper_functions {
} }
return $_record; return $_record;
} // end of member function conversion } // end of member function conversion
/** /**
* callback for preg_replace_callback from self::conversion. * callback for preg_replace_callback from self::conversion.
* This function gets called when 2nd level conversions are made, * This function gets called when 2nd level conversions are made,
* like the cat() and account() statements in the conversions. * like the cat() and account() statements in the conversions.
* *
* @param array $_matches * @param array $_matches
@ -249,7 +248,7 @@ class importexport_helper_functions {
private static function c2_dispatcher( $_matches ) { private static function c2_dispatcher( $_matches ) {
$action = &$_matches[1]; // cat or account ... $action = &$_matches[1]; // cat or account ...
$data = &$_matches[2]; // datas for action $data = &$_matches[2]; // datas for action
switch ( $action ) { switch ( $action ) {
case 'strtotime' : case 'strtotime' :
list( $string, $format ) = explode( ',', $data ); list( $string, $format ) = explode( ',', $data );
@ -259,40 +258,36 @@ class importexport_helper_functions {
return self::$method( $data ); return self::$method( $data );
} }
} }
private static function strclean( $_matches ) { private static function strclean( $_matches ) {
switch( $_matches[1] ) { switch( $_matches[1] ) {
case '|T{' : return trim( $_matches[2] ); case '|T{' : return trim( $_matches[2] );
case '|TC{' : return trim( preg_replace( '/[\x01-\x1F]+/', '', $_matches[2] ) ); case '|TC{' : return trim( preg_replace( '/[\x01-\x1F]+/', '', $_matches[2] ) );
case '|TCnCL{' : return trim( preg_replace( '/[\x01-\x09\x11\x12\x14-\x1F]+/', '', $_matches[2] ) ); case '|TCnCL{' : return trim( preg_replace( '/[\x01-\x09\x11\x12\x14-\x1F]+/', '', $_matches[2] ) );
case '|INE{' : return preg_match( '/\^.+\^/', $_matches[2] ) ? $_matches[2] : ''; case '|INE{' : return preg_match( '/\^.+\^/', $_matches[2] ) ? $_matches[2] : '';
default: default:
throw new Exception('Error in conversion string! "'. substr( $_matches[1], 0, -1 ). '" is not valid!'); throw new Exception('Error in conversion string! "'. substr( $_matches[1], 0, -1 ). '" is not valid!');
} }
} }
/** /**
* returns a list of importexport plugins * returns a list of importexport plugins
* *
* @param string $_tpye {import | export | all} * @param string $_tpye {import | export | all}
* @param string $_appname {<appname> | all} * @param string $_appname {<appname> | all}
* @return array(<appname> => array( <type> => array(<plugin> => <title>))) * @return array(<appname> => array( <type> => array(<plugin> => <title>)))
*/ */
public static function get_plugins( $_appname = 'all', $_type = 'all' ) { public static function get_plugins( $_appname = 'all', $_type = 'all' ) {
/*
$plugins = egw_cache::getTree( $plugins = egw_cache::getTree(
__CLASS__, __CLASS__,
'plugins', 'plugins',
array('importexport_helper_functions','_get_plugins'), array('importexport_helper_functions','_get_plugins'),
array(array_keys($GLOBALS['egw_info']['apps']), array('import', 'export')), array(array_keys($GLOBALS['egw_info']['apps']), array('import', 'export')),
self::CACHE_EXPIRATION self::CACHE_EXPIRATION
); );
*/
$plugins = self::_get_plugins(array_keys($GLOBALS['egw_info']['apps']), array('import', 'export'));
$appnames = $_appname == 'all' ? array_keys($GLOBALS['egw_info']['apps']) : (array)$_appname; $appnames = $_appname == 'all' ? array_keys($GLOBALS['egw_info']['apps']) : (array)$_appname;
$types = $_type == 'all' ? array('import','export') : (array)$_type; $types = $_type == 'all' ? array('import','export') : (array)$_type;
foreach($plugins as $appname => $types) { foreach($plugins as $appname => $types) {
if(!in_array($appname, $appnames)) unset($plugins['appname']); if(!in_array($appname, $appnames)) unset($plugins['appname']);
} }
@ -302,7 +297,7 @@ class importexport_helper_functions {
return $plugins; return $plugins;
} }
protected static function _get_plugins(Array $appnames, Array $types) { public static function _get_plugins(Array $appnames, Array $types) {
$plugins = array(); $plugins = array();
foreach ($appnames as $appname) { foreach ($appnames as $appname) {
if(array_key_exists($appname, self::$blacklist_files) && self::$blacklist_files[$appname] === true) continue; if(array_key_exists($appname, self::$blacklist_files) && self::$blacklist_files[$appname] === true) continue;
@ -310,7 +305,7 @@ class importexport_helper_functions {
$appdir = EGW_INCLUDE_ROOT. "/$appname/inc"; $appdir = EGW_INCLUDE_ROOT. "/$appname/inc";
if(!is_dir($appdir)) continue; if(!is_dir($appdir)) continue;
$d = dir($appdir); $d = dir($appdir);
// step through each file in appdir // step through each file in appdir
while (false !== ($entry = $d->read())) { while (false !== ($entry = $d->read())) {
// Blacklisted? // Blacklisted?
@ -318,12 +313,12 @@ class importexport_helper_functions {
list( ,$classname, ,$extension) = explode('.',$entry); list( ,$classname, ,$extension) = explode('.',$entry);
$file = $appdir. '/'. $entry; $file = $appdir. '/'. $entry;
foreach ($types as $type) { foreach ($types as $type) {
if( !is_file($file) || strpos($entry, $type) === false || $extension != 'php' ) continue; if( !is_file($file) || strpos($entry, $type) === false || $extension != 'php' ) continue;
require_once($file); require_once($file);
$reflectionClass = new ReflectionClass($classname); $reflectionClass = new ReflectionClass($classname);
if($reflectionClass->IsInstantiable() && if($reflectionClass->IsInstantiable() &&
$reflectionClass->implementsInterface('importexport_iface_'.$type.'_plugin')) { $reflectionClass->implementsInterface('importexport_iface_'.$type.'_plugin')) {
try { try {
$plugin_object = new $classname; $plugin_object = new $classname;
@ -339,9 +334,9 @@ class importexport_helper_functions {
$d->close(); $d->close();
} }
//error_log(__CLASS__.__FUNCTION__.print_r($plugins,true)); //error_log(__CLASS__.__FUNCTION__.print_r($plugins,true));
return $plugins; return $plugins;
} }
/** /**
* returns list of apps which have plugins of given type. * returns list of apps which have plugins of given type.
* *
@ -353,7 +348,6 @@ class importexport_helper_functions {
} }
public static function guess_filetype( $_file ) { public static function guess_filetype( $_file ) {
} }
} // end of importexport_helper_functions } // end of importexport_helper_functions
?>