small text corrections
[Packages/TYPO3.CMS.git] / t3lib / class.t3lib_diff.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 1999-2005 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 * 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 *
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
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 * @return string Formatted output.
84 */
85 function makeDiffDisplay($str1,$str2) {
86 if ($this->stripTags) {
87 $str1 = strip_tags($str1);
88 $str2 = strip_tags($str2);
89 } else {
90 $str1 = $this->tagSpace($str1);
91 $str2 = $this->tagSpace($str2);
92 }
93 $str1Lines = $this->explodeStringIntoWords($str1);
94 $str2Lines = $this->explodeStringIntoWords($str2);
95
96 $diffRes = $this->getDiff(implode(chr(10),$str1Lines).chr(10),implode(chr(10),$str2Lines).chr(10));
97
98 if (is_array($diffRes)) {
99 reset($diffRes);
100 $c=0;
101 $diffResArray=array();
102 while(list(,$lValue)=each($diffRes)) {
103 if (intval($lValue)) {
104 $c=intval($lValue);
105 $diffResArray[$c]['changeInfo']=$lValue;
106 }
107 if (substr($lValue,0,1)=='<') {
108 $diffResArray[$c]['old'][]=substr($lValue,2);
109 }
110 if (substr($lValue,0,1)=='>') {
111 $diffResArray[$c]['new'][]=substr($lValue,2);
112 }
113 }
114
115 $outString='';
116 $clearBuffer='';
117 for ($a=-1;$a<count($str1Lines);$a++) {
118 if (is_array($diffResArray[$a+1])) {
119 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.
120 $clearBuffer.=htmlspecialchars($str1Lines[$a]).' ';
121 }
122
123 $outString.=$this->addClearBuffer($clearBuffer);
124 $clearBuffer='';
125 if (is_array($diffResArray[$a+1]['old'])) {
126 $outString.='<span class="diff-r">'.htmlspecialchars(implode(' ',$diffResArray[$a+1]['old'])).'</span> ';
127 }
128 if (is_array($diffResArray[$a+1]['new'])) {
129 $outString.='<span class="diff-g">'.htmlspecialchars(implode(' ',$diffResArray[$a+1]['new'])).'</span> ';
130 }
131 $chInfParts = explode(',',$diffResArray[$a+1]['changeInfo']);
132 if (!strcmp($chInfParts[0],$a+1)) {
133 $newLine = intval($chInfParts[1])-1;
134 if ($newLine>$a) $a=$newLine; // Security that $a is not set lower than current for some reason...
135 }
136 } else {
137 $clearBuffer.=htmlspecialchars($str1Lines[$a]).' ';
138 }
139 }
140 $outString.=$this->addClearBuffer($clearBuffer,1);
141
142 $outString = str_replace(' ',chr(10),$outString);
143 if (!$this->stripTags) {
144 $outString = $this->tagSpace($outString,1);
145 }
146 return $outString;
147 }
148 }
149
150 /**
151 * Produce a diff (using the "diff" application) between two strings
152 * The function will write the two input strings to temporary files, then execute the diff program, delete the temp files and return the result.
153 *
154 * @param string String 1
155 * @param string String 2
156 * @return array The result from the exec() function call.
157 * @access private
158 */
159 function getDiff($str1,$str2) {
160 // Create file 1 and write string
161 $file1 = t3lib_div::tempnam('diff1_');
162 t3lib_div::writeFile($file1,$str1);
163 // Create file 2 and write string
164 $file2 = t3lib_div::tempnam('diff2_');
165 t3lib_div::writeFile($file2,$str2);
166 // Perform diff.
167 $cmd = $GLOBALS['TYPO3_CONF_VARS']['BE']['diff_path'].' '.$this->diffOptions.' '.$file1.' '.$file2;
168 exec($cmd,$res);
169
170 unlink($file1);
171 unlink($file2);
172
173 return $res;
174 }
175
176 /**
177 * 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
178 *
179 * @param string The input string.
180 * @param boolean If set, it indicates that the string should just end with ... (thus no "complete" ending)
181 * @return string Processed string.
182 * @access private
183 */
184 function addClearBuffer($clearBuffer,$last=0) {
185 if (strlen($clearBuffer)>200) {
186 $clearBuffer=($this->clearBufferIdx?t3lib_div::fixed_lgd_cs($clearBuffer,70):'').'['.strlen($clearBuffer).']'.(!$last?t3lib_div::fixed_lgd_cs($clearBuffer,-70):'');
187 }
188 $this->clearBufferIdx++;
189 return $clearBuffer;
190 }
191
192 /**
193 * Explodes the input string into words.
194 * 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.
195 *
196 * @param string The string input
197 * @return array Array with words.
198 * @access private
199 */
200 function explodeStringIntoWords($str) {
201 $strArr = t3lib_div::trimExplode(chr(10),$str);
202 $outArray=array();
203 reset($strArr);
204 while(list(,$lineOfWords)=each($strArr)) {
205 $allWords = t3lib_div::trimExplode(' ',$lineOfWords,1);
206 $outArray = array_merge($outArray,$allWords);
207 $outArray[]='';
208 $outArray[]='';
209 }
210 return $outArray;
211 }
212
213 /**
214 * Adds a space character before and after HTML tags (more precisely any found < or >)
215 *
216 * @param string String to process
217 * @param boolean If set, the < > searched for will be &lt; and &gt;
218 * @return string Processed string
219 * @access private
220 */
221 function tagSpace($str,$rev=0) {
222 if ($rev) {
223 return str_replace(' &lt;','&lt;',str_replace('&gt; ','&gt;',$str));
224 } else {
225 return str_replace('<',' <',str_replace('>','> ',$str));
226 }
227 }
228 }
229
230 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_diff.php']) {
231 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_diff.php']);
232 }
233 ?>