コード例 #1
0
ファイル: UpdateChangedPages.php プロジェクト: gtoffoli/swete
 function getTranslationMemory($tmid)
 {
     if (!isset($this->_tms[$tmid])) {
         $this->_tms[$tmid] = XFTranslationMemory::loadTranslationMemoryById($tmid);
     }
     return $this->_tms[$tmid];
 }
コード例 #2
0
 function handle(&$params)
 {
     try {
         $app = Dataface_Application::getInstance();
         $query =& $app->getQuery();
         $jt = Dataface_JavascriptTool::getInstance();
         $jt->import('swete/ui/filter_translations.js');
         $app->addHeadContent('<link rel="stylesheet" type="text/css" href="css/swete/actions/review_translations.css"/>');
         if (!@$query['-recordid']) {
             throw new Exception("No record id was specified");
         }
         $record = df_get_record_by_id($query['-recordid']);
         $job = new SweteJob($record);
         $tm = XFTranslationMemory::loadTranslationMemoryFor($record, $record->val('source_language'), $record->val('destination_language'));
         $translations = $job->getTranslations();
         $template = 'swete/actions/review_translations.html';
         if (@$query['-isDialog']) {
             $template = 'swete/actions/review_translations_dlg.html';
         }
         df_display(array('job' => $job, 'translations' => $translations), $template);
     } catch (Exception $e) {
         if ($e->getCode() == E_USER_ERROR) {
             echo $e->getMessage();
         } else {
             throw $e;
         }
     }
 }
コード例 #3
0
ファイル: websites.php プロジェクト: gtoffoli/swete
 function beforeInsert($record)
 {
     if (!$record->val('translation_memory_id')) {
         require_once 'modules/tm/lib/XFTranslationMemory.php';
         $tm = XFTranslationMemory::createTranslationMemory($record->val('website_name') . ' Dictionary', $record->val('source_language'), $record->val('target_language'));
         $record->setValue('translation_memory_id', $tm->getRecord()->val('translation_memory_id'));
     }
 }
