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-12 00:51:19

Added
+3
lines
Removed
-1
lines
Context
224
unchanged
Blobs
from 6bddacd3e8ab
to 902f256a1a7c
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;
1919use MODS::Diff;
2020use MODS::CompileCheck;
2121use JSON::PP ();
2222
2323my $cgi = CGI->new;
2424my $db = MODS::DBConnect->new;
2525my $tpl = MODS::Template->new;
26$|=1; print "Content-Type: text/html; charset=utf-8\nCache-Control: no-cache\n\n";
26$|=1;
2727
2828my $kind = $cgi->param('kind') || '';
2929my $id = int($cgi->param('id') || 0);
3030my $op = $cgi->param('op') || '';
3131
3232# ---- AJAX: compile check ------------------------------------------
3333if ($op eq 'compile' && $id) {
3434 my $dbh2 = $db->db_connect;
3535 my $r = $db->db_readwrite($dbh2, qq~
3636 SELECT fc.file_name, fc.blob_sha, bs.content_gz
3737 FROM FILE_CHANGES fc
3838 LEFT JOIN BLOB_STORE bs ON bs.blob_sha = fc.blob_sha
3939 WHERE fc.file_changes_id = $id LIMIT 1
4040 ~, __FILE__, __LINE__);
4141 $db->db_disconnect($dbh2);
4242 print "Content-Type: application/json\nCache-Control: no-cache\n\n";
4343 unless ($r && $r->{content_gz}) {
4444 print JSON::PP::encode_json({ status => 'unchecked', message => 'no blob' });
4545 exit;
4646 }
4747 my $content = eval { Compress::Zlib::uncompress($r->{content_gz}) };
4848 my $result = MODS::CompileCheck::check($content, $r->{file_name});
4949 print JSON::PP::encode_json($result);
5050 exit;
5151}
5252
53# HTML branch from here on
54print "Content-Type: text/html; charset=utf-8\nCache-Control: no-cache\n\n";
5355my $dbh = $db->db_connect or die "DB connect failed\n";
5456
5557my ($title, $subtitle, $blob_sha, $prev_sha, $meta) = ('', '', '', '', {});
5658
5759if ($kind eq 'file' && $id) {
5860 my $row = $db->db_readwrite($dbh, qq~
5961 SELECT fc.file_changes_id AS id, fc.file_name, fc.status, fc.blob_sha,
6062 fc.date_time, UNIX_TIMESTAMP(fc.date_time) AS ts_epoch,
6163 fc.uid, fc.gid, fc.permissions, fc.is_ts_only,
6264 fc.file_monitor_list_id, s.server_name
6365 FROM FILE_CHANGES fc LEFT JOIN SERVERS s ON s.server_id = fc.server_id
6466 WHERE fc.file_changes_id = $id LIMIT 1
6567 ~, __FILE__, __LINE__);
6668 if ($row && $row->{id}) {
6769 # Find the previous captured version of this file for the "before" pane.
6870 # Prefer same-monitor scope, but fall back to file-name-only for legacy
6971 # rows where file_monitor_list_id is NULL.
7072 my $q_fname = $dbh->quote($row->{file_name});
7173 my $scope = defined $row->{file_monitor_list_id}
7274 ? "file_monitor_list_id = $row->{file_monitor_list_id} AND "
7375 : '';
7476 my $prev = $db->db_readwrite($dbh, qq~
7577 SELECT blob_sha FROM FILE_CHANGES
7678 WHERE $scope
7779 file_name = $q_fname
7880 AND file_changes_id < $id
7981 AND blob_sha IS NOT NULL
8082 ORDER BY file_changes_id DESC LIMIT 1
8183 ~, __FILE__, __LINE__);
8284 $title = $row->{file_name};
8385 my $ep = int($row->{ts_epoch} || 0);
8486 $subtitle = qq~$row->{status} on $row->{server_name} at <span class="ts" data-ts="$ep" data-fmt="datetime">$row->{date_time}</span>~;
8587 $blob_sha = $row->{blob_sha};
8688 $prev_sha = $prev && $prev->{blob_sha} ? $prev->{blob_sha} : '';
8789 $meta = { %$row };
8890 }
8991}
9092elsif ($kind eq 'schema' && $id) {
9193 my $row = $db->db_readwrite($dbh, qq~
9294 SELECT sc.schema_change_id AS id, sc.database_name, sc.table_name, sc.changes,
9395 sc.schema_blob_sha, sc.change_datetime,
9496 UNIX_TIMESTAMP(sc.change_datetime) AS ts_epoch,
9597 s.server_name, sc.server_id
9698 FROM SCHEMA_CHANGE sc LEFT JOIN SERVERS s ON s.server_id = sc.server_id
9799 WHERE sc.schema_change_id = $id LIMIT 1
98100 ~, __FILE__, __LINE__);
99101 if ($row && $row->{id}) {
100102 my $q_db = $dbh->quote($row->{database_name});
101103 my $q_t = $dbh->quote($row->{table_name});
102104 my $prev = $db->db_readwrite($dbh, qq~
103105 SELECT schema_blob_sha FROM SCHEMA_CHANGE
104106 WHERE server_id = $row->{server_id}
105107 AND database_name = $q_db AND table_name = $q_t
106108 AND schema_change_id < $id
107109 AND schema_blob_sha IS NOT NULL
108110 ORDER BY schema_change_id DESC LIMIT 1
109111 ~, __FILE__, __LINE__);
110112 $title = "$row->{database_name}.$row->{table_name}";
111113 my $ep = int($row->{ts_epoch} || 0);
112114 $subtitle = qq~on $row->{server_name} at <span class="ts" data-ts="$ep" data-fmt="datetime">$row->{change_datetime}</span>~;
113115 $blob_sha = $row->{schema_blob_sha};
114116 $prev_sha = $prev && $prev->{schema_blob_sha} ? $prev->{schema_blob_sha} : '';
115117 $meta = { %$row };
116118 }
117119}
118120
119121# Fetch + decompress blobs
120122my $curr = $blob_sha ? _fetch_blob($db, $dbh, $blob_sha) : '';
121123my $prev = $prev_sha ? _fetch_blob($db, $dbh, $prev_sha) : '';
122124
123125$db->db_disconnect($dbh);
124126
125127# Compute a naive line-level diff
126128my @lines = MODS::Diff::lines($prev // '', $curr // '');
127129my ($ct_add, $ct_del) = (0, 0);
128130foreach my $L (@lines) {
129131 $ct_add++ if $L->{op} eq 'add';
130132 $ct_del++ if $L->{op} eq 'del';
131133 # HTML-escape diff'd content -- files under monitor may themselves be
132134 # HTML/JS/CSS, and dumping raw content into an on-screen table renders
133135 # the tags live. Escape it so it displays as source.
134136 my $t = $L->{text}; $t = '' unless defined $t;
135137 $t =~ s/&/&amp;/g; $t =~ s/</&lt;/g; $t =~ s/>/&gt;/g;
136138 $L->{text} = $t;
137139}
138140
139141my $tvars = {
140142 title => $title || '(unknown)',
141143 subtitle => $subtitle,
142144 kind => $kind,
143145 id => $id,
144146 has_diff => scalar(@lines) ? 1 : 0,
145147 lines => \@lines,
146148 ct_add => $ct_add,
147149 ct_del => $ct_del,
148150 ct_context => scalar(@lines) - $ct_add - $ct_del,
149151 has_prev => $prev_sha ? 1 : 0,
150152 curr_sha => substr($blob_sha || '', 0, 12),
151153 prev_sha => substr($prev_sha || '', 0, 12),
152154 restore_url => ($kind eq 'file' && $blob_sha) ? "/restore.cgi?id=$id" : '',
153155};
154156
155157my @body = $tpl->template('diff.html', $tvars);
156158MODS::PageWrapper->new->wrapper(
157159 page_title => "Diff -- $title",
158160 page_key => ($kind eq 'schema' ? 'schema_changes' : 'file_changes'),
159161 body_html => join('', @body),
160162 userinfo => { display_name => 'Operator' },
161163);
162164
163165#---------------------------------------------------------------------
164166sub _fetch_blob {
165167 my ($db, $dbh, $sha) = @_;
166168 my $sth = $dbh->prepare("SELECT content_gz FROM BLOB_STORE WHERE blob_sha = ?");
167169 return '' unless $sth && $sth->execute($sha);
168170 my $row = $sth->fetchrow_arrayref;
169171 $sth->finish;
170172 return '' unless $row && $row->[0];
171173 my $decompressed = eval { uncompress($row->[0]) };
172174 return $decompressed // '';
173175}
174176
175177#---------------------------------------------------------------------
176178# _line_diff -- naive line-level diff. Returns an array of hashrefs:
177179# { op => 'add' | 'del' | 'ctx', text => $line, old_n, new_n }
178180# Uses a simple LCS-based approach; adequate for small-to-medium files.
179181# For huge diffs shell out to /usr/bin/diff (future optimization).
180182#---------------------------------------------------------------------
181183sub _line_diff {
182184 my ($old, $new) = @_;
183185 my @old_lines = split /\r?\n/, ($old // '');
184186 my @new_lines = split /\r?\n/, ($new // '');
185187
186188 # LCS length table (dynamic programming) -- capped to prevent
187189 # runaway memory on huge inputs.
188190 my $O = scalar @old_lines;
189191 my $N = scalar @new_lines;
190192 if ($O > 5000 || $N > 5000) {
191193 # Fall back: just dump both sides linearly, marked as del/add.
192194 my @out;
193195 my $i = 0;
194196 push @out, { op => 'del', text => $_, old_n => ++$i, new_n => '' } for @old_lines;
195197 my $j = 0;
196198 push @out, { op => 'add', text => $_, old_n => '', new_n => ++$j } for @new_lines;
197199 return @out;
198200 }
199201
200202 my @lcs; for my $i (0..$O) { $lcs[$i][0] = 0; }
201203 for my $j (0..$N) { $lcs[0][$j] = 0; }
202204 for my $i (1..$O) {
203205 for my $j (1..$N) {
204206 $lcs[$i][$j] = $old_lines[$i-1] eq $new_lines[$j-1]
205207 ? $lcs[$i-1][$j-1] + 1
206208 : ($lcs[$i-1][$j] >= $lcs[$i][$j-1] ? $lcs[$i-1][$j] : $lcs[$i][$j-1]);
207209 }
208210 }
209211
210212 my @out;
211213 my ($i, $j) = ($O, $N);
212214 while ($i > 0 || $j > 0) {
213215 if ($i > 0 && $j > 0 && $old_lines[$i-1] eq $new_lines[$j-1]) {
214216 unshift @out, { op => 'ctx', text => $old_lines[$i-1], old_n => $i, new_n => $j };
215217 $i--; $j--;
216218 } elsif ($j > 0 && ($i == 0 || $lcs[$i][$j-1] >= $lcs[$i-1][$j])) {
217219 unshift @out, { op => 'add', text => $new_lines[$j-1], old_n => '', new_n => $j };
218220 $j--;
219221 } else {
220222 unshift @out, { op => 'del', text => $old_lines[$i-1], old_n => $i, new_n => '' };
221223 $i--;
222224 }
223225 }
224226 return @out;
225227}