<?php
/* transformation fichier rhythmdb.xml en base MySQL, ce qui revient à importer du XML dans du MySQL
# Required: rhythmdb, mysql, php 5 ou +
#
# Author:
# Fred Radeff, radeff@akademia.ch, www.akademia.ch
# History
# 2007-12-29 : FR, created
# No Copyright, steal this!
#todo: adapt mysqlDB structure (not text only)
#structure of the mysqlDB
CREATE TABLE IF NOT EXISTS `musique` (
`title` text NOT NULL,
`genre` text NOT NULL,
`artist` text NOT NULL,
`album` text NOT NULL,
`duration` text NOT NULL,
`file_size` text NOT NULL,
`location` text NOT NULL,
`mountpoint` text NOT NULL,
`mtime` text NOT NULL,
`first_seen` text NOT NULL,
`last_seen` text NOT NULL,
`bitrate` text NOT NULL,
`date` text NOT NULL,
`mimetype` text NOT NULL,
`mb_trackid` text NOT NULL,
`mb_artistid` text NOT NULL,
`mb_albumid` text NOT NULL,
`mb_albumartistid` text NOT NULL,
`mb_artistsortname` text NOT NULL,
`id` int(12) NOT NULL auto_increment,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
#end of the preliminary headers, now go to the code
*/
include("connect.inc.php"); //see connect.inc.php in the wiki for details
$db=connect_db();
$db_name=db_name();
mysql_select_db($db_name,$db);
/* #these are the xml fields
title
genre
artist
album
duration
file_size
location
mountpoint
mtime
first_seen
last_seen
bitrate
date
mimetype
mb_trackid
mb_artistid
mb_albumid
mb_albumartistid
mb_artistsortname
*/
$doc = SimpleXML_load_file("rhythmdb.xml"); //open xml file
$sql= "TRUNCATE TABLE `musique`;";
echo $sql ."<br>";
$doit=mysql_query($sql);
if(!$doit) {
echo "Problème avec l'effacement des données, veuillez recommencer";
}
// Boucle sur l'ensemble des donnees
echo "Merci d'attendre, l'importation est en cours...";
$probleme=0; //initialisation du compteur
foreach ($doc->entry as $morceau) //boucle sur le fichier xml
{
//recuperation des valeurs depuis le fichier xml
$genre=$morceau->genre;
$artist=$morceau->artist;
$album=$morceau->album;
$title=$morceau->title;
$genre=$morceau->genre;
$artist=$morceau->artist;
$album=$morceau->album;
$duration=$morceau->duration;
$file_size=$morceau->file-size;
$location=$morceau->location;
$mountpoint=$morceau->mountpoint;
$mtime=$morceau->mtime;
$first_seen=$morceau->first-seen;
$last_seen=$morceau->last-seen;
$bitrate=$morceau->bitrate;
$date=$morceau->date;
$mimetype=$morceau->mimetype;
$mb_trackid=$morceau->mb-trackid;
$mb_artistid=$morceau->mb-artistid;
$mb_albumid=$morceau->mb-albumid;
$mb_albumartistid=$morceau->mb-albumartistid;
$mb_artistsortname=$morceau->mb-artistsortname;
#on convertit pour éviter les problèmes lors de l'injection sql
$genre=utf8_decode(addslashes($genre));
$artist=utf8_decode(addslashes($artist));
$album=utf8_decode(addslashes($album));
$title=utf8_decode(addslashes($title));
$genre=utf8_decode(addslashes($genre));
$artist=utf8_decode(addslashes($artist));
$album=utf8_decode(addslashes($album));
$duration=utf8_decode(addslashes($duration));
$file_size=utf8_decode(addslashes($file_size));
$location=utf8_decode(addslashes($location));
$mountpoint=utf8_decode(addslashes($mountpoint));
$mtime=utf8_decode(addslashes($mtime));
$first_seen=utf8_decode(addslashes($first_seen));
$last_seen=utf8_decode(addslashes($last_seen));
$bitrate=utf8_decode(addslashes($bitrate));
$date=utf8_decode(addslashes($date));
$mimetype=utf8_decode(addslashes($mimetype));
$mb_trackid=utf8_decode(addslashes($mb_trackid));
$mb_artistid=utf8_decode(addslashes($mb_artistid));
$mb_albumid=utf8_decode(addslashes($mb_albumid));
$mb_albumartistid=utf8_decode(addslashes($mb_albumartistid));
$mb_artistsortname=utf8_decode(addslashes($mb_artistsortname));
$sql= "INSERT INTO `akademiach`.`musique` (`title`, `genre`, `artist`, `album`, `duration`, `file_size`, `location`, `mountpoint`, `mtime`, `first_seen`, `last_seen`, `bitrate`, `date`, `mimetype`, `mb_trackid`, `mb_artistid`, `mb_albumid`, `mb_albumartistid`, `mb_artistsortname`, `id`) VALUES ('$title', '$genre', '$artist', '$album', '$duration', '$file_size', '$location', '$mountpoint', '$mtime', '$first_seen', '$last_seen', '$bitrate', '$date', '$mimetype', '$mb_trackid', '$mb_artistid', '$mb_albumid', '$mb_albumartistid', '$mb_artistsortname', NULL);";
#echo $sql ."<br>";
$doit=mysql_query($sql);
if(!$doit) {
echo "Problème avec l'importation de:<br>" .$sql ."<hr>";
$probleme=$probleme+1;
}
}
echo "<hr>" ."Fin de l'importation des données";
if($probleme>0) {
echo "<br>Il y a eu : " .$probleme ." problèmes";
}
?>