| 1 | # | ||
| 2 | # Web::DataService::Diagnostic | ||
| 3 | # | ||
| 4 | # This module provides a role that is used by 'Web::DataService'. It implements | ||
| 5 | # routines for generating diagnostic output about the data service. | ||
| 6 | # | ||
| 7 | # Author: Michael McClennen | ||
| 8 | |||
| 9 | use strict; | ||
| 10 | |||
| 11 | package Web::DataService::Diagnostic; | ||
| 12 | |||
| 13 | use Carp qw(carp croak); | ||
| 14 | |||
| 15 | use Moo::Role; | ||
| 16 | |||
| 17 | |||
| 18 | our ($CWD); | ||
| 19 | |||
| 20 | our (%DIAG_PARAM) = ( show => 1, splat => 1 ); | ||
| 21 | |||
| 22 | # diagnostic_request ( @args ) | ||
| 23 | # | ||
| 24 | # Generate diagnostic output requested from the command-line. This is done by | ||
| 25 | # running the data service application with the following command-line | ||
| 26 | # arguments: | ||
| 27 | # | ||
| 28 | # app_name diag <path> <parameters> | ||
| 29 | # | ||
| 30 | # The path is used to select a data service node, in the same way as if | ||
| 31 | # responding to a request, and the parameters (using the same syntax as URL | ||
| 32 | # parameters) specify the exact diagnostic output to be generated. | ||
| 33 | # | ||
| 34 | # Examples: | ||
| 35 | # | ||
| 36 | # app_name diag /data/records/list 'show=digest' | ||
| 37 | # | ||
| 38 | # app_name diag /data/ 'show=fields&doc=short' | ||
| 39 | # | ||
| 40 | # The path argument is required, and so is the parameter "show". The | ||
| 41 | # parameter string must follow URL parameter syntax, so don't include any | ||
| 42 | # whitespace around the = and & signs. Possible values are: | ||
| 43 | # | ||
| 44 | # show=digest | ||
| 45 | # | ||
| 46 | # Generate a digest of the configuration of one or more data service | ||
| 47 | # nodes, serialized into YAML. This can be saved to a file and then | ||
| 48 | # analyzed by a separate program. The purpose of this functionality is to | ||
| 49 | # provide for the generation of a report summarizing the differences | ||
| 50 | # between the user interfaces of different data service versions | ||
| 51 | # (parameters, responses, formats, vocabularies, etc.), for the purposes | ||
| 52 | # of generating change logs and other documentation. By default, the node | ||
| 53 | # specified by the path argument is included in the digest, plus | ||
| 54 | # (recursively) every node that it links to. The digest will also include | ||
| 55 | # every output block, set, and ruleset linked by any of the nodes. | ||
| 56 | # | ||
| 57 | # The following additional parameters are available: | ||
| 58 | # | ||
| 59 | # node = <pattern> | ||
| 60 | # | ||
| 61 | # Only include nodes whose path matches the specified string, which | ||
| 62 | # may contain the standard shell wildcards * and ?. You can use this | ||
| 63 | # to select a subset of the nodes that would otherwise be included. | ||
| 64 | # | ||
| 65 | # show=fields | ||
| 66 | # | ||
| 67 | # Generate a report (as unformatted text) that tabulates all of the output | ||
| 68 | # field names matching the other parameters. This functionality can be | ||
| 69 | # used to make sure that field names and values are consistent between all | ||
| 70 | # of the different data service operations. The path argument is used only to | ||
| 71 | # select which data service to analyze, if the application defines more | ||
| 72 | # than one. Other parameters include: | ||
| 73 | # | ||
| 74 | # vocab = <vocab> | ||
| 75 | # | ||
| 76 | # Only report output field names from the specified vocabulary. If | ||
| 77 | # the specified vocabulary has the 'use_field_names' attribute, then | ||
| 78 | # the names of the underlying data fields will be used whenever no | ||
| 79 | # name was explicitly specified for this vocabulary. | ||
| 80 | # | ||
| 81 | # name = <pattern> | ||
| 82 | # | ||
| 83 | # Only report output field names which match the specified pattern. | ||
| 84 | # The pattern may contain the standard shell wildcards * and ?. | ||
| 85 | # | ||
| 86 | # data = <pattern> | ||
| 87 | # | ||
| 88 | # Only report output field names which are linked to a data field | ||
| 89 | # matching the specified pattern. The pattern may contain the standard | ||
| 90 | # shell wildcards * and ?. | ||
| 91 | # | ||
| 92 | |||
| 93 | sub diagnostic_request { | ||
| 94 | |||
| 95 | my ($ds, $request) = @_; | ||
| 96 | |||
| 97 | # Start by getting the request parameters, which will tell us which | ||
| 98 | # diagnostic operation was requested. | ||
| 99 | |||
| 100 | my $params = $Web::DataService::FOUNDATION->get_params($request); | ||
| 101 | |||
| 102 | my $diag = $params->{show} ? lc $params->{show} : ''; | ||
| 103 | |||
| 104 | if ( $diag eq 'fields' ) | ||
| 105 | { | ||
| 106 | return $ds->diagnostic_fields($request, $params); | ||
| 107 | } | ||
| 108 | |||
| 109 | elsif ( $diag eq 'digest' ) | ||
| 110 | { | ||
| 111 | return $ds->diagnostic_digest($request, $params); | ||
| 112 | } | ||
| 113 | |||
| 114 | else | ||
| 115 | { | ||
| 116 | print STDERR "Usage:\n\n $0 diag [path] 'show=fields'\n $0 diag [path] 'show=digest'\n\n"; | ||
| 117 | return; | ||
| 118 | } | ||
| 119 | } | ||
| 120 | |||
| 121 | |||
| 122 | our (%FIELD_PARAM) = ( name => 1, | ||
| 123 | vocab => 1, | ||
| 124 | data => 1, | ||
| 125 | doc => 1 ); | ||
| 126 | |||
| 127 | # diagnostic_fields ( request ) | ||
| 128 | # | ||
| 129 | # Generate diagnostic information about the output fields defined for this | ||
| 130 | # data service. See above for documentation. The report generated by this | ||
| 131 | # function is written to standard output, because it is designed to be run | ||
| 132 | # only from the command-line. | ||
| 133 | |||
| 134 | sub diagnostic_fields { | ||
| 135 | |||
| 136 | my ($ds, $request, $params) = @_; | ||
| 137 | |||
| 138 | # First check the query parameters. | ||
| 139 | |||
| 140 | my $bad_param; | ||
| 141 | |||
| 142 | foreach my $key ( keys %$params ) | ||
| 143 | { | ||
| 144 | unless ( $DIAG_PARAM{$key} || $FIELD_PARAM{$key} ) | ||
| 145 | { | ||
| 146 | print STDERR "ERROR: unknown parameter '$key'\n"; | ||
| 147 | $bad_param = 1; | ||
| 148 | } | ||
| 149 | } | ||
| 150 | |||
| 151 | return if $bad_param; | ||
| 152 | |||
| 153 | my $query_vocab = $params->{vocab}; | ||
| 154 | my $query_name = &diag_generate_regex($params->{name}); | ||
| 155 | my $query_field = &diag_generate_regex($params->{data}); | ||
| 156 | |||
| 157 | if ( $query_vocab and not $ds->{vocab}{$query_vocab} ) | ||
| 158 | { | ||
| 159 | print STDERR "ERROR: vocabulary '$query_vocab' is not defined for this data service.\n"; | ||
| 160 | return; | ||
| 161 | } | ||
| 162 | |||
| 163 | my @query; | ||
| 164 | |||
| 165 | push @query, "vocab = $query_vocab" if $query_vocab; | ||
| 166 | push @query, "name = $params->{name}" if $query_name; | ||
| 167 | push @query, "data = $params->{data}" if $query_field; | ||
| 168 | |||
| 169 | # Now check each of the output blocks defined for this data service, looking | ||
| 170 | # for fields in those blocks that match the query parameters. | ||
| 171 | |||
| 172 | my (%by_name); | ||
| 173 | |||
| 174 | foreach my $block ( keys %{$ds->{block}} ) | ||
| 175 | { | ||
| 176 | my $output_list = $ds->{block}{$block}{output_list}; | ||
| 177 | next unless ref $output_list eq 'ARRAY'; | ||
| 178 | |||
| 179 | # Check all entries in the block's output list. | ||
| 180 | |||
| 181 | FIELD: | ||
| 182 | foreach my $f ( @$output_list ) | ||
| 183 | { | ||
| 184 | # Ignore any entry in the output_list that does not correspond to | ||
| 185 | # an output field. | ||
| 186 | |||
| 187 | next unless ref $f eq 'HASH' && $f->{output}; | ||
| 188 | |||
| 189 | # If an output name was speciied, then ignore any entries whose | ||
| 190 | # 'output' attribute does not match. | ||
| 191 | |||
| 192 | if ( $query_field ) | ||
| 193 | { | ||
| 194 | next FIELD if $f->{output} !~ $query_field; | ||
| 195 | } | ||
| 196 | |||
| 197 | # If a vocabulary was specified, then determine the output name. | ||
| 198 | # Ignore entries which do not have a name under this vocabulary. | ||
| 199 | |||
| 200 | my (@matches); | ||
| 201 | |||
| 202 | if ( $query_vocab ) | ||
| 203 | { | ||
| 204 | my $name = $f->{"${query_vocab}_name"} ||= $f->{name}; | ||
| 205 | $name ||= $f->{output} if $ds->{vocab}{$query_vocab}{use_field_names}; | ||
| 206 | |||
| 207 | my $value = $f->{"${query_vocab}_value"} || $f->{value}; | ||
| 208 | |||
| 209 | next unless defined $name && $name ne ''; | ||
| 210 | next if $query_name && $name !~ $query_name; | ||
| 211 | |||
| 212 | push @matches, [$name, $query_vocab, $value]; | ||
| 213 | } | ||
| 214 | |||
| 215 | # If no vocabulary was specified, then determine the output name | ||
| 216 | # under each available vocabulary. | ||
| 217 | |||
| 218 | else | ||
| 219 | { | ||
| 220 | foreach my $v ( @{$ds->{vocab_list}} ) | ||
| 221 | { | ||
| 222 | my $name = $f->{"${v}_name"} || $f->{name}; | ||
| 223 | $name ||= $f->{output} if $ds->{vocab}{$v}{use_field_names}; | ||
| 224 | |||
| 225 | my $value = $f->{"${v}_value"} || $f->{value}; | ||
| 226 | |||
| 227 | next unless defined $name && $name ne ''; | ||
| 228 | next if $query_name && $name !~ $query_name; | ||
| 229 | |||
| 230 | push @matches, [$name, $v, $value]; | ||
| 231 | } | ||
| 232 | } | ||
| 233 | |||
| 234 | # Ignore entries for which we did not find at least one match. | ||
| 235 | |||
| 236 | next FIELD unless @matches; | ||
| 237 | |||
| 238 | # If we get to this point, then the entry matches the query so | ||
| 239 | # tabulate it by each name. | ||
| 240 | |||
| 241 | foreach my $m (@matches) | ||
| 242 | { | ||
| 243 | my ($name, $vocab, $value) = @$m; | ||
| 244 | my $new = { block => $block, vocab => $vocab, vvalue => $value, %$f }; | ||
| 245 | |||
| 246 | push @{$by_name{"$vocab:$name"}}, $new; | ||
| 247 | } | ||
| 248 | } | ||
| 249 | } | ||
| 250 | |||
| 251 | # Get the current working directory, so we can trim path names. | ||
| 252 | |||
| 253 | require "Cwd.pm"; $CWD = &Cwd::getcwd; | ||
| 254 | |||
| 255 | # Go through the entries and compute field widths. | ||
| 256 | |||
| 257 | my $options = { doc => $params->{doc}, values => $params->{values} }; | ||
| 258 | |||
| 259 | my @column_widths = 0 x 5; | ||
| 260 | |||
| 261 | foreach my $key ( sort { lc $a cmp lc $b } keys %by_name ) | ||
| 262 | { | ||
| 263 | foreach my $f ( @{$by_name{$key}} ) | ||
| 264 | { | ||
| 265 | $ds->diag_field_widths($key, $f, $options, \@column_widths); | ||
| 266 | } | ||
| 267 | } | ||
| 268 | |||
| 269 | # Now use this list of rows to print out a report tabulated by field name. | ||
| 270 | |||
| 271 | $options->{template} = " %-$column_widths[0]s %-$column_widths[1]s %-$column_widths[2]s %-$column_widths[3]s\n"; | ||
| 272 | |||
| 273 | print STDOUT "\n"; | ||
| 274 | print STDOUT "DIAGNOSTIC: FIELDS " . join(', ', @query) . "\n"; | ||
| 275 | print STDOUT "===============================================================================\n\n"; | ||
| 276 | |||
| 277 | print STDOUT " field name\n\n"; | ||
| 278 | |||
| 279 | my @headings = qw(field block conditionals definition); | ||
| 280 | |||
| 281 | foreach my $i (0..3) | ||
| 282 | { | ||
| 283 | $headings[$i] = '' unless $column_widths[$i]; | ||
| 284 | } | ||
| 285 | |||
| 286 | print STDOUT sprintf($options->{template}, @headings); | ||
| 287 | print STDOUT "\n"; | ||
| 288 | |||
| 289 | foreach my $key ( sort { lc $a cmp lc $b } keys %by_name ) | ||
| 290 | { | ||
| 291 | my ($vocab, $name) = split qr/:/, $key; | ||
| 292 | |||
| 293 | print STDOUT " $vocab : '$name'\n\n"; | ||
| 294 | |||
| 295 | foreach my $f ( @{$by_name{$key}} ) | ||
| 296 | { | ||
| 297 | $ds->diag_field_output($name, $f, $options); | ||
| 298 | } | ||
| 299 | |||
| 300 | print STDOUT "\n"; | ||
| 301 | } | ||
| 302 | |||
| 303 | unless ( keys %by_name ) | ||
| 304 | { | ||
| 305 | print STDOUT "No matching fields were found.\n\n"; | ||
| 306 | } | ||
| 307 | } | ||
| 308 | |||
| 309 | |||
| 310 | # diag_field_widths ( name, record, options, widths ) | ||
| 311 | # | ||
| 312 | # By repeatedly calling this function for each output record, the maximum | ||
| 313 | # width for each field will be computed. | ||
| 314 | |||
| 315 | sub diag_field_widths { | ||
| 316 | |||
| 317 | my ($ds, $name, $record, $options, $widths) = @_; | ||
| 318 | |||
| 319 | my ($block, $loc, $output, @conditionals); | ||
| 320 | |||
| 321 | $block = $record->{block}; | ||
| 322 | $loc = $ds->{block_loc}{$block}; | ||
| 323 | $output = $record->{output}; | ||
| 324 | |||
| 325 | $loc =~ s{$CWD/}{}; | ||
| 326 | |||
| 327 | $output .= " \"$record->{vvalue}\"" if defined $record->{vvalue} && $record->{vvalue} ne ''; | ||
| 328 | |||
| 329 | $widths->[0] = length($output) if !defined $widths->[0] || length($output) > $widths->[0]; | ||
| 330 | $widths->[1] = length($block) if !defined $widths->[1] || length($block) > $widths->[1]; | ||
| 331 | |||
| 332 | foreach my $c ( qw(if_block not_block if_vocab not_vocab if_field not_field | ||
| 333 | if_format not_format if_code not_code) ) | ||
| 334 | { | ||
| 335 | my $value = $record->{$c}; | ||
| 336 | next unless $value; | ||
| 337 | |||
| 338 | $value = join(q{, }, @$value) if ref $value eq 'ARRAY'; | ||
| 339 | my $cond = "$c $value"; | ||
| 340 | $widths->[2] = length($cond) if !defined $widths->[2] || length($cond) > $widths->[2]; | ||
| 341 | } | ||
| 342 | |||
| 343 | $widths->[3] = length($loc) if !defined $widths->[3] || length($loc) > $widths->[3]; | ||
| 344 | |||
| 345 | foreach my $i ( 0..4 ) | ||
| 346 | { | ||
| 347 | $widths->[$i] //= '0'; | ||
| 348 | } | ||
| 349 | } | ||
| 350 | |||
| 351 | |||
| 352 | # diag_field_output ( name, record, options ) | ||
| 353 | # | ||
| 354 | # Generate a description of a single output field and write it to standard | ||
| 355 | # output. | ||
| 356 | |||
| 357 | sub diag_field_output { | ||
| 358 | |||
| 359 | my ($ds, $name, $record, $options) = @_; | ||
| 360 | |||
| 361 | my ($block, $loc, $output, @conditionals); | ||
| 362 | |||
| 363 | $block = $record->{block}; | ||
| 364 | $loc = $ds->{block_loc}{$block}; | ||
| 365 | $output = $record->{output}; | ||
| 366 | |||
| 367 | $loc =~ s{$CWD/}{}; | ||
| 368 | |||
| 369 | $output .= " \"$record->{vvalue}\"" if defined $record->{vvalue} && $record->{vvalue} ne ''; | ||
| 370 | |||
| 371 | foreach my $c ( qw(if_block not_block if_vocab not_vocab if_field not_field | ||
| 372 | if_format not_format if_code not_code) ) | ||
| 373 | { | ||
| 374 | my $value = $record->{$c}; | ||
| 375 | next unless $value; | ||
| 376 | |||
| 377 | $value = join(q{, }, @$value) if ref $value eq 'ARRAY'; | ||
| 378 | push @conditionals, "$c $value"; | ||
| 379 | } | ||
| 380 | |||
| 381 | push @conditionals, '' unless @conditionals; | ||
| 382 | |||
| 383 | print STDOUT sprintf($options->{template}, $output, $block, $conditionals[0], $loc); | ||
| 384 | |||
| 385 | for ( my $i = 1; $i < @conditionals; $i++ ) | ||
| 386 | { | ||
| 387 | print STDOUT sprintf($options->{template}, '', '>>>', $conditionals[$i], ''); | ||
| 388 | } | ||
| 389 | |||
| 390 | if ( $options->{doc} && defined $record->{doc_string} && $record->{doc_string} ne '' ) | ||
| 391 | { | ||
| 392 | my $doc = $record->{doc_string}; | ||
| 393 | |||
| 394 | if ( $options->{doc} eq 'long' ) | ||
| 395 | { | ||
| 396 | $doc =~ s/\n/"\n "/gs; | ||
| 397 | } | ||
| 398 | else | ||
| 399 | { | ||
| 400 | $doc =~ s/\n.*//s; | ||
| 401 | } | ||
| 402 | |||
| 403 | print STDOUT " \"$doc\"\n"; | ||
| 404 | } | ||
| 405 | } | ||
| 406 | |||
| 407 | |||
| 408 | # generate_regex ( string ) | ||
| 409 | # | ||
| 410 | # Generate a regular expression that will match the given string, with * and ? | ||
| 411 | # as wildcards. | ||
| 412 | |||
| 413 | sub diag_generate_regex { | ||
| 414 | |||
| 415 | my ($string) = @_; | ||
| 416 | |||
| 417 | return unless defined $string && $string ne ''; | ||
| 418 | |||
| 419 | $string =~ s/[*]/.*/g; | ||
| 420 | $string =~ s/[?]/./g; | ||
| 421 | |||
| 422 | return qr{^$string$}; | ||
| 423 | } | ||
| 424 | |||
| 425 | |||
| 426 | our (%DIGEST_PARAM) = ( node => 1 ); | ||
| 427 | |||
| 428 | # diagnostic_digest ( request, params ) | ||
| 429 | # | ||
| 430 | # Generate diagnostic information about the user-level specification of this | ||
| 431 | # data service: the parameters accepted for each operation and the result | ||
| 432 | # fields returned. | ||
| 433 | |||
| 434 | sub diagnostic_digest { | ||
| 435 | |||
| 436 | my ($ds, $request, $params) = @_; | ||
| 437 | |||
| 438 | # First check the query parameters. | ||
| 439 | |||
| 440 | my $bad_param; | ||
| 441 | |||
| 442 | foreach my $key ( keys %$params ) | ||
| 443 | { | ||
| 444 | unless ( $DIAG_PARAM{$key} || $DIGEST_PARAM{$key} ) | ||
| 445 | { | ||
| 446 | print STDERR "ERROR: unknown parameter '$key'\n"; | ||
| 447 | $bad_param = 1; | ||
| 448 | } | ||
| 449 | } | ||
| 450 | |||
| 451 | return if $bad_param; | ||
| 452 | |||
| 453 | # Then check the node corresponding to the request path. If it is an | ||
| 454 | # operation node, report the specification of that operation. | ||
| 455 | |||
| 456 | my $path = $request->node_path; | ||
| 457 | my $node_query = &diag_generate_regex($params->{node}); | ||
| 458 | my $digest = { }; | ||
| 459 | |||
| 460 | if ( $node_query ) | ||
| 461 | { | ||
| 462 | $digest->{node_query} = $node_query; | ||
| 463 | $digest->{_node_query} = $params->{node}; | ||
| 464 | } | ||
| 465 | |||
| 466 | # Add all of the nodes defined for this data service. | ||
| 467 | |||
| 468 | foreach my $p ( sort keys %{$ds->{node_attrs}} ) | ||
| 469 | { | ||
| 470 | $ds->diag_add_node($digest, $p); | ||
| 471 | } | ||
| 472 | |||
| 473 | # Add values from the data service object. | ||
| 474 | |||
| 475 | $ds->diag_add_ds_obj($digest); | ||
| 476 | |||
| 477 | # Delete keys that were used only during the digest process. | ||
| 478 | |||
| 479 | delete $digest->{node_query}; | ||
| 480 | |||
| 481 | # Now dump the entire specification as a YAML file. | ||
| 482 | |||
| 483 | require "YAML.pm"; | ||
| 484 | |||
| 485 | binmode(STDOUT, ":utf8"); | ||
| 486 | print STDOUT YAML::Dump($digest); | ||
| 487 | } | ||
| 488 | |||
| 489 | |||
| 490 | # diag_add_ds_obj ( digest ) | ||
| 491 | # | ||
| 492 | # Add a record to the specified digest object to represent important fields | ||
| 493 | # from the data service object that are not specific to any node. | ||
| 494 | |||
| 495 | sub diag_add_ds_obj { | ||
| 496 | |||
| 497 | my ($ds, $digest) = @_; | ||
| 498 | |||
| 499 | $digest->{_wds_version} = $Web::DataService::VERSION; | ||
| 500 | |||
| 501 | $digest->{ds}{feature} = { %{$ds->{feature}} } if ref $ds->{feature} eq 'HASH'; | ||
| 502 | $digest->{ds}{special} = { %{$ds->{special}} } if ref $ds->{special} eq 'HASH'; | ||
| 503 | $digest->{ds}{special_alias} = { %{$ds->{special_alias}} } if ref $ds->{special_alias} eq 'HASH'; | ||
| 504 | $digest->{ds}{format} = { %{$ds->{format}} } if ref $ds->{format} eq 'HASH'; | ||
| 505 | $digest->{ds}{format_list} = [ @{$ds->{format_list}} ] if ref $ds->{format_list} eq 'ARRAY'; | ||
| 506 | $digest->{ds}{vocab} = { %{$ds->{vocab}} } if ref $ds->{vocab} eq 'HASH'; | ||
| 507 | $digest->{ds}{vocab_list} = [ @{$ds->{vocab_list}} ] if ref $ds->{vocab_list} eq 'ARRAY'; | ||
| 508 | |||
| 509 | foreach my $key ( qw( name title version path_prefix path_re ruleset_prefix | ||
| 510 | data_source data_provider data_license license_url | ||
| 511 | contact_name contact_email ) ) | ||
| 512 | { | ||
| 513 | $digest->{ds}{$key} = $ds->{$key}; | ||
| 514 | } | ||
| 515 | } | ||
| 516 | |||
| 517 | |||
| 518 | # diag_add_node ( digest, path, options ) | ||
| 519 | # | ||
| 520 | # Add a record to the specified digest object to represent the specified | ||
| 521 | # node. Then recursively add records to represent all of the other objects | ||
| 522 | # (nodes, output blocks, sets, rulesets) linked to it. | ||
| 523 | |||
| 524 | sub diag_add_node { | ||
| 525 | |||
| 526 | my ($ds, $digest, $path) = @_; | ||
| 527 | |||
| 528 | # First do some basic checks. Return without doing anything unless we | ||
| 529 | # have an actual path, and return immediately if we have already added | ||
| 530 | # this path. | ||
| 531 | |||
| 532 | return unless defined $path && $path ne ''; | ||
| 533 | return if $digest->{node}{$path}; | ||
| 534 | |||
| 535 | # Fail gracefully if this node doesn't exist, by simply returning. We | ||
| 536 | # want to complete the specification as best we can. The caller is | ||
| 537 | # responsible for checking and adding and error message if necessary. | ||
| 538 | |||
| 539 | return unless ref $ds->{node_attrs}{$path}; | ||
| 540 | |||
| 541 | # If a node query parameter was specified, then only include nodes whose | ||
| 542 | # path matches the specified pattern. | ||
| 543 | |||
| 544 | if ( ref $digest->{node_query} eq 'Regexp' && $path !~ $digest->{node_query} ) | ||
| 545 | { | ||
| 546 | return; | ||
| 547 | } | ||
| 548 | |||
| 549 | # Otherwise copy all of the node attributes into a new hash and add it to | ||
| 550 | # the specification. | ||
| 551 | |||
| 552 | # print STDERR "Added node $path\n"; | ||
| 553 | |||
| 554 | my $node = { %{$ds->{node_attrs}{$path}} }; | ||
| 555 | |||
| 556 | $digest->{node}{$path} = $node; | ||
| 557 | |||
| 558 | # Then compute some important attributes that might be inherited. | ||
| 559 | |||
| 560 | foreach my $key ( qw(disabled undocumented role method arg ruleset output output_label | ||
| 561 | optional_output summary public_access default_format default_limit | ||
| 562 | default_header default_datainfo default_count default_linebreak | ||
| 563 | default_save_filename allow_method allow_format allow_vocab) ) | ||
| 564 | { | ||
| 565 | my $value = $ds->node_attr($path, $key); | ||
| 566 | |||
| 567 | if ( defined $value && $value ne '' ) | ||
| 568 | { | ||
| 569 | $node->{$key} = $value; | ||
| 570 | } | ||
| 571 | } | ||
| 572 | |||
| 573 | # Then compute some other values that might not be specified directly. | ||
| 574 | |||
| 575 | $node->{ruleset} ||= $ds->determine_ruleset($path); | ||
| 576 | |||
| 577 | if ( my @subnode_list = $ds->get_nodelist($path) ) | ||
| 578 | { | ||
| 579 | # my @subnode_paths = map { $_->{path} } @subnode_list; | ||
| 580 | $node->{node_list} = \@subnode_list; | ||
| 581 | } | ||
| 582 | |||
| 583 | # Then we go through and figure out all of the blocks, sets, and rulesets | ||
| 584 | # referenced by this node and add those to the specification as well. | ||
| 585 | |||
| 586 | my (@show_list, @block_list); | ||
| 587 | |||
| 588 | my @out_list = &diag_list_value($node, 'output'); | ||
| 589 | |||
| 590 | foreach my $blockname ( @out_list ) | ||
| 591 | { | ||
| 592 | push @block_list, $blockname; | ||
| 593 | $ds->diag_add_block($digest, $blockname); | ||
| 594 | $ds->diag_add_check($digest, 'block', $blockname, "node '$path': output"); | ||
| 595 | } | ||
| 596 | |||
| 597 | my @summary = &diag_list_value($node, 'summary'); | ||
| 598 | |||
| 599 | foreach my $blockname ( @summary ) | ||
| 600 | { | ||
| 601 | $ds->diag_add_block($digest, $blockname); | ||
| 602 | $ds->diag_add_check($digest, 'block', $blockname, "node '$path': summary"); | ||
| 603 | } | ||
| 604 | |||
| 605 | if ( my $outmap = $node->{optional_output} ) | ||
| 606 | { | ||
| 607 | my $set = $ds->{set}{$outmap}; | ||
| 608 | |||
| 609 | if ( $ds->diag_add_check($digest, 'set', $outmap, "node '$path': optional_output") ) | ||
| 610 | { | ||
| 611 | $ds->diag_add_set($digest, $outmap); | ||
| 612 | |||
| 613 | foreach my $v ( @{$set->{value_list}} ) | ||
| 614 | { | ||
| 615 | my $vr = $set->{value}{$v}; | ||
| 616 | |||
| 617 | push @show_list, $vr->{value}; | ||
| 618 | |||
| 619 | if ( $vr->{maps_to} ) | ||
| 620 | { | ||
| 621 | push @block_list, $vr->{maps_to}; | ||
| 622 | $ds->diag_add_block($digest, $vr->{maps_to}); | ||
| 623 | $ds->diag_add_check($digest, 'block', $vr->{maps_to}, "node '$path': optional_output"); | ||
| 624 | } | ||
| 625 | } | ||
| 626 | } | ||
| 627 | } | ||
| 628 | |||
| 629 | $node->{show_list} = \@show_list; | ||
| 630 | $node->{block_list} = \@block_list; | ||
| 631 | |||
| 632 | if ( my $rs_name = $node->{ruleset} ) | ||
| 633 | { | ||
| 634 | $ds->diag_add_ruleset($digest, $rs_name); | ||
| 635 | $ds->diag_add_check($digest, 'ruleset', $rs_name, "node '$path'"); | ||
| 636 | } | ||
| 637 | |||
| 638 | elsif ( $node->{method} ) | ||
| 639 | { | ||
| 640 | $ds->diag_add_error($digest, "node '$path'", "no ruleset defined for this node"); | ||
| 641 | } | ||
| 642 | } | ||
| 643 | |||
| 644 | |||
| 645 | sub diag_list_value { | ||
| 646 | |||
| 647 | my ($hash, $field) = @_; | ||
| 648 | |||
| 649 | return unless defined $hash->{$field} && $hash->{$field} ne ''; | ||
| 650 | |||
| 651 | if ( ref $hash->{$field} eq 'ARRAY' ) | ||
| 652 | { | ||
| 653 | return grep { defined $_ && $_ ne '' } @{$hash->{$field}}; | ||
| 654 | } | ||
| 655 | |||
| 656 | else | ||
| 657 | { | ||
| 658 | return $hash->{$field}; | ||
| 659 | } | ||
| 660 | } | ||
| 661 | |||
| 662 | |||
| 663 | # diag_add_subnodes ( digest, path ) | ||
| 664 | # | ||
| 665 | # Go through the list of nodes that are in the "node list" associated with the | ||
| 666 | # specified path, and add them to the digest. These are the nodes that are | ||
| 667 | # linked as "subnodes" of the specified path. | ||
| 668 | |||
| 669 | sub diag_add_subnodes { | ||
| 670 | |||
| 671 | my ($ds, $digest, $path) = @_; | ||
| 672 | |||
| 673 | # Do some basic checks. Return without doing anything unless we have an | ||
| 674 | # actual path. | ||
| 675 | |||
| 676 | return unless defined $path && $path ne ''; | ||
| 677 | |||
| 678 | # If there are any nodes listed as subnodes of this path, then add them. | ||
| 679 | |||
| 680 | my @subnodes = $ds->get_nodelist($path); | ||
| 681 | |||
| 682 | foreach my $n ( @subnodes ) | ||
| 683 | { | ||
| 684 | my $subnode_path = $n->{path}; | ||
| 685 | $ds->diag_add_node($digest, $subnode_path); | ||
| 686 | $ds->diag_add_check($digest, 'node', $subnode_path, "node '$path': subnode list"); | ||
| 687 | } | ||
| 688 | } | ||
| 689 | |||
| 690 | |||
| 691 | sub diag_add_block { | ||
| 692 | |||
| 693 | my ($ds, $digest, $name) = @_; | ||
| 694 | |||
| 695 | # First do some basic checks. Return without doing anything if we weren't | ||
| 696 | # given an actual block name, or if we have already added this block. | ||
| 697 | |||
| 698 | return unless defined $name && $name ne ''; | ||
| 699 | return if $digest->{block}{$name}; | ||
| 700 | |||
| 701 | # Just as with nodes, fail gracefully if it doesn't exist. | ||
| 702 | |||
| 703 | return unless ref $ds->{block}{$name}; | ||
| 704 | |||
| 705 | # Otherwise copy all of the block attributes into a new hash and add it to | ||
| 706 | # the specification. | ||
| 707 | |||
| 708 | my $new = { %{$ds->{block}{$name}} }; | ||
| 709 | |||
| 710 | $digest->{block}{$name} = $new; | ||
| 711 | |||
| 712 | # Blocks don't have any other named structures hanging off of them, so we | ||
| 713 | # can stop here. | ||
| 714 | } | ||
| 715 | |||
| 716 | |||
| 717 | sub diag_add_set { | ||
| 718 | |||
| 719 | my ($ds, $digest, $name) = @_; | ||
| 720 | |||
| 721 | # First do some basic checks. Return without doing anything if we weren't | ||
| 722 | # given an actual set name, or if we have already added this set. | ||
| 723 | |||
| 724 | return unless defined $name && $name ne ''; | ||
| 725 | return if $digest->{set}{$name}; | ||
| 726 | |||
| 727 | # Just as with nodes, fail gracefully if it doesn't exist. | ||
| 728 | |||
| 729 | return unless ref $ds->{set}{$name}; | ||
| 730 | |||
| 731 | # Otherwise copy all of the set attributes into a new hash and add it to | ||
| 732 | # the specification. | ||
| 733 | |||
| 734 | my $new = { %{$ds->{set}{$name}} }; | ||
| 735 | |||
| 736 | $digest->{set}{$name} = $new; | ||
| 737 | |||
| 738 | # Sets don't have any other named structures hanging off of them, so we | ||
| 739 | # can stop here. | ||
| 740 | } | ||
| 741 | |||
| 742 | |||
| 743 | sub diag_add_ruleset { | ||
| 744 | |||
| 745 | my ($ds, $digest, $name) = @_; | ||
| 746 | |||
| 747 | # First do some basic checks. Return without doing anything if we weren't | ||
| 748 | # given an actual ruleset name, or if we have already added this ruleset. | ||
| 749 | |||
| 750 | return unless defined $name && $name ne ''; | ||
| 751 | return if $digest->{ruleset}{$name}; | ||
| 752 | |||
| 753 | # Just as with nodes, fail gracefully if it doesn't exist. | ||
| 754 | |||
| 755 | my $rs_list = $ds->{ruleset_diag}{$name}; | ||
| 756 | |||
| 757 | return unless ref $rs_list eq 'ARRAY'; | ||
| 758 | |||
| 759 | # Otherwise add the ruleset list to the specification. | ||
| 760 | |||
| 761 | $digest->{ruleset}{$name} = $rs_list; | ||
| 762 | |||
| 763 | # Then go through the rules and add any referenced sets. | ||
| 764 | |||
| 765 | foreach my $rule ( @$rs_list ) | ||
| 766 | { | ||
| 767 | next unless ref $rule eq 'HASH'; | ||
| 768 | |||
| 769 | # First look at the 'valid' field. This might be an array, so look at | ||
| 770 | # each of the values in turn. | ||
| 771 | |||
| 772 | my @valid = ref $rule->{valid} eq 'ARRAY' ? @{$rule->{valid}} : $rule->{valid}; | ||
| 773 | my @new_valid; | ||
| 774 | |||
| 775 | foreach my $v (@valid) | ||
| 776 | { | ||
| 777 | # If we find a reference, then we must use Perl internal voodoo to | ||
| 778 | # unpack it and figure out the name of whatever it refers to. | ||
| 779 | |||
| 780 | if ( ref $v ) | ||
| 781 | { | ||
| 782 | push @new_valid, &diag_decode_ref($v); | ||
| 783 | } | ||
| 784 | |||
| 785 | # Otherwise, we assume that it is the name of a Set. So add this | ||
| 786 | # set to the digest unless it's already there. | ||
| 787 | |||
| 788 | elsif ( $v && $v ne 'FLAG_VALUE' && $v ne 'ANY_VALUE' ) | ||
| 789 | { | ||
| 790 | $ds->diag_add_set($digest, $v); | ||
| 791 | $ds->diag_add_check($digest, 'set', $v, "ruleset'$name'"); | ||
| 792 | push @new_valid, $v; | ||
| 793 | } | ||
| 794 | } | ||
| 795 | |||
| 796 | # Now copy this list back to 'valid'. | ||
| 797 | |||
| 798 | if ( @new_valid == 1 ) | ||
| 799 | { | ||
| 800 | $rule->{valid} = $new_valid[0]; | ||
| 801 | } | ||
| 802 | |||
| 803 | elsif ( @new_valid > 1 ) | ||
| 804 | { | ||
| 805 | $rule->{valid} = \@new_valid; | ||
| 806 | } | ||
| 807 | |||
| 808 | # If this rule is an inclusion rule, recursively include the target | ||
| 809 | # ruleset as well. | ||
| 810 | |||
| 811 | my $inclusion = $rule->{allow} || $rule->{require}; | ||
| 812 | |||
| 813 | if ( $inclusion && ! ref $inclusion ) | ||
| 814 | { | ||
| 815 | $ds->diag_add_ruleset($digest, $inclusion); | ||
| 816 | $ds->diag_add_check($digest, 'ruleset', $inclusion, "ruleset '$name'"); | ||
| 817 | } | ||
| 818 | } | ||
| 819 | } | ||
| 820 | |||
| 821 | |||
| 822 | sub diag_decode_ref { | ||
| 823 | |||
| 824 | return unless ref $_[0]; | ||
| 825 | |||
| 826 | my $obj = B::svref_2object($_[0]); | ||
| 827 | |||
| 828 | return '*UNKNOWN*' unless $obj->can('GV'); | ||
| 829 | |||
| 830 | my $name = $obj->GV->NAME; | ||
| 831 | my $pkg = $obj->GV->STASH->NAME; | ||
| 832 | my $sigil = ref $_[0] eq 'CODE' ? '&' | ||
| 833 | : ref $_[0] eq 'HASH' ? '%' | ||
| 834 | : ref $_[0] eq 'ARRAY' ? '@' | ||
| 835 | : '?'; | ||
| 836 | |||
| 837 | return "${sigil}${pkg}::${name}"; | ||
| 838 | } | ||
| 839 | |||
| 840 | |||
| 841 | sub diag_add_check { | ||
| 842 | |||
| 843 | my ($ds, $digest, $type, $name, $key) = @_; | ||
| 844 | |||
| 845 | # Check to see if a proper thingy exists under the proper name. If so, | ||
| 846 | # return true. | ||
| 847 | |||
| 848 | my $hashkey = $type; | ||
| 849 | $hashkey = 'ruleset_diag' if $type eq 'ruleset'; | ||
| 850 | $hashkey = 'node_attrs' if $type eq 'node'; | ||
| 851 | |||
| 852 | return 1 if ref $ds->{$hashkey}{$name}; | ||
| 853 | |||
| 854 | # Otherwise, add an error to the specification record and return false. | ||
| 855 | |||
| 856 | $ds->diag_add_error($digest, $key, "unknown $type '$name'"); | ||
| 857 | return 0; | ||
| 858 | } | ||
| 859 | |||
| 860 | |||
| 861 | sub diag_add_error { | ||
| 862 | |||
| 863 | my ($ds, $digest, $key, $message) = @_; | ||
| 864 | |||
| 865 | return unless defined $message && $message ne ''; | ||
| 866 | |||
| 867 | $key ||= 'unclassified'; | ||
| 868 | |||
| 869 | push @{$digest->{errors}{$key}}, $message; | ||
| 870 | } | ||
| 871 | |||
| 872 | |||
| 873 | 1; |