From dd9c4534c4362293095bd9e175e26e93794da52c Mon Sep 17 00:00:00 2001 From: Phan Tung Bui Date: Wed, 28 Feb 2024 11:12:19 -0500 Subject: [PATCH] Bug 36027 : (follow-up) Improvement: Simplified command line options and add --help option --- .../search_for_data_inconsistencies.pl | 120 ++++++++---------- 1 file changed, 54 insertions(+), 66 deletions(-) diff --git a/misc/maintenance/search_for_data_inconsistencies.pl b/misc/maintenance/search_for_data_inconsistencies.pl index 658be3ca0a..2f7b7a8a80 100755 --- a/misc/maintenance/search_for_data_inconsistencies.pl +++ b/misc/maintenance/search_for_data_inconsistencies.pl @@ -27,63 +27,41 @@ use Koha::Items; use Koha::ItemTypes; use Koha::Patrons; use C4::Biblio qw( GetMarcFromKohaField ); +use Getopt::Long; +use Pod::Usage; -# %methods hash defines options and their corresponding subroutines -# Each numeric key represents an option, and its associated subroutine reference defines the action to be taken -# Option 1: CheckItemsBranch - Check for items without home or holding library -# Option 2: CheckItemsAuthHeader - Check for authority records with invalid authority type -# Option 3: CheckItemsStatus - Check for bibliographic records and items without an item type or with an invalid item type -# Option 4: CheckItemsFramework - Check for invalid values in fields where the framework limits to an authorized value category -# Option 5: CheckItemsTitle - Check for bibliographic records without a title -# Option 6: CheckAgeForCategory - Check for patrons with invalid age for category -# Option 7: CheckRelationshipsLoop - Check for relationships or dependencies between borrowers in a loop - -my %methods = ( - 1 => \&CheckItemsBranch, - 2 => \&CheckItemsAuthHeader, - 3 => \&CheckItemsStatus, - 4 => \&CheckItemsFramework, - 5 => \&CheckItemsTitle, - 6 => \&CheckAgeForCategory, +my %options = ( + 'branch' => 1, + 'auth' => 1, + 'status' => 1, + 'framework' => 1, + 'title' => 1, + 'age' => 1, + 'help' => 0, ); -my @methods_to_run; +# Parse command line options +GetOptions( + 'skip-branch' => sub { $options{'branch'} = 0 }, + 'skip-auth' => sub { $options{'auth'} = 0 }, + 'skip-status' => sub { $options{'status'} = 0 }, + 'skip-framework' => sub { $options{'framework'} = 0 }, + 'skip-title' => sub { $options{'title'} = 0 }, + 'skip-age' => sub { $options{'age'} = 0 }, + 'help' => sub { $options{'help'} = 1; }, +) or pod2usage(2); # Print usage if invalid options are provided -if ( @ARGV == 0 ) { +# Print usage and exit if --help flag is provided +pod2usage(1) if $options{'help'}; - # If no arguments are provided, add all methods to the list - @methods_to_run = keys %methods; -} -else { - foreach my $arg (@ARGV) { - if ( $arg =~ /^(\d+)$/ ) { - my $method_number = $1; - if ( $method_number >= 1 && $method_number <= 7 ) { - push @methods_to_run, $method_number; - } - else { - print "Invalid method number: $method_number\n"; - } - } - else { - print "Invalid argument: $arg\n"; - } - } -} +# Run checks based on options +CheckItemsBranch() if $options{'branch'}; +CheckItemsAuthHeader() if $options{'auth'}; +CheckItemsStatus() if $options{'status'}; +CheckItemsFramework() if $options{'framework'}; +CheckItemsTitle() if $options{'title'}; +CheckAgeForCategory() if $options{'age'}; -foreach my $choice (@methods_to_run) { - if ( $choice =~ /^\d+$/ && $choice >= 1 && $choice <= 7 ) { - if ( exists $methods{$choice} ) { - $methods{$choice}->(); - } - else { - print "Method $choice not found\n"; - } - } - else { - print "Invalid choice: $choice\n"; - } -} sub CheckItemsBranch { my $items = Koha::Items->search({ -or => { homebranch => undef, holdingbranch => undef }}); @@ -394,25 +372,35 @@ sub new_hint { say "=> $name"; } -=head1 NAME - -search_for_data_inconsistencies.pl =head1 SYNOPSIS - perl search_for_data_inconsistencies.pl +search_for_data_inconsistencies.pl [options] + +=head2 DESCRIPTION + +Catch data inconsistencies in Koha database: + + * Items with undefined homebranch and/or holdingbranch + * Authorities with undefined authtypecodes/authority types + * Item types: + * if item types are defined at item level (item-level_itypes=specific item), + then items.itype must be set else biblioitems.itemtype must be set + * Item types defined in items or biblioitems must be defined in the itemtypes table + * Bibliographic records without a title + * Invalid MARCXML in bibliographic records + * Patrons with invalid category types due to lower and upper age limits -=head1 DESCRIPTION +=head2 OPTIONS -Catch data inconsistencies in Koha database + --skip-branch Skip checking for items without home or holding library + --skip-auth Skip checking for authority records with invalid authority type + --skip-status Skip checking for bibliographic records and items without an item type or with an invalid item type + --skip-framework Skip checking for invalid values in fields where the framework limits to an authorized value category + --skip-title Skip checking for bibliographic records without a title + --skip-age Skip checking for patrons with invalid age for category + --help Print usage information -* Items with undefined homebranch and/or holdingbranch -* Authorities with undefined authtypecodes/authority types -* Item types: - * if item types are defined at item level (item-level_itypes=specific item), - then items.itype must be set else biblioitems.itemtype must be set - * Item types defined in items or biblioitems must be defined in the itemtypes table -* Invalid MARCXML in bibliographic records -* Patrons with invalid category types due to lower and upper age limits +Note: If no options are provided, all tests will be run. -=cut +=cut \ No newline at end of file -- 2.34.1