* Created new function, t3lib_div::fixed_lgd_cs(), which should be used in the backen...
[Packages/TYPO3.CMS.git] / t3lib / class.t3lib_diff.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 1999-2004 Kasper Skaarhoj (kasper@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 * $Id$
31 * Revised for TYPO3 3.6 November/2003 by Kasper Skaarhoj
32 * XHTML Compliant
33 *
34 * @author Kasper Skaarhoj <kasper@typo3.com>
35 */
36 /**
37 * [CLASS/FUNCTION INDEX of SCRIPT]
38 *
39 *
40 *
41 * 67: class t3lib_diff
42 * 86: function makeDiffDisplay($str1,$str2)
43 * 160: function getDiff($str1,$str2)
44 * 187: function addClearBuffer($clearBuffer,$last=0)
45 * 203: function explodeStringIntoWords($str)
46 * 224: function tagSpace($str,$rev=0)
47 *
48 * TOTAL FUNCTIONS: 5
49 * (This index is automatically created/updated by the extension "extdeveval")
50 *
51 */
52
53
54
55
56
57
58
59 /**
60 * This class has functions which generates a difference output of a content string
61 * Currently works only with LINUX/UNIX
62 *
63 * @author Kasper Skaarhoj <kasper@typo3.com>
64 * @package TYPO3
65 * @subpackage t3lib
66 */
67 class t3lib_diff {
68
69 // External, static:
70 var $stripTags = 0; // If set, the HTML tags are stripped from the input strings first.
71 var $diffOptions = ''; // Diff options. eg "--unified=3"
72
73 // Internal, dynamic:
74 var $clearBufferIdx=0; // This indicates the number of times the function addClearBuffer has been called - and used to detect the very first call...
75
76
77
78
79 /**
80 * This will produce a color-marked-up diff output in HTML from the input strings.
81 *
82 * @param string String 1
83 * @param string String 2
84 * @return string Formatted output.
85 */
86 function makeDiffDisplay($str1,$str2) {
87 if ($this->stripTags) {
88 $str1 = strip_tags($str1);
89 $str2 = strip_tags($str2);
90 } else {
91 $str1 = $this->tagSpace($str1);
92 $str2 = $this->tagSpace($str2);
93 }
94 $str1Lines = $this->explodeStringIntoWords($str1);
95 $str2Lines = $this->explodeStringIntoWords($str2);
96
97 $diffRes = $this->getDiff(implode(chr(10),$str1Lines).chr(10),implode(chr(10),$str2Lines).chr(10));
98
99 if (is_array($diffRes)) {
100 reset($diffRes);
101 $c=0;
102 $diffResArray=array();
103 while(list(,$lValue)=each($diffRes)) {
104 if (intval($lValue)) {
105 $c=intval($lValue);
106 $diffResArray[$c]['changeInfo']=$lValue;
107 }
108 if (substr($lValue,0,1)=='<') {
109 $diffResArray[$c]['old'][]=substr($lValue,2);
110 }
111 if (substr($lValue,0,1)=='>') {
112 $diffResArray[$c]['new'][]=substr($lValue,2);
113 }
114 }
115
116 $outString='';
117 $clearBuffer='';
118 for ($a=-1;$a<count($str1Lines);$a++) {
119 if (is_array($diffResArray[$a+1])) {
120 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.
121 $clearBuffer.=htmlspecialchars($str1Lines[$a]).' ';
122 }
123
124 $outString.=$this->addClearBuffer($clearBuffer);
125 $clearBuffer='';
126 if (is_array($diffResArray[$a+1]['old'])) {
127 $outString.='<span class="diff-r">'.htmlspecialchars(implode(' ',$diffResArray[$a+1]['old'])).'</span> ';
128 }
129 if (is_array($diffResArray[$a+1]['new'])) {
130 $outString.='<span class="diff-g">'.htmlspecialchars(implode(' ',$diffResArray[$a+1]['new'])).'</span> ';
131 }
132 $chInfParts = explode(',',$diffResArray[$a+1]['changeInfo']);
133 if (!strcmp($chInfParts[0],$a+1)) {
134 $newLine = intval($chInfParts[1])-1;
135 if ($newLine>$a) $a=$newLine; // Security that $a is not set lower than current for some reason...
136 }
137 } else {
138 $clearBuffer.=htmlspecialchars($str1Lines[$a]).' ';
139 }
140 }
141 $outString.=$this->addClearBuffer($clearBuffer,1);
142
143 $outString = str_replace(' ',chr(10),$outString);
144 if (!$this->stripTags) {
145 $outString = $this->tagSpace($outString,1);
146 }
147 return $outString;
148 }
149 }
150
151 /**
152 * Produce a diff (with the "diff" application on unix) between two strings
153 * The function will write the two input strings to temporary files, then execute the diff program, delete the temp files and return the result.
154 *
155 * @param string String 1
156 * @param string String 2
157 * @return array The result from the exec() function call.
158 * @access private
159 */
160 function getDiff($str1,$str2) {
161 if (TYPO3_OS!='WIN') {
162 // Create file 1 and write string
163 $file1 = t3lib_div::tempnam('diff1_');
164 t3lib_div::writeFile($file1,$str1);
165 // Create file 2 and write string
166 $file2 = t3lib_div::tempnam('diff2_');
167 t3lib_div::writeFile($file2,$str2);
168 // Perform diff.
169 $cmd = $GLOBALS['TYPO3_CONF_VARS']['BE']['diff_path'].' '.$this->diffOptions.' '.$file1.' '.$file2;
170 exec($cmd,$res);
171
172 unlink($file1);
173 unlink($file2);
174
175 return $res;
176 }
177 }
178
179 /**
180 * 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
181 *
182 * @param string The input string.
183 * @param boolean If set, it indicates that the string should just end with ... (thus no "complete" ending)
184 * @return string Processed string.
185 * @access private
186 */
187 function addClearBuffer($clearBuffer,$last=0) {
188 if (strlen($clearBuffer)>200) {
189 $clearBuffer=($this->clearBufferIdx?t3lib_div::fixed_lgd_cs($clearBuffer,70):'').'['.strlen($clearBuffer).']'.(!$last?t3lib_div::fixed_lgd_cs($clearBuffer,-70):'');
190 }
191 $this->clearBufferIdx++;
192 return $clearBuffer;
193 }
194
195 /**
196 * Explodes the input string into words.
197 * 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.
198 *
199 * @param string The string input
200 * @return array Array with words.
201 * @access private
202 */
203 function explodeStringIntoWords($str) {
204 $strArr = t3lib_div::trimExplode(chr(10),$str);
205 $outArray=array();
206 reset($strArr);
207 while(list(,$lineOfWords)=each($strArr)) {
208 $allWords = t3lib_div::trimExplode(' ',$lineOfWords,1);
209 $outArray = array_merge($outArray,$allWords);
210 $outArray[]='';
211 $outArray[]='';
212 }
213 return $outArray;
214 }
215
216 /**
217 * Adds a space character before and after HTML tags (more precisely any found < or >)
218 *
219 * @param string String to process
220 * @param boolean If set, the < > searched for will be &lt; and &gt;
221 * @return string Processed string
222 * @access private
223 */
224 function tagSpace($str,$rev=0) {
225 if ($rev) {
226 return str_replace(' &lt;','&lt;',str_replace('&gt; ','&gt;',$str));
227 } else {
228 return str_replace('<',' <',str_replace('>','> ',$str));
229 }
230 }
231 }
232
233 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_diff.php']) {
234 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_diff.php']);
235 }
236 ?>