2012-08-22 17:52:08 +02:00
|
|
|
<?php
|
|
|
|
|
2017-12-20 22:24:39 +01:00
|
|
|
namespace SilverStripe\TextExtraction\Extension;
|
|
|
|
|
2018-07-03 05:55:02 +02:00
|
|
|
use SilverStripe\Assets\File;
|
2018-07-03 01:23:27 +02:00
|
|
|
use SilverStripe\ORM\DataExtension;
|
|
|
|
use SilverStripe\TextExtraction\Cache\FileTextCache;
|
|
|
|
use SilverStripe\TextExtraction\Extractor\FileTextExtractor;
|
2017-12-20 22:24:39 +01:00
|
|
|
|
2012-08-22 17:52:08 +02:00
|
|
|
/**
|
|
|
|
* Decorate File or a File derivative to enable text extraction from the file content. Uses a set of subclasses of
|
|
|
|
* FileTextExtractor to do the extraction based on the content type of the file.
|
2017-12-20 22:24:39 +01:00
|
|
|
*
|
2012-08-22 17:52:08 +02:00
|
|
|
* Adds an additional property which is the cached contents, which is populated on demand.
|
|
|
|
*
|
|
|
|
* @author mstephens
|
|
|
|
*/
|
2015-11-18 05:07:31 +01:00
|
|
|
class FileTextExtractable extends DataExtension
|
|
|
|
{
|
2017-12-20 22:24:39 +01:00
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
* @config
|
|
|
|
*/
|
2018-07-03 01:23:27 +02:00
|
|
|
private static $db = [
|
2015-11-18 05:07:31 +01:00
|
|
|
'FileContentCache' => 'Text'
|
2018-07-03 01:23:27 +02:00
|
|
|
];
|
2012-08-22 17:52:08 +02:00
|
|
|
|
2017-12-20 22:24:39 +01:00
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
* @config
|
|
|
|
*/
|
2018-07-03 01:23:27 +02:00
|
|
|
private static $casting = [
|
2015-11-18 05:07:31 +01:00
|
|
|
'FileContent' => 'Text'
|
2018-07-03 01:23:27 +02:00
|
|
|
];
|
2015-02-18 03:31:38 +01:00
|
|
|
|
2017-12-20 22:24:39 +01:00
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
* @config
|
|
|
|
*/
|
2018-07-03 01:23:27 +02:00
|
|
|
private static $dependencies = [
|
2018-07-03 05:55:02 +02:00
|
|
|
'TextCache' => '%$' . FileTextCache::class,
|
2018-07-03 01:23:27 +02:00
|
|
|
];
|
2015-05-05 05:52:01 +02:00
|
|
|
|
2015-11-18 05:07:31 +01:00
|
|
|
/**
|
|
|
|
* @var FileTextCache
|
|
|
|
*/
|
|
|
|
protected $fileTextCache = null;
|
2015-05-05 05:52:01 +02:00
|
|
|
|
2015-11-18 05:07:31 +01:00
|
|
|
/**
|
2017-12-20 22:24:39 +01:00
|
|
|
* @param FileTextCache $cache
|
2018-07-03 01:23:27 +02:00
|
|
|
* @return $this
|
2015-11-18 05:07:31 +01:00
|
|
|
*/
|
|
|
|
public function setTextCache(FileTextCache $cache)
|
|
|
|
{
|
|
|
|
$this->fileTextCache = $cache;
|
2018-07-03 01:23:27 +02:00
|
|
|
return $this;
|
2015-11-18 05:07:31 +01:00
|
|
|
}
|
2015-05-05 05:52:01 +02:00
|
|
|
|
2015-11-18 05:07:31 +01:00
|
|
|
/**
|
|
|
|
* @return FileTextCache
|
|
|
|
*/
|
|
|
|
public function getTextCache()
|
|
|
|
{
|
|
|
|
return $this->fileTextCache;
|
|
|
|
}
|
2015-05-05 05:52:01 +02:00
|
|
|
|
2015-11-18 05:07:31 +01:00
|
|
|
/**
|
|
|
|
* Helper function for template
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getFileContent()
|
|
|
|
{
|
|
|
|
return $this->extractFileAsText();
|
|
|
|
}
|
2015-02-18 03:31:38 +01:00
|
|
|
|
2015-11-18 05:07:31 +01:00
|
|
|
/**
|
2018-07-03 01:37:38 +02:00
|
|
|
* Tries to parse the file contents if a FileTextExtractor class exists to handle the file type, and
|
|
|
|
* returns the text. The value is also cached into the File record itself.
|
2017-12-20 22:24:39 +01:00
|
|
|
*
|
2015-11-18 05:07:31 +01:00
|
|
|
* @param boolean $disableCache If false, the file content is only parsed on demand.
|
2017-12-20 22:24:39 +01:00
|
|
|
* If true, the content parsing is forced, bypassing
|
|
|
|
* the cached version
|
2018-07-03 01:23:27 +02:00
|
|
|
* @return string|null
|
2015-11-18 05:07:31 +01:00
|
|
|
*/
|
|
|
|
public function extractFileAsText($disableCache = false)
|
|
|
|
{
|
2018-07-03 05:55:02 +02:00
|
|
|
/** @var File $file */
|
|
|
|
$file = $this->owner;
|
2015-11-18 05:07:31 +01:00
|
|
|
if (!$disableCache) {
|
2018-07-03 05:55:02 +02:00
|
|
|
$text = $this->getTextCache()->load($file);
|
2015-11-18 05:07:31 +01:00
|
|
|
if ($text) {
|
|
|
|
return $text;
|
|
|
|
}
|
|
|
|
}
|
2012-08-22 17:52:08 +02:00
|
|
|
|
2015-11-18 05:07:31 +01:00
|
|
|
// Determine which extractor can process this file.
|
2018-07-03 05:55:02 +02:00
|
|
|
$extractor = FileTextExtractor::for_file($file);
|
2015-11-18 05:07:31 +01:00
|
|
|
if (!$extractor) {
|
|
|
|
return null;
|
|
|
|
}
|
2012-08-22 17:52:08 +02:00
|
|
|
|
2018-07-03 05:55:02 +02:00
|
|
|
$text = $extractor->getContent($file);
|
2015-11-18 05:07:31 +01:00
|
|
|
if (!$text) {
|
|
|
|
return null;
|
|
|
|
}
|
2012-08-22 17:52:08 +02:00
|
|
|
|
2017-02-22 03:17:32 +01:00
|
|
|
if (!$disableCache) {
|
2018-07-03 05:55:02 +02:00
|
|
|
$this->getTextCache()->save($file, $text);
|
2017-02-22 03:17:32 +01:00
|
|
|
}
|
2012-08-22 17:52:08 +02:00
|
|
|
|
2015-11-18 05:07:31 +01:00
|
|
|
return $text;
|
|
|
|
}
|
2015-05-12 05:56:20 +02:00
|
|
|
|
2017-12-20 22:24:39 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2015-11-18 05:07:31 +01:00
|
|
|
public function onBeforeWrite()
|
|
|
|
{
|
|
|
|
// Clear cache before changing file
|
|
|
|
$this->getTextCache()->invalidate($this->owner);
|
|
|
|
}
|
2012-08-22 17:52:08 +02:00
|
|
|
}
|