Parser.freemind.class.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. /**
  3. * km => FreeMind
  4. */
  5. require_once "Tpl.class.php";
  6. require_once "FileHelper.class.php";
  7. require_once "Ziper.class.php";
  8. require_once "ImageCapture.class.php";
  9. class FreeMindParser {
  10. public static function parse ( $source ) {
  11. $sourceJSON = json_decode( $source, true );
  12. $data = self::doParse( $sourceJSON );
  13. return $data;
  14. }
  15. private static function doParse ( $source ) {
  16. $data = self::parseTopic( $source );
  17. $content = Tpl::compile( "freemind/freemind.xml", array (
  18. 'topic' => $data
  19. ) );
  20. $savepath = self::getSavePath();
  21. $filepath = $savepath . '/' . $data[ 'meta' ][ 'id' ] . '.mm';
  22. file_put_contents( $filepath, $content );
  23. return $filepath;
  24. }
  25. /**
  26. * topic 解析
  27. * @param array $source KM数据源
  28. * @return 解析后的topic数据
  29. */
  30. private static function parseTopic ( &$source, $position = null ) {
  31. $pos = [ "right", "left" ];
  32. $hasPosition = empty( $position );
  33. if ( !array_key_exists( "data", $source ) ) {
  34. return $source;
  35. }
  36. $source[ 'meta' ] = self::getTimeAndId();
  37. if ( !$hasPosition ) {
  38. $source[ 'position' ] = $position;
  39. }
  40. if ( !array_key_exists( "children", $source ) ) {
  41. return $source;
  42. }
  43. foreach ( $source[ "children" ] as $key => &$child ) {
  44. if ( $hasPosition ) {
  45. $position = $pos[ $key % 2 ];
  46. }
  47. self::parseTopic( $child, $position );
  48. }
  49. return $source;
  50. }
  51. private static function getSavePath () {
  52. $config = require( dirname(__FILE__) . '/../config.php' );
  53. $savepath = $config[ 'savepath' ];
  54. if ( !file_exists( $savepath ) ) {
  55. mkdir( $savepath, 0770, true );
  56. }
  57. return pathinfo( $savepath . '/test', PATHINFO_DIRNAME );
  58. }
  59. private static function getTimeAndId () {
  60. $timestamp = intval( microtime( true ) * 1000 );
  61. return array(
  62. 'timestamp' => $timestamp,
  63. 'id' => md5( $timestamp )
  64. );
  65. }
  66. }