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