|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SilverStripe\TextExtraction\Extractor; |
|
4
|
|
|
|
|
5
|
|
|
use SilverStripe\Assets\File; |
|
6
|
|
|
use SilverStripe\Core\ClassInfo; |
|
7
|
|
|
use SilverStripe\Core\Config\Config; |
|
8
|
|
|
use SilverStripe\Core\Config\Configurable; |
|
9
|
|
|
use SilverStripe\Core\Injector\Injectable; |
|
10
|
|
|
use SilverStripe\Core\Injector\Injector; |
|
11
|
|
|
use SilverStripe\TextExtraction\Extractor\FileTextExtractor\Exception; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* A decorator for File or a subclass that provides a method for extracting full-text from the file's external contents. |
|
15
|
|
|
* @author mstephens |
|
16
|
|
|
*/ |
|
17
|
|
|
abstract class FileTextExtractor |
|
18
|
|
|
{ |
|
19
|
|
|
use Configurable; |
|
20
|
|
|
use Injectable; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Set priority from 0-100. |
|
24
|
|
|
* The highest priority extractor for a given content type will be selected. |
|
25
|
|
|
* |
|
26
|
|
|
* @config |
|
27
|
|
|
* @var integer |
|
28
|
|
|
*/ |
|
29
|
|
|
private static $priority = 50; |
|
|
|
|
|
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Cache of extractor class names, sorted by priority |
|
33
|
|
|
* |
|
34
|
|
|
* @var array |
|
35
|
|
|
*/ |
|
36
|
|
|
protected static $sorted_extractor_classes = null; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Gets the list of prioritised extractor classes |
|
40
|
|
|
* |
|
41
|
|
|
* @return array |
|
42
|
|
|
*/ |
|
43
|
|
|
protected static function get_extractor_classes() |
|
44
|
|
|
{ |
|
45
|
|
|
// Check cache |
|
46
|
|
|
if (self::$sorted_extractor_classes) { |
|
|
|
|
|
|
47
|
|
|
return self::$sorted_extractor_classes; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
// Generate the sorted list of extractors on demand. |
|
51
|
|
|
$classes = ClassInfo::subclassesFor(__CLASS__); |
|
52
|
|
|
array_shift($classes); |
|
53
|
|
|
$classPriorities = []; |
|
54
|
|
|
|
|
55
|
|
|
foreach ($classes as $class) { |
|
56
|
|
|
$classPriorities[$class] = Config::inst()->get($class, 'priority'); |
|
57
|
|
|
} |
|
58
|
|
|
arsort($classPriorities); |
|
59
|
|
|
|
|
60
|
|
|
// Save classes |
|
61
|
|
|
$sortedClasses = array_keys($classPriorities); |
|
62
|
|
|
return self::$sorted_extractor_classes = $sortedClasses; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Get the text file extractor for the given class |
|
67
|
|
|
* |
|
68
|
|
|
* @param string $class |
|
69
|
|
|
* @return FileTextExtractor |
|
70
|
|
|
*/ |
|
71
|
|
|
protected static function get_extractor($class) |
|
72
|
|
|
{ |
|
73
|
|
|
return Injector::inst()->get($class); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Given a File object, decide which extractor instance to use to handle it |
|
78
|
|
|
* |
|
79
|
|
|
* @param File $file |
|
80
|
|
|
* @return FileTextExtractor|null |
|
81
|
|
|
*/ |
|
82
|
|
|
public static function for_file(File $file) |
|
83
|
|
|
{ |
|
84
|
|
|
if (!$file) { |
|
|
|
|
|
|
85
|
|
|
return null; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
$extension = $file->getExtension(); |
|
89
|
|
|
$mime = $file->getMimeType(); |
|
90
|
|
|
|
|
91
|
|
|
foreach (self::get_extractor_classes() as $className) { |
|
92
|
|
|
$extractor = self::get_extractor($className); |
|
93
|
|
|
|
|
94
|
|
|
// Skip unavailable extractors |
|
95
|
|
|
if (!$extractor->isAvailable()) { |
|
96
|
|
|
continue; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
// Check extension |
|
100
|
|
|
if ($extension && $extractor->supportsExtension($extension)) { |
|
101
|
|
|
return $extractor; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
// Check mime |
|
105
|
|
|
if ($mime && $extractor->supportsMime($mime)) { |
|
106
|
|
|
return $extractor; |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Some text extractors (like pdftotext) may require a physical file to read from, so write the current |
|
113
|
|
|
* file contents to a temp file and return its path |
|
114
|
|
|
* |
|
115
|
|
|
* @param File $file |
|
116
|
|
|
* @return string |
|
117
|
|
|
* @throws Exception |
|
118
|
|
|
*/ |
|
119
|
|
|
protected function getPathFromFile(File $file) |
|
120
|
|
|
{ |
|
121
|
|
|
$path = tempnam(TEMP_PATH, 'pdftextextractor_'); |
|
122
|
|
|
if (false === $path) { |
|
123
|
|
|
throw new Exception(static::class . '->getPathFromFile() could not allocate temporary file name'); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
// Append extension to temp file if one is set |
|
127
|
|
|
if ($file->getExtension()) { |
|
128
|
|
|
$path .= '.' . $file->getExtension(); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
// Remove any existing temp files with this name |
|
132
|
|
|
if (file_exists($path)) { |
|
133
|
|
|
unlink($path); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
$bytesWritten = file_put_contents($path, $file->getStream()); |
|
137
|
|
|
if (false === $bytesWritten) { |
|
138
|
|
|
throw new Exception(static::class . '->getPathFromFile() failed to write temporary file'); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
return $path; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* Checks if the extractor is supported on the current environment, |
|
146
|
|
|
* for example if the correct binaries or libraries are available. |
|
147
|
|
|
* |
|
148
|
|
|
* @return boolean |
|
149
|
|
|
*/ |
|
150
|
|
|
abstract public function isAvailable(); |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* Determine if this extractor supports the given extension. |
|
154
|
|
|
* If support is determined by mime/type only, then this should return false. |
|
155
|
|
|
* |
|
156
|
|
|
* @param string $extension |
|
157
|
|
|
* @return boolean |
|
158
|
|
|
*/ |
|
159
|
|
|
abstract public function supportsExtension($extension); |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* Determine if this extractor supports the given mime type. |
|
163
|
|
|
* Will only be called if supportsExtension returns false. |
|
164
|
|
|
* |
|
165
|
|
|
* @param string $mime |
|
166
|
|
|
* @return boolean |
|
167
|
|
|
*/ |
|
168
|
|
|
abstract public function supportsMime($mime); |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* Given a File instance, extract the contents as text. |
|
172
|
|
|
* |
|
173
|
|
|
* @param File|string $file Either the File instance, or a file path for a file to load |
|
174
|
|
|
* @return string |
|
175
|
|
|
*/ |
|
176
|
|
|
abstract public function getContent($file); |
|
177
|
|
|
} |
|
178
|
|
|
|