TYPO3 v15 dev-main snapshot ()
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* It is free software; you can redistribute it and/or modify it under
|
||||
* the terms of the GNU General Public License, either version 2
|
||||
* of the License, or any later version.
|
||||
*
|
||||
* For the full copyright and license information, please read the
|
||||
* LICENSE.txt file that was distributed with this source code.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Core\Resource\TextExtraction;
|
||||
|
||||
use TYPO3\CMS\Core\Resource\FileInterface;
|
||||
use TYPO3\CMS\Core\Utility\PathUtility;
|
||||
|
||||
/**
|
||||
* A simple text extractor to extract text from plain text files.
|
||||
*/
|
||||
class PlainTextExtractor implements TextExtractorInterface
|
||||
{
|
||||
/**
|
||||
* Checks if the given file can be read by this extractor
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function canExtractText(FileInterface $file)
|
||||
{
|
||||
$canExtract = false;
|
||||
|
||||
if ($file->getMimeType() === 'text/plain') {
|
||||
$canExtract = true;
|
||||
}
|
||||
|
||||
return $canExtract;
|
||||
}
|
||||
|
||||
/**
|
||||
* The actual text extraction.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function extractText(FileInterface $file)
|
||||
{
|
||||
$localTempFile = $file->getForLocalProcessing(false);
|
||||
|
||||
// extract text
|
||||
$content = (string)file_get_contents($localTempFile);
|
||||
|
||||
// In case of remote storage, the temporary copy of the
|
||||
// original file in typo3temp must be removed
|
||||
// Simply compare the filenames, because the filename is so unique that
|
||||
// it is nearly impossible to have a file with this name in a storage
|
||||
if (PathUtility::basename($localTempFile) !== $file->getName()) {
|
||||
unlink($localTempFile);
|
||||
}
|
||||
|
||||
return $content;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* It is free software; you can redistribute it and/or modify it under
|
||||
* the terms of the GNU General Public License, either version 2
|
||||
* of the License, or any later version.
|
||||
*
|
||||
* For the full copyright and license information, please read the
|
||||
* LICENSE.txt file that was distributed with this source code.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Core\Resource\TextExtraction;
|
||||
|
||||
use TYPO3\CMS\Core\Resource\FileInterface;
|
||||
|
||||
/**
|
||||
* An interface for text extractors
|
||||
*/
|
||||
interface TextExtractorInterface
|
||||
{
|
||||
/**
|
||||
* Checks if the given file can be read by this extractor
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function canExtractText(FileInterface $file);
|
||||
|
||||
/**
|
||||
* The actual text extraction.
|
||||
*
|
||||
* Should return a string of the file's content
|
||||
*
|
||||
* @param FileInterface $file
|
||||
* @return string
|
||||
*/
|
||||
public function extractText(FileInterface $file);
|
||||
}
|
||||
@@ -0,0 +1,101 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* It is free software; you can redistribute it and/or modify it under
|
||||
* the terms of the GNU General Public License, either version 2
|
||||
* of the License, or any later version.
|
||||
*
|
||||
* For the full copyright and license information, please read the
|
||||
* LICENSE.txt file that was distributed with this source code.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Core\Resource\TextExtraction;
|
||||
|
||||
use TYPO3\CMS\Core\Resource\FileInterface;
|
||||
use TYPO3\CMS\Core\SingletonInterface;
|
||||
use TYPO3\CMS\Core\Utility\GeneralUtility;
|
||||
|
||||
class TextExtractorRegistry implements SingletonInterface
|
||||
{
|
||||
/**
|
||||
* Registered text extractor class names
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $textExtractorClasses = [];
|
||||
|
||||
/**
|
||||
* Instance cache for text extractor classes
|
||||
*
|
||||
* @var TextExtractorInterface[]
|
||||
*/
|
||||
protected $instances = [];
|
||||
|
||||
/**
|
||||
* Allows to register a text extractor class
|
||||
*
|
||||
* @param string $className
|
||||
* @throws \InvalidArgumentException
|
||||
*/
|
||||
public function registerTextExtractor($className)
|
||||
{
|
||||
if (!class_exists($className)) {
|
||||
throw new \InvalidArgumentException('The class "' . $className . '" you are trying to register is not available', 1422906893);
|
||||
}
|
||||
|
||||
if (!in_array(TextExtractorInterface::class, class_implements($className) ?: [], true)) {
|
||||
throw new \InvalidArgumentException($className . ' must implement interface' . TextExtractorInterface::class, 1422771427);
|
||||
}
|
||||
|
||||
$this->textExtractorClasses[] = $className;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all registered text extractor instances
|
||||
*
|
||||
* @return TextExtractorInterface[]
|
||||
*/
|
||||
public function getTextExtractorInstances()
|
||||
{
|
||||
if (empty($this->instances) && !empty($this->textExtractorClasses)) {
|
||||
foreach ($this->textExtractorClasses as $className) {
|
||||
$object = $this->createTextExtractorInstance($className);
|
||||
$this->instances[] = $object;
|
||||
}
|
||||
}
|
||||
|
||||
return $this->instances;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an instance of a certain text extractor class
|
||||
*
|
||||
* @param string $className
|
||||
* @return TextExtractorInterface
|
||||
*/
|
||||
protected function createTextExtractorInstance($className)
|
||||
{
|
||||
return GeneralUtility::makeInstance($className);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether any registered text extractor can deal with a given file
|
||||
* and returns it.
|
||||
*
|
||||
* @return TextExtractorInterface|null
|
||||
*/
|
||||
public function getTextExtractor(FileInterface $file)
|
||||
{
|
||||
foreach ($this->getTextExtractorInstances() as $textExtractor) {
|
||||
if ($textExtractor->canExtractText($file)) {
|
||||
return $textExtractor;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user