[TASK] Remove comments for revised in TYPO3 3.6
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Utility / DiffUtility.php
1 <?php
2 namespace TYPO3\CMS\Core\Utility;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 1999-2013 Kasper Skårhøj (kasperYYYY@typo3.com)
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 * A copy is found in the textfile GPL.txt and important notices to the license
19 * from the author is found in LICENSE.txt distributed with these scripts.
20 *
21 *
22 * This script is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * This copyright notice MUST APPEAR in all copies of the script!
28 ***************************************************************/
29
30 /**
31 * This class has functions which generates a difference output of a content string
32 *
33 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
34 */
35 class DiffUtility {
36
37 // External, static
38 // If set, the HTML tags are stripped from the input strings first.
39 /**
40 * @todo Define visibility
41 */
42 public $stripTags = 0;
43
44 // Diff options. eg "--unified=3"
45 /**
46 * @todo Define visibility
47 */
48 public $diffOptions = '';
49
50 // Internal, dynamic
51 // This indicates the number of times the function addClearBuffer has been called - and used to detect the very first call...
52 /**
53 * @todo Define visibility
54 */
55 public $clearBufferIdx = 0;
56
57 /**
58 * @todo Define visibility
59 */
60 public $differenceLgd = 0;
61
62 /**
63 * This will produce a color-marked-up diff output in HTML from the input strings.
64 *
65 * @param string $str1 String 1
66 * @param string $str2 String 2
67 * @param string $wrapTag Setting the wrapping tag name
68 * @return string Formatted output.
69 * @todo Define visibility
70 */
71 public function makeDiffDisplay($str1, $str2, $wrapTag = 'span') {
72 if ($this->stripTags) {
73 $str1 = strip_tags($str1);
74 $str2 = strip_tags($str2);
75 } else {
76 $str1 = $this->tagSpace($str1);
77 $str2 = $this->tagSpace($str2);
78 }
79 $str1Lines = $this->explodeStringIntoWords($str1);
80 $str2Lines = $this->explodeStringIntoWords($str2);
81 $diffRes = $this->getDiff(implode(LF, $str1Lines) . LF, implode(LF, $str2Lines) . LF);
82 if (is_array($diffRes)) {
83 $c = 0;
84 $diffResArray = array();
85 $differenceStr = '';
86 foreach ($diffRes as $lValue) {
87 if (intval($lValue)) {
88 $c = intval($lValue);
89 $diffResArray[$c]['changeInfo'] = $lValue;
90 }
91 if (substr($lValue, 0, 1) == '<') {
92 $differenceStr .= ($diffResArray[$c]['old'][] = substr($lValue, 2));
93 }
94 if (substr($lValue, 0, 1) == '>') {
95 $differenceStr .= ($diffResArray[$c]['new'][] = substr($lValue, 2));
96 }
97 }
98 $this->differenceLgd = strlen($differenceStr);
99 $outString = '';
100 $clearBuffer = '';
101 $str1LinesCount = count($str1Lines);
102 for ($a = -1; $a < $str1LinesCount; $a++) {
103 if (is_array($diffResArray[$a + 1])) {
104 // 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.
105 if (strstr($diffResArray[$a + 1]['changeInfo'], 'a')) {
106 $clearBuffer .= htmlspecialchars($str1Lines[$a]) . ' ';
107 }
108 $outString .= $this->addClearBuffer($clearBuffer);
109 $clearBuffer = '';
110 if (is_array($diffResArray[$a + 1]['old'])) {
111 $outString .= '<' . $wrapTag . ' class="diff-r">' . htmlspecialchars(implode(' ', $diffResArray[($a + 1)]['old'])) . '</' . $wrapTag . '> ';
112 }
113 if (is_array($diffResArray[$a + 1]['new'])) {
114 $outString .= '<' . $wrapTag . ' class="diff-g">' . htmlspecialchars(implode(' ', $diffResArray[($a + 1)]['new'])) . '</' . $wrapTag . '> ';
115 }
116 $chInfParts = explode(',', $diffResArray[$a + 1]['changeInfo']);
117 if (!strcmp($chInfParts[0], ($a + 1))) {
118 $newLine = intval($chInfParts[1]) - 1;
119 if ($newLine > $a) {
120 $a = $newLine;
121 }
122 }
123 } else {
124 $clearBuffer .= htmlspecialchars($str1Lines[$a]) . ' ';
125 }
126 }
127 $outString .= $this->addClearBuffer($clearBuffer, 1);
128 $outString = str_replace(' ', LF, $outString);
129 if (!$this->stripTags) {
130 $outString = $this->tagSpace($outString, 1);
131 }
132 return $outString;
133 }
134 }
135
136 /**
137 * Produce a diff (using the "diff" application) between two strings
138 * The function will write the two input strings to temporary files, then execute the diff program, delete the temp files and return the result.
139 *
140 * @param string $str1 String 1
141 * @param string $str2 String 2
142 * @return array The result from the exec() function call.
143 * @access private
144 * @todo Define visibility
145 */
146 public function getDiff($str1, $str2) {
147 // Create file 1 and write string
148 $file1 = \TYPO3\CMS\Core\Utility\GeneralUtility::tempnam('diff1_');
149 \TYPO3\CMS\Core\Utility\GeneralUtility::writeFile($file1, $str1);
150 // Create file 2 and write string
151 $file2 = \TYPO3\CMS\Core\Utility\GeneralUtility::tempnam('diff2_');
152 \TYPO3\CMS\Core\Utility\GeneralUtility::writeFile($file2, $str2);
153 // Perform diff.
154 $cmd = $GLOBALS['TYPO3_CONF_VARS']['BE']['diff_path'] . ' ' . $this->diffOptions . ' ' . $file1 . ' ' . $file2;
155 $res = array();
156 \TYPO3\CMS\Core\Utility\CommandUtility::exec($cmd, $res);
157 unlink($file1);
158 unlink($file2);
159 return $res;
160 }
161
162 /**
163 * 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
164 *
165 * @param string $clearBuffer The input string.
166 * @param boolean $last If set, it indicates that the string should just end with ... (thus no "complete" ending)
167 * @return string Processed string.
168 * @access private
169 * @todo Define visibility
170 */
171 public function addClearBuffer($clearBuffer, $last = 0) {
172 if (strlen($clearBuffer) > 200) {
173 $clearBuffer = ($this->clearBufferIdx ? \TYPO3\CMS\Core\Utility\GeneralUtility::fixed_lgd_cs($clearBuffer, 70) : '') . '[' . strlen($clearBuffer) . ']' . (!$last ? \TYPO3\CMS\Core\Utility\GeneralUtility::fixed_lgd_cs($clearBuffer, -70) : '');
174 }
175 $this->clearBufferIdx++;
176 return $clearBuffer;
177 }
178
179 /**
180 * Explodes the input string into words.
181 * 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.
182 *
183 * @param string $str The string input
184 * @return array Array with words.
185 * @access private
186 * @todo Define visibility
187 */
188 public function explodeStringIntoWords($str) {
189 $strArr = \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode(LF, $str);
190 $outArray = array();
191 foreach ($strArr as $lineOfWords) {
192 $allWords = \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode(' ', $lineOfWords, 1);
193 $outArray[] = $allWords;
194 $outArray[] = array('');
195 $outArray[] = array('');
196 }
197 return call_user_func_array('array_merge', $outArray);
198 }
199
200 /**
201 * Adds a space character before and after HTML tags (more precisely any found < or >)
202 *
203 * @param string $str String to process
204 * @param boolean $rev If set, the < > searched for will be &lt; and &gt;
205 * @return string Processed string
206 * @access private
207 * @todo Define visibility
208 */
209 public function tagSpace($str, $rev = 0) {
210 if ($rev) {
211 return str_replace(' &lt;', '&lt;', str_replace('&gt; ', '&gt;', $str));
212 } else {
213 return str_replace('<', ' <', str_replace('>', '> ', $str));
214 }
215 }
216
217 }
218
219
220 ?>