Parser.php
10.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
<?php
/**
* SearchWP Parser.
*
* @package SearchWP
* @author Jon Christopher
*/
namespace SearchWP;
include_once ABSPATH . 'wp-admin/includes/file.php';
/**
* Class Parser is responsible for extracting text from files.
*
* @since 4.0
*/
class Parser {
/**
* Extract text from this document.
*
* @since 4.0
* @param string $file The full file path.
* @param string $mime_type The file's mime type. Will be detected if not provided.
* @param mixed $data Additional data.
* @return string
*/
public static function extract_text( string $file, string $mime_type = '', $data = null ) {
if ( ! file_exists( $file ) ) {
return false;
}
if ( empty( $mime_type ) && function_exists( 'mime_content_type' ) ) {
$mime_type = mime_content_type( $file );
}
$mime_class = false;
foreach ( self::$mimes as $mimes_class => $mimes_class_mimes ) {
if ( in_array( $mime_type, $mimes_class_mimes ) ) {
$mime_class = $mimes_class;
break;
}
}
if ( empty( $mime_class ) ) {
return false;
}
switch ( $mime_class ) {
case 'pdf':
$content = self::extract_pdf_content( $file, $data );
break;
case 'text':
$content = self::extract_text_content( $file, $data );
break;
case 'richtext':
$content = self::extract_rich_text_content( $file, $data );
break;
case 'msoffice_word':
// .doc is not supported.
if ( 'application/msword' !== $mime_type ) {
$content = self::extract_msoffice_docx_text( $file, $data, $mime_type );
}
break;
case 'msoffice_excel':
$content = self::extract_msoffice_excel_text( $file, $data, $mime_type );
break;
case 'msoffice_powerpoint':
$content = self::extract_msoffice_powerpoint_text( $file, $data, $mime_type );
break;
default:
$content = '';
}
return $content;
}
/**
* Extract PDF content from the file.
*
* @since 4.0
* @param string $file The full path to the file.
* @param mixed $data Additional data for this call.
* @return string|boolean
*/
private static function extract_pdf_content( string $file, $data = null ) {
$content = apply_filters( 'searchwp\parser\pdf', '', [ 'file' => $file, 'data' => $data ] );
// If the content was populated externally, bail out.
if ( ! file_exists( $file ) || ! empty( $content ) ) {
return $content;
}
$pdf_parser = new \SearchWP\Dependencies\Smalot\PdfParser\Parser();
try {
$content = (string) $pdf_parser->parseFile( $file )->getText();
} catch (\Exception $e) {
do_action(
'searchwp\debug\log',
'PDF text extraction failed: ' . sanitize_text_field( $e->getMessage() ),
'parser'
);
$content = false;
}
return $content;
}
/**
* Extract plaintext content from the file.
*
* @since 4.0
* @param string $file The full path to the file.
* @param mixed $data Additional data for this call.
* @return string|boolean
*/
private static function extract_text_content( string $file, $data = null) {
$content = apply_filters( 'searchwp\parser\text', '', [ 'file' => $file, 'data' => $data ] );
// If the content was populated externally, bail out.
if ( ! file_exists( $file ) || ! empty( $content ) ) {
return $content;
}
return self::wp_filesystem_get_contents( $file );
}
/**
* Extract Rich text content from the file.
*
* @since 4.0
* @param string $file The full path to the file.
* @param mixed $data Additional data for this call.
* @return string|boolean
*/
private static function extract_rich_text_content( string $file, $data = null) {
$content = apply_filters( 'searchwp\parser\richtext', '', [ 'file' => $file, 'data' => $data ] );
// If the content was populated externally, bail out.
if ( ! file_exists( $file ) || ! empty( $content ) ) {
return $content;
}
$rtf_content = self::wp_filesystem_get_contents( $file );
$document = new \SearchWP\Dependencies\RtfHtmlPhp\Document( $rtf_content );
// Reduce the document to Text Objects.
$reduce_rtf_to_text = function( $group, $content ) use ( &$reduce_rtf_to_text ) {
if ( ! empty( $group->children ) && is_array( $group->children ) ) {
foreach ( $group->children as $entry ) {
if ( $entry instanceof \SearchWP\Dependencies\RtfHtmlPhp\Text ) {
$content .= htmlspecialchars( $entry->text, ENT_NOQUOTES, 'UTF-8' );
}
if ( $entry instanceof \SearchWP\Dependencies\RtfHtmlPhp\Group && ! empty( $entry->children ) ) {
$content .= $reduce_rtf_to_text( $entry->children, $content );
}
}
}
return $content;
};
return $reduce_rtf_to_text( $document->root, '' );
}
/**
* Extract text content from the file using WP_Filesystem.
*
* @since 4.0
* @param string $file The full path to the file.
* @return string|boolean
*/
private static function wp_filesystem_get_contents( string $file ) {
global $wp_filesystem;
WP_Filesystem();
$contents = '';
if ( method_exists( $wp_filesystem, 'exists' ) && method_exists( $wp_filesystem, 'get_contents' ) ) {
$contents = $wp_filesystem->exists( $file ) ? $wp_filesystem->get_contents( $file ) : '';
}
return $contents;
}
/**
* Extract text from this .docx.
*
* @since 4.0
* @param string $file The full path to the file.
* @param mixed $data Additional data for this call.
* @param string $mime_type File mime type.
* @return bool|mixed|string
*/
private static function extract_msoffice_docx_text( string $file, $data, string $mime_type ) {
if ( false !== strpos( $mime_type, 'opendocument' ) ) {
$content = self::get_file_content_from_package( $file, 'content.xml' );
} else {
$content = self::get_file_content_from_package( $file, 'word/document.xml' ); // Stored in the .docx zip.
}
return $content;
}
/**
* Retrieve embedded file content from MSOffice package.
*
* @since 4.0
* @param string $file The full path to the file.
* @param string $mime_type File mime type.
* @return string
*/
private static function get_file_content_from_package( string $file, string $stored_xml_filename ) {
if ( ! class_exists( 'ZipArchive' ) ) {
do_action( 'searchwp\debug\log', 'Document parsing failed: ZipArchive not available', 'parser' );
return '';
}
$output_text = '';
$zip_handle = new \ZipArchive;
if ( true === $zip_handle->open( $file ) ) {
if ( false !== ( $xml_index = $zip_handle->locateName( $stored_xml_filename ) ) ) {
$xml_datas = $zip_handle->getFromIndex( $xml_index );
$output_text = self::get_xml_content( $xml_datas );
}
$zip_handle->close();
} else {
do_action( 'searchwp\debug\log', 'Document parsing failed: unable to open file', 'parser' );
}
return $output_text;
}
/**
* Use DOMDocument to get partially cleaned XML content
*
* @since 4.0
* @param string $data
* @return mixed|string
*/
private static function get_xml_content( $data = '' ) {
if ( ! class_exists( 'DOMDocument' ) ) {
do_action( 'searchwp\debug\log', 'Document parsing ERROR: DOMDocument not found', 'parser' );
return '';
}
$xml_handle = new \DOMDocument();
$xml_handle->loadXML( $data, LIBXML_NOENT | LIBXML_XINCLUDE | LIBXML_NOERROR | LIBXML_NOWARNING );
return wp_strip_all_tags( str_replace( '<', ' <', $xml_handle->saveXML() ) );
}
/**
* Extract text from Excel document
*
* @since 4.0
* @param string $file The full path to the file.
* @param mixed $data Additional data for this call.
* @param string $mime_type File mime type.
* @return string
*/
private static function extract_msoffice_excel_text( string $file, $data, string $mime_type ) {
if ( false !== strpos( $mime_type, 'opendocument' ) ) {
$content = self::get_file_content_from_package( $file, 'content.xml' );
} else {
$content = self::get_file_content_from_package( $file, 'xl/sharedStrings.xml' ); // Stored in the .xlsx zip.
}
return $content;
}
/**
* Extract text from PowerPoint file.
*
* @since 4.0
* @param string $file The full path to the file.
* @param mixed $data Additional data for this call.
* @param string $mime_type File mime type.
* @return string
*/
private static function extract_msoffice_powerpoint_text( string $file, $data, string $mime_type ) {
if ( ! class_exists( 'ZipArchive' ) ) {
do_action( 'searchwp\debug\log', 'Document parsing failed: ZipArchive not available', 'parser' );
return '';
}
$zip_handle = new \ZipArchive;
$output_text = '';
if ( false !== strpos( $mime_type, 'opendocument' ) ) {
$output_text = self::get_file_content_from_package( $file, 'content.xml' );
} else {
if ( true === $zip_handle->open( $file ) ) {
$slide_number = 1; // Loop through slide files.
while ( false !== (
$xml_index = $zip_handle->locateName( 'ppt/slides/slide' . absint( $slide_number ) . '.xml' ) ) ) {
$xml_datas = $zip_handle->getFromIndex( $xml_index );
$output_text .= ' ' . self::get_xml_content( $xml_datas );
$slide_number++;
}
$zip_handle->close();
}
}
return $output_text;
}
/**
* Collection of supported MIME types.
*
* @since 4.0
* @var string[][]
*/
protected static $mimes = [
'pdf' => [
'application/pdf',
],
'text' => [
'text/plain',
'text/csv',
'text/tab-separated-values',
'text/calendar',
'text/css',
'text/html',
],
'richtext' => [
'text/richtext',
'application/rtf',
],
'msoffice_word' => [
'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
'application/vnd.ms-word.document.macroEnabled.12',
'application/vnd.openxmlformats-officedocument.wordprocessingml.template',
'application/vnd.ms-word.template.macroEnabled.12',
'application/vnd.oasis.opendocument.text',
],
'msoffice_excel' => [
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
'application/vnd.ms-excel.sheet.macroEnabled.12',
'application/vnd.ms-excel.sheet.binary.macroEnabled.12',
'application/vnd.openxmlformats-officedocument.spreadsheetml.template',
'application/vnd.ms-excel.template.macroEnabled.12',
'application/vnd.ms-excel.addin.macroEnabled.12',
'application/vnd.oasis.opendocument.spreadsheet',
'application/vnd.oasis.opendocument.chart',
'application/vnd.oasis.opendocument.database',
'application/vnd.oasis.opendocument.formula',
],
'msoffice_powerpoint' => [
'application/vnd.ms-powerpoint',
'application/vnd.openxmlformats-officedocument.presentationml.presentation',
'application/vnd.ms-powerpoint.presentation.macroEnabled.12',
'application/vnd.openxmlformats-officedocument.presentationml.slideshow',
'application/vnd.ms-powerpoint.slideshow.macroEnabled.12',
'application/vnd.openxmlformats-officedocument.presentationml.template',
'application/vnd.ms-powerpoint.template.macroEnabled.12',
'application/vnd.ms-powerpoint.addin.macroEnabled.12',
'application/vnd.openxmlformats-officedocument.presentationml.slide',
'application/vnd.ms-powerpoint.slide.macroEnabled.12',
'application/vnd.oasis.opendocument.presentation',
'application/vnd.oasis.opendocument.graphics',
],
];
}