00001 <?php
00012 require_once(dirname(__FILE__).'/utilities/array_merge_replace.php');
00013
00014 define('PPF_ROOT_DIR', dirname(__FILE__));
00015 define('PPF_DATA_DIR', dirname(__FILE__).'/datacache/');
00016
00017 if(file_exists(dirname(__FILE__).'/PEAR/'))
00018 ini_set('include_path', dirname(__FILE__).'/PEAR/');
00019
00020 if(!is_writable(PPF_DATA_DIR))
00021 trigger_error('PPF data directory ('.PPF_DATA_DIR.') is not writable! Please grant write priveleges.', E_USER_ERROR);
00022
00023 $GLOBALS['PPFLog'] = array();
00024
00029 class GenericPPFInterface
00030 {
00035 var $m_aDefaults = array( 'cacheParams' => array(),
00036 'providerParams' => array(),
00037 'options' => array('useFallback' => true, 'cacheUpdateOnly' => false),
00038 'cache' => null);
00039
00048 function GenericPPFInterface($params=array())
00049 {
00050
00051 if(!isset($params['cache']))
00052 {
00053 if(!class_exists('LFSWorldFileCache'))
00054 include PPF_ROOT_DIR.'/caches/lfsworldfilecache.php';
00055 $dir = (isset($params['cacheDir'])) ? ($params['cacheDir']) : (PPF_DATA_DIR);
00056 $cache = new Cache_lite(array('cacheDir' => $dir, 'lifeTime' => 60, 'automaticSerialization' => true));
00057 $this->m_aDefaults['cache'] = new LFSWorldFileCache($cache);
00058 }
00059
00060
00061 $this->m_aDefaults = array_merge_replace($this->m_aDefaults, $params);
00062 }
00063
00072 function getData(&$provider, $params)
00073 {
00074
00075 $cacheParams = $providerParams = $runtimeOptions = array();
00076 $data = $isFreshData = false;
00077 $logOffset = count($GLOBALS['PPFLog']);
00078
00079
00080 $params = array_merge_replace($this->m_aDefaults, $params);
00081 $cacheParams = array_merge_replace($provider->getSrcMetaData(), $params['cacheParams']);
00082 $providerParams = array_merge_replace($provider->getParams(), $params['providerParams']);
00083 $runtimeOptions = $params['options'];
00084 $cacheHandler =& $params['cache'];
00085 $cacheParams['group'] = 'LFSWPPF';
00086
00087
00088 if($provider->initParams($providerParams))
00089 {
00090
00091 if($runtimeOptions['cacheUpdateOnly'] == false)
00092 {
00093 $cacheParams['key'] = $cacheHandler->createKey($provider->getURL());
00094 $data = $cacheHandler->get($cacheParams);
00095 }
00096
00097 if($data['data'] == false)
00098 {
00099
00100 $data['data'] = $provider->getData();
00101
00102
00103 if($data['data'] != false)
00104 {
00105
00106 $isFreshData = true;
00107 $cacheHandler->save($data['data'], $cacheParams);
00108
00109
00110
00111 if($runtimeOptions['cacheUpdateOnly'] == false && $cacheHandler->requiresDataReload())
00112 $data = $cacheHandler->get($cacheParams);
00113 }
00114 else if($runtimeOptions['useFallback'] == true)
00115 {
00116
00117 $cacheParams['noValidityCheck'] = true;
00118 $data = $cacheHandler->get($cacheParams);
00119
00120
00121 if($data == false)
00122 $GLOBALS['PPFLog'][] = 'Cache fallback attempted but no valid data exists!';
00123 }
00124 }
00125 }
00126 else
00127 {
00128
00129 trigger_error("Bad or incomplete parameters specified for provider", E_USER_ERROR);
00130 }
00131
00132
00133 $data['isFreshData'] = $isFreshData;
00134 $data['errors'] = array_slice($GLOBALS['PPFLog'], $logOffset);
00135 $data['cacheKey'] = $cacheParams['key'];
00136 $data['success'] = (count($data['errors'])==0);
00137
00138 return $data;
00139 }
00140
00148 function setCacheHandler(&$handler)
00149 {
00150 $this->m_aDefaults['cache'] =& $handler;
00151 }
00152
00159 function &getCacheHandler()
00160 {
00161 return $this->m_aDefaults['cache'];
00162 }
00163 }
00164 ?>