| 1 |
#!/usr/bin/perl -w |
| 2 |
|
| 3 |
# Create dictionaries and affix rules palatable for PostgreSQL, using installed |
| 4 |
# myspell and hunspell dictionaries. |
| 5 |
# |
| 6 |
# (C) 2008-2009 Martin Pitt <[email protected]> |
| 7 |
# (C) 2012-2017 Christoph Berg <[email protected]> |
| 8 |
# |
| 9 |
# This program is free software; you can redistribute it and/or modify |
| 10 |
# it under the terms of the GNU General Public License as published by |
| 11 |
# the Free Software Foundation; either version 2 of the License, or |
| 12 |
# (at your option) any later version. |
| 13 |
# |
| 14 |
# This program is distributed in the hope that it will be useful, |
| 15 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 |
# GNU General Public License for more details. |
| 18 |
|
| 19 |
use strict; |
| 20 |
use warnings; |
| 21 |
my @srcdirs = ('/usr/share/hunspell', '/usr/share/myspell/dicts'); |
| 22 |
my $cachedir = '/var/cache/postgresql/dicts'; |
| 23 |
my $pgsharedir = '/usr/share/postgresql'; |
| 24 |
|
| 25 |
use PgCommon; |
| 26 |
|
| 27 |
# determine encoding of an .aff file |
| 28 |
sub get_encoding { |
| 29 |
open my $fh, '<', $_[0] or die "cannot open $_[0]: $!"; |
| 30 |
while (<$fh>) { |
| 31 |
if (/^SET ([\w-]+)\s*$/) { return $1; } |
| 32 |
} |
| 33 |
return undef; |
| 34 |
} |
| 35 |
|
| 36 |
umask 022; |
| 37 |
if ((system 'mkdir', '-p', $cachedir) != 0) { |
| 38 |
exit 1; |
| 39 |
} |
| 40 |
|
| 41 |
# keep track of all up to date files, so that we can clean up cruft |
| 42 |
my %current; |
| 43 |
|
| 44 |
print "Building PostgreSQL dictionaries from installed myspell/hunspell packages...\n"; |
| 45 |
for my $d (@srcdirs) { |
| 46 |
for my $aff (glob "$d/*.aff") { |
| 47 |
next if -l $aff; # ignore symlinks |
| 48 |
my $dic = substr($aff, 0, -3) . 'dic'; |
| 49 |
if (! -f $dic) { |
| 50 |
print STDERR "ERROR: $aff does not have corresponding $dic, ignoring\n"; |
| 51 |
next; |
| 52 |
} |
| 53 |
|
| 54 |
my $enc = get_encoding $aff; |
| 55 |
if (!$enc) { |
| 56 |
print STDERR "ERROR: no encoding defined in $aff, ignoring\n"; |
| 57 |
next; |
| 58 |
} |
| 59 |
|
| 60 |
my $locale = substr ((split '/', $aff)[-1], 0, -4); |
| 61 |
$locale =~ tr /A-Z/a-z/; |
| 62 |
|
| 63 |
$current{"$cachedir/$locale.affix"} = undef; |
| 64 |
$current{"$cachedir/$locale.dict"} = undef; |
| 65 |
|
| 66 |
# convert to UTF-8 and write to cache dir |
| 67 |
print " $locale\n"; |
| 68 |
if ((system 'iconv', '-f', $enc, '-t', 'UTF-8', '-o', |
| 69 |
"$cachedir/$locale.affix", $aff) != 0) { |
| 70 |
unlink "$cachedir/$locale.affix"; |
| 71 |
print STDERR "ERROR: Conversion of $aff failed\n"; |
| 72 |
next; |
| 73 |
} |
| 74 |
if ((system 'iconv', '-f', $enc, '-t', 'UTF-8', '-o', |
| 75 |
"$cachedir/$locale.dict", $dic) != 0) { |
| 76 |
unlink "$cachedir/$locale.affix"; |
| 77 |
unlink "$cachedir/$locale.dict"; |
| 78 |
print STDERR "ERROR: Conversion of $dic failed\n"; |
| 79 |
next; |
| 80 |
} |
| 81 |
|
| 82 |
# install symlinks to all versions >= 8.3 |
| 83 |
foreach my $v (get_versions) { |
| 84 |
next if $v < '8.3'; |
| 85 |
my $dest = "$pgsharedir/$v/tsearch_data"; |
| 86 |
next if ! -d $dest; |
| 87 |
$current{"$dest/$locale.affix"} = undef; |
| 88 |
$current{"$dest/$locale.dict"} = undef; |
| 89 |
next if -e "$dest/$locale.affix" && ! -l "$dest/$locale.affix"; |
| 90 |
next if -e "$dest/$locale.dict" && ! -l "$dest/$locale.dict"; |
| 91 |
unlink "$dest/$locale.affix"; |
| 92 |
unlink "$dest/$locale.dict"; |
| 93 |
symlink "$cachedir/$locale.affix", "$dest/$locale.affix"; |
| 94 |
symlink "$cachedir/$locale.dict", "$dest/$locale.dict"; |
| 95 |
} |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
# clean up files for locales which do not exist any more |
| 100 |
print "Removing obsolete dictionary files:\n"; |
| 101 |
foreach my $f (glob "$cachedir/*") { |
| 102 |
next if exists $current{$f}; |
| 103 |
print " $f\n"; |
| 104 |
unlink $f; |
| 105 |
} |
| 106 |
foreach my $f ((glob "$pgsharedir/*/tsearch_data/*.affix"), |
| 107 |
(glob "$pgsharedir/*/tsearch_data/*.dict")) { |
| 108 |
next unless -l $f; |
| 109 |
next if exists $current{$f}; |
| 110 |
print " $f\n"; |
| 111 |
unlink $f; |
| 112 |
} |
| 113 |
|
| 114 |
__END__ |
| 115 |
|
| 116 |
=head1 NAME |
| 117 |
|
| 118 |
pg_updatedicts - build PostgreSQL dictionaries from myspell/hunspell ones |
| 119 |
|
| 120 |
=head1 SYNOPSIS |
| 121 |
|
| 122 |
B<pg_updatedicts> |
| 123 |
|
| 124 |
=head1 DESCRIPTION |
| 125 |
|
| 126 |
B<pg_updatedicts> makes dictionaries and affix files from installed myspell |
| 127 |
and hunspell dictionary packages available to PostgreSQL for usage with tsearch |
| 128 |
and word stem support. In particular, it takes all I<*.dic> and I<*.aff> files |
| 129 |
from /usr/share/myspell/dicts/, converts them to UTF-8, puts them into |
| 130 |
/var/cache/postgresql/dicts/ with I<*.dict> and I<*.affix> suffixes, and |
| 131 |
symlinks them into /usr/share/postgresql/I<version>/tsearch_data/, where |
| 132 |
PostgreSQL looks for them. |
| 133 |
|
| 134 |
Through postgresql-common's dpkg trigger, this program is automatically run |
| 135 |
whenever a myspell or hunspell dictionary package is installed or upgraded. |
| 136 |
|
| 137 |
=head1 AUTHOR |
| 138 |
|
| 139 |
Martin Pitt L<E<lt>[email protected]<gt>> |