b243dbbc665a8f9732d08af84768388b4628f574
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Utility / DiffUtility.php
1 <?php
2 namespace TYPO3\CMS\Core\Utility;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 /**
18 * This class has functions which generates a difference output of a content string
19 */
20 class DiffUtility {
21
22 /**
23 * If set, the HTML tags are stripped from the input strings first.
24 *
25 * @var bool
26 */
27 public $stripTags = 0;
28
29 /**
30 * Diff options. eg "--unified=3"
31 *
32 * @var string
33 */
34 public $diffOptions = '';
35
36 /**
37 * This indicates the number of times the function addClearBuffer has been called - and used to detect the very first call...
38 *
39 * @var int
40 */
41 public $clearBufferIdx = 0;
42
43 /**
44 * @var int
45 */
46 public $differenceLgd = 0;
47
48 /**
49 * This will produce a color-marked-up diff output in HTML from the input strings.
50 *
51 * @param string $str1 String 1
52 * @param string $str2 String 2
53 * @param string $wrapTag Setting the wrapping tag name
54 * @return string Formatted output.
55 */
56 public function makeDiffDisplay($str1, $str2, $wrapTag = 'span') {
57 if ($this->stripTags) {
58 $str1 = strip_tags($str1);
59 $str2 = strip_tags($str2);
60 } else {
61 $str1 = $this->tagSpace($str1);
62 $str2 = $this->tagSpace($str2);
63 }
64 $str1Lines = $this->explodeStringIntoWords($str1);
65 $str2Lines = $this->explodeStringIntoWords($str2);
66 $diffRes = $this->getDiff(implode(LF, $str1Lines) . LF, implode(LF, $str2Lines) . LF);
67 if (is_array($diffRes)) {
68 $c = 0;
69 $diffResArray = array();
70 $differenceStr = '';
71 foreach ($diffRes as $lValue) {
72 if ((int)$lValue) {
73 $c = (int)$lValue;
74 $diffResArray[$c]['changeInfo'] = $lValue;
75 }
76 if ($lValue[0] === '<') {
77 $differenceStr .= ($diffResArray[$c]['old'][] = substr($lValue, 2));
78 }
79 if ($lValue[0] === '>') {
80 $differenceStr .= ($diffResArray[$c]['new'][] = substr($lValue, 2));
81 }
82 }
83 $this->differenceLgd = strlen($differenceStr);
84 $outString = '';
85 $clearBuffer = '';
86 $str1LinesCount = count($str1Lines);
87 for ($a = -1; $a < $str1LinesCount; $a++) {
88 if (is_array($diffResArray[$a + 1])) {
89 // a=Add, c=change, d=delete: If a, then the content is Added after the entry and we must insert the line content as well.
90 if (strstr($diffResArray[$a + 1]['changeInfo'], 'a')) {
91 $clearBuffer .= htmlspecialchars($str1Lines[$a]) . ' ';
92 }
93 $outString .= $this->addClearBuffer($clearBuffer);
94 $clearBuffer = '';
95 if (is_array($diffResArray[$a + 1]['old'])) {
96 $outString .= '<' . $wrapTag . ' class="text-danger">' . htmlspecialchars(implode(' ', $diffResArray[($a + 1)]['old'])) . '</' . $wrapTag . '> ';
97 }
98 if (is_array($diffResArray[$a + 1]['new'])) {
99 $outString .= '<' . $wrapTag . ' class="text-success">' . htmlspecialchars(implode(' ', $diffResArray[($a + 1)]['new'])) . '</' . $wrapTag . '> ';
100 }
101 $chInfParts = explode(',', $diffResArray[$a + 1]['changeInfo']);
102 if ((string)$chInfParts[0] === (string)($a + 1)) {
103 $newLine = (int)$chInfParts[1] - 1;
104 if ($newLine > $a) {
105 $a = $newLine;
106 }
107 }
108 } else {
109 $clearBuffer .= htmlspecialchars($str1Lines[$a]) . ' ';
110 }
111 }
112 $outString .= $this->addClearBuffer($clearBuffer, 1);
113 $outString = str_replace(' ', LF, $outString);
114 if (!$this->stripTags) {
115 $outString = $this->tagSpace($outString, 1);
116 }
117 return $outString;
118 }
119 }
120
121 /**
122 * Produce a diff (using the "diff" application) between two strings
123 * The function will write the two input strings to temporary files, then execute the diff program, delete the temp files and return the result.
124 *
125 * @param string $str1 String 1
126 * @param string $str2 String 2
127 * @return array The result from the exec() function call.
128 * @access private
129 */
130 public function getDiff($str1, $str2) {
131 // Create file 1 and write string
132 $file1 = GeneralUtility::tempnam('diff1_');
133 GeneralUtility::writeFile($file1, $str1);
134 // Create file 2 and write string
135 $file2 = GeneralUtility::tempnam('diff2_');
136 GeneralUtility::writeFile($file2, $str2);
137 // Perform diff.
138 $cmd = $GLOBALS['TYPO3_CONF_VARS']['BE']['diff_path'] . ' ' . $this->diffOptions . ' ' . $file1 . ' ' . $file2;
139 $res = array();
140 CommandUtility::exec($cmd, $res);
141 unlink($file1);
142 unlink($file2);
143 return $res;
144 }
145
146 /**
147 * Will bring down the length of strings to < 150 chars if they were longer than 200 chars. This done by preserving the 70 first and last chars and concatenate those strings with "..." and a number indicating the string length
148 *
149 * @param string $clearBuffer The input string.
150 * @param bool $last If set, it indicates that the string should just end with ... (thus no "complete" ending)
151 * @return string Processed string.
152 * @access private
153 */
154 public function addClearBuffer($clearBuffer, $last = 0) {
155 if (strlen($clearBuffer) > 200) {
156 $clearBuffer = ($this->clearBufferIdx ? GeneralUtility::fixed_lgd_cs($clearBuffer, 70) : '') . '[' . strlen($clearBuffer) . ']' . (!$last ? GeneralUtility::fixed_lgd_cs($clearBuffer, -70) : '');
157 }
158 $this->clearBufferIdx++;
159 return $clearBuffer;
160 }
161
162 /**
163 * Explodes the input string into words.
164 * This is done by splitting first by lines, then by space char. Each word will be in stored as a value in an array. Lines will be indicated by two subsequent empty values.
165 *
166 * @param string $str The string input
167 * @return array Array with words.
168 * @access private
169 */
170 public function explodeStringIntoWords($str) {
171 $strArr = GeneralUtility::trimExplode(LF, $str);
172 $outArray = array();
173 foreach ($strArr as $lineOfWords) {
174 $allWords = GeneralUtility::trimExplode(' ', $lineOfWords, TRUE);
175 $outArray[] = $allWords;
176 $outArray[] = array('');
177 $outArray[] = array('');
178 }
179 return call_user_func_array('array_merge', $outArray);
180 }
181
182 /**
183 * Adds a space character before and after HTML tags (more precisely any found < or >)
184 *
185 * @param string $str String to process
186 * @param bool $rev If set, the < > searched for will be &lt; and &gt;
187 * @return string Processed string
188 * @access private
189 */
190 public function tagSpace($str, $rev = 0) {
191 if ($rev) {
192 return str_replace(' &lt;', '&lt;', str_replace('&gt; ', '&gt;', $str));
193 } else {
194 return str_replace('<', ' <', str_replace('>', '> ', $str));
195 }
196 }
197
198 }