Esempio n. 1
0
 function getTranslationMemoryId($record, $source, $dest)
 {
     $res = df_q("select translation_memory_id from jobs where job_id='" . addslashes($record->val('job_id')) . "'");
     list($tmid) = mysql_fetch_row($res);
     @mysql_free_result($res);
     return $tmid;
 }
Esempio n. 2
0
 public static function decorateNewJob(SweteSite $site, Dataface_Record $rec)
 {
     require_once 'modules/tm/lib/XFTranslationMemory.php';
     try {
         SweteDb::q('start transaction');
         // First we need to create a translation memory for this job.
         $tm = XFTranslationMemory::createTranslationMemory('Job translation memory for site ' . $site->getRecord()->val('website_id') . ' ' . $site->getSourceLanguage() . '->' . $site->getDestinationLanguage(), $site->getSourceLanguage(), $site->getDestinationLanguage());
         $status = SweteJob::JOB_STATUS_NEW;
         if ($rec->val('assigned_to')) {
             $status = SweteJob::JOB_STATUS_ASSIGNED;
         }
         $rec->setValues(array('website_id' => $site->getRecord()->val('website_id'), 'date_created' => date('Y-m-d H:i:s'), 'job_status' => $status, 'translation_memory_id' => $tm->getRecord()->val('translation_memory_id'), 'source_language' => $site->getSourceLanguage(), 'destination_language' => $site->getDestinationLanguage()));
         //print_r($rec->vals());
         $res = $rec->save();
         if (PEAR::isError($res)) {
             //print_r($res);
             throw new Exception($res->getMessage(), $res->getCode());
         }
         if ($rec->val('posted_by')) {
             $res = df_q("insert ignore into job_roles (job_id, username, access_level) values ('" . addslashes($rec->val('job_id')) . "','" . addslashes($rec->val('posted_by')) . "','" . self::JOB_ROLE_OWNER . "')");
         }
         if ($rec->val('assigned_to')) {
             $res = df_q("insert ignore into job_roles (job_id, username, access_level) values ('" . addslashes($rec->val('job_id')) . "','" . addslashes($rec->val('assigned_to')) . "','" . self::JOB_ROLE_TRANSLATOR . "')");
         }
         SweteDb::q('commit');
         $out = new SweteJob($rec);
         $out->setTranslationMemory($tm);
         return $out;
     } catch (Exception $ex) {
         SweteDb::q('rollback');
         throw $ex;
     }
 }
Esempio n. 3
0
 function deleteRecord($record)
 {
     if ($record->val('translation_memory_id')) {
         $res = df_q(sprintf("delete from translation_miss_log where string_id=%d and translation_memory_id=%d", $record->val('string_id'), $record->val('translation_memory_id')));
         $res = df_q(sprintf("delete from xf_tm_translation_memory_strings where string_id=%d and translation_memory_id=%d", $record->val('string_id'), $record->val('translation_memory_id')));
     } else {
         $res = df_q(sprintf("delete from translation_miss_log where string_id=%d and translation_memory_id IS NULL", $record->val('string_id')));
     }
     return $res;
 }
 function update_1()
 {
     $q[] = "create table users (\n      username varchar(32) primary key not null,\n      password varchar(64)\n      )";
     $q[] = "create table sessions (\n      username varchar(32),\n      token_id varchar(64) primary key not null,\n      expires INT(11)\n      )";
     $q[] = "create table friends (\n      user1 varchar(32),\n      user2 varchar(32),\n      primary key (user1, user2 )\n      )";
     $q[] = "create table friend_requests (\n      sender varchar(32),\n      receiver varchar(32),\n      primary key (sender, receiver)\n      )";
     $q[] = "create table profiles (\n      username varchar(32) primary key not null,\n      screen_name varchar(100),\n      avatar varchar(100),\n      avatar_mimetype varchar(100)\n      )";
     $q[] = "create table posts (\n      post_id int(11) not null auto_increment primary key,\n      username varchar(32),\n      date_posted INT(11),\n      photo varchar(255),\n      photo_mimetype varchar(100),\n      comment text\n      )";
     df_q($q);
 }
Esempio n. 5
0
 public static function getRecentProcessInfo($limit = 10)
 {
     $username = '';
     $user = SweteTools::getUser();
     if ($user) {
         $username = $user->val('username');
     }
     $out = array();
     $res = df_q("select process_id, complete, running, time_started, time_finished, error, error_message, status_message, status_current_position, status_total from background_processes where \n\t\t\tusername='******'\n\t\t\torder by time_started desc\n\t\t\tlimit " . intval($limit));
     $out[] = mysql_fetch_object($res);
     @mysql_free_result($res);
     return $out;
 }
