Skip to content

Instantly share code, notes, and snippets.

@phistep
Created April 29, 2012 21:27
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 phistep/2553348 to your computer and use it in GitHub Desktop.
Save phistep/2553348 to your computer and use it in GitHub Desktop.
mdbl0g Wordpress Importer (https://github.com/Ps0ke/mdbl0g)
<?php
$outputdir = './mdbl0g-wp-import-posts';
$db = @new MySQLi('localhost', 'username', 'password', 'database');
/* *************************** */
error_reporting(0);
if (mysqli_connect_errno())
die('MySQL error occured:'.mysqli_connect_error());
clearstatcache();
$permissions = substr(sprintf('%o', fileperms('.')), -4);
if(!is_writable('.')){
echo '<b>WARNING: file permissions (<code>'.$permissions.'</code>) do not allow to create the output directory automatically. Attempting to modify the permissions...</b><br><br>';
if(chmod(".", 0757))
echo "<b>success!</b>";
else
echo "<b>ERROR: could not modifiy file permissions! You need to create the directory <code>".$outputdir."</code> manually!</b><br><br>";
}
// Reference: https://codex.wordpress.org/Database_Description#Table:_wp_posts
$query =
"SELECT
ID,
post_title AS title,
post_content AS content,
DATE_FORMAT(post_date_gmt,'%Y-%m-%d_%H-%i') AS date
FROM
wp_posts
WHERE
post_type = 'post'";
// AND post_status = 'publish'
$result = $db->query($query)
or die('Query error: '.$db->error);
if ($result->num_rows) {
echo "<html><head><title>mdbl0g wordpress importer</title></head><body>\n";
echo '<a href="?export">export to <code>'.$outputdir.'</code></a><br>(Content is viewable in the page source)<br><br>'."\n";
echo '<table border="1" cellpadding="5">'."\n<thead><tr><td>ID</td><td>Date</td><td>Title</td></tr></thead>\n<tbody>\n";
while ($row = $result->fetch_assoc()) {
echo '<tr><td>'.$row['ID'].'</td><td>'.$row['date'].'</td><td>'.$row['title']."</td></tr>\n";
echo "<!-- \n".$row['content']." \n -->\n";
if(isset($_GET['export'])){
if(!file_exists($outputdir)) mkdir($outputdir);
if(file_put_contents($outputdir.'/'.$row['date'].'.md', utf8_encode($row['title'])."\n".utf8_encode($row['content'])))
echo '<b>'.$row['ID']." exported!</b><br>\n";
else
echo '<b>'.$row['ID']." NOT exported!</b><br>\n";
}
}
echo "\n</tbody></table></body></html>\n";
}
else
echo "Noting found. Maybe check your SQL credentials?";
@chmod('.', $permissions);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment