2 namespace TYPO3\CMS\Core\Utility
;
5 * This file is part of the TYPO3 CMS project.
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.
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
14 * The TYPO3 project - inspiring people to share!
18 * This class has functions which generates a difference output of a content string
20 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
25 * If set, the HTML tags are stripped from the input strings first.
29 public $stripTags = 0;
32 * Diff options. eg "--unified=3"
36 public $diffOptions = '';
39 * This indicates the number of times the function addClearBuffer has been called - and used to detect the very first call...
43 public $clearBufferIdx = 0;
48 public $differenceLgd = 0;
51 * This will produce a color-marked-up diff output in HTML from the input strings.
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.
58 public function makeDiffDisplay($str1, $str2, $wrapTag = 'span') {
59 if ($this->stripTags
) {
60 $str1 = strip_tags($str1);
61 $str2 = strip_tags($str2);
63 $str1 = $this->tagSpace($str1);
64 $str2 = $this->tagSpace($str2);
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)) {
71 $diffResArray = array();
73 foreach ($diffRes as $lValue) {
76 $diffResArray[$c]['changeInfo'] = $lValue;
78 if ($lValue[0] === '<') {
79 $differenceStr .= ($diffResArray[$c]['old'][] = substr($lValue, 2));
81 if ($lValue[0] === '>') {
82 $differenceStr .= ($diffResArray[$c]['new'][] = substr($lValue, 2));
85 $this->differenceLgd
= strlen($differenceStr);
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]) . ' ';
95 $outString .= $this->addClearBuffer($clearBuffer);
97 if (is_array($diffResArray[$a +
1]['old'])) {
98 $outString .= '<' . $wrapTag . ' class="text-danger">' . htmlspecialchars(implode(' ', $diffResArray[($a +
1)]['old'])) . '</' . $wrapTag . '> ';
100 if (is_array($diffResArray[$a +
1]['new'])) {
101 $outString .= '<' . $wrapTag . ' class="text-success">' . htmlspecialchars(implode(' ', $diffResArray[($a +
1)]['new'])) . '</' . $wrapTag . '> ';
103 $chInfParts = explode(',', $diffResArray[$a +
1]['changeInfo']);
104 if ((string)$chInfParts[0] === (string)($a +
1)) {
105 $newLine = (int)$chInfParts[1] - 1;
111 $clearBuffer .= htmlspecialchars($str1Lines[$a]) . ' ';
114 $outString .= $this->addClearBuffer($clearBuffer, 1);
115 $outString = str_replace(' ', LF
, $outString);
116 if (!$this->stripTags
) {
117 $outString = $this->tagSpace($outString, 1);
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.
127 * @param string $str1 String 1
128 * @param string $str2 String 2
129 * @return array The result from the exec() function call.
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);
140 $cmd = $GLOBALS['TYPO3_CONF_VARS']['BE']['diff_path'] . ' ' . $this->diffOptions
. ' ' . $file1 . ' ' . $file2;
142 CommandUtility
::exec($cmd, $res);
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
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.
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) : '');
160 $this->clearBufferIdx++
;
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.
168 * @param string $str The string input
169 * @return array Array with words.
172 public function explodeStringIntoWords($str) {
173 $strArr = GeneralUtility
::trimExplode(LF
, $str);
175 foreach ($strArr as $lineOfWords) {
176 $allWords = GeneralUtility
::trimExplode(' ', $lineOfWords, TRUE);
177 $outArray[] = $allWords;
178 $outArray[] = array('');
179 $outArray[] = array('');
181 return call_user_func_array('array_merge', $outArray);
185 * Adds a space character before and after HTML tags (more precisely any found < or >)
187 * @param string $str String to process
188 * @param bool $rev If set, the < > searched for will be < and >
189 * @return string Processed string
192 public function tagSpace($str, $rev = 0) {
194 return str_replace(' <', '<', str_replace('> ', '>', $str));
196 return str_replace('<', ' <', str_replace('>', '> ', $str));