Esempio n. 6
0
 function block__before_result_list_content()
 {
     $app = Dataface_Application::getInstance();
     $query = $app->getQuery();
     $builder = new Dataface_QueryBuilder($query['-table'], $query);
     $sql = 'select sum(num_words) ' . $builder->_from() . $builder->_where();
     $res = df_q($sql);
     $row = mysql_fetch_row($res);
     @mysql_free_result($res);
     $app->addHeadContent('<style type="text/css">#total-words-found {float:right;width: 200px;}</style>');
     echo '<div id="total-words-found">Total Words: ' . $row[0] . '</div>';
     Dataface_JavascriptTool::getInstance()->import('swete/actions/batch_google_translate.js');
 }
Esempio n. 7
0
 public function run()
 {
     require_once 'inc/SweteSite.class.php';
     require_once 'inc/SweteWebpage.class.php';
     require_once 'inc/PageProcessor.php';
     require_once 'modules/tm/lib/XFTranslationMemory.php';
     $wpids = array();
     $numPages = 0;
     foreach ($this->changedTranslationMemories as $tmid => $strids) {
         if (!$strids) {
             $strids = array(0);
         }
         $strids = implode(',', $strids);
         $sql = "select distinct wp.webpage_id, s.source_language\n\t\t\t\tfrom \n\t\t\t\t\twebpage_properties wp \n\t\t\t\t\tinner join webpage_strings ws on wp.webpage_id=ws.webpage_id\n\t\t\t\t\tinner join webpages w on wp.webpage_id=w.webpage_id\n\t\t\t\t\tinner join websites s on w.website_id=s.website_id\n\t\t\t\twhere\n\t\t\t\t\twp.effective_translation_memory_id='" . addslashes($tmid) . "' and\n\t\t\t\t\tws.string_id in (" . $strids . ")";
         $res = df_q($sql);
         $numPages += mysql_num_rows($res);
         $wplangs = array();
         while ($row = mysql_fetch_row($res)) {
             list($webpageId, $sourceLanguage) = $row;
             $wpids[] = $webpageId;
             $wplangs[] = $sourceLanguage;
         }
         @mysql_free_result($res);
     }
     df_q("update background_processes set \n\t\t\t\tstatus_message='" . addslashes('Updating webpages with new translations') . "',\n\t\t\t\tstatus_current_position=0,\n\t\t\t\tstatus_total='" . addslashes($numPages) . "'\n\t\t\t\twhere process_id='" . addslashes($this->getProcessId()) . "'");
     $count = 1;
     foreach ($wpids as $k => $webpageId) {
         $pageWrapper = SweteWebpage::loadById($webpageId, $wplangs[$k]);
         $pageWrapper->setSite($this->getSite($pageWrapper->getRecord()->val('website_id')));
         $tmid = $pageWrapper->getTranslationMemoryId(true);
         if ($tmid) {
             $tm = $this->getTranslationMemory($tmid);
             if ($tm) {
                 $processor = new PageProcessor();
                 $processor->site = $pageWrapper->getSite();
                 $processor->translationMemory = $tm;
                 $processor->page = $pageWrapper;
                 $processor->translateMinStatus = 3;
                 $processor->translateMaxStatus = 5;
                 $processor->logTranslationMisses = true;
                 $processor->savePage = true;
                 $processor->saveTranslationLogRecord = true;
                 $processor->process();
             }
         }
         df_q("update background_processes set \n\t\t\t\tstatus_current_position='" . addslashes($count) . "'\n\t\t\t\twhere process_id='" . addslashes($this->getProcessId()) . "'");
         $count++;
     }
     df_q("update background_processes set \n\t\t\t\tstatus_message='" . addslashes('New translations successfully applied to ' . $numPages . ' pages.') . "',\n\t\t\t\tstatus_current_position=0,\n\t\t\t\tstatus_total='" . addslashes($numPages) . "'\n\t\t\t\twhere process_id='" . addslashes($this->getProcessId()) . "'");
 }
Esempio n. 8
0
 function beforeHandleRequest()
 {
     $app = Dataface_Application::getInstance();
     $app->addHeadContent('<link rel="stylesheet" type="text/css" href="' . htmlspecialchars(DATAFACE_SITE_URL . '/css/swete/global.css') . '"/>');
     $query =& $app->getQuery();
     $res = df_q("select language_code, language_label from languages");
     $langs = array();
     while ($row = mysql_fetch_row($res)) {
         $langs[$row[0]] = $row[1];
     }
     @mysql_free_result($res);
     $app->_conf['languages'] = $langs;
     $app->registerEventListener('tm.setTranslationStatus', array($this, 'handleTranslationStatusChanged'));
     Dataface_JavascriptTool::getInstance()->import('swete/global.js');
     if (@$app->_conf['using_default_action'] and $query['-table'] == 'dashboard') {
         $query['-action'] = 'dashboard';
     }
 }
