From 4cc9b813d198ddcb46f0ddaaa544df2e230d9553 Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Sun, 5 Oct 2014 15:23:52 -0400 Subject: [PATCH] CRM-14949 - Switch dynamic l10n js back to a callback instead of a file --- CRM/Admin/Page/AJAX.php | 7 +--- CRM/Core/BAO/WordReplacement.php | 2 +- CRM/Core/Invoke.php | 2 +- CRM/Core/Page/AJAX.php | 11 ++++++ CRM/Core/Resources.php | 33 +++-------------- CRM/Core/xml/Menu/Misc.xml | 4 ++ CRM/Upgrade/Page/Upgrade.php | 3 +- CRM/Utils/File.php | 63 -------------------------------- 8 files changed, 25 insertions(+), 100 deletions(-) diff --git a/CRM/Admin/Page/AJAX.php b/CRM/Admin/Page/AJAX.php index 2677fab16f..49f459ed8c 100644 --- a/CRM/Admin/Page/AJAX.php +++ b/CRM/Admin/Page/AJAX.php @@ -45,12 +45,7 @@ class CRM_Admin_Page_AJAX { static function getNavigationMenu() { $contactID = CRM_Core_Session::singleton()->get('userID'); if ($contactID) { - // Set headers to encourage browsers to cache for a long time - $year = 60*60*24*364; - header('Expires: '.gmdate('D, d M Y H:i:s \G\M\T', time() + $year)); - header('Content-Type: application/javascript'); - header("Cache-Control: max-age=$year, public"); - + CRM_Core_Page_AJAX::setJsHeaders(); print CRM_Core_Smarty::singleton()->fetchWith('CRM/common/navigation.js.tpl', array( 'navigation' => CRM_Core_BAO_Navigation::createNavigation($contactID), )); diff --git a/CRM/Core/BAO/WordReplacement.php b/CRM/Core/BAO/WordReplacement.php index ed023400aa..10eb8d4b56 100644 --- a/CRM/Core/BAO/WordReplacement.php +++ b/CRM/Core/BAO/WordReplacement.php @@ -208,7 +208,7 @@ WHERE domain_id = %1 // Reset navigation CRM_Core_BAO_Navigation::resetNavigation(); // Clear js localization - CRM_Core_Resources::singleton()->flushStrings()->rebuildDynamicResources(); + CRM_Core_Resources::singleton()->flushStrings()->resetCacheCode(); } return TRUE; diff --git a/CRM/Core/Invoke.php b/CRM/Core/Invoke.php index a1e5f726f9..4c25c0f885 100644 --- a/CRM/Core/Invoke.php +++ b/CRM/Core/Invoke.php @@ -404,7 +404,7 @@ class CRM_Core_Invoke { CRM_Core_BAO_Setting::updateSettingsFromMetaData(); // Clear js caches - CRM_Core_Resources::singleton()->flushStrings()->rebuildDynamicResources(); + CRM_Core_Resources::singleton()->flushStrings()->resetCacheCode(); CRM_Case_XMLRepository::singleton(TRUE); // also rebuild triggers if requested explicitly diff --git a/CRM/Core/Page/AJAX.php b/CRM/Core/Page/AJAX.php index 42ac838b46..0b5f72fae6 100644 --- a/CRM/Core/Page/AJAX.php +++ b/CRM/Core/Page/AJAX.php @@ -197,6 +197,17 @@ class CRM_Core_Page_AJAX { CRM_Utils_System::civiExit(); } + /** + * Set headers appropriate for a js file + */ + static function setJsHeaders() { + // Encourage browsers to cache for a long time - 1 year + $year = 60*60*24*364; + header('Expires: '.gmdate('D, d M Y H:i:s \G\M\T', time() + $year)); + header('Content-Type: application/javascript'); + header("Cache-Control: max-age=$year, public"); + } + /** * Send autocomplete results to the client. Input can be a simple or nested array. * @param array $results - If nested array, also provide: diff --git a/CRM/Core/Resources.php b/CRM/Core/Resources.php index 8baab9edf6..2a34bde700 100644 --- a/CRM/Core/Resources.php +++ b/CRM/Core/Resources.php @@ -476,7 +476,7 @@ class CRM_Core_Resources { } // Dynamic localization script - $this->addScriptUrl($this->addLocalizationJs(), $jsWeight++, $region); + $this->addScriptUrl(CRM_Utils_System::url('civicrm/ajax/l10n-js/' . $config->lcMessages, array('r' => $this->getCacheCode())), $jsWeight++, $region); // Add global settings $settings = array('config' => array( @@ -531,16 +531,6 @@ class CRM_Core_Resources { return $this; } - /** - * Deletes and rebuilds dynamic resource files - * @return CRM_Core_Resources - */ - public function rebuildDynamicResources() { - CRM_Utils_File::flushDynamicResources(); - $this->addLocalizationJs(); - return $this; - } - /** * Translate strings in a javascript file * @@ -567,27 +557,13 @@ class CRM_Core_Resources { $this->addString($stringsByFile[$file]); } - /** - * Add dynamic l10n js - * - * @return string URL of JS file - */ - private function addLocalizationJs() { - $config = CRM_Core_Config::singleton(); - $fileName = 'l10n-' . $config->lcMessages . '.js'; - if (!is_file(CRM_Utils_File::dynamicResourcePath($fileName))) { - CRM_Utils_File::addDynamicResource($fileName, $this->createLocalizationJs()); - } - // Dynamic localization script - return CRM_Utils_File::dynamicResourceUrl($fileName); - } - /** * Create dynamic script for localizing js widgets * * @return string javascript content */ - private function createLocalizationJs() { + static function outputLocalizationJS() { + CRM_Core_Page_AJAX::setJsHeaders(); $config = CRM_Core_Config::singleton(); $vars = array( 'moneyFormat' => json_encode(CRM_Utils_Money::format(1234.56)), @@ -595,7 +571,8 @@ class CRM_Core_Resources { 'otherSearch' => json_encode(ts('Enter search term...')), 'contactCreate' => CRM_Core_BAO_UFGroup::getCreateLinks(), ); - return CRM_Core_Smarty::singleton()->fetchWith('CRM/common/localization.js.tpl', $vars); + print CRM_Core_Smarty::singleton()->fetchWith('CRM/common/localization.js.tpl', $vars); + CRM_Utils_System::civiExit(); } /** diff --git a/CRM/Core/xml/Menu/Misc.xml b/CRM/Core/xml/Menu/Misc.xml index e1f4a1d11d..64c350ec7d 100644 --- a/CRM/Core/xml/Menu/Misc.xml +++ b/CRM/Core/xml/Menu/Misc.xml @@ -175,4 +175,8 @@ CRM_Core_Page_Angular access CiviCRM + + civicrm/ajax/l10n-js + CRM_Core_Resources::outputLocalizationJS + diff --git a/CRM/Upgrade/Page/Upgrade.php b/CRM/Upgrade/Page/Upgrade.php index f3386177df..f7f7bd2c4f 100644 --- a/CRM/Upgrade/Page/Upgrade.php +++ b/CRM/Upgrade/Page/Upgrade.php @@ -90,7 +90,8 @@ class CRM_Upgrade_Page_Upgrade extends CRM_Core_Page { } // All cached content needs to be cleared because the civi codebase was just replaced - CRM_Core_Resources::singleton()->flushStrings()->rebuildDynamicResources(); + CRM_Core_Resources::singleton()->flushStrings()->resetCacheCode(); + CRM_Core_Menu::store(); // This could be removed in later rev if ($currentVer == '2.1.6') { diff --git a/CRM/Utils/File.php b/CRM/Utils/File.php index 82f18f5f45..42ef38472b 100644 --- a/CRM/Utils/File.php +++ b/CRM/Utils/File.php @@ -689,68 +689,5 @@ HTACCESS; } return TRUE; } - - /** - * Create a static file (e.g. css or js) in the dynamic resource directory - * Note: if the file already exists it will be overwritten - * @param string $fileName - * @param string $contents - */ - static function addDynamicResource($fileName, $contents) { - // First ensure the directory exists - $path = self::dynamicResourcePath(); - if (!is_dir($path)) { - self::createDir($path); - self::restrictBrowsing($path); - } - file_put_contents("$path/$fileName", $contents); - } - - /** - * Get the path of a dynamic resource file - * With no fileName supplied, returns the path of the directory - * @param string $fileName - * @return string - */ - static function dynamicResourcePath($fileName = NULL) { - $config = CRM_Core_Config::singleton(); - // FIXME: Use self::baseFilePath once url issue has been resolved - // Windows PHP accepts any mix of "/" or "\"; simpler if we only deal with one of those - $imageUploadDir = str_replace('\\', '/', $config->imageUploadDir); - $path = self::addTrailingSlash(str_replace('/persist/contribute', '', $imageUploadDir), '/') . 'dynamic'; - if ($fileName !== NULL) { - $path .= "/$fileName"; - } - return $path; - } - - /** - * Get the URL of a dynamic resource file - * @param string $fileName - * @return string - */ - static function dynamicResourceUrl($fileName, $addCacheCode = TRUE) { - $config = CRM_Core_Config::singleton(); - // FIXME: Need a better way of getting the url of the baseFilePath - $url = self::addTrailingSlash(str_replace('/persist/contribute', '', $config->imageUploadURL), '/') . 'dynamic/' . $fileName; - if ($addCacheCode) { - return $url . '?r=' . CRM_Core_Resources::singleton()->getCacheCode(); - } - return $url; - } - - /** - * Delete all files from the dynamic resource directory - * Change the cache code to force browsers to reload new resources - */ - static function flushDynamicResources() { - $files = glob(self::dynamicResourcePath('*')); - foreach ($files ? $files : array() as $file) { - if (is_file($file)) { - unlink($file); - } - } - CRM_Core_Resources::singleton()->resetCacheCode(); - } } -- 2.25.1