summaryrefslogtreecommitdiffstats
path: root/insert-entries.pl
blob: 71f4dbd05125c165ff4f07439a5db4c9e16196f9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#! /usr/local/bin/perl -w

# Insert weblog entries into a PostgreSQL database.

require 5.10.0;

use strict;
use warnings;

use Encode;

use XML::LibXML qw(:libxml :ns);

use DBI;

use Digest::SHA1 qw(sha1_hex);

use Getopt::Std;

use constant {
    XHTML_PUBID => "-//W3C//DTD XHTML 1.0 Strict//EN",
    XHTML_URI => "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd",
    XHTML_NS => "http://www.w3.org/1999/xhtml",
};

my %opts;

getopts('cd:', \%opts);

my $dbname = "weblog" // $opts{d};  # The PostgreSQL database to use
my $obtain_cdates = $opts{c};

my $parser = XML::LibXML->new();


# Functions to take care of the x-daml-magic URI scheme (this should go away)

sub match_daml_magic_uri {
    my $uri = shift;
    return $uri =~ /^x-daml-magic\:/;
}

my %daml_magic_values = (
    "x-daml-magic://daml/weblog/domaxentries.xml" => "INCLUDE",
    "x-daml-magic://daml/weblog/maxentries.xml" => "20",
    "x-daml-magic://daml/weblog/domonth.xml" => "IGNORE",
    "x-daml-magic://daml/weblog/month.xml" => "",
    "x-daml-magic://daml/weblog/docategory.xml" => "IGNORE",
    "x-daml-magic://daml/weblog/category-en.xml" => "",
    "x-daml-magic://daml/weblog/category-fr.xml" => "",
    );

sub open_daml_magic_uri {
    my $uri = shift;
    my $s = $daml_magic_values{$uri};
    if ( defined($s) ) {
	return \$s;
    } else {
	return undef;
    }
}

sub read_daml_magic_uri {
    my $handler = shift;
    my $length = shift;
    my $buf = substr($$handler, 0, $length, "");
    return $buf;
}

sub close_daml_magic_uri {
    my $handler = shift;
    return "0 but true";
}

my $input_callbacks = XML::LibXML::InputCallback->new();
$input_callbacks->register_callbacks([ \&match_daml_magic_uri,
				       \&open_daml_magic_uri,
				       \&read_daml_magic_uri,
				       \&close_daml_magic_uri ]);
$parser->input_callbacks($input_callbacks);

# Avoid accessing the W3C web site all the time.
$parser->load_catalog("/etc/xml/catalog");

my $doc;
if ( defined($ARGV[0]) ) {
    $doc = $parser->parse_file($ARGV[0]);
} else {
    $doc = $parser->parse_fh(\*STDIN);
}

my $dbh = DBI->connect("dbi:Pg:dbname=$dbname", "", "", {AutoCommit=>1,RaiseError=>1,pg_enable_utf8=>1});
$dbh->do("SET TIME ZONE 0");

my ($sth_check, $sth_insert, $sth_update, $sth_clear_cat, $sth_set_cat);
$sth_check = $dbh->prepare("SELECT sha1 FROM entries WHERE id=?");
if ( $obtain_cdates ) {
    $sth_insert = $dbh->prepare("INSERT INTO entries(id,edate,lang,title,title_xml,content,sha1,cdate) VALUES (?,?,?,?,?,?,?,?)");
    $sth_update = $dbh->prepare("UPDATE entries SET (edate,mdate,lang,title,title_xml,content,sha1,cdate)=(?,DEFAULT,?,?,?,?,?,?) WHERE id=?");
} else {
    $sth_insert = $dbh->prepare("INSERT INTO entries(id,edate,lang,title,title_xml,content,sha1) VALUES (?,?,?,?,?,?,?)");
    $sth_update = $dbh->prepare("UPDATE entries SET (edate,mdate,lang,title,title_xml,content,sha1)=(?,DEFAULT,?,?,?,?,?) WHERE id=?");
}
$sth_clear_cat = $dbh->prepare("DELETE FROM incat WHERE id=?");
$sth_set_cat = $dbh->prepare("INSERT INTO incat(id,code) VALUES (?,?)");

sub get_node_lang {
    my $node = shift;
    while ( defined($node) ) {
	my $lang = $node->getAttributeNS(XML_XML_NS, "lang");
	return $lang if defined($lang);
#	$lang = $node->getAttribute("lang");
#	return $lang if defined($lang);
	$node = $node->parentNode;
    }
}

my $xpc = XML::LibXML::XPathContext->new($doc);
#$xpc->registerNs('h', XHTML_NS);
my $entry_list = $xpc->findnodes("//d:weblog/d:entry");
foreach my $node ( $entry_list->get_nodelist ) {
    my $id = $node->getAttribute("number")+0;
    my $date = $node->getAttribute("date");
    my $catlist = $node->getAttribute("cat");
    my $lang = get_node_lang $node;
    $node->setAttributeNS(XML_XML_NS, "lang", $lang) if defined($lang);
    my $str = $node->serialize();
    my $sha1 = sha1_hex(encode("utf8",$str));
    $sth_check->execute($id);
    my $exists = $sth_check->rows;
    next if $exists && ($sth_check->fetchrow_array)[0] eq $sha1;
    my $title_node = ($xpc->findnodes("d:title", $node))[0];
    my $title = defined($title_node)?$title_node->textContent:undef;
    my $title_xml = defined($title_node)?$title_node->serialize:undef;
    my $cdate;
    if ( $obtain_cdates ) {
	if ( $date =~ /^(\d+)-03-32$/ ) {
	    $cdate = "$1-04-01 12:00:00+00";
	} else {
	    $cdate = "$date 12:00:00+00";
	}
    }
    $dbh->{AutoCommit} = 0;
    if ( $exists ) {
	print STDERR "Updating entry $id\n";
	if ( $obtain_cdates ) {
	    $sth_update->execute($date, $lang, $title, $title_xml, $str, $sha1, $cdate, $id);
	} else {
	    $sth_update->execute($date, $lang, $title, $title_xml, $str, $sha1, $id);
	}
    } else {
	print STDERR "Registering entry $id\n";
	if ( $obtain_cdates ) {
	    $sth_insert->execute($id, $date, $lang, $title, $title_xml, $str, $sha1, $cdate);
	} else {
	    $sth_insert->execute($id, $date, $lang, $title, $title_xml, $str, $sha1);
	}
    }
    $sth_clear_cat->execute($id);
    if ( defined($catlist) ) {
	foreach my $cat ( split(' ', $catlist) ) {
	    $sth_set_cat->execute($id,$cat);
	}
    }
    $dbh->commit;
}
$sth_check->finish;
$sth_insert->finish;
$sth_update->finish;
$sth_clear_cat->finish;
$sth_set_cat->finish;
$dbh->disconnect;