1 | <?php |
||
33 | class HtmlContentExtractor |
||
34 | { |
||
35 | |||
36 | /** |
||
37 | * Unicode ranges which should get stripped before sending a document to solr. |
||
38 | * This is necessary if a document (PDF, etc.) contains unicode characters which |
||
39 | * are valid in the font being used in the document but are not available in the |
||
40 | * font being used for displaying results. |
||
41 | * |
||
42 | * This is often the case if PDFs are being indexed where special fonts are used |
||
43 | * for displaying bullets, etc. Usually those bullets reside in one of the unicode |
||
44 | * "Private Use Zones" or the "Private Use Area" (plane 15 + 16) |
||
45 | * |
||
46 | * @see http://en.wikipedia.org/wiki/Unicode_block |
||
47 | * @var array |
||
48 | */ |
||
49 | protected static $stripUnicodeRanges = [ |
||
50 | ['FFFD', 'FFFD'], |
||
51 | // Replacement Character (�) @see http://en.wikipedia.org/wiki/Specials_%28Unicode_block%29 |
||
52 | ['E000', 'F8FF'], |
||
53 | // Private Use Area (part of Plane 0) |
||
54 | ['F0000', 'FFFFF'], |
||
55 | // Supplementary Private Use Area (Plane 15) |
||
56 | ['100000', '10FFFF'], |
||
57 | // Supplementary Private Use Area (Plane 16) |
||
58 | ]; |
||
59 | /** |
||
60 | * The raw HTML markup content to extract clean content from. |
||
61 | * |
||
62 | * @var string |
||
63 | */ |
||
64 | protected $content; |
||
65 | /** |
||
66 | * Mapping of HTML tags to Solr document fields. |
||
67 | * |
||
68 | * @var array |
||
69 | */ |
||
70 | protected $tagToFieldMapping = [ |
||
71 | 'h1' => 'tagsH1', |
||
72 | 'h2' => 'tagsH2H3', |
||
73 | 'h3' => 'tagsH2H3', |
||
74 | 'h4' => 'tagsH4H5H6', |
||
75 | 'h5' => 'tagsH4H5H6', |
||
76 | 'h6' => 'tagsH4H5H6', |
||
77 | 'u' => 'tagsInline', |
||
78 | 'b' => 'tagsInline', |
||
79 | 'strong' => 'tagsInline', |
||
80 | 'i' => 'tagsInline', |
||
81 | 'em' => 'tagsInline', |
||
82 | 'a' => 'tagsA', |
||
83 | ]; |
||
84 | |||
85 | /** |
||
86 | * @var TypoScriptConfiguration |
||
87 | */ |
||
88 | private $configuration; |
||
89 | |||
90 | /** |
||
91 | * Constructor. |
||
92 | * |
||
93 | * @param string $content Content HTML markup |
||
94 | */ |
||
95 | 73 | public function __construct($content) |
|
99 | |||
100 | /** |
||
101 | * @return TypoScriptConfiguration|array |
||
102 | */ |
||
103 | 72 | protected function getConfiguration() |
|
104 | { |
||
105 | 72 | if ($this->configuration == null) { |
|
106 | 62 | $this->configuration = Util::getSolrConfiguration(); |
|
107 | } |
||
108 | |||
109 | 72 | return $this->configuration; |
|
110 | } |
||
111 | |||
112 | /** |
||
113 | * @param TypoScriptConfiguration $configuration |
||
114 | */ |
||
115 | 10 | public function setConfiguration(TypoScriptConfiguration $configuration) |
|
119 | |||
120 | /** |
||
121 | * Returns the cleaned indexable content from the page's HTML markup. |
||
122 | * |
||
123 | * The content is cleaned from HTML tags and control chars Solr could |
||
124 | * stumble on. |
||
125 | * |
||
126 | * @return string Indexable, cleaned content ready for indexing. |
||
127 | */ |
||
128 | public function getIndexableContent() |
||
138 | |||
139 | /** |
||
140 | * Strips html tags, and tab, new-line, carriage-return, whitespace |
||
141 | * characters. |
||
142 | * |
||
143 | * @param string $content String to clean |
||
144 | * @return string String cleaned from tags and special whitespace characters |
||
145 | */ |
||
146 | 70 | public static function cleanContent($content) |
|
147 | { |
||
148 | 70 | $content = self::stripControlCharacters($content); |
|
149 | // remove Javascript |
||
150 | 70 | $content = preg_replace('@<script[^>]*>.*?<\/script>@msi', '', $content); |
|
151 | |||
152 | // remove internal CSS styles |
||
153 | 70 | $content = preg_replace('@<style[^>]*>.*?<\/style>@msi', '', $content); |
|
154 | |||
155 | // prevents concatenated words when stripping tags afterwards |
||
156 | 70 | $content = str_replace(['<', '>'], [' <', '> '], $content); |
|
157 | 70 | $content = static::stripTags($content); |
|
158 | |||
159 | 70 | $content = str_replace(["\t", "\n", "\r", ' '], ' ', $content); |
|
160 | 70 | $content = self::stripUnicodeRanges($content); |
|
161 | 70 | $content = trim($content); |
|
162 | |||
163 | 70 | return $content; |
|
164 | } |
||
165 | |||
166 | /** |
||
167 | * Strips html tags, but keeps single < and > characters. |
||
168 | * |
||
169 | * @param string $content |
||
170 | * @return mixed |
||
171 | */ |
||
172 | 70 | protected static function stripTags($content) |
|
173 | { |
||
174 | 70 | $content = preg_replace('@<([^>]+(<|\z))@msi', '##lt##$1', $content); |
|
175 | 70 | $content = strip_tags($content); |
|
176 | // unescape < that are not used to open a tag |
||
177 | 70 | return str_replace('##lt##', '<', $content); |
|
178 | } |
||
179 | |||
180 | /** |
||
181 | * Strips control characters that cause Jetty/Solr to fail. |
||
182 | * |
||
183 | * @param string $content the content to sanitize |
||
184 | * @return string the sanitized content |
||
185 | * @see http://w3.org/International/questions/qa-forms-utf-8.html |
||
186 | */ |
||
187 | 70 | public static function stripControlCharacters($content) |
|
188 | { |
||
189 | // Printable utf-8 does not include any of these chars below x7F |
||
190 | 70 | return preg_replace('@[\x00-\x08\x0B\x0C\x0E-\x1F]@', ' ', $content); |
|
191 | } |
||
192 | |||
193 | /** |
||
194 | * Strips unusable unicode ranges |
||
195 | * |
||
196 | * @param string $content Content to sanitize |
||
197 | * @return string Sanitized content |
||
198 | */ |
||
199 | 70 | public static function stripUnicodeRanges($content) |
|
200 | { |
||
201 | 70 | foreach (self::$stripUnicodeRanges as $range) { |
|
202 | 70 | $content = self::stripUnicodeRange($content, $range[0], $range[1]); |
|
203 | } |
||
204 | |||
205 | 70 | return $content; |
|
206 | } |
||
207 | |||
208 | /** |
||
209 | * Strips a UTF-8 character range |
||
210 | * |
||
211 | * @param string $content Content to sanitize |
||
212 | * @param string $start Unicode range start character as uppercase hexadecimal string |
||
213 | * @param string $end Unicode range end character as uppercase hexadecimal string |
||
214 | * @return string Sanitized content |
||
215 | */ |
||
216 | 70 | public static function stripUnicodeRange($content, $start, $end) |
|
221 | |||
222 | /** |
||
223 | * Shortcut method to retrieve the raw content marked for indexing. |
||
224 | * |
||
225 | * @return string Content marked for indexing. |
||
226 | */ |
||
227 | 1 | public function getContentMarkedForIndexing() |
|
231 | |||
232 | /** |
||
233 | * Extracts HTML tag content from tags in the content marked for indexing. |
||
234 | * |
||
235 | * @return array A mapping of Solr document field names to content found in defined tags. |
||
236 | */ |
||
237 | 63 | public function getTagContent() |
|
269 | } |
||
270 |