Skip to content

Instantly share code, notes, and snippets.

@KTamas
Created April 13, 2013 15:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KTamas/5378789 to your computer and use it in GitHub Desktop.
Save KTamas/5378789 to your computer and use it in GitHub Desktop.
Freeblog -> Wordpress the hackish way
<?php
ini_set('memory_limit', '256M');
ini_set('display_errors', 'on');
set_time_limit(0);
/**
* Filmbuzi.hu / pocok
*/
/**
* Converts a string to a valid docname and returns it
*
* @param string $string
* @return string
*/
function convertStringToDocname($string) {
$string = strtolower (iconv ('UTF-8', 'ASCII//TRANSLIT', $string));
$string = trim($string);
$string = str_replace (' ', '-', $string);
$string = preg_replace ('/[^-_a-z0-9]/', '', $string);
$string = ($string == 'nyeremenyjatek' ? 'nyeremenyjatek-2' : $string);
return $string;
}
function permalink($titletext) {
$permalink = $titletext;
$permalink = strtolower (iconv ('UTF-8', 'ASCII//TRANSLIT', $permalink));
$permalink = trim($permalink);
$mitmire = array ("á"=>"a","é"=>"e","í"=>"i","ó"=>"o","ö"=>"o","ő"=>"o","ú"=>"u","ü"=>"u","ű"=>"u","Á"=>"A","É"=>"E","Í"=>"I","Ó"=>"O","Ö"=>"O","Ő"=>"O","Ú"=>"U","Ü"=>"U","Ű"=>"U"," "=>"_");
$permalink=strtr($permalink, $mitmire);
$allowed = "/[^\w-]/i";
$permalink = preg_replace($allowed,"",$permalink);
return $permalink;
}
function query($query) {
global $db;
$result = mysqli_query($db, $query) or die(mysqli_error($db) . ' - QUERY: ' . $query);
return $result;
}
function quote($string) {
global $db;
return "'" . mysqli_real_escape_string($db, $string) . "'";
}
function getMappedAuthor($name) {
$authorMaps = array('gabe75' => 'sulemia');
return (isset($authorMaps[$name]) ? $authorMaps[$name] : $name);
}
$db = mysqli_connect('localhost', 'user', 'pass') or die('Unable to connect');
mysqli_select_db($db, 'databasename') or die(mysqli_error($db));
mysqli_query($db, 'SET NAMES \'utf8\'');
//mysqli_query($db, 'BEGIN');
date_default_timezone_set('Europe/Budapest');
echo "\n\nReading categories\n\n";
$categoryXml = simplexml_load_file(__DIR__ . '/categories.xml');
gc_enable();
$taxonomies = array();
foreach ($categoryXml->channel->item as $item) {
$children = $item->children('http://enyim.com/schemas/rss/core/2006');
$taxonomies['category_' . $item->guid] = array(
'name' => (string)$item->title,
'slug' => (string)$children->alias,
'taxonomy' => 'category',
'guid' => (string)$item->guid,
);
}
unset($categoryXml);
echo "\nCategory reading finished. " . count($taxonomies) . " categories read\n";
echo "\nReading entries\n";
$entryXml = simplexml_load_file(__DIR__ . '/entries.xml');
$entries = array();
$authors = array();
foreach($entryXml->channel->item as $item) {
if (!isset($authors[(string)$item->author])) {
$authors[(string)$item->author] = array('name' => (string)$item->author);
}
$row = array(
'timestamp' => strtotime((string)$item->pubDate),
'guid' => (int)$item->guid,
'title' => (string)$item->title,
'author' => (string)$item->author,
'content' => preg_replace('#filmbuzi\.hu/files#i', 'static.filmbuzi.hu/files', (string)$item->description),
'tags' => array(),
'category' => (int)$item->category,
'excerpt' => preg_replace('#filmbuzi\.hu/files#i', 'static.filmbuzi.hu/files', $item->children('http://enyim.com/schemas/rss/core/2006')->excerpt),
);
foreach ($item->children('http://enyim.com/schemas/rss/core/2006')->tag as $tag) {
if (!isset($taxonomies['tag_' . $tag])) {
$taxonomies['tag_' . $tag] = array(
'name' => $tag,
'slug' => convertStringToDocname($tag),
'taxonomy' => 'post_tag',
);
}
if (strtolower((string)$tag) == 'dune' || strtolower((string)$tag) == 'pokember') {
continue;
}
$row['tags'][] = (string)$tag;
}
$entries[$row['guid']] = $row;
}
echo "\nEntry reading finished. " . count($entries) . " entries read\n";
unset($entryXml, $item, $excerpt);
foreach ($taxonomies as $key => $taxonomy) {
$query = 'SELECT tt.term_taxonomy_id FROM wp_terms t JOIN wp_term_taxonomy tt ON tt.term_id = t.term_id WHERE
tt.taxonomy = \'' . mysqli_real_escape_string($db, $taxonomy['taxonomy']) . '\'
AND t.name = \'' . mysqli_real_escape_string($db, $taxonomy['name']) . '\'';
$res = query($query);
$row = mysqli_fetch_assoc($res);
if (empty($row['term_taxonomy_id'])) {
$slug = $taxonomy['slug'];
$slug2 = $slug;
$suffix = 1;
do {
$query = 'SELECT name FROM wp_terms WHERE slug = ' . quote($slug2);
$res = query($query);
$row = mysqli_fetch_assoc($res);
$currentSlug = $slug2;
$slug2 = $slug . '-' . ++$suffix;
} while(!empty($row));
$query = 'INSERT INTO wp_terms(name, slug, term_group) VALUES(
\'' . mysqli_real_escape_string($db, $taxonomy['name']) . '\',
\'' . mysqli_real_escape_string($db, $currentSlug) . '\',
0
)';
query($query);
$id = mysqli_insert_id($db);
$query = 'INSERT INTO wp_term_taxonomy(term_id, taxonomy, description, parent, count) VALUES(
' . $id . ',
\'' . mysqli_real_escape_string($db, $taxonomy['taxonomy']) . '\',
\'\',
0,
0
)';
query($query);
$id = mysqli_insert_id($db);
} else {
$id = $row['term_taxonomy_id'];
}
$taxonomies[$key]['id'] = $id;
}
foreach ($authors as $key => $author) {
$query = '
SELECT
ID
FROM
wp_users
WHERE
user_login = \'' . mysqli_real_escape_string($db, getMappedAuthor($author['name'])) . '\'
';
$res = query($query);
$row = mysqli_fetch_assoc($res);
$authors[$key]['id'] = $row['ID'];
}
foreach ($entries as $key => $entry) {
$query = '
INSERT INTO
wp_posts
(
post_author, post_date, post_date_gmt,
post_content, post_title, post_excerpt,
post_name
)
VALUES
(
' . $authors[$entry['author']]['id'] . ', ' . quote(date('Y-m-d H:i:s', $entry['timestamp'])) . ', ' . quote(gmdate('Y-m-d H:i:s', $entry['timestamp'])) . ',
' . quote($entry['content']) . ', ' . quote($entry['title']) . ', ' . quote($entry['excerpt']) . ',
' . quote(permalink($entry['title'])) . '
)
';
query($query);
$id = mysqli_insert_id($db);
$entries[$key]['id'] = $id;
if (!empty($entry['category'])) {
$query = '
INSERT INTO
wp_term_relationships
(
object_id, term_taxonomy_id
)
VALUES
(
' . $id . ', ' . $taxonomies['category_' . $entry['category']]['id'] . '
)
';
query($query);
}
foreach ($entry['tags'] as $tag) {
$query = '
INSERT INTO
wp_term_relationships
(
object_id, term_taxonomy_id
)
VALUES
(
' . $id . ', ' . $taxonomies['tag_' . $tag]['id'] . '
)
';
query($query);
}
}
echo "\nReading comments\n";
$commentXml = simplexml_load_file(__DIR__ . '/comments.xml');
foreach($commentXml->channel->item as $item) {
$exportItem = $item->children('http://enyim.com/schemas/blossom/export/2008');
$comment = array(
'author' => (string)$item->author,
'timestamp' => strtotime((string)$item->pubDate),
'guid' => (int)$item->guid,
'content' => (string)$item->description,
'ip' => (string)$exportItem->ip,
'entryGuid' => (int)$exportItem->entry,
'email' => (string)$exportItem->email,
);
$query = '
INSERT INTO
wp_comments
(
comment_post_ID, comment_author, comment_author_email,
comment_author_IP, comment_date, comment_date_gmt,
comment_content, comment_approved
)
VALUES
(
' . $entries[$comment['entryGuid']]['id'] . ', ' . quote($comment['author']) . ', ' . quote($comment['email']) . ',
' . quote($comment['ip']) . ', ' . quote(date('Y-m-d H:i:s', $comment['timestamp'])) . ', ' . quote(gmdate('Y-m-d H:i:s', $comment['timestamp'])) . ',
' . quote($comment['content']) . ', 1
)
';
query($query);
}
$query = '
UPDATE wp_term_taxonomy tt SET count = (SELECT count(*) FROM wp_posts p JOIN wp_term_relationships tr ON tr.object_id=p.ID WHERE tr.term_taxonomy_id = tt.term_taxonomy_id)
';
query($query);
$query = '
UPDATE wp_posts p SET comment_count = (SELECT count(*) FROM wp_comments WHERE comment_post_ID = p.ID)
';
query($query);
echo "FINISHED\n\n";
#!/usr/bin/env ruby
# encoding: utf-8
require 'open-uri'
out = File.open("comments_out2.xml", 'w')
File.open("comments.xml", 'r').each do |line|
if line.match(/<description>(\[(.+?)\]\(#\d+\))/)
line = line.gsub($1, $2)
end
out.write(line)
end
out.close
#<description>[Ági](#13446723)
`mv comments.xml comments.xml.bak2`
`mv comments_out2.xml comments.xml`
#!/usr/bin/env ruby
# encoding: utf-8
require 'open-uri'
def get_fb_profile_name(profile_number)
profile_url = "http://admin.freeblog.hu/profile/" + profile_number + "/";
content = open(profile_url).read
if content.match(/freeblog.hu &raquo; (.*) profilja/i)
return $1
end
end
out = File.open("comments_out.xml", 'w')
File.open("comments.xml", 'r').each do |line|
if line.match(/<author isAuthenticated="true">(.+?)<\/author>/)
author = get_fb_profile_name($1)
puts author
line = line.gsub($1, author)
end
out.write(line)
end
out.close
`mv comments.xml comments.xml.bak1`
`mv comments_out.xml comments.xml`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment