nurjns icon

MigrateBlogToWordPress Script

nurjns | PRO | 11/19/23 03:49:57 PM UTC (Edited) | 0 ⭐ | 1233 👁️ | Never ⏰ | []
PHP |

7.56 KB

|

None

|

0 👍

/

0 👎

<?php
 
/*
1. "wp_posts" leeren
2. "wp_term_relationships" leeren
3. "wp_comments" leeren
4. "wp_term_taxonomy" importieren
5. "wp_terms" importieren
6. Script aufrufen
7. Blog aufrufen
*/
 
/*
EINSTELLUNGEN START
Tabellen und Spalten müssen im Script entsprechend angepasst werden.
*/
 
$domain = 'MeinBlog.tld';
 
// Verbindung zur alten Datenbank herstellen
$oldDb = new mysqli('localhost', 'BENUTZERNAME_HIER', 'PASSWORT_HIER', 'DATENBANK_HIER');
 
// Verbindung zur neuen Datenbank herstellen
$newDb = new mysqli('localhost', 'BENUTZERNAME_HIER', 'PASSWORT_HIER', 'DATENBANK_HIER');
 
/*
EINSTELLUNGEN ENDE
*/
 
// Überprüfen, ob die Verbindungen erfolgreich waren
if ($oldDb->connect_error || $newDb->connect_error) {
    die('Verbindungsfehler: ' . $oldDb->connect_error . ' | ' . $newDb->connect_error);
}
 
// SQL-Abfrage für den Datenbanktransfer
$query = "SELECT * FROM tipps";
$result = $oldDb->query($query);
 
// Überprüfen, ob die Abfrage erfolgreich war
if (!$result) {
    die('Abfragefehler: ' . $oldDb->error);
}
 
$i_blog = 0;
$i_comments = 0;
 
// Blog-Beiträge migrieren
while ($row = $result->fetch_assoc()) {
    $i_blog++;
    // Inhalte ersetzen und formatieren
    $postTitle = mysqli_real_escape_string($newDb, $row['titel']);
    $postContent = mysqli_real_escape_string($newDb, $row['inhalt']);
    $postDate = date('Y-m-d H:i:s', $row['datum']);
    $postModified = date('Y-m-d H:i:s', $row['lastedit']);
    $postAuthor = 1;
 
    // Weitere Inhalte generieren
    $postName = strtolower(str_replace(
        ['--', 'ß', 'ä', 'ü', 'ö', 'Ä', 'Ü', 'Ö'],
        ['-','ss','ae','ue','oe','ae','ue','oe'],
        preg_replace('/[^A-Za-z0-9\-]/', '', str_replace(
            ' ',
            '-',
            str_replace('&', 'und', str_replace(
                '&amp;',
                'and',
                str_replace(
                    ' / ',
                    '-',
                    str_replace(
                        '.',
                        '-',
                        str_replace(
                            ' – ',
                            '-',
                            str_replace(
                                ' - ',
                                '-',
                                str_replace(
                                    '+',
                                    'plus',
                                    $postTitle
                                )
                            )
                        )
                    )
                )
            )
        ))
    )));
 
    $guid = 'https://'.strtolower($domain).'/?p=' . $row['id'];
 
    // SQL-Abfrage für den Datenbankeinfügung in die neue Tabelle wp_posts
    $insertQuery = "INSERT INTO wp_posts (ID, post_author, post_date, post_date_gmt, post_content, post_title, post_excerpt, post_status, comment_status, ping_status, post_password, post_name, to_ping, pinged, post_modified, post_modified_gmt, post_content_filtered, post_parent, guid, menu_order, post_type, post_mime_type, comment_count)
                    VALUES (NULL, $postAuthor, '$postDate', '$postDate', '$postContent', '$postTitle', '', 'publish', 'open', 'open', '', '$postName', '', '', '$postModified', '$postModified', '', 0, '$guid', 0, 'post', '', 0)";
 
    // Überprüfen, ob die Abfrage erfolgreich war
    if (!$newDb->query($insertQuery)) {
        die('Fehler beim Einfügen: ' . $newDb->error);
    }
 
    // Kategorie-ID aus der alten Tabelle "tipps"
    $alteKategorieId = $row['kategorie_id'];
 
    // Neue Kategorie-ID in "wp_terms" um 1 erhöhen
    $neueKategorieId = $alteKategorieId + 1;
 
    // SQL-Abfrage für den Datenbankeinfügung in die neue Tabelle wp_term_relationships
    $insertTermRelationshipsQuery = "INSERT INTO wp_term_relationships (object_id, term_taxonomy_id, term_order)
                                    VALUES (LAST_INSERT_ID(), $neueKategorieId, 0)";
 
    // Überprüfen, ob die Abfrage erfolgreich war
    if (!$newDb->query($insertTermRelationshipsQuery)) {
        die('Fehler beim Einfügen in wp_term_relationships: ' . $newDb->error);
    }
 
    // SQL-Abfrage für die Aktualisierung der count-Spalte in wp_term_taxonomy
    $updateTermTaxonomyQuery = "UPDATE wp_term_taxonomy SET count = count + 1 WHERE term_id = $neueKategorieId";
 
    // Überprüfen, ob die Abfrage erfolgreich war
    if (!$newDb->query($updateTermTaxonomyQuery)) {
        die('Fehler beim Aktualisieren von wp_term_taxonomy: ' . $newDb->error);
    }
}
 
