1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Decorate File or a File derivative to enable text extraction from the file content. Uses a set of subclasses of |
5
|
|
|
* FileTextExtractor to do the extraction based on the content type of the file. |
6
|
|
|
* |
7
|
|
|
* Adds an additional property which is the cached contents, which is populated on demand. |
8
|
|
|
* |
9
|
|
|
* @author mstephens |
10
|
|
|
* |
11
|
|
|
*/ |
12
|
|
|
class FileTextExtractable extends DataExtension |
|
|
|
|
13
|
|
|
{ |
14
|
|
|
private static $db = array( |
|
|
|
|
15
|
|
|
'FileContentCache' => 'Text' |
16
|
|
|
); |
17
|
|
|
|
18
|
|
|
private static $casting = array( |
|
|
|
|
19
|
|
|
'FileContent' => 'Text' |
20
|
|
|
); |
21
|
|
|
|
22
|
|
|
private static $dependencies = array( |
|
|
|
|
23
|
|
|
'TextCache' => '%$FileTextCache' |
24
|
|
|
); |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var FileTextCache |
28
|
|
|
*/ |
29
|
|
|
protected $fileTextCache = null; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* |
33
|
|
|
* @param FileTextCache $cache |
34
|
|
|
*/ |
35
|
|
|
public function setTextCache(FileTextCache $cache) |
36
|
|
|
{ |
37
|
|
|
$this->fileTextCache = $cache; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @return FileTextCache |
42
|
|
|
*/ |
43
|
|
|
public function getTextCache() |
44
|
|
|
{ |
45
|
|
|
return $this->fileTextCache; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Helper function for template |
50
|
|
|
* |
51
|
|
|
* @return string |
52
|
|
|
*/ |
53
|
|
|
public function getFileContent() |
54
|
|
|
{ |
55
|
|
|
return $this->extractFileAsText(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Tries to parse the file contents if a FileTextExtractor class exists to handle the file type, and returns the text. |
60
|
|
|
* The value is also cached into the File record itself. |
61
|
|
|
* |
62
|
|
|
* @param boolean $disableCache If false, the file content is only parsed on demand. |
63
|
|
|
* If true, the content parsing is forced, bypassing the cached version |
64
|
|
|
* @return string |
65
|
|
|
*/ |
66
|
|
|
public function extractFileAsText($disableCache = false) |
67
|
|
|
{ |
68
|
|
|
if (!$disableCache) { |
69
|
|
|
$text = $this->getTextCache()->load($this->owner); |
70
|
|
|
if ($text) { |
71
|
|
|
return $text; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
// Determine which extractor can process this file. |
76
|
|
|
$extractor = FileTextExtractor::for_file($this->owner->FullPath); |
77
|
|
|
if (!$extractor) { |
78
|
|
|
return null; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
$text = $extractor->getContent($this->owner->FullPath); |
82
|
|
|
if (!$text) { |
83
|
|
|
return null; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$this->getTextCache()->save($this->owner, $text); |
87
|
|
|
|
88
|
|
|
return $text; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function onBeforeWrite() |
92
|
|
|
{ |
93
|
|
|
// Clear cache before changing file |
94
|
|
|
$this->getTextCache()->invalidate($this->owner); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.