Esempio n. 9
0
 function handle($params)
 {
     session_write_close();
     header('Connection: close');
     $app = Dataface_Application::getInstance();
     $query = $app->getQuery();
     if (!@$query['job_id']) {
         throw new Exception("NO job id specified");
     }
     if (!@$query['url_hash']) {
         throw new Exception("No URL Hash specified");
     }
     $job = df_get_record('jobs', array('job_id' => '=' . $query['job_id']));
     if (!$job) {
         throw new Exception("Job could not be found.");
     }
     require_once 'inc/SweteJob.class.php';
     require_once 'inc/SweteJobPageSucker.php';
     $jobO = new SweteJob($job);
     $pageSucker = new SweteJobPageSucker($jobO);
     $resource = $pageSucker->loadResource($query['url_hash']);
     if (!$resource) {
         header('HTTP/1.0 404 Not Found');
         exit;
     }
     if (!$job->checkPermission('preview job')) {
         header('HTTP/1.0 400 Permission denied');
         exit;
     }
     $res = df_q("select * from job_content where job_content_id='" . addslashes($resource->val('job_content_id')) . "' limit 1");
     $content = mysql_fetch_object($res);
     $output = $content->content;
     if (preg_match('#css#', $content->content_type)) {
         $output = $pageSucker->renderCss($output, DATAFACE_SITE_HREF . '?-action=swete_job_serve_content&job_id=' . $query['job_id'] . '&url_hash=');
     }
     header('Content-Length: ' . strlen($output));
     header('Content-Type: ' . $content->content_type);
     echo $output;
     flush();
 }
Esempio n. 10
0
 /**
  * Checks to see if any of the dependent tables have been modified since this table
  * was last modified.  If so, it will drop the table and regenerate it.
  */
 public function update()
 {
     $mod_times = \Dataface_Table::getTableModificationTimes();
     if (!isset($mod_times[$this->tableName])) {
         $me = 0;
     } else {
         $me = $mod_times[$this->tableName];
     }
     $outOfDate = false;
     foreach ($this->dependencies as $dep) {
         if (@$mod_times[$dep] > $me) {
             $outOfDate = true;
             break;
         }
     }
     if ($outOfDate) {
         \df_q("DROP TABLE IF EXISTS `" . str_replace('`', '', $this->tableName) . "`");
         \df_q($this->sql);
         import('Dataface/IO.php');
         \Dataface_IO::touchTable($this->tableName);
     }
 }
