blib/script/ftw_new_app.pl untested

filter: Statementstmt 0.0% 0 / 69 Branchbran 0.0% 0 / 6 Conditioncond 0.0% 0 / 19 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 9 Podpod n/a totaltotal 0.0% 0 / 103 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 =head1 NAME
2  
3 ftw_new_app - create a new File::Tabular::Web application
4  
5 =head1 SYNOPSIS
6  
7 cp some/data.txt /path/to/http/htdocs/some/data.txt
8 perl ftw_new_app.pl -m field1 -m field2 -e \
9 -f "|" /path/to/http/htdocs/some/data.txt
10  
11 =head1 DESCRIPTION
12  
13 This script creates a skeleton for a Web application around
14 a tabular datafile. The concepts and basic setup instructions
15 are described in L<File::Tabular::Web>. After having created
16 the skeleton, you can navigate to URL C<http://myServer/some/data.ftw>.
17  
18  
19 =head1 CREATED FILES
20  
21 The script will create the following files, in the same directory
22 as the data file :
23  
24 =over
25  
26 =item data.ftw
27  
28 The application configuration file (also used as URL).
29  
30 =item data_wrapper.tt
31  
32 General wrapper template for the application, including the search form.
33  
34 =item data_home.tt
35  
36 Home page.
37  
38 =item data_short.tt
39  
40 Template for displaying result sets (short display of each record).
41  
42 =item data_long.tt
43  
44 Template for displaying detailed view of a single record.
45  
46 =item data_modif.tt
47  
48 HTML form for modifying a record.
49  
50 =item data_msg.tt
51  
52 Template for displaying a message (for example confirmation
53 of an update operation).
54  
55 =back
56  
57 All templates may be later be moved out of the C<htdocs> tree,
58 to a more appropriate location : you just have to specify the
59 new location in the configuration file (C<dir> variable in the C<[template]>
60 section).
61  
62  
63 =head1 OPTIONS
64  
65 Options may be abbreviated to the first letter, i.e.
66  
67 -m field1 -f "|"
68  
69 instead of
70  
71 -menu field1 -fieldSep "|"
72  
73  
74 =head2 -help
75  
76 Help to using this script.
77  
78 =head2 -menu field1 -menu field2 ...
79  
80 For each specified field, will inspect all values currently in the
81 data file, and creates an simple menu, like a spreadsheet
82 "automatic filter". However, this is currently merely static, so
83 if the data changes, menus will not be updated accordingly ---
84 it will improve in a future release.
85  
86 =head2 -fieldSep
87  
88 Specifies which character is the field separator in the data file.
89 Default is C<|>.
90  
91 =head2 -override
92  
93 Forces creation of the files, overriding previous files of
94 the same names.
95  
96 =head2 -editor username1 -editor username2
97  
98 Writes into the configuration file that the data is
99 editable by the specified usernames. Usernames must
100 correspond to names returned by your Apache
101 authentication layer. If no username is given,
102 the default value is C<*>, which means "editable by all".
103  
104 =cut
105  
106  
107  
108 use strict;
109 use warnings;
110 no warnings 'uninitialized';
111 use File::Basename;
112 use File::Tabular;
113 use Getopt::Long;
114 use Template;
115 use List::MoreUtils qw/uniq any all/;
116 use Pod::Usage;
117  
118 my %vars = ();
119  
120 my $field_sep;
121 my @menus;
122 my $override;
123 my $help;
124 my @editor;
125  
126 GetOptions("menu=s" => \@menus,
127 "fieldSep=s" => \$field_sep,
128 "override!" => \$override,
129 "help" => \$help,
130 "editor:s" => \@editor,
131 )
132 or pod2usage(-verbose => 0);
133  
134 pod2usage(-verbose => 1) if $help;
135  
136  
137 # open data file
138 my $file_name = $ARGV[0]
139 or pod2usage(-message => "$0 : NO DATA FILE");
140 @vars{qw/base dir suffix/} = fileparse($file_name, qr/\.[^.]*$/);
141 chdir $vars{dir} or die $!;
142 my $tab_file = File::Tabular->new("$vars{base}$vars{suffix}",
143 {fieldSep => $field_sep});
144 my @headers = $tab_file->headers;
145  
146  
147 # build menus
148 if (@menus) {
149 my %menu_values;
150  
151 # check that menus correspond to headers in datafile
152 foreach my $menu (@menus) {
153 any {$_ eq $menu } @headers or die "unknown menu field : $menu";
154 }
155  
156 while (my $row = $tab_file->fetchrow) {
157 foreach my $menu (@menus) {
158 my $val = $row->{$menu};
159 $menu_values{$menu}{$val} = 1;
160 }
161 }
162  
163 foreach my $menu (@menus) {
164 $vars{menus}{$menu} = [sort keys %{$menu_values{$menu}}];
165 }
166 }
167  
168  
169 # some data for templates
170 $vars{headers} = \@headers;
171 $vars{key_header} = $headers[0];
172 $vars{fieldSep} = $field_sep;
173 if (@editor) {
174 $editor[0] ||= "*"; # default
175 $vars{editor} = join ",", @editor;
176 }
177  
178  
179 # split DATA into template files (keys are filenames, values are tmpl contents)
180 local $/ = undef;
181 my (undef, %templates) = split /^__(.+?)__+\r?\n/m, <DATA>;
182  
183  
184  
185 # create files from templates
186  
187 # We will generate templates from templates, so there must be directives
188 # for first pass (template generation) and for second pass (runtime
189 # page generation). We use "{% .. %}" for first pass and default
190 # "[% .. %]" for second pass.
191 my %tmpl_config = (START_TAG => '{%',
192 END_TAG => '%}',
193 );
194  
195 my $tmpl = Template->new(\%tmpl_config);
196 while (my ($name, $content) = each %templates) {
197 my $output = sprintf $name, $vars{base};
198 $override or not -e $output or die "$output exists, will not clobber";
199 $tmpl->process(\$content, \%vars, $output) or die $tmpl->error();
200 }
201  
202  
203 #----------------------------------------------------------------------
204 # END OF MAIN PROGRAM. DATA SECTION BELOW CONTAINS THE TEMPLATES
205 #----------------------------------------------------------------------
206  
207  
208 __DATA__