]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/support-scripts/sitemap_generator
Revert "LP#1635737 Use new OpenSRF interval_to_seconds() context"
[working/Evergreen.git] / Open-ILS / src / support-scripts / sitemap_generator
1 #!/usr/bin/perl
2 # Copyright (C) 2014 Laurentian University
3 # Author: Dan Scott <dscott@laurentian.ca>
4 #
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.
9 #
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.
14
15 use strict; use warnings;
16 use XML::LibXML;
17 use File::Copy;
18 use Getopt::Long;
19 use File::Spec;
20 use File::Basename;
21 use DBI qw(:sql_types);
22 use DBD::Pg qw(:pg_types);
23
24 my ($dbhost, $dbport, $dbname, $dbuser, $dbpw, $help);
25 my $config_file = '';
26 my $sysconfdir = '';
27
28 =item create_sitemaps() - Write the sitemap files
29
30 With a maximum of 50,000 URLs per sitemap, this method
31 automatically increments the sitemap file numbers and
32 generates a corresponding sitemap index that lists all
33 of the individual sitemap files.
34
35 See http://www.sitemaps.org/ for the specification
36
37 =cut
38 sub create_sitemaps {
39     my ($settings, $bibs, $aou_id) = @_;
40
41     my $f_cnt = 1;
42     my $r_cnt = 0;
43     my @sitemaps;
44     my $fn = $settings->{'prefix'} . "sitemap$f_cnt.xml";
45     push(@sitemaps, $fn);
46     open(FH, '>', $fn) or die "Could not write sitemap $f_cnt\n";
47     print FH '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
48     print FH '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . "\n";
49
50     foreach my $bib (@$bibs) {
51         print FH "<url><loc>" . $settings->{'lib-hostname'} . "/eg/opac/record/" . $bib->[0];
52         if ($aou_id) {
53             print FH "?locg=$aou_id";
54         }
55         print FH "</loc><lastmod>" . $bib->[1] . "</lastmod></url>\n";
56         $r_cnt++;
57         if ($r_cnt % 50000 == 0) {
58             $f_cnt++;
59             print FH "</urlset>\n";
60             close(FH);
61             my $fn = $settings->{'prefix'} . "sitemap$f_cnt.xml";
62             push(@sitemaps, $fn);
63             open(FH, '>', $fn) or die "Could not write bibs\n";
64             print FH '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
65             print FH '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . "\n";
66         }
67     }
68     print FH "</urlset>\n";
69     close(FH);
70
71     open(INDEXFH, '>', $settings->{'prefix'} . "sitemapindex.xml") or die "Could not write sitemap index\n";
72     print INDEXFH '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
73     print INDEXFH '<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . "\n";
74     foreach my $fn (@sitemaps) {
75         print INDEXFH "<sitemap><loc>" . $settings->{'lib-hostname'} . "/$fn</loc></sitemap>\n";
76     }
77     print INDEXFH "</sitemapindex>\n";
78     close(INDEXFH);
79     
80
81 }
82
83 =item get_settings() - Extracts database settings from opensrf.xml
84 =cut
85 sub get_settings {
86     my $settings = shift;
87
88     my $host = "/opensrf/default/apps/open-ils.reporter-store/app_settings/database/host/text()";
89     my $port = "/opensrf/default/apps/open-ils.reporter-store/app_settings/database/port/text()";
90     my $dbname = "/opensrf/default/apps/open-ils.reporter-store/app_settings/database/db/text()";
91     my $user = "/opensrf/default/apps/open-ils.reporter-store/app_settings/database/user/text()";
92     my $pw = "/opensrf/default/apps/open-ils.reporter-store/app_settings/database/pw/text()";
93
94     my $parser = XML::LibXML->new();
95     my $opensrf_config = $parser->parse_file($config_file);
96
97     # If the user passed in settings at the command line,
98     # we don't want to override them
99     $settings->{host} = $settings->{host} || $opensrf_config->findnodes($host);
100     $settings->{port} = $settings->{port} || $opensrf_config->findnodes($port);
101     $settings->{db} = $settings->{db} || $opensrf_config->findnodes($dbname);
102     $settings->{user} = $settings->{user} || $opensrf_config->findnodes($user);
103     $settings->{pw} = $settings->{pw} || $opensrf_config->findnodes($pw);
104 }
105
106 =item get_record_ids() - Gets a list of record IDs
107 =cut
108 sub get_record_ids {
109     my $settings = shift;
110     my $aou_id;
111
112     my $dbh = DBI->connect('dbi:Pg:dbname=' . $settings->{db} . 
113         ';host=' . $settings->{host} . ';port=' . $settings->{port} . ';',
114          $settings->{user} . "", $settings->{pw} . "", {AutoCommit => 1}
115     );
116     if ($dbh->err) {
117         print STDERR "Could not connect to database. ";
118         print STDERR "Error was " . $dbh->errstr . "\n";
119         return;
120     }
121
122     if ($settings->{'lib-shortname'}) {
123         my $stmt = $dbh->prepare("SELECT id FROM actor.org_unit WHERE shortname = ?");
124         $stmt->execute(($settings->{'lib-shortname'}));
125         my $rv = $stmt->bind_columns(\$aou_id);
126         $stmt->fetch();
127     }
128
129     my $q = "
130         WITH date_floor AS (
131             SELECT ?::date AS val
132         )
133     ";
134     if ($aou_id) {
135         $q .= "
136         , copy_orgs AS (
137             SELECT id
138             FROM actor.org_unit
139             WHERE id IN (SELECT id FROM actor.org_unit_descendants(?))
140         ),
141         uri_orgs AS (
142             SELECT id
143             FROM actor.org_unit
144             WHERE id IN (SELECT id FROM actor.org_unit_ancestors(?))
145                 AND id NOT IN (SELECT id FROM org_top())
146         )
147         ";
148     }
149     $q .= "
150         SELECT DISTINCT id, edit_date FROM (
151             SELECT bre.id,
152                 CASE
153                     WHEN bre.edit_date::date < (SELECT val FROM date_floor LIMIT 1) THEN (SELECT val FROM date_floor LIMIT 1)
154                     ELSE bre.edit_date::date
155                 END AS edit_date
156             FROM biblio.record_entry bre
157                 INNER JOIN asset.opac_visible_copies aovc ON bre.id = aovc.record
158     ";
159     if ($aou_id) {
160         $q .= " WHERE circ_lib IN (SELECT id FROM copy_orgs)";
161     }
162     $q .= "
163             UNION 
164             SELECT bre.id,
165                 CASE
166                     WHEN bre.edit_date::date < (SELECT val FROM date_floor LIMIT 1) THEN (SELECT val FROM date_floor LIMIT 1)
167                     ELSE bre.edit_date::date
168                 END AS edit_date
169             FROM biblio.record_entry bre
170                 INNER JOIN asset.call_number acn ON bre.id = acn.record
171             WHERE bre.deleted IS FALSE AND acn.deleted IS FALSE 
172     ";
173     if ($aou_id) {
174         $q .= "
175             AND owning_lib IN (SELECT id FROM uri_orgs) AND label = '##URI##'
176         ";
177     }
178     $q .= "
179         ) x
180         ORDER BY edit_date DESC, id DESC
181     ";
182     my $stmt = $dbh->prepare($q);
183     if ($aou_id) {
184         $stmt->bind_param(1, $settings->{'date'}, { pg_type => PG_DATE });
185         $stmt->bind_param(2, $aou_id, SQL_INTEGER);
186         $stmt->bind_param(3, $aou_id, SQL_INTEGER);
187     } else {
188         $stmt->bind_param(1, $settings->{'date'}, { pg_type => PG_DATE });
189     }
190     $stmt->execute();
191
192     my $bibs = $stmt->fetchall_arrayref([0, 1]);
193
194     if ($dbh->err) {
195         print STDERR "Error was " . $dbh->errstr . "\n";
196         return;
197     }
198     return ($bibs, $aou_id);
199 }
200
201 my $hostname;
202 my $aou_shortname;
203 my %settings = (
204     prefix => '',
205     date => '2010-01-01'
206 );
207
208 GetOptions(
209         "lib-hostname=s" => \$settings{'lib-hostname'},
210         "lib-shortname=s" => \$settings{'lib-shortname'},
211         "prefix=s" => \$settings{'prefix'},
212         "date-floor=s" => \$settings{'date'},
213         "config-file=s" => \$config_file,
214         "user=s" => \$settings{'user'},
215         "password=s" => \$settings{'pw'},
216         "database=s" => \$settings{'db'},
217         "hostname=s" => \$settings{'host'},
218         "port=i" => \$settings{'port'}, 
219         "help" => \$help
220 );
221
222 if (!$config_file) { 
223     my @temp = `eg_config --sysconfdir`;
224     chomp $temp[0];
225     $sysconfdir = $temp[0];
226     $config_file = File::Spec->catfile($sysconfdir, "opensrf.xml");
227 }
228
229 unless (-e $config_file) { die "Error: $config_file does not exist. \n"; }
230
231 if ($settings{'lib-hostname'}) {
232     # Get additional settings from the config file
233     get_settings(\%settings);
234
235     my ($bibs, $aou_id) = get_record_ids(\%settings);
236     create_sitemaps(\%settings, $bibs, $aou_id);
237 } else {
238     $help = 1;
239 }
240
241 if ($help) {
242     print <<HERE;
243
244 SYNOPSIS
245     sitemap_generator [OPTION] ... [COMMAND] ... [CONFIG OPTIONS]
246
247 DESCRIPTION
248     Creates a set of sitemaps for enabling web crawlers to crawl
249     freshly changed bibliographic records.
250
251 OPTIONS
252     --config-file
253         specifies the opensrf.xml file
254
255     --lib-hostname
256         REQUIRED: hostname for the catalog (e.g "https://example.com")
257
258     --prefix
259         filename to add as a prefix to the generated set of sitemap files
260
261     --date-floor
262         a date in YYYY-MM-DD format that specifies the minimum date that
263         should be reflected for when a record was last updated; useful if
264         you enrich or change the HTML without changing records. Defaults
265         to 2010-01-01
266
267     --lib-shortname
268         include all records for the specified library and its children;
269         defaults to all records
270
271 EXAMPLES
272    This script will normally be run as a cron job by the opensrf user from
273    the web root directory.
274
275    sitemap_generator --lib-hostname https://example.com --lib-shortname BR1 \
276       --prefix example_
277
278    This generates a set of sitemap files like so:
279      * example_sitemapindex.xml
280      * example_sitemap1.xml
281      * example_sitemap2.xml
282      * ...
283
284 HERE
285 }
286