Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
75.73% covered (warning)
75.73%
78 / 103
66.67% covered (warning)
66.67%
2 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
Content
75.73% covered (warning)
75.73%
78 / 103
66.67% covered (warning)
66.67%
2 / 3
56.58
0.00% covered (danger)
0.00%
0 / 1
 read
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
1
 processNodes
72.83% covered (warning)
72.83%
67 / 92
0.00% covered (danger)
0.00%
0 / 1
57.20
 getSection
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2/**
3 * This file is part of PHPWord - A pure PHP library for reading and writing
4 * word processing documents.
5 *
6 * PHPWord is free software distributed under the terms of the GNU Lesser
7 * General Public License version 3 as published by the Free Software Foundation.
8 *
9 * For the full copyright and license information, please read the LICENSE
10 * file that was distributed with this source code. For the full list of
11 * contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
12 *
13 * @see         https://github.com/PHPOffice/PHPWord
14 *
15 * @license     http://www.gnu.org/licenses/lgpl.txt LGPL version 3
16 */
17
18namespace PhpOffice\PhpWord\Reader\ODText;
19
20use DateTime;
21use DOMElement;
22use DOMNodeList;
23use PhpOffice\Math\Reader\MathML;
24use PhpOffice\PhpWord\Element\Section;
25use PhpOffice\PhpWord\Element\TrackChange;
26use PhpOffice\PhpWord\PhpWord;
27use PhpOffice\PhpWord\Shared\XMLReader;
28
29/**
30 * Content reader.
31 *
32 * @since 0.10.0
33 */
34class Content extends AbstractPart
35{
36    /** @var ?Section */
37    private $section;
38
39    /**
40     * Read content.xml.
41     */
42    public function read(PhpWord $phpWord): void
43    {
44        $xmlReader = new XMLReader();
45        $xmlReader->getDomFromZip($this->docFile, $this->xmlFile);
46
47        $trackedChanges = [];
48
49        $nodes = $xmlReader->getElements('office:body/office:text/*');
50        $this->section = null;
51        $this->processNodes($nodes, $xmlReader, $phpWord);
52        $this->section = null;
53    }
54
55    /** @param DOMNodeList<DOMElement> $nodes */
56    public function processNodes(DOMNodeList $nodes, XMLReader $xmlReader, PhpWord $phpWord): void
57    {
58        if ($nodes->length > 0) {
59            foreach ($nodes as $node) {
60                // $styleName = $xmlReader->getAttribute('text:style-name', $node);
61                switch ($node->nodeName) {
62                    case 'text:h': // Heading
63                        $depth = $xmlReader->getAttribute('text:outline-level', $node);
64                        $this->getSection($phpWord)->addTitle($node->nodeValue, $depth);
65
66                        break;
67                    case 'text:p': // Paragraph
68                        $styleName = $xmlReader->getAttribute('text:style-name', $node);
69                        if (substr($styleName, 0, 2) === 'SB') {
70                            break;
71                        }
72                        $element = $xmlReader->getElement('draw:frame/draw:object', $node);
73                        if ($element) {
74                            $mathFile = str_replace('./', '', $element->getAttribute('xlink:href')) . '/content.xml';
75
76                            $xmlReaderObject = new XMLReader();
77                            $mathElement = $xmlReaderObject->getDomFromZip($this->docFile, $mathFile);
78                            if ($mathElement) {
79                                $mathXML = $mathElement->saveXML($mathElement);
80
81                                if (is_string($mathXML)) {
82                                    $reader = new MathML();
83                                    $math = $reader->read($mathXML);
84
85                                    $this->getSection($phpWord)->addFormula($math);
86                                }
87                            }
88                        } else {
89                            $children = $node->childNodes;
90                            $spans = false;
91                            /** @var DOMElement $child */
92                            foreach ($children as $child) {
93                                switch ($child->nodeName) {
94                                    case 'text:change-start':
95                                        $changeId = $child->getAttribute('text:change-id');
96                                        if (isset($trackedChanges[$changeId])) {
97                                            $changed = $trackedChanges[$changeId];
98                                        }
99
100                                        break;
101                                    case 'text:change-end':
102                                        unset($changed);
103
104                                        break;
105                                    case 'text:change':
106                                        $changeId = $child->getAttribute('text:change-id');
107                                        if (isset($trackedChanges[$changeId])) {
108                                            $changed = $trackedChanges[$changeId];
109                                        }
110
111                                        break;
112                                    case 'text:span':
113                                        $spans = true;
114
115                                        break;
116                                }
117                            }
118
119                            if ($spans) {
120                                $element = $this->getSection($phpWord)->addTextRun();
121                                foreach ($children as $child) {
122                                    switch ($child->nodeName) {
123                                        case 'text:span':
124                                            /** @var DOMElement $child2 */
125                                            foreach ($child->childNodes as $child2) {
126                                                switch ($child2->nodeName) {
127                                                    case '#text':
128                                                        $element->addText($child2->nodeValue);
129
130                                                        break;
131                                                    case 'text:tab':
132                                                        $element->addText("\t");
133
134                                                        break;
135                                                    case 'text:s':
136                                                        $spaces = (int) $child2->getAttribute('text:c') ?: 1;
137                                                        $element->addText(str_repeat(' ', $spaces));
138
139                                                        break;
140                                                }
141                                            }
142
143                                            break;
144                                    }
145                                }
146                            } else {
147                                $element = $this->getSection($phpWord)->addText($node->nodeValue);
148                            }
149                            if (isset($changed) && is_array($changed)) {
150                                $element->setTrackChange($changed['changed']);
151                                if (isset($changed['textNodes'])) {
152                                    foreach ($changed['textNodes'] as $changedNode) {
153                                        $element = $this->getSection($phpWord)->addText($changedNode->nodeValue);
154                                        $element->setTrackChange($changed['changed']);
155                                    }
156                                }
157                            }
158                        }
159
160                        break;
161                    case 'text:list': // List
162                        $listItems = $xmlReader->getElements('text:list-item/text:p', $node);
163                        foreach ($listItems as $listItem) {
164                            // $listStyleName = $xmlReader->getAttribute('text:style-name', $listItem);
165                            $this->getSection($phpWord)->addListItem($listItem->nodeValue, 0);
166                        }
167
168                        break;
169                    case 'text:tracked-changes':
170                        $changedRegions = $xmlReader->getElements('text:changed-region', $node);
171                        foreach ($changedRegions as $changedRegion) {
172                            $type = ($changedRegion->firstChild->nodeName == 'text:insertion') ? TrackChange::INSERTED : TrackChange::DELETED;
173                            $creatorNode = $xmlReader->getElements('office:change-info/dc:creator', $changedRegion->firstChild);
174                            $author = $creatorNode[0]->nodeValue;
175                            $dateNode = $xmlReader->getElements('office:change-info/dc:date', $changedRegion->firstChild);
176                            $date = $dateNode[0]->nodeValue;
177                            $date = preg_replace('/\.\d+$/', '', $date);
178                            $date = DateTime::createFromFormat('Y-m-d\TH:i:s', $date);
179                            $changed = new TrackChange($type, $author, $date);
180                            $textNodes = $xmlReader->getElements('text:deletion/text:p', $changedRegion);
181                            $trackedChanges[$changedRegion->getAttribute('text:id')] = ['changed' => $changed, 'textNodes' => $textNodes];
182                        }
183
184                        break;
185                    case 'text:section': // Section
186                        // $sectionStyleName = $xmlReader->getAttribute('text:style-name', $listItem);
187                        $this->section = $phpWord->addSection();
188                        $children = $node->childNodes;
189                        $this->processNodes($children, $xmlReader, $phpWord);
190
191                        break;
192                }
193            }
194        }
195    }
196
197    private function getSection(PhpWord $phpWord): Section
198    {
199        $section = $this->section;
200        if ($section === null) {
201            $section = $this->section = $phpWord->addSection();
202        }
203
204        return $section;
205    }
206}