export.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. /**
  3. * dolphin. Collection of useful PHP skeletons.
  4. * Copyright (C) 2016 Johannes 'Banana' Keßler
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the COMMON DEVELOPMENT AND DISTRIBUTION LICENSE
  8. *
  9. * You should have received a copy of the
  10. * COMMON DEVELOPMENT AND DISTRIBUTION LICENSE (CDDL) Version 1.0
  11. * along with this program. If not, see http://www.sun.com/cddl/cddl.html
  12. */
  13. /**
  14. * this is a very simple serendipity to md export.
  15. * user this as a base to improve your own export.
  16. */
  17. mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); # throw exeptions
  18. $DB = new mysqli('127.0.0.1', 'root', 'pasword', 'blog');
  19. if ($DB->connect_errno) exit('Can not connect to MySQL Server');
  20. #$DB->set_charset("utf8mb4");
  21. #$DB->query("SET collation_connection = 'utf8mb4_bin'");
  22. $workingDir = getcwd();
  23. date_default_timezone_set('Europe/Berlin');
  24. # get the tags as array per entrie
  25. $tags = array();
  26. $queryStr = "SELECT * FROM serendipity_entrytags";
  27. $query = $DB->query($queryStr);
  28. while($result = $query->fetch_assoc()) {
  29. $tags[$result['entryid']][] = $result['tag'];
  30. }
  31. $categories = array();
  32. $queryStr = "SELECT ec.entryid, c.category_name FROM `serendipity_entrycat` AS ec
  33. LEFT JOIN serendipity_category as c ON ec.categoryid = c.categoryid";
  34. $query = $DB->query($queryStr);
  35. while($result = $query->fetch_assoc()) {
  36. $categories[$result['entryid']][] = $result['category_name'];
  37. }
  38. # get the links and preformat them
  39. $links = array();
  40. $queryStr = "SELECT * FROM serendipity_permalinks WHERE `type` = 'entry'";
  41. $query = $DB->query($queryStr);
  42. while($result = $query->fetch_assoc()) {
  43. $_link = $result['permalink'];
  44. $_link = str_replace('archives/'.$result['entry_id'].'-','',$_link);
  45. $_link = str_replace('.html','',$_link);
  46. $_link = trim($_link, ' .,');
  47. $links[$result['entry_id']] = $_link;
  48. }
  49. # get the entries
  50. $queryStr = "SELECT * FROM serendipity_entries WHERE `isdraft` = 'false'";
  51. $query = $DB->query($queryStr);
  52. while($result = $query->fetch_assoc()) {
  53. $_tags = '';
  54. if(isset($tags[$result['id']])) {
  55. $_tags = '"'.implode('","',$tags[$result['id']]).'"';
  56. }
  57. $tagstring = $_tags;
  58. $_categories = '';
  59. if(isset($categories[$result['id']])) {
  60. $_categories = '"'.implode('","',$categories[$result['id']]).'"';
  61. }
  62. $category = '"Dev",'.$_categories;
  63. $category = trim($category, " ,");
  64. $filename = $result['id'];
  65. if(isset($links[$result['id']])) {
  66. $filename = $links[$result['id']];
  67. }
  68. $year = date("Y",$result['timestamp']);
  69. $month = date("m",$result['timestamp']);
  70. if(!file_exists('output/'.$year)) {
  71. mkdir('output/'.$year);
  72. }
  73. if(!file_exists('output/'.$year.'/'.$month)) {
  74. mkdir('output/'.$year.'/'.$month);
  75. }
  76. $targetfile = 'output/'.$year.'/'.$month.'/'.$filename.".md";
  77. $date = date("Y-m-d",$result['timestamp']);
  78. $title = trim($result['title']);
  79. $title = str_replace('"','',$title);
  80. $filedata = <<<FDATA
  81. +++
  82. date = "$date"
  83. title = "$title"
  84. description = ""
  85. tags = [ $tagstring ]
  86. topics = [ $category ]
  87. subheadline = ""
  88. $year = "$month"
  89. +++
  90. FDATA;
  91. $body = nl2br($result['body'],false);
  92. # create the md content from the existing content
  93. file_put_contents('body.html',$body);
  94. $command = "pandoc --no-wrap --parse-raw -f html -t markdown -o ".$workingDir."/body.md ".$workingDir."/body.html";
  95. exec($command);
  96. $mdData = file_get_contents('body.md');
  97. file_put_contents($targetfile,$filedata.$mdData);
  98. unlink('body.html');
  99. unlink('body.md');
  100. #var_dump($targetfile);
  101. #var_dump($result);
  102. }
  103. $DB->close();