From 42f1a95785ed839d49f2a97285b44e7ca05d3990 Mon Sep 17 00:00:00 2001 From: demeritcowboy Date: Fri, 1 Dec 2023 23:26:05 -0500 Subject: [PATCH 1/4] find best candidate folder --- .../installFiles/l10nDownload.civi-setup.php | 40 +++++++++++++------ 1 file changed, 28 insertions(+), 12 deletions(-) diff --git a/setup/plugins/installFiles/l10nDownload.civi-setup.php b/setup/plugins/installFiles/l10nDownload.civi-setup.php index 8c7f881d5f60..96b006cbb2e7 100644 --- a/setup/plugins/installFiles/l10nDownload.civi-setup.php +++ b/setup/plugins/installFiles/l10nDownload.civi-setup.php @@ -16,22 +16,38 @@ ->addListener('civi.setup.checkRequirements', function(\Civi\Setup\Event\CheckRequirementsEvent $e) { $lang = \Civi\Setup::instance()->getModel()->lang; if ($lang && $lang != 'en_US') { - // Use CiviCRM Core directory as a fall back. - $baseDir = $e->getModel()->srcPath; - if (isset($e->getModel()->paths['civicrm.private']['path'])) { - // If the civicrm files directory is set use this as the base path. - $baseDir = $e->getModel()->paths['civicrm.private']['path']; + // build list of candidate folders in preferred order + $candidates = []; + // if it's already set, that's our pref + if (isset($e->getModel()->paths['civicrm.l10n']['path'])) { + $candidates[] = $e->getModel()->paths['civicrm.l10n']['path']; } - // Set l10n basedir as a define. The GenCode.php tries to locate the l10n files + // Now check CIVICRM_L10N_BASEDIR via either define or env. + // The GenCode.php tries to locate the l10n files // from this location if other than l10n in the civicrm core directory. - if (!isset($e->getModel()->paths['civicrm.l10n']['path'])) { - if (\CRM_Utils_Constant::value('CIVICRM_L10N_BASEDIR')) { - $e->getModel()->paths['civicrm.l10n']['path'] = \CRM_Utils_Constant::value('CIVICRM_L10N_BASEDIR'); - } - else { - $e->getModel()->paths['civicrm.l10n']['path'] = $baseDir . DIRECTORY_SEPARATOR . 'l10n'; + $civicrm_l10n_basedir = CRM_Utils_Constant::value('CIVICRM_L10N_BASEDIR'); + if ($civicrm_l10n_basedir) { + $candidates[] = $civicrm_l10n_basedir . DIRECTORY_SEPARATOR . 'l10n'; + } + elseif (isset($e->getModel()->paths['civicrm.private']['path'])) { + // If the civicrm files directory is set use this as the base path. + $candidates[] = $e->getModel()->paths['civicrm.private']['path'] . DIRECTORY_SEPARATOR . 'l10n'; + } + // Use CiviCRM Core directory as a fall back. + $candidates[] = $e->getModel()->srcPath . DIRECTORY_SEPARATOR . 'l10n'; + + // Now see if any of the folders already exist. + foreach ($candidates as $candidate) { + if (is_dir($candidate)) { + $e->getModel()->paths['civicrm.l10n']['path'] = $candidate; + break; } } + // If none existed, then take our first preference. We know there's always at least one. + if (!isset($e->getModel()->paths['civicrm.l10n']['path'])) { + $e->getModel()->paths['civicrm.l10n']['path'] = $candidates[0]; + } + if (getenv('CIVICRM_L10N_BASEDIR') === FALSE) { // Set the environment variable CIVICRM_L10N_BASEDIR which is used in xml/GenCode.php // to create the localized sql files. From 71427b18854a67fb2221a8ede78e07e34ffea0aa Mon Sep 17 00:00:00 2001 From: demeritcowboy Date: Fri, 1 Dec 2023 23:26:37 -0500 Subject: [PATCH 2/4] if the lang subfolder already exists don't redownload --- .../installFiles/l10nDownload.civi-setup.php | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/setup/plugins/installFiles/l10nDownload.civi-setup.php b/setup/plugins/installFiles/l10nDownload.civi-setup.php index 96b006cbb2e7..5fbe1cd2e9d1 100644 --- a/setup/plugins/installFiles/l10nDownload.civi-setup.php +++ b/setup/plugins/installFiles/l10nDownload.civi-setup.php @@ -68,18 +68,18 @@ if (!is_dir($downloadDir)) { \Civi\Setup::log()->info("Creating directory: " . $downloadDir); \CRM_Utils_File::createDir($downloadDir, FALSE); - } - foreach ($e->getModel()->moFiles as $moFile => $url) { - $l10DownloadFile = str_replace('[locale]', $lang, $url); - \Civi\Setup::log() - ->info("Download translation '.$moFile.' from " . $l10DownloadFile . ' into ' . $downloadDir); - $client = new \GuzzleHttp\Client(); - $response = $client->get($l10DownloadFile); - if ($response->getStatusCode() == 200) { - $success = file_put_contents($downloadDir . DIRECTORY_SEPARATOR . $moFile, $response->getBody()); - if (!$success) { - $e->addError('l10n', 'download', 'Unable to download translation file'); + foreach ($e->getModel()->moFiles as $moFile => $url) { + $l10DownloadFile = str_replace('[locale]', $lang, $url); + \Civi\Setup::log() + ->info("Download translation '.$moFile.' from " . $l10DownloadFile . ' into ' . $downloadDir); + $client = new \GuzzleHttp\Client(); + $response = $client->get($l10DownloadFile); + if ($response->getStatusCode() == 200) { + $success = file_put_contents($downloadDir . DIRECTORY_SEPARATOR . $moFile, $response->getBody()); + if (!$success) { + $e->addError('l10n', 'download', 'Unable to download translation file'); + } } } } From c6dc2134373cd6a9911ce4b6ba7f1ff7a43e0a0d Mon Sep 17 00:00:00 2001 From: demeritcowboy Date: Fri, 1 Dec 2023 23:37:33 -0500 Subject: [PATCH 3/4] error check folder creation --- setup/plugins/installFiles/l10nDownload.civi-setup.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/setup/plugins/installFiles/l10nDownload.civi-setup.php b/setup/plugins/installFiles/l10nDownload.civi-setup.php index 5fbe1cd2e9d1..baaec23f73eb 100644 --- a/setup/plugins/installFiles/l10nDownload.civi-setup.php +++ b/setup/plugins/installFiles/l10nDownload.civi-setup.php @@ -55,7 +55,9 @@ } if (!is_dir($e->getModel()->paths['civicrm.l10n']['path'])) { \Civi\Setup::log()->info("Creating directory: " . $e->getModel()->paths['civicrm.l10n']['path']); - \CRM_Utils_File::createDir($e->getModel()->paths['civicrm.l10n']['path'], FALSE); + if (!mkdir($e->getModel()->paths['civicrm.l10n']['path'], 0777, TRUE)) { + $e->addError('system', 'l10nWritable', sprintf('Unable to create l10n directory "%s"', $e->getModel()->paths['civicrm.l10n']['path'])); + } } } }, \Civi\Setup::PRIORITY_MAIN); @@ -67,7 +69,9 @@ $downloadDir = $e->getModel()->paths['civicrm.l10n']['path'] . DIRECTORY_SEPARATOR . $lang . DIRECTORY_SEPARATOR . 'LC_MESSAGES'; if (!is_dir($downloadDir)) { \Civi\Setup::log()->info("Creating directory: " . $downloadDir); - \CRM_Utils_File::createDir($downloadDir, FALSE); + if (!mkdir($downloadDir, 0777, TRUE)) { + $e->addError('system', 'l10nWritable', sprintf('Unable to create language directory "%s"', $downloadDir)); + } foreach ($e->getModel()->moFiles as $moFile => $url) { $l10DownloadFile = str_replace('[locale]', $lang, $url); From 6767fbbeae2ff2765a17ce8edc5bb1666c389333 Mon Sep 17 00:00:00 2001 From: demeritcowboy Date: Sat, 2 Dec 2023 10:49:30 -0500 Subject: [PATCH 4/4] remove weird dots --- setup/plugins/installFiles/l10nDownload.civi-setup.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup/plugins/installFiles/l10nDownload.civi-setup.php b/setup/plugins/installFiles/l10nDownload.civi-setup.php index baaec23f73eb..9f9f210fba8e 100644 --- a/setup/plugins/installFiles/l10nDownload.civi-setup.php +++ b/setup/plugins/installFiles/l10nDownload.civi-setup.php @@ -76,7 +76,7 @@ foreach ($e->getModel()->moFiles as $moFile => $url) { $l10DownloadFile = str_replace('[locale]', $lang, $url); \Civi\Setup::log() - ->info("Download translation '.$moFile.' from " . $l10DownloadFile . ' into ' . $downloadDir); + ->info("Download translation '$moFile' from " . $l10DownloadFile . ' into ' . $downloadDir); $client = new \GuzzleHttp\Client(); $response = $client->get($l10DownloadFile); if ($response->getStatusCode() == 200) {