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