Esempio n. 11
0
 /**
  * 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')));
     }
 }
Esempio n. 12
0
 function testPage()
 {
     $url = DATAFACE_SITE_URL . '/tests/testsites/site1/index.html';
     $site = new Dataface_Record('websites', array());
     $site->setValues(array('website_url' => df_absolute_url(DATAFACE_SITE_URL . '/tests/testsites/site1/'), 'source_language' => 'en', 'target_language' => 'fr', 'website_name' => 'Site 1 French', 'active' => 1, 'base_path' => DATAFACE_SITE_URL . '/proxies/site1/', 'host' => $_SERVER['HTTP_HOST']));
     $site->save();
     df_q("delete from site_text_filters where website_id='" . addslashes($site->val('website_id')) . "'");
     $server = new ProxyServer();
     $server->site = SweteSite::loadSiteById($site->val('website_id'));
     $server->SERVER = array('REQUEST_METHOD' => 'get');
     $server->URL = df_absolute_url(DATAFACE_SITE_URL . '/proxies/site1/index.html');
     $server->buffer = true;
     $server->handleRequest();
     $doc = new DOMDocument();
     $doc->loadHtml(file_get_contents('tests/testsites/site1_output/index.out.html'));
     $expected = $doc->saveHtml();
     //echo $server->contentBuffer;
     $doc2 = new DOMDocument();
     $doc2->loadHtml($server->contentBuffer);
     $actual = $doc2->saveHtml();
     //$this->assertEquals(trim($expected), trim($actual));
     // Cancelled this test because WTF!!!!  Even if I print the actual output, copy it to the file
     // and compare it to itself, it still fails!!!! WTF!!!!
 }
Esempio n. 13
0
 function afterSave($record)
 {
     if ($record->val('posted_by')) {
         $res = df_q("insert ignore into job_roles (job_id, username, access_level) values ('" . addslashes($record->val('job_id')) . "','" . addslashes($record->val('posted_by')) . "','" . SweteJob::JOB_ROLE_OWNER . "')");
     }
     if ($record->val('assigned_to')) {
         $res = df_q("insert ignore into job_roles (job_id, username, access_level) values ('" . addslashes($record->val('job_id')) . "','" . addslashes($record->val('assigned_to')) . "','" . SweteJob::JOB_ROLE_TRANSLATOR . "')");
     }
 }
Esempio n. 14
0
 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++;
     }
 }
Esempio n. 15
0
 function update_4802()
 {
     $sql[] = "CREATE OR REPLACE VIEW `swete_strings` AS\n            select  s.string_id,\n                    s.normalized_value as normalized_string,\n                    s.normalized_value as string,\n                    s.num_words,\n                    t.normalized_translation_value,\n                    if(tml.webpage_id is null,hrl.proxy_request_url, concat(ws.website_url,w.webpage_url)) as request_url,\n                    ifnull(tml.translation_memory_id,tm.translation_memory_id) as translation_memory_id,\n                    tm.translation_memory_uuid,\n                    tm.translation_memory_name,\n                    tm.source_language,\n                    tm.destination_language,\n                    ws.website_name,\n                    ws.website_id,\n                    tml.date_inserted\n                    from \n                    xf_tm_strings s\n                    inner join xf_tm_translation_memories tm on 1\n                    left join xf_tm_translation_memory_strings tms on s.string_id=tms.string_id and tms.translation_memory_id=tm.translation_memory_id\n                    left join xf_tm_translations t on tms.current_translation_id=t.translation_id\n                    left join translation_miss_log tml on tml.string_id=s.string_id and tml.translation_memory_id=tm.translation_memory_id\n                    left join http_request_log hrl on tml.http_request_log_id=hrl.http_request_log_id\n                    left join webpages w on tml.webpage_id=w.webpage_id\n                    left join websites ws on (tml.website_id=ws.website_id OR tm.translation_memory_id=ws.translation_memory_id)\n                    where ifnull(tml.translation_memory_id,tm.translation_memory_id) IS NOT NULL\n            ";
     try {
         df_q($sql);
         df_clear_views();
         df_clear_cache();
     } catch (Exception $ex) {
     }
 }
Esempio n. 16
0
 /**
  * Creates a history table corresponding to the given table.
  * @param string $tablename The name of the table whose history table is
  * 		to be created.
  */
 function createHistoryTable($tablename)
 {
     $app =& Dataface_Application::getInstance();
     $sql = "create table `" . $this->logTableName($tablename) . "` (\n\t\t\t`history__id` int(11) auto_increment NOT NULL,\n\t\t\t`history__language` varchar(2) DEFAULT NULL,\n\t\t\t`history__comments` text default null,\n\t\t\t`history__user` varchar(32) default null,\n\t\t\t`history__state` int(5) default 0,\n\t\t\t`history__modified` datetime,";
     $table =& Dataface_Table::loadTable($tablename);
     $res = df_q("SHOW TABLE STATUS LIKE '" . addslashes($tablename) . "'");
     $status = xf_db_fetch_assoc($res);
     $charset = substr($status['Collation'], 0, strpos($status['Collation'], '_'));
     $collation = $status['Collation'];
     $fieldnames = array_keys($table->fields());
     $fielddefs = array();
     foreach ($fieldnames as $fieldname) {
         $field =& $table->getField($fieldname);
         $type = strcasecmp($field['Type'], 'container') === 0 ? 'varchar(64)' : $field['Type'];
         $fielddefs[] = "`" . $fieldname . "` " . $type;
         unset($field);
     }
     $sql .= implode(",\n", $fielddefs);
     $sql .= ",\n\t\t\tPRIMARY KEY (`history__id`),\n\t\t\tKEY prikeys using hash (`" . implode('`,`', array_keys($table->keys())) . "`),\n\t\t\tKEY datekeys using btree (`history__modified`)) " . (@$status['Engine'] ? "ENGINE=" . $status['Engine'] : '') . " " . ($charset ? "DEFAULT CHARSET=" . $charset . ($collation ? " COLLATE {$collation}" : '') : '');
     $res = xf_db_query($sql, $app->db());
     if (!$res) {
         trigger_error(xf_db_error($app->db()), E_USER_ERROR);
     }
 }
Esempio n. 17
0
 function afterInsert($record)
 {
     require_once 'inc/SweteTools.php';
     SweteTools::updateDb();
     // Add default text filters.
     df_q("insert into site_text_filters (website_id, filter_id, filter_type, filter_order)\n\t\t\tselect " . intval($record->val('website_id')) . ", filter_id, 'Prefilter', default_order\n\t\t\tfrom text_filters where is_default_prefilter=1 and \n\t\t\t(`language` IS NULL or `language`='" . addslashes($record->val('source_language')) . "')");
     df_q("insert into site_text_filters (website_id, filter_id, filter_type, filter_order)\n\t\t\tselect " . intval($record->val('website_id')) . ", filter_id, 'Postfilter', default_order\n\t\t\tfrom text_filters where is_default_postfilter=1 and \n\t\t\t(`language` IS NULL or `language`='" . addslashes($record->val('source_language')) . "')");
 }
