openrat-cms

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

commit e36cd05dd5f7c5ff489d498351828fd41f48acd8
parent 8740b59946e7215671fcc4eb8adfed45b9e31806
Author: dankert <openrat@jandankert.de>
Date:   Sun, 13 Feb 2022 20:14:39 +0100

Refactoring: removed duplicate code, use inheritance ;)

Diffstat:
Mmodules/cms/generator/FileGenerator.class.php | 2+-
Mmodules/cms/generator/FileHistoryContext.class.php | 19+++----------------
Mmodules/cms/generator/FileHistoryGenerator.class.php | 109++++++++-----------------------------------------------------------------------
Mmodules/cms/model/File.class.php | 1+
4 files changed, 16 insertions(+), 115 deletions(-)

diff --git a/modules/cms/generator/FileGenerator.class.php b/modules/cms/generator/FileGenerator.class.php @@ -44,7 +44,7 @@ class FileGenerator extends BaseGenerator * @param $file File * @return string */ - private function filterValue( $file ) + protected function filterValue( $file ) { $contentId = $file->contentid; diff --git a/modules/cms/generator/FileHistoryContext.class.php b/modules/cms/generator/FileHistoryContext.class.php @@ -8,10 +8,8 @@ namespace cms\generator; /** * The file history context, necessary for generating and publishing a file. */ -class FileHistoryContext extends BaseContext +class FileHistoryContext extends FileContext { - public $sourceObjectId; - /** * value id. * @@ -27,23 +25,12 @@ class FileHistoryContext extends BaseContext */ public function __construct($sourceObjectId,$valueId ) { - $this->sourceObjectId = $sourceObjectId; + parent::__construct($sourceObjectId,Producer::SCHEME_PREVIEW); $this->valueId = $valueId; - $this->scheme = Producer::SCHEME_PREVIEW; } public function getCacheKey() { - return [ - 'filehistory', - $this->sourceObjectId, - $this->valueId - ]; - } - - - public function getObjectId() - { - return $this->sourceObjectId; + return array_merge( parent::getCacheKey(),['history',$this->valueId] ); } } \ No newline at end of file diff --git a/modules/cms/generator/FileHistoryGenerator.class.php b/modules/cms/generator/FileHistoryGenerator.class.php @@ -10,123 +10,36 @@ use cms\model\Value; use logger\Logger; use util\exception\GeneratorException; -class FileHistoryGenerator extends BaseGenerator +class FileHistoryGenerator extends FileGenerator { /** * @param $fileContext FileHistoryContext */ public function __construct($fileContext ) { - $this->context = $fileContext; - } - - protected function generate() - { - $value = new Value(); - $value->loadWithId( $this->context->valueId); - - return $value->file; // Should we filter here? - } - - public function getPublicFilename() - { - return null; + parent::__construct( $fileContext ); } /** - * @param $file File + * Generate value in the wished version. * @return string */ - private function filterValue( $file ) + protected function generate() { - $contentId = $file->contentid; - - $totalSettings = $file->getTotalSettings(); - $proxyFileId = @$totalSettings['proxy-file-id']; - - if ( $proxyFileId ) { - $proxyFile = new File( $proxyFileId ); // This is a proxy for another file. - $proxyFile->load(); - $contentId = $proxyFile->contentid; - } - - $v = new Value(); - $v->contentid = $contentId; - - if ( $this->context->scheme == Producer::SCHEME_PREVIEW ) - $v->load(); - else - $v->loadPublished(); - - $value = $v->file; - - foreach(\util\ArrayUtils::getSubArray($totalSettings, array( 'filter')) as $filterEntry ) - { - $filterName = ucfirst(@$filterEntry['name']); - $extension = @$filterEntry['extension']; - - if ( $extension && strtolower($extension) != strtolower($file->getRealExtension()) ) - continue; // File extension does not match - - $filterType = $this->context->scheme==Producer::SCHEME_PUBLIC?'public':'preview'; - - $onPublish = (array) @$filterEntry['on']; - if ( ! $onPublish || in_array('all',$onPublish ) ) - $onPublish = ['edit','public','preview','show']; - - if ( $onPublish && ! in_array($filterType,$onPublish)) - continue; // Publish type does not match - - $parameter = (array) @$filterEntry['parameter']; - - $filterClassNameWithNS = 'cms\\generator\\filter\\' . $filterName.'Filter'; - - if ( !class_exists( $filterClassNameWithNS ) ) - throw new \LogicException("Filter '$filterName' does not exist."); - - /** @var AbstractFilter $filter */ - $filter = new $filterClassNameWithNS(); - $filter->context = $this->context; - - // Copy filter configuration to filter instance. - foreach( $parameter as $parameterName=>$parameterValue) { - if ( property_exists($filter,$parameterName)) - $filter->$parameterName = $parameterValue; - } - - - // Execute the filter. - Logger::debug("Filtering '$file->filename' with filter '$filterName'."); - - try { - - $value = $filter->filter( $value ); - } catch( \Exception $e ) { - // Filter has some undefined error. - Logger::warn( $e->getTraceAsString() ); - throw new GeneratorException('Could not generate file '.$file->objectid.'. Filter '.$filterName.' has an error.', $e ); - } - } - - return $value; + $value = new Value(); + $value->loadWithId( $this->context->valueId); + return $value->file; // Should we filter here? } /** - * Calculates the MIME type of this file. - * - * @return string + * Not useful: FileHistory will only be used in the preview. + * @return null */ - public function getMimeType() + public function getPublicFilename() { - $file = new File( $this->context->sourceObjectId ); - $file->load(); - $ext = strtolower( $file->getRealExtension() ); - - $mimeType = File::getMimeType( $ext ); - - return( $mimeType ); + return null; } } \ No newline at end of file diff --git a/modules/cms/model/File.class.php b/modules/cms/model/File.class.php @@ -570,6 +570,7 @@ SQL $this->size = $row['size']; } + // Because we are reading directly from the value table we must do base64-encoding here. $storeValueAsBase64 = DB::get()->conf['base64']; if ( $storeValueAsBase64 )