convert(); class Delicious_Converter { private $fields = array('Title','URL','Date added','Private','Tags','Notes'), $fh = NULL, $driver = NULL; public function __construct($file, $format) { $this->fh = fopen($file, "r"); $this->set_driver($format); } public function set_driver($format) { $this->driver = $this->get_driver($format, $this->fields); } public function convert() { $this->driver->emit_header(); $bookmark = NULL; while ($row = rtrim(fgets($this->fh))) { $id = substr($row, 0, 3); switch(true) { case $id == 'driver->emit_row($bookmark); } $bookmark = $this->parse_row($row); break; case $id == 'parse_notes($row); break; } } if ($bookmark) { $this->driver->emit_row($bookmark); } $this->driver->emit_footer(); } private function parse_row($data) { $bookmark = array_fill_keys($this->fields, ''); if (preg_match("#(.*)#", $data, $match)) { $bookmark['Title'] = html_entity_decode(trim($match[1]), ENT_QUOTES); } if (preg_match("# return substr(trim($data), 4); } private function get_driver($name, $fields) { $klass = "Bookmark_Exporter_Driver_{$name}"; return new $klass($fields); } } /* Exporters Drivers */ abstract class Bookmark_Exporter_Driver { public $fields; public function __construct($fields) { $this->fields = $fields; } abstract public function emit_header(); abstract public function emit_row($row); abstract public function emit_footer(); } /* CSV Exporter driver */ class Bookmark_Exporter_Driver_CSV extends Bookmark_Exporter_Driver { public function emit_header() { print join(";", $this->fields) . ";\n"; } public function emit_row($row) { $usemb = function_exists('mb_convert_encoding'); foreach($row as $field) { if ($usemb) { print $this->encode_csv(mb_convert_encoding($field, 'ISO-8859-15', 'UTF-8')) . ";"; } else { print $this->encode_csv(utf8_decode($field)) . ";"; } } print "\n"; } public function emit_footer() { } private function encode_csv($val) { $enc = FALSE; $val = trim($val); if (FALSE !== strpos($val,',')) { $enc = TRUE; } if (FALSE !== strpos($val,';')) { $enc = TRUE; } if (is_numeric($val)) { $val = str_replace('.', ',', $val); $enc = TRUE; } if (FALSE !== strpos($val,'"')) { $val = str_replace('"', '""', $val); $enc = TRUE; } if (FALSE !== strpos($val,"\n")) { $enc = TRUE; } return $enc ? "\"{$val}\"" : $val; } } /* PHP Exporter driver (just a print_r) */ class Bookmark_Exporter_Driver_PHP extends Bookmark_Exporter_Driver { public function emit_header() { print "Starting dump\n"; } public function emit_row($row) { print_r($row); } public function emit_footer() { print "End dump\n"; } } ?>