Subversion Repositories php_utils

Rev

Rev 5 | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. <?php
  2.  
  3. /*
  4.  * PHP diff functions
  5.  * Copyright 2012 Daniel Marschall, ViaThinkSoft
  6.  * Revision 2012-11-16
  7.  *
  8.  * Licensed under the Apache License, Version 2.0 (the "License");
  9.  * you may not use this file except in compliance with the License.
  10.  * You may obtain a copy of the License at
  11.  *
  12.  *     http://www.apache.org/licenses/LICENSE-2.0
  13.  *
  14.  * Unless required by applicable law or agreed to in writing, software
  15.  * distributed under the License is distributed on an "AS IS" BASIS,
  16.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17.  * See the License for the specific language governing permissions and
  18.  * limitations under the License.
  19.  */
  20.  
  21. define('TABS_WS', 6);
  22.  
  23. function output_unified_diff($fileA, $fileB, $num_lines=3) {
  24.         $fileA = realpath($fileA);
  25.         $fileB = realpath($fileB);
  26.  
  27.         ob_start();
  28.         system("diff -wbB --ignore-blank-lines -U ".escapeshellarg($num_lines)." ".escapeshellarg($fileA)." ".escapeshellarg($fileB));
  29.         $cont = ob_get_contents();
  30.         ob_end_clean();
  31.  
  32.         $ary = explode("\n", $cont);
  33.         foreach ($ary as $n => $a) {
  34.                 $c = substr($a, 0, 1);
  35.                 $c2 = substr($a, 0, 2);
  36.                 $c3 = substr($a, 0, 3);
  37.  
  38.                 echo '<code>';
  39.                 if (($c3 == '+++') || ($c3 == '---')) {
  40.                         echo '<b><font color="gray">'.html_format($a).'</font></b>';
  41.                 } else if ($c2 == '@@') {
  42.                         echo '<b><font color="blue">'.html_format($a).'</font></b>';
  43.                 } else if ($c == '+') {
  44.                         echo '<font color="green">'.html_format($a).'</font>';
  45.                 } else if ($c == '-') {
  46.                         echo '<font color="red">'.html_format($a).'</font>';
  47.                 } else {
  48.                         echo html_format($a);
  49.                 }
  50.                 echo "</code><br />\n";
  51.         }
  52. }
  53.  
  54. function output_diff($fileA, $fileB, $num_lines=3) {
  55.         $fileA = realpath($fileA);
  56.         $fileB = realpath($fileB);
  57.  
  58.         ob_start();
  59.         system("diff -wbB --ignore-blank-lines ".escapeshellarg($fileA)." ".escapeshellarg($fileB));
  60.         $cont = ob_get_contents();
  61.         ob_end_clean();
  62.  
  63.         $ary = explode("\n", $cont);
  64.         foreach ($ary as $n => $a) {
  65.                 $c = substr($a, 0, 1);
  66.  
  67.                 echo '<code>';
  68.                 if (($c == '>') || ($c == '<')) {
  69.                         echo '<b><font color="blue">'.html_format($c).'</font></b>'.html_format(substr($a, 1));
  70.                 } else {
  71.                         echo '<b><font color="blue">'.html_format($a).'</font></b>';
  72.                 }
  73.                 echo "</code><br />\n";
  74.         }
  75. }
  76.  
  77. function html_format($x) {
  78.         $x = htmlentities($x);
  79.         $x = str_replace("\t", str_repeat(' ', TABS_WS), $x);
  80.         $x = str_replace(' ', '&nbsp;', $x);
  81.         return $x;
  82. }
  83.