forked from Muzer/EanaEltu
/
MwfPlgNaviSQL.pm
79 lines (67 loc) · 3.4 KB
/
MwfPlgNaviSQL.pm
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
#------------------------------------------------------------------------------
# mwForum - Web-based discussion forum
# Copyright (c) 1999-2008 Markus Wichitill
#
# MwfPlgNaviSQL.pm - SQL Generation
# Copyright (c) 2010 Tobias Jaeggi, modified for SQL 2010 by Murray Colpman
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#------------------------------------------------------------------------------
package MwfPlgNaviSQL;
use strict;
use warnings;
our $VERSION = "2.22.1";
use Net::FTP;
sub create {
my %params = @_;
my $m = $params{m};
my $cfg = $m->{cfg};
my @words = @{$params{words}};
my $ftp = $params{ftp};
my @lcs = @{$params{languages}};
# Clean up
`rm -f $cfg->{EE}{tmpDir}/*.sql`;
# Open file handles to avoid iterating X times through @words
my $file;
open($file, '>::utf8', "$cfg->{EE}{tmpDir}/NaviData.sql") or $m->error("Could not open file! ($! for $cfg->{EE}{tmpDir}/NaviData.sql)");
$file or $m->error("could not open file");
print $file <<EOSQL;
-- IMPORTANT notices about this SQL file
-- Eana Eltu SQL data by Tobias Jaeggi (Tuiq, tuiq\@clonk2c.ch), Richard Littauer (Taronyu, richard\@learnnavi.org) and others is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License ( http://creativecommons.org/licenses/by-nc-sa/3.0/ ).
-- The full license text is available at http://creativecommons.org/licenses/by-nc-sa/3.0/legalcode .
-- localized table.
CREATE TABLE IF NOT EXISTS `localizedWords` (`id` int(11) NOT NULL,`languageCode` char(5) NOT NULL,`localized` text NULL,`partOfSpeech` varchar(100) NULL, UNIQUE KEY `idlc` (`id`,`languageCode`)) DEFAULT CHARSET=utf8;
-- meta table.
CREATE TABLE IF NOT EXISTS `metaWords` (`id` int(11) NOT NULL, `navi` varchar(100) NOT NULL,`ipa` varchar(100) NOT NULL,`infixes` varchar(100) NULL,`partOfSpeech` varchar(100) NOT NULL,PRIMARY KEY (`id`)) DEFAULT CHARSET=utf8;
-- TRUNCATE ACTION!
TRUNCATE TABLE `metaWords`;
TRUNCATE TABLE `localizedWords`;
-- INSERT MASSACRE
EOSQL
# Iterate through @words
for my $word (@words) {
print $file "INSERT INTO `metaWords` (`id`,`navi`,`ipa`,`infixes`,`partOfSpeech`) VALUES ('", $word->{id}, "',", $m->{dbh}->quote($word->{nav}), ",", $m->{dbh}->quote($word->{ipa}), ",", $m->{dbh}->quote($word->{svnav}), ",", $m->{dbh}->quote($word->{type}), ");";
# And now for each language...
for my $lc (@lcs) {
next if !$word->{$lc};
my $type = $word->{"type$lc"} ? $word->{"type$lc"} : $word->{type};
print $file "INSERT INTO `localizedWords` (`id`,`languageCode`,`localized`,`partOfSpeech`) VALUES ('", $word->{id}, "','", $lc, "',", $m->{dbh}->quote($word->{$lc}), ",", $m->{dbh}->quote($type), ");";
}
# We don't show mercy to poor editors.
#~ print $file "\n";
}
close $file;
# FTP
$ftp->delete("NaviData.sql");
$ftp->put("$cfg->{EE}{tmpDir}/NaviData.sql", "NaviData.sql") or $m->error("could not ftp: $!");
}
#-----------------------------------------------------------------------------
1;