コード例 #4
0
ファイル: string_imports.php プロジェクト: gtoffoli/swete
 /**
  * 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')));
     }
 }
コード例 #5
0
ファイル: PageProcessor.php プロジェクト: gtoffoli/swete
 public function process()
 {
     $this->translationStats = null;
     $this->translationMissLogRecord = null;
     $this->translatedPage = null;
     $proxyWriter = $this->site->getProxyWriter();
     $pageWrapper = $this->page;
     $page = $pageWrapper->getRecord();
     $tmid = null;
     if (!isset($this->translationMemory)) {
         $tmid = $pageWrapper->getTranslationMemoryId(true);
     } else {
         $tmid = $this->translationMemory->getRecord()->val('translation_memory_id');
     }
     $translatedContent = null;
     $untranslatedContent = $page->val('webpage_content');
     if (!trim($untranslatedContent)) {
         // There is nothing to process on this page.
         return;
     }
     if ($tmid or $this->translationMemory) {
         if ($this->translationMemory) {
             $tm = $this->translationMemory;
         } else {
             $tm = $this->getTranslationMemory($tmid);
         }
         if ($tm) {
             if ($tm->getSourceLanguage() != $pageWrapper->getLanguage()) {
                 throw new Exception("Translation memory language does not match the record language.  Translation memory source language is " . $tm->getSourceLanguage() . " but the page language is " . $pageWrapper->getLanguage() . '.');
             }
             $proxyWriter->setTranslationMemory($tm);
             $proxyWriter->setMinTranslationStatus($this->translateMinStatus);
             $translatedContent = $proxyWriter->translateHtml($untranslatedContent, $translationStats, $this->logTranslationMisses);
             $this->translationStats = $translationStats;
             $page->setValues(array('last_translation_memory_applied' => date('Y-m-d H:i:s'), 'last_translation_memory_misses' => $translationStats['misses'], 'last_translation_memory_hits' => $translationStats['matches']));
             // Let's record the strings in this page.
             $res = df_q("delete from webpage_strings where webpage_id='" . addslashes($page->val('webpage_id')) . "'");
             if ($proxyWriter->lastStrings) {
                 //print_r($proxyWriter->lastStrings);exit;
                 $sqlpre = "insert into webpage_strings (webpage_id,string_id) values ";
                 $sql = array();
                 $wpid = $page->val('webpage_id');
                 foreach ($proxyWriter->lastStrings as $str) {
                     if (!trim($str)) {
                         continue;
                     }
                     if (preg_match('/^[^\\w]+$/', trim($str))) {
                         // This is to skip any strings that contain only
                         // non-word characters(e.g. numbers)
                         continue;
                     }
                     $encStr = TMTools::encode($str, $params);
                     $strRec = XFTranslationMemory::addString($encStr, $tm->getSourceLanguage());
                     $sql[] = '(' . $wpid . ',' . $strRec->val('string_id') . ')';
                 }
                 $sql = $sqlpre . implode(',', $sql);
                 df_q($sql);
             }
             $translatedPage = SweteWebpage::loadById($page->val('webpage_id'), $this->site->getDestinationLanguage());
             $translatedPage->getRecord()->setValue('webpage_content', $translatedContent);
             $res = $translatedPage->getRecord()->save();
             if (PEAR::isError($res)) {
                 throw new Exception(mysql_error(df_db()));
             }
             $lastApproved = $translatedPage->getLastVersionWithStatus(SweteWebpage::STATUS_APPROVED);
             if ($lastApproved and $lastApproved->val('webpage_content') == $translatedContent) {
                 $page->setValue('webpage_status', SweteWebpage::STATUS_APPROVED);
             } else {
                 if ($translationStats['matches'] > 0 and $translationStats['misses'] == 0) {
                     // We have perfect matches in what we are supposed to be translating
                     // We are either approving this page or we are marking it pending approval
                     if ($translatedPage->getAutoApprove(true)) {
                         $page->setValue('webpage_status', SweteWebpage::STATUS_APPROVED);
                         $lastApproved = $translatedPage->setStatus(SweteWebpage::STATUS_APPROVED);
                     } else {
                         $page->setValue('webpage_status', SweteWebpage::STATUS_PENDING_APPROVAL);
                     }
                 } else {
                     if ($translationStats['misses'] > 0) {
                         $page->setValue('webpage_status', SweteWebpage::STATUS_CHANGED);
                     } else {
                         $page->setValue('webpage_status', null);
                     }
                 }
             }
             if ($this->logTranslationMisses and @$translationStats['log']) {
                 //print_r($translationStats);exit;
                 foreach ($translationStats['log'] as $str) {
                     $tlogEntry = new Dataface_Record('translation_miss_log', array());
                     $nstr = TMTools::normalize($str);
                     $estr = TMTools::encode($str, $junk);
                     $hstr = md5($estr);
                     $strRec = XFTranslationMemory::findString($estr, $this->site->getSourceLanguage());
                     if (!$strRec) {
                         $strRec = XFTranslationMemory::addString($estr, $this->site->getSourceLanguage());
                     }
                     $tlogEntry->setValues(array('string' => $str, 'normalized_string' => $nstr, 'encoded_string' => $estr, 'string_hash' => $hstr, 'date_inserted' => date('Y-m-d H:i:s'), 'webpage_id' => $page->val('webpage_id'), 'website_id' => $page->val('website_id'), 'source_language' => $this->site->getSourceLanguage(), 'destination_language' => $this->site->getDestinationLanguage(), 'translation_memory_id' => $tmid, 'string_id' => $strRec->val("string_id")));
                     if (isset($this->webpageRefreshLogId)) {
                         $tlogEntry->setValue('webpage_refresh_log_id', $this->webpageRefreshLogId);
                     }
                     if ($this->saveTranslationLogRecord) {
                         $res = $tlogEntry->save();
                         if (PEAR::isError($res)) {
                             //throw new Exception($res->getMessage());
                             // This will throw an error if there is a duplicate... we don't care... we're not interested in duplicates
                         }
                     }
                     $this->translationMissLogRecord = $tlogEntry;
                 }
             }
             if ($this->savePage) {
                 $res = $page->save();
                 if (PEAR::isError($res)) {
                     throw new Exception($res->getMessage());
                 }
             }
         }
     }
 }
コード例 #6
0
ファイル: SweteWebpage.class.php プロジェクト: gtoffoli/swete
 /**
  * @brief Applies translations from a translation memory to the webpage contents.  It returns another SweteWebpage
  * object representing the same webpage but in the destination language of the translation memory.
  * No changes are saved to the database.. you need to save the resulting SweteWebpage to make the changes
  * persist.  
  *
  * Note: If there are no matches in the translation memory, then the resulting object will just contain
  * the source language.  This could be overwriting a previous translation so be very careful before 
  * saving.
  *
  * @param XFTranslationMemory $mem The translation memory to apply to the webpage contents.
  * @param array &$stats Out array of stats from the translation.  This array contains the following
  *		keys:
  *			<ul>
  *				<li><b>misses</b> - The number of strings for which there were no translations in the memory.</li>
  *				<li><b>matches</b> - The number of strings that were replaced with a translation.</li>
  *			</ul>
  * @param int $minStatus The minimum approval status to accept for a translation.
  * @param int $maxStatus The maximum approval status to accept for a translation.
  * @returns SweteWebpage A webpage in the destination language including the translations.
  */
 public function applyTranslationsFromMemory(XFTranslationMemory $mem, &$stats, $minStatus = 3, $maxStatus = 5)
 {
     if ($mem->getSourceLanguage() != $this->getLanguage()) {
         throw new Exception("Translation memory language does not match the record language.");
     }
     import('inc/ProxyWriter.php');
     $proxy = new ProxyWriter();
     $proxy->setTranslationMemory($mem);
     $proxy->setMinTranslationStatus($minStatus);
     $proxy->setMaxTranslationStatus($maxStatus);
     $html = $proxy->translateHtml($this->_rec->val('webpage_content'), $stats);
     $out = $this->getTranslation($mem->getDestinationLanguage());
     $out->getRecord()->setValue('webpage_content', $html);
     return $out;
 }
