protected function processForm()
 {
     if (0 < count(QubitOaiRepository::getByURI($this->form->getValue('uri')))) {
         $this->request->setAttribute('preExistingRepository', true);
         $this->forward('qtOaiPlugin', 'harvesterNewRepository');
     }
     $oaiSimple = simplexml_load_file("{$this->form->getValue('uri')}?verb=Identify");
     libxml_use_internal_errors(true);
     if ($oaiSimple) {
         $repository = new QubitOaiRepository();
         $Identify = $oaiSimple->Identify;
         $repository->setName($Identify->repositoryName);
         $repository->setUri($this->form->getValue('uri'));
         $repository->setAdminEmail($Identify->adminEmail);
         $repository->setEarliestTimestamp($Identify->earliestDatestamp);
         $repository->save();
         $harvest = new QubitOaiHarvest();
         $harvest->setOaiRepository($repository);
         $harvest->setMetadataPrefix('oai_dc');
         $harvest->save();
         $this->redirect(array('module' => 'qtOaiPlugin', 'action' => 'harvesterNewRepository'));
     } else {
         $this->request->setAttribute('parsingErrors', true);
         $this->forward('qtOaiPlugin', 'harvesterNewRepository');
     }
 }
 /**
  * Executes action
  *
  * @param sfRequest $request A request object
  */
 public function execute($request)
 {
     //Set resumption token to null so that we enter in the harvester loop
     $resumptionToken = 1;
     //Set no records match to false to start with
     $this->noRecordsMatch = false;
     //Keep track of number of records harvested
     $this->recordCount = 0;
     //If the request did not go through the proper routing, forward to 404
     if (!isset($request->id)) {
         $this->forward404();
     }
     $harvestInfo = QubitOaiHarvest::getById($request->id);
     $harvestInfo->setLastHarvestAttempt(QubitOai::getDate());
     $harvestInfo->save();
     $rep = $harvestInfo->getOaiRepository();
     //If repository was not found 404
     if (!$rep) {
         $this->forward404();
     }
     $this->repositoryName = $rep->getName();
     //Initialise $oaiSimpleRes
     $verb = '';
     $oaiSimpleRes = array();
     $from = date('Y-m-d\\TH:i:s\\Z', strtotime($harvestInfo->getLastHarvest()));
     $until = gmdate('Y-m-d\\TH:i:s\\Z');
     //Create the base request
     $verb = 'verb=ListRecords';
     if ($harvestInfo->getLastHarvest() != null) {
         $verb .= '&from=' . $from;
     }
     $verb .= '&until=' . $until;
     $verb .= '&metadataPrefix=' . $harvestInfo->getMetadataPrefix();
     //Add the set parameter if supplied
     if ($harvestInfo->getSet() != null) {
         $verb .= '&set=' . $harvestInfo->getSet();
     }
     while ($resumptionToken) {
         //Load XML through simplexml http
         $oaiSimple = simplexml_load_file($rep->getUri() . '?' . $verb);
         //Strip oai header, construct array of records
         $oaiSimple->registerXPathNamespace('c', 'http://www.openarchives.org/OAI/2.0/');
         if ($oaiSimple->xpath('//c:error')) {
             $oaiReceivedError = $oaiSimple->xpath('//c:error');
             $oaiReceivedErrorAttr = $oaiReceivedError[0]->attributes();
             if ($oaiReceivedErrorAttr['code'] == 'noRecordsMatch') {
                 $this->noRecordsMatch = true;
             } else {
                 $this->forward404();
             }
         }
         if (!$this->noRecordsMatch) {
             //Container for xml import errors
             $this->errorsFound = array();
             $this->errorsXML = array();
             //Create header and footer for XML record for it to validate
             $oaiHeader = '<?xml version="1.0" encoding="UTF-8" ?>';
             $oaiFooter = '';
             $oaiRecords = $oaiSimple->xpath('//c:ListRecords/c:record');
             foreach ($oaiRecords as $oaiRec) {
                 $oaiRec = $oaiHeader . $oaiRec->asXML() . $oaiFooter . "\n";
                 $options = array();
                 $options = $options['strictXmlParsing'] = false;
                 $importer = new QubitXmlImport();
                 $importer->import($oaiRec, $options);
                 //          $importer = QubitXmlImport::execute($oaiRec, $options);
                 if ($importer->hasErrors()) {
                     $this->errorsFound[] = $importer->getErrors();
                     $this->errorsXML[] = $oaiRec;
                 }
             }
             // Increment record count to keep track of number of records harvested
             $this->recordCount += count($oaiRecords);
             $nbrErrors = count($this->errorsFound);
             $errorReport = '';
             $errorReportHTML = '';
             for ($i = 0; $i < $nbrErrors; $i++) {
                 $errorReport .= "Error when importing record:\n\n" . $this->errorsXML[$i] . "\n\nError message:\n" . $this->errorsFound[$i];
                 $errorReport .= "\n**************************************************\n\n";
                 $errorReportHTML .= "Error when importing record:\n\n <br>" . $this->errorsXML[$i] . "\n\n <br> Error message: <br> \n" . $this->errorsFound[$i];
             }
         }
         //Check for resumption token which will also be the while loop indicator
         $oaiResumptionToken = $oaiSimple->xpath('//c:ListRecords/c:resumptionToken');
         if ($oaiResumptionToken == false || count($oaiResumptionToken) > 1) {
             $resumptionToken = false;
         } else {
             $resumptionToken = $oaiResumptionToken[0];
             $verb = 'verb=ListRecords&resumptionToken=' . $resumptionToken;
         }
     }
     // Update last harvest date
     $harvestInfo->setLastHarvest($until);
     $harvestInfo->save();
 }
 public static function getoaiHarvestsById($id, array $options = array())
 {
     $criteria = new Criteria();
     self::addoaiHarvestsCriteriaById($criteria, $id);
     return QubitOaiHarvest::get($criteria, $options);
 }
  </thead>
  <tbody>
  <?php 
    foreach ($repositories as $rep) {
        ?>
    <tr>
      <td>
        <div>
          <a href="<?php 
        echo $rep->getUri();
        ?>
?verb=Identify"><?php 
        echo $rep->getName();
        ?>
</a><br>Last Harvest: <?php 
        $harvest = QubitOaiHarvest::getLastHarvestByID($rep->id);
        echo $harvest->getLastHarvest();
        ?>
        </div>
      </td><td>
      <?php 
        foreach ($rep->getOaiHarvests() as $harvestJob) {
            ?>
        <?php 
            echo link_to(__('Harvest'), array('module' => 'qtOaiPlugin', 'action' => 'harvesterHarvest', 'id' => $harvestJob->id));
            ?>
      <?php 
        }
        ?>
      </td><td>
        <?php