// Kommentare migrieren
// SQL-Abfrage für die Kommentare in comments
$commentsQuery = "SELECT * FROM comments ORDER BY datum ASC";
$commentsResult = $oldDb->query($commentsQuery);
 
// SQL-Abfrage für die Antworten in comments_answers
$answersQuery = "SELECT * FROM comments_answers ORDER BY datum ASC";
$answersResult = $oldDb->query($answersQuery);
 
// Antworten in ein Array umwandeln
$answersArray = array();
while ($answerRow = $answersResult->fetch_assoc()) {
    $answersArray[] = $answerRow;
}
 
// Schleife für Kommentare
while ($commentRow = $commentsResult->fetch_assoc()) {
    $i_comments++;
    // Inhalte formatieren
    $commentPostID = $commentRow['tipp_id'];
    $commentAuthor = mysqli_real_escape_string($newDb, $commentRow['name']);
    $commentDate = date('Y-m-d H:i:s', $commentRow['datum']);
    $commentContent = mysqli_real_escape_string($newDb, $commentRow['kommentar']);
    $commentEmail = $commentRow['email'];
 
    // SQL-Abfrage für den Datenbankeinfügung in die neue Tabelle wp_comments
    $insertCommentQuery = "INSERT INTO wp_comments (comment_post_ID, comment_author, comment_date, comment_date_gmt, comment_content, comment_author_email, comment_approved, comment_type, comment_parent, user_id)
                           VALUES ($commentPostID, '$commentAuthor', '$commentDate', '$commentDate', '$commentContent', '$commentEmail', 1, 'comment', 0, $user_id)";
 
    // Überprüfen, ob die Abfrage erfolgreich war
    if (!$newDb->query($insertCommentQuery)) {
        die('Fehler beim Einfügen von Kommentaren: ' . $newDb->error);
    }
 
    // Schleife für Antworten
    foreach ($answersArray as $answerRow) {
        // Wenn die Antwort zur aktuellen Kommentar-ID gehört
        if ($answerRow['comment_id'] == $commentRow['id']) {
            // Inhalte formatieren
            $answerPostID = $answerRow['tipp_id'];
            $answerAuthor = mysqli_real_escape_string($newDb, $answerRow['name']);
            $answerDate = date('Y-m-d H:i:s', $answerRow['datum']);
            $answerContent = mysqli_real_escape_string($newDb, $answerRow['antwort']);
            $answerEmail = $answerRow['email'];
 
            // SQL-Abfrage für den Datenbankeinfügung in die neue Tabelle wp_comments für Antworten
            $insertAnswerQuery = "INSERT INTO wp_comments (comment_post_ID, comment_author, comment_date, comment_date_gmt, comment_content, comment_author_email, comment_approved, comment_type, comment_parent, user_id)
                                 VALUES ($answerPostID, '$answerAuthor', '$answerDate', '$answerDate', '$answerContent', '$answerEmail', 1, 'comment', LAST_INSERT_ID(), $user_id)";
 
            // Überprüfen, ob die Abfrage erfolgreich war
            if (!$newDb->query($insertAnswerQuery)) {
                die('Fehler beim Einfügen von Antworten: ' . $newDb->error);
            }
        }
    }
}
 
// Verbindungen schließen
$oldDb->close();
$newDb->close();
 
echo 'Migration abgeschlossen! '.$i_blog.' Blog-Beiträge und '.$i_comments.' Kommentare wurden migriert.';
?>

Comments