[FEATURE] Allow .ts file extension for static typoscript templates
[Packages/TYPO3.CMS.git] / t3lib / class.t3lib_diff.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 1999-2011 Kasper Skårhøj (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 * Revised for TYPO3 3.6 November/2003 by Kasper Skårhøj
31 * XHTML Compliant
32 *
33 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
34 */
35
36 /**
37 * This class has functions which generates a difference output of a content string
38 *
39 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
40 * @package TYPO3
41 * @subpackage t3lib
42 */
43 class t3lib_diff {
44
45 // External, static
46 // If set, the HTML tags are stripped from the input strings first.
47 var $stripTags = 0;
48 // Diff options. eg "--unified=3"
49 var $diffOptions = '';
50
51 // Internal, dynamic
52 // This indicates the number of times the function addClearBuffer has been called - and used to detect the very first call...
53 var $clearBufferIdx = 0;
54 var $differenceLgd = 0;
55
56
57 /**
58 * This will produce a color-marked-up diff output in HTML from the input strings.
59 *
60 * @param string $str1 String 1
61 * @param string $str2 String 2
62 * @param string $wrapTag Setting the wrapping tag name
63 * @return string Formatted output.
64 */
65 function makeDiffDisplay($str1, $str2, $wrapTag = 'span') {
66 if ($this->stripTags) {
67 $str1 = strip_tags($str1);
68 $str2 = strip_tags($str2);
69 } else {
70 $str1 = $this->tagSpace($str1);
71 $str2 = $this->tagSpace($str2);
72 }
73 $str1Lines = $this->explodeStringIntoWords($str1);
74 $str2Lines = $this->explodeStringIntoWords($str2);
75
76 $diffRes = $this->getDiff(implode(LF, $str1Lines) . LF, implode(LF, $str2Lines) . LF);
77
78 if (is_array($diffRes)) {
79 $c = 0;
80 $diffResArray = array();
81 $differenceStr = '';
82 foreach ($diffRes as $lValue) {
83 if (intval($lValue)) {
84 $c = intval($lValue);
85 $diffResArray[$c]['changeInfo'] = $lValue;
86 }
87 if (substr($lValue, 0, 1) == '<') {
88 $differenceStr .= $diffResArray[$c]['old'][] = substr($lValue, 2);
89 }
90 if (substr($lValue, 0, 1) == '>') {
91 $differenceStr .= $diffResArray[$c]['new'][] = substr($lValue, 2);
92 }
93 }
94
95 $this->differenceLgd = strlen($differenceStr);
96
97 $outString = '';
98 $clearBuffer = '';
99 for ($a = -1; $a < count($str1Lines); $a++) {
100 if (is_array($diffResArray[$a + 1])) {
101 // 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.
102 if (strstr($diffResArray[$a + 1]['changeInfo'], 'a')) {
103 $clearBuffer .= htmlspecialchars($str1Lines[$a]) . ' ';
104 }
105
106 $outString .= $this->addClearBuffer($clearBuffer);
107 $clearBuffer = '';
108 if (is_array($diffResArray[$a + 1]['old'])) {
109 $outString .= '<' . $wrapTag . ' class="diff-r">' . htmlspecialchars(implode(' ', $diffResArray[$a + 1]['old'])) . '</' . $wrapTag . '> ';
110 }
111 if (is_array($diffResArray[$a + 1]['new'])) {
112 $outString .= '<' . $wrapTag . ' class="diff-g">' . htmlspecialchars(implode(' ', $diffResArray[$a + 1]['new'])) . '</' . $wrapTag . '> ';
113 }
114 $chInfParts = explode(',', $diffResArray[$a + 1]['changeInfo']);
115 if (!strcmp($chInfParts[0], $a + 1)) {
116 $newLine = intval($chInfParts[1]) - 1;
117 if ($newLine > $a) {
118 $a = $newLine;
119 } // Security that $a is not set lower than current for some reason...
120 }
121 } else {
122 $clearBuffer .= htmlspecialchars($str1Lines[$a]) . ' ';
123 }
124 }
125 $outString .= $this->addClearBuffer($clearBuffer, 1);
126
127 $outString = str_replace(' ', LF, $outString);
128 if (!$this->stripTags) {
129 $outString = $this->tagSpace($outString, 1);
130 }
131 return $outString;
132 }
133 }
134
135 /**
136 * Produce a diff (using the "diff" application) between two strings
137 * The function will write the two input strings to temporary files, then execute the diff program, delete the temp files and return the result.
138 *
139 * @param string $str1 String 1
140 * @param string $str2 String 2
141 * @return array The result from the exec() function call.
142 * @access private
143 */
144 function getDiff($str1, $str2) {
145 // Create file 1 and write string
146 $file1 = t3lib_div::tempnam('diff1_');
147 t3lib_div::writeFile($file1, $str1);
148 // Create file 2 and write string
149 $file2 = t3lib_div::tempnam('diff2_');
150 t3lib_div::writeFile($file2, $str2);
151 // Perform diff.
152 $cmd = $GLOBALS['TYPO3_CONF_VARS']['BE']['diff_path'] . ' ' . $this->diffOptions . ' ' . $file1 . ' ' . $file2;
153 $res = array();
154 t3lib_utility_Command::exec($cmd, $res);
155
156 unlink($file1);
157 unlink($file2);
158
159 return $res;
160 }
161
162 /**
163 * 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
164 *
165 * @param string $clearBuffer The input string.
166 * @param boolean $last If set, it indicates that the string should just end with ... (thus no "complete" ending)
167 * @return string Processed string.
168 * @access private
169 */
170 function addClearBuffer($clearBuffer, $last = 0) {
171 if (strlen($clearBuffer) > 200) {
172 $clearBuffer = ($this->clearBufferIdx ? t3lib_div::fixed_lgd_cs($clearBuffer, 70) : '') . '[' . strlen($clearBuffer) . ']' . (!$last ? t3lib_div::fixed_lgd_cs($clearBuffer, -70) : '');
173 }
174 $this->clearBufferIdx++;
175 return $clearBuffer;
176 }
177
178 /**
179 * Explodes the input string into words.
180 * 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.
181 *
182 * @param string $str The string input
183 * @return array Array with words.
184 * @access private
185 */
186 function explodeStringIntoWords($str) {
187 $strArr = t3lib_div::trimExplode(LF, $str);
188 $outArray = array();
189 foreach ($strArr as $lineOfWords) {
190 $allWords = t3lib_div::trimExplode(' ', $lineOfWords, 1);
191 $outArray = array_merge($outArray, $allWords);
192 $outArray[] = '';
193 $outArray[] = '';
194 }
195 return $outArray;
196 }
197
198 /**
199 * Adds a space character before and after HTML tags (more precisely any found < or >)
200 *
201 * @param string $str String to process
202 * @param boolean $rev If set, the < > searched for will be &lt; and &gt;
203 * @return string Processed string
204 * @access private
205 */
206 function tagSpace($str, $rev = 0) {
207 if ($rev) {
208 return str_replace(' &lt;', '&lt;', str_replace('&gt; ', '&gt;', $str));
209 } else {
210 return str_replace('<', ' <', str_replace('>', '> ', $str));
211 }
212 }
213 }
214
215 ?>