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:48:51

Added
+24
lines
Removed
-0
lines
Context
201
unchanged
Blobs
from 7d22f9eb85f4
to 6bddacd3e8ab
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;
20use MODS::CompileCheck;
21use JSON::PP ();
2022
2123my $cgi = CGI->new;
2224my $db = MODS::DBConnect->new;
2325my $tpl = MODS::Template->new;
2426$|=1; print "Content-Type: text/html; charset=utf-8\nCache-Control: no-cache\n\n";
2527
2628my $kind = $cgi->param('kind') || '';
2729my $id = int($cgi->param('id') || 0);
30my $op = $cgi->param('op') || '';
31
32# ---- AJAX: compile check ------------------------------------------
33if ($op eq 'compile' && $id) {
34 my $dbh2 = $db->db_connect;
35 my $r = $db->db_readwrite($dbh2, qq~
36 SELECT fc.file_name, fc.blob_sha, bs.content_gz
37 FROM FILE_CHANGES fc
38 LEFT JOIN BLOB_STORE bs ON bs.blob_sha = fc.blob_sha
39 WHERE fc.file_changes_id = $id LIMIT 1
40 ~, __FILE__, __LINE__);
41 $db->db_disconnect($dbh2);
42 print "Content-Type: application/json\nCache-Control: no-cache\n\n";
43 unless ($r && $r->{content_gz}) {
44 print JSON::PP::encode_json({ status => 'unchecked', message => 'no blob' });
45 exit;
46 }
47 my $content = eval { Compress::Zlib::uncompress($r->{content_gz}) };
48 my $result = MODS::CompileCheck::check($content, $r->{file_name});
49 print JSON::PP::encode_json($result);
50 exit;
51}
2852
2953my $dbh = $db->db_connect or die "DB connect failed\n";
3054
3155my ($title, $subtitle, $blob_sha, $prev_sha, $meta) = ('', '', '', '', {});
3256
3357if ($kind eq 'file' && $id) {
3458 my $row = $db->db_readwrite($dbh, qq~
3559 SELECT fc.file_changes_id AS id, fc.file_name, fc.status, fc.blob_sha,
3660 fc.date_time, UNIX_TIMESTAMP(fc.date_time) AS ts_epoch,
3761 fc.uid, fc.gid, fc.permissions, fc.is_ts_only,
3862 fc.file_monitor_list_id, s.server_name
3963 FROM FILE_CHANGES fc LEFT JOIN SERVERS s ON s.server_id = fc.server_id
4064 WHERE fc.file_changes_id = $id LIMIT 1
4165 ~, __FILE__, __LINE__);
4266 if ($row && $row->{id}) {
4367 # Find the previous captured version of this file for the "before" pane.
4468 # Prefer same-monitor scope, but fall back to file-name-only for legacy
4569 # rows where file_monitor_list_id is NULL.
4670 my $q_fname = $dbh->quote($row->{file_name});
4771 my $scope = defined $row->{file_monitor_list_id}
4872 ? "file_monitor_list_id = $row->{file_monitor_list_id} AND "
4973 : '';
5074 my $prev = $db->db_readwrite($dbh, qq~
5175 SELECT blob_sha FROM FILE_CHANGES
5276 WHERE $scope
5377 file_name = $q_fname
5478 AND file_changes_id < $id
5579 AND blob_sha IS NOT NULL
5680 ORDER BY file_changes_id DESC LIMIT 1
5781 ~, __FILE__, __LINE__);
5882 $title = $row->{file_name};
5983 my $ep = int($row->{ts_epoch} || 0);
6084 $subtitle = qq~$row->{status} on $row->{server_name} at <span class="ts" data-ts="$ep" data-fmt="datetime">$row->{date_time}</span>~;
6185 $blob_sha = $row->{blob_sha};
6286 $prev_sha = $prev && $prev->{blob_sha} ? $prev->{blob_sha} : '';
6387 $meta = { %$row };
6488 }
6589}
6690elsif ($kind eq 'schema' && $id) {
6791 my $row = $db->db_readwrite($dbh, qq~
6892 SELECT sc.schema_change_id AS id, sc.database_name, sc.table_name, sc.changes,
6993 sc.schema_blob_sha, sc.change_datetime,
7094 UNIX_TIMESTAMP(sc.change_datetime) AS ts_epoch,
7195 s.server_name, sc.server_id
7296 FROM SCHEMA_CHANGE sc LEFT JOIN SERVERS s ON s.server_id = sc.server_id
7397 WHERE sc.schema_change_id = $id LIMIT 1
7498 ~, __FILE__, __LINE__);
7599 if ($row && $row->{id}) {
76100 my $q_db = $dbh->quote($row->{database_name});
77101 my $q_t = $dbh->quote($row->{table_name});
78102 my $prev = $db->db_readwrite($dbh, qq~
79103 SELECT schema_blob_sha FROM SCHEMA_CHANGE
80104 WHERE server_id = $row->{server_id}
81105 AND database_name = $q_db AND table_name = $q_t
82106 AND schema_change_id < $id
83107 AND schema_blob_sha IS NOT NULL
84108 ORDER BY schema_change_id DESC LIMIT 1
85109 ~, __FILE__, __LINE__);
86110 $title = "$row->{database_name}.$row->{table_name}";
87111 my $ep = int($row->{ts_epoch} || 0);
88112 $subtitle = qq~on $row->{server_name} at <span class="ts" data-ts="$ep" data-fmt="datetime">$row->{change_datetime}</span>~;
89113 $blob_sha = $row->{schema_blob_sha};
90114 $prev_sha = $prev && $prev->{schema_blob_sha} ? $prev->{schema_blob_sha} : '';
91115 $meta = { %$row };
92116 }
93117}
94118
95119# Fetch + decompress blobs
96120my $curr = $blob_sha ? _fetch_blob($db, $dbh, $blob_sha) : '';
97121my $prev = $prev_sha ? _fetch_blob($db, $dbh, $prev_sha) : '';
98122
99123$db->db_disconnect($dbh);
100124
101125# Compute a naive line-level diff
102126my @lines = MODS::Diff::lines($prev // '', $curr // '');
103127my ($ct_add, $ct_del) = (0, 0);
104128foreach my $L (@lines) {
105129 $ct_add++ if $L->{op} eq 'add';
106130 $ct_del++ if $L->{op} eq 'del';
107131 # HTML-escape diff'd content -- files under monitor may themselves be
108132 # HTML/JS/CSS, and dumping raw content into an on-screen table renders
109133 # the tags live. Escape it so it displays as source.
110134 my $t = $L->{text}; $t = '' unless defined $t;
111135 $t =~ s/&/&amp;/g; $t =~ s/</&lt;/g; $t =~ s/>/&gt;/g;
112136 $L->{text} = $t;
113137}
114138
115139my $tvars = {
116140 title => $title || '(unknown)',
117141 subtitle => $subtitle,
118142 kind => $kind,
119143 id => $id,
120144 has_diff => scalar(@lines) ? 1 : 0,
121145 lines => \@lines,
122146 ct_add => $ct_add,
123147 ct_del => $ct_del,
124148 ct_context => scalar(@lines) - $ct_add - $ct_del,
125149 has_prev => $prev_sha ? 1 : 0,
126150 curr_sha => substr($blob_sha || '', 0, 12),
127151 prev_sha => substr($prev_sha || '', 0, 12),
128152 restore_url => ($kind eq 'file' && $blob_sha) ? "/restore.cgi?id=$id" : '',
129153};
130154
131155my @body = $tpl->template('diff.html', $tvars);
132156MODS::PageWrapper->new->wrapper(
133157 page_title => "Diff -- $title",
134158 page_key => ($kind eq 'schema' ? 'schema_changes' : 'file_changes'),
135159 body_html => join('', @body),
136160 userinfo => { display_name => 'Operator' },
137161);
138162
139163#---------------------------------------------------------------------
140164sub _fetch_blob {
141165 my ($db, $dbh, $sha) = @_;
142166 my $sth = $dbh->prepare("SELECT content_gz FROM BLOB_STORE WHERE blob_sha = ?");
143167 return '' unless $sth && $sth->execute($sha);
144168 my $row = $sth->fetchrow_arrayref;
145169 $sth->finish;
146170 return '' unless $row && $row->[0];
147171 my $decompressed = eval { uncompress($row->[0]) };
148172 return $decompressed // '';
149173}
150174
151175#---------------------------------------------------------------------
152176# _line_diff -- naive line-level diff. Returns an array of hashrefs:
153177# { op => 'add' | 'del' | 'ctx', text => $line, old_n, new_n }
154178# Uses a simple LCS-based approach; adequate for small-to-medium files.
155179# For huge diffs shell out to /usr/bin/diff (future optimization).
156180#---------------------------------------------------------------------
157181sub _line_diff {
158182 my ($old, $new) = @_;
159183 my @old_lines = split /\r?\n/, ($old // '');
160184 my @new_lines = split /\r?\n/, ($new // '');
161185
162186 # LCS length table (dynamic programming) -- capped to prevent
163187 # runaway memory on huge inputs.
164188 my $O = scalar @old_lines;
165189 my $N = scalar @new_lines;
166190 if ($O > 5000 || $N > 5000) {
167191 # Fall back: just dump both sides linearly, marked as del/add.
168192 my @out;
169193 my $i = 0;
170194 push @out, { op => 'del', text => $_, old_n => ++$i, new_n => '' } for @old_lines;
171195 my $j = 0;
172196 push @out, { op => 'add', text => $_, old_n => '', new_n => ++$j } for @new_lines;
173197 return @out;
174198 }
175199
176200 my @lcs; for my $i (0..$O) { $lcs[$i][0] = 0; }
177201 for my $j (0..$N) { $lcs[0][$j] = 0; }
178202 for my $i (1..$O) {
179203 for my $j (1..$N) {
180204 $lcs[$i][$j] = $old_lines[$i-1] eq $new_lines[$j-1]
181205 ? $lcs[$i-1][$j-1] + 1
182206 : ($lcs[$i-1][$j] >= $lcs[$i][$j-1] ? $lcs[$i-1][$j] : $lcs[$i][$j-1]);
183207 }
184208 }
185209
186210 my @out;
187211 my ($i, $j) = ($O, $N);
188212 while ($i > 0 || $j > 0) {
189213 if ($i > 0 && $j > 0 && $old_lines[$i-1] eq $new_lines[$j-1]) {
190214 unshift @out, { op => 'ctx', text => $old_lines[$i-1], old_n => $i, new_n => $j };
191215 $i--; $j--;
192216 } elsif ($j > 0 && ($i == 0 || $lcs[$i][$j-1] >= $lcs[$i-1][$j])) {
193217 unshift @out, { op => 'add', text => $new_lines[$j-1], old_n => '', new_n => $j };
194218 $j--;
195219 } else {
196220 unshift @out, { op => 'del', text => $old_lines[$i-1], old_n => $i, new_n => '' };
197221 $i--;
198222 }
199223 }
200224 return @out;
201225}