Diff -- /var/www/vhosts/3dshawn.com/site1/diff.cgi
Diff
/var/www/vhosts/3dshawn.com/site1/diff.cgi
modified on local at 2026-07-10 23:21:55
Added
+9
lines
Removed
-4
lines
Context
190
unchanged
Blobs
from d0239a381229
to 7c3bc8553478
to 7c3bc8553478
Unified diff
Naive LCS-based line diff. Additions in emerald, deletions in rose. Both sides decompressed live from BLOB_STORE.| 1 | 1 | #!/usr/bin/perl |
| 2 | 2 | #====================================================================== |
| 3 | 3 | # DriftSense -- /diff.cgi |
| 4 | 4 | # |
| 5 | 5 | # Renders a two-pane diff for a single change captured in FILE_CHANGES |
| 6 | 6 | # or SCHEMA_CHANGE. Pulls both blobs from BLOB_STORE, decompresses, |
| 7 | 7 | # runs a naive LCS-style line diff, marks added / removed / context, |
| 8 | 8 | # renders as syntax-highlight-ish HTML with monospace typography. |
| 9 | 9 | # |
| 10 | 10 | # URL: |
| 11 | 11 | # /diff.cgi?kind=file&id=N -- FILE_CHANGES row |
| 12 | 12 | # /diff.cgi?kind=schema&id=N -- SCHEMA_CHANGE row |
| 13 | 13 | #====================================================================== |
| 14 | 14 | use strict; |
| 15 | 15 | use warnings; |
| 16 | 16 | use CGI (); |
| 17 | 17 | use Compress::Zlib qw(uncompress); |
| 18 | 18 | use MODS::Config; use MODS::DBConnect; use MODS::Template; use MODS::PageWrapper; |
| 19 | 19 | |
| 20 | 20 | my $cgi = CGI->new; |
| 21 | 21 | my $db = MODS::DBConnect->new; |
| 22 | 22 | my $tpl = MODS::Template->new; |
| 23 | 23 | $|=1; print "Content-Type: text/html; charset=utf-8\nCache-Control: no-cache\n\n"; |
| 24 | 24 | |
| 25 | 25 | my $kind = $cgi->param('kind') || ''; |
| 26 | 26 | my $id = int($cgi->param('id') || 0); |
| 27 | 27 | |
| 28 | 28 | my $dbh = $db->db_connect or die "DB connect failed\n"; |
| 29 | 29 | |
| 30 | 30 | my ($title, $subtitle, $blob_sha, $prev_sha, $meta) = ('', '', '', '', {}); |
| 31 | 31 | |
| 32 | 32 | if ($kind eq 'file' && $id) { |
| 33 | 33 | my $row = $db->db_readwrite($dbh, qq~ |
| 34 | 34 | SELECT fc.file_changes_id AS id, fc.file_name, fc.status, fc.blob_sha, |
| 35 | fc.date_time, fc.uid, fc.gid, fc.permissions, fc.is_ts_only, | |
| 35 | fc.date_time, UNIX_TIMESTAMP(fc.date_time) AS ts_epoch, | |
| 36 | fc.uid, fc.gid, fc.permissions, fc.is_ts_only, | |
| 36 | 37 | fc.file_monitor_list_id, s.server_name |
| 37 | 38 | FROM FILE_CHANGES fc LEFT JOIN SERVERS s ON s.server_id = fc.server_id |
| 38 | 39 | WHERE fc.file_changes_id = $id LIMIT 1 |
| 39 | 40 | ~, __FILE__, __LINE__); |
| 40 | 41 | if ($row && $row->{id}) { |
| 41 | 42 | # Find the previous captured version of this file for the "before" pane. |
| 42 | 43 | # Prefer same-monitor scope, but fall back to file-name-only for legacy |
| 43 | 44 | # rows where file_monitor_list_id is NULL. |
| 44 | 45 | my $q_fname = $dbh->quote($row->{file_name}); |
| 45 | 46 | my $scope = defined $row->{file_monitor_list_id} |
| 46 | 47 | ? "file_monitor_list_id = $row->{file_monitor_list_id} AND " |
| 47 | 48 | : ''; |
| 48 | 49 | my $prev = $db->db_readwrite($dbh, qq~ |
| 49 | 50 | SELECT blob_sha FROM FILE_CHANGES |
| 50 | 51 | WHERE $scope |
| 51 | 52 | file_name = $q_fname |
| 52 | 53 | AND file_changes_id < $id |
| 53 | 54 | AND blob_sha IS NOT NULL |
| 54 | 55 | ORDER BY file_changes_id DESC LIMIT 1 |
| 55 | 56 | ~, __FILE__, __LINE__); |
| 56 | 57 | $title = $row->{file_name}; |
| 57 | $subtitle = "$row->{status} on $row->{server_name} at $row->{date_time}"; | |
| 58 | my $ep = int($row->{ts_epoch} || 0); | |
| 59 | $subtitle = qq~$row->{status} on $row->{server_name} at <span class="ts" data-ts="$ep" data-fmt="datetime">$row->{date_time}</span>~; | |
| 58 | 60 | $blob_sha = $row->{blob_sha}; |
| 59 | 61 | $prev_sha = $prev && $prev->{blob_sha} ? $prev->{blob_sha} : ''; |
| 60 | 62 | $meta = { %$row }; |
| 61 | 63 | } |
| 62 | 64 | } |
| 63 | 65 | elsif ($kind eq 'schema' && $id) { |
| 64 | 66 | my $row = $db->db_readwrite($dbh, qq~ |
| 65 | 67 | SELECT sc.schema_change_id AS id, sc.database_name, sc.table_name, sc.changes, |
| 66 | sc.schema_blob_sha, sc.change_datetime, s.server_name, sc.server_id | |
| 68 | sc.schema_blob_sha, sc.change_datetime, | |
| 69 | UNIX_TIMESTAMP(sc.change_datetime) AS ts_epoch, | |
| 70 | s.server_name, sc.server_id | |
| 67 | 71 | FROM SCHEMA_CHANGE sc LEFT JOIN SERVERS s ON s.server_id = sc.server_id |
| 68 | 72 | WHERE sc.schema_change_id = $id LIMIT 1 |
| 69 | 73 | ~, __FILE__, __LINE__); |
| 70 | 74 | if ($row && $row->{id}) { |
| 71 | 75 | my $q_db = $dbh->quote($row->{database_name}); |
| 72 | 76 | my $q_t = $dbh->quote($row->{table_name}); |
| 73 | 77 | my $prev = $db->db_readwrite($dbh, qq~ |
| 74 | 78 | SELECT schema_blob_sha FROM SCHEMA_CHANGE |
| 75 | 79 | WHERE server_id = $row->{server_id} |
| 76 | 80 | AND database_name = $q_db AND table_name = $q_t |
| 77 | 81 | AND schema_change_id < $id |
| 78 | 82 | AND schema_blob_sha IS NOT NULL |
| 79 | 83 | ORDER BY schema_change_id DESC LIMIT 1 |
| 80 | 84 | ~, __FILE__, __LINE__); |
| 81 | 85 | $title = "$row->{database_name}.$row->{table_name}"; |
| 82 | $subtitle = "on $row->{server_name} at $row->{change_datetime}"; | |
| 86 | my $ep = int($row->{ts_epoch} || 0); | |
| 87 | $subtitle = qq~on $row->{server_name} at <span class="ts" data-ts="$ep" data-fmt="datetime">$row->{change_datetime}</span>~; | |
| 83 | 88 | $blob_sha = $row->{schema_blob_sha}; |
| 84 | 89 | $prev_sha = $prev && $prev->{schema_blob_sha} ? $prev->{schema_blob_sha} : ''; |
| 85 | 90 | $meta = { %$row }; |
| 86 | 91 | } |
| 87 | 92 | } |
| 88 | 93 | |
| 89 | 94 | # Fetch + decompress blobs |
| 90 | 95 | my $curr = $blob_sha ? _fetch_blob($db, $dbh, $blob_sha) : ''; |
| 91 | 96 | my $prev = $prev_sha ? _fetch_blob($db, $dbh, $prev_sha) : ''; |
| 92 | 97 | |
| 93 | 98 | $db->db_disconnect($dbh); |
| 94 | 99 | |
| 95 | 100 | # Compute a naive line-level diff |
| 96 | 101 | my @lines = _line_diff($prev // '', $curr // ''); |
| 97 | 102 | my ($ct_add, $ct_del) = (0, 0); |
| 98 | 103 | foreach my $L (@lines) { |
| 99 | 104 | $ct_add++ if $L->{op} eq 'add'; |
| 100 | 105 | $ct_del++ if $L->{op} eq 'del'; |
| 101 | 106 | # HTML-escape diff'd content -- files under monitor may themselves be |
| 102 | 107 | # HTML/JS/CSS, and dumping raw content into an on-screen table renders |
| 103 | 108 | # the tags live. Escape it so it displays as source. |
| 104 | 109 | my $t = $L->{text}; $t = '' unless defined $t; |
| 105 | 110 | $t =~ s/&/&/g; $t =~ s/</</g; $t =~ s/>/>/g; |
| 106 | 111 | $L->{text} = $t; |
| 107 | 112 | } |
| 108 | 113 | |
| 109 | 114 | my $tvars = { |
| 110 | 115 | title => $title || '(unknown)', |
| 111 | 116 | subtitle => $subtitle, |
| 112 | 117 | kind => $kind, |
| 113 | 118 | id => $id, |
| 114 | 119 | has_diff => scalar(@lines) ? 1 : 0, |
| 115 | 120 | lines => \@lines, |
| 116 | 121 | ct_add => $ct_add, |
| 117 | 122 | ct_del => $ct_del, |
| 118 | 123 | ct_context => scalar(@lines) - $ct_add - $ct_del, |
| 119 | 124 | has_prev => $prev_sha ? 1 : 0, |
| 120 | 125 | curr_sha => substr($blob_sha || '', 0, 12), |
| 121 | 126 | prev_sha => substr($prev_sha || '', 0, 12), |
| 122 | 127 | }; |
| 123 | 128 | |
| 124 | 129 | my @body = $tpl->template('diff.html', $tvars); |
| 125 | 130 | MODS::PageWrapper->new->wrapper( |
| 126 | 131 | page_title => "Diff -- $title", |
| 127 | 132 | page_key => ($kind eq 'schema' ? 'schema_changes' : 'file_changes'), |
| 128 | 133 | body_html => join('', @body), |
| 129 | 134 | userinfo => { display_name => 'Operator' }, |
| 130 | 135 | ); |
| 131 | 136 | |
| 132 | 137 | #--------------------------------------------------------------------- |
| 133 | 138 | sub _fetch_blob { |
| 134 | 139 | my ($db, $dbh, $sha) = @_; |
| 135 | 140 | my $sth = $dbh->prepare("SELECT content_gz FROM BLOB_STORE WHERE blob_sha = ?"); |
| 136 | 141 | return '' unless $sth && $sth->execute($sha); |
| 137 | 142 | my $row = $sth->fetchrow_arrayref; |
| 138 | 143 | $sth->finish; |
| 139 | 144 | return '' unless $row && $row->[0]; |
| 140 | 145 | my $decompressed = eval { uncompress($row->[0]) }; |
| 141 | 146 | return $decompressed // ''; |
| 142 | 147 | } |
| 143 | 148 | |
| 144 | 149 | #--------------------------------------------------------------------- |
| 145 | 150 | # _line_diff -- naive line-level diff. Returns an array of hashrefs: |
| 146 | 151 | # { op => 'add' | 'del' | 'ctx', text => $line, old_n, new_n } |
| 147 | 152 | # Uses a simple LCS-based approach; adequate for small-to-medium files. |
| 148 | 153 | # For huge diffs shell out to /usr/bin/diff (future optimization). |
| 149 | 154 | #--------------------------------------------------------------------- |
| 150 | 155 | sub _line_diff { |
| 151 | 156 | my ($old, $new) = @_; |
| 152 | 157 | my @old_lines = split /\r?\n/, ($old // ''); |
| 153 | 158 | my @new_lines = split /\r?\n/, ($new // ''); |
| 154 | 159 | |
| 155 | 160 | # LCS length table (dynamic programming) -- capped to prevent |
| 156 | 161 | # runaway memory on huge inputs. |
| 157 | 162 | my $O = scalar @old_lines; |
| 158 | 163 | my $N = scalar @new_lines; |
| 159 | 164 | if ($O > 5000 || $N > 5000) { |
| 160 | 165 | # Fall back: just dump both sides linearly, marked as del/add. |
| 161 | 166 | my @out; |
| 162 | 167 | my $i = 0; |
| 163 | 168 | push @out, { op => 'del', text => $_, old_n => ++$i, new_n => '' } for @old_lines; |
| 164 | 169 | my $j = 0; |
| 165 | 170 | push @out, { op => 'add', text => $_, old_n => '', new_n => ++$j } for @new_lines; |
| 166 | 171 | return @out; |
| 167 | 172 | } |
| 168 | 173 | |
| 169 | 174 | my @lcs; for my $i (0..$O) { $lcs[$i][0] = 0; } |
| 170 | 175 | for my $j (0..$N) { $lcs[0][$j] = 0; } |
| 171 | 176 | for my $i (1..$O) { |
| 172 | 177 | for my $j (1..$N) { |
| 173 | 178 | $lcs[$i][$j] = $old_lines[$i-1] eq $new_lines[$j-1] |
| 174 | 179 | ? $lcs[$i-1][$j-1] + 1 |
| 175 | 180 | : ($lcs[$i-1][$j] >= $lcs[$i][$j-1] ? $lcs[$i-1][$j] : $lcs[$i][$j-1]); |
| 176 | 181 | } |
| 177 | 182 | } |
| 178 | 183 | |
| 179 | 184 | my @out; |
| 180 | 185 | my ($i, $j) = ($O, $N); |
| 181 | 186 | while ($i > 0 || $j > 0) { |
| 182 | 187 | if ($i > 0 && $j > 0 && $old_lines[$i-1] eq $new_lines[$j-1]) { |
| 183 | 188 | unshift @out, { op => 'ctx', text => $old_lines[$i-1], old_n => $i, new_n => $j }; |
| 184 | 189 | $i--; $j--; |
| 185 | 190 | } elsif ($j > 0 && ($i == 0 || $lcs[$i][$j-1] >= $lcs[$i-1][$j])) { |
| 186 | 191 | unshift @out, { op => 'add', text => $new_lines[$j-1], old_n => '', new_n => $j }; |
| 187 | 192 | $j--; |
| 188 | 193 | } else { |
| 189 | 194 | unshift @out, { op => 'del', text => $old_lines[$i-1], old_n => $i, new_n => '' }; |
| 190 | 195 | $i--; |
| 191 | 196 | } |
| 192 | 197 | } |
| 193 | 198 | return @out; |
| 194 | 199 | } |