|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * @author Jason Bradley |
| 4 | + * |
| 5 | + * Creates a diff of two files from large files |
| 6 | + */ |
| 7 | +class SpeedyDiff |
| 8 | +{ |
| 9 | + protected $file1; |
| 10 | + protected $file2; |
| 11 | + |
| 12 | + protected $diff_file = ''; |
| 13 | + protected $has_difference = false; |
| 14 | + |
| 15 | + public function __construct($file1, $file2)) |
| 16 | + { |
| 17 | + if (!is_file($file1) || !is_file($file2)) |
| 18 | + { |
| 19 | + throw new Exception("Provide two files."); |
| 20 | + } |
| 21 | + |
| 22 | + |
| 23 | + $this->file1 = $file1; |
| 24 | + $this->file2 = $file2; |
| 25 | + |
| 26 | + $this->runDiff(); |
| 27 | + } |
| 28 | + |
| 29 | + protected function createDiffFile($tmp_file) |
| 30 | + { |
| 31 | + if (!is_file($tmp_file)) |
| 32 | + { |
| 33 | + throw new Exception("No diff file provided."); |
| 34 | + } |
| 35 | + |
| 36 | + $this->diff_file = tempnam('/tmp', 'speeddifffinal'); |
| 37 | + |
| 38 | + $tmp_diff_handle = fopen($tmp_file, 'r'); |
| 39 | + $diff_file_handle = fopen($this->diff_file, 'a'); |
| 40 | + |
| 41 | + while (($line = fgets($tmp_diff_handle)) !== false) |
| 42 | + { |
| 43 | + $first_char = substr($line, 0, 1); |
| 44 | + |
| 45 | + if ($first_char == '>') |
| 46 | + { |
| 47 | + fwrite($diff_file_handle, substr($line, 2)); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + fclose($tmp_diff_handle); |
| 52 | + fclose($diff_file_handle); |
| 53 | + |
| 54 | + //remove the tmp file |
| 55 | + unlink($tmp_file); |
| 56 | + |
| 57 | + if (!is_file($this->diff_file)) |
| 58 | + { |
| 59 | + throw new Exception("There was a problem writing the new diff file."); |
| 60 | + } |
| 61 | + else if (file_get_contents($this->diff_file) == '') |
| 62 | + { |
| 63 | + throw new Exception("Nothing was written to the new diff file."); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + protected function runDiff() |
| 68 | + { |
| 69 | + $tmp_file = tempnam('/tmp', 'speeddiff'); |
| 70 | + $cmd = "diff {$this->file1} {$this->file2} --speed-large-files -b > $tmp_file"; |
| 71 | + exec($cmd, $output, $return_var); |
| 72 | + |
| 73 | + unset($output); |
| 74 | + |
| 75 | + if ($return_var > 1) |
| 76 | + { |
| 77 | + unlink($tmp_file); |
| 78 | + throw new Exception("There was an error when calling $cmd"); |
| 79 | + } |
| 80 | + else if (!is_file($tmp_file)) |
| 81 | + { |
| 82 | + throw new Exception("Unable to write to tmp file, $tmp_file"); |
| 83 | + } |
| 84 | + else if (file_get_contents($tmp_file) == '') |
| 85 | + { |
| 86 | + return; |
| 87 | + } |
| 88 | + |
| 89 | + $this->has_difference = true; |
| 90 | + |
| 91 | + $this->createDiffFile($tmp_file); |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Get the file created from the diff |
| 96 | + * |
| 97 | + * @return String|Boolean |
| 98 | + */ |
| 99 | + public function getDiffFile() |
| 100 | + { |
| 101 | + return (is_file($this->diff_file)) ? $this->diff_file : false; |
| 102 | + } |
| 103 | + |
| 104 | + public function getHasDifference() |
| 105 | + { |
| 106 | + return $this->has_difference; |
| 107 | + } |
| 108 | +} |
0 commit comments