Initial revision
[Packages/TYPO3.CMS.git] / t3lib / class.t3lib_diff.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 1999-2003 Kasper Skårhøj (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 * This class has functions which generates a difference output of a content string
29 *
30 * @author Kasper Skårhøj <kasper@typo3.com>
31 * @package TYPO3
32 * @subpackage t3lib
33 */
34
35
36 class t3lib_diff {
37 var $stripTags = 0;
38 var $clearBufferIdx=0;
39
40 function getDiff($str1,$str2) {
41 if (TYPO3_OS!="WIN") {
42 // Create file 1 and write string
43 $file1 = tempnam("","");
44 t3lib_div::writeFile($file1,$str1);
45 // Create file 2 and write string
46 $file2 = tempnam("","");
47 t3lib_div::writeFile($file2,$str2);
48 // Perform diff.
49 $cmd = $GLOBALS["TYPO3_CONF_VARS"]["BE"]["diff_path"]." ".$file1." ".$file2;
50 exec($cmd,$res);
51
52 unlink($file1);
53 unlink($file2);
54
55 return $res;
56 }
57 }
58 function explodeStringIntoWords($str) {
59 $strArr = t3lib_div::trimExplode(chr(10),$str);
60 $outArray=array();
61 reset($strArr);
62 while(list(,$lineOfWords)=each($strArr)) {
63 $allWords = t3lib_div::trimExplode(" ",$lineOfWords,1);
64 $outArray = array_merge($outArray,$allWords);
65 $outArray[]="";
66 $outArray[]="";
67 }
68 return $outArray;
69 }
70 function tagSpace($str,$rev=0) {
71 if ($rev) {
72 return str_replace(" &lt;","&lt;",str_replace("&gt; ","&gt;",$str));
73 } else {
74 return str_replace("<"," <",str_replace(">","> ",$str));
75 }
76 }
77 function makeDiffDisplay($str1,$str2) {
78 if ($this->stripTags) {
79 $str1 = strip_tags($str1);
80 $str2 = strip_tags($str2);
81 } else {
82 $str1 = $this->tagSpace($str1);
83 $str2 = $this->tagSpace($str2);
84 }
85 $str1Lines = $this->explodeStringIntoWords($str1);
86 $str2Lines = $this->explodeStringIntoWords($str2);
87 // debug($str1Lines);
88 // debug($str2Lines);
89 $diffRes = $this->getDiff(implode(chr(10),$str1Lines).chr(10),implode(chr(10),$str2Lines).chr(10));
90 //debug($diffRes);
91 if (is_array($diffRes)) {
92 reset($diffRes);
93 $c=0;
94 $diffResArray=array();
95 while(list(,$lValue)=each($diffRes)) {
96 if (intval($lValue)) {
97 $c=intval($lValue);
98 $diffResArray[$c]["changeInfo"]=$lValue;
99 }
100 if (substr($lValue,0,1)=="<") {
101 $diffResArray[$c]["old"][]=substr($lValue,2);
102 }
103 if (substr($lValue,0,1)==">") {
104 $diffResArray[$c]["new"][]=substr($lValue,2);
105 }
106 }
107 // debug($str1Lines);
108 // debug($str2Lines);
109 // debug($diffResArray);
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.='<font color=red>'.htmlspecialchars(implode(" ",$diffResArray[$a+1]["old"])).'</font> ';
123 }
124 if (is_array($diffResArray[$a+1]["new"])) {
125 $outString.='<font color=green>'.htmlspecialchars(implode(" ",$diffResArray[$a+1]["new"])).'</font> ';
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) $a=$newLine; // Security that $a is not set lower than current for some reason...
131 }
132 } else {
133 $clearBuffer.=htmlspecialchars($str1Lines[$a])." ";
134 }
135 }
136 $outString.=$this->addClearBuffer($clearBuffer,1);
137
138 $outString = str_replace(" ",chr(10),$outString);
139 if (!$this->stripTags) {
140 $outString = $this->tagSpace($outString,1);
141 }
142 return $outString;
143 }
144 }
145 function addClearBuffer($clearBuffer,$last=0) {
146 if (strlen($clearBuffer)>200) {
147 $clearBuffer=($this->clearBufferIdx?t3lib_div::fixed_lgd($clearBuffer,70):"")."[".strlen($clearBuffer)."]".(!$last?t3lib_div::fixed_lgd_pre($clearBuffer,70):"");
148 }
149 $this->clearBufferIdx++;
150 return $clearBuffer;
151 }
152 }
153
154
155
156 if (defined("TYPO3_MODE") && $TYPO3_CONF_VARS[TYPO3_MODE]["XCLASS"]["t3lib/class.t3lib_diff.php"]) {
157 include_once($TYPO3_CONF_VARS[TYPO3_MODE]["XCLASS"]["t3lib/class.t3lib_diff.php"]);
158 }
159
160 ?>