2 # Copyright (C) 2014 Laurentian University
3 # Author: Dan Scott <dscott@laurentian.ca>
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 use strict; use warnings;
21 use DBI qw(:sql_types);
23 my ($dbhost, $dbport, $dbname, $dbuser, $dbpw, $help);
27 =item create_sitemaps() - Write the sitemap files
29 With a maximum of 50,000 URLs per sitemap, this method
30 automatically increments the sitemap file numbers and
31 generates a corresponding sitemap index that lists all
32 of the individual sitemap files.
34 See http://www.sitemaps.org/ for the specification
38 my ($settings, $bibs, $aou_id) = @_;
43 my $fn = $settings->{'prefix'} . "sitemap$f_cnt.xml";
45 open(FH, '>', $fn) or die "Could not write sitemap $f_cnt\n";
46 print FH '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
47 print FH '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . "\n";
49 foreach my $bib (@$bibs) {
50 print FH "<url><loc>" . $settings->{'lib-hostname'} . "/eg/opac/record/" . $bib->[0];
52 print FH "?locg=$aou_id";
54 print FH "</loc><lastmod>" . $bib->[1] . "</lastmod></url>\n";
56 if ($r_cnt % 50000 == 0) {
58 print FH "</urlset>\n";
60 my $fn = $settings->{'prefix'} . "sitemap$f_cnt.xml";
62 open(FH, '>', $fn) or die "Could not write bibs\n";
63 print FH '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
64 print FH '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . "\n";
67 print FH "</urlset>\n";
70 open(INDEXFH, '>', $settings->{'prefix'} . "sitemapindex.xml") or die "Could not write sitemap index\n";
71 print INDEXFH '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
72 print INDEXFH '<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . "\n";
73 foreach my $fn (@sitemaps) {
74 print INDEXFH "<sitemap><loc>" . $settings->{'lib-hostname'} . "/$fn</loc></sitemap>\n";
76 print INDEXFH "</sitemapindex>\n";
82 =item get_settings() - Extracts database settings from opensrf.xml
87 my $host = "/opensrf/default/apps/open-ils.reporter-store/app_settings/database/host/text()";
88 my $port = "/opensrf/default/apps/open-ils.reporter-store/app_settings/database/port/text()";
89 my $dbname = "/opensrf/default/apps/open-ils.reporter-store/app_settings/database/db/text()";
90 my $user = "/opensrf/default/apps/open-ils.reporter-store/app_settings/database/user/text()";
91 my $pw = "/opensrf/default/apps/open-ils.reporter-store/app_settings/database/pw/text()";
93 my $parser = XML::LibXML->new();
94 my $opensrf_config = $parser->parse_file($config_file);
96 # If the user passed in settings at the command line,
97 # we don't want to override them
98 $settings->{host} = $settings->{host} || $opensrf_config->findnodes($host);
99 $settings->{port} = $settings->{port} || $opensrf_config->findnodes($port);
100 $settings->{db} = $settings->{db} || $opensrf_config->findnodes($dbname);
101 $settings->{user} = $settings->{user} || $opensrf_config->findnodes($user);
102 $settings->{pw} = $settings->{pw} || $opensrf_config->findnodes($pw);
105 =item get_record_ids() - Gets a list of record IDs
108 my $settings = shift;
111 my $dbh = DBI->connect('dbi:Pg:dbname=' . $settings->{db} .
112 ';host=' . $settings->{host} . ';port=' . $settings->{port} . ';',
113 $settings->{user} . "", $settings->{pw} . "", {AutoCommit => 1}
116 print STDERR "Could not connect to database. ";
117 print STDERR "Error was " . $dbh->errstr . "\n";
121 if ($settings->{'lib-shortname'}) {
122 my $stmt = $dbh->prepare("SELECT id FROM actor.org_unit WHERE shortname = ?");
123 $stmt->execute(($settings->{'lib-shortname'}));
124 my $rv = $stmt->bind_columns(\$aou_id);
129 SELECT DISTINCT bre.id, edit_date::date AS edit_date
130 FROM biblio.record_entry bre
131 INNER JOIN asset.opac_visible_copies aovc ON bre.id = aovc.record
134 $q .= " WHERE circ_lib IN (SELECT id FROM actor.org_unit WHERE id = ? OR parent_ou = ?)";
136 $q .= " ORDER BY edit_date DESC";
137 my $stmt = $dbh->prepare($q);
139 $stmt->bind_param(1, $aou_id, { TYPE => SQL_INTEGER });
140 $stmt->bind_param(2, $aou_id, { TYPE => SQL_INTEGER });
146 my $bibs = $stmt->fetchall_arrayref([0, 1]);
149 print STDERR "Error was " . $dbh->errstr . "\n";
152 return ($bibs, $aou_id);
162 "lib-hostname=s" => \$settings{'lib-hostname'},
163 "lib-shortname=s" => \$settings{'lib-shortname'},
164 "prefix=s" => \$settings{'prefix'},
165 "config-file=s" => \$config_file,
166 "user=s" => \$settings{'user'},
167 "password=s" => \$settings{'pw'},
168 "database=s" => \$settings{'db'},
169 "hostname=s" => \$settings{'host'},
170 "port=i" => \$settings{'port'},
175 my @temp = `eg_config --sysconfdir`;
177 $sysconfdir = $temp[0];
178 $config_file = File::Spec->catfile($sysconfdir, "opensrf.xml");
181 unless (-e $config_file) { die "Error: $config_file does not exist. \n"; }
183 if ($settings{'lib-hostname'}) {
184 # Get additional settings from the config file
185 get_settings(\%settings);
187 my ($bibs, $aou_id) = get_record_ids(\%settings);
188 create_sitemaps(\%settings, $bibs, $aou_id);
197 sitemap_generator [OPTION] ... [COMMAND] ... [CONFIG OPTIONS]
200 Creates a set of sitemaps for enabling web crawlers to crawl
201 freshly changed bibliographic records.
205 specifies the opensrf.xml file
208 REQUIRED: hostname for the catalog (e.g "https://example.com")
211 filename to add as a prefix to the generated set of sitemap files
214 include all records for the specified library and its children;
215 defaults to all records
218 This script will normally be run as a cron job by the opensrf user from
219 the web root directory.
221 sitemap_generator --lib-hostname https://example.com --lib-shortname BR1 \
224 This generates a set of sitemap files like so:
225 * example_sitemapindex.xml
226 * example_sitemap1.xml
227 * example_sitemap2.xml