From 25e5c0cf9d5c225cbc8604986a961f7d1447b87f Mon Sep 17 00:00:00 2001 From: Alexander Ebert Date: Mon, 25 Jul 2011 16:04:58 +0200 Subject: [PATCH] Merge pull request #27 from wbb3addons/directoryUtilUse --- .../data/language/LanguageEditor.class.php | 21 ++++++------- .../cache/source/DiskCacheSource.class.php | 31 ++++++------------- .../system/template/TemplateEngine.class.php | 8 ++--- 3 files changed, 22 insertions(+), 38 deletions(-) diff --git a/wcfsetup/install/files/lib/data/language/LanguageEditor.class.php b/wcfsetup/install/files/lib/data/language/LanguageEditor.class.php index 57d4bea1d8..0f7037585c 100644 --- a/wcfsetup/install/files/lib/data/language/LanguageEditor.class.php +++ b/wcfsetup/install/files/lib/data/language/LanguageEditor.class.php @@ -12,6 +12,7 @@ use wcf\system\language\LanguageFactory; use wcf\system\package\PackageDependencyHandler; use wcf\system\WCF; use wcf\util\XML; +use wcf\util\DirectoryUtil; /** * Provides functions to edit languages. @@ -284,13 +285,12 @@ class LanguageEditor extends DatabaseObjectEditor { * @param string $category * @param string $packageID */ - public static function deleteLanguageFiles($languageID = '*', $category = '*', $packageID = '*') { - $files = @glob(WCF_DIR."language/".$packageID."_".$languageID."_".$category.".php"); - if (is_array($files)) { - foreach ($files as $filename) { - @unlink($filename); - } - } + public static function deleteLanguageFiles($languageID = '.*', $category = '.*', $packageID = '.*') { + if ($category != '.*') $category = preg_quote($category, '~'); + if ($languageID != '.*') $languageID = intval($languageID); + if ($packageID != '.*') $packageID = intval($packageID); + + DirectoryUtil::getInstance(WCF_DIR.'language/')->deletePattern('~'.$packageID.'_'.$languageID.'_'.$category.'\.php$~'); } /** @@ -298,12 +298,9 @@ class LanguageEditor extends DatabaseObjectEditor { */ public function deleteCompiledTemplates() { // templates - $filenames = glob(WCF_DIR.'templates/compiled/*_'.$this->languageID.'_*.php'); - if ($filenames) foreach ($filenames as $filename) @unlink($filename); - + DirectoryUtil::getInstance(WCF_DIR.'templates/compiled/')->deletePattern('~.*_'.$this->languageID.'_.*\.php$~'); // acp templates - $filenames = glob(WCF_DIR.'acp/templates/compiled/*_'.$this->languageID.'_*.php'); - if ($filenames) foreach ($filenames as $filename) @unlink($filename); + DirectoryUtil::getInstance(WCF_DIR.'acp/templates/compiled/')->deletePattern('~.*_'.$this->languageID.'_.*\.php$~'); } /** diff --git a/wcfsetup/install/files/lib/system/cache/source/DiskCacheSource.class.php b/wcfsetup/install/files/lib/system/cache/source/DiskCacheSource.class.php index 6b9d9f4b5c..aff0441963 100644 --- a/wcfsetup/install/files/lib/system/cache/source/DiskCacheSource.class.php +++ b/wcfsetup/install/files/lib/system/cache/source/DiskCacheSource.class.php @@ -4,6 +4,7 @@ use wcf\system\exception\SystemException; use wcf\system\io\File; use wcf\system\WCF; use wcf\util\FileUtil; +use wcf\util\DirectoryUtil; /** * DiskCacheSource is an implementation of CacheSource that stores the cache as simple files in the file system. @@ -87,19 +88,15 @@ class DiskCacheSource implements ICacheSource { */ public function clear($directory, $filepattern, $forceDelete = false) { $filepattern = str_replace('*', '.*', str_replace('.', '\.', $filepattern)); - $directory = FileUtil::addTrailingSlash($directory); - - if (@file_exists($directory)) { - $dirh = opendir($directory); - while ($filename = readdir($dirh)) { - if ($filename != '.' && $filename != '..' && preg_match('%^'.$filepattern.'$%i', $filename)) { - if ($forceDelete || !@touch($directory.$filename, 1)) { - @unlink($directory.$filename); - } - } - } - closedir($dirh); + if (substr($directory, -1) != '/') { + $directory .= '/'; } + + DirectoryUtil::getInstance($directory)->executeCallback(function ($filename) use ($forceDelete) { + if ($forceDelete || !@touch($filename, 1)) { + @unlink($filename); + } + }, '%^'.$directory.$filepattern.'$%i'); } /** @@ -205,15 +202,7 @@ class DiskCacheSource implements ICacheSource { while ($row = $statement->fetchArray()) { $packageDir = FileUtil::getRealPath(WCF_DIR.$row['packageDir']); $cacheDir = $packageDir.'cache'; - if (file_exists($cacheDir)) { - // get files in cache directory - $files = glob($cacheDir.'/*.php'); - if (is_array($files)) { - foreach ($files as $file) { - @unlink($file); - } - } - } + DirectoryUtil::getInstance($cacheDir)->deletePattern('~.*\.php$'); } } } diff --git a/wcfsetup/install/files/lib/system/template/TemplateEngine.class.php b/wcfsetup/install/files/lib/system/template/TemplateEngine.class.php index 2db0782ad4..f17a10cbd3 100644 --- a/wcfsetup/install/files/lib/system/template/TemplateEngine.class.php +++ b/wcfsetup/install/files/lib/system/template/TemplateEngine.class.php @@ -6,6 +6,7 @@ use wcf\system\exception\SystemException; use wcf\system\SingletonFactory; use wcf\util\HeaderUtil; use wcf\util\StringUtil; +use wcf\util\DirectoryUtil; /** * TemplateEngine loads and displays template. @@ -528,12 +529,9 @@ class TemplateEngine extends SingletonFactory { */ public static function deleteCompiledTemplates($compileDir = '') { if (empty($compileDir)) $compileDir = WCF_DIR.'templates/compiled/'; - + // delete compiled templates - $matches = glob($compileDir . '*_*_*.php'); - if (is_array($matches)) { - foreach ($matches as $match) @unlink($match); - } + DirectoryUtil::getInstance($compileDir)->deletePattern('~.*_.*_.*\.php$'); } /** -- 2.20.1