forked from phpip/DocumentsParser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DocumentsParser.php
712 lines (623 loc) · 18.9 KB
/
DocumentsParser.php
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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
<?php
/**
* DOCx files parser
*
* Supported elements:
* - paragraphs (w:p),
* - images (pic:pic),
* - links (w:hyperlink),
* - tables (w:t),
* - boorkmarks (w:bookmarkStart),
* - lists.
*
* TODO:
* - add links filter
* - use absolutely equal images as one (remove extra images)
* - add i18n
*
* Known issues:
* - memory consuming
* - for big files, executing more than 30 sec (default timout time)
*
* @author Igor Timoshenkov
* @started 20.03.2013
* @version 0.2
* @email igor.timoshenkov@gmail.com
*
* OpenXML Standards - http://www.schemacentral.com/sc/ooxml/ss.html
*/
class DocumentsParser {
public $filesDestinationFolder = "";
public $exclude = array();
public $errors = array();
public $styles = array();
protected $previousListState = "";
// default parser settings
private $defaultSettings = array(
'filesDestinationFolder' => 'images',
'exclude' => array(),
'force' => true
);
// default element styles
private $defaultStyles = array(
'table' => array(
'border' => '1',
),
'ul' => array(
'style' => 'margin-top: 5px; list-style: inside;',
),
'li' => array(),
'p' => array(
'style' => 'text-align: justify;',
),
'img' => array(
'style' => 'clear: both; margin: 5px; float: left;',
),
'h2' => array(
'style' => 'margin-top: 20px; margin-bottom: 5px; clear: both;',
),
'h3' => array(
'style' => 'margin-top: 20px; margin-bottom: 5px; clear: both;',
),
'h4' => array(
'style' => 'margin-top: 20px; margin-bottom: 5px; clear: both; font-size: 15px;',
)
);
/**
* Init parser
*
* @param string $settings - parser settings
* - 'imagesFolder' - path to the folder for images save
* - 'exclude' - array of strings to exclude from the content
* - 'force' - change memory limit and etc.
*
* @param array $styles - default html elements styles (see defaultStyles)
* - 'element' => array(
* 'attribute' => 'values'
* )
*/
public function __construct($settings = array(), $styles = array())
{
// merge settings
$settings = array_merge($this->defaultSettings, $settings);
$styles = array_merge($this->defaultStyles, $styles);
if ($settings['force']) {
error_reporting(E_ALL);
set_time_limit(0);
ini_set('memory_limit', '10000M');
ini_set('max_execution_time', '0');
}
// check destination folder to be writable
if (isset($settings['filesDestinationFolder']) && is_dir($settings['filesDestinationFolder']) && is_writable($settings['filesDestinationFolder'])) {
$this->filesDestinationFolder = rtrim($settings['filesDestinationFolder'], '/')."/";
}
else {
$this->errors[] = "The images folder should be writable. Please, check the permissions.";
return;
}
$this->exclude = $settings['exclude'];
$this->styles = $styles;
}
/**
* Parse single file
*
* @param string $filePath
*
* @return string
*/
public function parseFile($filePath)
{
if (is_file($filePath)) {
$zip = new ZipArchive;
$dataFile = 'word/document.xml';
$resourseFile = 'word/_rels/document.xml.rels';
if (true === $zip->open($filePath)) {
// search for resourses file
$this->resource_index = array();
if (($resourses = $zip->locateName($resourseFile)) !== false)
{
$resouse = $zip->getFromIndex($resourses);
$resXML = simplexml_load_string($resouse, 'SimpleXMLElement');
$this->setNameSpaces($resXML);
foreach ($resXML->children() as $rel) {
$attributes = $rel->attributes();
$type = (string) $attributes['Type'][0];
$tmp = explode("/", $type);
$type = array_pop($tmp);
if (in_array($type, array('image', 'hyperlink'))) {
$target = (string) $attributes['Target'][0];
$this->resource_index[(string) $attributes['Id'][0]] = array(
'type' => $type,
'target' => $target,
'external' => isset($attributes['TargetMode']) && $attributes['TargetMode'] == 'External'
);
// copy files if image
if ('image' == $type) {
$fp = $zip->getStream('word/'.$target);
ob_start();
fpassthru($fp);
$image = ob_get_contents();
ob_end_clean();
file_put_contents($this->filesDestinationFolder.str_replace('media/', '', $target), $image);
}
}
}
}
// If done, search for the data file in the archive
if (($index = $zip->locateName($dataFile)) !== false)
{
// If found, read it to the string
$data = $zip->getFromIndex($index);
// Load XML from a string
$docXML = simplexml_load_string($data, 'SimpleXMLElement');
$this->setNameSpaces($docXML);
$html = "";
foreach ($docXML->xpath('w:body') as $parent)
{
$elementsCount = $this->countChildren($parent);
foreach ($parent->xpath('*') as $sub)
{
switch ($sub->getName()) {
case 'hyperlink':
$html .= $this->parseHyperlink($sub);
break;
default:
case 'r':
case 'drawing':
$html .= $this->parseImage($sub);
$html .= $this->parseTextElement($sub);
break;
case 'p':
$html .= $this->parsePragraph($sub);
break;
case 'tbl':
if ($elementsCount > 50) {
$html .= $this->parseTable($sub);
}
break;
}
$this->filterContent($html);
}
if ($elementsCount < 50) {
foreach ($docXML->xpath('w:body/w:tbl/w:tr') as $tblRow) {
foreach ($tblRow->xpath('w:tc') as $tblCell) {
foreach ($tblCell->xpath('*') as $cellContent)
{
switch ($cellContent->getName()) {
case 'hyperlink':
$html .= $this->parseHyperlink($cellContent);
break;
default:
case 'r':
case 'drawing':
$html .= $this->parseImage($cellContent);
$html .= $this->parseTextElement($cellContent);
break;
case 'p':
$html .= $this->parsePragraph($cellContent);
break;
case 'tbl':
$html .= $this->parseTable($cellContent);
break;
}
$this->filterContent($html);
}
}
}
}
}
}
// Close archive file
$zip->close();
// empty resources
$this->resource_index = array();
return $html;
}
else {
$this->errors[] = "Couldn't open file {$filePath}.";
}
}
return "";
}
/**
* Add namespaces to SimpleXmlElement
*
* @param SimpleXmlElement $xml
*
* @return false
*/
protected function setNameSpaces($xml)
{
$xml->registerXPathNamespace("a", "http://schemas.openxmlformats.org/drawingml/2006/main");
$xml->registerXPathNamespace("pic", "http://schemas.openxmlformats.org/drawingml/2006/picture");
$xml->registerXPathNamespace("r", "http://schemas.openxmlformats.org/officeDocument/2006/relationships");
$xml->registerXPathNamespace("w", "http://schemas.microsoft.com/office/word/2003/wordml");
$xml->registerXPathNamespace("wp", "http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing");
}
/**
* Parse table element
*
* @param SimpleXMLElement $tableXML
*
* @return string
*/
protected function parseTable($tableXML)
{
$content = "";
// close the list if needed
if ($this->previousListState) {
$content .= "</ul>";
$this->previousListState = false;
}
// start a table
$content .= "<table ".$this->getAttributes('table').">";
foreach ($tableXML->xpath('w:tr') as $tableRow) {
$row = "<tr ".$this->getAttributes('td').">";
$cellsPerRow = 0;
foreach ($tableRow->xpath('w:tc') as $tableCell) {
$cellsPerRow++;
$merge = 0;
$colspanFound = $tableCell->xpath('w:tcPr/w:gridSpan/@w:val');
if (isset($colspanFound)) {
foreach ($tableCell->xpath('w:tcPr/w:gridSpan/@w:val') as $colspan) {
$merge = (string) $colspan[0];
}
}
if ($merge > 0) {
$row .= "<td colspan='{$merge}' ".$this->getAttributes('td').">";
$tdTag = "<td colspan='{$merge}' ".$this->getAttributes('td').">";
}
else {
$row .= "<td ".$this->getAttributes('td').">";
$tdTag = "<td ".$this->getAttributes('td').">";
}
foreach ($tableCell->xpath('*') as $element)
{
switch ($element->getName()) {
case 'tbl':
$row .= $this->parseTable($element);
break;
case 'r':
$row .= $this->parseImage($element, true);
$row .= $this->parseTextElement($element);
break;
default:
$row .= $this->parsePragraph($element, true);
break;
}
}
// remove first <br> in a cell
$row = preg_replace("@".$tdTag."<br/>@", $tdTag, $row);
$row .= "</td>";
}
$row .= "</tr>";
$stripped = strip_tags($row);
$hasImages = strpos($row, '<img');
// center single image in cell
if (empty($stripped) && $cellsPerRow == 1 && $hasImages) {
$row = preg_replace("@<br/>@", "", $row);
$content .= str_replace("<p", "<p style='text-align: center;'", $row);
}
// remove empty rows
elseif (!empty($stripped) || $hasImages) {
$content .= $row;
}
}
$content .= "</table><br/>";
return $content;
}
/**
* Get attributes string for the specified element
*
* @param string $elementName
*
* @return string
*/
private function getAttributes($elementName)
{
$attrString = "";
foreach ($this->styles[$elementName] as $key => $value) {
$attrString .= $key ."=\"{$value}\"";
}
return $attrString;
}
/**
* If an html part starts with the br tag - remove it
*
* @param string $html
*
* @return void
*/
protected function removeFirstBr($html)
{
$firstBrPos = strpos($html, "<br/>");
if ($firstBrPos == 0 && $firstBrPos !== false) {
$html = substr($html, 4, strlen($html));
}
return $html;
}
/**
* Parse link element
*
* @param SimpleXMLElement $xmlElement
*
* @return string (html)
*/
protected function parseHyperlink($xmlElement)
{
$text = $xmlElement->xpath('w:r/w:t');
if (isset($text) && gettype($text) != 'NULL') {
$text_string = "";
foreach ($text as $char) {
$text_string .= $char;
}
$text_string = trim($text_string);
// remove wiki brackets
$text_string = str_replace("[", "", $text_string);
$text_string = str_replace("]", "", $text_string);
$href = $this->resource_index[(string) $xmlElement->xpath('@r:id')[0]['id']]['target'];
return "<a href='".$href."' target='_blank' ".$this->getAttributes('a').">".$text_string."</a>";
}
return "";
}
/**
* Parse paragraph element
*
* @param SimpleXMLElement $xmlElement
* @param bool $inline
*
* @return void
*/
protected function parsePragraph($xmlElement, $inline = false)
{
$p_html = "";
$children = $xmlElement->xpath('*');
// check if it's a list item
$isListElement = (bool) strpos($xmlElement->saveXML(), "<w:numPr>");
if (!empty($children)) {
foreach ($children as $sub_element)
{
switch ($sub_element->getName()) {
case 'hyperlink':
$p_html .= $this->parseHyperlink($sub_element);
break;
case 'bookmarkStart':
$p_html .= $this->parseBookmark($sub_element);
break;
case 'tbl':
$p_html .= $this->parseTable($element);
break;
default:
case 'r':
$p_html .= $this->parseImage($sub_element, ($inline || $isListElement));
$p_html .= $this->parseTextElement($sub_element);
break;
case 'drawing':
$p_html .= $this->parseImage($sub_element);
break;
case 't':
$p_html .= $this->parseTextElement($sub_element);
break;
}
}
}
$p_html = $this->normalize($p_html);
if (!in_array(trim(strip_tags($p_html)), $this->exclude)) {
// check for size
$xmlString = $xmlElement->saveXML();
$h2BySize = (bool) strpos($xmlString, "w:val=\"Heading2\"");
$h3BySize = (bool) strpos($xmlString, "w:val=\"Heading3\"");
$h4BySize = (bool) strpos($xmlString, "w:val=\"Heading4\"");
$bold = (bool) strpos($xmlString, "w:val=\"Strong\"");
$italic = (bool) strpos($xmlString, "w:val=\"Emphasis\"");
$underlined = (bool) strpos($xmlString, "<w:u ");
$prefix = "";
// handle list if needed
if ($isListElement && !empty($p_html)) {
// previous paragraph is also a list element?
if ($this->previousListState) {
return "<li ".$this->getAttributes('li').">".$p_html."</li>";
}
else {
$this->previousListState = true;
return "<ul ".$this->getAttributes('ul')."><li ".$this->getAttributes('li').">".$p_html."</li>";
}
}
else {
// was previous a list element?
if ($this->previousListState) {
$prefix = "</ul>";
}
}
$this->previousListState = $isListElement;
if (!empty($p_html)) {
if ($h2BySize || $h3BySize || $h4BySize || $bold || $italic || $underlined)
{
$tag = null;
$style = "";
if ($italic) {
$tag = "i";
}
if ($italic) {
$tag = "u";
}
if ($h2BySize) {
$tag = "h2";
}
if ($h3BySize) {
$tag = "h3";
}
if ($h4BySize) {
$tag = "h4";
}
if (!is_null($tag)) {
return $prefix."<{$tag} ".$this->getAttributes($tag).">".$p_html."</{$tag}>";
}
else {
return $prefix."<p ".$this->getAttributes('p').">".$p_html."</p>";
}
}
else {
// try to get text size
if ($xmlElement->xpath("w:pPr/w:rPr/w:sz/@w:val")[0]) {
$sizeStr = $xmlElement->xpath("w:pPr/w:rPr/w:sz/@w:val")[0]->saveXML();
$sizeStrParts = explode("=\"", $sizeStr);
$size = (int) trim($sizeStrParts[1], "\"") / 2;
if ($size > 12 && $size < 18) {
return $prefix."<h4 ".$this->getAttributes('h4').">".$p_html."</h4>";
}
}
return $prefix."<p ".$this->getAttributes('p').">".$p_html."</p>";
}
}
else {
return $prefix."<br/>";
}
}
return $prefix."";
}
/**
* Remove symbols like Â, etc.
*
* @param string $string
*
* @return string
*/
private function normalize($string) {
return mb_convert_encoding($string, "HTML-ENTITIES", "UTF-8");
}
/**
* Simply get text out of an element
*
* @param SimpleXmlElement $xmlElement
*
* @return string
*/
protected function parseTextElement($xmlElement)
{
$text = "";
$italic = (strpos($xmlElement->saveXML(), "<w:i/>") !== false);
$bold = (strpos($xmlElement->saveXML(), "<w:b/>") !== false);
$underline = (strpos($xmlElement->saveXML(), "<w:u/>") !== false);
foreach ($xmlElement->xpath('w:t') as $textElement) {
$text .= strip_tags($textElement->saveXML());
}
// exclude if needed
if (in_array($text, $this->exclude)) {
return "";
}
// apply styles
$text = $this->normalize($text);
$text = $italic ? "<i ".$this->getAttributes('i').">".$text."</i>" : $text;
$text = $bold ? "<b ".$this->getAttributes('b').">".$text."</b>" : $text;
$text = $underline ? "<u ".$this->getAttributes('u').">".$text."</u>" : $text;
return $text;
}
/**
* Parse images
*
* @param SimpleXMLElement $xmlElement
* @param bool $inline
*
* @return string (html)
*/
protected function parseImage($xmlElement, $inline = false)
{
preg_match("@<a:blip .*>@", $xmlElement->saveXML(), $matches);
if (!empty($matches)) {
$parts = explode("/>", $matches[0]);
$blipXML = simplexml_load_string($parts[0]."/>");
if (gettype($blipXML) == 'object') {
$this->setNameSpaces($blipXML);
$id = (string) $blipXML->attributes()['r:embed'];
if ($this->resource_index[$id]['external']) {
$src = $this->resource_index[$id]['target'];
}
else {
$src = $this->resource_index[$id]['target'];
$src = $this->filesDestinationFolder.str_replace('media/', '', $src);
}
return "<img src='{$src}' ".$this->getAttributes('img')." />";
}
}
// Epic images in shapes part goes here:
foreach ($xmlElement->xpath('w:pict') as $picture) {
// iterate through groups
foreach ($picture->xpath('v:group') as $group) {
// ... and sub groups
foreach ($group->xpath('v:group') as $subGroup) {
foreach ($subGroup->xpath('v:shape') as $shape) {
foreach ($subGroup->xpath('*/v:imagedata') as $imagedata) {
$imageId = (string) $imagedata->xpath('@r:id')[0];
$src = $this->resource_index[$imageId]['target'];
$src = $this->filesDestinationFolder.str_replace('media/', '', $src);
return "<img src='{$src}' ".$this->getAttributes('img')." />";
}
}
}
// but don't also forget about images
foreach ($group->xpath('v:shape') as $shape) {
foreach ($group->xpath('*/v:imagedata') as $imagedata) {
$imageId = (string) $imagedata->xpath('@r:id')[0];
$src = $this->resource_index[$imageId]['target'];
$src = $this->filesDestinationFolder.str_replace('media/', '', $src);
return "<img src='{$src}' ".$this->getAttributes('img')." />";
}
}
}
// but don't also forget about images
foreach ($picture->xpath('v:shape') as $shape) {
foreach ($picture->xpath('*/v:imagedata') as $imagedata) {
$imageId = (string) $imagedata->xpath('@r:id')[0];
$src = $this->resource_index[$imageId]['target'];
$src = $this->filesDestinationFolder.str_replace('media/', '', $src);
return "<img src='{$src}' ".$this->getAttributes('img')." />";
}
}
}
return "";
}
/**
* Count Simple Xml Element children
*
* @param SimpleXmlElement $parent
*
* @return int
*/
private function countChildren($parent)
{
$elementsCount = 0;
foreach ($parent->xpath('*') as $sub) {
$elementsCount++;
}
return $elementsCount;
}
/**
* Handle multiple linebreaks, remove spear ones
*
* @param string $html
*
* @return void
*/
protected function filterContent(&$html)
{
// many <br>'s to one
$html = preg_replace("@(<br/>)+@", "<br/>", $html);
// remove the first br
if (strpos($html, "<br/>") !== false && strpos($html, "<br/>") == 0) {
$html = substr($html, 5);
}
// remove extra <br> before tables
$html = preg_replace("@(<br/><table)+@", "<table", $html);
// remove extra <br> before and after headers - margins are set there
$html = preg_replace("@(<br/><h1)+@", "<h1", $html);
$html = preg_replace("@(<br/><h2)+@", "<h2", $html);
$html = preg_replace("@(<br/><h3)+@", "<h3", $html);
$html = preg_replace("@(<br/><h4)+@", "<h4", $html);
$html = preg_replace("@(</h1><br/>)+@", "</h1>", $html);
$html = preg_replace("@(</h2><br/>)+@", "</h2>", $html);
$html = preg_replace("@(</h3><br/>)+@", "</h3>", $html);
$html = preg_replace("@(</h4><br/>)+@", "</h4>", $html);
}
}
?>