Skip to content

Add support for comments to reader #2161

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/intro.rst
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ Readers
+---------------------------+----------------------+--------+-------+-------+-------+-------+
| | Endnote | ✓ | | | | |
+---------------------------+----------------------+--------+-------+-------+-------+-------+
| | Comments | | | | | |
| | Comments | | | | | |
+---------------------------+----------------------+--------+-------+-------+-------+-------+
| **Graphs** | 2D basic graphs | | | | | |
+---------------------------+----------------------+--------+-------+-------+-------+-------+
Expand Down
46 changes: 46 additions & 0 deletions src/PhpWord/PhpWord.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

namespace PhpOffice\PhpWord;

use InvalidArgumentException;
use PhpOffice\PhpWord\Element\AbstractElement;
use PhpOffice\PhpWord\Element\Section;
use PhpOffice\PhpWord\Exception\Exception;

Expand Down Expand Up @@ -87,6 +89,14 @@ class PhpWord
*/
private $metadata = array();

/**
* Comment reference cache
*
* @var array
* @since 0.18.3
*/
private $commentReferenceCache = array();

/**
* Create new instance
*
Expand Down Expand Up @@ -422,4 +432,40 @@ public function setDocumentProperties($documentProperties)

return $this;
}

/**
* Cache commentReference (as well as commentRangeStart and commentRangeEnd) for later use
*
* @param 'start'|'end' $type
* @param string $id,
* @param $element
*
* @return self
*/
public function cacheCommentReference(string $type, string $id, AbstractElement $element)
{
//dump('cacheCommentReference', func_get_args(), array_key_exists($id, $this->commentReferenceCache));
if (!in_array($type, [ 'start', 'end' ])) {
throw new InvalidArgumentException('Type must be "start" or "end"');
}

if (!array_key_exists($id, $this->commentReferenceCache)) {
$this->commentReferenceCache[$id] = (object)[
"start" => null,
"end" => null
];
}
$this->commentReferenceCache[$id]->{$type} = $element;

return $this;
}

public function getCommentReference(string $id)
{
if (!array_key_exists($id, $this->commentReferenceCache)) {
//dd($this->commentReferenceCache);
throw new InvalidArgumentException('Comment with id '.$id.' isn\'t referenced in document');
}
return $this->commentReferenceCache[$id];
}
}
1 change: 1 addition & 0 deletions src/PhpWord/Reader/Word2007.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public function load($docFile)
'endnotes' => 'Endnotes',
'footnotes' => 'Footnotes',
'settings' => 'Settings',
'comments' => 'Comments'
)),
);

