$objNarroImporter->TranslationPath = $argv[array_search('--translation-directory', $argv) + 1]; } else { $objNarroImporter->TranslationPath = $objNarroImporter->Project->DefaultTranslationPath; } try { $intPid = NarroUtils::IsProcessRunning('import', $objNarroImporter->Project->ProjectId); if ($intPid && $intPid != getmypid()) { throw new Exception(sprintf('An import process is already running for this project with pid %d', $intPid)); } $strProcPidFile = __TMP_PATH__ . '/' . $objNarroImporter->Project->ProjectId . '-' . $objNarroImporter->TargetLanguage->LanguageCode . '-import-process.pid'; if (file_exists($strProcPidFile)) { unlink($strProcPidFile); } file_put_contents($strProcPidFile, getmypid()); chmod($strProcPidFile, 0666); $blnResult = $objNarroImporter->ImportProject(); } catch (Exception $objEx) { NarroLogger::LogError(sprintf('An error occurred during import: %s', $objEx->getMessage())); exit; } if ($blnResult) { foreach (NarroImportStatistics::$arrStatistics as $strName => $strValue) { if ($strName == 'Start time') { $strValue = date('Y-m-d H:i:s', $strValue); } if ($strName == 'End time') { $strValue = date('Y-m-d H:i:s', $strValue); } if ($strValue != 0) { NarroLogger::LogInfo(stripslashes($strName) . ': ' . $strValue); }
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); } } } }