コード例 #7
0
ファイル: ProxyServer.php プロジェクト: gtoffoli/swete
 /**
  * @brief Handles an HTTP request.  Processes the inputs and returns the 
  * correct output.
  */
 public function handleRequest()
 {
     $this->mark("handleRequest: " . $this->URL);
     $url = $this->URL;
     $siteId = $this->site->getRecord()->val('website_id');
     if (file_exists('sites/' . basename($siteId) . '/Delegate.php')) {
         require_once 'sites/' . basename($siteId) . '/Delegate.php';
         $clazz = 'sites_' . intval($siteId) . '_Delegate';
         if (class_exists($clazz) and method_exists($clazz, 'init')) {
             call_user_func(array($clazz, 'init'));
         }
     }
     $proxyWriter = $this->site->getProxyWriter();
     $proxyWriter->useHtml5Parser = $this->useHtml5Parser;
     $logger = $this->logger;
     $logger->proxyRequestUrl = $url;
     $isPost = strtolower($this->SERVER['REQUEST_METHOD']) === 'post';
     if ($isPost) {
         // We cannot cache post requests.
         // The cacher knows this, but let's make doubly sure.
         Dataface_Application::getInstance()->_conf['nocache'] = 1;
     }
     if (!$isPost and !preg_match('#\\.(ico|ICO|gif|GIF|jpg|JPG|jpeg|JPEG|SWF|swf|css|CSS|png|PNG|pdf|PDF|doc|DOC|svg|SVG|fla|FLA|zip|ZIP|js|JS)$#', $url)) {
         $logger->proxyRequestHeaders = serialize(apache_request_headers());
         $logger->proxyRequestPostVars = serialize($this->POST);
         $logger->proxyRequestMethod = $this->SERVER['REQUEST_METHOD'];
         $logger->settingsSiteId = $this->site->getRecord()->val('settings_site_id');
         $this->mark('Loading webpage');
         $page = $this->site->loadWebpageByProxifiedUrl($url);
         $this->mark('Webpage loaded');
         if ($page) {
             $logger->webpageId = $page->getRecord()->val('webpage_id');
             if (!$page->getRecord()->val('active')) {
                 $logger->webpageNotUsedReason = 'Not active';
             } else {
                 $this->mark('Loading latest version with status ' . $this->pageStatus);
                 $version = $page->getLastVersionWithStatus($this->pageStatus, $this->site->getDestinationLanguage());
                 $this->mark('Version loaded');
                 if ($version) {
                     $logger->webpageVersionId = $version->val('webpage_version_id');
                     //print_r($version->vals());exit;
                     $this->mark('Proxifying html');
                     $out = $this->site->getProxyWriter()->proxifyHtml($version->val('page_content'));
                     $this->mark('Finished proxifying html');
                     $logger->outputContent = $out;
                     $this->header('Content-Length: ' . strlen($out));
                     $this->header('Connection: close');
                     $this->header('Cache-Control: max-age=36000');
                     $this->header('X-SWeTE-Handler: ProxyServer/Static page/v' . $logger->webpageVersionId . '/' . __LINE__);
                     $this->output($out);
                     while (@ob_end_flush()) {
                     }
                     flush();
                     $this->mark('Flushed contents to browser');
                     $logger->outputResponseHeaders = serialize(headers_list());
                     $logger->save();
                     return;
                 }
             }
         }
     }
     //Wasn't found so we try to load the
     $this->mark('Getting the source page');
     if (@$this->inputContent) {
         // The content was provided.. we don't need to try to load it from
         // the source site
         $client = $this->createClientForInputContent();
     } else {
         if ($this->liveCache and $this->liveCache->client) {
             $client = $this->liveCache->client;
         } else {
             $client = $this->getSourcePage();
         }
     }
     $this->mark('Source page loaded');
     //echo "We got the source page.";
     //print_r($client->headers);
     $isHtml = preg_match('/html|xml/', $client->contentType);
     $isJson = (preg_match('/json/', $client->contentType) or $client->content and ($client->content[0] == '{' or $client->content[0] == '['));
     $isCSS = preg_match('/css/', $client->contentType);
     $json = null;
     if ($isJson) {
         $json = json_decode($client->content, true);
         if (isset($json)) {
             $html = $proxyWriter->jsonToHtml($json);
             $isHtml = isset($html);
             if ($isHtml) {
                 $client->content = $html;
             } else {
                 $isJson = false;
             }
         } else {
             $isJson = false;
         }
     }
     $delegate = new ProxyClientPreprocessor($this->site->getRecord()->val('website_id'));
     $delegate->preprocessHeaders($client->headers);
     $headers = $proxyWriter->proxifyHeaders($client->headers, true);
     $locHeaders = preg_grep('/^Location:/i', $headers);
     // Let's see if this should be a passthru
     $translationMode = $delegate->getTranslationMode($client);
     if (!$isHtml and !$isCSS and $translationMode !== ProxyClient::TRANSLATION_MODE_TRANSLATE) {
         //$skip_decoration_phase = true;
         $cacheControlSet = false;
         foreach ($headers as $h) {
             if (preg_match('/^Cache-control:(.*)$/i', $h, $matches)) {
                 // We need to respect caching rules.
                 // If this content is private then we cannot cache it
                 $cacheControlSet = true;
                 if (preg_match('/private|no-store|max-age=0|s-maxage=0/', $matches[1])) {
                     Dataface_Application::getInstance()->_conf['nocache'];
                 }
             }
             $this->header($h);
         }
         $this->header('Content-Length: ' . strlen($client->content));
         $this->header('Connection: close');
         $this->header('X-SWeTE-Handler: ProxyServer Unprocessed/Non-HTML/Non-CSS/' . __LINE__);
         //if ( !$cacheControlSet ) $this->header('Cache-Control: max-age=3600, public');
         $this->output($client->content);
         if (!$this->buffer) {
             while (@ob_end_flush()) {
             }
             flush();
         }
         $this->mark('Flushed non-html content');
         return;
     }
     $stats = array();
     if ($isHtml and $translationMode !== ProxyClient::TRANSLATION_MODE_NOTRANSLATE and !$locHeaders) {
         $this->mark('Preprocessing page content');
         $client->content = $delegate->preprocess($client->content);
         $this->mark('Finished preprocessing');
         $logger->requestDate = date('Y-m-d H:i:s');
         $logger->proxyRequestUrl = $url;
         $logger->proxyRequestHeaders = serialize(apache_request_headers());
         $logger->proxyRequestPostVars = serialize($this->POST);
         $logger->proxyRequestMethod = $this->SERVER['REQUEST_METHOD'];
         $logger->websiteId = $this->site->getRecord()->val('website_id');
         $this->mark('Getting the profile for this page');
         $profile = $this->site->getProfile($proxyWriter->stripBasePath($url));
         $this->mark('Profile retrieved');
         if ($profile and $profile->val('enable_live_translation')) {
             if (isset($this->liveCache)) {
                 $this->liveCache->live = true;
             }
             try {
                 $translation_memory_id = $profile->val('translation_memory_id');
                 if (!$translation_memory_id) {
                     throw new Exception("No translation memory is set up for this page: " . $url);
                 }
                 if (isset($this->liveCache)) {
                     $this->liveCache->translationMemoryId = $translation_memory_id;
                 }
                 require_once 'modules/tm/lib/XFTranslationMemory.php';
                 //$minApprovalLevel = $profile->val('live_translation_min_approval_level');
                 $minApprovalLevel = 3;
                 //XFTranslationMemory::TRANSLATION_APPROVED;
                 $logger->liveTranslationEnabled = 1;
                 $logger->liveTranslationMinStatus = $minApprovalLevel;
                 $this->mark('Loading translation memory: ' . $translation_memory_id);
                 $tm = XFTranslationMemory::loadTranslationMemoryById($translation_memory_id);
                 $this->mark('Translation memory loaded');
                 $logger->translationMemoryId = $translation_memory_id;
                 $proxyWriter->setTranslationMemory($tm);
                 $proxyWriter->setMinTranslationStatus($minApprovalLevel);
                 $this->mark('Translating html');
                 $client->content = $proxyWriter->translateHtml($client->content, $stats, $this->logTranslationMisses);
                 $this->mark('Translation complete');
                 //print_r($stats);exit;
                 $logger->liveTranslationHits = $stats['matches'];
                 $logger->liveTranslationMisses = $stats['misses'];
             } catch (Exception $ex) {
                 error_log($ex->getMessage());
             }
         }
         $this->mark('PROXIFY HTML START');
         $client->content = $proxyWriter->proxifyHtml($client->content);
         $this->mark('PROXIFY HTML END');
         //$client->content = preg_replace('#</head>#', '<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script><script src="http://localhost/sfutheme/js/newclf.js"></script><link rel="stylesheet" type="text/css" href="http://localhost/sfutheme/css/newclf.css"/></head>', $client->content);
         if ($isJson) {
             $client->content = $proxyWriter->htmlToJson($json, $client->content);
         }
     } else {
         if ($isCSS) {
             if (isset($this->liveCache)) {
                 $this->liveCache->live = true;
             }
             $logger->requestLoggingEnabled = false;
             $this->mark('PROXIFY CSS START');
             $client->content = $proxyWriter->proxifyCss($client->content);
             $this->mark('PROXIFY CSS END');
         } else {
             $logger->requestLoggingEnabled = false;
         }
     }
     $cacheControlSet = false;
     foreach ($headers as $h) {
         if (preg_match('/^Cache-control:(.*)$/i', $h, $matches)) {
             // We need to respect caching rules.
             // If this content is private then we cannot cache it
             $cacheControlSet = true;
             if (preg_match('/private|no-store|max-age=0|s-maxage=0/', $matches[1])) {
                 Dataface_Application::getInstance()->_conf['nocache'] = 1;
             }
         }
         //error_log("Setting header: $h");
         $this->header($h);
     }
     $this->header('Content-Length: ' . strlen($client->content));
     $this->header('X-SWeTE-Handler: ProxyServer Live/Processed/' . __LINE__);
     $this->header('Connection: close');
     // We won't add our own cache-control.  We'll let the source site decide this and send
     // their own headers.
     if (!$cacheControlSet and class_exists('Xataface_Scaler') and !@Dataface_Application::getInstance()->_conf['nocache']) {
         //$this->header('Cache-Control: max-age=3600');
     }
     if ($this->inputContentType === 'text/plain') {
         $client->content = trim(strip_tags($client->content));
     }
     $this->output($client->content);
     if (!$this->buffer) {
         while (@ob_end_flush()) {
         }
         flush();
         if (isset($this->liveCache)) {
             $this->mark('The live cache is enabled.  Lets set the content');
             $this->liveCache->siteId = $this->site->getRecord()->val('website_id');
             $this->liveCache->sourceLanguage = $this->site->getSourceLanguage();
             $this->liveCache->proxyLanguage = $this->site->getDestinationLanguage();
             $this->liveCache->proxyUrl = $this->site->getProxyUrl();
             $this->liveCache->siteUrl = $this->site->getSiteUrl();
             $this->liveCache->sourceDateLocale = $this->site->getRecord()->val('source_date_locale');
             $this->liveCache->targetDateLocale = $this->site->getRecord()->val('target_date_locale');
             if ($this->site->getRecord()->val('translation_parser_version')) {
                 $this->liveCache->translationParserVersion = intval($this->site->getRecord()->val('translation_parser_version'));
             }
             $this->liveCache->content = $client->content;
             $this->liveCache->headers = headers_list();
             $this->liveCache->calculateExpires();
             if ($this->logTranslationMisses) {
                 $this->liveCache->skipLiveCache = true;
             } else {
                 $this->liveCache->skipLiveCache = false;
             }
             $this->mark('About to check if resource can be cached.');
             if ($this->liveCache->expires > time()) {
                 $this->mark('Caching resource for live cache');
                 $this->liveCache->save();
                 if (!$this->liveCache->noServerCache) {
                     $this->liveCache->saveContent();
                 }
                 $this->mark('Finished cashing resource for live cache.');
             } else {
                 if ($this->enableProfiling) {
                     $this->mark('Resource cannot be cached with live cache.  Expiry is ' . date($this->liveCache->expires) . ' but now is ' . time() . '.');
                 }
                 $this->mark('Saving just the cache info entry');
                 $this->liveCache->save();
             }
         }
     }
     $this->mark('Content flushed');
     $logger->outputContent = $client->content;
     $logger->outputResponseHeaders = serialize(headers_list());
     $logger->save();
     $this->mark('Loading the translation miss log');
     $tlogEntry = new Dataface_Record('translation_miss_log', array());
     if ($this->logTranslationMisses and @$stats['log']) {
         $this->mark('ITERATING TRANSLATION MISSES START (' . count($stats['log']) . ')');
         foreach ($stats['log'] as $str) {
             $tlogEntry = new Dataface_Record('translation_miss_log', array());
             $nstr = TMTools::normalize($str);
             $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));
             $strRec = XFTranslationMemory::addString($estr, $this->site->getSourceLanguage());
             $hstr = md5($estr);
             $tlogEntry->setValues(array('http_request_log_id' => $logger->getRecord()->val('http_request_log_id'), 'string' => $str, 'normalized_string' => $nstr, 'encoded_string' => $estr, 'string_hash' => $hstr, 'date_inserted' => date('Y-m-d H:i:s'), 'website_id' => $this->site->getRecord()->val('website_id'), 'source_language' => $this->site->getSourceLanguage(), 'destination_language' => $this->site->getDestinationLanguage(), 'translation_memory_id' => @$translation_memory_id, 'string_id' => $strRec->val('string_id')));
             $res = $tlogEntry->save();
             if (PEAR::isError($res)) {
                 //throw new Exception($res->getMessage());
                 // This will throw an error if there is a duplicate... we don't care... we're not interested in duplicates
             }
         }
         $this->mark('ITERATING TRANSLATION MISSES END');
     }
     return;
 }
