Location: PHPKode > projects > Taxonomic Search Engine > wrapper.php
<?php


/**
 * @file wrapper.php
 *
 */
 
require_once("utility.php");
 
define('EXACT', 1);
define('CONTAINS', 2);
 

/**
 * @brief Base class for external database wrappers
 *
 */
class Wrapper {
	var $server;
	var $info;
	
	var $authority;
	var $namespace;
	
	var $xml;
	
	var $t0;
	var $t1;
	var $time_used;
		
	// Connection

	function Wrapper ()
	{
		$this->xml = "";
		$this->server = "";
		$this->authority = "";
		$this->namespace = "";
	}
		
	// Is the service alive?
	function IsAlive () {}

	//----------------------------------------------------------------------------
	/**
	 * @brief Search database for a name.
	 *	
	 * @param name The taxon name to search for
	 * @param qualifier Kind of search (default is EXACT)
	 * @param max_results The maximum number of records to return (default is 1)
	 */
	function NameSearch ($name, $qualifier = EXACT, $max_results = 1) 
	{
	}
	
	function Error ($kind, $message)
	{
		$this->xml .= "<error kind=\"$kind\">";
		$this->xml .= $message;
		$this->xml .= "</error>";
	}
	
	/**
	 * @brief Create XML document with XML declaration and root element
	 *
	 */
	function StartXML()
	{
		// Using DOM would be more elegant, but I can't get phpdomxml to work properly
		$this->xml = "<?xml version='1.0' encoding='utf-8'?>\n";
		$this->xml .= "<NameSearchResult authority=\"";
		$this->xml .= $this->authority;
		$this->xml .= "\">\n";
	}
	
	/**
	 * @brief Close root element of XML document
	 *
	 */
	function EndXML()
	{
		$this->xml .= "</NameSearchResult>\n";

	}	
	
	/**
	 * @brief Start the stopwatch to record the time a process takes
	 *
	 */
	function StartTimer()
	{
		$this->t0 = microtime();
	}
	
	/**
	 * @brief Stop the stopwatch to record the time a process takes
	 *
	 */
	function StopTimer()
	{
		$this->t1 = microtime();
		$this->time_used = microtime_diff($this->t0, $this->t1);
		$this->time_used = sprintf("%0.3f", $this->time_used);	
	}
	
	/**
	 * @brief Write the time used in the XML document
	 *
	 * Not all wrappers will use this function, as those that use XSLT
	 * transformations to convert XML will pass the time used as a
	 * parameter.
	 *
	 */
	function ReportTimeUsed()
	{
		$this->xml .= "<searchtime units=\"sec\">$this->time_used</searchtime>\n";
	}
	
	
	function GetDataFromCache ($query, $extension, $id)
	{
		global $config;
		
		$data = "";
		
		if ($config['cache_time'] == 0)
		{
			return $data;
		}
		
		$cache_subdirname = $config['cache_dir'] . "/" . $this->authority . "/" . $query;
		$cache_filename = $cache_subdirname . "/" . $id . "." . $extension;
		
		//echo $cache_filename;

		if (file_exists($cache_filename))
		{
			// How old is it?
			$Diff = time() - filemtime ($cache_filename);
			//echo $Diff, "<br/>";
			
			if ($Diff > $config['cache_time'])
			{
				// Cached file is now too old so delete it
				unlink($cache_filename);
			}
			else
			{
				// Read the cache
				$this->StartTimer();
	
				// Load data from cache
				$cache_file = @fopen($cache_filename, "r") or die("could't open file \"$cache_filename\"");
				$data = @fread($cache_file, filesize ($cache_filename));
				fclose($cache_file);
				
				$this->StopTimer();
	
				//echo "Getting data from cache...";
			}

		}
		return $data;
		
	}
	
	function StoreDataInCache ($query, $extension, $id, $data)
	{
		global $config;
		
		// Don't store if we're not caching
		if ($config['cache_time'] == 0)
		{
			return;
		}

		
		$cache_authority = $config['cache_dir'] . "/" . $this->authority;
		$cache_subdirname = $cache_authority . "/" . $query;
		$cache_filename = $cache_subdirname . "/" . $id . "." . $extension;
		
		//echo "Storing data in cache...";
		
		// Ensure cache subfolder exists for this authority
		if (!file_exists($cache_authority))
		{
			$oldumask = umask(0); 
			mkdir($cache_authority, 0777);
			umask($oldumask);
		}
	
		// Ensure cache query exists
		if (!file_exists($cache_subdirname))
		{
			$oldumask = umask(0); 
			mkdir($cache_subdirname, 0777);
			umask($oldumask);
		}
		// Store data in cache
		$cache_file = @fopen($cache_filename, "w+") or die("could't open file \"$cache_filename\"");
		@fwrite($cache_file, $data);
		fclose($cache_file);
	}
		
	
}

?>
Return current item: Taxonomic Search Engine