~vcs-imports/wacs/trunk

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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
#!/usr/bin/perl
#
# WacsExport
#
#    (C) Copyright 2006,2007,2008,2011 B King
#    This file is part of WACS.
#
#    WACS 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.
#
#    WACS 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.
#
#    You should have received a copy of the GNU General Public License
#    along with WACS.  If not, see <http://www.gnu.org/licenses/>.
#

use Wacs;
use DBI;
use XML::Simple;
use Data::Dumper;
use MIME::Base64;

# initialise Wacs
read_conf;
# check_auth not needed for console apps

# global variables
$modelno=0;
$confidential=1;
$expimages=1;
$maximg=1049600;	# maximum image size (1024*1025 = 1MB)
$actsize=0;
$debug=conf_get_attr("debug","app_wacsexport");
#
# MAIN
#
setup( "wacsexport" );
if( $ARGV[0] ne "" )
{
	if( $ARGV[0] eq "--noimages" )
	{
		$expimages=0;
		shift @ARGV;
	}
	$modelno = $ARGV[0];
}
else
{
	print STDERR "usage: wacsexport [--noimages] <modelno>\n";
	exit( -1 );
}
exportmodel( $modelno );
exit( 0 );

#
# SUB-ROUTINES
#
sub setup ( $ ) 
{
	my( $myname )=@_;

	# database initialisation
	$dbienv = conf_get_attr( "database","dbienvvar" );
	if( $dbienv ne "" )
	{
		$ENV{$dbienv}= conf_get_attr( "database","dbienvvalue" );
	}
	$dbhandle=DBI->connect( conf_get_attr("database","dbiconnect"),
			conf_get_attr("database","dbuser"),
			conf_get_attr("database","dbpass") ) ||
	die("Can't connect to database\nReason given was $DBI::errstr\n");

	# tune DBI parameters - insist on lower case fieldnames
	$dbhandle->{FetchHashKeyName} = "NAME_lc";
}

