[Bio] / Sprout / AttrDBRefresh.pl Repository:
ViewVC logotype

Annotation of /Sprout/AttrDBRefresh.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.12 - (view) (download) (as text)

1 : parrello 1.1 #!/usr/bin/perl -w
2 :    
3 :     =head1 AttrDBRefresh
4 :    
5 : parrello 1.5 This script performs useful function on the custom attributes database.
6 : parrello 1.1
7 :     The currently-supported command-line options are as follows.
8 :    
9 :     =over 4
10 :    
11 :     =item user
12 :    
13 :     Name suffix to be used for log files. If omitted, the PID is used.
14 :    
15 :     =item trace
16 :    
17 :     Numeric trace level. A higher trace level causes more messages to appear. The
18 : parrello 1.2 default trace level is C<3>.
19 : parrello 1.1
20 :     =item sql
21 :    
22 :     If specified, turns on tracing of SQL activity.
23 :    
24 :     =item background
25 :    
26 :     Save the standard and error output to files. The files will be created
27 :     in the FIG temporary directory and will be named C<err>I<User>C<.log> and
28 :     C<out>I<User>C<.log>, respectively, where I<User> is the value of the
29 :     B<user> option above.
30 :    
31 :     =item h
32 :    
33 :     Display this command's parameters and options.
34 :    
35 :     =item phone
36 :    
37 :     Phone number to message when the script is complete.
38 :    
39 : parrello 1.2 =item migrate
40 :    
41 : parrello 1.10 If specified, a comma-delimited list of attributes to be migrated from the old system to the
42 :     new one. The attributes will be erased before migration.
43 : parrello 1.2
44 : parrello 1.4 =item initializeAndClear
45 : parrello 1.2
46 : parrello 1.4 If specified, then the tables in the attribute database are dropped and re-created.
47 : parrello 1.2
48 : parrello 1.12 =item appendLoad
49 :    
50 :     If specified, existing keys will not be erased before loading the attribute
51 :     data. This option only makes sense if C<load> is specified.
52 :    
53 : parrello 1.5 =item load
54 :    
55 :     If specified, the name of a file containing attribute data to be loaded into the
56 :     system. The file is presumed to be tab-delimited. The first column must be the
57 :     object ID, the second the attribute key name, and the remaining columns the
58 :     attribute values. All attribute keys mentioned in the file will be erased before
59 : parrello 1.12 loading unless the C<appendLoad> option is specified.
60 : parrello 1.5
61 : parrello 1.7 =item loadKey
62 :    
63 :     If specified, the name of a tab-delimited file containing attribute key data. For each key,
64 :     there is a pair of lines. The first line contains the ID, data type, and description
65 :     of the key. The second line contains the marker C<#GROUPS> followed by zero or more
66 :     group names. The attribute will be connected to all the specified groups.
67 :    
68 : parrello 1.5 =item backup
69 :    
70 :     If specified, the name of a file into which all the attribute data should be
71 : parrello 1.7 dumped. The file itself will receive the attribute data in the format expected
72 :     by C<load>. A second file, consisting of the same file name with the suffix C<.key>,
73 :     will contain the attribute key data in the format expected by C<loadKey>.
74 : parrello 1.5
75 : parrello 1.6 =item hh_copy
76 :    
77 :     If specified, the attributes used by the collections system will be converted from the
78 :     old system to the new one. In the old system, these attributes are assigned either to
79 :     a genome or to the word "Subsystem". If they are attached to the word "Subsystem", then
80 :     the value is the subsystem name; otherwise, the value is ignored. In the new attribute
81 :     system, the attribute key is always "collection" and the value is the collection name.
82 :     Rather than attaching attributes to the word "Subsystem", we attach them to the
83 :     subsystem itself.
84 :    
85 : parrello 1.10 =item compare
86 :    
87 :     If specified, the name of a file to contain a comparision report. The comparison report
88 :     contains a table of the attribute keys and the number of values of the key in the new
89 :     and old systems, along with a list of the attributes and values not in the new system.
90 :     The report is formatted as a web page.
91 :    
92 :     =item summary
93 :    
94 :     If specified, the name of a file to contain a summary report. The summary report
95 :     contains a table of the attribute keys and the number of values of each. The report
96 :     is formatted as a web page.
97 :    
98 :     =item trimSpaces
99 :    
100 :     If specified, the name of an attribute value backup file. The file will be processed to
101 :     remove excess spaces. The fixed file will have the same name as the incoming backup
102 :     file with the extension <.fixed>. This new file can then be reloaded using the
103 :     C<load> option.
104 :    
105 : parrello 1.1 =back
106 :    
107 :     =cut
108 :    
109 :     use strict;
110 :     use Tracer;
111 :     use DocUtils;
112 :     use TestUtils;
113 :     use Cwd;
114 :     use File::Copy;
115 :     use File::Path;
116 :     use CustomAttributes;
117 : parrello 1.5 use ERDBLoad;
118 : parrello 1.2 use FIG;
119 : parrello 1.10 use CGI;
120 : parrello 1.1
121 :     # Get the command-line options and parameters.
122 : parrello 1.5 my ($options, @parameters) = StandardSetup([qw(CustomAttributes FIG) ],
123 : parrello 1.1 {
124 : parrello 1.2 trace => [3, "trace level"],
125 : parrello 1.4 initializeAndClear => ["", "if specified, the tables of the attribute database will be re-created"],
126 : parrello 1.2 migrate => ["", "if specified, attribute data will be migrated along with the object IDs"],
127 : parrello 1.1 phone => ["", "phone number (international format) to call when load finishes"],
128 : parrello 1.5 load => ["", "file from which to load attribute data"],
129 : parrello 1.7 loadKey => ["", "file from which to load attribute key data"],
130 : parrello 1.6 backup => ["", "file to which attribute data should be dumped"],
131 : parrello 1.8 hh_copy => ["", "if specified, the attributes used by the collections system will be converted"],
132 : parrello 1.10 compare => ["", "name of a file into which a comparison report will be written"],
133 :     summary => ["", "name of a file into which a summary report will be written"],
134 :     trimSpaces => ["", "if specified, the name of a backup file, which will be processed to remove excess spaces"],
135 : parrello 1.12 appendLoad => ["", "if specified, no data will be erased before loading from the load file"],
136 : parrello 1.1 },
137 :     "",
138 :     @ARGV);
139 :     # Set a variable to contain return type information.
140 :     my $rtype;
141 : parrello 1.10 # Create a CGI object.
142 :     my $cgi = CGI->new();
143 : parrello 1.1 # Insure we catch errors.
144 :     eval {
145 : parrello 1.3 # Insure we don't use the new attribute system for accessing the old attributes.
146 :     $FIG_Config::attrOld = 1;
147 : parrello 1.2 # Get the FIG object.
148 :     my $fig = FIG->new();
149 : parrello 1.4 # Get the attribute database.
150 :     Trace("Connecting to attribute database.") if T(2);
151 :     my $ca = CustomAttributes->new();
152 :     # Process according to the options selected.
153 : parrello 1.7 if ($options->{backup}) {
154 :     # Back up the attributes to the specified file.
155 :     my $backupFileName = $options->{backup};
156 :     Trace("Backing up attribute data.") if T(2);
157 :     my $stats = $ca->BackupAllAttributes($backupFileName);
158 :     Trace("Attribute backup statistics:\n" . $stats->Show()) if T(2);
159 :     Trace("Backing up key data.") if T(2);
160 :     $stats = $ca->BackupKeys("$backupFileName.key");
161 :     Trace("Key backup statistics:\n" . $stats->Show()) if T(2);
162 : parrello 1.5 }
163 : parrello 1.4 if ($options->{initializeAndClear}) {
164 :     # Create the tables.
165 :     $ca->CreateTables();
166 :     Trace("Tables recreated.") if T(2);
167 : parrello 1.2 }
168 :     if ($options->{migrate}) {
169 : parrello 1.10 # Migrate data from the old system.
170 :     Trace("Migrating attributes: $options->{migrate}.") if T(2);
171 :     # Create a statistics object to track our progress.
172 :     my $stats = Stats->new('keys', 'values');
173 :     # Loop through the attributes specified.
174 :     my @keys = split /\s*,\s*/, $options->{migrate};
175 :     for my $key (@keys) {
176 :     # Look for the key in the new system.
177 :     if (! $ca->Exists('AttributeKey', $key)) {
178 :     # If it's not found, we have an error.
179 :     Trace("Attribute key $key not found in new system.") if T(0);
180 :     $stats->AddMessage("$key not found.");
181 :     $stats->Add(badKeys => 1);
182 :     } else {
183 :     # Denote we're processing another key.
184 :     $stats->Add(keys => 1);
185 :     # Get the values from the old system.
186 :     Trace("Retrieving values for key $key.") if T(3);
187 :     my @valueList = $fig->get_attributes(undef, $key);
188 :     Trace("Erasing existing values for key $key.") if T(3);
189 :     # Erase the values in the new attribute system.
190 :     $ca->EraseAttribute($key);
191 :     Trace("Moving values for $key into new system.") if T(3);
192 :     # Loop through the old system values, adding them to the new system.
193 :     for my $valueRow (@valueList) {
194 :     my ($id, $key, @values) = @{$valueRow};
195 :     $ca->AddAttribute($id, $key, @values);
196 :     }
197 :     # Record what we did.
198 :     my $count = scalar(@valueList);
199 :     Trace("$count values migrated for $key.") if T(3);
200 :     $stats->Add(values => $count);
201 :     }
202 :     }
203 : parrello 1.5 Trace("Migration statistics:\n" . $stats->Show()) if T(2);
204 :     }
205 : parrello 1.6 if ($options->{hh_copy}) {
206 :     # Migrate the collections keys to the new system.
207 :     Trace("Migrating collections data.") if T(2);
208 :     my $stats = MigrateCollections($ca, $fig);
209 :     Trace("Collection statistics:\n" . $stats->Show()) if T(2);
210 :     }
211 : parrello 1.10 if ($options->{trimSpaces}) {
212 :     # Here we need to remove unnecessary spaces from an attribute values backup
213 :     # file. First, we open the input backup file.
214 :     my $fileName = $options->{trimSpaces};
215 :     my $ih = Open(undef, "<$fileName");
216 :     # Now we open the proposed output file.
217 :     my $oh = Open(undef, ">$fileName.fixed");
218 : parrello 1.11 # Create a statistics object to track our progress.
219 :     my $stats = Stats->new('lines', 'trims');
220 : parrello 1.10 Trace("Cleaning $fileName and copying to $fileName.fixed.") if T(2);
221 :     # Loop through the input file.
222 :     while (! eof $ih) {
223 :     # Get the next record in the input file.
224 : parrello 1.11 $stats->Add(lines => 1);
225 : parrello 1.10 my ($id, $key, @values) = Tracer::GetLine($ih);
226 :     # Trim the values.
227 : parrello 1.11 for my $value (@values) {
228 :     if ($value =~ /(\S.+\S)\s+/) {
229 :     $value = $1;
230 :     $stats->Add(trims => 1);
231 :     }
232 :     }
233 : parrello 1.10 # Write the result to the output file.
234 :     Tracer::PutLine($oh, [$id, $key, @values]);
235 :     }
236 :     # Close the files.
237 :     close $ih;
238 :     close $oh;
239 : parrello 1.11 Trace("$fileName.fixed is now a cleaned backup.\n" . $stats->Show()) if T(2);
240 : parrello 1.10 }
241 : parrello 1.7 if ($options->{loadKey}) {
242 :     # We want to load the attribute data from the specified file, but
243 :     # first we need to verify that the file exists.
244 :     my $loadFileName = $options->{loadKey};
245 :     if (! -f $loadFileName) {
246 :     Confess("Cannot load keys: file \"$loadFileName\" is not found or not a file.");
247 :     } else {
248 :     Trace("Loading key data from $loadFileName.") if T(2);
249 :     my $stats = $ca->RestoreKeys($loadFileName);
250 :     Trace("Load statistics:\n" . $stats->Show()) if T(2);
251 :     }
252 :     }
253 : parrello 1.5 if ($options->{load}) {
254 :     # We want to load the attribute data from the specified file, but
255 :     # first we need to verify that the file exists.
256 :     my $loadFileName = $options->{load};
257 :     if (! -f $loadFileName) {
258 :     Confess("Cannot load: file \"$loadFileName\" is not found or not a file.");
259 :     } else {
260 : parrello 1.12 # Set up options. We may need to specify the append option.
261 :     my %loadOptions;
262 :     if ($options->{appendLoad}) {
263 :     $loadOptions{append} = 1;
264 :     }
265 :     # Now we can load.
266 : parrello 1.5 Trace("Loading attribute data from $loadFileName.") if T(2);
267 : parrello 1.12 my $stats = $ca->LoadAttributesFrom($loadFileName, %loadOptions);
268 : parrello 1.5 Trace("Load statistics:\n" . $stats->Show()) if T(2);
269 :     }
270 :     }
271 : parrello 1.10 if ($options->{summary}) {
272 :     # Here we have a summary report. The value of the option is the name of a file that is to
273 :     # contain an html-formatted report. We start by getting a complete list of the keys and
274 :     # the associated counts.
275 :     my $keyCounts = GetAllKeys($ca);
276 :     # Buffer the lines in the following list.
277 :     my @lines = ();
278 :     # Start the table.
279 :     push @lines, $cgi->start_table({border => 2});
280 :     push @lines, $cgi->Tr($cgi->th({align => 'left'}, 'Key Name'), $cgi->th({align => 'right'}, 'Values'));
281 :     # Loop through the key hash, building row data.
282 :     for my $key (sort keys %{$keyCounts}) {
283 :     push @lines, $cgi->Tr($cgi->td({align => 'left'}, $key), $cgi->td({align => 'right'}, $keyCounts->{$key}));
284 :     }
285 :     # Close off the table.
286 :     push @lines, $cgi->end_table();
287 :     # Output the page.
288 :     WritePage($options->{summary}, 'Attribute Key Summary', \@lines);
289 :     }
290 :     if ($options->{compare}) {
291 :     # Here we have a comparison report. The value of the option is the name of a file that is
292 :     # to contain an html-formatted report. We need access to the SEED database to pull this
293 :     # off.
294 :     my $dbh = $fig->db_handle();
295 :     Trace("Retrieving key data.") if T(2);
296 :     # Get counts for all the keys in the old system.
297 :     my $oldKeys = $dbh->SQL("SELECT tag, COUNT(*) FROM attribute GROUP BY tag");
298 :     # Insure the query worked.
299 :     if (! defined($oldKeys)) {
300 :     Trace("Database error retrieving old system keys:" . $dbh->errstr) if T(0);
301 :     } else {
302 :     # Convert the key data into a hash.
303 :     my %oldKeyHash = map { $_->[0] => $_->[1] } @{$oldKeys};
304 :     # Get the counts for all the keys in the new system.
305 :     my $newKeyHashRef = GetNewKeyCounts($ca);
306 :     # We've got our data, so the next step is to start accumulating the lines of the web page.
307 :     Trace("Processing key table for new system.") if T(2);
308 :     my @lines = ();
309 :     # Start the first table.
310 :     push @lines, $cgi->h3("New System Keys");
311 :     push @lines, $cgi->start_table({border => 2});
312 :     push @lines, $cgi->Tr($cgi->th({align => 'left'}, 'Key Name'), $cgi->th({align => 'right'}, 'New Values'),
313 :     $cgi->th({align => 'right'}, 'Old Values'));
314 :     # Now we process the new keys. As we run through them, we'll delete matching keys from
315 :     # the old key hash. The remaining keys will be output as a missing-keys table.
316 :     for my $key (sort keys %{$newKeyHashRef}) {
317 :     # Get the new system count.
318 :     my $newCount = $newKeyHashRef->{$key};
319 :     # Default to a blank in the old system count column.
320 :     my $oldCount = '&nbsp;';
321 :     # Check to see if this key exists in the old system.
322 :     if (exists $oldKeyHash{$key}) {
323 :     # If it does, save its count and delete it from the old system hash.
324 :     $oldCount = $oldKeyHash{$key};
325 :     delete $oldKeyHash{$key};
326 :     }
327 :     # Output this table row.
328 :     push @lines, $cgi->Tr($cgi->td({align => 'left'}, $key), $cgi->td({align => 'right'}, $newCount),
329 :     $cgi->td({align => 'right'}, $oldCount));
330 :     }
331 :     # Close the table.
332 :     push @lines, $cgi->end_table();
333 :     # Now the remaining keys in the old key hash are missing from the new system. We create a new table
334 :     # to display them.
335 :     my @missingKeys = sort keys %oldKeyHash;
336 :     # Only do this if there's at least one missing key.
337 :     if (@missingKeys == 0) {
338 :     push @lines, $cgi->p("No missing keys found.");
339 :     } else {
340 :     Trace("Processing missing key table.") if T(2);
341 :     # Start the second table.
342 :     push @lines, $cgi->h3("Keys Missing from New System");
343 :     push @lines, $cgi->start_table({border => 2});
344 :     push @lines, $cgi->Tr($cgi->th({align => 'left'}, 'Key Name'), $cgi->td({align => 'right'}, 'Values'));
345 :     # Loop through the missing keys, writing them to the table.
346 :     for my $key (@missingKeys) {
347 :     push @lines, $cgi->Tr($cgi->td({align => 'left'}, $key), $cgi->td({align => 'right'}, $oldKeyHash{$key}));
348 :     }
349 :     # Close the table.
350 :     push @lines, $cgi->end_table();
351 :     }
352 :     # Write the web page.
353 :     WritePage($options->{compare}, 'Attribute System Comparison', \@lines);
354 :     }
355 :     }
356 : parrello 1.2 Trace("Processing complete.") if T(2);
357 : parrello 1.1 };
358 :     if ($@) {
359 :     Trace("Script failed with error: $@") if T(0);
360 :     $rtype = "error";
361 :     } else {
362 :     Trace("Script complete.") if T(2);
363 :     $rtype = "no error";
364 :     }
365 :     if ($options->{phone}) {
366 :     my $msgID = Tracer::SendSMS($options->{phone}, "RefreshAttrDB terminated with $rtype.");
367 :     if ($msgID) {
368 :     Trace("Phone message sent with ID $msgID.") if T(2);
369 :     } else {
370 :     Trace("Phone message not sent.") if T(2);
371 :     }
372 :     }
373 :    
374 : parrello 1.4 =head3 MigrateAttributes
375 :    
376 : parrello 1.6 C<< my $stats = MigrateAttributes($ca, $fig, $preserve); >>
377 : parrello 1.4
378 :     Migrate all the attributes data from the specified FIG instance. This is a long, slow
379 :     method used to convert the old attribute data to the new system. Only attribute
380 :     keys that are already in the database will be loaded, and they will completely
381 :     replace the existing values for those keys. Therefore, it is very important that the
382 :     FIG instance not be connected to the attribute database.
383 :    
384 :     =over 4
385 :    
386 :     =item ca
387 :    
388 :     B<CustomAttributes> object used to access the attribute database.
389 :    
390 :     =item fig
391 :    
392 :     A FIG object that can be used to retrieve attributes for migration purposes.
393 :    
394 : parrello 1.6 =item preserve (optional)
395 :    
396 :     A comma-delimited list of attributes that are not to be migrated.
397 :    
398 : parrello 1.4 =item RETURN
399 :    
400 :     Returns a statistical object for the load process.
401 :    
402 :     =back
403 :    
404 :     =cut
405 :    
406 :     sub MigrateAttributes {
407 :     # Get the parameters.
408 : parrello 1.6 my ($ca, $fig, $preserve) = @_;
409 : parrello 1.4 # Create the return value.
410 : parrello 1.5 my $retVal = Stats->new('keysIn');
411 :     # Create a loader for the value table.
412 :     my $hasValueFor = ERDBLoad->new($ca, 'HasValueFor', $FIG_Config::temp);
413 :     # Create a hash for the target objects.
414 :     my %targetObjectHash = ();
415 : parrello 1.6 # Get a list of the attributes we're to preserve.
416 :     my %preserve = ();
417 :     if (defined $preserve) {
418 :     %preserve = map { $_ => 1 } split /\s*,\s*/, $preserve;
419 :     }
420 :     # Put the preserved keys into the load file.
421 :     for my $key (keys %preserve) {
422 :     Trace("Preserving key $key.") if T(3);
423 :     my @newValues = $ca->GetAttributes(undef, $key);
424 :     Trace(scalar(@newValues) . " values of $key will be preserved.");
425 :     # Put the values into the load file.
426 :     PutValue($hasValueFor, $ca, @newValues);
427 :     }
428 : parrello 1.4 # Get a list of all our attribute keys.
429 : parrello 1.6 my @allKeys = $ca->GetFlat(['AttributeKey'], "", [], 'AttributeKey(id)');
430 :     # Delete the preserved keys.
431 :     my @keys = grep { ! $preserve{$_} } @allKeys;
432 :     # Loop through the reset, building the load files.
433 : parrello 1.4 for my $key (@keys) {
434 :     Trace("Migrating key $key.") if T(3);
435 :     $retVal->Add(keysIn => 1);
436 :     # Get all the values of the specified key.
437 :     my @oldValues = $fig->get_attributes(undef, $key);
438 :     my $count = scalar(@oldValues);
439 :     Trace("$count values found for $key in source system.") if T(3);
440 : parrello 1.6 # Put the values into the load file.
441 :     PutValue($hasValueFor, $ca, @oldValues);
442 : parrello 1.4 }
443 : parrello 1.5 # Close and finish the loads to upload the data.
444 :     Trace("Closing value table.") if T(2);
445 :     my $hvfStats = $hasValueFor->FinishAndLoad();
446 :     Trace("Statistics from value table load:\n" . $hvfStats->Show()) if T(2);
447 :     # Merge the statistics.
448 :     $retVal->Accumulate($hvfStats);
449 : parrello 1.4 # Return the statistics object.
450 :     return $retVal;
451 :     }
452 :    
453 : parrello 1.6 =head3 PutValue
454 :    
455 :     C<< PutValue($hasValueFor, $ca, @values); >>
456 :    
457 :     Put the values from an attribute value list into a HasValueFor load file.
458 :    
459 :     =over 4
460 :    
461 :     =item hasValueFor
462 :    
463 :     Load object for the HasValueFor table.
464 :    
465 :     =item ca
466 :    
467 :     A CustomAttribute object. We get the splitter value from it.
468 :    
469 :     =item value
470 :    
471 :     A list of tuples, each consisting of an object ID, a key name, and one or more values.
472 :    
473 :     =back
474 :    
475 :     =cut
476 :    
477 :     sub PutValue {
478 :     # Get the parameters.
479 :     my ($hasValueFor, $ca, @values) = @_;
480 :     # Loop through the value rows.
481 :     for my $row (@values) {
482 :     # Get this row's data.
483 :     my ($id, $key, @values) = @{$row};
484 :     # Format the values.
485 :     my $valueString = join($ca->{splitter}, @values);
486 :     # Add the value.
487 :     $hasValueFor->Put($key, $id, $valueString);
488 :     }
489 :     }
490 :    
491 :     =head3 MigrateCollections
492 :    
493 :     C<< my $stats = MigrateCollections($ca, $fig); >>
494 :    
495 :     This method copies the collection data from the specified FIG object and stores it as values
496 :     of the C<collection> attribute in the specified custom attribute database.
497 :    
498 :     =over 4
499 :    
500 :     =item ca
501 :    
502 :     Custom attribute database into which the collections are to be stored.
503 :    
504 :     =item fig
505 :    
506 :     FIG object from which the collection attributes are to be harvested.
507 :    
508 :     =item RETURN
509 :    
510 :     Returns a statistics object with informatino about the migration.
511 :    
512 :     =back
513 :    
514 :     =cut
515 :    
516 :     sub MigrateCollections {
517 :     # Get the parameters.
518 :     my ($ca, $fig) = @_;
519 :     # Declare the return variable.
520 :     my $retVal = Stats->new();
521 :     # Get the collection names.
522 :     my @collections = qw(higher_plants eukaryotic_ps nonoxygenic_ps hundred_hundred functional_coupling_paper ecoli_essentiality_paper);
523 :     # Erase the current collection date.
524 :     $ca->EraseAttribute('collection');
525 :     # Loop through the collection attributes.
526 :     for my $cname (@collections) {
527 :     $retVal->Add(collection => 1);
528 :     # Get this attribute from the old system.
529 :     my @rows = $fig->get_attributes(undef, $cname);
530 :     # Loop through its values.
531 :     for my $row (@rows) {
532 :     $retVal->Add($cname => 1);
533 :     # Determine the object key.
534 :     my $objectID = ($row->[0] eq 'Subsystem' ? $row->[2] : $row->[0]);
535 :     $ca->AddAttribute($objectID, 'collection', $cname);
536 :     }
537 :     }
538 :     # Return the statistics.
539 :     return $retVal;
540 :     }
541 :    
542 : parrello 1.10 =head3 GetAllKeys
543 :    
544 :     C<< my @keys = GetAllKeys($ca); >>
545 :    
546 :     Return a sorted list of the attribute keys.
547 :    
548 :     =over 4
549 :    
550 :     =item ca
551 :    
552 :     CustomAttributes object used to access the database.
553 :    
554 :     =item RETURN
555 :    
556 :     Returns a sorted list of all the attribute keys.
557 :    
558 :     =back
559 :    
560 :     =cut
561 :    
562 :     sub GetAllKeys {
563 :     # Get the parameters.
564 :     my ($ca) = @_;
565 :     # Get the attribute data.
566 :     my %keyData = $ca->GetAttributeData('name', '');
567 :     # Sort the keys.
568 :     my @retVal = sort keys %keyData;
569 :     # Return the result.
570 :     return @retVal;
571 :     }
572 :    
573 :     =head3 OpenPage
574 :    
575 :     C<< my $fh = OpenPage($fileName, $title); >>
576 :    
577 :     Start writing an HTML page to a file and return the file handle.
578 :    
579 :     =over 4
580 :    
581 :     =item fileName
582 :    
583 :     Name of the file to which the page will be written.
584 :    
585 :     =item title
586 :    
587 :     Title for the page.
588 :    
589 :     =item RETURN
590 :    
591 :     Returns the file handle for writing the rest of the page.
592 :    
593 :     =back
594 :    
595 :     =cut
596 :    
597 :     sub OpenPage {
598 :     # Get the parameters.
599 :     my ($fileName, $title) = @_;
600 :     # Open the file.
601 :     my $retVal = Open(undef, ">$fileName");
602 :     # Write out the HTML headers.
603 :     print $retVal $cgi->start_html(-title => $title, -BGCOLOR => 'silver');
604 :     print $retVal "\n";
605 :     # Return the file handle.
606 :     return $retVal;
607 :     }
608 :    
609 :     =head3 GetNewKeyCounts
610 :    
611 :     C<< my %counts = GetNewKeyCounts($ca); >>
612 :    
613 :     Return a hash mapping attribute key names to counts.
614 :    
615 :     =over 4
616 :    
617 :     =item ca
618 :    
619 :     CustomAttributes object for accessing the attribute database.
620 :    
621 :     =item RETURN
622 :    
623 :     Returns a reference to a hash mapping each key name to a count of the key's values.
624 :    
625 :     =back
626 :    
627 :     =cut
628 :    
629 :     sub GetNewKeyCounts {
630 :     # Get the parameters.
631 :     my ($ca) = @_;
632 :     # Declare the return variable.
633 :     my $retVal = {};
634 :     # Get all of the keys.
635 :     my @keys = GetAllKeys($ca);
636 :     # Loop through the list, filling the hash.
637 :     for my $key (@keys) {
638 :     my $count = $ca->GetCount(['HasValueFor'], "HasValueFor(from-link) = ?", [$key]);
639 :     $retVal->{$key} = $count;
640 :     }
641 :     # Return the result.
642 :     return $retVal;
643 :     }
644 :    
645 :     =head3 WritePage
646 :    
647 :     C<< WritePage($fileName, $title, \@lines); >>
648 :    
649 :     Write the specified lines to the specified file as a web page. The lines are assumed to be raw
650 :     HTML body text. They will be preceded by a standard HTML header and followed by a standard
651 :     HTML footer.
652 :    
653 :     =over 4
654 :    
655 :     =item fileName
656 :    
657 :     Name of the output file.
658 :    
659 :     =item title
660 :    
661 :     Title for the web page.
662 :    
663 :     =item lines
664 :    
665 :     Reference to a list of lines of HTML.
666 :    
667 :     =back
668 :    
669 :     =cut
670 :    
671 :     sub WritePage {
672 :     # Get the parameters.
673 :     my ($fileName, $title, $lines) = @_;
674 :     # Open the file and write the header to it. The header includes everything up to and including
675 :     # the BODY tag.
676 :     Trace("Writing web page to $fileName.") if T(2);
677 :     my $oh = OpenPage($fileName, $title);
678 :     # Write the lines one at a time.
679 :     for my $line (@{$lines}) {
680 :     print $oh "$line\n";
681 :     }
682 :     # Write the HTML footer.
683 :     print $oh $cgi->end_html();
684 :     # Close the output file.
685 :     close $oh;
686 :     Trace("Web page created in $fileName.") if T(2);
687 :     }
688 :    
689 : parrello 1.1 1;

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3