Bug 17600: Standardize our EXPORT_OK
[srvgit] / misc / cronjobs / delete_records_via_leader.pl
index b262ded..8456f42 100755 (executable)
@@ -5,18 +5,18 @@
 #
 # This file is part of Koha.
 #
-# Koha 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.
+# Koha 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.
 #
-# Koha 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.
+# Koha 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 Koha; if not, write to the Free Software Foundation, Inc.,
-# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+# You should have received a copy of the GNU General Public License
+# along with Koha; if not, see <http://www.gnu.org/licenses>.
 #-----------------------------------
 
 use Modern::Perl;
@@ -27,15 +27,18 @@ BEGIN {
 
     # find Koha's Perl modules
     # test carefully before changing this
-    use FindBin;
+    use FindBin ();
     eval { require "$FindBin::Bin/../kohalib.pl" };
 }
 
-use Getopt::Long;
-
-use C4::Biblio;
-use C4::Items;
+use Getopt::Long qw( GetOptions );
+use Pod::Usage qw( pod2usage );
+use Koha::Script -cron;
+use C4::Biblio qw( DelBiblio );
 use Koha::Database;
+use Koha::Biblios;
+use Koha::Biblio::Metadatas;
+use Koha::Items;
 
 my $delete_items;
 my $confirm;
@@ -51,7 +54,14 @@ GetOptions(
     'h|help'            => \$help,
 );
 
-if ( $help || !$confirm ) {
+pod2usage(q|--test and --confirm cannot be specified together|) if $test and $confirm;
+
+unless ( $confirm or $test ) {
+    warn "Running in test mode as --confirm is not passed\n";
+    $test = 1;
+}
+
+if ( $help ) {
     say qq{
 delete_records_via_leader.pl - Attempt to delete any MARC records where the leader character 5 equals 'd'
 usage: delete_records_via_leader.pl --confirm --verbose [--test]
@@ -68,39 +78,44 @@ This script has the following parameters :
     exit();
 }
 
-my $schema = Koha::Database->new()->schema();
-my @biblioitems =
-  $schema->resultset('Biblioitem')->search( { marc => { LIKE => '_____d%' } } );
+my @metadatas =    # Should be replaced by a call to C4::Search on zebra index
+                   # Record-status when bug 15537 will be pushed
+  Koha::Biblio::Metadatas->search( { format => 'marcxml', schema => C4::Context->preference('marcflavour'), metadata => { LIKE => '%<leader>_____d%' } } );
 
-my $total_records_count   = @biblioitems;
+my $total_records_count   = @metadatas;
 my $deleted_records_count = 0;
 my $total_items_count     = 0;
 my $deleted_items_count   = 0;
-
-foreach my $biblioitem (@biblioitems) {
-    my $biblionumber = $biblioitem->get_column('biblionumber');
+foreach my $m (@metadatas) {
+    my $biblionumber = $m->get_column('biblionumber');
 
     say "RECORD: $biblionumber" if $verbose;
 
     if ($delete_items) {
         my $deleted_count = 0;
-        foreach my $item ( $biblioitem->items() ) {
-            my $itemnumber = $item->itemnumber();
-
-            my $error = $test ? "Test mode enabled" : DelItemCheck( undef, $biblionumber, $itemnumber );
-            $error = undef if $error eq '1';
-
-            if ($error) {
-                say "ERROR DELETING ITEM $itemnumber: $error";
-            }
-            else {
-                say "DELETED ITEM $itemnumber" if $verbose;
-                $deleted_items_count++;
+        my $biblio = Koha::Biblios->find( $biblionumber );
+        my @items = Koha::Items->search( { biblionumber => $biblionumber } );
+        foreach my $item ( @items ) {
+            my $itemnumber = $item->itemnumber;
+
+            if( $test ){
+                my $result = $item->safe_to_delete;
+                if ( $result eq "1") {
+                    say "TEST MODE: Item $itemnumber would have been deleted";
+                } else {
+                    say "TEST MODE: ERROR DELETING ITEM $itemnumber: $result";
+                }
+            } else {
+                my $result = $item->safe_delete;
+                if ( ref $result eq "Koha::Item" ){
+                    say "DELETED ITEM $itemnumber" if $verbose;
+                    $deleted_items_count++;
+                } else {
+                    say "ERROR DELETING ITEM $itemnumber: $result";
+                }
             }
-
             $total_items_count++;
         }
-
     }
 
     my $error = $test ? q{Test mode enabled} : DelBiblio($biblionumber);