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