public function write_index_sequence($dest_path, $title, $type, array $posts, $template = 'main.html', $archive_array = false, $posts_per_page = 20) { $sequence = 0; $new_dest_path = $dest_path; // $dest_uri = substring_after(substring_before($dest_path, '.', true), Updater::$dest_path); $dest_uri = substring_after(substring_after($dest_path, '/', true), Updater::$dest_path); $total_sequences = ceil(count($posts) / $posts_per_page); while ($posts) { $sequence++; $seq_array = array_splice($posts, 0, $posts_per_page); if ($sequence == 1) { continue; } // skip writing the redundant "-1" page $new_dest_path = $dest_path . '-' . $sequence . '.html'; $posts_data = array(); foreach ($seq_array as $p) { $posts_data[] = $p->array_for_template(); } $t = new Template($template); $t->content = array('page-title' => html_entity_decode(SmartyPants($title), ENT_QUOTES, 'UTF-8'), 'blog-title' => html_entity_decode(SmartyPants(self::$blog_title), ENT_QUOTES, 'UTF-8'), 'blog-url' => self::$blog_url, 'blog-description' => html_entity_decode(SmartyPants(self::$blog_description), ENT_QUOTES, 'UTF-8'), 'page-type' => $type, 'posts' => $posts_data, 'previous_page_url' => $sequence != 1 ? $sequence == 2 ? $dest_uri : $dest_uri . '-' . ($sequence - 1) : false, 'next_page_url' => $sequence < $total_sequences ? $dest_uri . '-' . ($sequence + 1) : false, 'archives' => $archive_array ? $archive_array : array()); $output_html = $t->outputHTML(); $output_path = dirname($new_dest_path); if (!file_exists($output_path)) { mkdir_as_parent_owner($output_path, 0755, true); } file_put_contents_as_dir_owner($new_dest_path, $output_html); } return $total_sequences; }
function substring_between($haystack, $left_needle, $right_needle, $from_end = false) { return substring_before(substring_after($haystack, $left_needle), $right_needle, $from_end); }
private static function _update($restart_if_resequenced = true) { if (!file_exists(self::$dest_path)) { mkdir_as_parent_owner(self::$dest_path, 0755, true); } if (!file_exists(self::$dest_path . '/.htaccess')) { copy(dirname(__FILE__) . '/default.htaccess', self::$dest_path . '/.htaccess'); } if (!file_exists(self::$source_path . '/drafts/_publish-now')) { mkdir_as_parent_owner(self::$source_path . '/drafts/_publish-now', 0755, true); } if (!file_exists(self::$source_path . '/drafts/_previews')) { mkdir_as_parent_owner(self::$source_path . '/drafts/_previews', 0755, true); } if (!file_exists(self::$source_path . '/pages')) { mkdir_as_parent_owner(self::$source_path . '/pages', 0755, true); } self::update_pages(); self::update_drafts(); foreach (self::changed_files_in_directory(self::$source_path . '/media') as $filename => $info) { error_log("Changed media file: {$filename}"); $uri = substring_after($filename, self::$source_path); $dest_filename = self::$dest_path . $uri; $output_path = dirname($dest_filename); if (!file_exists($output_path)) { mkdir_as_parent_owner($output_path, 0755, true); } copy($filename, $dest_filename); self::$changes_were_written = true; } $resequence_days = array(); foreach (self::changed_files_in_directory(self::$source_path . '/posts') as $filename => $info) { self::$posts_to_be_updated[$filename] = true; if (substr($filename, -strlen(self::$post_extension)) == self::$post_extension) { list($old_info, $new_info) = $info; list($old_hash, $old_type, $old_tags) = explode('|', $old_info, 3); list($new_hash, $new_type, $new_tags) = explode('|', $new_info, 3); if ($old_type) { self::$types_to_be_updated[$old_type] = true; } if ($new_type) { self::$types_to_be_updated[$new_type] = true; } foreach (Post::parse_tag_str($old_tags) as $tag) { self::$tags_to_be_updated[$tag] = true; } foreach (Post::parse_tag_str($new_tags) as $tag) { self::$tags_to_be_updated[$tag] = true; } } $yearpos = strpos($filename, '/posts/') + 7; $year = substr($filename, $yearpos, 4); $month = substr($filename, $yearpos + 5, 2); $day = substr($filename, $yearpos + 14, 2); $resequence_days[$year . $month . $day] = array($year, $month, $day); self::$index_months_to_be_updated[$year . '-' . $month] = true; self::$index_to_be_updated = true; } foreach (self::$posts_to_be_updated as $filename => $x) { if (!file_exists($filename)) { // This file was deleted. Delete corresponding output file $filename_datestr = substr(basename($filename), 0, 8); if (is_numeric($filename_datestr)) { $year = substr($filename_datestr, 0, 4); $month = substr($filename_datestr, 4, 2); $day = substr($filename_datestr, 6, 2); $slug = substr(basename($filename), 12, -strlen(self::$post_extension)); $target_filename = self::$dest_path . "/{$year}/{$month}/{$day}/{$slug}"; if ($year && $month && $day && $slug && file_exists($target_filename)) { error_log("Deleting abandoned target file: {$target_filename}"); safe_unlink($target_filename); } } continue; } error_log("Updating post: {$filename}"); self::$changes_were_written = true; if (substr($filename, -strlen(self::$post_extension)) == self::$post_extension) { $post = new Post($filename); $post->write_permalink_page(); self::set_has_posts_for_month($post->year, $post->month); foreach ($post->tags as $tag) { self::set_has_posts_for_month($post->year, $post->month, 'tagged-' . $tag); } if ($post->type) { self::set_has_posts_for_month($post->year, $post->month, 'type-' . $post->type); } } else { $filename_datestr = substr(basename($filename), 0, 8); if (is_numeric($filename_datestr)) { $year = intval(substr($filename_datestr, 0, 4)); $month = intval(substr($filename_datestr, 4, 2)); $day = intval(substr($filename_datestr, 6, 2)); if ($year && $month && $day) { $output_path = self::$dest_path . "/{$year}/" . str_pad($month, 2, '0', STR_PAD_LEFT) . '/' . str_pad($day, 2, '0', STR_PAD_LEFT); if (!file_exists($output_path)) { mkdir_as_parent_owner($output_path, 0755, true); } $output_filename = $output_path . '/' . basename($filename); copy($filename, $output_filename); $resequence_days[$year . $month . $day] = array($year, $month, $day); } else { error_log("Can't figure out where to put unrecognized numeric filename [{$filename}]"); } } else { error_log("Can't figure out where to put unrecognized filename [{$filename}]"); } } } $sequence_changed = false; foreach ($resequence_days as $a) { list($year, $month, $day) = array_map('intval', $a); if (self::resequence_post_offsets($year, $month, $day)) { $sequence_changed = true; } } if ($sequence_changed && $restart_if_resequenced) { self::$changes_were_written = true; error_log("Resequencing was performed. Restarting update..."); return self::RESEQUENCED_POSTS; } if (!self::$index_to_be_updated) { self::$index_to_be_updated = self::$index_months_to_be_updated || self::$tags_to_be_updated || self::$types_to_be_updated || self::$posts_to_be_updated; } if (self::$index_to_be_updated) { error_log("Updating frontpage..."); self::$changes_were_written = true; $seq_count = 0; if (self::$frontpage_paginate) { $seq_count = Post::write_index_sequence(self::$dest_path . "/index", Post::$blog_title, 'frontpage', Post::from_files(self::most_recent_post_filenames(0, self::$frontpage_tag_filter, self::$frontpage_type_filter)), self::$frontpage_template, self::archive_array(), self::$frontpage_post_limit); } Post::write_index(self::$dest_path . "/index.html", Post::$blog_title, 'frontpage', Post::from_files(self::most_recent_post_filenames(self::$frontpage_post_limit, self::$frontpage_tag_filter, self::$frontpage_type_filter)), self::$frontpage_template, self::archive_array(), $seq_count); error_log("Updating RSS..."); Post::write_index(self::$dest_path . "/rss.xml", Post::$blog_title, 'rss', Post::from_files(self::most_recent_post_filenames(self::$rss_post_limit, self::$rss_tag_filter, self::$rss_type_filter)), self::$rss_template); } foreach (self::$index_months_to_be_updated as $ym => $x) { error_log("Updating month index: {$ym}"); self::$changes_were_written = true; list($year, $month) = explode('-', $ym); $posts = Post::from_files(self::post_filenames_in_year_month($year, $month, self::$archive_tag_filter, self::$archive_type_filter)); $ts = mktime(0, 0, 0, $month, 15, $year); Post::write_index(self::$dest_path . "/{$year}/{$month}/index.html", date('F Y', $ts), 'archive', $posts, self::$archive_month_template, self::archive_array()); } foreach (self::$tags_to_be_updated as $tag => $x) { if (!strlen($tag)) { continue; } error_log("Updating tag: {$tag}"); self::$changes_were_written = true; $seq_count = Post::write_index_sequence(self::$dest_path . "/tagged-{$tag}", Post::$blog_title, 'tag', Post::from_files(self::most_recent_post_filenames(0, $tag, self::$archive_tag_filter)), self::$tag_page_template, self::archive_array(), self::$tag_page_post_limit); Post::write_index(self::$dest_path . "/tagged-{$tag}.html", Post::$blog_title, 'tag', Post::from_files(self::most_recent_post_filenames(self::$frontpage_post_limit, $tag, self::$archive_tag_filter)), self::$tag_page_template, self::archive_array('tagged-' . $tag), $seq_count); Post::write_index(self::$dest_path . "/tagged-{$tag}.xml", Post::$blog_title, 'tag', Post::from_files(self::most_recent_post_filenames(self::$rss_post_limit, $tag, self::$archive_tag_filter)), self::$rss_template, self::archive_array('tagged-' . $tag)); $months_with_posts = self::months_with_posts('tagged-' . $tag); foreach (self::$index_months_to_be_updated as $ym => $x) { list($year, $month) = explode('-', $ym); if (!isset($months_with_posts[$year]) || !isset($months_with_posts[$year][intval($month)])) { continue; } error_log("Updating month index: {$ym} for tag: {$tag}"); $posts = Post::from_files(self::post_filenames_in_year_month($year, $month, $tag, self::$archive_type_filter)); $ts = mktime(0, 0, 0, $month, 15, $year); Post::write_index(self::$dest_path . "/{$year}/{$month}/tagged-{$tag}.html", date('F Y', $ts), 'tag', $posts, self::$tag_page_template, self::archive_array('tagged-' . $tag)); } } foreach (self::$types_to_be_updated as $type => $x) { if (!strlen($type)) { continue; } error_log("Updating type: {$type}"); self::$changes_were_written = true; Post::write_index(self::$dest_path . "/type-{$type}.html", Post::$blog_title, 'type', Post::from_files(self::most_recent_post_filenames(self::$frontpage_post_limit, self::$archive_type_filter, $type)), self::$type_page_template, self::archive_array('type-' . $type)); Post::write_index(self::$dest_path . "/type-{$type}.xml", Post::$blog_title, 'type', Post::from_files(self::most_recent_post_filenames(self::$rss_post_limit, self::$archive_type_filter, $type)), self::$rss_template, self::archive_array('type-' . $type)); $months_with_posts = self::months_with_posts('type-' . $type); foreach (self::$index_months_to_be_updated as $ym => $x) { list($year, $month) = explode('-', $ym); if (!isset($months_with_posts[$year]) || !isset($months_with_posts[$year][intval($month)])) { continue; } error_log("Updating month index: {$ym} for type: {$type}"); $posts = Post::from_files(self::post_filenames_in_year_month($year, $month, self::$archive_tag_filter, $type)); $ts = mktime(0, 0, 0, $month, 15, $year); Post::write_index(self::$dest_path . "/{$year}/{$month}/type-{$type}.html", date('F Y', $ts), 'type', $posts, self::$type_page_template, self::archive_array('type-' . $type)); } } }