Last active
December 15, 2015 10:49
-
-
Save freekrai/5248135 to your computer and use it in GitHub Desktop.
Secondcrack to Wordpress
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 | |
/* | |
Move second crack files to wordpress: | |
1) Create a folder called "migrate", and upload this file to that folder. | |
2) Upload the PHPMarkdownExtra and PHP Smartypants libraries to this folder as well. | |
3) Move the posts folder from your second crack site into this folder. | |
4) In your browser, run: http://mysite/migrate/migrate.php and let it run. | |
This will move all posts in your posts folder into wordpress, it doesn't touch pages, only posts. | |
One final note, this worked for me perfectly, you may have to play around with it a little for your own set up. | |
*/ | |
$fdir = dirname(__FILE__); | |
require_once($fdir . '/PHPMarkdownExtra/markdown.php'); | |
require_once($fdir . '/PHPSmartyPants/smartypants.php'); | |
set_time_limit(0); | |
ignore_user_abort( true ); | |
date_default_timezone_set('America/Vancouver'); | |
require('../wp-load.php'); | |
$posts = array(); | |
$folder = "posts"; | |
// /$year/$month | |
$all_files = filelist( "posts/", true); | |
# echo "<pre>".print_r( $all_files,true )."</pre>"; | |
$i = 0; | |
foreach($all_files as $fullpath=>$v){ | |
$orig = new Post($fullpath); | |
$row = $orig->array_for_template(); | |
# echo "<pre>".print_r($row,true)."</pre><hr />"; | |
$post = array(); | |
$perma = $row['post-permalink']; | |
$post['post_status'] = 'publish'; | |
$post['post_category'] = catList( $orig->tags ); | |
$post['post_date'] = date('Y-m-d H:i:s',$orig->timestamp); | |
$post['post_title'] = $row['post-title']; | |
$post['post_content'] = $row['post-body']; | |
$post['body'] = $orig->body; | |
$post['guid'] = $row['post-absolute-permalink']; | |
if( !empty($orig->headers['link']) ){ | |
$post['link'] = $orig->headers['link']; | |
} | |
// $post['tags'] = $row->tags; | |
$posts[ $perma ] = $post; | |
$i++; | |
} | |
#echo "<pre>".print_r($posts,true)."</pre><hr />"; | |
foreach ($posts as $id=>$post) { | |
$tags = $post['tags']; | |
$link = $post['link']; | |
$body = $post['body']; | |
unset( $post['body'] ); | |
unset( $post['link'] ); | |
unset( $post['tags'] ); | |
$pid = wp_insert_post($post); | |
if( !empty($pid) ){ | |
add_post_meta($pid,'orig',$body); | |
add_post_meta($pid, 'orig_id', $id); | |
add_post_meta($pid, 'tags', $tags); | |
add_post_meta($pid, 'link', $link); | |
} | |
} | |
function catList($makes){ | |
$taxIDs = array(); | |
foreach($makes as $make) { | |
$make = trim($make); | |
$make = str_replace(" "," ",$make); | |
if( empty($make) ) continue; | |
$term = get_term_by( 'name', $make, 'category' ); | |
if( $term->term_id ){ | |
$taxIDs[] = $term->term_id; | |
}else{ | |
$term = wp_insert_term( | |
$make, | |
'category', | |
array( | |
'description'=> $make, | |
'slug' => $make, | |
'parent'=> 0 | |
) | |
); | |
if( is_array($term) ){ | |
$taxIDs[] = $term['term_id']; | |
}else{ | |
} | |
} | |
} | |
return $taxIDs; | |
} | |
function filelist($dir, $parse_info_in_text_files = true, $use_cached_info = array(), &$deleted_files = array()) | |
{ | |
$out = array(); | |
if (is_dir($dir)) { | |
if ($dh = opendir($dir)) { | |
while ( ($file = readdir($dh) ) !== false) { | |
if ($file[0] == '.') continue; | |
$fullpath = $dir . '/' . $file; | |
if (is_dir($fullpath)) { | |
$out = array_merge($out, filelist($fullpath, $parse_info_in_text_files, $use_cached_info, $deleted_files)); | |
} else { | |
if (isset($deleted_files[$fullpath])) unset($deleted_files[$fullpath]); | |
$new_stat_prefix = filemtime($fullpath) . '-' . filesize($fullpath); | |
if (isset($use_cached_info[$fullpath])) { | |
$stat_prefix = substr($use_cached_info[$fullpath], 0, strpos($use_cached_info[$fullpath], '|')); | |
if ($stat_prefix == $new_stat_prefix) continue; | |
} | |
$tags = ''; | |
$type = ''; | |
if ($parse_info_in_text_files && substr($fullpath, -(strlen('.txt'))) == '.txt') { | |
$post = new Post($fullpath); | |
$tags = implode(',', $post->tags); | |
$type = $post->type; | |
$expected_fname = $post->expected_source_filename(); | |
#echo $expected_fname."<br />"; | |
if ($expected_fname != $fullpath && ! $post->is_draft) { | |
$fullpath = $expected_fname; | |
} | |
} | |
$out[$fullpath] = $new_stat_prefix . '|' . $type . '|' . $tags; | |
} | |
} | |
closedir($dh); | |
} | |
} | |
return $out; | |
} | |
class Post{ | |
public $type; | |
public $tags = array(); | |
public $expected_source_filename; | |
public $is_draft; | |
public $source_filename; | |
public $timestamp; | |
public $year; | |
public $month; | |
public $day; | |
public $offset_in_day; | |
public $slug; | |
public $body; | |
public $headers = array(); | |
public $publish_now; | |
public $title; | |
public function __construct($source_filename, $is_draft = -1 /* auto */){ | |
$this->source_filename = $source_filename; | |
$this->is_draft = ($is_draft === -1 ? (false !== strpos($source_filename, 'drafts/') || false !== strpos($source_filename, 'pages/')) : $is_draft); | |
$this->timestamp = filemtime($source_filename); | |
$segments = explode("\n\n", trim(file_get_contents($source_filename)), 2); | |
if (! isset($segments[1])) $segments[1] = ''; | |
if (count($segments) > 1) { | |
$headers = explode("\n", $segments[0]); | |
$has_title_yet = false; | |
foreach ($headers as $header) { | |
if (isset($header[0]) && $header[0] == '=') { | |
$has_title_yet = true; | |
continue; | |
} | |
if (! $has_title_yet) { | |
$has_title_yet = true; | |
$this->title = $header; | |
continue; | |
} | |
if ($this->is_draft && strtolower($header) == 'publish-now') { | |
$this->publish_now = true; | |
continue; | |
} | |
$fields = explode(':', $header, 2); | |
if (count($fields) < 2) continue; | |
$fname = strtolower($fields[0]); | |
$fields[1] = trim($fields[1]); | |
if ($fname == 'tags') { | |
$this->tags = $this->parse_tag_str($fields[1]); | |
} else if ($fname == 'type') { | |
$this->type = str_replace('|', ' ', $fields[1]); | |
} else if ($fname == 'published') { | |
$this->timestamp = strtotime($fields[1]); | |
} else { | |
$this->headers[$fname] = $fields[1]; | |
} | |
if (isset($this->headers['link'])) $this->type = 'link'; | |
} | |
array_shift($segments); | |
} | |
$this->body = isset($segments[0]) ? $segments[0] : ''; | |
# $this->body = SmartyPants( Markdown( $this->body ) ); | |
$filename = basename($source_filename); | |
$filename_datestr = substr($filename, 0, 8); | |
if (is_numeric($filename_datestr)) { | |
$this->year = intval(substr($filename_datestr, 0, 4)); | |
$this->month = intval(substr($filename_datestr, 4, 2)); | |
$this->day = intval(substr($filename_datestr, 6, 2)); | |
$this->offset_in_day = intval(substr($filename, 9, 2)); | |
$this->slug = ltrim(substr($filename, 11, -(strlen('.txt'))), '-'); | |
} else { | |
$this->year = intval(idate('Y', $this->timestamp)); | |
$this->month = intval(idate('m', $this->timestamp)); | |
$this->day = intval(idate('d', $this->timestamp)); | |
$posts_in_ym = Updater::posts_in_year_month($this->year, $this->month); | |
$this->offset_in_day = isset($posts_in_ym[$this->day]) ? count($posts_in_ym[$this->day]) + 1 : 1; | |
$this->slug = substring_before($filename, '.'); | |
} | |
# echo "----> ".$source_filename." ---- ".$this->expected_source_filename()."<br />"; | |
if (! $this->is_draft && $source_filename != $this->expected_source_filename()) { | |
# die("Expected filename mismatch:\nfilename: $source_filename\nexpected: " . $this->expected_source_filename()); | |
} | |
} | |
public function array_for_template(){ | |
$tags = array(); | |
foreach ($this->tags as $tag) { $tags[$tag] = array('post-tag' => $tag); } | |
// Convert relative image references to absolute so index pages work | |
$base_uri = '/' . $this->year . '/' . str_pad($this->month, 2, '0', STR_PAD_LEFT) . '/' . str_pad($this->day, 2, '0', STR_PAD_LEFT); | |
return array_merge( | |
$this->headers, | |
array( | |
'post-title' => html_entity_decode(SmartyPants($this->title), ENT_QUOTES, 'UTF-8'), | |
'post-slug' => $this->slug, | |
'post-timestamp' => $this->timestamp, | |
'post-rss-date' => date('D, d M Y H:i:s T', $this->timestamp), | |
'post-body' => SmartyPants(Markdown($this->body)), | |
'post-tags' => $tags, | |
'post-type' => $this->type, | |
'post-permalink' => $base_uri . '/' . $this->slug, | |
'post-permalink-or-link' => isset($this->headers['link']) && $this->headers['link'] ? $this->headers['link'] : $base_uri . '/' . $this->slug, | |
'post-absolute-permalink' => rtrim('http://rogerstringer.com', '/') . $base_uri . '/' . $this->slug, | |
'post-absolute-permalink-or-link' => rtrim('http://rogerstringer.com', '/') . (isset($this->headers['link']) && $this->headers['link'] ? $this->headers['link'] : $base_uri . '/' . $this->slug), | |
'post-is-first-on-date' => $this->is_first_post_on_this_date ? 'yes' : '', | |
) | |
); | |
} | |
public function expected_source_filename($for_new_file = false){ | |
$padded_month = str_pad($this->month, 2, '0', STR_PAD_LEFT); | |
$padded_day = str_pad($this->day, 2, '0', STR_PAD_LEFT); | |
$prefix = 'posts/' . $this->year . '/' . $padded_month . '/' . $this->year . $padded_month . $padded_day . '-'; | |
if ($for_new_file) $padded_offset = self::next_sequence_number_for_day($prefix); | |
else $padded_offset = str_pad($this->offset_in_day, 2, '0', STR_PAD_LEFT); | |
if ($for_new_file) { | |
$slug = trim(preg_replace('/[^a-z0-9-]+/ms', '-', strtolower($this->slug)), '-'); | |
if (! $slug) $slug = 'post'; | |
} else { | |
$slug = $this->slug; | |
} | |
# echo( $prefix . $padded_offset . '-' . strtolower($slug) . '.txt' ); | |
return $prefix . $padded_offset . '-' . strtolower($slug) . '.txt'; | |
} | |
public static function next_sequence_number_for_day($filename_prefix){ | |
$max = 0; | |
foreach (glob($filename_prefix . '*' . '.txt') as $filename) { | |
$n = intval(substr(substring_after($filename, $filename_prefix), 0, 2)); | |
if ($n > $max) $max = $n; | |
} | |
return str_pad($max + 1, 2, '0', STR_PAD_LEFT); | |
} | |
public function parse_tag_str($tag_str){ | |
// Tags are comma-separated, and any spaces between multiple words in a tag will be converted to underscores for URLs | |
if (! strlen($tag_str)) return array(); | |
$tags = array_map('trim', explode(',', strtolower($tag_str))); | |
$tags = str_replace(' ', '_', $tags); | |
$tags = array_unique(array_filter($tags, 'strlen')); | |
sort($tags); | |
return $tags; | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment