Documentation TYPO3 par Ameos |
00001 <?php 00002 /*************************************************************** 00003 * Copyright notice 00004 * 00005 * (c) 1999-2004 Kasper Skaarhoj (kasperYYYY@typo3.com) 00006 * All rights reserved 00007 * 00008 * This script is part of the TYPO3 project. The TYPO3 project is 00009 * free software; you can redistribute it and/or modify 00010 * it under the terms of the GNU General Public License as published by 00011 * the Free Software Foundation; either version 2 of the License, or 00012 * (at your option) any later version. 00013 * 00014 * The GNU General Public License can be found at 00015 * http://www.gnu.org/copyleft/gpl.html. 00016 * A copy is found in the textfile GPL.txt and important notices to the license 00017 * from the author is found in LICENSE.txt distributed with these scripts. 00018 * 00019 * 00020 * This script is distributed in the hope that it will be useful, 00021 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00022 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00023 * GNU General Public License for more details. 00024 * 00025 * This copyright notice MUST APPEAR in all copies of the script! 00026 ***************************************************************/ 00067 class t3lib_diff { 00068 00069 // External, static: 00070 var $stripTags = 0; // If set, the HTML tags are stripped from the input strings first. 00071 var $diffOptions = ''; // Diff options. eg "--unified=3" 00072 00073 // Internal, dynamic: 00074 var $clearBufferIdx=0; // This indicates the number of times the function addClearBuffer has been called - and used to detect the very first call... 00075 00076 00077 00078 00086 function makeDiffDisplay($str1,$str2) { 00087 if ($this->stripTags) { 00088 $str1 = strip_tags($str1); 00089 $str2 = strip_tags($str2); 00090 } else { 00091 $str1 = $this->tagSpace($str1); 00092 $str2 = $this->tagSpace($str2); 00093 } 00094 $str1Lines = $this->explodeStringIntoWords($str1); 00095 $str2Lines = $this->explodeStringIntoWords($str2); 00096 00097 $diffRes = $this->getDiff(implode(chr(10),$str1Lines).chr(10),implode(chr(10),$str2Lines).chr(10)); 00098 00099 if (is_array($diffRes)) { 00100 reset($diffRes); 00101 $c=0; 00102 $diffResArray=array(); 00103 while(list(,$lValue)=each($diffRes)) { 00104 if (intval($lValue)) { 00105 $c=intval($lValue); 00106 $diffResArray[$c]['changeInfo']=$lValue; 00107 } 00108 if (substr($lValue,0,1)=='<') { 00109 $diffResArray[$c]['old'][]=substr($lValue,2); 00110 } 00111 if (substr($lValue,0,1)=='>') { 00112 $diffResArray[$c]['new'][]=substr($lValue,2); 00113 } 00114 } 00115 00116 $outString=''; 00117 $clearBuffer=''; 00118 for ($a=-1;$a<count($str1Lines);$a++) { 00119 if (is_array($diffResArray[$a+1])) { 00120 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. 00121 $clearBuffer.=htmlspecialchars($str1Lines[$a]).' '; 00122 } 00123 00124 $outString.=$this->addClearBuffer($clearBuffer); 00125 $clearBuffer=''; 00126 if (is_array($diffResArray[$a+1]['old'])) { 00127 $outString.='<span class="diff-r">'.htmlspecialchars(implode(' ',$diffResArray[$a+1]['old'])).'</span> '; 00128 } 00129 if (is_array($diffResArray[$a+1]['new'])) { 00130 $outString.='<span class="diff-g">'.htmlspecialchars(implode(' ',$diffResArray[$a+1]['new'])).'</span> '; 00131 } 00132 $chInfParts = explode(',',$diffResArray[$a+1]['changeInfo']); 00133 if (!strcmp($chInfParts[0],$a+1)) { 00134 $newLine = intval($chInfParts[1])-1; 00135 if ($newLine>$a) $a=$newLine; // Security that $a is not set lower than current for some reason... 00136 } 00137 } else { 00138 $clearBuffer.=htmlspecialchars($str1Lines[$a]).' '; 00139 } 00140 } 00141 $outString.=$this->addClearBuffer($clearBuffer,1); 00142 00143 $outString = str_replace(' ',chr(10),$outString); 00144 if (!$this->stripTags) { 00145 $outString = $this->tagSpace($outString,1); 00146 } 00147 return $outString; 00148 } 00149 } 00150 00160 function getDiff($str1,$str2) { 00161 if (TYPO3_OS!='WIN') { 00162 // Create file 1 and write string 00163 $file1 = t3lib_div::tempnam('diff1_'); 00164 t3lib_div::writeFile($file1,$str1); 00165 // Create file 2 and write string 00166 $file2 = t3lib_div::tempnam('diff2_'); 00167 t3lib_div::writeFile($file2,$str2); 00168 // Perform diff. 00169 $cmd = $GLOBALS['TYPO3_CONF_VARS']['BE']['diff_path'].' '.$this->diffOptions.' '.$file1.' '.$file2; 00170 exec($cmd,$res); 00171 00172 unlink($file1); 00173 unlink($file2); 00174 00175 return $res; 00176 } 00177 } 00178 00187 function addClearBuffer($clearBuffer,$last=0) { 00188 if (strlen($clearBuffer)>200) { 00189 $clearBuffer=($this->clearBufferIdx?t3lib_div::fixed_lgd_cs($clearBuffer,70):'').'['.strlen($clearBuffer).']'.(!$last?t3lib_div::fixed_lgd_cs($clearBuffer,-70):''); 00190 } 00191 $this->clearBufferIdx++; 00192 return $clearBuffer; 00193 } 00194 00203 function explodeStringIntoWords($str) { 00204 $strArr = t3lib_div::trimExplode(chr(10),$str); 00205 $outArray=array(); 00206 reset($strArr); 00207 while(list(,$lineOfWords)=each($strArr)) { 00208 $allWords = t3lib_div::trimExplode(' ',$lineOfWords,1); 00209 $outArray = array_merge($outArray,$allWords); 00210 $outArray[]=''; 00211 $outArray[]=''; 00212 } 00213 return $outArray; 00214 } 00215 00224 function tagSpace($str,$rev=0) { 00225 if ($rev) { 00226 return str_replace(' <','<',str_replace('> ','>',$str)); 00227 } else { 00228 return str_replace('<',' <',str_replace('>','> ',$str)); 00229 } 00230 } 00231 } 00232 00233 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_diff.php']) { 00234 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_diff.php']); 00235 } 00236 ?>