]> git.mjollnir.org Git - s9y.git/commitdiff
Fix import UTF-8 bug, thanks to jan
authorgarvinhicking <garvinhicking>
Fri, 27 Oct 2006 09:18:19 +0000 (09:18 +0000)
committergarvinhicking <garvinhicking>
Fri, 27 Oct 2006 09:18:19 +0000 (09:18 +0000)
docs/NEWS
include/admin/entries.inc.php
include/admin/import.inc.php
include/admin/importers/generic.inc.php
include/admin/importers/livejournal.inc.php [new file with mode: 0644]
include/admin/importers/nucleus.inc.php
include/admin/importers/voodoopad.inc.php [changed mode: 0644->0755]

index e64f1bf2d0c6e60b924319fc3d20fc744551d935..0ff5e0d092f8d86ef0d672374a457aa835ae0aac 100644 (file)
--- a/docs/NEWS
+++ b/docs/NEWS
@@ -3,6 +3,10 @@
 Version 1.1 ()
 ------------------------------------------------------------------------
 
+    * Fixed bug that prevented native imports from other blog system
+      to recode ISO-charsets into UTF-8. Major thanks to Jan of
+      blog.salid.de. (garvinhicking)
+
     * Added functionality to reply to comments in the admin interface
       (garvinhicking)
 
