public function ImportFile($strTemplate, $strTranslatedFile = null) { // No need to check equals for gettext $this->blnCheckEqual = false; $arrTemplateFile = $this->getFieldGroups($strTemplate); $arrTranslatedFile = $this->getFieldGroups($strTranslatedFile); $intTotalContexts = count($arrTemplateFile); $intCurrentContext = 0; foreach ($arrTemplateFile as $strIndex => $arrTemplateFields) { $intCurrentContext++; NarroProgress::SetProgressPerFile($intCurrentContext / $intTotalContexts * 100, $this->objProject->ProjectId, 'import'); /** * ignore po header */ if ($arrTemplateFields['MsgId'] === '') { continue; } if (isset($arrTranslatedFile[$strIndex]['MsgStr']) && $arrTranslatedFile[$strIndex]['MsgStr'] != '' && isset($arrTranslatedFile[$strIndex]['MsgId']) && $arrTranslatedFile[$strIndex]['MsgId'] == $arrTemplateFields['MsgId']) { $arrTranslatedFile[$strIndex]['MsgStr'] = str_replace('\\"', '"', $arrTranslatedFile[$strIndex]['MsgStr']); } else { $arrTranslatedFile[$strIndex]['MsgStr'] = null; } if (strstr($arrTranslatedFile[$strIndex]['Flag'], 'fuzzy')) { /** * if the string is marked fuzzy, don't import the translation and delete fuzzy flag */ $arrTranslatedFile[$strIndex]['MsgStr'] = ''; } for ($intPluralId = 0; $intPluralId < $this->objTargetLanguage->Plurals; $intPluralId++) { if (strstr($arrTranslatedFile[$strIndex]['Flag'], 'fuzzy')) { /** * if the string is marked fuzzy, don't import the translation and delete fuzzy flag */ $arrTranslatedFile[$strIndex]['MsgStr' . $intPluralId] = ''; $arrTranslatedFile[$strIndex]['Flag'] = str_replace(', fuzzy', '', $arrTranslatedFile[$strIndex]['Flag']); /** * if no other flags are found, just empty the variable */ if (strlen(trim($arrTranslatedFile[$strIndex]['Flag'])) < 4) { $arrTranslatedFile[$strIndex]['Flag'] = null; } } else { if (isset($arrTranslatedFile[$strIndex]['MsgStr' . $intPluralId]) && $arrTranslatedFile[$strIndex]['MsgStr' . $intPluralId] != '' && isset($arrTranslatedFile[$strIndex]['MsgPluralId']) && $arrTranslatedFile[$strIndex]['MsgPluralId'] == $arrTemplateFields['MsgPluralId']) { $arrTranslatedFile[$strIndex]['MsgStr' . $intPluralId] = str_replace('\\"', '"', $arrTranslatedFile[$strIndex]['MsgStr' . $intPluralId]); } else { $arrTranslatedFile[$strIndex]['MsgStr' . $intPluralId] = ''; } } } $arrTranslatedFile[$strIndex]['Flag'] = str_replace(', fuzzy', '', $arrTranslatedFile[$strIndex]['Flag']); /** * if no other flags are found, just empty the variable */ if (strlen(trim($arrTranslatedFile[$strIndex]['Flag'])) < 4) { $arrTranslatedFile[$strIndex]['Flag'] = null; } /** * if it's not a plural, just add msgid and msgstr */ if (is_null($arrTemplateFields['MsgPluralId'])) { $this->AddTranslation($this->stripAccessKey($arrTemplateFields['MsgId']), $this->getAccessKey($arrTemplateFields['MsgId']), $this->stripAccessKey($arrTranslatedFile[$strIndex]['MsgStr']), $this->getAccessKey($arrTranslatedFile[$strIndex]['MsgStr']), $arrTemplateFields['Context'], $arrTemplateFields['ContextComment']); } else { /** * if it's a plural, add the pluralid with all the msgstr's available * the first one is added with msgid/msgstr[0] (this is the singular) * the next ones are added with plural id, so in fact they will be tied to the same text */ if (!is_null($arrTemplateFields['MsgStr0'])) { $this->AddTranslation($this->stripAccessKey($arrTemplateFields['MsgId']), $this->getAccessKey($arrTemplateFields['MsgId']), $this->stripAccessKey($arrTranslatedFile[$strIndex]['MsgStr0']), $this->getAccessKey($arrTranslatedFile[$strIndex]['MsgStr0']), $arrTemplateFields['Context'] . "\nThis text has plurals.", $arrTemplateFields['ContextComment']); } for ($intPluralId = 1; $intPluralId < $this->objTargetLanguage->Plurals; $intPluralId++) { if (!is_null($arrTranslatedFile[$strIndex]['MsgStr' . $intPluralId])) { $this->AddTranslation($this->stripAccessKey($arrTemplateFields['MsgPluralId']), $this->getAccessKey($arrTemplateFields['MsgPluralId']), $arrTranslatedFile[$strIndex]['MsgStr' . $intPluralId], $this->getAccessKey($arrTranslatedFile[$strIndex]['MsgStr' . $intPluralId]), $arrTemplateFields['Context'] . "\nThis is plural form {$intPluralId} for the text \"" . $arrTemplateFields['MsgId'] . "\".", $arrTemplateFields['ContextComment']); } } } } }
public function btnImport_Click($strFormId, $strControlId, $strParameter) { if (!QApplication::HasPermissionForThisLang('Can import project', $this->objProject->ProjectId)) { return false; } $strProcLogFile = __TMP_PATH__ . '/' . $this->objProject->ProjectId . '-' . QApplication::$TargetLanguage->LanguageCode . '-import-process.log'; $strProcPidFile = __TMP_PATH__ . '/' . $this->objProject->ProjectId . '-' . QApplication::$TargetLanguage->LanguageCode . '-import-process.pid'; $strProgressFile = __TMP_PATH__ . '/import-' . $this->objProject->ProjectId . '-' . QApplication::$TargetLanguage->LanguageCode; if ($strParameter == 1) { if (NarroUtils::IsProcessRunning('import', $this->objProject->ProjectId)) { $this->objImportProgress->Translated = NarroProgress::GetProgress($this->objProject->ProjectId, 'import'); $this->objImportProgress->MarkAsModified(); } else { $this->lblImport->Text = t('Import finished.'); if (QApplication::$UseAjax) { QApplication::ExecuteJavaScript('if (typeof lastImportId != \'undefined\') clearInterval(lastImportId)'); } if (file_exists($strProcLogFile) && filesize($strProcLogFile)) { NarroLogger::LogInfo(sprintf('There are messages from the background process: %s', file_get_contents($strProcLogFile))); } if (file_exists($strProcLogFile)) { unlink($strProcLogFile); } if (file_exists($strProcPidFile)) { unlink($strProcPidFile); } if (file_exists($strProgressFile)) { unlink($strProgressFile); } $this->lblImport->Visible = true; $this->btnImport->Visible = true; $this->btnKillProcess->Visible = false; $this->objImportProgress->Translated = 0; $this->objImportProgress->Visible = false; } $this->pnlLogViewer->MarkAsModified(); } elseif ($strParameter == 2) { $this->pnlLogViewer->ProjectId = $this->objProject->ProjectId; $this->pnlLogViewer->LanguageId = QApplication::GetLanguageId(); $this->pnlLogViewer->DateStart = QDateTime::Now(); NarroProgress::ClearProgressFileName($this->objProject->ProjectId, 'import'); set_time_limit(0); if (file_exists($strProcLogFile)) { unlink($strProcLogFile); } if (file_exists($strProcPidFile)) { unlink($strProcPidFile); } if (file_exists($strProgressFile)) { unlink($strProgressFile); } $objNarroImporter = new NarroProjectImporter(); /** * Get boolean options */ $objNarroImporter->CheckEqual = true; $objNarroImporter->Approve = $this->chkApproveImportedTranslations->Checked; $objNarroImporter->ApproveAlreadyApproved = !$this->chkApproveOnlyNotApproved->Checked; $objNarroImporter->OnlySuggestions = !$this->chkImportSourceTexts->Checked; $objNarroImporter->Project = $this->objProject; $objNarroImporter->ImportUnchangedFiles = $this->chkImportUnchangedFiles->Checked; $objNarroImporter->User = QApplication::$User; $objNarroImporter->TargetLanguage = QApplication::$TargetLanguage; $objNarroImporter->SourceLanguage = NarroLanguage::LoadByLanguageCode(NarroLanguage::SOURCE_LANGUAGE_CODE); try { $objNarroImporter->TranslationPath = $this->pnlTranslationsSource->Directory; $objNarroImporter->TemplatePath = $this->objProject->DefaultTemplatePath; } catch (Exception $objEx) { NarroLogger::LogError(sprintf('An error occurred during import: %s', $objEx->getMessage())); $this->lblImport->Text = sprintf(t('Import failed: %s'), $objEx->getMessage()); return false; } try { $objNarroImporter->ImportProject(); } catch (Exception $objEx) { NarroLogger::LogError(sprintf('An error occurred during import: %s', $objEx->getMessage())); $this->lblImport->Text = sprintf(t('Import failed: %s'), $objEx->getMessage()); } $this->lblImport->Visible = true; $this->btnImport->Visible = true; $this->btnKillProcess->Visible = false; $this->objImportProgress->Visible = false; $this->pnlLogViewer->MarkAsModified(); } else { $this->pnlLogViewer->ProjectId = $this->objProject->ProjectId; $this->pnlLogViewer->LanguageId = QApplication::GetLanguageId(); $this->pnlLogViewer->DateStart = QDateTime::Now(); NarroProgress::ClearProgressFileName($this->objProject->ProjectId, 'import'); $this->pnlLogViewer->MarkAsModified(); $this->btnImport->Visible = false; $this->btnKillProcess->Visible = QApplication::HasPermission('Administrator', $this->objProject, QApplication::$TargetLanguage->LanguageCode) && !$this->btnImport->Visible; $this->objImportProgress->Visible = true; $this->objImportProgress->Translated = 0; $this->lblImport->Text = ''; try { $strCommand = sprintf(__PHP_CLI_PATH__ . ' ' . escapeshellarg(sprintf('%s/includes/narro/importer/narro-cli.php', __DOCROOT__ . __SUBDIRECTORY__)) . ' --import --minloglevel 3 --project %d --user %d --check-equal ' . ($this->chkApproveImportedTranslations->Checked ? '--approve ' : '') . ($this->chkImportUnchangedFiles->Checked ? '--import-unchanged-files ' : '') . ($this->chkApproveOnlyNotApproved->Checked ? '' : '--approve-already-approved ') . (!$this->chkImportSourceTexts->Checked || !QApplication::HasPermission('Can import project', $this->objProject->ProjectId) ? '--only-suggestions ' : '') . ' --template-lang %s --translation-lang %s --template-directory %s --translation-directory %s', (int) $this->objProject->ProjectId, (int) QApplication::$User->UserId, escapeshellarg(NarroLanguage::SOURCE_LANGUAGE_CODE), escapeshellarg(QApplication::$TargetLanguage->LanguageCode), escapeshellarg($this->objProject->DefaultTemplatePath), escapeshellarg($this->pnlTranslationsSource->Directory)); } catch (Exception $objEx) { NarroLogger::LogError(sprintf('An error occurred during import: %s', $objEx->getMessage())); $this->lblImport->Text = sprintf(t('Import failed: %s'), $objEx->getMessage()); $this->lblImport->Visible = true; $this->btnImport->Visible = true; $this->btnKillProcess->Visible = QApplication::HasPermission('Administrator', $this->objProject, QApplication::$TargetLanguage->LanguageCode) && !$this->btnImport->Visible; $this->objImportProgress->Translated = 0; $this->objImportProgress->Visible = false; $this->pnlLogViewer->MarkAsModified(); return false; } if (file_exists($strProcLogFile) && is_writable($strProcLogFile)) { unlink($strProcLogFile); } NarroUtils::Exec($strCommand, $arrOutput, $arrError, $intRetVal, true); if ($intRetVal == 0) { if (QApplication::$UseAjax) { QApplication::ExecuteJavaScript(sprintf('lastImportId = setInterval("qc.pA(\'%s\', \'%s\', \'QClickEvent\', \'1\')", %d);', $strFormId, $strControlId, 2000)); } else { $this->btnImport_Click($strFormId, $strControlId, 1); } } else { $this->objImportProgress->Visible = false; NarroLogger::LogError('Failed to launch a background process, there will be no progress displayed, and it might take a while, please wait for more messages'); $this->pnlLogViewer->MarkAsModified(); /** * try importing without launching a background process */ if (QApplication::$UseAjax) { QApplication::ExecuteJavaScript(sprintf('lastImportId = setTimeout("qc.pA(\'%s\', \'%s\', \'QClickEvent\', \'2\')", %d);', $strFormId, $strControlId, 2000)); } else { $this->btnImport_Click($strFormId, $strControlId, 2); } } } }
public function ExportFromDirectory() { // NarroLogger::LogDebug(sprintf('Starting to export in directory "%s"', $this->strTranslationPath)); /** * get the file list with complete paths */ $arrFiles = $this->ListDir($this->strTemplatePath); $intTotalFilesToProcess = count($arrFiles); if ($intTotalFilesToProcess == 1 && pathinfo($arrFiles[0], PATHINFO_EXTENSION) == 'xpi') { return $this->ExportToXpi($arrFiles[0]); } if ($intTotalFilesToProcess > __MAXIMUM_FILE_COUNT_TO_EXPORT__) { NarroLogger::LogError(sprintf('Too many files to process: %d. The maximum number of files to export is set in the configuration file at %d', $intTotalFilesToProcess, __MAXIMUM_FILE_COUNT_TO_EXPORT__)); return false; } // NarroLogger::LogDebug(sprintf('Starting to process %d files', $intTotalFilesToProcess)); $arrDirectories = array(); NarroProgress::SetProgress(0, $this->objProject->ProjectId, 'export', $intTotalFilesToProcess); if (is_array($arrFiles)) { foreach ($arrFiles as $intFileNo => $strFileToExport) { $arrFileParts = explode('/', str_replace($this->strTemplatePath, '', $strFileToExport)); $strFileName = $arrFileParts[count($arrFileParts) - 1]; unset($arrFileParts[count($arrFileParts) - 1]); unset($arrFileParts[0]); $strPath = ''; $intParentId = null; $arrDirectories = array(); foreach ($arrFileParts as $intPos => $strDir) { $strPath = $strPath . '/' . $strDir; if (!isset($arrDirectories[$strPath])) { if (!is_null($intParentId)) { $objFile = NarroFile::QuerySingle(QQ::AndCondition(QQ::Equal(QQN::NarroFile()->ProjectId, $this->objProject->ProjectId), QQ::Equal(QQN::NarroFile()->FileName, $strDir), QQ::Equal(QQN::NarroFile()->TypeId, NarroFileType::Folder), QQ::Equal(QQN::NarroFile()->ParentId, $intParentId))); } else { $objFile = NarroFile::QuerySingle(QQ::AndCondition(QQ::Equal(QQN::NarroFile()->ProjectId, $this->objProject->ProjectId), QQ::Equal(QQN::NarroFile()->FileName, $strDir), QQ::Equal(QQN::NarroFile()->TypeId, NarroFileType::Folder), QQ::IsNull(QQN::NarroFile()->ParentId))); } if (!$objFile instanceof NarroFile) { NarroLogger::LogWarn(sprintf('Could not find folder "%s" with parent id "%d" in the database.', $strDir, $intParentId)); continue; } $arrDirectories[$strPath] = $objFile->FileId; } $intParentId = $arrDirectories[$strPath]; } $strTranslatedFileToExport = str_replace($this->strTemplatePath, $this->strTranslationPath, $strFileToExport); if (!file_exists(dirname($strTranslatedFileToExport))) { if (!mkdir(dirname($strTranslatedFileToExport), 0777, true)) { NarroLogger::LogWarn(sprintf('Failed to create the parent directories for the file %s', $strFileToExport)); return false; } NarroUtils::RecursiveChmod(dirname($strTranslatedFileToExport)); } $objFile = NarroFile::QuerySingle(QQ::AndCondition(QQ::Equal(QQN::NarroFile()->ProjectId, $this->objProject->ProjectId), QQ::Equal(QQN::NarroFile()->FileName, $strFileName), QQ::Equal(QQN::NarroFile()->ParentId, $intParentId), QQ::Equal(QQN::NarroFile()->Active, 1))); if (!$objFile instanceof NarroFile) { continue; } // NarroLogger::LogDebug(sprintf('Exporting file "%s" using template "%s"', $objFile->FileName, $strTranslatedFileToExport)); $intTime = time(); $this->ExportFile($objFile, $strFileToExport, $strTranslatedFileToExport); $intElapsedTime = time() - $intTime; // NarroLogger::LogDebug(sprintf('Processed file "%s" in %d seconds, %d files left', str_replace($this->strTemplatePath, '', $strFileToExport), $intElapsedTime, (count($arrFiles) - $intFileNo - 1))); NarroImportStatistics::$arrStatistics['Exported files']++; NarroProgress::SetProgress((int) ceil($intFileNo * 100 / $intTotalFilesToProcess), $this->objProject->ProjectId, 'export'); } } return true; }
public function btnExport_Click($strFormId, $strControlId, $strParameter) { if (!QApplication::HasPermissionForThisLang('Can export project', $this->objProject->ProjectId)) { return false; } $strProcLogFile = __TMP_PATH__ . '/' . $this->objProject->ProjectId . '-' . QApplication::$TargetLanguage->LanguageCode . '-export-process.log'; if ($strParameter == 1) { if (NarroUtils::IsProcessRunning('export', $this->objProject->ProjectId)) { $this->objExportProgress->Translated = NarroProgress::GetProgress($this->objProject->ProjectId, 'export'); $this->objExportProgress->MarkAsModified(); } else { $this->lblExport->Text = t('Export finished.'); if (QApplication::$UseAjax) { QApplication::ExecuteJavaScript('if (typeof lastExportId != \'undefined\') clearInterval(lastExportId)'); } if (file_exists($strProcLogFile) && filesize($strProcLogFile)) { NarroLogger::LogInfo(sprintf('There are messages from the background process: %s', file_get_contents($strProcLogFile))); } $this->lblExport->Visible = true; $this->btnExport->Visible = true; $this->btnKillProcess->Visible = false; $this->objExportProgress->Translated = 0; $this->objExportProgress->Visible = false; QApplication::$PluginHandler->DisplayExportMessage($this->objProject); if (is_array(QApplication::$PluginHandler->PluginReturnValues)) { foreach (QApplication::$PluginHandler->PluginReturnValues as $strPluginName => $mixReturnValue) { if (count($mixReturnValue) == 2 && $mixReturnValue[0] instanceof NarroProject && is_string($mixReturnValue[1]) && $mixReturnValue[1] != '') { $this->lblExport->Text .= sprintf('<br /><span class="info"><b>%s</b>: %s</span>', $strPluginName, nl2br($mixReturnValue[1])); } } } } $this->pnlLogViewer->MarkAsModified(); } elseif ($strParameter == 2) { $this->pnlLogViewer->DateStart = QDateTime::Now(); $this->pnlLogViewer->ProjectId = $this->objProject->ProjectId; $this->pnlLogViewer->LanguageId = QApplication::GetLanguageId(); NarroProgress::ClearProgressFileName($this->objProject->ProjectId, 'import'); set_time_limit(0); if ($this->chkCleanDirectory->Checked) { NarroUtils::RecursiveDelete($this->objProject->DefaultTranslationPath . '/*'); } $objNarroImporter = new NarroProjectImporter(); /** * Get boolean options */ $objNarroImporter->ExportedSuggestion = $this->lstExportSuggestionType->SelectedValue; $objNarroImporter->Project = $this->objProject; $objNarroImporter->ExportAuthorList = $this->txtAuthor->Text; $objNarroImporter->User = QApplication::$User; $objNarroImporter->TargetLanguage = QApplication::$TargetLanguage; $objNarroImporter->SourceLanguage = NarroLanguage::LoadByLanguageCode(NarroLanguage::SOURCE_LANGUAGE_CODE); try { $objNarroImporter->TranslationPath = $this->objProject->DefaultTranslationPath; $objNarroImporter->TemplatePath = $this->objProject->DefaultTemplatePath; } catch (Exception $objEx) { NarroLogger::LogError(sprintf('An error occurred during export: %s', $objEx->getMessage())); $this->lblExport->Text = t('Export failed.'); } try { $objNarroImporter->ExportProject(); } catch (Exception $objEx) { NarroLogger::LogError(sprintf('An error occurred during export: %s', $objEx->getMessage())); $this->lblExport->Text = t('Export failed.'); } $this->lblExport->Visible = true; $this->btnExport->Visible = true; $this->btnKillProcess->Visible = false; $this->objExportProgress->Visible = false; $this->pnlLogViewer->MarkAsModified(); } else { $this->pnlLogViewer->DateStart = QDateTime::Now(); $this->pnlLogViewer->ProjectId = $this->objProject->ProjectId; $this->pnlLogViewer->LanguageId = QApplication::GetLanguageId(); if ($this->chkCleanDirectory->Checked) { NarroUtils::RecursiveDelete($this->objProject->DefaultTranslationPath . '/*'); } $this->btnExport->Visible = false; $this->btnKillProcess->Visible = $this->btnKillProcess->Visible = QApplication::HasPermission('Administrator', $this->objProject, QApplication::$TargetLanguage->LanguageCode); $this->objExportProgress->Visible = true; $this->objExportProgress->Translated = 0; $this->lblExport->Text = ''; try { $strCommand = sprintf('%s %s --export --project %d --user %d --template-lang %s --translation-lang %s --template-directory %s --translation-directory %s --exported-suggestion %d --export-author-list %s', __PHP_CLI_PATH__, escapeshellarg(sprintf('%s/includes/narro/importer/narro-cli.php', __DOCROOT__ . __SUBDIRECTORY__)), (int) $this->objProject->ProjectId, (int) QApplication::$User->UserId, escapeshellarg(NarroLanguage::SOURCE_LANGUAGE_CODE), escapeshellarg(QApplication::$TargetLanguage->LanguageCode), escapeshellarg($this->objProject->DefaultTemplatePath), escapeshellarg($this->objProject->DefaultTranslationPath), (int) $this->lstExportSuggestionType->SelectedValue, escapeshellarg($this->txtAuthor->Text)); } catch (Exception $objEx) { NarroLogger::LogError(sprintf('An error occurred during export: %s', $objEx->getMessage())); $this->lblExport->Text = t('Export failed.'); $this->lblExport->Visible = true; $this->btnExport->Visible = true; $this->btnKillProcess->Visible = false; $this->objExportProgress->Translated = 0; $this->objExportProgress->Visible = false; $this->pnlLogViewer->MarkAsModified(); return false; } if (file_exists($strProcLogFile) && is_writable($strProcLogFile)) { unlink($strProcLogFile); } $mixProcess = proc_open("{$strCommand} &", array(2 => array("file", $strProcLogFile, 'a')), $foo); if ($mixProcess) { if (QApplication::$UseAjax) { QApplication::ExecuteJavaScript(sprintf('lastExportId = setInterval("qc.pA(\'%s\', \'%s\', \'QClickEvent\', \'1\')", %d);', $strFormId, $strControlId, 2000)); } else { $this->btnExport_Click($strFormId, $strControlId, 1); } } else { $this->objExportProgress->Visible = false; NarroLogger::LogError('Failed to launch a background process, there will be no progress displayed, and it might take a while, please wait for more messages'); $this->pnlLogViewer->MarkAsModified(); /** * try exporting without launching a background process */ if (QApplication::$UseAjax) { QApplication::ExecuteJavaScript(sprintf('lastExportId = setTimeout("qc.pA(\'%s\', \'%s\', \'QClickEvent\', \'2\')", %d);', $strFormId, $strControlId, 2000)); } else { $this->btnExport_Click($strFormId, $strControlId, 2); } } } }