Replace file storage by a PostgreSQL table. Resign to the additional
dependency for this kind of social feature since it's much more appropriate
(especially with upcoming extensions), and optional similar to external
comment services.
Identical results after importing existing files:
ls melding/2*/*/*.html | perl -MFile::Slurp -lnE '
my ($page, $meta) = m{(.*)/(.*)};
my ($time, $author) = split /[:.]/, $meta;
$time .= "00" while length($time) < 14;
$time =~ s/^(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})/$1-$2-$3 $4:$5:/g;
my $html = read_file($_);
chomp, s/\\\K/\\/g, s/\n/\\n/g, s/\t/\\t/g for $html;
say join "\t", $page, $author, $time, $html;
' | psql -c 'COPY comments (page, author, created, message) FROM STDIN'
+# local configuration
+/.dbconfig.inc.php
+
# assets built by make rules
/lib
!/lib/photoswipe.html
# assets built by make rules
/lib
!/lib/photoswipe.html
--- /dev/null
+<?php
+$dsn = require '.dbconfig.inc.php';
+$options = [ PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION ];
+$Db = new PDO($dsn, NULL, NULL, $options);
--- /dev/null
+CREATE TABLE comments (
+ page text,
+ message text,
+ created timestamptz DEFAULT now(),
+ author text,
+ id serial PRIMARY KEY
+);
+require_once 'database.inc.php';
print '<h3>Reacties</h3>'."\n";
print '<h3>Reacties</h3>'."\n";
-$pagelink = $Page.$Args;
- @mkdir($pagelink);
- $target = $pagelink.'/'.date('YmdHis').':'.$User->login.'.html';
$html = nl2br(htmlspecialchars($_POST['reply']));
$html = nl2br(htmlspecialchars($_POST['reply']));
- $html = "<p>$html</p>\n";
- $written = file_put_contents($target, $html);
- if ($written === FALSE) {
+ $html = "<p>$html</p>";
+ $query = $Db->prepare('INSERT INTO comments (page, message, author) VALUES (?, ?, ?)');
+ $query->execute([ $Page, $html, $User->login ]);
+ if (!$query->rowCount()) {
throw new Exception('Fout bij opslaan');
}
$_POST['reply'] = NULL;
throw new Exception('Fout bij opslaan');
}
$_POST['reply'] = NULL;
+$query = $Db->prepare('SELECT * FROM comments WHERE page = ? ORDER BY created');
+$query->execute([$Page]);
+
print '<ul class="replies">';
print '<ul class="replies">';
-foreach (glob("$pagelink/*.html") as $reply) {
- preg_match('</(\d{2,14}) : ([^:]*) [^/]* \.html$>x', $reply, $replymeta);
- if (!$replymeta) continue;
- $replydate = str_split($replymeta[1], 2);
- $replydate[0] = array_shift($replydate) . $replydate[0];
- $replyuser = new User("profile/{$replymeta[2]}");
+while ($row = $query->fetch(PDO::FETCH_OBJ)) {
+ $rowuser = new User("profile/{$row->author}");
print '<li>';
printf('<strong>%s</strong> <small class=date>%s</small>',
print '<li>';
printf('<strong>%s</strong> <small class=date>%s</small>',
- $replyuser->html, showdate($replydate)
+ $rowuser->html, showdate(preg_split('/\D/', $row->created))
- printf("<blockquote>%s</blockquote>\n", file_get_contents($reply));
+ printf("<blockquote>%s</blockquote>\n", $row->message);