123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- <?php
- /**
- * dolphin. Collection of useful PHP skeletons.
- * Copyright (C) 2016 Johannes 'Banana' Keßler
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the COMMON DEVELOPMENT AND DISTRIBUTION LICENSE
- *
- * You should have received a copy of the
- * COMMON DEVELOPMENT AND DISTRIBUTION LICENSE (CDDL) Version 1.0
- * along with this program. If not, see http://www.sun.com/cddl/cddl.html
- */
- /**
- * this is a very simple serendipity to md export.
- * user this as a base to improve your own export.
- */
- mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); # throw exeptions
- $DB = new mysqli('127.0.0.1', 'root', 'pasword', 'blog');
- if ($DB->connect_errno) exit('Can not connect to MySQL Server');
- #$DB->set_charset("utf8mb4");
- #$DB->query("SET collation_connection = 'utf8mb4_bin'");
- $workingDir = getcwd();
- date_default_timezone_set('Europe/Berlin');
- # get the tags as array per entrie
- $tags = array();
- $queryStr = "SELECT * FROM serendipity_entrytags";
- $query = $DB->query($queryStr);
- while($result = $query->fetch_assoc()) {
- $tags[$result['entryid']][] = $result['tag'];
- }
- $categories = array();
- $queryStr = "SELECT ec.entryid, c.category_name FROM `serendipity_entrycat` AS ec
- LEFT JOIN serendipity_category as c ON ec.categoryid = c.categoryid";
- $query = $DB->query($queryStr);
- while($result = $query->fetch_assoc()) {
- $categories[$result['entryid']][] = $result['category_name'];
- }
- # get the links and preformat them
- $links = array();
- $queryStr = "SELECT * FROM serendipity_permalinks WHERE `type` = 'entry'";
- $query = $DB->query($queryStr);
- while($result = $query->fetch_assoc()) {
- $_link = $result['permalink'];
- $_link = str_replace('archives/'.$result['entry_id'].'-','',$_link);
- $_link = str_replace('.html','',$_link);
- $_link = trim($_link, ' .,');
- $links[$result['entry_id']] = $_link;
- }
- # get the entries
- $queryStr = "SELECT * FROM serendipity_entries WHERE `isdraft` = 'false'";
- $query = $DB->query($queryStr);
- while($result = $query->fetch_assoc()) {
- $_tags = '';
- if(isset($tags[$result['id']])) {
- $_tags = '"'.implode('","',$tags[$result['id']]).'"';
- }
- $tagstring = $_tags;
-
- $_categories = '';
- if(isset($categories[$result['id']])) {
- $_categories = '"'.implode('","',$categories[$result['id']]).'"';
- }
- $category = '"Dev",'.$_categories;
- $category = trim($category, " ,");
- $filename = $result['id'];
- if(isset($links[$result['id']])) {
- $filename = $links[$result['id']];
- }
-
- $year = date("Y",$result['timestamp']);
- $month = date("m",$result['timestamp']);
- if(!file_exists('output/'.$year)) {
- mkdir('output/'.$year);
- }
- if(!file_exists('output/'.$year.'/'.$month)) {
- mkdir('output/'.$year.'/'.$month);
- }
- $targetfile = 'output/'.$year.'/'.$month.'/'.$filename.".md";
- $date = date("Y-m-d",$result['timestamp']);
- $title = trim($result['title']);
- $title = str_replace('"','',$title);
-
- $filedata = <<<FDATA
- +++
- date = "$date"
- title = "$title"
- description = ""
- tags = [ $tagstring ]
- topics = [ $category ]
- subheadline = ""
- $year = "$month"
- +++
- FDATA;
- $body = nl2br($result['body'],false);
- # create the md content from the existing content
- file_put_contents('body.html',$body);
-
- $command = "pandoc --no-wrap --parse-raw -f html -t markdown -o ".$workingDir."/body.md ".$workingDir."/body.html";
- exec($command);
- $mdData = file_get_contents('body.md');
- file_put_contents($targetfile,$filedata.$mdData);
- unlink('body.html');
- unlink('body.md');
- #var_dump($targetfile);
- #var_dump($result);
- }
- $DB->close();
|