. * * ***************************************************************************************************************************************************/ require_once("Variables.php"); require_once("HttpRequest.php"); class eFetch { var $DB; var $QueryKey; var $WebEnv; var $Translation; var $RetStart; var $RetMax; var $IdList; var $RetType; var $CacheName; var $QueryData; var $Xml; var $Array; function eFetch( $DB = 'pubmed', $QueryKey = null, $WebEnv = null, $Translation = null, $RetStart = 0, $RetMax = 20, $RetType = 'abstract' ) { $this->DB = $DB; if (isset($QueryKey)) $this->QueryKey = $QueryKey; if (isset($WebEnv)) $this->WebEnv = $WebEnv; $this->Translation = md5($Translation); if (isset($RetStart)) $this->RetStart = $RetStart; if (isset($RetMax)) $this->RetMax = $RetMax; if (isset($RetType)) $this->RetType = $RetType; } function Run($Level = 0) { $this->MakeCacheName(); $this->MakeQueryData(); $this->GetXmlFile(); if ($Level == 1) $this->MakeArray(); } function MakeCacheName() { Global $ncbi_CachePath; $this->CacheName = $ncbi_CachePath . $this->DB; if (!file_exists($this->CacheName)) mkdir($this->CacheName); $this->CacheName .= "/eFetch/"; if (!file_exists($this->CacheName)) mkdir($this->CacheName); if (isset($this->Translation)) { $this->CacheName .= substr($this->Translation, 0, 1) . "/"; if (!file_exists($this->CacheName)) mkdir($this->CacheName); $this->CacheName .= $this->Translation . "," . (isset($this->RetStart) ? $this->RetStart : '') . "," . (isset($this->RetMax) ? $this->RetMax : '') . "," . (isset($this->RetType) ? $this->RetType : '') . ".xml"; } elseif (isset($this->IdList)) { $this->CacheName .= "byid/"; if (!file_exists($this->CacheName)) mkdir($this->CacheName); $this->CacheName .= implode(",", $this->IdList) . "," . (isset($this->RetStart) ? $this->RetStart : '') . "," . (isset($this->RetMax) ? $this->RetMax : '') . "," . (isset($this->RetType) ? $this->RetType : '') . ".xml"; } else throw new Exception( "No WebEnv or IdList specified." ); } function MakeQueryData() { $this->QueryData = array( "db" => $this->DB, "rettype" => $this->RetType, ); if (!empty($this->QueryKey)) $this->QueryData['query_key'] = $this->QueryKey; if (!empty($this->WebEnv)) $this->QueryData['WebEnv'] = $this->WebEnv; if (!empty($this->IdList)) $this->QueryData['id'] = implode(",", $this->IdList); if (!empty($this->RetStart)) $this->QueryData['retstart'] = $this->RetStart; if (!empty($this->RetMax)) $this->QueryData['retmax'] = $this->RetMax; } function GetXmlFile() { try { $Request = new EntrezRequest( "eFetch", $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"); try { $Arr = new XmlArray($this->Xml, true); $this->Array = $Arr->Array; unset($Arr); } catch (Exception $e) { $this->DeleteCache(); // The failure of XmlArray may be because PubMed's servers broke -- delete the cache and we could try it again return false; // If using Run(), this will now cause an exception to be thrown. Applications should catch this and either try Run() again, reload, or give up } return $this->Array; } function DeleteCache() { if (empty($this->CacheName)) $this->MakeCacheName(); if (file_exists($this->CacheName)) unlink($this->CacheName); return true; } } ?>