Esempio n. 18
0
    function handle($params)
    {
        session_write_close();
        while (@ob_end_clean()) {
        }
        set_time_limit(0);
        header('Connection: Keep-Alive');
        header("Cache-Control: no-cache, must-revalidate");
        header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
        flush();
        echo <<<END
<!doctype html>
<html>
\t<head>
\t\t<title>Batch Google Translate</title>
\t</head>
\t<body>
END;
        flush();
        for ($i = 0; $i < 20; $i++) {
            echo "                                                               ";
        }
        flush();
        require_once 'modules/tm/lib/googleTranslatePlugin.php';
        require_once 'modules/tm/lib/XFTranslationMemory.php';
        $app = Dataface_Application::getInstance();
        $query = $app->getQuery();
        $strings = df_get_selected_records($query);
        $errors = array();
        $translated = array();
        $googleCodes = array();
        $res = df_q("select language_code, google_language_code from languages");
        while ($row = mysql_fetch_row($res)) {
            $googleCodes[$row[0]] = $row[1] ? $row[1] : $row[0];
        }
        @mysql_free_result($res);
        $i = 1;
        foreach ($strings as $string) {
            $site = $this->getSite($string->val('website_id'));
            $encString = TMTools::encode($string->val('string'), $garbage);
            $compiledString = $this->compileString($encString);
            echo "<script>progressBar.progressbar('option','value'," . ceil(floatval($i) / floatval(count($strings))) . ");\n\t\t\t\tprogressLabel.text('Processing ['+" . json_encode($encString) . ".substring(0,30)+'...]');\n\t\t\t\tsuccessMarker.text(" . count($translated) . ");\n\t\t\t\tfailedMarker.text(" . count($errors) . ");\n\t\t\t</script>";
            flush();
            if (!$site) {
                $errors[] = $err = "The string [" . $encString . "] could not be translated because the site with id " . $string->val('website_id') . " could not be found.";
                echo "<script>progressLog.val(progressLog.val()+'\\n-------\\n'+" . json_encode($err) . ");</script>";
                flush();
                continue;
            }
            if (!$site->checkPermission('google translate')) {
                $errors[] = $err = "The string [" . $encString . "] could not be translated because you don't have permission.";
                echo "<script>progressLog.val(progressLog.val()+'\\n------\\n'+" . json_encode($err) . ");</script>";
                flush();
                continue;
            }
            $apiKey = $site->val('google_api_key');
            if (!$apiKey) {
                $errors[] = $err = "The string [" . $encString . "] could not be translated because the site that it belongs to does not have a Google API key set.";
                echo "<script>progressLog.val(progressLog.val()+'\\n-------\\n'+" . json_encode($err) . ");</script>";
                flush();
                continue;
            }
            $sourceCode = $string->val('source_language');
            $destCode = $string->val('destination_language');
            $gtp = new googleTranslatePlugin($googleCodes[$sourceCode], $googleCodes[$destCode]);
            $gtp->setGoogleAPIKey($apiKey);
            //echo $compiledString;exit;
            $gtp->addSourceString($compiledString);
            try {
                $translations = $gtp->getTranslations();
            } catch (Exception $ex) {
                $errors[] = $err = "Failed to translate string [" . $string->val('encoded_string') . "] due to a google translate error: " . $ex->getMessage();
                echo "<script>progressLog.val(progressLog.val()+'\\n-----\\n'+" . json_encode($err) . ");</script>";
                flush();
                continue;
            }
            if (!isset($translations[$compiledString])) {
                $errors[] = $err = "Failed to translate string [" . $encString . "].  The string returned null after translation.";
                echo "<script>progressLog.val(progressLog.val()+'\\n-------\\n'+" . json_encode($err) . ");</script>";
                flush();
                continue;
            }
            $tm = $this->getTranslationMemory($site->val('translation_memory_id'));
            $trString = $this->uncompileString($translations[$compiledString]);
            //echo $trString;exit;
            $tm->setTranslationStatus($encString, $trString, XFTranslationMemory::TRANSLATION_SUBMITTED, 'Google');
            $translated[$encString] = $trString;
        }
        echo "<script> progressBar.progressbar('option','value',100);\n\t\tprogressLabel.text(" . json_encode('Translated ' . count($translated) . ' strings successfully.  ' . count($errors) . ' errors.') . ");\n\t\tsuccessMarker.text(" . count($translated) . ");\n\t\tfailedMarker.text(" . count($errors) . ");\n\t\t</script>";
        flush();
        /*
        $out = array(
        	'translated' => $translated,
        	'errors' => $errors,
        	'code' => 200,
        	'message' => 'Translated '.count($translated).' strings successfully.  '.count($errors).' errors.'
        );
        
        $this->out($out);
        */
        exit;
    }
