-
Notifications
You must be signed in to change notification settings - Fork 0
/
myth-migrate-playlists.php
145 lines (111 loc) · 3.69 KB
/
myth-migrate-playlists.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<?php
# myth-migrate-playlists.php v0.1
# (c) Nicolas Krzywinski http://www.nskComputing.de
#
# Created: 2018 by Nicolas Krzywinski
# Description: Migrates playlists from one MythTV database to a new one, while converting the song ids
# Last Changed: 2018-07-25
# Change Desc: Created
# Remarks: This is a quick and dirty script - USE AT YOUR OWN RISK!
# License: GPL
# SETTINGS
$db_host = "hostname-of-your-mysql-system";
$db_user = "youruser-with-access-to-both-dbs";
$db_pwd = "yourpassword";
$old_db = "mythconverg_old";
$new_db = "mythconverg";
# CONNECT
$mysqli = new mysqli($db_host, $db_user, $db_pwd, $old_db);
if ($mysqli->connect_errno)
{
echo "<h1>Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error . "</h1>";
exit;
}
echo "<h3>Connected to: " . $mysqli->host_info . "</h3>\n";
$res = $mysqli->query("SET NAMES 'utf8'");
if ($res === false)
{
echo "<h1>Error: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error . "</h1>";
}
# QUERY OLD LIST
$res = $mysqli->query("select * from $old_db.music_playlists");
if ($res === false)
{
echo "<h1>Error: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error . "</h1>";
}
$res->data_seek(0);
while ($row = $res->fetch_assoc())
{
$oldlists[$row["playlist_name"]] = $row;
}
echo "<h1>## PROCESS EACH PLAYLIST ##</h1>";
foreach ($oldlists as $listname => $oldlist)
{
unset($namearray);
unset($filenames);
unset($idarray);
unset($songids);
unset($insert);
echo "<h2>Processing playlist '$listname' ...</h2>";
echo "<ul>";
if ($oldlist["playlist_songs"] == "")
{
echo "<li>Playlist empty, skipping.</li></ul>";
continue;
}
echo "<li><b># QUERY SONG FILENAMES</b></li>";
$sql = "select filename from $old_db.music_songs where song_id in (" . $oldlist["playlist_songs"] . ")";
$res = $mysqli->query($sql);
if ($res === false)
{
echo "<h1>Error: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error . "</h1>";
echo "<li>SQL: $sql</li>";
continue;
}
$res->data_seek(0);
while ($row = $res->fetch_assoc())
{
$namearray[] = "'" . $mysqli->real_escape_string($row["filename"]) . "'";
}
$filenames = implode(",", $namearray);
echo "<li>Detected " . count($namearray) . " songs for playlist '$listname': " . htmlentities($filenames, ENT_SUBSTITUTE) . "</li>";
echo "<li><b># GET NEW SONG IDS</b></li>";
$sql = "select song_id from $new_db.music_songs where filename in (" . $filenames . ")";
$res = $mysqli->query($sql);
if ($res === false)
{
echo "<h1>Error: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error . "</h1>";
echo "<li>SQL: " . htmlentities($sql) . "</li>";
continue;
}
$res->data_seek(0);
while ($row = $res->fetch_assoc())
{
$idarray[] = $row["song_id"];
}
$songids = implode(",", $idarray);
echo "<li>Detected new song ids for playlist '$listname': $songids</li>";
echo "<li><b># BUILD INSERT</b></li>";
$insert = "INSERT INTO $new_db.music_playlists VALUES(NULL, '$listname', '$songids', '" .
$oldlist["last_accessed"] . "', " . $oldlist["length"] . ", " . $oldlist["songcount"] . ", '" . $oldlist["hostname"] . "')";
echo "<li>So, what about this insert? $insert</li>";
if (@$_GET["mode"] == "insert")
{
echo "<li>INSERT mode is set, executing this insert now ...";
$res = $mysqli->query($insert);
if ($res === false)
{
echo "</li><h1>Error: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error . "</h1>";
}
else
{
echo " done (id=" . $mysqli->insert_id . ")</li>";
}
}
else
{
echo "<li>If this insert (and all others) seem ok, use get parameter 'mode=insert' to execute all the inserts, or click here: <a href=\"" . $PHP_SELF . "?mode=insert\">Reload with INSERTs ENABLED!</a></li>";
}
echo "</ul>";
}
?>