TYPO3 CMS  TYPO3_6-2
DiffUtility.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Core\Utility;
3 
22 class DiffUtility {
23 
24  // External, static
25  // If set, the HTML tags are stripped from the input strings first.
29  public $stripTags = 0;
30 
31  // Diff options. eg "--unified=3"
35  public $diffOptions = '';
36 
37  // Internal, dynamic
38  // This indicates the number of times the function addClearBuffer has been called - and used to detect the very first call...
42  public $clearBufferIdx = 0;
43 
47  public $differenceLgd = 0;
48 
58  public function makeDiffDisplay($str1, $str2, $wrapTag = 'span') {
59  if ($this->stripTags) {
60  $str1 = strip_tags($str1);
61  $str2 = strip_tags($str2);
62  } else {
63  $str1 = $this->tagSpace($str1);
64  $str2 = $this->tagSpace($str2);
65  }
66  $str1Lines = $this->explodeStringIntoWords($str1);
67  $str2Lines = $this->explodeStringIntoWords($str2);
68  $diffRes = $this->getDiff(implode(LF, $str1Lines) . LF, implode(LF, $str2Lines) . LF);
69  if (is_array($diffRes)) {
70  $c = 0;
71  $diffResArray = array();
72  $differenceStr = '';
73  foreach ($diffRes as $lValue) {
74  if ((int)$lValue) {
75  $c = (int)$lValue;
76  $diffResArray[$c]['changeInfo'] = $lValue;
77  }
78  if ($lValue[0] === '<') {
79  $differenceStr .= ($diffResArray[$c]['old'][] = substr($lValue, 2));
80  }
81  if ($lValue[0] === '>') {
82  $differenceStr .= ($diffResArray[$c]['new'][] = substr($lValue, 2));
83  }
84  }
85  $this->differenceLgd = strlen($differenceStr);
86  $outString = '';
87  $clearBuffer = '';
88  $str1LinesCount = count($str1Lines);
89  for ($a = -1; $a < $str1LinesCount; $a++) {
90  if (is_array($diffResArray[$a + 1])) {
91  // 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.
92  if (strstr($diffResArray[$a + 1]['changeInfo'], 'a')) {
93  $clearBuffer .= htmlspecialchars($str1Lines[$a]) . ' ';
94  }
95  $outString .= $this->addClearBuffer($clearBuffer);
96  $clearBuffer = '';
97  if (is_array($diffResArray[$a + 1]['old'])) {
98  $outString .= '<' . $wrapTag . ' class="diff-r">' . htmlspecialchars(implode(' ', $diffResArray[($a + 1)]['old'])) . '</' . $wrapTag . '> ';
99  }
100  if (is_array($diffResArray[$a + 1]['new'])) {
101  $outString .= '<' . $wrapTag . ' class="diff-g">' . htmlspecialchars(implode(' ', $diffResArray[($a + 1)]['new'])) . '</' . $wrapTag . '> ';
102  }
103  $chInfParts = explode(',', $diffResArray[$a + 1]['changeInfo']);
104  if ((string)$chInfParts[0] === (string)($a + 1)) {
105  $newLine = (int)$chInfParts[1] - 1;
106  if ($newLine > $a) {
107  $a = $newLine;
108  }
109  }
110  } else {
111  $clearBuffer .= htmlspecialchars($str1Lines[$a]) . ' ';
112  }
113  }
114  $outString .= $this->addClearBuffer($clearBuffer, 1);
115  $outString = str_replace(' ', LF, $outString);
116  if (!$this->stripTags) {
117  $outString = $this->tagSpace($outString, 1);
118  }
119  return $outString;
120  }
121  }
122 
133  public function getDiff($str1, $str2) {
134  // Create file 1 and write string
137  // Create file 2 and write string
140  // Perform diff.
141  $cmd = $GLOBALS['TYPO3_CONF_VARS']['BE']['diff_path'] . ' ' . $this->diffOptions . ' ' . $file1 . ' ' . $file2;
142  $res = array();
144  unlink($file1);
145  unlink($file2);
146  return $res;
147  }
148 
158  public function addClearBuffer($clearBuffer, $last = 0) {
159  if (strlen($clearBuffer) > 200) {
160  $clearBuffer = ($this->clearBufferIdx ? \TYPO3\CMS\Core\Utility\GeneralUtility::fixed_lgd_cs($clearBuffer, 70) : '') . '[' . strlen($clearBuffer) . ']' . (!$last ? \TYPO3\CMS\Core\Utility\GeneralUtility::fixed_lgd_cs($clearBuffer, -70) : '');
161  }
162  $this->clearBufferIdx++;
163  return $clearBuffer;
164  }
165 
175  public function explodeStringIntoWords($str) {
177  $outArray = array();
178  foreach ($strArr as $lineOfWords) {
179  $allWords = \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode(' ', $lineOfWords, TRUE);
180  $outArray[] = $allWords;
181  $outArray[] = array('');
182  $outArray[] = array('');
183  }
184  return call_user_func_array('array_merge', $outArray);
185  }
186 
196  public function tagSpace($str, $rev = 0) {
197  if ($rev) {
198  return str_replace(' &lt;', '&lt;', str_replace('&gt; ', '&gt;', $str));
199  } else {
200  return str_replace('<', ' <', str_replace('>', '> ', $str));
201  }
202  }
203 
204 }
static writeFile($file, $content, $changePermissions=FALSE)
makeDiffDisplay($str1, $str2, $wrapTag='span')
Definition: DiffUtility.php:58
static trimExplode($delim, $string, $removeEmptyValues=FALSE, $limit=0)
static tempnam($filePrefix, $fileSuffix='')
static fixed_lgd_cs($string, $chars, $appendString='...')
if(!defined('TYPO3_MODE')) $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_userauth.php']['logoff_pre_processing'][]
static exec($command, &$output=NULL, &$returnValue=0)
addClearBuffer($clearBuffer, $last=0)