data_directory = 'raw/' . $this->SITE_CONFIG['HTTP_HOST']; } function deduce_post_type($post_path) { $ext = pathinfo($post_path, PATHINFO_EXTENSION); if(preg_match("/\.(\w+)\.md$/", $post_path, $ext_match)) { $ext = $ext_match[1]; } $ext_mapping = [ '' => 'directory', 'md' => 'text/markdown', 'png' => 'image', 'jpg' => 'image', 'jpeg' => 'image' ]; return $ext_mapping[$ext] ?? '?'; } function fill_in_post_meta($post_path, $meta) { $icon_mapping = [ '' => 'question', 'text/markdown' => 'markdown', 'directory' => 'folder', 'gallery' => 'images', 'image' => 'image' ]; $meta["title"] ??= basename($post_path); if($meta["title"] == "") { $meta["title"] = "root"; } if(!isset($meta['media_file']) and preg_match("/\.(\w+)\.md$/", $post_path)) { $meta['media_file'] = "https://" . $this->SITE_CONFIG['HTTP_HOST'] . chop($post_path, ".md"); } $meta['tags'] ??= []; $meta['type'] ??= $this->deduce_post_type($post_path); $meta['icon'] ??= $icon_mapping[$meta['type']] ?? 'question'; return $meta; } function _normalize_post_data($post_data) { $post_data = parent::_normalize_post_data($post_data); if(!$post_data['found']) { return $post_data; } $post_data["post_basename"] = basename($post_data["post_path"]); $post_meta = $post_data['post_metadata']; $post_data['post_metadata'] = $this->fill_in_post_meta( $post_data['post_path'], $post_meta); $post_data["post_file_dir"] = '/raw' . $post_data["post_path"]; return $post_data; } function make_post_directory($directory) { $data_directory = $this->data_directory . $directory; is_dir($data_directory) || mkdir($data_directory, 0777, true); parent::make_post_directory($directory); } function update_or_create_post(...$args) { $this->_exec("TRUNCATE feed_cache"); parent::update_or_create_post(...$args); } function save_file($post_path, $file_path) { move_uploaded_file($file_path, $this->data_directory . $post_path); } function save_markdown_post($post_path, $post_data) { $frontmatter_post = YamlFrontMatter::parse($post_data); $post_path = $this->_sanitize_path($post_path); $post_content = $frontmatter_post->body(); $post_metadata = $frontmatter_post->matter(); $post_metadata = $this->fill_in_post_meta( $post_path, $post_metadata); $post_metadata['tags'][]= 'type:' . $post_metadata['type']; if(basename($post_path) == "README.md") { $readme_metadata = []; if(isset($post_metadata['settings'])) { $readme_metadata['settings'] = $post_metadata['settings']; } if(isset($post_metadata['directory'])) { $readme_metadata = $post_metadata['directory']; } $this->update_or_create_post(dirname($post_path), $readme_metadata, $post_content); } $this->update_or_create_post($post_path, $post_metadata, $post_content); } function handle_upload($post_path, $file_path) { $ext = pathinfo($post_path, PATHINFO_EXTENSION); switch($ext) { case "md": $this->save_markdown_post($post_path, file_get_contents($file_path)); move_uploaded_file($file_path, $this->data_directory . $post_path); break; default: $this->save_file($post_path, $file_path); } } function try_get_cached_feed($path, $export_opt) { $post_cache = $this->_exec("SELECT feed_content, feed_created_on FROM feed_cache WHERE host=? AND search_path=? AND export_type=?", "sss", $this->SITE_CONFIG['HTTP_HOST'], $path, $export_opt)->fetch_assoc(); if(!isset($post_cache)) { return null; } return ['feed' => $post_cache['feed_content'], 'feed_ts' => $post_cache['feed_created_on']]; } function construct_feed($path) { $path = $this->_sanitize_path($path); $feed = @new Feed; $feed->setTitle("DergFeed"); $feed->setLink($this->SITE_CONFIG['uri_prefix'] . $path); $feed->setFeedLink($this->SITE_CONFIG['uri_prefix'] . "/feeds/atom" . $path, "atom"); $feed->setDateModified(time()); $feed->setDescription("DergenFeed for all your " . $path . " needs"); $feed_posts = $this->_exec("SELECT post_path, post_create_time, post_update_time, post_content, post_metadata FROM posts WHERE (host = ?) AND ((post_path = ?) OR (post_path LIKE ?)) ORDER BY post_create_time DESC LIMIT 200", "sss", $this->SITE_CONFIG['HTTP_HOST'], $path, $path . '/%'); while($row = $feed_posts->fetch_array(MYSQLI_ASSOC)) { $row = $this->_normalize_post_data($row); $pmeta = $row['post_metadata']; if($pmeta['type'] == 'directory') { continue; } $entry = $feed->createEntry(); $entry->setTitle($row['post_path'] . '> ' . $pmeta['title']); $entry->setLink($this->SITE_CONFIG['uri_prefix'] . $row['post_path']); $entry->setDateModified(strtotime($row['post_update_time'])); $entry->setDateCreated(strtotime($row['post_create_time'])); $entry->setDescription($pmeta['brief'] ?? $pmeta['title']); $feed->addEntry($entry); } return $feed; } function get_laminas_feed($path, $export_opt) { $path = $this->_sanitize_path($path); $feed_cache = $this->try_get_cached_feed($path, $export_opt); if(isset($feed_cache)) { return $feed_cache; } $feed = $this->construct_feed($path); $this->_exec("INSERT INTO feed_cache (host, search_path, export_type, feed_content) VALUES (?, ?, 'atom', ?), (?, ?, 'rss', ?)", "ssssss", $this->SITE_CONFIG['HTTP_HOST'], $path, $feed->export('atom'), $this->SITE_CONFIG['HTTP_HOST'], $path, $feed->export('rss')); return $this->try_get_cached_feed($path, $export_opt); } } ?>