|
TYPO3 API
SVNRelease
|
00001 <?php 00002 /*************************************************************** 00003 * Copyright notice 00004 * 00005 * (c) 1999-2011 Kasper Skårhøj (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 ***************************************************************/ 00027 /** 00028 * Contains class which has functions that generates a difference output of a content string 00029 * 00030 * $Id: class.t3lib_diff.php 10121 2011-01-18 20:15:30Z ohader $ 00031 * Revised for TYPO3 3.6 November/2003 by Kasper Skårhøj 00032 * XHTML Compliant 00033 * 00034 * @author Kasper Skårhøj <kasperYYYY@typo3.com> 00035 */ 00036 /** 00037 * [CLASS/FUNCTION INDEX of SCRIPT] 00038 * 00039 * 00040 * 00041 * 66: class t3lib_diff 00042 * 86: function makeDiffDisplay($str1,$str2,$wrapTag='span') 00043 * 163: function getDiff($str1,$str2) 00044 * 189: function addClearBuffer($clearBuffer,$last=0) 00045 * 205: function explodeStringIntoWords($str) 00046 * 226: function tagSpace($str,$rev=0) 00047 * 00048 * TOTAL FUNCTIONS: 5 00049 * (This index is automatically created/updated by the extension "extdeveval") 00050 * 00051 */ 00052 00053 00054 /** 00055 * This class has functions which generates a difference output of a content string 00056 * 00057 * @author Kasper Skårhøj <kasperYYYY@typo3.com> 00058 * @package TYPO3 00059 * @subpackage t3lib 00060 */ 00061 class t3lib_diff { 00062 00063 // External, static: 00064 var $stripTags = 0; // If set, the HTML tags are stripped from the input strings first. 00065 var $diffOptions = ''; // Diff options. eg "--unified=3" 00066 00067 // Internal, dynamic: 00068 var $clearBufferIdx = 0; // This indicates the number of times the function addClearBuffer has been called - and used to detect the very first call... 00069 var $differenceLgd = 0; 00070 00071 00072 /** 00073 * This will produce a color-marked-up diff output in HTML from the input strings. 00074 * 00075 * @param string String 1 00076 * @param string String 2 00077 * @param string Setting the wrapping tag name 00078 * @return string Formatted output. 00079 */ 00080 function makeDiffDisplay($str1, $str2, $wrapTag = 'span') { 00081 if ($this->stripTags) { 00082 $str1 = strip_tags($str1); 00083 $str2 = strip_tags($str2); 00084 } else { 00085 $str1 = $this->tagSpace($str1); 00086 $str2 = $this->tagSpace($str2); 00087 } 00088 $str1Lines = $this->explodeStringIntoWords($str1); 00089 $str2Lines = $this->explodeStringIntoWords($str2); 00090 00091 $diffRes = $this->getDiff(implode(LF, $str1Lines) . LF, implode(LF, $str2Lines) . LF); 00092 00093 if (is_array($diffRes)) { 00094 $c = 0; 00095 $diffResArray = array(); 00096 $differenceStr = ''; 00097 foreach ($diffRes as $lValue) { 00098 if (intval($lValue)) { 00099 $c = intval($lValue); 00100 $diffResArray[$c]['changeInfo'] = $lValue; 00101 } 00102 if (substr($lValue, 0, 1) == '<') { 00103 $differenceStr .= $diffResArray[$c]['old'][] = substr($lValue, 2); 00104 } 00105 if (substr($lValue, 0, 1) == '>') { 00106 $differenceStr .= $diffResArray[$c]['new'][] = substr($lValue, 2); 00107 } 00108 } 00109 00110 $this->differenceLgd = strlen($differenceStr); 00111 00112 $outString = ''; 00113 $clearBuffer = ''; 00114 for ($a = -1; $a < count($str1Lines); $a++) { 00115 if (is_array($diffResArray[$a + 1])) { 00116 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. 00117 $clearBuffer .= htmlspecialchars($str1Lines[$a]) . ' '; 00118 } 00119 00120 $outString .= $this->addClearBuffer($clearBuffer); 00121 $clearBuffer = ''; 00122 if (is_array($diffResArray[$a + 1]['old'])) { 00123 $outString .= '<' . $wrapTag . ' class="diff-r">' . htmlspecialchars(implode(' ', $diffResArray[$a + 1]['old'])) . '</' . $wrapTag . '> '; 00124 } 00125 if (is_array($diffResArray[$a + 1]['new'])) { 00126 $outString .= '<' . $wrapTag . ' class="diff-g">' . htmlspecialchars(implode(' ', $diffResArray[$a + 1]['new'])) . '</' . $wrapTag . '> '; 00127 } 00128 $chInfParts = explode(',', $diffResArray[$a + 1]['changeInfo']); 00129 if (!strcmp($chInfParts[0], $a + 1)) { 00130 $newLine = intval($chInfParts[1]) - 1; 00131 if ($newLine > $a) { 00132 $a = $newLine; 00133 } // Security that $a is not set lower than current for some reason... 00134 } 00135 } else { 00136 $clearBuffer .= htmlspecialchars($str1Lines[$a]) . ' '; 00137 } 00138 } 00139 $outString .= $this->addClearBuffer($clearBuffer, 1); 00140 00141 $outString = str_replace(' ', LF, $outString); 00142 if (!$this->stripTags) { 00143 $outString = $this->tagSpace($outString, 1); 00144 } 00145 return $outString; 00146 } 00147 } 00148 00149 /** 00150 * Produce a diff (using the "diff" application) between two strings 00151 * The function will write the two input strings to temporary files, then execute the diff program, delete the temp files and return the result. 00152 * 00153 * @param string String 1 00154 * @param string String 2 00155 * @return array The result from the exec() function call. 00156 * @access private 00157 */ 00158 function getDiff($str1, $str2) { 00159 // Create file 1 and write string 00160 $file1 = t3lib_div::tempnam('diff1_'); 00161 t3lib_div::writeFile($file1, $str1); 00162 // Create file 2 and write string 00163 $file2 = t3lib_div::tempnam('diff2_'); 00164 t3lib_div::writeFile($file2, $str2); 00165 // Perform diff. 00166 $cmd = $GLOBALS['TYPO3_CONF_VARS']['BE']['diff_path'] . ' ' . $this->diffOptions . ' ' . $file1 . ' ' . $file2; 00167 $res = array(); 00168 t3lib_utility_Command::exec($cmd, $res); 00169 00170 unlink($file1); 00171 unlink($file2); 00172 00173 return $res; 00174 } 00175 00176 /** 00177 * 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 00178 * 00179 * @param string The input string. 00180 * @param boolean If set, it indicates that the string should just end with ... (thus no "complete" ending) 00181 * @return string Processed string. 00182 * @access private 00183 */ 00184 function addClearBuffer($clearBuffer, $last = 0) { 00185 if (strlen($clearBuffer) > 200) { 00186 $clearBuffer = ($this->clearBufferIdx ? t3lib_div::fixed_lgd_cs($clearBuffer, 70) : '') . '[' . strlen($clearBuffer) . ']' . (!$last ? t3lib_div::fixed_lgd_cs($clearBuffer, -70) : ''); 00187 } 00188 $this->clearBufferIdx++; 00189 return $clearBuffer; 00190 } 00191 00192 /** 00193 * Explodes the input string into words. 00194 * 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. 00195 * 00196 * @param string The string input 00197 * @return array Array with words. 00198 * @access private 00199 */ 00200 function explodeStringIntoWords($str) { 00201 $strArr = t3lib_div::trimExplode(LF, $str); 00202 $outArray = array(); 00203 foreach ($strArr as $lineOfWords) { 00204 $allWords = t3lib_div::trimExplode(' ', $lineOfWords, 1); 00205 $outArray = array_merge($outArray, $allWords); 00206 $outArray[] = ''; 00207 $outArray[] = ''; 00208 } 00209 return $outArray; 00210 } 00211 00212 /** 00213 * Adds a space character before and after HTML tags (more precisely any found < or >) 00214 * 00215 * @param string String to process 00216 * @param boolean If set, the < > searched for will be < and > 00217 * @return string Processed string 00218 * @access private 00219 */ 00220 function tagSpace($str, $rev = 0) { 00221 if ($rev) { 00222 return str_replace(' <', '<', str_replace('> ', '>', $str)); 00223 } else { 00224 return str_replace('<', ' <', str_replace('>', '> ', $str)); 00225 } 00226 } 00227 } 00228 00229 if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_diff.php'])) { 00230 include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_diff.php']); 00231 } 00232 ?>
1.8.0