Skip to content

Instantly share code, notes, and snippets.

@otuoma
Last active January 30, 2019 12:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save otuoma/177ab5f407d18cb06c270ee7b34289c5 to your computer and use it in GitHub Desktop.
Save otuoma/177ab5f407d18cb06c270ee7b34289c5 to your computer and use it in GitHub Desktop.
This codeigniter script helped me to migrate koha's biblioitems.marcxml into biblio_metadata table after upgrading. To use it, set up codeigniter and configure it to connect to the old koha database and the new database. Then include this class in controllers. Run it via the commandline.
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class Migrate extends CI_Controller {
public function __construct(){
parent::__construct();
$db = &get_instance();
$this->db = $db->load->database('default', TRUE);
$old_koha = &get_instance();
$this->old_koha = $old_koha->load->database('old_koha', TRUE);
}
public function migrate_marcxml(){
$q = $this->old_koha->query("SELECT biblionumber, timestamp, marcxml FROM biblioitems");
$success = 0;
$fails = 0;
foreach ($q->result() as $row){
echo "\nMigrating biblio ".$row->biblionumber;
if (!$row->marcxml){$marcxml = "";}else{$marcxml = $row->marcxml;}
$data = array(
'biblionumber'=>$row->biblionumber,
'metadata'=>$marcxml,
'timestamp'=>$row->timestamp,
'format'=>'marcxml',
'marcflavour'=>'MARC21'
);
$sql = "INSERT IGNORE INTO biblio_metadata ";
$sql .= "(biblionumber, metadata, timestamp, format, marcflavour) ";
$sql .= "VALUES ('$row->biblionumber', ".$this->db->escape($row->marcxml).", '$row->timestamp', 'marcxml', 'MARC21');";
$this->db->query($sql);
if($this->db->affected_rows() > 0){
echo " Success\n";
$success = $success + 1;
}else{
echo " Failed\n";
$fails = $fails + 1;
}
} //end foreach
echo "=======END=============\n";
echo "Total records : ".$q->num_rows()."\n";;
echo "Success : ".$success." records\n";
echo "Failed : ".$fails." records\n";
} //end public function
}
@otuoma
Copy link
Author

otuoma commented Jan 30, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment