From ed07404f08eafa267a4bca13fdfbfacf5f19975f Mon Sep 17 00:00:00 2001 From: czaks Date: Wed, 1 Apr 2015 17:50:59 +0200 Subject: smart_build.php: initial release --- smart_build.php | 139 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 139 insertions(+) create mode 100644 smart_build.php (limited to 'smart_build.php') diff --git a/smart_build.php b/smart_build.php new file mode 100644 index 00000000..6621238e --- /dev/null +++ b/smart_build.php @@ -0,0 +1,139 @@ + $config['max_pages']) return false; + $config['try_smarter'] = true; + $build_pages = array($page); + buildIndex("skip"); + return true; +} + +function sb_api_board($b, $page = 0) { $page = (int)$page; + return sb_board($b, $page + 1); +} + +function sb_thread($b, $thread) { global $config; $thread = (int)$thread; + if (!openBoard($b)) return false; + buildThread($thread); + return true; +} + +function sb_api($b) { global $config; + if (!openBoard($b)) return false; + $config['try_smarter'] = true; + $build_pages = array(-1); + buildIndex(); + return true; +} + +function sb_ukko() { + rebuildTheme("ukko", "post-thread"); + return true; +} + +function sb_catalog($b) { + rebuildTheme("catalog", "post-thread", $b); + return true; +} + +function sb_recent() { + rebuildTheme("recent", "post-thread"); + return true; +} + +$entrypoints = array(); + +$entrypoints['/%b/'] = 'sb_board'; +$entrypoints['/%b/'.$config['file_index']] = 'sb_board'; +$entrypoints['/%b/'.$config['file_page']] = 'sb_board'; +$entrypoints['/%b/%d.json'] = 'sb_api_board'; +if ($config['api']['enabled']) { + $entrypoints['/%b/threads.json'] = 'sb_api'; + $entrypoints['/%b/catalog.json'] = 'sb_api'; +} + +$entrypoints['/%b/'.$config['dir']['res'].$config['file_page']] = 'sb_thread'; +$entrypoints['/%b/'.$config['dir']['res'].$config['file_page50']] = 'sb_thread'; +if ($config['slugify']) { + $entrypoints['/%b/'.$config['dir']['res'].$config['file_page_slug']] = 'sb_thread'; + $entrypoints['/%b/'.$config['dir']['res'].$config['file_page50_slug']] = 'sb_thread'; +} +if ($config['api']['enabled']) { + $entrypoints['/%b/'.$config['dir']['res'].'%d.json'] = 'sb_thread'; +} + +$entrypoints['/*/'] = 'sb_ukko'; +$entrypoints['/*/index.html'] = 'sb_ukko'; +$entrypoints['/recent.html'] = 'sb_recent'; +$entrypoints['/%b/catalog.html'] = 'sb_catalog'; + +$reached = false; + +$request = $_SERVER['REQUEST_URI']; + +foreach ($entrypoints as $id => $fun) { + $id = '@^' . preg_quote($id, '@') . '$@u'; + + $id = str_replace('%b', '('.$config['board_regex'].')', $id); + $id = str_replace('%d', '([0-9]+)', $id); + $id = str_replace('%s', '[a-zA-Z0-9-]+', $id); + + $matches = null; + + if (preg_match ($id, $request, $matches)) { + array_shift($matches); + + $reached = call_user_func_array($fun, $matches); + + break; + } +} + +if ($reached) { + if ($request[strlen($request)-1] == '/') { + $request .= 'index.html'; + } + $request = '.'.$request; + + if (!file_exists($request)) { + header("Location: ".$config['page_404']); + die(); + } + + header("HTTP/1.1 200 OK"); + header("Status: 200 OK"); + if (preg_match('/\.json$/', $request)) { + header("Content-Type", "application/json"); + } + elseif (preg_match('/\.js$/', $request)) { + header("Content-Type", "text/javascript; charset=utf-8"); + } + else { + header("Content-Type", "text/html; charset=utf-8"); + } + header("Cache-Control: public, nocache, no-cache, max-age=0, must-revalidate"); + header("Expires: Fri, 22 Feb 1991 06:00:00 GMT"); + header("Last-Modified: ".date('r', filemtime($request))); + + //if (isset ($_SERVER['HTTP_ACCEPT_ENCODING']) && preg_match('/gzip/', $_SERVER['HTTP_ACCEPT_ENCODING']) && file_exists($request.".gz")) { + // header("Content-Encoding: gzip"); + // $file = fopen($request.".gz", 'r'); + //} + //else { + $file = fopen($request, 'r'); + //} + fpassthru($file); + fclose($file); +} +else { + header("Location: ".$config['page_404']); +} -- cgit v1.3 From a19853dbe82f10fa9a3c7c69f3da6662fd0fc589 Mon Sep 17 00:00:00 2001 From: czaks Date: Wed, 1 Apr 2015 18:46:48 +0200 Subject: smart_build.php: misc fixes --- smart_build.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'smart_build.php') diff --git a/smart_build.php b/smart_build.php index 6621238e..d937241b 100644 --- a/smart_build.php +++ b/smart_build.php @@ -7,8 +7,12 @@ if (!$config['smart_build']) { $config['smart_build'] = false; // Let's disable it, so we can build the page for real +function after_open_board() { global $config; + $config['smart_build'] = false; +}; + function sb_board($b, $page = 1) { global $config, $build_pages; $page = (int)$page; - if ($page < 1 && $page != -1) return false; + if ($page < 1) return false; if (!openBoard($b)) return false; if ($page > $config['max_pages']) return false; $config['try_smarter'] = true; @@ -46,7 +50,7 @@ function sb_catalog($b) { } function sb_recent() { - rebuildTheme("recent", "post-thread"); + rebuildTheme("recent", "post-thread"); return true; } -- cgit v1.3 From 0870acb88e9f872071c8891fbec49e0e673beaea Mon Sep 17 00:00:00 2001 From: Marcin Łabanowski Date: Thu, 2 Apr 2015 20:27:10 +0200 Subject: smart_build.php: optimizations, guards, also a functionality to correct slugs --- smart_build.php | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 60 insertions(+), 8 deletions(-) (limited to 'smart_build.php') diff --git a/smart_build.php b/smart_build.php index d937241b..56299aee 100644 --- a/smart_build.php +++ b/smart_build.php @@ -25,12 +25,53 @@ function sb_api_board($b, $page = 0) { $page = (int)$page; return sb_board($b, $page + 1); } -function sb_thread($b, $thread) { global $config; $thread = (int)$thread; +function sb_thread($b, $thread, $slugcheck = false) { global $config; $thread = (int)$thread; + if ($thread < 1) return false; + + if (!preg_match('/^'.$config['board_regex'].'$/u', $b)) return false; + + if (Cache::get("thread_exists_".$b."_".$thread) == "no") return false; + + $query = prepare(sprintf("SELECT MAX(`id`) AS `max` FROM ``posts_%s``", $b)); + if (!$query->execute()) return false; + + $s = $query->fetch(PDO::FETCH_ASSOC); + $max = $s['max']; + + if ($thread > $max) return false; + + $query = prepare(sprintf("SELECT `id` FROM ``posts_%s`` WHERE `id` = :id AND `thread` IS NULL", $b)); + $query->bindValue(':id', $thread); + + if (!$query->execute() || !$query->fetch(PDO::FETCH_ASSOC) ) { + Cache::set("thread_exists_".$b."_".$thread, "no"); + return false; + } + + if ($slugcheck && $config['slugify']) { + global $request; + + $link = link_for(array("id" => $thread), $slugcheck === 50, array("uri" => $b)); + $link = "/".$b."/".$config['dir']['res'].$link; + + if ($link != $request) { + header("Location: $link", true, 301); + die(); + } + } + if (!openBoard($b)) return false; buildThread($thread); return true; } +function sb_thread_slugcheck($b, $thread) { + return sb_thread($b, $thread, true); +} +function sb_thread_slugcheck50($b, $thread) { + return sb_thread($b, $thread, 50); +} + function sb_api($b) { global $config; if (!openBoard($b)) return false; $config['try_smarter'] = true; @@ -65,11 +106,11 @@ if ($config['api']['enabled']) { $entrypoints['/%b/catalog.json'] = 'sb_api'; } -$entrypoints['/%b/'.$config['dir']['res'].$config['file_page']] = 'sb_thread'; -$entrypoints['/%b/'.$config['dir']['res'].$config['file_page50']] = 'sb_thread'; +$entrypoints['/%b/'.$config['dir']['res'].$config['file_page']] = 'sb_thread_slugcheck'; +$entrypoints['/%b/'.$config['dir']['res'].$config['file_page50']] = 'sb_thread_slugcheck50'; if ($config['slugify']) { - $entrypoints['/%b/'.$config['dir']['res'].$config['file_page_slug']] = 'sb_thread'; - $entrypoints['/%b/'.$config['dir']['res'].$config['file_page50_slug']] = 'sb_thread'; + $entrypoints['/%b/'.$config['dir']['res'].$config['file_page_slug']] = 'sb_thread_slugcheck'; + $entrypoints['/%b/'.$config['dir']['res'].$config['file_page50_slug']] = 'sb_thread_slugcheck50'; } if ($config['api']['enabled']) { $entrypoints['/%b/'.$config['dir']['res'].'%d.json'] = 'sb_thread'; @@ -102,6 +143,18 @@ foreach ($entrypoints as $id => $fun) { } } +function die_404() { global $config; + if (!$config['page_404']) { + header("HTTP/1.1 404 Not Exists"); + header("Status: 404 Not Exists"); + echo "

404 Not Exists

Page doesn't exist


vichan
"; + } + else { + header("Location: ".$config['page_404']); + } + die(); +} + if ($reached) { if ($request[strlen($request)-1] == '/') { $request .= 'index.html'; @@ -109,8 +162,7 @@ if ($reached) { $request = '.'.$request; if (!file_exists($request)) { - header("Location: ".$config['page_404']); - die(); + die_404(); } header("HTTP/1.1 200 OK"); @@ -139,5 +191,5 @@ if ($reached) { fclose($file); } else { - header("Location: ".$config['page_404']); + die_404(); } -- cgit v1.3 From 018ec3b605091af93b58a48d563410976212d470 Mon Sep 17 00:00:00 2001 From: czaks Date: Thu, 2 Apr 2015 20:35:17 +0200 Subject: smart_build.php: additional fixes --- smart_build.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'smart_build.php') diff --git a/smart_build.php b/smart_build.php index 56299aee..d8352621 100644 --- a/smart_build.php +++ b/smart_build.php @@ -124,6 +124,7 @@ $entrypoints['/%b/catalog.html'] = 'sb_catalog'; $reached = false; $request = $_SERVER['REQUEST_URI']; +list($request) = explode('?', $request); foreach ($entrypoints as $id => $fun) { $id = '@^' . preg_quote($id, '@') . '$@u'; @@ -145,13 +146,14 @@ foreach ($entrypoints as $id => $fun) { function die_404() { global $config; if (!$config['page_404']) { - header("HTTP/1.1 404 Not Exists"); - header("Status: 404 Not Exists"); - echo "

404 Not Exists

Page doesn't exist


vichan
"; + header("HTTP/1.1 404 Not Found"); + header("Status: 404 Not Found"); + echo "

404 Not Found

Page doesn't exist


vichan
"; } else { header("Location: ".$config['page_404']); } + die(); } -- cgit v1.3 From fd0f3195b2ae36cbe2b4d599734cf00b80e3a1f0 Mon Sep 17 00:00:00 2001 From: czaks Date: Thu, 2 Apr 2015 20:43:06 +0200 Subject: smart_build.php: add a protection for +50; maybe we don`t need to rebuild after all? --- smart_build.php | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'smart_build.php') diff --git a/smart_build.php b/smart_build.php index d8352621..dd9450ae 100644 --- a/smart_build.php +++ b/smart_build.php @@ -59,6 +59,14 @@ function sb_thread($b, $thread, $slugcheck = false) { global $config; $thread = die(); } } + + if ($slugcheck == 50) { // Should we really generate +50 page? Maybe there are not enough posts anyway + global $request; + $r = str_replace("+50", "", $request); + $r = substr($r, 1); // Cut the slash + + if (file_exists($r)) return false; + } if (!openBoard($b)) return false; buildThread($thread); -- cgit v1.3 From e1a8c4aa7dc62186e7c74436db9fe89071647b3d Mon Sep 17 00:00:00 2001 From: czaks Date: Thu, 2 Apr 2015 20:53:50 +0200 Subject: smart_build.php: a small fix to catalog handling --- smart_build.php | 2 ++ 1 file changed, 2 insertions(+) (limited to 'smart_build.php') diff --git a/smart_build.php b/smart_build.php index dd9450ae..fb467233 100644 --- a/smart_build.php +++ b/smart_build.php @@ -94,6 +94,8 @@ function sb_ukko() { } function sb_catalog($b) { + if (!openBoard($b)) return false; + rebuildTheme("catalog", "post-thread", $b); return true; } -- cgit v1.3 From e2731113404bda42ee506e29b6927165b7576f61 Mon Sep 17 00:00:00 2001 From: czaks Date: Fri, 3 Apr 2015 05:57:39 +0200 Subject: sitemap theme for smart_build --- smart_build.php | 9 ++++++++ templates/themes/sitemap/theme.php | 47 ++++++++++++++++++++++---------------- 2 files changed, 36 insertions(+), 20 deletions(-) (limited to 'smart_build.php') diff --git a/smart_build.php b/smart_build.php index fb467233..43734e4f 100644 --- a/smart_build.php +++ b/smart_build.php @@ -105,6 +105,11 @@ function sb_recent() { return true; } +function sb_sitemap() { + rebuildTheme("sitemap", "all"); + return true; +} + $entrypoints = array(); $entrypoints['/%b/'] = 'sb_board'; @@ -130,6 +135,7 @@ $entrypoints['/*/'] = 'sb_ukko'; $entrypoints['/*/index.html'] = 'sb_ukko'; $entrypoints['/recent.html'] = 'sb_recent'; $entrypoints['/%b/catalog.html'] = 'sb_catalog'; +$entrypoints['/sitemap.xml'] = 'sb_sitemap'; $reached = false; @@ -185,6 +191,9 @@ if ($reached) { elseif (preg_match('/\.js$/', $request)) { header("Content-Type", "text/javascript; charset=utf-8"); } + elseif (preg_match('/\.xml$/', $request)) { + header("Content-Type", "application/xml"); + } else { header("Content-Type", "text/html; charset=utf-8"); } diff --git a/templates/themes/sitemap/theme.php b/templates/themes/sitemap/theme.php index 41a7f304..52779d53 100644 --- a/templates/themes/sitemap/theme.php +++ b/templates/themes/sitemap/theme.php @@ -10,30 +10,37 @@ // - boards (board list changed) // - post (a post has been made) // - thread (a thread has been made) + + if ($action != 'all') { + if ($action != 'post-thread' && $action != 'post-delete') + return; - if ($action != 'post-thread' && $action != 'post-delete') - return; - - if (isset($settings['regen_time']) && $settings['regen_time'] > 0) { - if ($last_gen = @filemtime($settings['path'])) { - if (time() - $last_gen < (int)$settings['regen_time']) - return; // Too soon + if (isset($settings['regen_time']) && $settings['regen_time'] > 0) { + if ($last_gen = @filemtime($settings['path'])) { + if (time() - $last_gen < (int)$settings['regen_time']) + return; // Too soon + } } } + + if ($config['smart_build']) { + file_unlink($settings['path']); + } + else { + $boards = explode(' ', $settings['boards']); - $boards = explode(' ', $settings['boards']); - - $threads = array(); + $threads = array(); - foreach ($boards as $board) { - $query = query(sprintf("SELECT `id`, `id` AS `thread_id`, `slug`, (SELECT `time` FROM ``posts_%s`` WHERE `thread` = `thread_id` OR `id` = `thread_id` ORDER BY `time` DESC LIMIT 1) AS `lastmod` FROM ``posts_%s`` WHERE `thread` IS NULL", $board, $board)) or error(db_error()); - $threads[$board] = $query->fetchAll(PDO::FETCH_ASSOC); - } + foreach ($boards as $board) { + $query = query(sprintf("SELECT `id`, `id` AS `thread_id`, `slug`, (SELECT `time` FROM ``posts_%s`` WHERE `thread` = `thread_id` OR `id` = `thread_id` ORDER BY `time` DESC LIMIT 1) AS `lastmod` FROM ``posts_%s`` WHERE `thread` IS NULL", $board, $board)) or error(db_error()); + $threads[$board] = $query->fetchAll(PDO::FETCH_ASSOC); + } - file_write($settings['path'], Element('themes/sitemap/sitemap.xml', Array( - 'settings' => $settings, - 'config' => $config, - 'threads' => $threads, - 'boards' => $boards, - ))); + file_write($settings['path'], Element('themes/sitemap/sitemap.xml', Array( + 'settings' => $settings, + 'config' => $config, + 'threads' => $threads, + 'boards' => $boards, + ))); + } } -- cgit v1.3 From 64a0b486f2dd49a749104eb200127208f7b592df Mon Sep 17 00:00:00 2001 From: czaks Date: Mon, 6 Apr 2015 18:42:23 +0200 Subject: smart_build.php: nginx – cache 404 replies for 120s MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- smart_build.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'smart_build.php') diff --git a/smart_build.php b/smart_build.php index 43734e4f..993d7858 100644 --- a/smart_build.php +++ b/smart_build.php @@ -169,7 +169,7 @@ function die_404() { global $config; else { header("Location: ".$config['page_404']); } - + header("X-Accel-Expires: 120"); die(); } -- cgit v1.3 From 00ad1ecf1560c663e56b53e5686d13d021c195ba Mon Sep 17 00:00:00 2001 From: czaks Date: Sat, 11 Apr 2015 21:41:22 +0200 Subject: smart_build: fix one possible performance problem --- smart_build.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'smart_build.php') diff --git a/smart_build.php b/smart_build.php index 993d7858..31d8110a 100644 --- a/smart_build.php +++ b/smart_build.php @@ -80,7 +80,7 @@ function sb_thread_slugcheck50($b, $thread) { return sb_thread($b, $thread, 50); } -function sb_api($b) { global $config; +function sb_api($b) { global $config, $build_pages; if (!openBoard($b)) return false; $config['try_smarter'] = true; $build_pages = array(-1); -- cgit v1.3