. * * ***************************************************************************************************************************************************/ require_once("Variables.php"); require_once("HttpRequest.php"); require_once("XmlArray.php"); class eSearch { var $DB; var $SearchTerm; var $MinDate; var $MaxDate; var $RelDate; var $RetMax; var $RetType; var $DateType; var $QueryData; var $CacheName; var $Xml; var $Array; var $QueryKey; var $WebEnv; var $Translation; var $Count; var $IdList; var $Html; var $Error; function eSearch( $DB = 'pubmed', $SearchTerm = null, $MinDate = null, $MaxDate = null, $RetMax = 20, $RetType = 'uilist', $DateType = null ) { $this->DB = $DB; if (isset($SearchTerm)) $this->SearchTerm = $SearchTerm; if (isset($MinDate)) $this->MinDate = $MinDate; if (isset($MaxDate)) $this->MaxDate = $MaxDate; if (isset($RetMax)) $this->RetMax = $RetMax; $this->RetType = $RetType; if (isset($DateType)) $this->DateType = $DateType; } function Run( $Level = 0 ) { if (isset($this->Xml)) unset($this->Xml); if (isset($this->Array)) unset($this->Array); $this->MakeCacheName(); $this->MakeQueryData(); // We MUST make the query data fresh on each run, as one can run multiple searches with the same object $this->GetXmlFile(); if ($Level >= 1) { $this->MakeArray(); if (empty($this->Array)) { $this->DeleteCache(); if (isset($this->Error)) throw new Exception( "Failed to create an Array from the XML in eSearch:" . $this->Error ); else throw new Exception( "Failed to create an Array from the XML in eSearch->Run->MakeArray calling to XmlArray->eSearch." ); } } if ($Level > 1) $this->ExtractResults(); 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 .= "/eSearch"; if (!file_exists($dir)) mkdir($dir); $dir .= "/" . strtolower(substr(trim($this->SearchTerm, " /.(\"["), 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 . "/" . (isset($this->DB) ? $this->DB : 'pubmed') . "," . $this->RetType . "," . str_replace("/", "", $this->MinDate) . "," . (isset($this->MaxDate) ? str_replace("/", "", $this->MaxDate) : date('Ymd')) . "," . (isset($this->DateType) ? $this->DateType : '') . (isset($this->RelDate) ? $this->RelDate : '') . "," . str_replace("/", "", $this->SearchTerm) . "," . str_replace("/", "", $this->RetMax) . ",.txt"; } function MakeQueryData() { // Construct the Query Data $this->QueryData = array( "db" => "pubmed", // Default database is pubmed -- overule by setting $this->DB "usehistory" => "y", "term" => $this->SearchTerm ); if (!empty($this->DB)) $this->QueryData['db'] = $this->DB; if (!empty($this->RetMax)) $this->QueryData['retmax'] = $this->RetMax; if (!empty($this->MinDate)) $this->QueryData['mindate'] = $this->MinDate; if (!empty($this->MaxDate)) $this->QueryData['maxdate'] = $this->MaxDate; if (!empty($this->RetType)) $this->QueryData['rettype'] = $this->RetType; if (!empty($this->RelDate)) $this->QueryData['reldate'] = $this->RelDate; if (!empty($this->DateType)) $this->QueryData['datetype'] = $this->DateType; } function GetXmlFile() { try { $Request = new EntrezRequest( "eSearch", $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 $this->Error = $e->getMessage(); // 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 false; } return $this->Array; } function ExtractResults() { // These all need to use 'isset' rather than '!empty' -- e.g. for Counts of 0 if (isset($this->Array['QueryKey'])) $this->QueryKey = $this->Array['QueryKey']; if (isset($this->Array['WebEnv'])) $this->WebEnv = $this->Array['WebEnv']; if (isset($this->Array['Count'])) $this->Count = $this->Array['Count']; if (isset($this->Array['QueryTranslation'])) $this->Translation = str_replace(""", "\"", $this->Array['QueryTranslation']); if (isset($this->Array['IdList'])) $this->IdList = $this->Array['IdList']; } function DeleteCache() { if (empty($this->CacheName)) $this->MakeCacheName(); /* TEMP FIX */ Global $ncbi_WinFiles; if ($ncbi_WinFiles) { // Escape filenames when using windows $this->CacheName = str_replace("\"", "", $this->CacheName); $this->CacheName = str_replace("[", "", $this->CacheName); $this->CacheName = str_replace("]", "", $this->CacheName); } /* END TEMP FIX */ if (file_exists($this->CacheName)) { unlink($this->CacheName); return true; } else return false; } } ?>