. * * ***************************************************************************************************************************************************/ require_once("Variables.php"); require_once("HttpRequest.php"); require_once("XmlArray.php"); class eSummary { var $DB; var $QueryKey; // int - entrez's session query number var $WebEnv; // string - entrez's session ID var $IdList; // array - if you're not using sessions, you'll need to feed in a list of IDs -- this only works if WebEnv is not set var $RetStart; var $RetMax; var $QueryData; var $Identifier; // string - used in place of QueryKey for the cache name as a more perminent form of identification, e.g. a pubmed QueryTranslation var $CacheName; var $Xml; var $Html; var $Error; function eSummary( $DB = 'pubmed', $QueryKey = null, $WebEnv = null, $RetStart = 0, $RetMax = null ) { Global $ncbi_RetMax; $this->DB = $DB; if (isset($QueryKey)) $this->QueryKey = $QueryKey; if (isset($WebEnv)) $this->WebEnv = $WebEnv; if (isset($RetStart)) $this->RetStart = $RetStart; if (isset($RetMax)) $this->RetMax = $RetMax; else $this->RetMax = $ncbi_RetMax; } function Run( $Level = 0 ) { if (empty($this->CacheName)) $this->MakeCacheName(); if (empty($this->QueryData)) $this->MakeQueryData(); $this->GetXmlFile(); if ($Level >= 1) { $this->MakeArray(); if (empty($this->Array)) throw new Exception( "Failed to create an Array from the XML in eSearch->Run->MakeArray calling to XmlArray->eSearch." ); if ($this->CheckArray() == false) throw new Exception( $this->Array['ERROR'] ); } return true; } function MakeCacheName() { Global $ncbi_CachePath; $this->DB = str_replace("/", "", $this->DB); // escape slashes in the DB name for the unlikely event that this is used for filesystem attacks $dir = $ncbi_CachePath . $this->DB; // Make database specific folders if (!file_exists($dir)) mkdir($dir); $dir .= "/eSummary"; if (!file_exists($dir)) mkdir($dir); // Create a file name -- this will be md5 scrambled because otherwise we might end up with excessive file name lengths through using QueryTranslations if (isset($this->Identifier)) $fn = array($this->RetStart, $this->RetMax, $this->Identifier); else $fn = array($this->RetStart, $this->RetMax, $this->WebEnv); $fn = implode("--", $fn); $fn = md5($fn); $dir .= "/" . substr($fn, 0, 1); // Add the first letter of the search term to the folder to prevent dirs getting too big if (!file_exists($dir)) mkdir($dir); $this->CacheName = $dir . "/" . $fn . ".txt"; } function MakeQueryData() { $this->QueryData = array( "db" => "pubmed", "usehistory" => "y", ); if (isset($this->DB)) $this->QueryData['db'] = $this->DB; if (isset($this->WebEnv)) $this->QueryData['WebEnv'] = $this->WebEnv; if (isset($this->QueryKey)) $this->QueryData['query_key'] = $this->QueryKey; if (isset($this->RetStart)) $this->QueryData['retstart'] = $this->RetStart; if (isset($this->RetMax)) $this->QueryData['retmax'] = $this->RetMax; if (isset($this->IdList) and empty($this->WebEnv)) $this->QueryData['id'] = implode(",", $this->IdList); } function GetXmlFile() { try { $Request = new EntrezRequest( "eSummary", $this->CacheName, $this->QueryData ); } catch (Exception $e) { throw new Exception($e->getMessage()); } // Pass on any errors if (!empty($Request->Html)) $this->Html .= $Request->Html; // Pass on any HTML messages if (empty($Request->Xml)) { throw new Exception( "Empty XML file." ); } $this->Xml = $Request->Xml; unset($Request); } function MakeArray() { if (empty($this->Xml)) return false; require_once("XmlArray.php"); $Arr = new XmlArray($this->Xml); $this->Array = $Arr->Array; unset($Arr); return $this->Array; } function CheckArray() { if (isset($this->Array['ERROR'])) { $this->DeleteCache(); return false; } return true; } function DeleteCache() { if (empty($this->CacheName)) $this->MakeCacheName(); if (file_exists($this->CacheName)) { unlink($this->CacheName); return true; } else return false; } } ?>