From 93b0f1a3bff98f3b9aa9f2e0ca2db4bc23ca3746 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20M=C3=BCller?= Date: Mon, 6 Oct 2014 12:38:59 +0200 Subject: [PATCH] adding cssmin and jssmin(minify) adding argument deleteSelf to rmdirr() - if false the directory itself will not be deleted only it's content adding repair step to clean the asset cache after upgrade + coding style adjustments --- 3rdparty | 2 +- lib/private/helper.php | 17 ++- lib/private/repair.php | 26 ++-- lib/private/template.php | 34 +++++- lib/private/template/templatefilelocator.php | 2 + lib/private/templatelayout.php | 119 +++++++++---------- lib/repair/assetcache.php | 30 +++++ 7 files changed, 148 insertions(+), 82 deletions(-) create mode 100644 lib/repair/assetcache.php diff --git a/3rdparty b/3rdparty index 5db359cb71..635aaf81d0 160000 --- a/3rdparty +++ b/3rdparty @@ -1 +1 @@ -Subproject commit 5db359cb710c51747d3fb78b605f8b8cdcd1e605 +Subproject commit 635aaf81d0d97f000c9d4b90fc5a3240d05cf371 diff --git a/lib/private/helper.php b/lib/private/helper.php index ea91cc5751..823e82ceeb 100644 --- a/lib/private/helper.php +++ b/lib/private/helper.php @@ -402,9 +402,10 @@ class OC_Helper { /** * Recursive deletion of folders * @param string $dir path to the folder + * @param bool $deleteSelf if set to false only the content of the folder will be deleted * @return bool */ - static function rmdirr($dir) { + static function rmdirr($dir, $deleteSelf = true) { if (is_dir($dir)) { $files = new RecursiveIteratorIterator( new RecursiveDirectoryIterator($dir, RecursiveDirectoryIterator::SKIP_DOTS), @@ -419,15 +420,19 @@ class OC_Helper { unlink($fileInfo->getRealPath()); } } - rmdir($dir); + if ($deleteSelf) { + rmdir($dir); + } } elseif (file_exists($dir)) { - unlink($dir); + if ($deleteSelf) { + unlink($dir); + } } - if (file_exists($dir)) { - return false; - } else { + if (!$deleteSelf) { return true; } + + return !file_exists($dir); } /** diff --git a/lib/private/repair.php b/lib/private/repair.php index c7db8b2617..081aeb32c6 100644 --- a/lib/private/repair.php +++ b/lib/private/repair.php @@ -10,6 +10,13 @@ namespace OC; use OC\Hooks\BasicEmitter; use OC\Hooks\Emitter; +use OC\Repair\AssetCache; +use OC\Repair\Collation; +use OC\Repair\InnoDB; +use OC\Repair\RepairConfig; +use OC\Repair\RepairLegacyStorages; +use OC\Repair\RepairMimeTypes; +use OC\Repair\SearchLuceneTables; class Repair extends BasicEmitter { /** @@ -69,9 +76,10 @@ class Repair extends BasicEmitter { */ public static function getRepairSteps() { return array( - new \OC\Repair\RepairMimeTypes(), - new \OC\Repair\RepairLegacyStorages(\OC::$server->getConfig(), \OC_DB::getConnection()), - new \OC\Repair\RepairConfig(), + new RepairMimeTypes(), + new RepairLegacyStorages(\OC::$server->getConfig(), \OC_DB::getConnection()), + new RepairConfig(), + new AssetCache() ); } @@ -83,14 +91,14 @@ class Repair extends BasicEmitter { */ public static function getBeforeUpgradeRepairSteps() { $steps = array( - new \OC\Repair\InnoDB(), - new \OC\Repair\Collation(\OC::$server->getConfig(), \OC_DB::getConnection()), - new \OC\Repair\SearchLuceneTables() + new InnoDB(), + new Collation(\OC::$server->getConfig(), \OC_DB::getConnection()), + new SearchLuceneTables() ); //There is no need to delete all previews on every single update - //only 7.0.0 thru 7.0.2 generated broken previews - $currentVersion = \OC_Config::getValue('version'); + //only 7.0.0 through 7.0.2 generated broken previews + $currentVersion = \OC::$server->getConfig()->getSystemValue('version'); if (version_compare($currentVersion, '7.0.0.0', '>=') && version_compare($currentVersion, '7.0.2.2', '<=')) { $steps[] = new \OC\Repair\Preview(); @@ -102,7 +110,7 @@ class Repair extends BasicEmitter { /** * {@inheritDoc} * - * Redeclared as public to allow invocation from within the closure above in php 5.3 + * Re-declared as public to allow invocation from within the closure above in php 5.3 */ public function emit($scope, $method, $arguments = array()) { parent::emit($scope, $method, $arguments); diff --git a/lib/private/template.php b/lib/private/template.php index fce26117ed..fe0cde53ff 100644 --- a/lib/private/template.php +++ b/lib/private/template.php @@ -198,8 +198,8 @@ class OC_Template extends \OC\Template\Base { * Includes another template. use inc('template'); ?> to * do this. */ - public function inc( $file, $additionalparams = null ) { - return $this->load($this->path.$file.'.php', $additionalparams); + public function inc( $file, $additionalParams = null ) { + return $this->load($this->path.$file.'.php', $additionalParams); } /** @@ -277,4 +277,34 @@ class OC_Template extends \OC\Template\Base { $content->printPage(); die(); } + + /** + * @return bool + */ + public static function isAssetPipelineEnabled() { + // asset management enabled? + $useAssetPipeline = \OC::$server->getConfig()->getSystemValue('asset-pipeline.enabled', false); + if (!$useAssetPipeline) { + return false; + } + + // assets folder exists? + $assetDir = \OC::$SERVERROOT . '/assets'; + if (!is_dir($assetDir)) { + if (!mkdir($assetDir)) { + \OCP\Util::writeLog('assets', + "Folder <$assetDir> does not exist and/or could not be generated.", \OCP\Util::ERROR); + return false; + } + } + + // assets folder can be accessed? + if (!touch($assetDir."/.oc")) { + \OCP\Util::writeLog('assets', + "Folder <$assetDir> could not be accessed.", \OCP\Util::ERROR); + return false; + } + return $useAssetPipeline; + } + } diff --git a/lib/private/template/templatefilelocator.php b/lib/private/template/templatefilelocator.php index 4676fceb37..8e9f3bd810 100644 --- a/lib/private/template/templatefilelocator.php +++ b/lib/private/template/templatefilelocator.php @@ -24,6 +24,8 @@ class TemplateFileLocator { /** * @param string $template + * @return string + * @throws \Exception */ public function find( $template ) { if ($template === '') { diff --git a/lib/private/templatelayout.php b/lib/private/templatelayout.php index 9f996e19f8..10abff6267 100644 --- a/lib/private/templatelayout.php +++ b/lib/private/templatelayout.php @@ -2,8 +2,10 @@ use Assetic\Asset\AssetCollection; use Assetic\Asset\FileAsset; use Assetic\AssetWriter; -use Assetic\Filter\CssRewriteFilter; use Assetic\Filter\CssImportFilter; +use Assetic\Filter\CssMinFilter; +use Assetic\Filter\CssRewriteFilter; +use Assetic\Filter\JSMinFilter; /** * Copyright (c) 2012 Bart Visscher @@ -17,13 +19,22 @@ class OC_TemplateLayout extends OC_Template { private static $versionHash = ''; /** - * @param string $renderas - * @param string $appid application id + * @var \OCP\IConfig */ - public function __construct( $renderas, $appid = '' ) { + private $config; + + /** + * @param string $renderAs + * @param string $appId application id + */ + public function __construct( $renderAs, $appId = '' ) { + + // yes - should be injected .... + $this->config = \OC::$server->getConfig(); + // Decide which page we show - if( $renderas == 'user' ) { + if( $renderAs == 'user' ) { parent::__construct( 'core', 'layout.user' ); if(in_array(OC_APP::getCurrentApp(), array('settings','admin', 'help'))!==false) { $this->assign('bodyid', 'body-settings'); @@ -32,9 +43,12 @@ class OC_TemplateLayout extends OC_Template { } // Update notification - if(OC_Config::getValue('updatechecker', true) === true) { - $data=OC_Updater::check(); - if(isset($data['version']) && $data['version'] != '' and $data['version'] !== Array() && OC_User::isAdminUser(OC_User::getUser())) { + if($this->config->getSystemValue('updatechecker', true) === true && + OC_User::isAdminUser(OC_User::getUser())) { + $updater = new \OC\Updater(); + $data = $updater->check('http://apps.owncloud.com/updater.php'); + + if(isset($data['version']) && $data['version'] != '' and $data['version'] !== Array()) { $this->assign('updateAvailable', true); $this->assign('updateVersion', $data['versionstring']); $this->assign('updateLink', $data['web']); @@ -47,7 +61,7 @@ class OC_TemplateLayout extends OC_Template { // Add navigation entry $this->assign( 'application', '', false ); - $this->assign( 'appid', $appid ); + $this->assign( 'appid', $appId ); $navigation = OC_App::getNavigation(); $this->assign( 'navigation', $navigation); $this->assign( 'settingsnavigation', OC_App::getSettingsNavigation()); @@ -57,15 +71,15 @@ class OC_TemplateLayout extends OC_Template { break; } } - $user_displayname = OC_User::getDisplayName(); - $this->assign( 'user_displayname', $user_displayname ); + $userDisplayName = OC_User::getDisplayName(); + $this->assign( 'user_displayname', $userDisplayName ); $this->assign( 'user_uid', OC_User::getUser() ); $this->assign( 'appsmanagement_active', strpos(OC_Request::requestUri(), OC_Helper::linkToRoute('settings_apps')) === 0 ); - $this->assign('enableAvatars', \OC_Config::getValue('enable_avatars', true)); - } else if ($renderas == 'error') { + $this->assign('enableAvatars', $this->config->getSystemValue('enable_avatars', true)); + } else if ($renderAs == 'error') { parent::__construct('core', 'layout.guest', '', false); $this->assign('bodyid', 'body-login'); - } else if ($renderas == 'guest') { + } else if ($renderAs == 'guest') { parent::__construct('core', 'layout.guest'); $this->assign('bodyid', 'body-login'); } else { @@ -76,27 +90,27 @@ class OC_TemplateLayout extends OC_Template { self::$versionHash = md5(implode(',', OC_App::getAppVersions())); } - $useAssetPipeline = $this->isAssetPipelineEnabled(); + $useAssetPipeline = self::isAssetPipelineEnabled(); if ($useAssetPipeline) { $this->append( 'jsfiles', OC_Helper::linkToRoute('js_config', array('v' => self::$versionHash))); $this->generateAssets(); } else { // Add the js files - $jsfiles = self::findJavascriptFiles(OC_Util::$scripts); + $jsFiles = self::findJavascriptFiles(OC_Util::$scripts); $this->assign('jsfiles', array(), false); - if (OC_Config::getValue('installed', false) && $renderas!='error') { + if ($this->config->getSystemValue('installed', false) && $renderAs != 'error') { $this->append( 'jsfiles', OC_Helper::linkToRoute('js_config', array('v' => self::$versionHash))); } - foreach($jsfiles as $info) { + foreach($jsFiles as $info) { $web = $info[1]; $file = $info[2]; $this->append( 'jsfiles', $web.'/'.$file . '?v=' . self::$versionHash); } // Add the css files - $cssfiles = self::findStylesheetFiles(OC_Util::$styles); + $cssFiles = self::findStylesheetFiles(OC_Util::$styles); $this->assign('cssfiles', array()); - foreach($cssfiles as $info) { + foreach($cssFiles as $info) { $web = $info[1]; $file = $info[2]; @@ -113,10 +127,10 @@ class OC_TemplateLayout extends OC_Template { // Read the selected theme from the config file $theme = OC_Util::getTheme(); - // Read the detected formfactor and use the right file name. - $fext = self::getFormFactorExtension(); + // Read the detected form factor and use the right file name. + $formFactorExt = self::getFormFactorExtension(); - $locator = new \OC\Template\CSSResourceLocator( $theme, $fext, + $locator = new \OC\Template\CSSResourceLocator( $theme, $formFactorExt, array( OC::$SERVERROOT => OC::$WEBROOT ), array( OC::$THIRDPARTYROOT => OC::$THIRDPARTYWEBROOT )); $locator->find($styles); @@ -131,18 +145,17 @@ class OC_TemplateLayout extends OC_Template { // Read the selected theme from the config file $theme = OC_Util::getTheme(); - // Read the detected formfactor and use the right file name. - $fext = self::getFormFactorExtension(); + // Read the detected form factor and use the right file name. + $formFactorExt = self::getFormFactorExtension(); - $locator = new \OC\Template\JSResourceLocator( $theme, $fext, + $locator = new \OC\Template\JSResourceLocator( $theme, $formFactorExt, array( OC::$SERVERROOT => OC::$WEBROOT ), array( OC::$THIRDPARTYROOT => OC::$THIRDPARTYWEBROOT )); $locator->find($scripts); return $locator->getResources(); } - public function generateAssets() - { + public function generateAssets() { $jsFiles = self::findJavascriptFiles(OC_Util::$scripts); $jsHash = self::hashScriptNames($jsFiles); @@ -150,7 +163,13 @@ class OC_TemplateLayout extends OC_Template { $jsFiles = array_map(function ($item) { $root = $item[0]; $file = $item[2]; - return new FileAsset($root . '/' . $file, array(), $root, $file); + // no need to minifiy minified files + if (substr($file, -strlen('.min.js')) === '.min.js') { + return new FileAsset($root . '/' . $file, array(), $root, $file); + } + return new FileAsset($root . '/' . $file, array( + new JSMinFilter() + ), $root, $file); }, $jsFiles); $jsCollection = new AssetCollection($jsFiles); $jsCollection->setTargetPath("assets/$jsHash.js"); @@ -170,12 +189,13 @@ class OC_TemplateLayout extends OC_Template { $sourceRoot = \OC::$SERVERROOT; $sourcePath = substr($assetPath, strlen(\OC::$SERVERROOT)); return new FileAsset( - $assetPath, + $assetPath, array( - new CssRewriteFilter(), + new CssRewriteFilter(), + new CssMinFilter(), new CssImportFilter() ), - $sourceRoot, + $sourceRoot, $sourcePath ); }, $cssFiles); @@ -194,8 +214,8 @@ class OC_TemplateLayout extends OC_Template { * @param array $files * @return string */ - private static function hashScriptNames($files) - { + private static function hashScriptNames($files) { + $files = array_map(function ($item) { $root = $item[0]; $file = $item[2]; @@ -204,36 +224,7 @@ class OC_TemplateLayout extends OC_Template { sort($files); // include the apps' versions hash to invalidate the cached assets - $files[]= self::$versionHash; + $files[] = self::$versionHash; return hash('md5', implode('', $files)); } - - /** - * @return bool - */ - private function isAssetPipelineEnabled() { - // asset management enabled? - $useAssetPipeline = OC_Config::getValue('asset-pipeline.enabled', false); - if (!$useAssetPipeline) { - return false; - } - - // assets folder exists? - $assetDir = \OC::$SERVERROOT . '/assets'; - if (!is_dir($assetDir)) { - if (!mkdir($assetDir)) { - \OCP\Util::writeLog('assets', - "Folder <$assetDir> does not exist and/or could not be generated.", \OCP\Util::ERROR); - return false; - } - } - - // assets folder can be accessed? - if (!touch($assetDir."/.oc")) { - \OCP\Util::writeLog('assets', - "Folder <$assetDir> could not be accessed.", \OCP\Util::ERROR); - return false; - } - return $useAssetPipeline; - } } diff --git a/lib/repair/assetcache.php b/lib/repair/assetcache.php new file mode 100644 index 0000000000..d7677a10d1 --- /dev/null +++ b/lib/repair/assetcache.php @@ -0,0 +1,30 @@ + + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +namespace OC\Repair; + +use Doctrine\DBAL\Platforms\MySqlPlatform; +use OC\Hooks\BasicEmitter; + +class AssetCache extends BasicEmitter implements \OC\RepairStep { + + public function getName() { + return 'Clear asset cache after upgrade'; + } + + public function run() { + if (!\OC_Template::isAssetPipelineEnabled()) { + $this->emit('\OC\Repair', 'info', array('Asset pipeline disabled -> nothing to do')); + return; + } + $assetDir = \OC::$SERVERROOT . '/assets'; + \OC_Helper::rmdirr($assetDir, false); + $this->emit('\OC\Repair', 'info', array('Asset cache cleared.')); + } +} +