sub exportmodel( $ ) 
{
	my( $expmodel )=@_;
	my( $expsql, $expcsr, $exphash, $idsql, $idcsr, $idhash );
	my( $dlsql, $dlcsr, $dlhash );
	my( $modfile, $wacsxml, $wacsdata, $imghash, $bimghash );
	my( @idmaps, @dnlrecs );

	# initialise wacsdata
	$wacsdata= {};

	# prepare and execute for models table
	$expsql = "select * from ".conf_get_attr("tables","models")." ";
	$expsql.= "where modelno = ? ";
	$expcsr = $dbhandle->prepare( $expsql );
	$expcsr->execute( $expmodel );

	if( $DBI::errstr ne "" )
	{
		print STDERR "SQL Croaked\n";
		print STDERR "Error was: $DBI::errstr\n";
		print STDERR "Query was: $expsql\n";
		exit( -1 );
	}

	# prepare and execute for idmap table
	$idsql = "select * from ".conf_get_attr("tables","idmap")." ";
	$idsql.= "where imodelno = ? ";
	$idcsr = $dbhandle->prepare( $idsql );
	$idcsr->execute( $expmodel );

	if( $DBI::errstr ne "" )
	{
		print STDERR "SQL Croaked\n";
		print STDERR "Error was: $DBI::errstr\n";
		print STDERR "Query was: $idsql\n";
		exit( -1 );
	}

	# prepare and execute for download table
	$dlsql = "select * from ".conf_get_attr("tables","download")." ";
	$dlsql.= "where dmodelno = ? ";
	$dlcsr = $dbhandle->prepare( $dlsql );
	$dlcsr->execute( $expmodel );

	if( $DBI::errstr ne "" )
	{
		print STDERR "SQL Croaked\n";
		print STDERR "Error was: $DBI::errstr\n";
		print STDERR "Query was: $dlsql\n";
		exit( -1 );
	}

	# fetch the model record
	$exphash = $expcsr->fetchrow_hashref;
	if( $exphash eq undef )
	{
		# nothing found - abort
		print STDERR "Failed to find record for model $expmodel\n";
		exit( -1 );
	}
	# remove any contact information before exporting
	if( $confidential == 1 )
	{
		$exphash->{mcontact} = undef;
	}
	# reformat the dates into Oracle style if coming from MySQL or 
	# other databases
	if( conf_get_attr("database","dateformat") ne "DD-MON-YYYY" )
	{
		# we need to convert dates
		$exphash->{madded} = convertdate( $exphash->{madded} );
		$exphash->{mamended} = convertdate( $exphash->{mamended} );
	}
	# prepare the model data for XML output
	if( $debug > 3 )
	{
		print STDERR "Result: ".Dumper( $exphash )."\n";
	}
	$wacsdata->{model} = $exphash;

	# export the JPEGs (unless we've been asked not to)
	if( $expimages )
	{
		# first normal size - 120 x 156
		$actsize=0;  # global - updated by getimage
		$imghash->{filename} = $exphash->{mimage};
		$imghash->{encoding} = "jpeg";
		$imghash->{type} = "standard";
		$imghash->{data} = getimage( "standard", $exphash->{mimage} );
		$imghash->{bytes} = $actsize;   # size of the image
		if( $debug > 1 )
		{
			print STDERR "Image (in base64): ".Dumper($imghash).
					"\n";
		}
		$wacsdata->{image} = $imghash;

		# now, if it exists, the big icon
		$actsize=0;  # global - updated by getimage
		if( $exphash->{mbigimage} ne "" &&
		    $exphash->{mbigimage} ne undef )
		{
			$bimghash->{filename} = $exphash->{mbigimage};
			$bimghash->{encoding} = "jpeg";
			$bimghash->{type} = "big";
			$bimghash->{data} = getimage( "big",
						$exphash->{mbigimage} );
			$bimghash->{bytes} = $actsize;   # size of the image
			if( $debug > 3 )
			{
				print STDERR "Image (in base64): ".
						Dumper($bimghash)."\n";
			}
			$wacsdata->{bigimage} = $bimghash;
		}
	}

	# export the idmaps
	@idmaps = ();
	while( $idhash = $idcsr->fetchrow_hashref )
	{
		# reformat the dates into Oracle style if coming from MySQL or 
		# other databases
		if( conf_get_attr("database","dateformat") ne "DD-MON-YYYY" )
		{
			# we need to convert dates
			$idhash->{ichecked} = convertdate($idhash->{ichecked});
			$idhash->{ichanged} = convertdate($idhash->{ichanged});
			$idhash->{iadded} = convertdate($idhash->{iadded});
			$idhash->{iamended} = convertdate($idhash->{iamended});
		}
		if( $debug > 2 )
		{
			print STDERR "IdMap: ".Dumper( $idhash )."\n";
		}
		push( @idmaps, $idhash );
	}
	$wacsdata->{idmap} = [ @idmaps ];

	# export the download records
	@dnlrecs = ();
	while( $dlhash = $dlcsr->fetchrow_hashref )
	{
		# reformat the dates into Oracle style if coming from MySQL or 
		# other databases
		if( conf_get_attr("database","dateformat") ne "DD-MON-YYYY" )
		{
			# we need to convert dates
			$dlhash->{dpulled} = convertdate($dlhash->{dpulled});
			$dlhash->{dadded} = convertdate($dlhash->{dadded});
			$dlhash->{damended} = convertdate($dlhash->{damended});
		}
		if( $debug > 3 )
		{
			print STDERR "Download: ".Dumper( $dlhash )."\n";
		}
		push( @dnlrecs, $dlhash );
	}
	$wacsdata->{download} = [ @dnlrecs ];

	# add a few tags
	$wacsdata->{version} = "WACS_EXPORT_VERSION_2"; # indicates 0.9 schema
	$wacsdata->{coreschema} = "models";
	$wacsdata->{exportdate} = gettoday( 'format'=>"DD-MON-YYYY" );
	$wacsdata->{exportspec} = $expmodel.": ".$exphash->{mname};

	# give the file a .xml extension to help those people with
	# windows somewhere in the chain...
	$modfile = $exphash->{mname}."-".$expmodel.".xml";
	$modfile =~ s/\s//g;
	$wacsxml = XMLout( $wacsdata, XMLDecl => 1, SuppressEmpty=> 1,
			  NoAttr=> 1, RootName=>'WACS_model_data' );

	# open the output file
	open( XMODEL, "> $modfile ") ||
		die( "Can't write model XML file: $modfile\n");
	# send it all out
	print XMODEL $wacsxml;
	# all done - close output file
	close XMODEL;
	# informational report
	if( $debug > 0 )
	{
		print STDERR "Wrote ".$modfile." at ".$wacsdata->{exportdate}.
				"\n";
	}
}

sub getimage( $$ )
{
	my( $isize, $ifname )=@_;
	my( $fpath, $result );

	# which icon and sanity checks
	if( $isize eq "standard" )
	{
		$fpath = conf_get_attr("fsloc","modicons")."/".$ifname ;
	}
	elsif( $isize eq "big" )
	{
		$fpath = conf_get_attr("fsloc","modbigicons")."/".$ifname ;
	}
	else
	{
		print STDERR "unrecognised size request: $isize\n";
		return( undef );
	}

	# is the icon file there?
	if( ! -f $fpath )
	{
		print STDERR "can't find image file at $fpath.\n";
		return( undef );
	}

	open( IMAGE, "< ".$fpath ) ||
		die( "Can't open icon: ".$fpath."\n" );
	$actsize = read( IMAGE, $result, $maximg );
	close( IMAGE );

	return( encode_base64( $result ));
}

sub convertdate( $ )
{
	my( $indate )=@_;
	my( $day, $month, $year, $outdate );
	my( @monnames )=('Err','Jan','Feb','Mar','Apr','May','Jun','Jul',
		'Aug','Sep','Oct','Nov','Dec' );

	if( $indate eq "" )
	{
		return undef;
	}
	( $day, $month, $year ) = timecomps( $indate );
	if( $year < 100 )
	{
		$year += 2000;
	}
	$outdate = sprintf( "%-2.2d-%-3.3s-%-4.4d", $day, uc($monnames[$month]),
				$year );
	if( $debug > 0 )
	{
		print STDERR "convertdate: ".$indate." became ".$outdate."\n";
	}
	return( $outdate );
}