From 7998d676ab7b451b33755ab64effd23d6297ea92 Mon Sep 17 00:00:00 2001 From: garvinhicking Date: Mon, 8 Aug 2005 10:49:19 +0000 Subject: [PATCH] Improve moveableType importer, also import comments+trackbacks. --- docs/NEWS | 3 + include/admin/importers/movabletype.inc.php | 290 +++++++++++++++++--- 2 files changed, 250 insertions(+), 43 deletions(-) diff --git a/docs/NEWS b/docs/NEWS index 83a1708..6f06344 100644 --- a/docs/NEWS +++ b/docs/NEWS @@ -5,6 +5,9 @@ Version 0.9 () * Atom 1.0 feed support (garvinhicking) + * MoveableType importer now also recognizes comments and trackbacks. + Tested with MT 3.17. (garvinhicking) + * Make template directory allow to contain subdirectories with more templates. This allows you to symbolically link the "additional_themes" CVS directory within your templates path, just like you can do diff --git a/include/admin/importers/movabletype.inc.php b/include/admin/importers/movabletype.inc.php index 719c29b..e6df6ed 100644 --- a/include/admin/importers/movabletype.inc.php +++ b/include/admin/importers/movabletype.inc.php @@ -35,13 +35,42 @@ class Serendipity_Import_MovableType extends Serendipity_Import { 'name' => 'mt_force', 'default' => 'true'), + array('text' => CHARSET, + 'type' => 'list', + 'name' => 'charset', + 'value' => 'UTF-8', + 'default' => $this->getCharsets(true)), + array('text' => ACTIVATE_AUTODISCOVERY, 'type' => 'bool', 'name' => 'autodiscovery', + 'default' => 'false'), + + array('text' => 'Debugging', + 'type' => 'bool', + 'name' => 'debug', 'default' => 'false') ); } + function debug($string) { + static $debug = null; + static $c = 0; + + if ($debug === null) { + if ($this->data['debug'] == 'true') { + $debug = true; + } else { + $debug = false; + } + } + + if ($debug) { + $c++; + echo '#' . $c . ' [' . date('d.m.Y H:i.s') . '] ' . $string . "
\n"; + } + } + function getImportNotes(){ $notes = array(); if (!class_exists('serendipity_event_nl2br')){ @@ -66,14 +95,25 @@ class Serendipity_Import_MovableType extends Serendipity_Import { $entry = array(); $entry['categories'] = array(); $entryprops = array(); + + $this->debug("doEntryWork: " . print_r($mt_entry, true)); - foreach($mt_entry as $name => $data){ - switch($name){ - case 'AUTHOR': + foreach($mt_entry as $name => $data) { + $name = trim($name); + if (is_string($data)) { + $data = trim($data); + } + $this->debug($name . ': "' . print_r($data, true) . '"'); + switch($name) { + case 's9y_comments': + $entry['s9y_comments'] = $data; + break; + case 'AUTHOR': if ( !isset($authors[$data]) ) { $au_inf = serendipity_fetchAuthor($data); if ( !is_array($au_inf) ) { $tasks[] = sprintf(CREATE_AUTHOR, htmlspecialchars($data)); + $tasks[] = 'Input array is: ' . print_r($data, true) . '
Return is: ' . print_r($au_inf, true) . '
'; $au_inf = serendipity_fetchAuthor($serendipity['authorid']); } $authors[$data] = $au_inf[0]; @@ -92,19 +132,25 @@ class Serendipity_Import_MovableType extends Serendipity_Import { break; case 'DATE': $entry['timestamp'] = strtotime($data); - if ( $entry['timestamp'] == 0 ) { + if ($entry['timestamp'] == 0) { $entry['timestamp'] = time(); } break; + + case 's9y_body': case 'BODY': $entry['body'] = $data; break; + + case 's9y_extended': case 'EXTENDED BODY': $entry['extended'] = $data; break; + case 'CONVERT BREAKS': $entryprops['nl2br'] = ($data == '1') ? true : false; break; + case 'PRIMARY CATEGORY': case 'CATEGORY': $cat_found = false; @@ -131,10 +177,71 @@ class Serendipity_Import_MovableType extends Serendipity_Import { return $entry; } + function doCommentWork(&$mt_entry, &$tasks, $type = 'NORMAL'){ + $comment = array( + 'parent_id' => 0, + 'status' => 'approved', + 'subscribed' => 'false', + 'type' => $type, + 'body' => '' + ); + + $this->debug("MT_ENTRY: " . print_r($mt_entry, true)); + $parsed_entry = array(); + $unparsed_entry = explode("\n", $mt_entry[$type == 'NORMAL' ? 'COMMENT' : 'PING']); + foreach($unparsed_entry AS $line) { + if (preg_match('/^([A-Z\s]+):\s+(.*)$/', $line, $match)) { + $parsed_entry[$match[1]] = $match[2]; + } else { + $parsed_entry['s9y_body'] .= $line . "\n"; + } + } + + foreach($parsed_entry as $name => $data){ + $data = trim($data); + $name = trim($name); + + switch($name) { + case 'EMAIL': + $comment['email'] = $data; + break; + + case 'URL': + $comment['url'] = $data; + break; + + case 'IP': + $comment['ip'] = $data; + break; + + case 'AUTHOR': + case 'BLOG NAME': + $comment['author'] = $data; + break; + + case 'DATE': + $comment['timestamp'] = strtotime($data); + if ($comment['timestamp'] == 0) { + $comment['timestamp'] = time(); + } + break; + + case 'TITLE': + break; + + default: + $comment['body'] .= $data; + } + } + + $this->debug("S9Y_ENTRY: " . print_r($comment, true)); + return $comment; + } + function import() { global $serendipity; - $force = ($this->data['force'] == 'true'); + $force = ($this->data['mt_force'] == 'true'); if ($this->data['autodiscovery'] == 'false') { $serendipity['noautodiscovery'] = 1; @@ -150,59 +257,156 @@ class Serendipity_Import_MovableType extends Serendipity_Import { $entries = array(); - $fh = fopen($_FILES['serendipity']['tmp_name']['import']['mt_dat'], 'r'); + if (empty($_FILES['serendipity']['tmp_name']['import']['mt_dat'])) { + $fh = fopen('/tmp/mt.dat', 'r'); + } else { + $fh = fopen($_FILES['serendipity']['tmp_name']['import']['mt_dat'], 'r'); + } $n = 0; - $entry = array(); - $el = ""; - $skip = false; - while (!feof($fh)){ - $line = fgets($fh, 8192); - if ($skip && (!preg_match('/^--------/', $line))) + $entry = array(); + $el = ""; + $c_el = ""; + $skip = false; + $is_comment = false; + $is_trackback = false; + $nofetch = false; + while (!feof($fh)) { + if ($nofetch === false) { + $line = $this->decode(fgets($fh, 8192)); + } else { + // Keep line from previous run. + $nofetch = false; + } + + if ($is_comment || $is_trackback) { + $this->debug("COMMENT/TRACKBACK mode is active."); + if (preg_match('/^--------/', $line)) { + $this->debug("Next full section requested."); + $is_comment = $is_trackback = false; + } elseif (preg_match('/^-----/', $line)) { + $this->debug("Next partial section requested."); + if ($is_trackback) { + $this->debug("Parsing trackback."); + $entry['s9y_comments'][] = $this->doCommentWork($comment, $tasks, 'TRACKBACK'); + } elseif ($is_comment) { + $this->debug("Parsing comment."); + $entry['s9y_comments'][] = $this->doCommentWork($comment, $tasks, 'NORMAL'); + } + $el = $c_el = ""; + } + } + + if ($skip && (!preg_match('/^--------/', $line))) { + $this->debug("No next section match, and skip is activated. Skipping '$line'"); continue; - if (preg_match('/^--------/', $line)){ - // We found the end marker of the current entry. Add to - // entries-Array - $entries[] = $this->doEntryWork($entry, $tasks); - $entry = array(); - $skip = false; - }elseif (preg_match('/^-----/', $line)){ - if (empty($el)){ - $line = fgets($fh, 8192); - if (preg_match('/^([A-Z]+):/', $line, $matches)){ - if ($matches[1] == 'COMMENT'){ - // todo: really handle comments. - $skip = true; - }else{ - $el = $matches[1]; + } + + if (preg_match('/^--------/', $line)) { + // We found the end marker of the current entry. Add to entries-Array + $this->debug("End marker found. Parsing full entry."); + $entries[] = $this->doEntryWork($entry, $tasks); + $entry = array(); + $el = ""; + $c_el = ""; + $skip = false; + $is_comment = false; + $is_trackback = false; + } elseif (preg_match('/^-----/', $line)) { + $this->debug("New section match. Current EL: $el"); + if (empty($el)) { + $line = $this->decode(fgets($fh, 8192)); + $this->debug("Inspecting next line: $line"); + $tline = trim($line); + while (($is_comment || $is_trackback) && empty($tline)) { + $line = $this->decode(fgets($fh, 8192)); + $tline = trim($line); + $this->debug("Continuing inspecting next line: $line"); + } + if (preg_match('/^([A-Z\s]+):/', $line, $matches)) { + $this->debug("Match result: $matches[1]"); + if ($matches[1] == 'COMMENT') { + $this->debug("Marking COMMENT."); + $is_comment = true; + $is_trackback = false; + $comment = array(); + $skip = false; + } elseif ($matches[1] == 'PING') { + $this->debug("Marking TRACKBACK"); + $is_comment = false; + $is_trackback = true; + $comment = array(); + $skip = false; } + + $this->debug("Setting EL to {$matches[1]}"); + $el = $matches[1]; + $c_el = ""; + } else { + $this->debug("Could not parse next line. Keeping it for next cycle."); + $nofetch = true; } - }else{ - $el = ""; + } else { + $this->debug("Resetting EL to an empty string"); + $el = $c_el = ""; } - }else{ - if (empty($el) ){ - if (preg_match('/^([A-Z ]+): (.*)$/', $line, $matches)){ - $entry[$matches[1]] = $matches[2]; - } - }else{ - $entry[$el] .= $line; - } - } - + } elseif (empty($el)) { + $this->debug("EL is empty. Line is '$line'"); + $content = ""; + if (preg_match('/^([A-Z\s]+):\s+(.*)$/s', $line, $matches)) { + $this->debug("Section match {$matches[1]} found, input: {$matches[2]}"); + $c_el = $matches[1]; + $content = $matches[2]; + } elseif (!empty($c_el)) { + $this->debug("Still in subsection of previous run: $c_el."); + $content = trim($line); + } + + if (!empty($content)) { + if ($is_comment || $is_trackback) { + $this->debug("Appending to comments: $line"); + $comment[$c_el] = $content; + } else { + $this->debug("Appending to entry: $line"); + $entry[$c_el] = $content; + } + } + } elseif ($is_comment || $is_trackback) { + $this->debug("Appending Line in current Element $el to comments: $line"); + $comment[$el] .= $line; + } else { + $this->debug("Appending Line in current Element $el to entries: $line"); + $entry[$el] .= $line; + } } fclose($fh); if ( !sizeof($tasks) || $force == true ) { serendipity_db_begin_transaction(); - foreach ( $entries as $entry ) { + foreach ($entries as $entry) { + if (empty($entry['authorid'])) { + $entry['authorid'] = $serendipity['authorid']; + $entry['author'] = $serendipity['realname']; + } + $comments = $entry['s9y_comments']; $entryprops = $entry['props']; unset($entry['props']); + unset($entry['s9y_comments']); + if ( !is_int($r = serendipity_updertEntry($entry)) ) { echo '
' . $r . '
'; - }else{ + } else { $entry['id'] = $r; + foreach((array)$comments AS $comment) { + $comment['entry_id'] = $r; + if ($rc = serendipity_db_insert('comments', $comment)) { + $cid = serendipity_db_insert_id('comments', 'id'); + serendipity_approveComment($cid, $entry['id'], true); + } else { + echo '
' . $rc . '
'; + } + } // Let the plugins do some additional stuff. Here it's used with // event_entryproperties in mind to setup the nl2br-stuff serendipity_plugin_api::hook_event('backend_import_entry', $entry, $entryprops); @@ -210,10 +414,10 @@ class Serendipity_Import_MovableType extends Serendipity_Import { } serendipity_db_end_transaction(true); return true; - }else { + } else { return ''; } } } return 'Serendipity_Import_MovableType'; -?> +?> \ No newline at end of file -- 2.39.5