| 1 | package Web::VWF::Display::table; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use version; our $VERSION = version->declare('0.003.0'); | ||
| 7 | |||
| 8 | use CGI::Info; | ||
| 9 | use Config::Auto; | ||
| 10 | use File::Spec; | ||
| 11 | use Text::xSV::Slurp; | ||
| 12 | use File::Slurp; # For read_file | ||
| 13 | |||
| 14 | # Read in a small flat file of ! delimted records and make them available | ||
| 15 | # as object methods so that they can be read from a page display routine or | ||
| 16 | # template | ||
| 17 | |||
| 18 | # An optional filename can be given, if it isn't then it's derived from the | ||
| 19 | # class name, which works most of the time | ||
| 20 | sub new { | ||
| 21 | my $proto = shift; | ||
| 22 | my %args = (ref($_[0]) eq 'HASH') ? %{$_[0]} : @_; | ||
| 23 | |||
| 24 | my $class = ref($proto) || $proto; | ||
| 25 | my $path; | ||
| 26 | |||
| 27 | if($ENV{'DOCUMENT_ROOT'}) { | ||
| 28 | $path = File::Spec->catdir( | ||
| 29 | $ENV{'DOCUMENT_ROOT'}, | ||
| 30 | File::Spec->updir(), | ||
| 31 | 'lib', | ||
| 32 | 'conf' | ||
| 33 | ); | ||
| 34 | } else { | ||
| 35 | $path = File::Spec->catdir( | ||
| 36 | $ENV{'HOME'}, | ||
| 37 | 'lib', | ||
| 38 | 'conf' | ||
| 39 | ); | ||
| 40 | } | ||
| 41 | my $info = $args{info} || CGI::Info->new(); | ||
| 42 | my $config; | ||
| 43 | eval { | ||
| 44 | $config = Config::Auto::parse($info->domain_name(), path => $path); | ||
| 45 | }; | ||
| 46 | if($@) { | ||
| 47 | die "Configuration error: $@: $path/", $info->domain_name(); | ||
| 48 | } | ||
| 49 | return bless { | ||
| 50 | _config => $config, | ||
| 51 | _info => $info, | ||
| 52 | _table => $args{table}, | ||
| 53 | }, $class; | ||
| 54 | } | ||
| 55 | |||
| 56 | sub get_file_path { | ||
| 57 | my $self = shift; | ||
| 58 | |||
| 59 | my $dir = $self->{_config}->{root_dir} || $self->{_info}->root_dir(); | ||
| 60 | $dir .= '/databases'; | ||
| 61 | |||
| 62 | my $filename; | ||
| 63 | |||
| 64 | if($self->{_table}) { | ||
| 65 | $filename = $self->{_table}; | ||
| 66 | } else { | ||
| 67 | $filename = ref($self); | ||
| 68 | } | ||
| 69 | $filename =~ s/::/\//g; | ||
| 70 | |||
| 71 | # The table name becomes part of a path, so it must stay inside databases/ | ||
| 72 | die "Invalid table name '$filename'" if(($filename =~ m{(?:\A|/)\.\.(?:/|\z)}) || ($filename =~ m{\A/})); | ||
| 73 | |||
| 74 | my $rc = "$dir/$filename"; | ||
| 75 | if((!-f $rc) || (!-r $rc)) { | ||
| 76 | die "Can't open $rc"; | ||
| 77 | } | ||
| 78 | return $rc; | ||
| 79 | } | ||
| 80 | |||
| 81 | sub columns { | ||
| 82 | my $self = shift; | ||
| 83 | |||
| 84 | return $self->load_data()->{'_columns'}; | ||
| 85 | } | ||
| 86 | |||
| 87 | sub fetch { | ||
| 88 | my $self = shift; | ||
| 89 | my %args = (ref($_[0]) eq 'HASH') ? %{$_[0]} : @_; | ||
| 90 | |||
| 91 | $self->load_data(); | ||
| 92 | |||
| 93 | my $key = $args{key}; | ||
| 94 | unless($key) { | ||
| 95 | # Return all the data as an array of hashes | ||
| 96 | return $self->{_data_as_array}; | ||
| 97 | } | ||
| 98 | # Return just one hash | ||
| 99 | return $self->{_data_as_hash}->{$key}; | ||
| 100 | } | ||
| 101 | |||
| 102 | sub fetch_as_hash { | ||
| 103 | my $self = shift; | ||
| 104 | |||
| 105 | return $self->load_data()->{'_data_as_hash'}; | ||
| 106 | } | ||
| 107 | |||
| 108 | sub load_data { | ||
| 109 | my $self = shift; | ||
| 110 | |||
| 111 | if($self->{_data_as_array} && $self->{_data_as_hash}) { | ||
| 112 | return $self; | ||
| 113 | } | ||
| 114 | my $filename = $self->get_file_path(); | ||
| 115 | unless(-r $filename) { | ||
| 116 | die "$filename: $!"; | ||
| 117 | } | ||
| 118 | # Slurp it all in, it won't be that big | ||
| 119 | # Remove comments and empty lines | ||
| 120 | my $in = join('', grep({ !/^\s*(#|$)/ } read_file($filename))); | ||
| 121 | |||
| 122 | my $data = xsv_slurp( | ||
| 123 | shape => 'aoh', | ||
| 124 | text_csv => { | ||
| 125 | sep_char => '!', | ||
| 126 | allow_loose_quotes => 1, | ||
| 127 | blank_is_undef => 1, | ||
| 128 | empty_is_undef => 1, | ||
| 129 | }, | ||
| 130 | string => \$in | ||
| 131 | ); | ||
| 132 | $self->{_data_as_array} = $data; | ||
| 133 | |||
| 134 | # Create list of columns | ||
| 135 | my $first = $self->{_data_as_array}[0]; | ||
| 136 | |||
| 137 | my @column_names = keys(%{$first}); | ||
| 138 | $self->{_columns} = \@column_names; | ||
| 139 | |||
| 140 | $self->{_data_as_hash} = xsv_slurp( | ||
| 141 | shape => 'hoh', | ||
| 142 | # FIXME: RT79478 | ||
| 143 | # key => $column_names[0], | ||
| 144 | key => 'key', | ||
| 145 | text_csv => { | ||
| 146 | sep_char => '!', | ||
| 147 | allow_loose_quotes => 1, | ||
| 148 | blank_is_undef => 1, | ||
| 149 | empty_is_undef => 1, | ||
| 150 | }, | ||
| 151 | string => \$in | ||
| 152 | ); | ||
| 153 | |||
| 154 | return $self; | ||
| 155 | } | ||
| 156 | |||
| 157 | # sub AUTOLOAD { | ||
| 158 | # my $self = shift; | ||
| 159 | # my $type = ref($self) or die "$self is not an object"; | ||
| 160 | # | ||
| 161 | # my $name = $AUTOLOAD; | ||
| 162 | # $name =~ s/.*://; # strip fully-qualified portion | ||
| 163 | # | ||
| 164 | # if($name eq 'DESTROY') { | ||
| 165 | # return; | ||
| 166 | # } | ||
| 167 | # | ||
| 168 | # $self->load_data(); | ||
| 169 | # | ||
| 170 | # warn $name; | ||
| 171 | # | ||
| 172 | # # unless (exists $self->{_permitted}->{$name} ) { | ||
| 173 | # # croak "Can't access `$name' field in class $type"; | ||
| 174 | # # } | ||
| 175 | # | ||
| 176 | # # if (@_) { | ||
| 177 | # # return $self->{$name} = shift; | ||
| 178 | # # } else { | ||
| 179 | # # return $self->{$name}; | ||
| 180 | # # } | ||
| 181 | # } | ||
| 182 | |||
| 183 | 1; | ||
| 184 | |||
| 185 | =encoding utf8 | ||
| 186 | |||
| 187 | =head1 NAME | ||
| 188 | |||
| 189 | Web::VWF::Display::table - Read a small flat file for a page or its template | ||
| 190 | |||
| 191 | =head1 VERSION | ||
| 192 | |||
| 193 | Version 0.003.0 | ||
| 194 | |||
| 195 | =head1 DESCRIPTION | ||
| 196 | |||
| 197 | B<This is not a page> and so it is not in the C<display> setting: it does not | ||
| 198 | inherit from L<Web::VWF::Display>. It is a helper for a page or template that | ||
| 199 | wants a small C<!>-separated file with a header line, read from | ||
| 200 | F<databases/NAME> in the site's C<root_dir>. Blank lines and lines starting | ||
| 201 | with C<#> are ignored. The site's own settings are found with L<Config::Auto> | ||
| 202 | in F<$DOCUMENT_ROOT/../lib/conf>, or F<$HOME/lib/conf>. | ||
| 203 | |||
| 204 | =head1 METHODS | ||
| 205 | |||
| 206 | =head2 new | ||
| 207 | |||
| 208 | Takes C<info> (a L<CGI::Info>, made if not given) and C<table>, the file's name, | ||
| 209 | which is the class name, with C</> for C<::>, if not given. Dies when the | ||
| 210 | configuration cannot be read. | ||
| 211 | |||
| 212 | =head2 get_file_path | ||
| 213 | |||
| 214 | The path of the file, which must be readable and inside F<databases/>. Dies | ||
| 215 | otherwise. | ||
| 216 | |||
| 217 | =head2 columns | ||
| 218 | |||
| 219 | The names of the columns, as an array reference. | ||
| 220 | |||
| 221 | =head2 fetch | ||
| 222 | |||
| 223 | With C<key>, the row with that first column as a hash reference; without, all the | ||
| 224 | rows as an array reference of hash references. | ||
| 225 | |||
| 226 | =head2 fetch_as_hash | ||
| 227 | |||
| 228 | All the rows as a hash reference keyed on the first column. | ||
| 229 | |||
| 230 | =head2 load_data | ||
| 231 | |||
| 232 | Reads the file once. The other methods call it. | ||
| 233 | |||
| 234 | =head3 API SPECIFICATION | ||
| 235 | |||
| 236 | =head4 INPUT | ||
| 237 | |||
| 238 | { info => { type => 'object', optional => 1 }, table => { type => 'string', optional => 1 } } | ||
| 239 | |||
| 240 | =head4 OUTPUT | ||
| 241 | |||
| 242 | { type => 'object', isa => 'Web::VWF::Display::table' } | ||
| 243 | |||
| 244 | =head1 AUTHOR | ||
| 245 | |||
| 246 | Nigel Horne, C<< <njh at nigelhorne.com> >> | ||
| 247 | |||
| 248 | =head1 LICENCE AND COPYRIGHT | ||
| 249 | |||
| 250 | Copyright 2010-2026 Nigel Horne. | ||
| 251 | |||
| 252 | Usage is subject to the GPL2 licence terms. | ||
| 253 | If you use it, | ||
| 254 | please let me know. | ||
| 255 | |||
| 256 | =cut |