Expand Down
18 changes: 16 additions & 2 deletions src/PhpWord/Reader/Word2007/AbstractPart.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@ protected function readParagraph(XMLReader $xmlReader, \DOMElement $domNode, $pa
// Paragraph style
$paragraphStyle = null;
$headingDepth = null;
if ($xmlReader->elementExists('w:commentReference', $domNode) || $xmlReader->elementExists('w:commentRangeStart', $domNode) || $xmlReader->elementExists('w:commentRangeEnd', $domNode)) {
$nodes = $xmlReader->getElements('w:commentReference|w:commentRangeStart|w:commentRangeEnd', $domNode);
$node = current(iterator_to_array($nodes));
$id = $node->attributes->getNamedItem('id')->value;
}
if ($xmlReader->elementExists('w:pPr', $domNode)) {
$paragraphStyle = $this->readParagraphStyle($xmlReader, $domNode);
$headingDepth = $this->getHeadingDepth($paragraphStyle);
Expand Down Expand Up @@ -163,7 +168,7 @@ protected function readParagraph(XMLReader $xmlReader, \DOMElement $domNode, $pa
$parent->addTitle($textContent, $headingDepth);
} else {
// Text and TextRun
$textRunContainers = $xmlReader->countElements('w:r|w:ins|w:del|w:hyperlink|w:smartTag', $domNode);
$textRunContainers = $xmlReader->countElements('w:r|w:ins|w:del|w:hyperlink|w:smartTag|w:commentReference|w:commentRangeStart|w:commentRangeEnd', $domNode);
if (0 === $textRunContainers) {
$parent->addTextBreak(null, $paragraphStyle);
} else {
Expand Down Expand Up @@ -212,7 +217,7 @@ private function getHeadingDepth(array $paragraphStyle = null)
*/
protected function readRun(XMLReader $xmlReader, \DOMElement $domNode, $parent, $docPart, $paragraphStyle = null)
{
if (in_array($domNode->nodeName, array('w:ins', 'w:del', 'w:smartTag', 'w:hyperlink'))) {
if (in_array($domNode->nodeName, array('w:ins', 'w:del', 'w:smartTag', 'w:hyperlink', 'w:commentReference'))) {
$nodes = $xmlReader->getElements('*', $domNode);
foreach ($nodes as $node) {
$this->readRun($xmlReader, $node, $parent, $docPart, $paragraphStyle);
Expand All @@ -224,6 +229,15 @@ protected function readRun(XMLReader $xmlReader, \DOMElement $domNode, $parent,
$this->readRunChild($xmlReader, $node, $parent, $docPart, $paragraphStyle, $fontStyle);
}
}

if($xmlReader->elementExists('.//*["commentReference"=local-name()]', $domNode)) {
$curEl = iterator_to_array($xmlReader->getElements('.//*["commentReference"=local-name()]', $domNode))[0];
$id = $curEl->attributes->getNamedItem('id')->value;
//$path = './/*[("commentRangeStart"=local-name() or "commentRangeEnd"=local-name()) and @*[local-name()="id" and .="'.$id.'"]]';
//$range = $xmlReader->getElements($path);
$this->phpWord->cacheCommentReference('start', $id, $parent->getElement($parent->countElements() - 1));
$this->phpWord->cacheCommentReference('end', $id, $parent->getElement($parent->countElements() - 1));
}
}

/**
Expand Down
109 changes: 109 additions & 0 deletions src/PhpWord/Reader/Word2007/Comments.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?php

namespace PhpOffice\PhpWord\Reader\Word2007;

use DateTime;
use PhpOffice\PhpWord\Element\Comment;
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\Reader\Word2007\AbstractPart;
use PhpOffice\PhpWord\Shared\XMLReader;

class Comments extends AbstractPart
{
/**
* Collection name comments
*
* @var string
*/
protected $collection = 'comments';

/**
* Read settings.xml.
*
* @param \PhpOffice\PhpWord\PhpWord $phpWord
*/
public function read(PhpWord $phpWord)
{
$xmlReader = new XMLReader();
$xmlReader->getDomFromZip($this->docFile, $this->xmlFile);

//$xmlReader2 = new XMLReader();
//$xmlReader2->getDomFromZip($this->docFile, 'word/document.xml');
//dd($xmlReader2);

$comments = $phpWord->getComments();

$nodes = $xmlReader->getElements('*');
if ($nodes->length > 0) {
foreach ($nodes as $node) {
$name = str_replace('w:', '', $node->nodeName);
$value = $xmlReader->getAttribute('w:author', $node);
$author = $xmlReader->getAttribute('w:author', $node);
$date = $xmlReader->getAttribute('w:date', $node);
$initials = $xmlReader->getAttribute('w:initials', $node);
$id = $xmlReader->getAttribute('w:id', $node);
$element = new Comment($author, new DateTime($date), $initials);//$this->getElement($phpWord, $id);
//$element->set
// $range = $xmlReader2->getElements('.//*[("commentRangeStart"=local-name() or "commentRangeEnd"=local-name()) and @*[local-name()="id" and .="'.$id.'"]]');
try {
unset($range);
$range = $phpWord->getCommentReference($id);
$range->start->setCommentRangeStart($element);
$range->end->setCommentRangeEnd($element);
} catch(\Exception $e) {
//dd('range', [$element, $id, $node, $node->C14N(), $range ?? null, $e]);
}
//dd($startElement, $endElement, current(current($phpWord->getSections())->getElements()));
//dump($element, $range);
//dd($element, $node, $id, $node->C14N());
$method = 'set' . $name;
//dump([$element, $id, $name, $value, $author, $date, $initials, $method, $xmlReader->getElements('w:p/w:r/w:t', $node)]);
//dd('dsf');
$pNodes = $xmlReader->getElements('w:p/w:r', $node);
foreach ($pNodes as $pNode) {
//dump(['>', $xmlReader, $pNode, $node, $this->collection, '<']);
$this->readRun($xmlReader, $pNode, $element, $this->collection);
}

/*if (in_array($name, $this::$booleanProperties)) {
if ($value == 'false') {
$comments->$method(false);
} else {
$comments->$method(true);
}
} else*/if (method_exists($this, $method)) {
$this->$method($xmlReader, $phpWord, $node);
} elseif (method_exists($comments, $method)) {
$comments->$method($value);
} elseif (method_exists($phpWord, $method)) {
$phpWord->$method($value);
} elseif (method_exists($comments, 'addItem')) {
$comments->addItem($element);
}
}
}
}

/**
* Searches for the element with the given relationId
*
* @param PhpWord $phpWord
* @param int $relationId
* @return \PhpOffice\PhpWord\Element\AbstractContainer|null
*/
private function getElement(PhpWord $phpWord, $relationId)
{
$getMethod = "get{$this->collection}";
//$getMethod = "getTrackChange";
$collection = $phpWord->$getMethod();//->getItems();

//not found by key, looping to search by relationId
foreach ($collection as $collectionElement) {
if ($collectionElement->getRelationId() == $relationId) {
return $collectionElement;
}
}

return null;
}
}
7 changes: 6 additions & 1 deletion src/PhpWord/Reader/Word2007/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class Document extends AbstractPart
*
* @var \PhpOffice\PhpWord\PhpWord
*/
private $phpWord;
protected $phpWord;

/**
* Read document.xml.
Expand Down Expand Up @@ -180,4 +180,9 @@ private function readWSectPrNode(XMLReader $xmlReader, \DOMElement $node, Sectio
$section->setStyle($style);
$this->readHeaderFooter($style, $section);
}

protected function cacheCommentReference(string $type, string $id, $element)
{
$this->phpWord->cacheCommentReference($type, $id, $element);
}
}
16 changes: 16 additions & 0 deletions tests/PhpWord/Reader/Word2007Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

namespace PhpOffice\PhpWord\Reader;

use DateTime;
use PhpOffice\PhpWord\Element\Comment;
use PhpOffice\PhpWord\IOFactory;
use PhpOffice\PhpWord\TestHelperDOCX;

Expand Down Expand Up @@ -78,4 +80,18 @@ public function testLoadWord2011()
$doc = TestHelperDOCX::getDocument($phpWord);
$this->assertTrue($doc->elementExists('/w:document/w:body/w:p[3]/w:r/w:pict/v:shape/v:imagedata'));
}

public function testLoadComments()
{
$filename = __DIR__ . '/../_files/documents/reader-ooxml-comments.docx';
$phpWord = IOFactory::load($filename);

$this->assertInstanceOf('PhpOffice\\PhpWord\\PhpWord', $phpWord);

//$doc = TestHelperDOCX::getDocument($phpWord);
$comment = new Comment('shaedrich', new DateTime('2021-10-28T13:56:00Z'), 'SH');
$comment2 = $phpWord->getComments()[0];
$this->assertEquals($comment->getAuthor(), $comment2->getAuthor());
$this->assertEquals($comment->getInitials(), $comment2->getInitials());
}
}
Binary file not shown.