Exemplo n.º 1
0
 /**
  * Save harvested records to disk and return the end date.
  *
  * @param object $records SimpleXML records.
  *
  * @return int
  */
 public function write($records)
 {
     // Array for tracking successfully harvested IDs:
     $harvestedIds = [];
     // Date of most recent record encountered:
     $endDate = 0;
     $this->strategy->beginWrite();
     // Loop through the records:
     foreach ($records as $record) {
         // Die if the record is missing its header:
         if (empty($record->header)) {
             throw new \Exception('Unexpected missing record header.');
         }
         // Get the ID of the current record:
         $id = $this->extractID($record);
         // Save the current record, either as a deleted or as a regular file:
         $attribs = $record->header->attributes();
         if (strtolower($attribs['status']) == 'deleted') {
             $this->strategy->addDeletedRecord($id);
         } else {
             $recordXML = $this->recordFormatter->format($id, $record);
             $this->strategy->addRecord($id, $recordXML);
             $harvestedIds[] = $id;
         }
         // If the current record's date is newer than the previous end date,
         // remember it for future reference:
         $date = $this->normalizeDate($record->header->datestamp);
         if ($date && $date > $endDate) {
             $endDate = $date;
         }
     }
     $this->strategy->endWrite();
     $this->writeHarvestedIdsLog($harvestedIds);
     return $endDate;
 }
Exemplo n.º 2
0
 /**
  * Get the record XML formatter.
  *
  * @param Communicator $communicator Communicator
  * @param array        $settings     Additional settings
  *
  * @return RecordXmlFormatter
  */
 protected function getFormatter(Communicator $communicator, array $settings)
 {
     // Build the formatter:
     $formatter = new RecordXmlFormatter($settings);
     // Load set names if we're going to need them:
     if ($formatter->needsSetNames()) {
         $loader = $this->getSetLoader($communicator, $settings);
         if ($writer = $this->getConsoleWriter($settings)) {
             $loader->setOutputWriter($writer);
         }
         $formatter->setSetNames($loader->getNames());
     }
     return $formatter;
 }
 /**
  * Test exception when metadata is missing.
  *
  * @return void
  *
  * @expectedException        Exception
  * @expectedExceptionMessage Unexpected missing record metadata.
  */
 public function testMissingMetadataException()
 {
     $formatter = new RecordXmlFormatter();
     $formatter->format('foo', simplexml_load_string('<empty />'));
 }