Update of /cvsroot/php-blog/serendipity/include/admin/importers
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv27249/include/admin/importers
Modified Files:
movabletype.inc.php
Log Message:
New MT-Importer.
- Actually works
- Tries hard to inform the user of missing plugins concerning conversion of linebreaks
- Uses transactions to import all in one transaction. Much faster in PG and SQLite
Index: movabletype.inc.php
===================================================================
RCS file: /cvsroot/php-blog/serendipity/include/admin/importers/movabletype.inc.php,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -d -r1.3 -r1.4
--- movabletype.inc.php 27 Nov 2004 22:14:16 -0000 1.3
+++ movabletype.inc.php 9 Jan 2005 10:34:04 -0000 1.4
@@ -4,6 +4,19 @@
* MovableType Importer, by Evan Nemerson *
*****************************************************************/
+switch ($serendipity['lang']) {
+ case 'de':
+ @define('IMPORTER_MT_WARN_PLUGIN', 'Bitte installieren Sie das Plugin "%s"');
+ @define('IMPORTER_MT_NOTE', 'Falls Sie weiter machen, ohne die Plugins zu installieren, werden möglicherweise Zeilenumbrüche falsch importiert (verdoppelt oder entfernt)');
+ break;
+
+ case 'en':
+ default:
+ @define('IMPORTER_MT_WARN_PLUGIN', 'Please install the plugin "%s"');
+ @define('IMPORTER_MT_NOTE', 'If you continue without installing those plugins, line breaks may be incorrectly imported (doubled or removed)');
+ break;
+}
+
class Serendipity_Import_MovableType extends Serendipity_Import {
var $info = array('software' => 'MovableType');
var $data = array();
@@ -27,6 +40,18 @@
);
}
+ function getImportNotes(){
+ $notes = array();
+ if (!class_exists('serendipity_event_nl2br')){
+ $notes[] = sprintf(IMPORTER_MT_WARN_PLUGIN, 'serendipity_event_nl2br');
+ }
+ if (!class_exists('serendipity_event_entryproperties')){
+ $notes[] = sprintf(IMPORTER_MT_WARN_PLUGIN, 'serendipity_event_entryproperties');
+ }
+ if (count($notes) > 0){
+ return '<ul><li>'.implode('</li><li>', $notes).'</li></ul>'.IMPORTER_MT_NOTE;
+ }
+ }
function validateData() {
return sizeof($this->data);
}
@@ -35,6 +60,75 @@
return $this->inputFields;
}
+ function doEntryWork(&$mt_entry, &$tasks){
+ $entry = array();
+ $entry['categories'] = array();
+ $entryprops = array();
+
+ foreach($mt_entry as $name => $data){
+ switch($name){
+ case 'AUTHOR':
+ if ( !isset($authors[$data]) ) {
+ $au_inf = serendipity_fetchAuthor($data);
+ if ( !is_array($au_inf) ) {
+ $tasks[] = sprintf(CREATE_AUTHOR, htmlspecialchars($data));
+ $au_inf = serendipity_fetchAuthor($serendipity['authorid']);
+ }
+ $authors[$data] = $au_inf[0];
+ }
+ $entry['authorid'] = $authors[$data]['authorid'];
+ $entry['author'] = $authors[$data]['username'];
+ break;
+ case 'TITLE':
+ $entry['title'] = $data;
+ break;
+ case 'STATUS':
+ $entry['isdraft'] = ($data == 'Publish') ? 'false' : 'true';
+ break;
+ case 'ALLOW COMMENTS':
+ $entry['allow_comments'] = ($data == '1') ? 'true' : 'false';
+ break;
+ case 'DATE':
+ $entry['timestamp'] = strtotime($data);
+ if ( $entry['timestamp'] == 0 ) {
+ $entry['timestamp'] = time();
+ }
+ break;
+ case 'BODY':
+ $entry['body'] = $data;
+ break;
+ 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;
+ if ( is_array($this->categories) ) {
+ for ( $y=0 ; $y<sizeof($this->categories) ; $y++ ) {
+ if ( $this->categories[$y]['category_name'] == $data ) {
+ $cat_found = true;
+ break;
+ }
+ }
+ if ( $cat_found){
+ if (!in_array($this->categories[$y]['categoryid'], $entry['categories']) ) {
+ //$entries[$n]['categories'][] = $categories[$y]['categoryid'];
+ $entry['categories'][] = $this->categories[$y]['categoryid'];
+ }
+ }else {
+ $tasks[] = sprintf(CREATE_CATEGORY, htmlspecialchars($data));
+ }
+ }
+ break;
+ }
+ }
+ $entry['props'] = $entryprops;
+ return $entry;
+ }
+
function import() {
global $serendipity;
@@ -44,87 +138,78 @@
$serendipity['noautodiscovery'] = 1;
}
- $contents = file_get_contents($serendipity['FILES']['tmp_name']['mt_dat']);
+ // Rewritten to parse the file line by line. Can save quite some
+ // memory on large blogs
+ //$contents = file_get_contents($_FILES['serendipity']['tmp_name']['import']['mt_dat']);
$authors = array();
- $categories = serendipity_fetchCategories();
+ $this->categories = serendipity_fetchCategories();
$tasks = array();
$entries = array();
+
+ $fh = fopen($_FILES['serendipity']['tmp_name']['import']['mt_dat'], 'r');
+
$n = 0;
- foreach ( preg_split('/\r?\n--------\r?\n/', $contents) as $entry ) {
- if ( preg_match_all('/([A-Z ]+):( |\r?\n)(.*)\r?\n(-----)?/', $entry, $res) ) {
- $entries[$n]['categories'] = array();
- for ( $x=0 ; $x<sizeof($res[1]) ; $x++ ) {
- switch ( $res[1][$x] ) {
- case 'AUTHOR':
- if ( !isset($authors[$res[3][$x]]) ) {
- $au_inf = serendipity_fetchAuthor($res[3][$x]);
- if ( !is_array($au_inf) ) {
- $tasks[] = sprintf(CREATE_AUTHOR, htmlspecialchars($res[3][$x]));
- $au_inf = serendipity_fetchAuthor($serendipity['authorid']);
- }
- $authors[$res[3][$x]] = $au_inf[0];
- }
- $entries[$n]['authorid'] = $authors[$res[3][$x]]['authorid'];
- $entries[$n]['author'] = $authors[$res[3][$x]]['username'];
- break;
- case 'TITLE':
- $entries[$n]['title'] = $res[3][$x];
- break;
- case 'STATUS':
- $entries[$n]['isdraft'] = ($res[3][$x] == 'Publish') ? 'false' : 'true';
- break;
- case 'ALLOW COMMENTS':
- $entries[$n]['allow_comments'] = ($res[3][$x] == '1') ? 'true' : 'false';
- break;
- case 'DATE':
- $entries[$n]['timestamp'] = strtotime($res[3][$x]);
- if ( $entries[$n]['timestamp'] == 0 ) {
- $entries[$n]['timestamp'] = time();
- }
- break;
- case 'BODY':
- $entries[$n]['body'] = $res[3][$x];
- break;
- case 'EXTENDED BODY':
- $entries[$n]['extended'] = $res[3][$x];
- break;
- case 'PRIMARY CATEGORY':
- case 'CATEGORY':
- $cat_found = false;
- if ( is_array($categories) ) {
- for ( $y=0 ; $y<sizeof($categories) ; $y++ ) {
- if ( $categories[$y]['category_name'] == $res[3][$x] ) {
- $cat_found = true;
- break;
- }
- }
- }
- if ( $cat_found && !in_array($categories[$y]['categoryid'], $entries[$n]['categories']) ) {
- $entries[$n]['categories'][] = $categories[$y]['categoryid'];
- $entries[$n]['categories'][] = $categories[$y]['categoryid'];
- }
- else {
- $tasks[] = sprintf(CREATE_CATEGORY, htmlspecialchars($res[3][$x]));
- }
- break;
+
+ $entry = array();
+ $el = "";
+ $skip = false;
+ while (!feof($fh)){
+ $line = fgets($fh, 8192);
+ if ($skip && (!preg_match('/^--------/', $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];
+ }
}
+ }else{
+ $el = "";
}
- }
- $n++;
+ }else{
+ if (empty($el) ){
+ if (preg_match('/^([A-Z ]+): (.*)$/', $line, $matches)){
+ $entry[$matches[1]] = $matches[2];
+ }
+ }else{
+ $entry[$el] .= $line;
+ }
+ }
+
}
+ fclose($fh);
if ( !sizeof($tasks) || $force == true ) {
+ serendipity_db_begin_transaction();
foreach ( $entries as $entry ) {
+ $entryprops = $entry['props'];
+ unset($entry['props']);
if ( !is_int($r = serendipity_updertEntry($entry)) ) {
echo '<div class="serendipityAdminMsgError">' . $r . '</div>';
+ }else{
+ $entry['id'] = $r;
+ // 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);
}
}
+ serendipity_db_end_transaction(true);
return true;
- }
- else {
- return array_unique($tasks);
+ }else {
+ return '<ul><li>'.implode('</li><li>', array_unique($tasks)).'</li></ul>';
}
}
}
|