-
Notifications
You must be signed in to change notification settings - Fork 1
/
guestStarLinks.php
156 lines (132 loc) · 5.08 KB
/
guestStarLinks.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
146
147
148
149
150
151
152
153
154
155
156
<?php
include_once "check.php";
include_once "./template/functions.php";
include_once "./template/config.php";
include_once "globals.php";
if (!isAdmin()) { die; }
$count = startImport();
echo '<span style="font:12px Verdana, Arial;">'.$count.' SQL-Statement'.($count != 1 ? 's were' : ' was').' executed!</span>'."\r\n";
function startImport() {
$count = 0;
$dbVer = fetchDbVer();
$dbh = getPDO();
try {
//$actorsSQL = 'SELECT idActor, strActor FROM actors WHERE strActor NOT LIKE "%Gast Star%" AND strActor NOT LIKE "%Guest Star%" AND strActor NOT LIKE "%Autor%";';
$actorsSQL = "SELECT ".mapDBC('idActor').", ".mapDBC('strActor')." FROM ".mapDBC('actors')." WHERE ".mapDBC('strActor')." NOT LIKE '%Gast Star%' AND ".mapDBC('strActor')." NOT LIKE '%Guest Star%' AND ".mapDBC('strActor')." NOT LIKE '%Autor%';";
$res = querySQL($actorsSQL, false, $dbh);
$actors = array();
foreach($res as $row) {
$id = $row[mapDBC('idActor')];
$str = trim($row[mapDBC('strActor')]);
$key = strtolower($str);
$key = str_replace('.', '', $key);
$key = str_replace(' ', '', $key);
$actors[$key]['str'] = $str;
$actors[$key]['id'] = $id;
}
$guestSQL = 'SELECT idEpisode,idShow,c04 FROM episode WHERE c04 LIKE "%Gast Star%" OR c04 LIKE "%Guest Star%" ORDER BY idEpisode DESC;';
$res = querySQL($guestSQL, false, $dbh);
$linkActs = array();
foreach($res as $row) {
$idEp = $row['idEpisode'];
$idShow = $row['idShow'];
$tmp = $row['c04'];
$tmp = str_replace(',', ' / ', $tmp);
$tmp = explode(" / ", $tmp);
#$gs = array();
foreach($tmp as $g) {
if (substr_count($g, 'Autor') > 0) { continue; }
if (substr_count($g, 'Gast') == 0 && substr_count($g, 'Guest') == 0) { continue; }
$g = str_replace('(Gast Star)', '', $g);
$g = str_replace('(Guest Star)', '', $g);
$g = str_replace('(Gast)', '', $g);
$g = str_replace('(Guest)', '', $g);
$g = trim($g);
$key = strtolower($g);
$key = str_replace('.', '', $key);
$key = str_replace(' ', '', $key);
$linkActs[$key]['str'] = $g;
if (empty($linkActs[$key]['idEp'])) {
$linkActs[$key]['idEp'] = array();
}
if (!in_array($idEp, $linkActs[$key]['idEp'])) {
$linkActs[$key]['idEp'][] = $idEp;
}
if (empty($linkActs[$key]['idShow'])) {
$linkActs[$key]['idShow'] = array();
}
if (!in_array($idShow, $linkActs[$key]['idShow'])) {
$linkActs[$key]['idShow'][] = $idShow;
}
}
}
$countBefore = fetchActorlinkCount($dbVer, $dbh);
if (!empty($dbh) && !$dbh->inTransaction()) { $dbh->beginTransaction(); }
foreach ($actors as $key => $actor) {
if (!isset($linkActs[$key])) { continue; }
$idActor = $actor['id'];
$strActor = $actor['str'];
$actLink = $linkActs[$key];
foreach($actLink['idEp'] as $idEp) {
$SQL = 'INSERT OR IGNORE INTO '.mapDBC('actorlinkepisode').' VALUES('.$idActor.', '.$idEp.', '.($dbVer >= 93 ? '"episode", ' : '').'"Gast Star", '.GUEST_STAR_ID.');';
execSQL($SQL, false, $dbh);
}
foreach($actLink['idShow'] as $idShow) {
$SQL = 'INSERT OR IGNORE INTO '.mapDBC('actorlinktvshow').' VALUES('.$idActor.', '.$idShow.', '.($dbVer >= 93 ? '"tvshow", ' : '').'"Gast Star", '.GUEST_STAR_ID.');';
execSQL($SQL, false, $dbh);
}
}
$NOT_LINKED = 'SELECT media_id,actor_id FROM actor_link WHERE media_type="episode" AND cast_order='.GUEST_STAR_ID.' AND actor_id NOT IN(SELECT actor_id FROM actor_link WHERE media_type="tvshow" AND cast_order='.GUEST_STAR_ID.');';
$res = querySQL($NOT_LINKED, false, $dbh);
$idEps = '';
$idEpActor = array();
if (!empty($res)) {
foreach($res as $row) {
$idEpisode = $row['media_id'];
$idEpActor[$idEpisode] = $row['actor_id'];
$idEps = $idEps.$idEpisode.',';
}
$idEps = substr($idEps, 0, -1);
}
$SHOW_EP_SQL = 'SELECT idShow,idEpisode FROM episode WHERE idEpisode IN('.$idEps.');';
$res = querySQL($SHOW_EP_SQL, false, $dbh);
$fixed = 0;
if (!empty($res)) {
foreach($res as $row) {
$fixed++;
$idShow = $row['idShow'];
$idEpisode = $row['idEpisode'];
$idActor = $idEpActor[$idEpisode];
$SQL = 'INSERT OR IGNORE INTO '.mapDBC('actorlinktvshow').' VALUES('.$idActor.', '.$idShow.', '.($dbVer >= 93 ? '"tvshow", ' : '').'"Gast Star", '.GUEST_STAR_ID.');';
execSQL($SQL, false, $dbh);
}
}
$countAfter = fetchActorlinkCount($dbVer, $dbh);
$count = $countAfter - $countBefore;
if (!empty($dbh) && $dbh->inTransaction()) {
if ($count > 0) { $dbh->commit(); }
else { $dbh->rollBack(); }
}
} catch(PDOException $e) {
echo $e;
if (!empty($dbh) && $dbh->inTransaction()) { $dbh->rollBack(); }
}
return $count;
}
function fetchActorlinkCount($dbVer, $dbh = null) {
$count = 0;
$res = fetchFromDB("SELECT COUNT(*) AS count FROM ".mapDBC('actorlinkepisode').";", false, $dbh);
if (!empty($res)) {
$count += $res['count'];
}
if ($dbVer >= 93) {
//dbVer 93 an above, the tables are the same
return $count;
}
$res = fetchFromDB("SELECT COUNT(*) AS count FROM ".mapDBC('actorlinktvshow').";", false, $dbh);
if (!empty($res)) {
$count += $res['count'];
}
return $count;
}
?>