コード例 #1
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());
                 }
             }
         }
     }
 }