Esempio n. 19
0
 /**
  * @brief Returns the last modified time of the record.
  *
  * @return long Unix timestamp marking the last time the record was modified.
  *
  * @since 0.8
  *
  * @section Synopsis
  *
  * This method will first check to see if the delegate class implements a method
  * named getLastModified() and return its result.  If none can be found it will 
  * attempt to guess which field is used to store the last modified date 
  * (based on the Dataface_Table::getLastUpdatedField() method).  Otherwise it will
  * simply return 0.
  *
  * This method is used throughout Xataface to mark the modification times of records.
  *
  * @see http://www.xataface.com/wiki/Delegate_class_methods
  * @see http://xataface.com/documentation/tutorial/getting_started/delegate_classes
  * @see Dataface_Table::getLastUpdatedField()
  */
 function getLastModified()
 {
     if ($res = $this->callDelegateFunction('getLastModified')) {
         return $res;
     } else {
         if ($lastModifiedField = $this->_table->getLastUpdatedField()) {
             if (strcasecmp($this->_table->getType($lastModifiedField), 'timestamp') === 0) {
                 $date = $this->val($lastModifiedField);
                 return strtotime($date['year'] . '-' . $date['month'] . '-' . $date['day'] . ' ' . $date['hours'] . ':' . $date['minutes'] . ':' . $date['seconds']);
             }
             $strtime = $this->strval($lastModifiedField);
             if ($strtime) {
                 return strtotime($strtime);
             }
         }
     }
     if (!isset($this->pouch['__mtime'])) {
         $sql = "select mtime from dataface__record_mtimes where recordhash='" . addslashes(md5($this->getId())) . "'";
         try {
             try {
                 $res = df_q($sql);
             } catch (Exception $ex) {
                 Dataface_IO::createRecordMtimes();
                 $res = df_q($sql);
             }
             list($mtime) = xf_db_fetch_row($res);
             @xf_db_free_result($res);
             $this->pouch['__mtime'] = intval($mtime);
         } catch (Exception $ex) {
             error_log("Failed SQL query {$sql}");
             $this->pouch['__mtime'] = 0;
         }
     }
     return $this->pouch['__mtime'];
 }
