genericppfinterface.php

Go to the documentation of this file.
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         //Setup default file cache
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         //Update default params
00061         $this->m_aDefaults = array_merge_replace($this->m_aDefaults, $params);
00062     }
00063 
00072     function getData(&$provider, $params)
00073     {
00074         //Init vars to be used later
00075         $cacheParams = $providerParams = $runtimeOptions = array();
00076         $data = $isFreshData = false;
00077         $logOffset = count($GLOBALS['PPFLog']);
00078 
00079         //Init params, merging defaults with RT to create final exec params
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         //Make sure the provider is OK with params provided
00088         if($provider->initParams($providerParams))
00089         {
00090             //Check cache. In a cacheUpdateOnly situation we want to force the data to be false so it will be updated.
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                 //Request fresh data from provider
00100                 $data['data'] = $provider->getData();
00101 
00102                 //Did provider exec OK?
00103                 if($data['data'] != false)
00104                 {
00105                     //Cache data
00106                     $isFreshData = true;
00107                     $cacheHandler->save($data['data'], $cacheParams);
00108 
00109                     //Some caches (i.e. SQL) will require a data reload after cache update
00110                     //If we only want to update the cache, not actually get the data then this isn't required
00111                     if($runtimeOptions['cacheUpdateOnly'] == false && $cacheHandler->requiresDataReload())
00112                         $data = $cacheHandler->get($cacheParams);
00113                 }
00114                 else if($runtimeOptions['useFallback'] == true) //Something went wrong with provider so use an old cache if we can
00115                 {
00116                     //Disable lifetime check on cache & try cache again. Last resort to failure
00117                     $cacheParams['noValidityCheck'] = true;
00118                     $data = $cacheHandler->get($cacheParams);
00119 
00120                     //Provider failed and no cache exists that we can use. We have to fail at this point.
00121                     if($data == false)
00122                         $GLOBALS['PPFLog'][] = 'Cache fallback attempted but no valid data exists!';
00123                 }
00124             }
00125         }
00126         else
00127         {
00128             //Bad params to the provider
00129             trigger_error("Bad or incomplete parameters specified for provider", E_USER_ERROR);
00130         }
00131 
00132         //Populate meta data (which will be in addition to the cache meta if the cache was hit)
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 ?>

Generated on Wed Oct 25 03:13:32 2006 for LFSWorldParsingProviderFramework by  doxygen 1.4.6