function check_and_update_track($trackobj, $albumindex, $artistindex, $artistname) { global $find_track, $update_track, $numdone, $prefs, $doing_search; static $current_trackartist = null; static $trackartistindex = null; $ttid = null; $lastmodified = null; $hidden = 0; $disc = 0; $uri = null; $issearchresult = 0; // Why are we not checking by URI? That should be unique, right? // Well, er. no. They're not. // Especially Spotify returns the same URI multiple times if it's in mutliple playlists // We CANNOT HANDLE that. Nor do we want to. // The other advantage of this is that we can put an INDEX on Albumindex, TrackNo, and Title, // which we can't do with Uri cos it's too long - this speeds the whole process up by a factor // of about 32 (9 minutes when checking by URI vs 15 seconds this way, on my collection) // Also, URIs might change if the user moves his music collection. if ($prefs['collection_type'] == "sqlite") { // Lord knows why, but we have to re-prepare these every single bloody time! prepare_findtracks(); } if ($find_track->execute(array($trackobj->tags['Title'], $albumindex, $trackobj->tags['Track'], $trackobj->tags['Disc'], $artistindex))) { $obj = $find_track->fetch(PDO::FETCH_OBJ); if ($obj) { $ttid = $obj->TTindex; $lastmodified = $obj->LastModified; $hidden = $obj->Hidden; $disc = $obj->Disc; $issearchresult = $obj->isSearchResult; } } else { show_sql_error(); return false; } // NOTE: It is imperative that the search results have been tidied up - // i.e. there are no 1s or 2s in the database before we do a collection update // When doing a search, we MUST NOT change lastmodified of any track, because this will cause // user-added tracks to get a lastmodified date, and lastmodified == NULL // is how we detect user-added tracks and prevent them being deleted on collection updates if ($ttid) { if (!$doing_search && $trackobj->tags['Last-Modified'] != $lastmodified || $doing_search && $issearchresult == 0 || $trackobj->tags['Disc'] != $disc && $trackobj->tags['Disc'] !== '' || $hidden != 0) { if ($prefs['debug_enabled'] > 6) { # Don't bother doing all these string comparisons if debugging is disabled. It's slow. debuglog(" Updating track with ttid {$ttid} because :", "MYSQL", 7); if (!$doing_search && $lastmodified === null) { debuglog(" LastModified is not set in the database", "MYSQL", 7); } if (!$doing_search && $trackobj->tags['Last-Modified'] === null) { debuglog(" TrackObj LastModified is NULL too!", "MYSQL", 7); } if (!$doing_search && $lastmodified != $trackobj->tags['Last-Modified']) { debuglog(" LastModified has changed: We have " . $lastmodified . " but track has " . $trackobj->tags['Last-Modified'], "MYSQL", 7); } if ($disc != $trackobj->tags['Disc']) { debuglog(" Disc Number has changed: We have " . $disc . " but track has " . $trackobj->tags['Disc'], "MYSQL", 7); } if ($hidden != 0) { debuglog(" It is hidden", "MYSQL", 7); } } $newsearchresult = 0; $newlastmodified = $trackobj->tags['Last-Modified']; if ($issearchresult == 0 && $doing_search) { $newsearchresult = $hidden != 0 ? 3 : 1; debuglog(" It needs to be marked as a search result : Value " . $newsearchresult, "MYSQL", 7); $newlastmodified = $lastmodified; } if ($update_track->execute(array($trackobj->tags['Track'], $trackobj->tags['Time'], $trackobj->tags['Disc'], $newlastmodified, $trackobj->tags['file'], $albumindex, $newsearchresult, $ttid))) { $numdone++; check_transaction(); } else { show_sql_error(); } } } else { $a = $trackobj->get_artist_string(); if ($a != $current_trackartist || $trackartistindex == null) { if ($artistname != $a && $a != null) { $trackartistindex = check_artist($a, false); } else { $trackartistindex = $artistindex; } } if ($trackartistindex == null) { debuglog("ERROR! Trackartistindex is still null!", "MYSQL_TBT", 1); return false; } $current_trackartist = $a; $sflag = $doing_search ? 2 : 0; $ttid = create_new_track($trackobj->tags['Title'], null, $trackobj->tags['Track'], $trackobj->tags['Time'], null, null, null, null, null, $trackobj->tags['file'], $trackartistindex, $artistindex, $albumindex, null, null, $trackobj->tags['Last-Modified'], $trackobj->tags['Disc'], null, null, 0, $trackobj->getImage(), $sflag); $numdone++; check_transaction(); } if ($ttid == null) { debuglog("ERROR! No ttid for track " . $trackobj->tags['file'], "MYSQL", 1); } else { if (!$doing_search) { generic_sql_query("INSERT INTO Foundtracks (TTindex) VALUES (" . $ttid . ")", false, false); } } }
function check_wishlist_doodads($ttids) { global $uri, $albumartist, $album, $albumuri, $image, $date; $donesomething = false; if ($stmt = sql_prepare_query("SELECT Uri, Albumindex FROM Tracktable WHERE TTindex = ?", $ttids[0])) { while ($ttidobj = $stmt->fetch(PDO::FETCH_OBJ)) { if ($ttidobj->Uri == null) { if ($up1 = sql_prepare_query("UPDATE Tracktable SET Uri = ? WHERE TTindex = ?", $uri, $ttids[0])) { debuglog(" .. Updated track URI for ex-wishlist item", "USERRATINGS", 5); $donesomething = true; } else { debuglog(" .. FAILED to update Track URI!", "USERRATINGS", 2); } if ($ttidobj->Albumindex == null) { $albumai = check_artist($albumartist, true); $albumindex = check_album($album, $albumai, $albumuri, $image, $date, "no", md5($albumartist . " " . $album), null, getDomain($uri), true); if ($up2 = sql_prepare_query("UPDATE Tracktable SET Albumindex = ? WHERE TTindex = ?", $albumindex, $ttids[0])) { debuglog(" .. Updated track album index for wishlist item", "USERRATINGS", 5); $donesomething = true; } else { debuglog(" .. FAILED to update Track Album Index!", "USERRATINGS", 2); } } } } } return $donesomething; }