/** * Harvest all available documents. * * @return string[] Array of MARCXML records * @throws Exception */ public function harvest() { $xml = $this->callXServer(['op' => 'login_request', 'user_name' => $this->username, 'user_password' => $this->password]); $doc = simplexml_load_string($xml); if (isset($doc->login_response->local_error)) { $this->_message("X-Server login failed: \n" . $xml, false, Logger::FATAL); throw new Exception("X-Server login failed"); } if (!isset($doc->login_response->auth)) { $this->_message("Could not find auth information in X-Server login response: \n" . $xml, false, Logger::FATAL); throw new Exception("X-Server login response missing auth information"); } if ((string) $doc->login_response->auth != 'Y') { $this->_message("X-Server login failed for '{$this->username}'", false, Logger::FATAL); throw new Exception("X-Server login failed"); } $session = (string) $doc->login_response->session_id; $xml = $this->callXServer(['op' => 'source_locate_request', 'session_id' => $session, 'locate_command' => $this->query, 'source_full_info_flag' => 'Y']); $style = new DOMDocument(); if ($style->load($this->basePath . '/transformations/strip_namespaces.xsl') === false) { throw new Exception('Could not load ' . $this->basePath . '/transformations/strip_namespaces.xsl'); } $doc = new DOMDocument(); if (!$doc->loadXML($xml)) { $this->_message("Failed to parse X-Server source locate response: \n" . $xml, false, Logger::FATAL); throw new Exception("Failed to parse X-Server source locate response"); } $responseNode = $doc->getElementsByTagName('source_locate_response'); if ($responseNode->length > 0) { $responseNode = $responseNode->item(0)->getElementsByTagName('local_error'); if ($responseNode->length > 0) { $this->_message("X-Server source locate request failed: \n" . $xml, false, Logger::FATAL); throw new Exception("X-Server source locate request failed"); } } $transformation = new XSLTProcessor(); $transformation->importStylesheet($style); $splitter = new FileSplitter($transformation->transformToDoc($doc), '//source_locate_response/source_full_info/record', ''); $records = []; while (!$splitter->getEOF()) { $oaiID = ''; $records[] = $splitter->getNextRecord($oaiID); } return $records; }
/** * Load records into the database from a file * * @param string $source Source id * @param string $files Wildcard pattern of files containing the records * * @throws Exception * @return int Number of records loaded */ public function loadFromFile($source, $files) { $this->loadSourceSettings($source); if (!$this->recordXPath) { $this->log->log('loadFromFile', 'recordXPath not defined', Logger::FATAL); throw new Exception('recordXPath not defined'); } $count = 0; foreach (glob($files) as $file) { $this->log->log('loadFromFile', "Loading records from '{$file}' into '{$source}'"); $data = file_get_contents($file); if ($data === false) { throw new Exception("Could not read file '{$file}'"); } if ($this->pretransformation) { if ($this->verbose) { echo "Executing pretransformation\n"; } $data = $this->pretransform($data); } if ($this->verbose) { echo "Creating FileSplitter\n"; } $splitter = new FileSplitter($data, $this->recordXPath, $this->oaiIDXPath); if ($this->verbose) { echo "Storing records\n"; } while (!$splitter->getEOF()) { $oaiID = ''; $data = $splitter->getNextRecord($oaiID); if ($this->verbose) { echo "Storing a record\n"; } $count += $this->storeRecord($oaiID, false, $data); if ($this->verbose) { echo "Stored records: {$count}\n"; } } $this->log->log('loadFromFile', "{$count} records loaded"); } $this->log->log('loadFromFile', "Total {$count} records loaded"); return $count; }