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-10 23:21:55

Added
+9
lines
Removed
-4
lines
Context
190
unchanged
Blobs
from d0239a381229
to 7c3bc8553478
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,
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,
3637 fc.file_monitor_list_id, s.server_name
3738 FROM FILE_CHANGES fc LEFT JOIN SERVERS s ON s.server_id = fc.server_id
3839 WHERE fc.file_changes_id = $id LIMIT 1
3940 ~, __FILE__, __LINE__);
4041 if ($row && $row->{id}) {
4142 # Find the previous captured version of this file for the "before" pane.
4243 # Prefer same-monitor scope, but fall back to file-name-only for legacy
4344 # rows where file_monitor_list_id is NULL.
4445 my $q_fname = $dbh->quote($row->{file_name});
4546 my $scope = defined $row->{file_monitor_list_id}
4647 ? "file_monitor_list_id = $row->{file_monitor_list_id} AND "
4748 : '';
4849 my $prev = $db->db_readwrite($dbh, qq~
4950 SELECT blob_sha FROM FILE_CHANGES
5051 WHERE $scope
5152 file_name = $q_fname
5253 AND file_changes_id < $id
5354 AND blob_sha IS NOT NULL
5455 ORDER BY file_changes_id DESC LIMIT 1
5556 ~, __FILE__, __LINE__);
5657 $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>~;
5860 $blob_sha = $row->{blob_sha};
5961 $prev_sha = $prev && $prev->{blob_sha} ? $prev->{blob_sha} : '';
6062 $meta = { %$row };
6163 }
6264}
6365elsif ($kind eq 'schema' && $id) {
6466 my $row = $db->db_readwrite($dbh, qq~
6567 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
6771 FROM SCHEMA_CHANGE sc LEFT JOIN SERVERS s ON s.server_id = sc.server_id
6872 WHERE sc.schema_change_id = $id LIMIT 1
6973 ~, __FILE__, __LINE__);
7074 if ($row && $row->{id}) {
7175 my $q_db = $dbh->quote($row->{database_name});
7276 my $q_t = $dbh->quote($row->{table_name});
7377 my $prev = $db->db_readwrite($dbh, qq~
7478 SELECT schema_blob_sha FROM SCHEMA_CHANGE
7579 WHERE server_id = $row->{server_id}
7680 AND database_name = $q_db AND table_name = $q_t
7781 AND schema_change_id < $id
7882 AND schema_blob_sha IS NOT NULL
7983 ORDER BY schema_change_id DESC LIMIT 1
8084 ~, __FILE__, __LINE__);
8185 $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>~;
8388 $blob_sha = $row->{schema_blob_sha};
8489 $prev_sha = $prev && $prev->{schema_blob_sha} ? $prev->{schema_blob_sha} : '';
8590 $meta = { %$row };
8691 }
8792}
8893
8994# Fetch + decompress blobs
9095my $curr = $blob_sha ? _fetch_blob($db, $dbh, $blob_sha) : '';
9196my $prev = $prev_sha ? _fetch_blob($db, $dbh, $prev_sha) : '';
9297
9398$db->db_disconnect($dbh);
9499
95100# Compute a naive line-level diff
96101my @lines = _line_diff($prev // '', $curr // '');
97102my ($ct_add, $ct_del) = (0, 0);
98103foreach my $L (@lines) {
99104 $ct_add++ if $L->{op} eq 'add';
100105 $ct_del++ if $L->{op} eq 'del';
101106 # HTML-escape diff'd content -- files under monitor may themselves be
102107 # HTML/JS/CSS, and dumping raw content into an on-screen table renders
103108 # the tags live. Escape it so it displays as source.
104109 my $t = $L->{text}; $t = '' unless defined $t;
105110 $t =~ s/&/&amp;/g; $t =~ s/</&lt;/g; $t =~ s/>/&gt;/g;
106111 $L->{text} = $t;
107112}
108113
109114my $tvars = {
110115 title => $title || '(unknown)',
111116 subtitle => $subtitle,
112117 kind => $kind,
113118 id => $id,
114119 has_diff => scalar(@lines) ? 1 : 0,
115120 lines => \@lines,
116121 ct_add => $ct_add,
117122 ct_del => $ct_del,
118123 ct_context => scalar(@lines) - $ct_add - $ct_del,
119124 has_prev => $prev_sha ? 1 : 0,
120125 curr_sha => substr($blob_sha || '', 0, 12),
121126 prev_sha => substr($prev_sha || '', 0, 12),
122127};
123128
124129my @body = $tpl->template('diff.html', $tvars);
125130MODS::PageWrapper->new->wrapper(
126131 page_title => "Diff -- $title",
127132 page_key => ($kind eq 'schema' ? 'schema_changes' : 'file_changes'),
128133 body_html => join('', @body),
129134 userinfo => { display_name => 'Operator' },
130135);
131136
132137#---------------------------------------------------------------------
133138sub _fetch_blob {
134139 my ($db, $dbh, $sha) = @_;
135140 my $sth = $dbh->prepare("SELECT content_gz FROM BLOB_STORE WHERE blob_sha = ?");
136141 return '' unless $sth && $sth->execute($sha);
137142 my $row = $sth->fetchrow_arrayref;
138143 $sth->finish;
139144 return '' unless $row && $row->[0];
140145 my $decompressed = eval { uncompress($row->[0]) };
141146 return $decompressed // '';
142147}
143148
144149#---------------------------------------------------------------------
145150# _line_diff -- naive line-level diff. Returns an array of hashrefs:
146151# { op => 'add' | 'del' | 'ctx', text => $line, old_n, new_n }
147152# Uses a simple LCS-based approach; adequate for small-to-medium files.
148153# For huge diffs shell out to /usr/bin/diff (future optimization).
149154#---------------------------------------------------------------------
150155sub _line_diff {
151156 my ($old, $new) = @_;
152157 my @old_lines = split /\r?\n/, ($old // '');
153158 my @new_lines = split /\r?\n/, ($new // '');
154159
155160 # LCS length table (dynamic programming) -- capped to prevent
156161 # runaway memory on huge inputs.
157162 my $O = scalar @old_lines;
158163 my $N = scalar @new_lines;
159164 if ($O > 5000 || $N > 5000) {
160165 # Fall back: just dump both sides linearly, marked as del/add.
161166 my @out;
162167 my $i = 0;
163168 push @out, { op => 'del', text => $_, old_n => ++$i, new_n => '' } for @old_lines;
164169 my $j = 0;
165170 push @out, { op => 'add', text => $_, old_n => '', new_n => ++$j } for @new_lines;
166171 return @out;
167172 }
168173
169174 my @lcs; for my $i (0..$O) { $lcs[$i][0] = 0; }
170175 for my $j (0..$N) { $lcs[0][$j] = 0; }
171176 for my $i (1..$O) {
172177 for my $j (1..$N) {
173178 $lcs[$i][$j] = $old_lines[$i-1] eq $new_lines[$j-1]
174179 ? $lcs[$i-1][$j-1] + 1
175180 : ($lcs[$i-1][$j] >= $lcs[$i][$j-1] ? $lcs[$i-1][$j] : $lcs[$i][$j-1]);
176181 }
177182 }
178183
179184 my @out;
180185 my ($i, $j) = ($O, $N);
181186 while ($i > 0 || $j > 0) {
182187 if ($i > 0 && $j > 0 && $old_lines[$i-1] eq $new_lines[$j-1]) {
183188 unshift @out, { op => 'ctx', text => $old_lines[$i-1], old_n => $i, new_n => $j };
184189 $i--; $j--;
185190 } elsif ($j > 0 && ($i == 0 || $lcs[$i][$j-1] >= $lcs[$i-1][$j])) {
186191 unshift @out, { op => 'add', text => $new_lines[$j-1], old_n => '', new_n => $j };
187192 $j--;
188193 } else {
189194 unshift @out, { op => 'del', text => $old_lines[$i-1], old_n => $i, new_n => '' };
190195 $i--;
191196 }
192197 }
193198 return @out;
194199}