[TASK] Adapt FormEngine translated records to Bootstrap
[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 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
21 */
22 class DiffUtility {
23
24 /**
25 * If set, the HTML tags are stripped from the input strings first.
26 *
27 * @var bool
28 */
29 public $stripTags = 0;
30
31 /**
32 * Diff options. eg "--unified=3"
33 *
34 * @var string
35 */
36 public $diffOptions = '';
37
38 /**
39 * This indicates the number of times the function addClearBuffer has been called - and used to detect the very first call...
40 *
41 * @var int
42 */
43 public $clearBufferIdx = 0;
44
45 /**
46 * @var int
47 */
48 public $differenceLgd = 0;
49
50 /**
51 * This will produce a color-marked-up diff output in HTML from the input strings.
52 *
53 * @param string $str1 String 1
54 * @param string $str2 String 2
55 * @param string $wrapTag Setting the wrapping tag name
56 * @return string Formatted output.
57 */
58 public function makeDiffDisplay($str1, $str2, $wrapTag = 'span') {
59 if ($this->stripTags) {
60 $str1 = strip_tags($str1);
61 $str2 = strip_tags($str2);
62 } else {
63 $str1 = $this->tagSpace($str1);
64 $str2 = $this->tagSpace($str2);
65 }
66 $str1Lines = $this->explodeStringIntoWords($str1);
67 $str2Lines = $this->explodeStringIntoWords($str2);
68 $diffRes = $this->getDiff(implode(LF, $str1Lines) . LF, implode(LF, $str2Lines) . LF);
69 if (is_array($diffRes)) {
70 $c = 0;
71 $diffResArray = array();
72 $differenceStr = '';
73 foreach ($diffRes as $lValue) {
74 if ((int)$lValue) {
75 $c = (int)$lValue;
76 $diffResArray[$c]['changeInfo'] = $lValue;
77 }
78 if ($lValue[0] === '<') {
79 $differenceStr .= ($diffResArray[$c]['old'][] = substr($lValue, 2));
80 }
81 if ($lValue[0] === '>') {
82 $differenceStr .= ($diffResArray[$c]['new'][] = substr($lValue, 2));
83 }
84 }
85 $this->differenceLgd = strlen($differenceStr);
86 $outString = '';
87 $clearBuffer = '';
88 $str1LinesCount = count($str1Lines);
89 for ($a = -1; $a < $str1LinesCount; $a++) {
90 if (is_array($diffResArray[$a + 1])) {
91 // 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.
92 if (strstr($diffResArray[$a + 1]['changeInfo'], 'a')) {
93 $clearBuffer .= htmlspecialchars($str1Lines[$a]) . ' ';
94 }
95 $outString .= $this->addClearBuffer($clearBuffer);
96 $clearBuffer = '';
97 if (is_array($diffResArray[$a + 1]['old'])) {
98 $outString .= '<' . $wrapTag . ' class="text-danger">' . htmlspecialchars(implode(' ', $diffResArray[($a + 1)]['old'])) . '</' . $wrapTag . '> ';
99 }
100 if (is_array($diffResArray[$a + 1]['new'])) {
101 $outString .= '<' . $wrapTag . ' class="text-success">' . htmlspecialchars(implode(' ', $diffResArray[($a + 1)]['new'])) . '</' . $wrapTag . '> ';
102 }
103 $chInfParts = explode(',', $diffResArray[$a + 1]['changeInfo']);
104 if ((string)$chInfParts[0] === (string)($a + 1)) {
105 $newLine = (int)$chInfParts[1] - 1;
106 if ($newLine > $a) {
107 $a = $newLine;
108 }
109 }
110 } else {
111 $clearBuffer .= htmlspecialchars($str1Lines[$a]) . ' ';
112 }
113 }
114 $outString .= $this->addClearBuffer($clearBuffer, 1);
115 $outString = str_replace(' ', LF, $outString);
116 if (!$this->stripTags) {
117 $outString = $this->tagSpace($outString, 1);
118 }
119 return $outString;
120 }
121 }
122
123 /**
124 * Produce a diff (using the "diff" application) between two strings
125 * The function will write the two input strings to temporary files, then execute the diff program, delete the temp files and return the result.
126 *
127 * @param string $str1 String 1
128 * @param string $str2 String 2
129 * @return array The result from the exec() function call.
130 * @access private
131 */
132 public function getDiff($str1, $str2) {
133 // Create file 1 and write string
134 $file1 = GeneralUtility::tempnam('diff1_');
135 GeneralUtility::writeFile($file1, $str1);
136 // Create file 2 and write string
137 $file2 = GeneralUtility::tempnam('diff2_');
138 GeneralUtility::writeFile($file2, $str2);
139 // Perform diff.
140 $cmd = $GLOBALS['TYPO3_CONF_VARS']['BE']['diff_path'] . ' ' . $this->diffOptions . ' ' . $file1 . ' ' . $file2;
141 $res = array();
142 CommandUtility::exec($cmd, $res);
143 unlink($file1);
144 unlink($file2);
145 return $res;
146 }
147
148 /**
149 * 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
150 *
151 * @param string $clearBuffer The input string.
152 * @param bool $last If set, it indicates that the string should just end with ... (thus no "complete" ending)
153 * @return string Processed string.
154 * @access private
155 */
156 public function addClearBuffer($clearBuffer, $last = 0) {
157 if (strlen($clearBuffer) > 200) {
158 $clearBuffer = ($this->clearBufferIdx ? GeneralUtility::fixed_lgd_cs($clearBuffer, 70) : '') . '[' . strlen($clearBuffer) . ']' . (!$last ? GeneralUtility::fixed_lgd_cs($clearBuffer, -70) : '');
159 }
160 $this->clearBufferIdx++;
161 return $clearBuffer;
162 }
163
164 /**
165 * Explodes the input string into words.
166 * 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.
167 *
168 * @param string $str The string input
169 * @return array Array with words.
170 * @access private
171 */
172 public function explodeStringIntoWords($str) {
173 $strArr = GeneralUtility::trimExplode(LF, $str);
174 $outArray = array();
175 foreach ($strArr as $lineOfWords) {
176 $allWords = GeneralUtility::trimExplode(' ', $lineOfWords, TRUE);
177 $outArray[] = $allWords;
178 $outArray[] = array('');
179 $outArray[] = array('');
180 }
181 return call_user_func_array('array_merge', $outArray);
182 }
183
184 /**
185 * Adds a space character before and after HTML tags (more precisely any found < or >)
186 *
187 * @param string $str String to process
188 * @param bool $rev If set, the < > searched for will be &lt; and &gt;
189 * @return string Processed string
190 * @access private
191 */
192 public function tagSpace($str, $rev = 0) {
193 if ($rev) {
194 return str_replace(' &lt;', '&lt;', str_replace('&gt; ', '&gt;', $str));
195 } else {
196 return str_replace('<', ' <', str_replace('>', '> ', $str));
197 }
198 }
199
200 }