Esempio n. 20
0
 function handle($params)
 {
     if (!SweteTools::getUser()) {
         return Dataface_Error::permissionDenied("You must log into access the dashboard");
     }
     $app = Dataface_Application::getInstance();
     // Get sites summary
     import('Dataface/ResultReader.php');
     /*
     $reader = new Dataface_ResultReader("select
     	ws.source_language,
     	ws.target_language,
     	ws.log_translation_misses,
     	ws.website_id, 
     	ws.website_name,
     	ws.website_url,
     	ws.translation_memory_id,
     	concat('http://',ws.host,ws.base_path) as proxy_url,
     	ws.source_language,
     	ws.target_language,
     	(
     		select count(*) from webpages w where w.website_id=ws.website_id
     	) as numpages,
     	(
     		select count(*) from swete_strings tml where tml.website_id=ws.website_id
     	) as numphrases,
     	ifnull((
     		select sum(tml.num_words) from xf_tm_strings xts 
     			inner join swete_strings tml on tml.string_id=xts.string_id
     		where tml.website_id=ws.website_id
     	), 0) as numwords,
     	(
     		select count(*) from swete_strings tml
     			inner join websites ws2 on ws2.website_id=tml.website_id
     			inner join xf_tm_translation_memory_strings xttms on xttms.translation_memory_id=ws2.translation_memory_id and xttms.string_id=tml.string_id
     		where xttms.current_translation_id is not null
     		 	and tml.website_id=ws.website_id
     		
     	) as translated_phrases,
     	
     	ifnull((
     		select sum(tml.num_words) from swete_strings tml
     			inner join websites ws2 on ws2.website_id=tml.website_id
     			inner join xf_tm_translation_memory_strings xttms on xttms.translation_memory_id=ws2.translation_memory_id and xttms.string_id=tml.string_id
     			inner join xf_tm_strings xts on xttms.string_id=xts.string_id
     		where xttms.current_translation_id is not null
     			and tml.website_id=ws.website_id
     			
     	), 0) as translated_words
     	
     		
     	from websites ws
     	", df_db());
     */
     $reader = new Dataface_ResultReader("select\n\t\t\tws.source_language,\n\t\t\tws.target_language,\n\t\t\tws.log_translation_misses,\n\t\t\tws.website_id, \n\t\t\tws.website_name,\n\t\t\tws.website_url,\n\t\t\tws.translation_memory_id,\n\t\t\tconcat('http://',ws.host,ws.base_path) as proxy_url,\n\t\t\tws.source_language,\n\t\t\tws.target_language\n\t\t\t\n\t\t\t\n\t\t\t\t\n\t\t\tfrom websites ws\n\t\t\t", df_db());
     $results = array();
     $languages = Dataface_Table::loadTable('websites')->getValuelist('languages');
     foreach ($reader as $row) {
         $results[] = $row;
         $row->untranslated_words = $row->numwords - $row->translated_words;
         $row->untranslated_phrases = $row->numphrases - $row->translated_phrases;
         $row->source_label = @$languages[$row->source_language] ? $languages[$row->source_language] : $row->source_language;
         $row->target_label = @$languages[$row->target_language] ? $languages[$row->target_language] : $row->target_language;
     }
     Dataface_JavascriptTool::getInstance()->import('swete/actions/dashboard.js');
     $res = df_q("select count(*) from webpages");
     list($numPages) = mysql_fetch_row($res);
     @mysql_free_result($res);
     $res = df_q("select count(*) num_phrases, sum(xts.num_words) as num_words from swete_strings tml \n\t\t\tleft join xf_tm_strings xts on tml.string_id=xts.string_id");
     list($numPhrases, $numWords) = mysql_fetch_row($res);
     @mysql_free_result($res);
     $res = df_q("select count(*) from websites");
     list($numSites) = mysql_fetch_row($res);
     @mysql_free_result($res);
     $res = df_q("select count(*) as numphrases, ifnull(sum(xts.num_words),0) as num_words from swete_strings tml \n\t\t\tinner join websites w on w.website_id=tml.website_id\n\t\t\tinner join xf_tm_translation_memory_strings xttms on w.translation_memory_id=xttms.translation_memory_id and xttms.string_id=tml.string_id\n\t\t\tinner join xf_tm_strings xts on xts.string_id=tml.string_id\n\t\t\twhere xttms.current_translation_id is not null");
     list($translatedPhrases, $translatedWords) = mysql_fetch_row($res);
     @mysql_free_result($res);
     df_display(array('results' => $results, 'systemStats' => array('numWords' => $numWords, 'numPhrases' => $numPhrases, 'numSites' => $numSites, 'numPages' => $numPages, 'translatedPhrases' => $translatedPhrases, 'translatedWords' => $translatedWords, 'untranslatedWords' => $numWords - $translatedWords, 'untranslatedPhrases' => $numPhrases - $translatedPhrases), 'swete_version' => file_get_contents('version.txt')), 'swete/actions/dashboard.html');
 }
Esempio n. 21
0
 function testCompileLiveSite()
 {
     $job = $this->createLiveJobWithTranslationMisses();
     $job->compile();
     $res = df_q("select count(*) from job_translatable where job_id='" . addslashes($job->getRecord()->val('job_id')) . "'");
     list($num) = mysql_fetch_row($res);
     $this->assertEquals(1, $num, 'Should be one translatable created for job.');
     $res = df_q("select count(*) from job_content where job_id='" . addslashes($job->getRecord()->val('job_id')) . "'");
     list($num) = mysql_fetch_row($res);
     $this->assertEquals(10, $num, 'Should be 10 content items in job_content for this job.');
     $translatable = df_get_record('job_translatable', array('job_id' => '=' . $job->getRecord()->val('job_id')));
     $this->assertTrue($translatable instanceof Dataface_Record, 'No translatable found for job.');
     $expectedContents = strip_tags(file_get_contents(dirname(__FILE__) . '/testsites/site2/index.html'));
     $this->assertEquals($expectedContents, strip_tags($translatable->val('full_contents')));
     $job->refresh();
     $this->assertEquals(1, $job->getRecord()->val('compiled'), 'Job compiled flag is not set but should be');
 }
Esempio n. 22
0
 public function addLanguageTables($lang)
 {
     $res = df_q("show tables like '%_en'");
     while ($row = xf_db_fetch_row($res)) {
         if (preg_match('/^(.*)_en$/', $row[0], $matches)) {
             $this->addLanguageTable($matches[1], $lang);
         }
     }
     @xf_db_free_result($res);
 }
Esempio n. 23
0
 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());
                 }
             }
         }
     }
 }
