[ Index ] |
PHP Cross Reference of Textpattern 4.0.8 |
[Summary view] [Print] [Text view]
1 <?php 2 //Long live zem! 3 4 //Time for 45KB: 0.9648-1.3698sec. 5 function doImportBLOGGER($file, $section, $status, $invite) { 6 7 # Parse a file in the MT Import Format, as described here: 8 # http://www.movabletype.org/docs/mtimport.html 9 # This doesn't interpret the data at all, just parse it into 10 # a structure. 11 12 $fp = fopen($file, 'r'); 13 if (!$fp) 14 return false; 15 16 //Keep some response on some part 17 $results = array(); 18 $multiline_type = ''; 19 $multiline_data = ''; 20 $state = 'metadata'; 21 $item = array(); 22 23 while (!feof($fp)) { 24 $line = rtrim(fgets($fp, 8192)); 25 26 # The states suggested by the spec are inconsisent, but 27 # we'll do our best to fake it 28 29 if ($line == '--------') { 30 # End of an item, so we can process it 31 $results[]=import_blogger_item($item, $section, $status, $invite); 32 $item = array(); 33 $state = 'metadata'; 34 } 35 elseif ($line == '-----' and $state == 'metadata') { 36 $state = 'multiline'; 37 $multiline_type = ''; 38 } 39 elseif ($line == '-----' and $state == 'multiline') { 40 if (!empty($multiline_type)) 41 $item[$multiline_type][] = $multiline_data; 42 $state = 'multiline'; 43 $multiline_type = ''; 44 } 45 elseif ($state == 'metadata') { 46 if (preg_match('/^([A-Z ]+):\s*(.*)$/', $line, $match)) 47 $item[$match[1]] = $match[2]; 48 } 49 elseif ($state == 'multiline' and empty($multiline_type)) { 50 if (preg_match('/^([A-Z ]+):\s*$/', $line, $match)) { 51 $multiline_type = $match[1]; 52 $multiline_data = array(); 53 } 54 } 55 elseif ($state == 'multiline') { 56 # Here's where things get hinky. Rather than put the 57 # multiline metadata before the field name, it goes 58 # after, with no clear separation between metadata 59 # and data. And either the metadata or data might be 60 # missing. 61 62 if (empty($multiline_data['content']) and preg_match('/^([A-Z ]+):\s*(.*)$/', $line, $match)) { 63 # Metadata within the multiline field 64 $multiline_data[$match[1]] = $match[2]; 65 } 66 elseif (empty($multiline_data['content'])) { 67 $multiline_data['content'] = ($line . "\n"); 68 } 69 else { 70 $multiline_data['content'] .= ($line . "\n"); 71 } 72 } 73 } 74 75 # Catch the last item in the file, if it doesn't end with a separator 76 if (!empty($item)) 77 $results[]= import_blogger_item($item, $section, $status, $invite, $blogid); 78 79 fclose($fp); 80 return join('<br />', $results); 81 } 82 //Some \n chars on empty fields should be removed from body_extended and excerpt 83 //What about the new title_html field? 84 85 //Fields that are on MT format, but have not sense for blogger are commented 86 function import_blogger_item($item, $section, $status, $invite) { 87 88 # Untested import code follows 89 90 if (empty($item)) return; 91 92 include_once txpath.'/lib/classTextile.php'; 93 $textile = new Textile(); 94 95 $title = $textile->TextileThis($item['TITLE'], 1); 96 $url_title = stripSpace($title,1); 97 98 $body = $item['BODY'][0]['content']; 99 $body_html = $textile->textileThis($body, 1); 100 101 $date = strtotime($item['DATE']); 102 $date = date('Y-m-d H:i:s', $date); 103 104 if (isset($item['STATUS'])) 105 $post_status = ($item['STATUS'] == 'Draft' ? 1 : 4); 106 else 107 $post_status = $status; 108 109 //Bogger can use special chars on author names. Strip them and check for realname 110 $authorid = safe_field('user_id', 'txp_users', "RealName = '".doSlash($item['AUTHOR'])."'"); 111 if (!$authorid) 112 // $authorid = safe_field('user_id', 'txp_users', 'order by user_id asc limit 1'); 113 //Add new authors 114 safe_insert('txp_users', "name='".doSlash(stripSpace($textile->TextileThis($item['AUTHOR'],1)))."', RealName='".doSlash($item['AUTHOR'])."'"); 115 116 117 if (!safe_field("ID", "textpattern", "Title = '".doSlash($title)."' AND Posted = '".doSlash($date)."'")) { 118 safe_insert('textpattern', 119 "Posted='".doSlash($date)."',". 120 "LastMod='".doSlash($date)."',". 121 "AuthorID='".doSlash($item['AUTHOR'])."',". 122 "LastModID='".doSlash($item['AUTHOR'])."',". 123 "Title='".doSlash($title)."',". 124 "Body='".doSlash($body)."',". 125 "Body_html='".doSlash($body_html)."',". 126 "AnnotateInvite='".doSlash($invite)."',". 127 "Status='".doSlash($post_status)."',". 128 "Section='".doSlash($section)."',". 129 "uid='".md5(uniqid(rand(),true))."',". 130 "feed_time='".substr($date,0,10)."',". 131 "url_title='".doSlash($url_title)."'"); 132 133 134 $parentid = mysql_insert_id(); 135 136 if (!empty($item['COMMENT'])) { 137 foreach ($item['COMMENT'] as $comment) { 138 $comment_date = date('Y-m-d H:i:s', strtotime(@$comment['DATE'])); 139 $comment_content = $textile->TextileThis(nl2br(@$comment['content']),1); 140 //Check for Comments authors 141 if (preg_match('/<a href="(.*)">(.*)<\/a>/', @$comment['AUTHOR'], $match)) { 142 @$comment['URL'] = $match[1]; 143 @$comment['AUTHOR'] = $match[2]; 144 } 145 if (!safe_field("discussid","txp_discuss","posted = '".doSlash($comment_date)."' AND message = '".doSlash($comment_content)."'")) { 146 safe_insert('txp_discuss', 147 "parentid='".doSlash($parentid)."',". 148 //blogger places the link to user profile page as comment author 149 "name='".doSlash(strip_tags(@$comment['AUTHOR']))."',". 150 // "email='".doSlash(@$item['EMAIL'])."',". 151 "web='".doSlash(@$comment['URL'])."',". 152 // "ip='".doSlash(@$item['IP'])."',". 153 "posted='".doSlash($comment_date)."',". 154 "message='".doSlash($comment_content)."',". 155 "visible='1'"); 156 } 157 } 158 } 159 return $title; 160 } 161 return $title.' already imported'; 162 } 163 164 ?>
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Thu May 21 23:03:01 2009 | Cross-referenced by PHPXref 0.7 |