line
stmt
bran
cond
sub
pod
time
code
1
package WWW::HyperGlossary::Base;
2
1
1
54562
use Class::Std;
1
33726
1
7
3
1
1
1286
use Class::Std::Utils;
1
4866
1
9
4
1
1
3265
use DBI;
1
20742
1
92
5
1
1
976
use DBIx::MySperql qw(DBConnect SQLExec $dbh);
1
1528
1
142
6
1
1
6
use Digest::MD5 qw (md5_hex);
1
1
1
92
7
8
1
1
5
use warnings;
1
1
1
18
9
1
1
3
use strict;
1
1
1
20
10
1
1
4
use Carp;
1
1
1
81
11
12
1
1
5
use version; our $VERSION = qv('0.0.2');
1
2
1
7
13
14
{
15
my %data_of : ATTR( );
16
17
sub BUILD {
18
0
0
0
my ($self, $ident, $arg_ref) = @_;
19
20
# my $data = IOSea::Hyperglossary::Words->new();
21
# $data_of{$ident} = $data->get_data();
22
23
0
return;
24
}
25
26
sub get_languages {
27
0
0
0
my ( $self ) = @_;
28
0
my $sql = "select language_id, language from hg_languages order by language asc";
29
0
return SQLExec( $sql, '\@@' );
30
}
31
32
sub get_definition_types {
33
0
0
0
my ( $self ) = @_;
34
0
my $sql = "select definition_type_id, definition_type from hg_definition_types order by definition_type asc";
35
0
return SQLExec( $sql, '\@@' );
36
}
37
38
sub get_categories {
39
0
0
0
my ( $self ) = @_;
40
0
my $sql = "select category_id, category from hg_categories order by category asc";
41
0
return SQLExec( $sql, '\@@' );
42
}
43
44
sub get_category_words {
45
0
0
0
my ( $self, $category_id ) = @_;
46
0
my $sql = "select hg_category_words.category_word_id, hg_words.word from hg_category_words, hg_words where hg_category_words.category_id = $category_id and hg_category_words.word_id = hg_words.word_id";
47
0
return SQLExec( $sql, '\@@' );
48
}
49
50
sub get_set_words {
51
0
0
0
my ( $self, $category_id, $set_id ) = @_;
52
0
my $sql = "select hg_category_words.category_word_id, hg_words.word from hg_category_words, hg_words where hg_category_words.category_id = $category_id and hg_category_words.word_id = hg_words.word_id and hg_category_words.set_id = '$set_id' order by length(hg_words.word) desc";
53
0
return SQLExec( $sql, '\@@' );
54
}
55
56
sub fill_url {
57
0
0
0
my ( $self, $url ) = @_;
58
59
# Fix up url so it doesn't cause an error message
60
# add http:// if the user didn't
61
# add trailing slash if bare domain e.g. www.google.com
62
# todo: See if URI library makes this all trivial
63
0
0
if ( !($url =~ /http:/) ) { $url = 'http://'.$url;}
0
64
0
0
if (! ($url =~ /\/.*\..*$/) ) {if ( $url =~ /\.\w*$/ ) { $url = $url.'/';}}
0
0
0
65
66
0
return $url;
67
}
68
69
70
sub _sql_escape {
71
0
0
my ( $self, $string ) = @_;
72
0
0
if ($string) { $string =~ s/(['"\\])/\\$1/g; }
0
73
0
return $string;
74
}
75
76
sub _html_to_sql {
77
0
0
my ( $self, $string ) = @_;
78
0
$string = $self->_html_unescape( $string );
79
0
$string = $self->_sql_escape( $string );
80
0
return $string;
81
}
82
83
sub _html_escape {
84
0
0
my ( $self, $string ) = @_;
85
0
$string =~ s/'/'/g;
86
0
$string =~ s/"/"/g;
87
0
return $string;
88
}
89
90
sub _html_encode {
91
0
0
my ( $self, $string ) = @_;
92
0
$string =~ s/ /%20/g;
93
0
$string =~ s/'/%27/g;
94
0
$string =~ s/\{/%7B/g;
95
0
$string =~ s/\}/%7D/g;
96
0
return $string;
97
}
98
99
sub _html_unescape {
100
0
0
my ( $self, $string ) = @_;
101
0
$string =~ s/'/'/g;
102
0
$string =~ s/"/"/g;
103
0
$string =~ s/%20/ /g;
104
0
return $string;
105
}
106
107
sub _phone_format {
108
0
0
my ( $self, $string ) = @_;
109
0
$string =~ s/(\d{3})(\d{3})(\d{4})/($1) $2-$3/;
110
0
return $string;
111
}
112
113
sub _phone_unformat {
114
0
0
my ( $self, $string ) = @_;
115
0
$string =~ s/[^\d]//g;
116
0
return $string;
117
}
118
119
sub _commify { # Perl Cookbook 2.17
120
0
0
my ( $self, $string ) = @_;
121
0
my $text = reverse $string;
122
0
$text =~ s/(\d\d\d)(?=\d)(?!\d*\.)/$1,/g;
123
0
return scalar reverse $text;
124
}
125
126
sub _build_html_select_options {
127
0
0
my ($self, $data, $current_id) = @_;
128
0
my ($html, $selected);
129
130
0
0
if (! $current_id) { $current_id = 0; $html .= " "; }
0
0
131
132
0
foreach my $datum (@{ $data }) {
0
133
0
my ($id, $label) = @$datum;
134
0
0
if ($id == $current_id) { $selected = ' selected'; } else { $selected = ''; }
0
0
135
0
$html .= " $label ";
136
}
137
138
0
return $html;
139
}
140
141
sub _get_files {
142
0
0
my ( $self, $root_dir ) = @_;
143
0
0
if (opendir(DIR, $root_dir)) {
144
0
my (@files);
145
0
my (@nodes) = (readdir(DIR));
146
147
0
foreach my $node (@nodes) {
148
0
0
if ($node =~ m/^\./) { next; }
0
149
150
0
my $pathnode = $root_dir . "/" . $node;
151
0
my @stat = stat($pathnode);
152
153
0
0
my $value = defined $stat[2] ? $stat[2] : '';
154
0
0
if ($value =~ /^[^1]/) {
155
0
push(@files, $node);
156
}
157
}
158
0
return @files;
159
} else {
160
0
return 0;
161
}
162
}
163
164
sub _get_dirs {
165
0
0
my ( $self, $path, $nestedflag) = @_;
166
167
# If the directory opens
168
0
0
if (opendir(DIR, $path)) {
169
# Read it
170
0
my (@dirs);
171
0
my (@nodes) = sort (readdir(DIR));
172
173
0
foreach my $node (@nodes) {
174
# Drop any dirs (or files) that start with a period
175
0
0
if ($node =~ m/^\./) { next; }
0
176
177
# Get file system node status
178
0
my @stat = stat($path . '/' . $node);
179
180
# if the first character of $mode is 1, then it is a dir
181
0
0
if ($stat[2] =~ /^1/) {
182
0
my $newpath = $path . "/" . $node;
183
0
push(@dirs, $newpath);
184
185
0
0
if ($nestedflag) {
186
0
my @subnodes = &GetDirs($newpath, $nestedflag);
187
0
push(@dirs, @subnodes);
188
}
189
}
190
}
191
0
return @dirs;
192
} else {
193
0
return 0;
194
}
195
}
196
197
sub _get_file_text {
198
0
0
my ( $self, $path_file_name ) = @_;
199
0
my ($text, $line);
200
0
0
if (-e $path_file_name) {
201
0
0
open (my $IN, '<', $path_file_name) || $self->_status( "(Get) Cannot open $path_file_name: $!" );
202
0
while ($line = <$IN>) { $text .= $line; }
0
203
0
0
close ($IN) || $self->_status( "(Get) Cannot close $path_file_name: $!" );
204
}
205
0
return $text;
206
}
207
208
sub _set_file_text {
209
0
0
my ( $self, $path_file_name, $text ) = @_;
210
0
0
open (my $OUT, '>', $path_file_name) || $self->_status( "(Set) Cannot open $path_file_name: $!" );
211
0
0
print {$OUT} $text || $self->_status( "(Set) Cannot write $path_file_name: $!" );
0
212
0
0
close ($OUT) || $self->_status( "(Set) Cannot close $path_file_name: $!" );
213
}
214
215
sub _add_file_text {
216
0
0
my ( $self, $path_file_name, $text ) = @_;
217
0
0
open (my $OUT, '>>', $path_file_name) || $self->_status( "(Add) Cannot open $path_file_name: $!" );
218
0
0
print {$OUT} $text || $self->_status( "(Add) Cannot write $path_file_name: $!" );
0
219
0
0
close ($OUT) || $self->_status( "(Add) Cannot close $path_file_name: $!" );
220
}
221
222
sub _status {
223
0
0
my ( $self, $msg ) = @_;
224
0
my $status_file = $self->get_status_filename();
225
0
0
open (my $OUT, '>>', $status_file) || croak( "(Status) Cannot open $status_file: $!" );
226
0
0
print {$OUT} " STATUS: $msg \n" || croak( "(Status) Cannot write $status_file: $!" );
0
227
0
0
close ($OUT) || croak( "(Status) Cannot close $status_file: $!" );
228
0
return;
229
}
230
231
}
232
233
1; # Magic true value required at end of module
234
__END__