silverstripe-textextraction/code/extensions/FileTextExtractable.php

43 lines
1.3 KiB
PHP
Raw Normal View History

2012-08-22 17:52:08 +02:00
<?php
/**
* 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.
*
* Adds an additional property which is the cached contents, which is populated on demand.
*
* @author mstephens
*
*/
2012-08-22 18:24:38 +02:00
class FileTextExtractable extends DataExtension {
2012-08-22 17:52:08 +02:00
2013-05-07 21:54:51 +02:00
private static $db = array(
2012-08-22 17:52:08 +02:00
'FileContentCache' => 'Text'
);
/**
* 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.
*
* @param $forceParse If false, the file content is only parsed on demand. If true, the content parsing is forced, bypassing the
* cached version
* @return String
*/
function extractFileAsText($forceParse = false) {
if (!$forceParse && $this->owner->FileContentCache) return $this->owner->FileContentCache;
// Determine which extractor can process this file.
$extractor = FileTextExtractor::for_file($this->owner->FullPath);
2012-08-22 17:52:08 +02:00
if (!$extractor) return null;
$text = $extractor->getContent($this->owner->FullPath);
2012-08-22 17:52:08 +02:00
if (!$text) return null;
$this->owner->FileContentCache = $text;
$this->owner->write();
return $text;
}
}
?>