コード例 #8
0
ファイル: test_SweteJob.php プロジェクト: gtoffoli/swete
 /**
  * Tests approve() on a job for a static site, where several strings were added to the job, and some of the strings were translated
  */
 function testApproveStaticString()
 {
     $job = SweteJob::createJob($this->staticSite);
     $username = '******';
     //add strings to the job
     $strings = array("Blah blah blah string", "Doop doop doop doop string", "Whee whee whee whee whee string", "Hey hey hey hey hey strings");
     $this->addWebpageForStaticSite($url, $strings);
     $misses = df_get_records_array('translation_miss_log', array('website_id' => '=' . $this->staticSite->getRecord()->val('website_id')));
     foreach ($misses as $miss) {
         $job->addTranslationMiss($miss->val('translation_miss_log_id'));
     }
     $job->compile();
     $tm = $job->getTranslationMemory();
     $trec = $tm->setTranslationStatus('Blah blah blah string', 'string', XFTranslationMemory::TRANSLATION_SUBMITTED, $username);
     $trec = $tm->setTranslationStatus('Whee whee whee whee whee string', 'string', XFTranslationMemory::TRANSLATION_SUBMITTED, $username);
     $this->assertEquals(SweteJob::JOB_STATUS_NEW, $job->getRecord()->val('job_status'));
     $job->approve('test_user');
     $job->refresh();
     $this->assertEquals(SweteJob::JOB_STATUS_CLOSED, $job->getRecord()->val('job_status'));
     foreach ($job->getWebpageRecords() as $webpage) {
         $tm = XFTranslationMemory::loadTranslationMemoryFor($webpage, $job->getRecord()->val('source_language'), $job->getRecord()->val('destination_language'));
         $this->assertTrue($tm->containsTranslation('Blah blah blah string', 'string'));
         $this->assertTrue($tm->containsTranslation('Whee whee whee whee whee string', 'string'));
     }
 }
コード例 #9
0
ファイル: CSVStringImporter.php プロジェクト: gtoffoli/swete
 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++;
     }
 }
コード例 #10
0
ファイル: SweteJob.class.php プロジェクト: gtoffoli/swete
 /**
  * Approves the job, accepting all translations that have been added.
  */
 public function approve($username)
 {
     require_once 'modules/tm/lib/XFTranslationMemory.php';
     //use import to copy translation memory from the job to the webpages
     foreach ($this->getWebpageRecords() as $webpage) {
         $tm = XFTranslationMemory::loadTranslationMemoryFor($webpage, $this->_rec->val('source_language'), $this->_rec->val('destination_language'));
         $tm->import($this->getTranslationMemory());
     }
     //close the job
     $this->setStatus(self::JOB_STATUS_CLOSED, $username);
 }