Bug 17600: Standardize our EXPORT_OK
[srvgit] / misc / migration_tools / switch_marc21_series_info.pl
index cfd204b..f3962e5 100755 (executable)
@@ -4,18 +4,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 2 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 strict;
 use warnings;
@@ -25,13 +25,14 @@ use warnings;
 BEGIN {
     # find Koha's Perl modules
     # test carefully before changing this
-    use FindBin;
+    use FindBin ();
     eval { require "$FindBin::Bin/../kohalib.pl" };
 }
 
-use C4::Biblio;
+use Koha::Script;
+use C4::Biblio qw( GetFrameworkCode GetMarcBiblio ModBiblioMarc );
 use C4::Context;
-use Getopt::Long;
+use Getopt::Long qw( GetOptions );
 
 my $commit;
 my $add_links;
@@ -58,16 +59,44 @@ if ( ! $result || $show_help ) {
 
 my $dbh = C4::Context->dbh;
 
-my $count_sth = $dbh->prepare( 'SELECT COUNT(biblionumber) FROM biblio CROSS JOIN biblioitems USING (biblionumber) WHERE ExtractValue(marcxml,\'//datafield[@tag="440"]/subfield[@code="a"]\') OR ExtractValue(marcxml,\'//datafield[@tag="440"]/subfield[@code="v"]\') OR ExtractValue(marcxml,\'//datafield[@tag="440"]/subfield[@code="n"]\') OR ExtractValue(marcxml,\'//datafield[@tag="490"]/subfield[@code="a"]\') OR ExtractValue(marcxml,\'//datafield[@tag="490"]/subfield[@code="v"]\')' );
-
-my $bibs_sth = $dbh->prepare( 'SELECT biblionumber FROM biblio CROSS JOIN biblioitems USING (biblionumber) WHERE ExtractValue(marcxml,\'//datafield[@tag="440"]/subfield[@code="a"]\') OR ExtractValue(marcxml,\'//datafield[@tag="440"]/subfield[@code="v"]\') OR ExtractValue(marcxml,\'//datafield[@tag="440"]/subfield[@code="n"]\') OR ExtractValue(marcxml,\'//datafield[@tag="490"]/subfield[@code="a"]\') OR ExtractValue(marcxml,\'//datafield[@tag="490"]/subfield[@code="v"]\')' );
+my $count_sth = $dbh->prepare(
+    q|
+    SELECT COUNT(biblionumber)
+    FROM biblio_metadata
+    WHERE format='marcxml'
+        AND `schema`=?
+        AND (
+            ExtractValue(metadata,'//datafield[@tag="440"]/subfield[@code="a"]')
+                OR ExtractValue(metadata,'//datafield[@tag="440"]/subfield[@code="v"]')
+                OR ExtractValue(metadata,'//datafield[@tag="440"]/subfield[@code="n"]')
+                OR ExtractValue(metadata,'//datafield[@tag="490"]/subfield[@code="a"]')
+                OR ExtractValue(metadata,'//datafield[@tag="490"]/subfield[@code="v"]')
+            )
+    |
+);
+
+my $bibs_sth = $dbh->prepare(
+    q|
+    SELECT biblionumber
+    FROM biblio_metadata
+    WHERE format='marcxml'
+        AND `schema`=?
+        AND (
+            ExtractValue(metadata,'//datafield[@tag="440"]/subfield[@code="a"]')
+                OR ExtractValue(metadata,'//datafield[@tag="440"]/subfield[@code="v"]')
+                OR ExtractValue(metadata,'//datafield[@tag="440"]/subfield[@code="n"]')
+                OR ExtractValue(metadata,'//datafield[@tag="490"]/subfield[@code="a"]')
+                OR ExtractValue(metadata,'//datafield[@tag="490"]/subfield[@code="v"]')
+            )
+    |
+);
 
 unless ( $commit ) {
     print_usage();
 }
 
 print "Examining MARC records...\n";
-$count_sth->execute();
+$count_sth->execute( C4::Context->preference('marcflavour') );
 my ( $num_records ) = $count_sth->fetchrow;
 
 unless ( $commit ) {
@@ -105,13 +134,13 @@ my %fields = (
     },
     );
 
-$bibs_sth->execute();
+$bibs_sth->execute( C4::Context->preference('marcflavour') );
 while ( my ( $biblionumber ) = $bibs_sth->fetchrow ) {
     my $framework = GetFrameworkCode( $biblionumber ) || '';
     my ( @newfields );
 
     # Get biblio marc
-    my $biblio = GetMarcBiblio( $biblionumber );
+    my $biblio = GetMarcBiblio({ biblionumber => $biblionumber });
 
     foreach my $field ( $biblio->field( '440' ) ) {
         my @newsubfields;
@@ -193,7 +222,7 @@ while ( my ( $biblionumber ) = $bibs_sth->fetchrow ) {
     else {
         print ".";
     }
-    ModBiblioMarc( $biblio, $biblionumber, $framework );
+    ModBiblioMarc( $biblio, $biblionumber );
 }
 print "\n";
 
@@ -202,22 +231,26 @@ if ( $update_frameworks ) {
 
     # set new mappings for koha fields
     $dbh->do(
-"UPDATE marc_subfield_structure SET kohafield='seriestitle'
+"UPDATE marc_subfield_structure SET kohafield='biblio.seriestitle'
   WHERE tagfield='490' AND tagsubfield='a'"
     );
     $dbh->do(
-"UPDATE marc_subfield_structure SET kohafield='volume'
+"UPDATE marc_subfield_structure SET kohafield='biblioitems.volume'
   WHERE tagfield='490' AND tagsubfield='v'"
     );
 
     # empty old koha fields
     $dbh->do(
 "UPDATE marc_subfield_structure SET kohafield=''
-  WHERE kohafield='seriestitle' AND tagfield='440' AND tagsubfield='a'"
+  WHERE kohafield='biblio.seriestitle' AND tagfield='440' AND tagsubfield='a'"
+        );
+    $dbh->do(
+"UPDATE marc_subfield_structure SET kohafield=''
+  WHERE kohafield='biblioitems.volume' AND tagfield='440' AND tagsubfield='v'"
         );
     $dbh->do(
 "UPDATE marc_subfield_structure SET kohafield=''
-  WHERE kohafield='volume' AND tagfield='440' AND tagsubfield='v'"
+  WHERE kohafield='biblioitems.number' AND tagfield='440' AND tagsubfield='n'"
         );
 }