*** empty log message ***
[Packages/TYPO3.CMS.git] / t3lib / class.t3lib_diff.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 1999-2003 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 *
32 * @author Kasper Skaarhoj <kasper@typo3.com>
33 */
34 /**
35 * [CLASS/FUNCTION INDEX of SCRIPT]
36 *
37 *
38 *
39 * 65: class t3lib_diff
40 * 74: function getDiff($str1,$str2)
41 * 99: function explodeStringIntoWords($str)
42 * 119: function tagSpace($str,$rev=0)
43 * 134: function makeDiffDisplay($str1,$str2)
44 * 206: function addClearBuffer($clearBuffer,$last=0)
45 *
46 * TOTAL FUNCTIONS: 5
47 * (This index is automatically created/updated by the extension "extdeveval")
48 *
49 */
50
51
52
53
54
55
56
57 /**
58 * This class has functions which generates a difference output of a content string
59 * Currently works only with LINUX/UNIX
60 *
61 * @author Kasper Skaarhoj <kasper@typo3.com>
62 * @package TYPO3
63 * @subpackage t3lib
64 */
65 class t3lib_diff {
66 var $stripTags = 0;
67 var $clearBufferIdx=0;
68
69 /**
70 * @param [type] $str1: ...
71 * @param [type] $str2: ...
72 * @return [type] ...
73 */
74 function getDiff($str1,$str2) {
75 if (TYPO3_OS!='WIN') {
76 // Create file 1 and write string
77 $file1 = tempnam('','');
78 t3lib_div::writeFile($file1,$str1);
79 // Create file 2 and write string
80 $file2 = tempnam('','');
81 t3lib_div::writeFile($file2,$str2);
82 // Perform diff.
83 $cmd = $GLOBALS['TYPO3_CONF_VARS']['BE']['diff_path'].' '.$file1.' '.$file2;
84 exec($cmd,$res);
85
86 unlink($file1);
87 unlink($file2);
88
89 return $res;
90 }
91 }
92
93 /**
94 * [Describe function...]
95 *
96 * @param [type] $str: ...
97 * @return [type] ...
98 */
99 function explodeStringIntoWords($str) {
100 $strArr = t3lib_div::trimExplode(chr(10),$str);
101 $outArray=array();
102 reset($strArr);
103 while(list(,$lineOfWords)=each($strArr)) {
104 $allWords = t3lib_div::trimExplode(' ',$lineOfWords,1);
105 $outArray = array_merge($outArray,$allWords);
106 $outArray[]='';
107 $outArray[]='';
108 }
109 return $outArray;
110 }
111
112 /**
113 * [Describe function...]
114 *
115 * @param [type] $str: ...
116 * @param [type] $rev: ...
117 * @return [type] ...
118 */
119 function tagSpace($str,$rev=0) {
120 if ($rev) {
121 return str_replace(' &lt;','&lt;',str_replace('&gt; ','&gt;',$str));
122 } else {
123 return str_replace('<',' <',str_replace('>','> ',$str));
124 }
125 }
126
127 /**
128 * [Describe function...]
129 *
130 * @param [type] $str1: ...
131 * @param [type] $str2: ...
132 * @return [type] ...
133 */
134 function makeDiffDisplay($str1,$str2) {
135 if ($this->stripTags) {
136 $str1 = strip_tags($str1);
137 $str2 = strip_tags($str2);
138 } else {
139 $str1 = $this->tagSpace($str1);
140 $str2 = $this->tagSpace($str2);
141 }
142 $str1Lines = $this->explodeStringIntoWords($str1);
143 $str2Lines = $this->explodeStringIntoWords($str2);
144
145 $diffRes = $this->getDiff(implode(chr(10),$str1Lines).chr(10),implode(chr(10),$str2Lines).chr(10));
146
147 if (is_array($diffRes)) {
148 reset($diffRes);
149 $c=0;
150 $diffResArray=array();
151 while(list(,$lValue)=each($diffRes)) {
152 if (intval($lValue)) {
153 $c=intval($lValue);
154 $diffResArray[$c]['changeInfo']=$lValue;
155 }
156 if (substr($lValue,0,1)=='<') {
157 $diffResArray[$c]['old'][]=substr($lValue,2);
158 }
159 if (substr($lValue,0,1)=='>') {
160 $diffResArray[$c]['new'][]=substr($lValue,2);
161 }
162 }
163
164 $outString='';
165 $clearBuffer='';
166 for ($a=-1;$a<count($str1Lines);$a++) {
167 if (is_array($diffResArray[$a+1])) {
168 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.
169 $clearBuffer.=htmlspecialchars($str1Lines[$a]).' ';
170 }
171
172 $outString.=$this->addClearBuffer($clearBuffer);
173 $clearBuffer='';
174 if (is_array($diffResArray[$a+1]['old'])) {
175 $outString.='<font color="red">'.htmlspecialchars(implode(' ',$diffResArray[$a+1]['old'])).'</font> ';
176 }
177 if (is_array($diffResArray[$a+1]['new'])) {
178 $outString.='<font color="green">'.htmlspecialchars(implode(' ',$diffResArray[$a+1]['new'])).'</font> ';
179 }
180 $chInfParts = explode(',',$diffResArray[$a+1]['changeInfo']);
181 if (!strcmp($chInfParts[0],$a+1)) {
182 $newLine = intval($chInfParts[1])-1;
183 if ($newLine>$a) $a=$newLine; // Security that $a is not set lower than current for some reason...
184 }
185 } else {
186 $clearBuffer.=htmlspecialchars($str1Lines[$a]).' ';
187 }
188 }
189 $outString.=$this->addClearBuffer($clearBuffer,1);
190
191 $outString = str_replace(' ',chr(10),$outString);
192 if (!$this->stripTags) {
193 $outString = $this->tagSpace($outString,1);
194 }
195 return $outString;
196 }
197 }
198
199 /**
200 * [Describe function...]
201 *
202 * @param [type] $clearBuffer: ...
203 * @param [type] $last: ...
204 * @return [type] ...
205 */
206 function addClearBuffer($clearBuffer,$last=0) {
207 if (strlen($clearBuffer)>200) {
208 $clearBuffer=($this->clearBufferIdx?t3lib_div::fixed_lgd($clearBuffer,70):'').'['.strlen($clearBuffer).']'.(!$last?t3lib_div::fixed_lgd_pre($clearBuffer,70):'');
209 }
210 $this->clearBufferIdx++;
211 return $clearBuffer;
212 }
213 }
214
215 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_diff.php']) {
216 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_diff.php']);
217 }
218 ?>