Esempio n. 24
0
 function df_q($sql)
 {
     if (is_array($sql)) {
         foreach ($sql as $q) {
             $res = df_q($q);
         }
         return $res;
     } else {
         $res = xf_db_query($sql, df_db());
         if (!$res) {
             error_log("Error executing SQL: {$sql}");
             error_log(xf_db_error(df_db()));
             throw new Exception(xf_db_error(df_db()));
         }
         return $res;
     }
 }
 function update_1()
 {
     $sql[] = "CREATE TABLE `courses` (\n\t\t  `course_id` int(11) unsigned NOT NULL AUTO_INCREMENT,\n\t\t  `student_id` int(11) unsigned DEFAULT NULL,\n\t\t  `subject` varchar(10) DEFAULT NULL,\n\t\t  `course_number` varchar(10) DEFAULT NULL,\n\t\t  `course_title` varchar(45) DEFAULT NULL,\n\t\t  PRIMARY KEY (`course_id`)\n\t\t)";
     $sql[] = "CREATE TABLE `students` (\n\t\t  `student_id` int(10) unsigned NOT NULL AUTO_INCREMENT,\n\t\t  `first_name` varchar(64) DEFAULT NULL,\n\t\t  `last_name` varchar(64) DEFAULT NULL,\n\t\t  PRIMARY KEY (`student_id`)\n\t\t)";
     df_q($sql);
 }
Esempio n. 26
0
 /**
  * Returns true if the record currently represented in the Table already exists 
  * in the database.
  *
  * @param tablename Alternative table where records may be stored.  This is useful if we are reading form import or delete tables.
  *
  */
 function recordExists(&$record, $keys = null, $tablename = null)
 {
     $this->lastVersionNumber = null;
     if (!is_a($record, "Dataface_Record")) {
         throw new Exception(df_translate('scripts.Dataface.IO.recordExists.ERROR_PARAMETER_1', "In Dataface_IO::recordExists() the first argument is expected to be either a 'Dataface_Record' object or an array of key values, but received neither.\n<br>"), E_USER_ERROR);
     }
     if ($tablename === null and $this->_altTablename !== null) {
         $tablename = $this->_altTablename;
     }
     $tempRecordCreated = false;
     if ($record->snapshotExists()) {
         $tempRecord = new Dataface_Record($record->_table->tablename, $record->getSnapshot());
         $tempRecordCreated = true;
     } else {
         $tempRecord =& $record;
     }
     if ($keys == null) {
         // Had to put in userialize(serialize(...)) because getValues() returns by reference
         // and we don't want to change actual values.
         $query = unserialize(serialize($tempRecord->getValues(array_keys($record->_table->keys()))));
     } else {
         $query = $keys;
     }
     $table_keys = array_keys($this->_table->keys());
     foreach ($table_keys as $key) {
         if (!isset($query[$key]) or !$query[$key]) {
             return false;
         }
     }
     foreach (array_keys($query) as $key) {
         //$query[$key] = '='.$this->_serializer->serialize($key, $tempRecord->getValue($key) );
         $query[$key] = $this->_serializer->serialize($key, $tempRecord->getValue($key));
     }
     if ($tempRecordCreated) {
         $tempRecord->__destruct();
     }
     //$qb = new Dataface_QueryBuilder($this->_table->tablename, $query);
     //$sql = $qb->select_num_rows(array(), $this->tablename($tablename));
     if ($record->table()->isVersioned()) {
         $versionField = "`" . $record->table()->getVersionField() . "`";
     } else {
         $versionField = "NULL";
     }
     $sql = "select `" . $table_keys[0] . "`, {$versionField} from `" . $this->tablename($tablename) . "` where ";
     $where = array();
     foreach ($query as $key => $val) {
         $where[] = '`' . $key . "`='" . addslashes($val) . "'";
     }
     $sql .= implode(' AND ', $where) . ' limit 1';
     $res = df_q($sql, $this->_table->db);
     $num = xf_db_num_rows($res);
     $row = xf_db_fetch_row($res);
     @xf_db_free_result($res);
     if ($num === 1) {
         // We have the correct number...
         // let's check the version
         $this->lastVersionNumber = intval($row[1]);
         return true;
     }
     if ($num > 1) {
         $err = PEAR::raiseError(Dataface_LanguageTool::translate('recordExists failure. Too many rows returned.', "Test for existence of record in recordExists() returned {$rows} records.  \n\t\t\t\t\tIt should have max 1 record.  \n\t\t\t\t\tThe query must be incorrect.  \n\t\t\t\t\tThe query used was '{$sql}'. ", array('table' => $this->_table->tablename, 'line' => 0, 'file' => '_', 'sql' => $sql)), DATAFACE_E_IO_ERROR);
         throw new Exception($err->toString(), E_USER_ERROR);
     }
     return false;
 }