[ Index ] |
|
Code source de Serendipity 1.2 |
1 <?php # $Id: generic.inc.php 717 2005-11-21 09:56:25Z garvinhicking $ 2 # Copyright (c) 2003-2005, Jannis Hermanns (on behalf the Serendipity Developer Team) 3 # All rights reserved. See LICENSE file for licensing details 4 5 require_once S9Y_PEAR_PATH . 'Onyx/RSS.php'; 6 7 class Serendipity_Import_LiveJournalXML extends Serendipity_Import { 8 var $info = array('software' => 'LiveJournal XML'); 9 var $data = array(); 10 var $inputFields = array(); 11 var $force_recode = false; 12 13 function Serendipity_Import_LiveJournalXML($data) { 14 global $serendipity; 15 $this->data = $data; 16 $this->inputFields = array(array('text' => 'LiveJournal XML', 17 'type' => 'input', 18 'name' => 'url', 19 'default' => $serendipity['serendipityPath'] . $serendipity['uploadPath'] . 'EVbackup.xml'), 20 21 array('text' => RSS_IMPORT_CATEGORY, 22 'type' => 'list', 23 'name' => 'category', 24 'value' => 0, 25 'default' => $this->_getCategoryList()), 26 27 array('text' => STATUS, 28 'type' => 'list', 29 'name' => 'type', 30 'value' => 'publish', 31 'default' => array('publish' => PUBLISH, 'draft' => DRAFT)), 32 33 ); 34 } 35 36 function _getCategoryList() { 37 $res = serendipity_fetchCategories('all'); 38 $ret = array(0 => NO_CATEGORY); 39 if (is_array($res)) { 40 foreach ($res as $v) { 41 $ret[$v['categoryid']] = $v['category_name']; 42 } 43 } 44 return $ret; 45 } 46 47 function GetChildren(&$vals, &$i) { 48 $children = array(); 49 $cnt = sizeof($vals); 50 while (++$i < $cnt) { 51 // compare type 52 switch ($vals[$i]['type']) { 53 case 'cdata': 54 $children[] = $vals[$i]['value']; 55 break; 56 57 case 'complete': 58 $children[] = array( 59 'tag' => $vals[$i]['tag'], 60 'attributes' => $vals[$i]['attributes'], 61 'value' => $vals[$i]['value'] 62 ); 63 break; 64 65 case 'open': 66 $children[] = array( 67 'tag' => $vals[$i]['tag'], 68 'attributes' => $vals[$i]['attributes'], 69 'value' => $vals[$i]['value'], 70 'children' => $this->GetChildren($vals, $i) 71 ); 72 break; 73 74 case 'close': 75 return $children; 76 } 77 } 78 } 79 80 function &parseXML(&$xml) { 81 // XML functions 82 $xml_string = '<?xml version="1.0" encoding="UTF-8" ?>'; 83 if (preg_match('@(<\?xml.+\?>)@imsU', $xml, $xml_head)) { 84 $xml_string = $xml_head[1]; 85 } 86 87 $encoding = 'UTF-8'; 88 if (preg_match('@encoding="([^"]+)"@', $xml_string, $xml_encoding)) { 89 $encoding = $xml_encoding[1]; 90 } 91 92 preg_match_all('@(<entry>.*</entry>)@imsU', $xml, $xml_matches); 93 if (!is_array($xml_matches)) { 94 return false; 95 } 96 97 $i = 0; 98 $tree = array(); 99 $tree[$i] = array( 100 'tag' => 'entries', 101 'attributes' => '', 102 'value' => '', 103 'children' => array() 104 ); 105 106 foreach($xml_matches[0] as $xml_index => $xml_package) { 107 $i = 0; 108 109 switch(strtolower($encoding)) { 110 case 'iso-8859-1': 111 case 'utf-8': 112 $p = xml_parser_create($encoding); 113 break; 114 115 default: 116 $p = xml_parser_create(''); 117 } 118 119 xml_parser_set_option($p, XML_OPTION_CASE_FOLDING, 0); 120 @xml_parser_set_option($p, XML_OPTION_TARGET_ENCODING, LANG_CHARSET); 121 $xml_package = $xml_string . "\n" . $xml_package; 122 xml_parse_into_struct($p, $xml_package, $vals); 123 xml_parser_free($p); 124 $tree[0]['children'][] = array( 125 'tag' => $vals[$i]['tag'], 126 'attributes' => $vals[$i]['attributes'], 127 'value' => $vals[$i]['value'], 128 'children' => $this->GetChildren($vals, $i) 129 ); 130 unset($vals); 131 } 132 133 return $tree; 134 } 135 136 function validateData() { 137 return sizeof($this->data); 138 } 139 140 function getInputFields() { 141 return $this->inputFields; 142 } 143 144 function getTimestamp($string) { 145 if (preg_match('@(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})@', $string, $match)) { 146 return mktime($match[4], $match[5], $match[6], $match[2], $match[3], $match[1]); 147 } else { 148 return time(); 149 } 150 } 151 152 function import() { 153 global $serendipity; 154 155 if (!file_exists($this->data['url'])) { 156 printf(FILE_NOT_FOUND, htmlspecialchars($this->data['url'])); 157 return false; 158 } 159 160 $file = file_get_contents($this->data['url']); 161 $tree =& $this->parseXML($file); 162 $serendipity['noautodiscovery'] = 1; 163 164 foreach($tree[0]['children'] AS $idx => $entry) { 165 if (!is_array($entry)) continue; 166 if ($entry['tag'] != 'entry') { 167 continue; 168 } 169 170 $new_entry = array( 171 'allow_comments' => true, 172 'extended' => '', 173 'categories' => array(), 174 'isdraft' => ($this->data['type'] == 'draft' ? 'true' : 'false'), 175 'categories' => array($this->data['category'] => $this->data['category']) 176 ); 177 178 if (!is_array($entry['children'])) continue; 179 180 foreach($entry['children'] AS $idx2 => $entrydata) { 181 if (!is_array($entrydata)) { 182 continue; 183 } 184 185 switch($entrydata['tag']) { 186 case 'eventtime': 187 $new_entry['timestamp'] = $this->getTimestamp($entrydata['value']); 188 break; 189 190 case 'subject': 191 $new_entry['title'] = $entrydata['value']; 192 break; 193 194 case 'event': 195 $new_entry['body'] = $entrydata['value']; 196 break; 197 } 198 } 199 $id = serendipity_updertEntry($new_entry); 200 echo 'Inserted entry #' . $id . ', "' . htmlspecialchars($new_entry['title']) . '"<br />' . "\n"; 201 202 if (function_exists('ob_flush')) { 203 @ob_flush(); 204 } 205 if (function_exists('flush')) { 206 @flush(); 207 } 208 } 209 210 return true; 211 } 212 } 213 214 return 'Serendipity_Import_LiveJournalXML'; 215 216 /* vim: set sts=4 ts=4 expandtab : */
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Sat Nov 24 09:00:37 2007 | par Balluche grâce à PHPXref 0.7 |
![]() |