Last active
February 27, 2022 06:02
-
-
Save jimt/c3ecf585bb5cebf68ba275937dfe691a to your computer and use it in GitHub Desktop.
Migrate SMF 2.0 Like Posts mod to SMF 2.1 Likes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* migrate likes from "Like Posts" mod to SMF 2.1 format | |
* | |
* 20220227 jimt | |
* | |
* The database schema used by the Like Posts mod for SMF 2.0 | |
* (https://custom.simplemachines.org/index.php?mod=3708) and | |
* the new built-in functionality of SMF 2.1 are different. | |
* This is a quick way to migrate old likes to the new system. | |
* | |
* Put this script in the same directory as your Settings.php | |
* file and run: | |
* php migrate_likes.php | |
*/ | |
require_once 'Settings.php'; | |
function connect(string $type, string $host, string $db, string $user, string $password): PDO { | |
try { | |
$dsn = "$type:host=$host;dbname=$db;"; | |
return new PDO( | |
$dsn, | |
$user, | |
$password, | |
[PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION] | |
); | |
} catch (PDOException $e) { | |
die($e->getMessage()); | |
} | |
} | |
$db = connect($db_type, $db_server, $db_name, $db_user, $db_passwd); | |
// find duplicates in old like table | |
echo "Prune duplicates from likes\n"; | |
$sql = "SELECT id_like, id_msg, COUNT(id_msg), id_member_gave, COUNT(id_member_gave) FROM smf_like_post GROUP BY id_msg, id_member_gave HAVING COUNT(id_msg) >1 AND COUNT(id_member_gave) > 1"; | |
$stmt = $db->prepare("DELETE FROM smf_like_post WHERE id_like = (SELECT id_like FROM smf_like_post WHERE id_msg=:id_msg AND id_member_gave=:id_member_gave ORDER BY liked_timestamp LIMIT 100000 OFFSET 1)"); | |
$stmt->bindParam(':id_msg', $id_msg); | |
$stmt->bindParam(':id_member_gave', $id_member_gave); | |
foreach ($db->query($sql) as $row) { | |
print $row['id_msg'] . "\t"; | |
print $row['id_member_gave'] . "\n"; | |
$id_msg = $row['id_msg']; | |
$id_member_gave = $row['id_member_gave']; | |
$stmt->execute(); | |
} | |
// migrate total likes per message | |
echo "Migrate total likes per message\n"; | |
$sql = 'SELECT id_msg,count(*) AS likes FROM smf_like_post GROUP BY id_msg ORDER BY id_msg'; | |
$stmt = $db->prepare("UPDATE smf_messages SET likes = likes + :newlikes WHERE id_msg = :id_msg"); | |
$stmt->bindParam(':newlikes', $likes); | |
$stmt->bindParam(':id_msg', $id_msg); | |
foreach ($db->query($sql) as $row) { | |
$id_msg = $row['id_msg']; | |
$likes = $row['likes']; | |
$stmt->execute(); | |
} | |
// migrate who liked what | |
echo "Migrate who liked what\n"; | |
$sql = 'SELECT id_member_gave, id_msg, liked_timestamp FROM smf_like_post ORDER BY liked_timestamp'; | |
$stmt = $db->prepare("INSERT INTO smf_user_likes (id_member, content_type, content_id, like_time) VALUES (:id_member, 'msg', :content_id, :like_time)"); | |
$stmt->bindParam(':id_member', $member); | |
$stmt->bindParam(':content_id', $msg); | |
$stmt->bindParam(':like_time', $timest); | |
foreach ($db->query($sql) as $row) { | |
$member = $row['id_member_gave']; | |
$msg = $row['id_msg']; | |
$timest = $row['liked_timestamp']; | |
$stmt->execute(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment