Diff -- /var/www/vhosts/3dshawn.com/site1/diff.cgi

O Operator
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
Restore this content →
Unified diff
Naive LCS-based line diff. Additions in emerald, deletions in rose. Both sides decompressed live from BLOB_STORE.
11#!/usr/bin/perl
22#======================================================================
33# DriftSense -- /diff.cgi
44#
55# Renders a two-pane diff for a single change captured in FILE_CHANGES
66# or SCHEMA_CHANGE. Pulls both blobs from BLOB_STORE, decompresses,
77# runs a naive LCS-style line diff, marks added / removed / context,
88# renders as syntax-highlight-ish HTML with monospace typography.
99#
1010# URL:
1111# /diff.cgi?kind=file&id=N -- FILE_CHANGES row
1212# /diff.cgi?kind=schema&id=N -- SCHEMA_CHANGE row
1313#======================================================================
1414use strict;
1515use warnings;
1616use CGI ();
1717use Compress::Zlib qw(uncompress);
1818use MODS::Config; use MODS::DBConnect; use MODS::Template; use MODS::PageWrapper;
1919
2020my $cgi = CGI->new;
2121my $db = MODS::DBConnect->new;
2222my $tpl = MODS::Template->new;
2323$|=1; print "Content-Type: text/html; charset=utf-8\nCache-Control: no-cache\n\n";
2424
2525my $kind = $cgi->param('kind') || '';
2626my $id = int($cgi->param('id') || 0);
2727
2828my $dbh = $db->db_connect or die "DB connect failed\n";
2929
3030my ($title, $subtitle, $blob_sha, $prev_sha, $meta) = ('', '', '', '', {});
3131
3232if ($kind eq 'file' && $id) {
3333 my $row = $db->db_readwrite($dbh, qq~
3434 SELECT fc.file_changes_id AS id, fc.file_name, fc.status, fc.blob_sha,
3535 fc.date_time, UNIX_TIMESTAMP(fc.date_time) AS ts_epoch,
3636 fc.uid, fc.gid, fc.permissions, fc.is_ts_only,
3737 fc.file_monitor_list_id, s.server_name
3838 FROM FILE_CHANGES fc LEFT JOIN SERVERS s ON s.server_id = fc.server_id
3939 WHERE fc.file_changes_id = $id LIMIT 1
4040 ~, __FILE__, __LINE__);
4141 if ($row && $row->{id}) {
4242 # Find the previous captured version of this file for the "before" pane.
4343 # Prefer same-monitor scope, but fall back to file-name-only for legacy
4444 # rows where file_monitor_list_id is NULL.
4545 my $q_fname = $dbh->quote($row->{file_name});
4646 my $scope = defined $row->{file_monitor_list_id}
4747 ? "file_monitor_list_id = $row->{file_monitor_list_id} AND "
4848 : '';
4949 my $prev = $db->db_readwrite($dbh, qq~
5050 SELECT blob_sha FROM FILE_CHANGES
5151 WHERE $scope
5252 file_name = $q_fname
5353 AND file_changes_id < $id
5454 AND blob_sha IS NOT NULL
5555 ORDER BY file_changes_id DESC LIMIT 1
5656 ~, __FILE__, __LINE__);
5757 $title = $row->{file_name};
5858 my $ep = int($row->{ts_epoch} || 0);
5959 $subtitle = qq~$row->{status} on $row->{server_name} at <span class="ts" data-ts="$ep" data-fmt="datetime">$row->{date_time}</span>~;
6060 $blob_sha = $row->{blob_sha};
6161 $prev_sha = $prev && $prev->{blob_sha} ? $prev->{blob_sha} : '';
6262 $meta = { %$row };
6363 }
6464}
6565elsif ($kind eq 'schema' && $id) {
6666 my $row = $db->db_readwrite($dbh, qq~
6767 SELECT sc.schema_change_id AS id, sc.database_name, sc.table_name, sc.changes,
6868 sc.schema_blob_sha, sc.change_datetime,
6969 UNIX_TIMESTAMP(sc.change_datetime) AS ts_epoch,
7070 s.server_name, sc.server_id
7171 FROM SCHEMA_CHANGE sc LEFT JOIN SERVERS s ON s.server_id = sc.server_id
7272 WHERE sc.schema_change_id = $id LIMIT 1
7373 ~, __FILE__, __LINE__);
7474 if ($row && $row->{id}) {
7575 my $q_db = $dbh->quote($row->{database_name});
7676 my $q_t = $dbh->quote($row->{table_name});
7777 my $prev = $db->db_readwrite($dbh, qq~
7878 SELECT schema_blob_sha FROM SCHEMA_CHANGE
7979 WHERE server_id = $row->{server_id}
8080 AND database_name = $q_db AND table_name = $q_t
8181 AND schema_change_id < $id
8282 AND schema_blob_sha IS NOT NULL
8383 ORDER BY schema_change_id DESC LIMIT 1
8484 ~, __FILE__, __LINE__);
8585 $title = "$row->{database_name}.$row->{table_name}";
8686 my $ep = int($row->{ts_epoch} || 0);
8787 $subtitle = qq~on $row->{server_name} at <span class="ts" data-ts="$ep" data-fmt="datetime">$row->{change_datetime}</span>~;
8888 $blob_sha = $row->{schema_blob_sha};
8989 $prev_sha = $prev && $prev->{schema_blob_sha} ? $prev->{schema_blob_sha} : '';
9090 $meta = { %$row };
9191 }
9292}
9393
9494# Fetch + decompress blobs
9595my $curr = $blob_sha ? _fetch_blob($db, $dbh, $blob_sha) : '';
9696my $prev = $prev_sha ? _fetch_blob($db, $dbh, $prev_sha) : '';
9797
9898$db->db_disconnect($dbh);
9999
100100# Compute a naive line-level diff
101101my @lines = _line_diff($prev // '', $curr // '');
102102my ($ct_add, $ct_del) = (0, 0);
103103foreach my $L (@lines) {
104104 $ct_add++ if $L->{op} eq 'add';
105105 $ct_del++ if $L->{op} eq 'del';
106106 # HTML-escape diff'd content -- files under monitor may themselves be
107107 # HTML/JS/CSS, and dumping raw content into an on-screen table renders
108108 # the tags live. Escape it so it displays as source.
109109 my $t = $L->{text}; $t = '' unless defined $t;
110110 $t =~ s/&/&amp;/g; $t =~ s/</&lt;/g; $t =~ s/>/&gt;/g;
111111 $L->{text} = $t;
112112}
113113
114114my $tvars = {
115115 title => $title || '(unknown)',
116116 subtitle => $subtitle,
117117 kind => $kind,
118118 id => $id,
119119 has_diff => scalar(@lines) ? 1 : 0,
120120 lines => \@lines,
121121 ct_add => $ct_add,
122122 ct_del => $ct_del,
123123 ct_context => scalar(@lines) - $ct_add - $ct_del,
124124 has_prev => $prev_sha ? 1 : 0,
125125 curr_sha => substr($blob_sha || '', 0, 12),
126126 prev_sha => substr($prev_sha || '', 0, 12),
127 restore_url => ($kind eq 'file' && $blob_sha) ? "/restore.cgi?id=$id" : '',
127128};
128129
129130my @body = $tpl->template('diff.html', $tvars);
130131MODS::PageWrapper->new->wrapper(
131132 page_title => "Diff -- $title",
132133 page_key => ($kind eq 'schema' ? 'schema_changes' : 'file_changes'),
133134 body_html => join('', @body),
134135 userinfo => { display_name => 'Operator' },
135136);
136137
137138#---------------------------------------------------------------------
138139sub _fetch_blob {
139140 my ($db, $dbh, $sha) = @_;
140141 my $sth = $dbh->prepare("SELECT content_gz FROM BLOB_STORE WHERE blob_sha = ?");
141142 return '' unless $sth && $sth->execute($sha);
142143 my $row = $sth->fetchrow_arrayref;
143144 $sth->finish;
144145 return '' unless $row && $row->[0];
145146 my $decompressed = eval { uncompress($row->[0]) };
146147 return $decompressed // '';
147148}
148149
149150#---------------------------------------------------------------------
150151# _line_diff -- naive line-level diff. Returns an array of hashrefs:
151152# { op => 'add' | 'del' | 'ctx', text => $line, old_n, new_n }
152153# Uses a simple LCS-based approach; adequate for small-to-medium files.
153154# For huge diffs shell out to /usr/bin/diff (future optimization).
154155#---------------------------------------------------------------------
155156sub _line_diff {
156157 my ($old, $new) = @_;
157158 my @old_lines = split /\r?\n/, ($old // '');
158159 my @new_lines = split /\r?\n/, ($new // '');
159160
160161 # LCS length table (dynamic programming) -- capped to prevent
161162 # runaway memory on huge inputs.
162163 my $O = scalar @old_lines;
163164 my $N = scalar @new_lines;
164165 if ($O > 5000 || $N > 5000) {
165166 # Fall back: just dump both sides linearly, marked as del/add.
166167 my @out;
167168 my $i = 0;
168169 push @out, { op => 'del', text => $_, old_n => ++$i, new_n => '' } for @old_lines;
169170 my $j = 0;
170171 push @out, { op => 'add', text => $_, old_n => '', new_n => ++$j } for @new_lines;
171172 return @out;
172173 }
173174
174175 my @lcs; for my $i (0..$O) { $lcs[$i][0] = 0; }
175176 for my $j (0..$N) { $lcs[0][$j] = 0; }
176177 for my $i (1..$O) {
177178 for my $j (1..$N) {
178179 $lcs[$i][$j] = $old_lines[$i-1] eq $new_lines[$j-1]
179180 ? $lcs[$i-1][$j-1] + 1
180181 : ($lcs[$i-1][$j] >= $lcs[$i][$j-1] ? $lcs[$i-1][$j] : $lcs[$i][$j-1]);
181182 }
182183 }
183184
184185 my @out;
185186 my ($i, $j) = ($O, $N);
186187 while ($i > 0 || $j > 0) {
187188 if ($i > 0 && $j > 0 && $old_lines[$i-1] eq $new_lines[$j-1]) {
188189 unshift @out, { op => 'ctx', text => $old_lines[$i-1], old_n => $i, new_n => $j };
189190 $i--; $j--;
190191 } elsif ($j > 0 && ($i == 0 || $lcs[$i][$j-1] >= $lcs[$i-1][$j])) {
191192 unshift @out, { op => 'add', text => $new_lines[$j-1], old_n => '', new_n => $j };
192193 $j--;
193194 } else {
194195 unshift @out, { op => 'del', text => $old_lines[$i-1], old_n => $i, new_n => '' };
195196 $i--;
196197 }
197198 }
198199 return @out;
199200}