/** * Handles the insertion of a new imported record. This should parse the * input file and place the strings appropriately into the translation_miss_log * table and the translation memory. * @param Dataface_Record $record */ public function afterInsert(Dataface_Record $record) { $filePath = $record->getContainerSource('file'); if (!file_exists($filePath)) { throw new Exception("Source file doesn't exist"); } switch ($record->val('file_format')) { case 'CSV': case 'XLS': $translationMemory = null; if ($record->val('target_translation_memory_uuid')) { $translationMemory = XFTranslationMemory::loadTranslationMemoryByUuid($record->val('target_translation_memory_uuid')); } if ($record->val('file_format') === 'XLS') { import('inc/ExcelStringImporter.php'); $importer = new ExcelStringImporter($filePath, $translationMemory); } else { $importer = new CSVStringImporter($filePath, $translationMemory); } $importer->fixEncoding(); $message = 'Import succeeded'; $status = 'COMPLETE'; try { $importer->import(); } catch (Exception $ex) { $message = 'Import failed: ' . $ex->getMessage(); $status = 'FAILED'; } $log = $message . "\r\n" . "Succeeded: " . $importer->succeeded . ", " . "Failed: " . $importer->failed . "\r\n" . "Error Log:\r\n===========\r\n"; foreach ($importer->errors as $row) { $log .= "Import Row: " . implode(",", $row['row']) . "\r\n" . "Error Message: " . $row['message']; } df_q(sprintf("update string_imports \n set \n log='%s', \n status='%s', \n succeeded=%d, \n failed=%d\n where\n string_import_id=%d", addslashes($log), addslashes($status), $importer->succeeded, $importer->failed, $record->val('string_import_id'))); break; default: throw new Exception(sprintf("Unrecognized file format: %s", $record->val('file_format'))); } }
public function import() { $fh = fopen($this->inputFilePath, 'r'); if (!$fh) { throw new Exception(sprintf("Failed to open input file '%s'", $this->inputFilePath)); } $headers = array_flip(fgetcsv($fh, 0, $this->separator)); $required_fields = array('normalized_string', 'normalized_translation_value'); if (!isset($this->targetTranslationMemory)) { $required_fields[] = 'translation_memory_uuid'; } foreach ($required_fields as $f) { if (!array_key_exists($f, $headers)) { throw new Exception(sprintf("Missing required column heading: %s", $f)); } } while (($row = fgetcsv($fh, 0, $this->separator)) !== false) { $string = $row[$headers['normalized_string']]; $translation = $row[$headers['normalized_translation_value']]; $translationMemory = $this->targetTranslationMemory; $tmuuid = $row[$headers['translation_memory_uuid']]; if (!isset($translationMemory)) { $translationMemory = XFTranslationMemory::loadTranslationMemoryByUuid($tmuuid); } if (!isset($translationMemory)) { $this->errors[] = array('row' => $row, 'message' => 'No translation memory assigned.'); $this->failed++; continue; } $strRec = XFTranslationMemory::addString($string, $translationMemory->getSourceLanguage()); $res = df_q(sprintf("select string_id from translation_miss_log where string_id=%d and translation_memory_id=%d", $strRec->val('string_id'), $translationMemory->getRecord()->val('translation_memory_id'))); if (mysql_num_rows($res) == 0) { @mysql_free_result($res); // This string is not in the translation miss log yet. We // will import it now $tlogEntry = new Dataface_Record('translation_miss_log', array()); $nstr = TMTools::normalize($string); $trimStripped = trim(strip_tags($nstr)); if (!$trimStripped) { continue; } if (preg_match('/^[0-9 \\.,%\\$#@\\(\\)\\!\\?\'":\\+=\\-\\/><]*$/', $trimStripped)) { continue; } // If the string is just a number or non-word we just skip it. //$estr = TMTools::normalize(TMTools::encode($nstr, $junk)); // We don't need to encode the string $res = df_q(sprintf("select website_id from websites where translation_memory_id=%d", $translationMemory->getRecord()->val('translation_memory_id'))); if (!$res) { $this->failed++; $this->errors[] = array('row' => $row, 'message' => sprintf("No website found for translation memory %d", $translationMemory->getRecord()->val('translation_memory_id'))); continue; } list($websiteId) = mysql_fetch_row($res); @mysql_free_result($res); $hstr = md5($string); $tlogEntry->setValues(array('http_request_log_id' => null, 'string' => $string, 'normalized_string' => $string, 'encoded_string' => $string, 'string_hash' => $hstr, 'date_inserted' => date('Y-m-d H:i:s'), 'website_id' => $websiteId, 'source_language' => $translationMemory->getSourceLanguage(), 'destination_language' => $translationMemory->getDestinationLanguage(), 'translation_memory_id' => $translationMemory->getRecord()->val('translation_memory_id'), 'string_id' => $strRec->val('string_id'))); $res = $tlogEntry->save(); if (PEAR::isError($res)) { $this->errors[] = array('row' => $row, 'message' => 'Failed to insert translation miss log entry: ' . $res->getMessage()); } } if (@trim($translation)) { try { $translationMemory->setTranslationStatus($string, $translation, XFTranslationMemory::TRANSLATION_APPROVED); } catch (Exception $ex) { $this->failed++; $this->errors[] = array('row' => $row, 'message' => 'Failed to set translation status: ' . $ex->getMessage()); continue; } } else { // No translation provided we don't need to import the translation } $this->succeeded++; } }