summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlvaro Herrera2008-12-09 16:05:53 +0000
committerAlvaro Herrera2008-12-09 16:05:53 +0000
commita45c3ec316c837c69a2bf266f95ce704e8220a7c (patch)
treeac16e6b4eb7f30b57b31e09c599972f606fc3275
parent8216ae67256e826e8e09a362b78cdc36d8bf9fbb (diff)
Use JSON files for storing the intermediate list and group info.
git-svn-id: file:///Users/dpage/pgweb/svn-repo/trunk@2334 8f5c7a92-453e-0410-a47f-ad33c8a6b003
-rwxr-xr-xarchives/bin/generate-list-descriptions138
-rw-r--r--archives/html/index.php58
-rw-r--r--archives/html/list_index.php32
3 files changed, 113 insertions, 115 deletions
diff --git a/archives/bin/generate-list-descriptions b/archives/bin/generate-list-descriptions
index d9423d15..061053db 100755
--- a/archives/bin/generate-list-descriptions
+++ b/archives/bin/generate-list-descriptions
@@ -1,64 +1,92 @@
-#!/bin/sh
+#!/usr/bin/perl -w
#
# $Id$
#
-# Script to generate text files with descriptions of each list and group.
+# Script to generate JSON files with descriptions of each list and group.
#
-# We generate five families of files:
-# <list>.descr -- contains the description of each list
-# <list>.group -- contains the group "id" that each list belongs to
-# (note: not the group ID on database; it's generated here)
-# group.<id> -- contains the name of each group, and the name of each list
-# on the group
-# groups -- contains the list of all groups, with its "id", the
-# name of the "first" list on the group, and the group name
-# groups.complete
-# a listing of all groups, with its name and the names
-# and descriptions of lists on it
+# We generate two files:
+# groups.json -- contains information about list groups
+# lists.json -- contains information about lists
#
-# The various files are used in several places, both in the PHP templates and
-# in the Perl program that calls Mhonarc. Probably it'd be possible to get rid
-# of most of the files and keep just "groups.complete", but I'm feeling lazy
-# today.
+# NOTE: groups.json contains the groups indexed by sortkey. This is
+# a kluge to support sorting in PHP with ksort(); apparently it's not
+# possible to specify a comparison callback in PHP.
-. $HOME/etc/archives.conf
+use warnings;
+use strict;
+use DBI;
+use JSON;
-psql_exec() {
- $PSQL -tA -U $DBUSER -h $DBHOST $DBNAME -c "$1"
+my ($group, $list);
+parse_config();
+$group = "$ENV{'ARCHIVES_TEMPLATES'}/groups.json";
+$list = "$ENV{'ARCHIVES_TEMPLATES'}/lists.json";
+
+my ($conninfo, $conn);
+$conninfo = sprintf("db=%s;host=%s", $ENV{'DBNAME'}, $ENV{'DBHOST'});
+$conn = DBI->connect("dbi:Pg:$conninfo", $ENV{'DBUSER'}, '', {AutoCommit => 0})
+ or die "cannot connect";
+
+my (%groups, %lists);
+
+my $query = "SELECT lg.id AS groupid, lg.name AS groupname, sortkey
+ FROM listgroups lg";
+my $sth = $conn->prepare($query);
+$sth->execute;
+while (my $h = $sth->fetchrow_hashref) {
+ $groups{$h->{'sortkey'}} = {
+ id => $h->{'groupid'},
+ name => $h->{'groupname'}
+ };
+}
+
+$query = "SELECT lg.id AS groupid, lg.sortkey, l.name, l.shortdesc,
+ l.description
+ FROM listgroups lg JOIN lists l ON (lg.id = l.grp)
+ ORDER BY sortkey, l.name";
+$sth = $conn->prepare($query);
+$sth->execute;
+while (my $h = $sth->fetchrow_hashref) {
+ push @{$groups{$h->{'sortkey'}}->{'lists'}}, $h->{'name'};
+ $lists{$h->{'name'}} = {
+ description => $h->{'description'},
+ shortdesc => $h->{'shortdesc'},
+ group => $h->{'groupid'}
+ };
}
-prevgrp=
-curr=0
-printedfirst=
-> $ARCHIVES_TEMPLATES/groups.new
-> $ARCHIVES_TEMPLATES/groups.complete.new
-psql_exec "select lg.id, lg.name, l.name, l.description from listgroups lg join lists l on (lg.id = l.grp) order by sortkey, l.name" |
-while read line; do
- # strip backslashes
- line=${line//\\/}
- descr=${line##[^|]*\|}
- line=${line%%\|$descr}
- list=${line##[^|]*\|}
- line=${line%%\|$list}
- grp=${line##[^|]*\|}
- grpid=${line%%\|$grp}
- if [ "$grp" != "$prevgrp" ]; then
- curr=$(($curr+1))
- filename=$ARCHIVES_TEMPLATES/group.$curr
- echo -n "group.$curr " >> $ARCHIVES_TEMPLATES/groups.new
- printedfirst=
- echo "groupname: $grp" > $filename
- echo "groupname: $grp" >> $ARCHIVES_TEMPLATES/groups.complete.new
- prevgrp=$grp
- fi
- echo "list: $list" >> $filename
- echo "list: $list $descr" >> $ARCHIVES_TEMPLATES/groups.complete.new
- if [ -z $printedfirst ]; then
- echo "$list $grp" >> $ARCHIVES_TEMPLATES/groups.new
- printedfirst=42
- fi
- echo "$curr" > $ARCHIVES_TEMPLATES/$list.group
- echo "$descr" > $ARCHIVES_TEMPLATES/$list.descr
-done
-mv $ARCHIVES_TEMPLATES/groups.new $ARCHIVES_TEMPLATES/groups
-mv $ARCHIVES_TEMPLATES/groups.complete.new $ARCHIVES_TEMPLATES/groups.complete
+my $json = new JSON;
+writefile($group, \%groups);
+writefile($list, \%lists);
+$conn->disconnect;
+
+sub writefile {
+ my $filename = shift;
+ my $data = shift;
+
+ my $tmpfile = "$filename.new";
+ if (open TMPOUT, ">", $tmpfile) {
+ print TMPOUT $json->encode($data);
+ close TMPOUT;
+ rename $tmpfile, $filename;
+ }
+}
+
+# parse config file
+sub parse_config {
+ open CONF, "<", "$ENV{'HOME'}/etc/archives.conf"
+ or die "cannot parse config file: $!";
+ while (<CONF>) {
+ $_ = $1 if m/(.*)#/; # strip comments
+ next if m/^$/; # skip empty lines
+ next if (!m/([A-Z_]+)=(.*)\s*$/); # only lines like FOO=bar
+ # poor man's shell variable evaluation
+ my $key = $1;
+ my $val = $2;
+ if ($val =~ /(.*)\$([[:alnum:]_]+)(.*)/) {
+ $val = "$1$ENV{$2}$3";
+ }
+ $ENV{$key} = $val;
+ }
+ close CONF;
+}
diff --git a/archives/html/index.php b/archives/html/index.php
index b7a72112..1082f5dd 100644
--- a/archives/html/index.php
+++ b/archives/html/index.php
@@ -7,51 +7,33 @@ require 'HTML/Template/Sigma.php';
$tpl =& new HTML_Template_Sigma('../templates/html');
$tpl->loadTemplateFile('index.html');
-$f = fopen("../templates/groups.complete", 'r');
-if (!$f)
- exit;
-while ($line = fgets($f)) {
-
- /* got a group name */
- list($group) = sscanf($line, "groupname: %[^$]s");
- if (isset($group)) {
-
- /* This is for index.html. We want a title for each group, and
- * the lists that are part of it. */
- $tpl->parse('listgroup'); // parse previous group, if any
- $tpl->setCurrentBlock('listgroup');
- $tpl->setVariable('groupname', $group);
-
- /* This is for the menu on top_config.html. On this page we only
- * link to the first list on each group. */
- $tpl->parse('top_listgroup');
- $tpl->setCurrentBlock('top_listgroup');
- $tpl->setVariable('top_groupname', $group);
- $gotfirst = false;
-
- continue;
- }
+$groups = json_decode(file_get_contents("../templates/groups.json"), true);
+$lists = json_decode(file_get_contents("../templates/lists.json"), true);
+
+ksort($groups);
+
+foreach ($groups as $group) {
+ /* This is for index.html. We want a title for each group, and
+ * the lists that are part of it. */
+ $tpl->parse('listgroup');
+ $tpl->setCurrentBlock('listgroup');
+ $tpl->setVariable('groupname', $group["name"]);
- /* got a list */
- list($list, $descr) = sscanf($line, "list: %s %[^$]s");
- if (isset($list)) {
+ foreach ($group["lists"] as $list) {
$tpl->setCurrentBlock('list');
$tpl->setVariable('name', $list);
- $tpl->setVariable('description', "$descr");
+ $tpl->setVariable('description', $lists[$list]["description"]);
$tpl->parseCurrentBlock();
-
- if (!$gotfirst) {
- $tpl->setVariable('top_group_firstlist', $list);
- $gotfirst = true;
- }
-
- continue;
}
- print "malformed line!";
- exit;
+ /* This is for the menu on top_config.html. On this page we only
+ * link to the first list on each group. */
+ $tpl->parse('top_listgroup');
+ $tpl->setCurrentBlock('top_listgroup');
+ $tpl->setVariable('top_groupname', $group["name"]);
+ $tpl->setVariable('top_group_firstlist', $group["lists"][0]);
}
-fclose($f);
+
$tpl->parseCurrentBlock();
$tpl->show();
diff --git a/archives/html/list_index.php b/archives/html/list_index.php
index 809f56ff..14952c5c 100644
--- a/archives/html/list_index.php
+++ b/archives/html/list_index.php
@@ -44,43 +44,31 @@ list($listname) = sscanf($_SERVER['PHP_SELF'], "/%[^/]s");
/* fill the left-side menu with all the group names, expanding lists
* for the group this list belongs */
-$groupid = file_get_contents($tmpldir . "/$listname.group");
-$f = fopen($tmpldir . "/groups", 'r');
-if (!$f)
- exit;
-while ($line = fgets($f)) {
- list($id, $firstlist, $group) = sscanf($line, "group.%d %s %[^$]s");
- if (!isset($id))
- exit;
- $group = rtrim($group);
+$groups = json_decode(file_get_contents($tmpldir."/groups.json"), true);
+$lists = json_decode(file_get_contents($tmpldir."/lists.json"), true);
+ksort($groups);
+$mygroup = $lists[$listname]["group"];
+foreach ($groups as $group) {
$tpl->setCurrentBlock('top_listgroup');
- $tpl->setVariable('top_groupname', $group);
- $tpl->setVariable('top_group_firstlist', $firstlist);
+ $tpl->setVariable('top_groupname', $group["name"]);
+ $tpl->setVariable('top_group_firstlist', $group["lists"][0]);
/* When we detect the group that this list belongs to, expand the lists
* on the group */
- if ($groupid == $id) {
+ if ($group["id"] == $mygroup) {
$tpl->setCurrentBlock('top_list');
- $g = fopen($tmpldir . "/group.$id", 'r');
- while ($line = fgets($g)) {
- list($list) = sscanf($line, "list: %[^$]s");
- if (!isset($list))
- continue;
+ foreach ($group["lists"] as $list) {
$tpl->setVariable('top_name', $list);
$tpl->parse('top_list');
}
- fclose($g);
}
-
$tpl->parse('top_listgroup');
}
-fclose($f);
// finally, set the list name and description
$tpl->setVariable('list', $listname);
-$descr = rtrim(file_get_contents($tmpldir . "/$listname.descr"));
-$tpl->setVariable('description', $descr);
+$tpl->setVariable('description', $lists[$listname]["description"]);
$tpl->parse();
$tpl->show();