index e7d06105f0caa4aed52a81db764dfa827fccc567..e7098cbc829f909d36d36a6937d9b3af7a4f7657 100644 (file)
@@ -309,7 +309,7 @@ function serendipity_drawList() {
             <tr>
                 <td align="right">
                     <input type="button" name="toggle" value="<?php echo INVERT_SELECTIONS ?>" onclick="invertSelection()" class="serendipityPrettyButton" />
-                    <input type="submit" name="toggle" value="<?php echo DELETE_SELECTED_COMMENTS ?>" class="serendipityPrettyButton" />
+                    <input type="submit" name="toggle" value="<?php echo DELETE_SELECTED_ENTRIES ?>" class="serendipityPrettyButton" />
                 </td>
             </tr>
         </table>
index 1c6976868bdc75ffb8d72bac3b913098ddc49748..bbe6240431bae80157b711115c53344d991a631b 100644 (file)
@@ -17,8 +17,8 @@ if (function_exists('set_time_limit')) {
 
 /* Class construct. Each importer plugin must extend this class. */
 class Serendipity_Import {
-    var $trans_table = '';
-
+    var $trans_table  = '';
+    var $force_recode = true;
 /**
  * Return textual notes of an importer plugin
  *
@@ -68,8 +68,8 @@ class Serendipity_Import {
  * @return string   converted string
  */
     function &decode($string) {
-        // xml_parser_* functions to recoding from ISO-8859-1/UTF-8
-        if (LANG_CHARSET == 'ISO-8859-1' || LANG_CHARSET == 'UTF-8') {
+        // xml_parser_* functions do recoding from ISO-8859-1/UTF-8
+        if (!$this->force_recode && (LANG_CHARSET == 'ISO-8859-1' || LANG_CHARSET == 'UTF-8')) {
             return $string;
         }
 
@@ -84,6 +84,8 @@ class Serendipity_Import {
                     $out = iconv('ISO-8859-1', LANG_CHARSET, $string);
                 } elseif (function_exists('recode')) {
                     $out = recode('iso-8859-1..' . LANG_CHARSET, $string);
+                } elseif (LANG_CHARSET == 'UTF-8') {
+                    return utf8_encode($string);
                 } else {
                     return $string;
                 }
index 66b65479aed2482495059c28f55e692f7659ae11..fe38f2145a6f132792dd6607c6f1d8b1937993ca 100644 (file)
@@ -8,6 +8,7 @@ class Serendipity_Import_Generic extends Serendipity_Import {
     var $info        = array('software' => IMPORT_GENERIC_RSS);
     var $data        = array();
     var $inputFields = array();
+    var $force_recode = false;
 
     function Serendipity_Import_Generic($data) {
         $this->data = $data;
diff --git a/include/admin/importers/livejournal.inc.php b/include/admin/importers/livejournal.inc.php
new file mode 100644 (file)
index 0000000..daa9cce
--- /dev/null
@@ -0,0 +1,216 @@
+<?php # $Id: generic.inc.php 717 2005-11-21 09:56:25Z garvinhicking $
+# Copyright (c) 2003-2005, Jannis Hermanns (on behalf the Serendipity Developer Team)
+# All rights reserved.  See LICENSE file for licensing details
+
+require_once S9Y_PEAR_PATH . 'Onyx/RSS.php';
+
+class Serendipity_Import_LiveJournalXML extends Serendipity_Import {
+    var $info        = array('software' => 'LiveJournal XML');
+    var $data        = array();
+    var $inputFields = array();
+    var $force_recode = false;
+
+    function Serendipity_Import_LiveJournalXML($data) {
+        global $serendipity;
+        $this->data = $data;
+        $this->inputFields = array(array('text'    => 'LiveJournal XML',
+                                         'type'    => 'input',
+                                         'name'    => 'url',
+                                         'default'   => $serendipity['serendipityPath'] . $serendipity['uploadPath'] . 'EVbackup.xml'),
+
+                                   array('text'    => RSS_IMPORT_CATEGORY,
+                                         'type'    => 'list',
+                                         'name'    => 'category',
+                                         'value'   => 0,
+                                         'default' => $this->_getCategoryList()),
+
+                                   array('text'    => STATUS,
+                                         'type'    => 'list',
+                                         'name'    => 'type',
+                                         'value'   => 'publish',
+                                         'default' => array('publish' => PUBLISH, 'draft' => DRAFT)),
+
+        );
+    }
+
+    function _getCategoryList() {
+        $res = serendipity_fetchCategories('all');
+        $ret = array(0 => NO_CATEGORY);
+        if (is_array($res)) {
+            foreach ($res as $v) {
+                $ret[$v['categoryid']] = $v['category_name'];
+            }
+        }
+        return $ret;
+    }
+
+    function GetChildren(&$vals, &$i) {
+        $children = array();
+        $cnt = sizeof($vals);
+        while (++$i < $cnt) {
+            // compare type
+            switch ($vals[$i]['type']) {
+                case 'cdata':
+                    $children[] = $vals[$i]['value'];
+                    break;
+
+                case 'complete':
+                    $children[] = array(
+                        'tag'        => $vals[$i]['tag'],
+                        'attributes' => $vals[$i]['attributes'],
+                        'value'      => $vals[$i]['value']
+                    );
+                    break;
+
+                case 'open':
+                    $children[] = array(
+                        'tag'        => $vals[$i]['tag'],
+                        'attributes' => $vals[$i]['attributes'],
+                        'value'      => $vals[$i]['value'],
+                        'children'   => $this->GetChildren($vals, $i)
+                    );
+                    break;
+
+                case 'close':
+                    return $children;
+            }
+        }
+    }
+
+    function &parseXML(&$xml) {
+        // XML functions
+        $xml_string = '<?xml version="1.0" encoding="UTF-8" ?>';
+        if (preg_match('@(<\?xml.+\?>)@imsU', $xml, $xml_head)) {
+            $xml_string = $xml_head[1];
+        }
+
+        $encoding = 'UTF-8';
+        if (preg_match('@encoding="([^"]+)"@', $xml_string, $xml_encoding)) {
+            $encoding = $xml_encoding[1];
+        }
+
+        preg_match_all('@(<entry>.*</entry>)@imsU', $xml, $xml_matches);
+        if (!is_array($xml_matches)) {
+            return false;
+        }
+
+        $i = 0;
+        $tree = array();
+        $tree[$i] = array(
+            'tag'        => 'entries',
+            'attributes' => '',
+            'value'      => '',
+            'children'   => array()
+        );
+
+        foreach($xml_matches[0] as $xml_index => $xml_package) {
+            $i = 0;
+
+            switch(strtolower($encoding)) {
+                case 'iso-8859-1':
+                case 'utf-8':
+                    $p = xml_parser_create($encoding);
+                    break;
+
+                default:
+                    $p = xml_parser_create('');
+            }
+
+            xml_parser_set_option($p, XML_OPTION_CASE_FOLDING, 0);
+            @xml_parser_set_option($p, XML_OPTION_TARGET_ENCODING, LANG_CHARSET);
+            $xml_package = $xml_string . "\n" . $xml_package;
+            xml_parse_into_struct($p, $xml_package, $vals);
+            xml_parser_free($p);
+            $tree[0]['children'][] = array(
+                'tag'        => $vals[$i]['tag'],
+                'attributes' => $vals[$i]['attributes'],
+                'value'      => $vals[$i]['value'],
+                'children'   => $this->GetChildren($vals, $i)
+            );
+            unset($vals);
+        }
+        
+        return $tree;
+    }
+
+    function validateData() {
+        return sizeof($this->data);
+    }
+
+    function getInputFields() {
+        return $this->inputFields;
+    }
+
+    function getTimestamp($string) {
+        if (preg_match('@(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})@', $string, $match)) {
+            return mktime($match[4], $match[5], $match[6], $match[2], $match[3], $match[1]);
+        } else {
+            return time();
+        }
+    }
+
+    function import() {
+        global $serendipity;
+
+        if (!file_exists($this->data['url'])) {
+            printf(FILE_NOT_FOUND, htmlspecialchars($this->data['url']));
+            return false;
+        }
+        
+        $file = file_get_contents($this->data['url']);
+        $tree =& $this->parseXML($file);
+        $serendipity['noautodiscovery'] = 1;
+        
+        foreach($tree[0]['children'] AS $idx => $entry) {
+            if (!is_array($entry)) continue;
+            if ($entry['tag'] != 'entry') {
+                continue;
+            }
+            
+            $new_entry = array(
+                'allow_comments' => true,
+                'extended'       => '',
+                'categories'     => array(),
+                'isdraft'        => ($this->data['type'] == 'draft' ? 'true' : 'false'),
+                'categories'     => array($this->data['category'] => $this->data['category'])
+            );
+            
+            if (!is_array($entry['children'])) continue;
+
+            foreach($entry['children'] AS $idx2 => $entrydata) {
+                if (!is_array($entrydata)) {
+                    continue;
+                }
+
+                switch($entrydata['tag']) {
+                    case 'eventtime':
+                        $new_entry['timestamp'] = $this->getTimestamp($entrydata['value']);
+                        break;
+                    
+                    case 'subject':
+                        $new_entry['title']     = $entrydata['value'];
+                        break;
+                    
+                    case 'event':
+                        $new_entry['body']      = $entrydata['value'];
+                        break;
+                }
+            }
+            $id = serendipity_updertEntry($new_entry);
+            echo 'Inserted entry #' . $id . ', "' . htmlspecialchars($new_entry['title']) . '"<br />' . "\n";
+            
+            if (function_exists('ob_flush')) {
+                @ob_flush();
+            }
+            if (function_exists('flush')) {
+                @flush();
+            }
+        }
+
+        return true;
+    }
+}
+
+return 'Serendipity_Import_LiveJournalXML';
+
+/* vim: set sts=4 ts=4 expandtab : */
index 9aa6ebe9178f0eb4c3af7a5f0a764f03bd74aa57..331d0763b578b576bbd2c322b416ef6fc5320839 100644 (file)
@@ -154,6 +154,11 @@ class Serendipity_Import_Nucleus extends Serendipity_Import {
         for ($x=0, $max_x = mysql_num_rows($res) ; $x < $max_x ; $x++ ) {
             $entries[$x] = mysql_fetch_assoc($res);
 
+
+            echo "BODY: " . $entries[$x]['ibody'] . "<br />\n";
+            echo "DECODED BODY: " . $this->strtr($entries[$x]['ibody']) . "<br />\n";
+            die('done');
+
             $entry = array('title'          => $this->decode($entries[$x]['ititle']),
                            'isdraft'        => ($entries[$x]['idraft'] != '1') ? 'false' : 'true',
                            'allow_comments' => ($entries[$x]['iclosed'] == '1' ) ? 'false' : 'true',
old mode 100644 (file)
new mode 100755 (executable)
index f52c970..c27e30b
@@ -38,6 +38,7 @@ class Serendipity_Import_VoodooPad extends Serendipity_Import {
     var $info        = array('software' => 'VoodooPad');
     var $data        = array();
     var $inputFields = array();
+    var $force_recode = false;
     
     function Serendipity_Import_VoodooPad($data) {
         $this->data = $data;