/apps/wiki/domain/Entry.php

http://zoop.googlecode.com/ · PHP · 140 lines · 121 code · 17 blank · 2 comment · 13 complexity · fcc7d0798d45be3525523bd162c2d74f MD5 · raw file

  1. <?php
  2. class Entry extends DbObject
  3. {
  4. public function init()
  5. {
  6. $this->keyAssignedBy = parent::keyAssignedBy_dev;
  7. }
  8. public function getDateNameUrl()
  9. {
  10. $datePart = substr($this->published_date, 0, strpos($this->published_date, ' '));
  11. $datePart = str_replace('-', '/', $datePart);
  12. return $datePart . '/' . $this->name;
  13. }
  14. public function getBasePath()
  15. {
  16. $id = str_pad($this->id, 4, '0', STR_PAD_LEFT);
  17. return Config::get('app.blog.contentDir') . "/{$id}_{$this->name}";
  18. }
  19. public function getContentPath()
  20. {
  21. return $this->getBasePath() . '/content.md';
  22. }
  23. public function getCacheDir()
  24. {
  25. return app_dir . "/tmp/content/{$this->id}";
  26. }
  27. public function streamAsset($name)
  28. {
  29. $info = pathinfo($name);
  30. // if($info['extension'] == 'png')
  31. // header('Content-Type: image/png');
  32. readfile($this->getCacheDir() . '/' . $name);
  33. }
  34. public function getHeaders()
  35. {
  36. $pre = file($this->getContentPath());
  37. $headers = array();
  38. foreach($pre as $line)
  39. {
  40. $line = trim($line);
  41. if(!$line)
  42. break;
  43. $pos = strpos($line, ':');
  44. $key = strtolower(trim(substr($line, 0, $pos)));
  45. $value = trim(substr($line, $pos + 1));
  46. $headers[$key] = $value;
  47. }
  48. return $headers;
  49. }
  50. public function assignHeaders()
  51. {
  52. $headers = $this->getHeaders();
  53. echo_r($headers);
  54. if(isset($headers['publish-date']))
  55. $this->published_date = $headers['publish-date'];
  56. if(isset($headers['title']))
  57. $this->title = $headers['title'];
  58. if(isset($headers['link']))
  59. $this->link = $headers['link'];
  60. if(isset($headers['link-text']))
  61. $this->link_text = $headers['link-text'];
  62. echo_r($this);
  63. }
  64. public function getContent($cacheResults = false)
  65. {
  66. $pre = file($this->getContentPath());
  67. $headers = array();
  68. $inHeaders = true;
  69. $content = '';
  70. foreach($pre as $line)
  71. {
  72. if($inHeaders && !trim($line))
  73. {
  74. $inHeaders = false;
  75. continue;
  76. }
  77. if($inHeaders)
  78. {
  79. $pos = strpos($line, ':');
  80. $key = strtolower(trim(substr($line, 0, $pos)));
  81. $value = trim(substr($line, $pos + 1));
  82. $headers[$key] = $value;
  83. }
  84. else
  85. $content .= $line;
  86. }
  87. if($cacheResults)
  88. {
  89. $cacheDir = $this->getCacheDir();
  90. if(!is_dir($cacheDir))
  91. mkdir($cacheDir, 0770, true);
  92. }
  93. if(isset($headers['filters']))
  94. {
  95. include_once app_dir . '/domain/filters/ContentFilter.php';
  96. $filterList = explode(',', $headers['filters']);
  97. foreach($filterList as $filterName)
  98. {
  99. $filterName = trim($filterName);
  100. include_once app_dir . "/domain/filters/$filterName.php";
  101. $className = ucfirst($filterName) . 'Filter';
  102. $filter = new $className();
  103. $filter->assetBaseUrl = 'index.php/asset/' . $this->id;
  104. $filter->assetSrcDir = $this->getBasePath();
  105. if($cacheResults)
  106. {
  107. $filter->assetCacheDir = $cacheDir;
  108. }
  109. $content = $filter->filter($content, $cacheResults);
  110. }
  111. }
  112. if($cacheResults)
  113. {
  114. $filename = "$cacheDir/{$this->name}.html";
  115. file_put_contents($filename, $this->getContent());
  116. }
  117. return $content;
  118. }
  119. public function cacheContent()
  120. {
  121. $this->getContent(true);
  122. }
  123. }