Main Koha release repository https://koha-community.org
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

137 lines
4.3 KiB

  1. #!/usr/bin/perl
  2. #-----------------------------------
  3. # Copyright 2013 ByWater Solutions
  4. #
  5. # This file is part of Koha.
  6. #
  7. # Koha is free software; you can redistribute it and/or modify it
  8. # under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation; either version 3 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # Koha is distributed in the hope that it will be useful, but
  13. # WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with Koha; if not, see <http://www.gnu.org/licenses>.
  19. #-----------------------------------
  20. use Modern::Perl;
  21. binmode( STDOUT, ":encoding(UTF-8)" );
  22. BEGIN {
  23. # find Koha's Perl modules
  24. # test carefully before changing this
  25. use FindBin;
  26. eval { require "$FindBin::Bin/../kohalib.pl" };
  27. }
  28. use Getopt::Long;
  29. use Pod::Usage;
  30. use Koha::Script -cron;
  31. use C4::Biblio;
  32. use C4::Items;
  33. use Koha::Database;
  34. use Koha::Biblios;
  35. use Koha::Biblio::Metadatas;
  36. use Koha::Items;
  37. my $delete_items;
  38. my $confirm;
  39. my $test;
  40. my $verbose;
  41. my $help;
  42. GetOptions(
  43. 'i|di|delete-items' => \$delete_items,
  44. 'c|confirm' => \$confirm,
  45. 't|test' => \$test,
  46. 'v|verbose' => \$verbose,
  47. 'h|help' => \$help,
  48. );
  49. pod2usage(q|--test and --confirm cannot be specified together|) if $test and $confirm;
  50. unless ( $confirm or $test ) {
  51. warn "Running in test mode as --confirm is not passed\n";
  52. $test = 1;
  53. }
  54. if ( $help ) {
  55. say qq{
  56. delete_records_via_leader.pl - Attempt to delete any MARC records where the leader character 5 equals 'd'
  57. usage: delete_records_via_leader.pl --confirm --verbose [--test]
  58. This script has the following parameters :
  59. -h --help: Prints this message
  60. -c --confirm: Script will do nothing without this parameter
  61. -v --verbose: Be verbose
  62. -t --test: Test mode, does not delete records.
  63. Test mode cannot determine if a record/item will be deleted successfully,
  64. it will only tell you what records and items the script will attempt to delete.
  65. -i --delete-items: Try deleting items before deleting record.
  66. Records with items cannot be deleted.
  67. };
  68. exit();
  69. }
  70. my @metadatas = # Should be replaced by a call to C4::Search on zebra index
  71. # Record-status when bug 15537 will be pushed
  72. Koha::Biblio::Metadatas->search( { format => 'marcxml', schema => C4::Context->preference('marcflavour'), metadata => { LIKE => '%<leader>_____d%' } } );
  73. my $total_records_count = @metadatas;
  74. my $deleted_records_count = 0;
  75. my $total_items_count = 0;
  76. my $deleted_items_count = 0;
  77. foreach my $m (@metadatas) {
  78. my $biblionumber = $m->get_column('biblionumber');
  79. say "RECORD: $biblionumber" if $verbose;
  80. if ($delete_items) {
  81. my $deleted_count = 0;
  82. my $biblio = Koha::Biblios->find( $biblionumber );
  83. my @items = Koha::Items->search( { biblionumber => $biblionumber } );
  84. foreach my $item ( @items ) {
  85. my $itemnumber = $item->itemnumber;
  86. if( $test ){
  87. my $result = $item->safe_to_delete;
  88. if ( $result eq "1") {
  89. say "TEST MODE: Item $itemnumber would have been deleted";
  90. } else {
  91. say "TEST MODE: ERROR DELETING ITEM $itemnumber: $result";
  92. }
  93. } else {
  94. my $result = $item->safe_delete;
  95. if ( ref $result eq "Koha::Item" ){
  96. say "DELETED ITEM $itemnumber" if $verbose;
  97. $deleted_items_count++;
  98. } else {
  99. say "ERROR DELETING ITEM $itemnumber: $result";
  100. }
  101. }
  102. $total_items_count++;
  103. }
  104. }
  105. my $error = $test ? q{Test mode enabled} : DelBiblio($biblionumber);
  106. if ( $error ) {
  107. say "ERROR DELETING BIBLIO $biblionumber: $error";
  108. } else {
  109. say "DELETED BIBLIO $biblionumber" if $verbose;
  110. $deleted_records_count++;
  111. }
  112. say q{};
  113. }
  114. if ( $verbose ) {
  115. say "DELETED $deleted_records_count OF $total_records_count RECORDS";
  116. say "DELETED $deleted_items_count OF $total_items_count ITEMS" if $delete_items;
  117. }