Spleender. If you want import all, use Splinder/Motime Importer Plugin.
Version: Alpha 0.2
Author: Francesco Terenzani
Author URI: http://www.terenzani.it/
Template model:
*/
## Get Contents functions ##
function splinder_get_pages($url){
$contents = file_get_contents($url);
if($contents){
# Last page
preg_match("#(ultima|last page)#",$contents,$end);
# Skip per page
preg_match("#2#",$contents,$page);
# Page 1
$urls[] = $url;
# All pages
$skip = $page[1];
while($page[1]<$end[1]){
$urls[] = $url ."?from=$page[1]";
$page[1] = $page[1]+$skip;
}
$urls[] = $url ."?from=$page[1]";
return $urls;
}
return false;
}
function splinder_get_permalinks($urls){
if(is_array($urls)){
$posts = array();
foreach($urls as $u){
$contents = file_get_contents($u);
# Permalinks
preg_match_all("##",$contents,$post_urls);
$posts = array_merge($posts, $post_urls[1]);
}
return $posts;
}
return false;
}
function splinder_traffic_light($url){
$url = splinder_sanitize_url($url);
## Already started?
$blogs = get_option('splinder_to_import_blogs');
$blogs = ($blogs) ? $blogs : array();
if(!in_array($url, $blogs)){
ini_set('max_execution_time',0);
$pages = splinder_get_pages($url);
if($pages){
$posts = splinder_get_permalinks($pages);
if($posts){
$posts_string = $url .implode($posts, "||".$url);
$posts = array_reverse(explode("||", $posts_string));
update_option('splinder_to_import_urls', $posts);
}
$blogs[] = $url;
update_option('splinder_to_import_blogs', $blogs);
return $url;
}
return false;
}
return $url;
}
function splinder_core(){
$posts = get_option('splinder_to_import_urls');
if($posts){
$saved = get_option('splinder_saved');
$saved = ($saved) ? $saved : array();
$posts = array_diff($posts, $saved);
if(!empty($posts)){
# Funzioni per salvare post e aggiornare opzione saved
// $return = splinder_save(current($posts));
// +
$return = spleender_save(current($posts));
if($return){
$saved[] = current($posts);
update_option('splinder_saved', $saved);
} else {
$skip = array_shift($posts);
$error = get_option('spleender_error_urls');
$error[] = $skip;
update_option('spleender_error_urls',$error);
update_option('splinder_to_import_urls',$posts);
return 'error:'.current($posts);
}
return current($posts);
} else {
// splinder_update_cats();
return 'end';
}
}
return false;
}
function spleender_save($post_url){
$content = splinder_get_post($post_url);
if($content){
# Getting
#WP-Blog are not UTF-8 encoded
if(strtoupper(get_option('blog_charset')) != 'UTF-8'){
$content = splinder_utf8_decode($content);
}
$date = splinder_get_date($content);
$comments = splinder_get_comments($content);
if($comments){
$comDate = splinder_com_date($comments);
$comText = splinder_com_text($comments);
$comAuthor = splinder_com_author($comments);
$comCount = count($comDate[0]);
if($comCount != count($comText[0]) || $comCount != count($comAuthor[2]) || $comCount != count($comAuthor[3])){
$error_log = get_option('splinder_error_log');
update_option('splinder_error_log', $error_log."Comments error: $post_url\n");
}
}
# Saving
$postID = spleender_get_post($date);
if($postID){
splinder_save_comments($postID, $comAuthor, $comDate, $comText);
spleender_update_com_num($postID);
return true;
}
return false;
}
return false;
}
function spleender_get_post($date){
global $wpdb;
$date = substr($date,0, -2);
$postID = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_date LIKE '$date%' LIMIT 1");
# Brute force search :P
if(!is_numeric($postID)){
$date = substr($date,0, 11) .'%' .substr($date, -4);
# Output: aaaa-mm-gg %:mm:
$postID = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_date LIKE '$date%' LIMIT 1");
if(!is_numeric($postID)){
$dayX = substr($date, 0, 10);
$m = substr($dayX, 5, 2);
$d = substr($dayX, 8, 2);
$y = substr($dayX, 0, 4);
$onedayafter = date("Y-m-d", mktime( 0, 0, 0, $m, $d, $y)+86400);
$date = str_replace($dayX, $onedayafter, $date);
$postID = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_date LIKE '$date%' LIMIT 1");
if(!is_numeric($postID)){
$dayX = substr($date, 0, 10);
$m = substr($dayX, 5, 2);
$d = substr($dayX, 8, 2);
$y = substr($dayX, 0, 4);
$onedayleft = date("Y-m-d", mktime( 0, 0, 0, $m, $d, $y)-172800);
$date = str_replace($dayX, $onedayleft, $date);
$postID = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_date LIKE '$date%' LIMIT 1");
}
}
}
return $postID;
}
function spleender_update_com_num($id){
global $wpdb;
$count = $wpdb->get_var("SELECT count(comment_post_ID) FROM $wpdb->comments WHERE comment_post_ID = $id");
if(!empty($count) && $count > 0){
$wpdb->query("UPDATE $wpdb->posts SET comment_count = '$count' WHERE ID = $id");
}
}
function splinder_get_post($post_url){
$content = file_get_contents($post_url);
if($content) return $content;
return false;
}
// Critical function... Splinder don't return everytime the same date format...
function splinder_get_date($content){
preg_match("##", $content, $postDate);
if(empty($postDate)) return false;
// if year is 2 chars length
if(strlen($postDate[3])==2){
// If > 7X = 197X; else 20XX... 64 years compatible :P
$postDate[3] = (substr($postDate[3],0,1)>7) ? "19".$postDate[3] : "20".$postDate[3];
return $postDate = "$postDate[3]-$postDate[1]-$postDate[2] $postDate[4]:00";
}
return $postDate = "$postDate[3]-$postDate[2]-$postDate[1] $postDate[4]:00";
}
//****** Comments
function splinder_get_comments($content){
preg_match("##s", $content, $comments);
if(!empty($comments)) return $comments[0];
return false;
}
function splinder_com_date($comments){
preg_match_all("#:right\">([0-9]{1,2}) ([\w]+) ([0-9]{4}) - ([0-9]{2}:[0-9]{2})#", $comments, $comDate);
if(!empty($comDate)) return $comDate;
return false;
}
function splinder_com_text($comments){
preg_match_all("#
(.+)
#Us", $comments, $comBody);
if(!empty($comBody)) return $comBody;
return false;
}
function splinder_com_author($comments){
preg_match_all("#[\r\n\t]+([\r\n\t]+ | [\r\n\t]+([\w\d \._-]+)[\r\n\t]+ | #Us", $comments, $comAuthor);
if(empty($comAuthor[0])){
preg_match_all("#