ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.PageCompare.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
24
29{
30 protected DomUtil $dom_util;
31
32 public function __construct()
33 {
34 global $DIC;
35
36 $this->dom_util = $DIC->copage()
37 ->internal()
38 ->domain()
39 ->domUtil();
40 }
41
42 public function compare(
43 \ilPageObject $main_page,
44 \ilPageObject $l_page,
45 \ilPageObject $r_page,
46 ): array {
47 $main_page->preparePageForCompare($l_page);
48 $main_page->preparePageForCompare($r_page);
49 $l_hashes = $this->getPageContentsHashes($l_page);
50 $r_hashes = $this->getPageContentsHashes($r_page);
51 // determine all deleted and changed page elements
52 foreach ($l_hashes as $pc_id => $h) {
53 if (!isset($r_hashes[$pc_id])) {
54 $l_hashes[$pc_id]["change"] = "Deleted";
55 } else {
56 if ($h["hash"] != $r_hashes[$pc_id]["hash"]) {
57 $l_hashes[$pc_id]["change"] = "Modified";
58 $r_hashes[$pc_id]["change"] = "Modified";
59
60 // if modified element is a paragraph, highlight changes
61 if ($l_hashes[$pc_id]["content"] != "" &&
62 $r_hashes[$pc_id]["content"] != "") {
63 $new_left = str_replace("\n", "<br />", $l_hashes[$pc_id]["content"]);
64 $new_right = str_replace("\n", "<br />", $r_hashes[$pc_id]["content"]);
65 $wldiff = new \WordLevelDiff(
66 array($new_left),
67 array($new_right)
68 );
69 $new_left = $wldiff->orig();
70 $new_right = $wldiff->closing();
71 $this->setParagraphContent($l_page, $l_hashes[$pc_id]["hier_id"], $new_left[0]);
72 $this->setParagraphContent($r_page, $l_hashes[$pc_id]["hier_id"], $new_right[0]);
73 }
74 }
75 }
76 }
77
78 // determine all new paragraphs
79 foreach ($r_hashes as $pc_id => $h) {
80 if (!isset($l_hashes[$pc_id])) {
81 $r_hashes[$pc_id]["change"] = "New";
82 }
83 }
84 $this->addChangeDivClasses($l_page, $l_hashes);
85 $this->addChangeDivClasses($r_page, $r_hashes);
86
87 return array("l_page" => $l_page,
88 "r_page" => $r_page,
89 "l_changes" => $l_hashes,
90 "r_changes" => $r_hashes
91 );
92 }
93
94 protected function setParagraphContent(
95 \ilPageObject $page,
96 string $a_hier_id,
97 string $content
98 ): void {
99 $node = $page->getContentDomNode($a_hier_id);
100 if (is_object($node)) {
101 $this->dom_util->setContent($node, $content);
102 }
103 }
104
105 protected function addChangeDivClasses(
106 \ilPageObject $page,
107 array $a_hashes
108 ): void {
109 $dom = $page->getDomDoc();
110 $path = "/*[1]";
111 $nodes = $this->dom_util->path($dom, $path);
112 $rnode = $nodes->item(0);
113 foreach ($a_hashes as $h) {
114 if (($h["change"] ?? "") != "") {
115 $dc_node = $dom->createElement("DivClass");
116 $dc_node->setAttribute("HierId", $h["hier_id"]);
117 $dc_node->setAttribute("Class", "ilEdit" . $h["change"]);
118 $dc_node = $rnode->appendChild($dc_node);
119 }
120 }
121 }
122
123
127 protected function getPageContentsHashes(\ilPageObject $page): array
128 {
129 $page->buildDom();
130 $page->addHierIDs();
131 $dom = $page->getDomDoc();
132
133 // get existing ids
134 $path = "//PageContent";
135 $hashes = array();
136 $nodes = $this->dom_util->path($dom, $path);
137 foreach ($nodes as $node) {
138 $hier_id = $node->getAttribute("HierId");
139 $pc_id = $node->getAttribute("PCID");
140 $dump = $this->dom_util->dump($node);
141 if (($hpos = strpos($dump, ' HierId="' . $hier_id . '"')) > 0) {
142 $dump = substr($dump, 0, $hpos) .
143 substr($dump, $hpos + strlen(' HierId="' . $hier_id . '"'));
144 }
145
146 $childs = $node->childNodes;
147 $content = "";
148 if ($childs->item(0) && $childs->item(0)->nodeName == "Paragraph") {
149 $content = $this->dom_util->dump($childs->item(0));
150 $content = substr(
151 $content,
152 strpos($content, ">") + 1,
153 strrpos($content, "<") - (strpos($content, ">") + 1)
154 );
155 $content = \ilPCParagraph::xml2output($content);
156 }
157 $hashes[$pc_id] =
158 array("hier_id" => $hier_id, "hash" => md5($dump), "content" => $content);
159 }
160
161 return $hashes;
162 }
163}
getPageContentsHashes(\ilPageObject $page)
Get page contents hashes.
addChangeDivClasses(\ilPageObject $page, array $a_hashes)
compare(\ilPageObject $main_page, \ilPageObject $l_page, \ilPageObject $r_page,)
setParagraphContent(\ilPageObject $page, string $a_hier_id, string $content)
static xml2output(string $a_text, bool $a_wysiwyg=false, bool $a_replace_lists=true, bool $unmask=true)
Converts xml from DB to output in edit textarea.
Class ilPageObject Handles PageObjects of ILIAS Learning Modules (see ILIAS DTD)
getDomDoc()
Get dom doc (DOMDocument)
addHierIDs()
Add hierarchical ID (e.g.
buildDom(bool $a_force=false)
preparePageForCompare(ilPageObject $page)
$path
Definition: ltiservices.php:30
global $DIC
Definition: shib_login.php:26