From bdb6001f3f7e93e0b9655c0491bbef6f1df8a5fa Mon Sep 17 00:00:00 2001 From: czaks Date: Tue, 10 Mar 2015 12:48:59 +0100 Subject: [PATCH 01/16] support for slugified links; may introduce a few bugs --- inc/config.php | 7 +- inc/display.php | 4 +- inc/functions.php | 92 +++++++++++++++++++++++-- inc/lib/Twig/Extensions/Extension/Tinyboard.php | 3 +- inc/mod/pages.php | 18 ++--- install.php | 6 +- mod.php | 5 ++ post.php | 19 +++-- templates/main.js | 1 + templates/mod/debug/recent_posts.html | 2 +- templates/mod/search_results.html | 4 +- templates/post_reply.html | 4 +- templates/post_thread.html | 10 +-- templates/posts.sql | 1 + templates/themes/catalog/theme.php | 2 +- templates/themes/recent/theme.php | 4 +- templates/themes/sitemap/sitemap.xml | 4 +- templates/themes/sitemap/theme.php | 2 +- 18 files changed, 145 insertions(+), 43 deletions(-) diff --git a/inc/config.php b/inc/config.php index edbe7b2a..ac4acaa4 100644 --- a/inc/config.php +++ b/inc/config.php @@ -566,6 +566,9 @@ // Allow dice rolling: an email field of the form "dice XdY+/-Z" will result in X Y-sided dice rolled and summed, // with the modifier Z added, with the result displayed at the top of the post body. $config['allow_roll'] = false; + + // Use semantic URLs for threads, like /b/res/12345/daily-programming-thread.html + $config['slugify'] = false; /* * ==================== @@ -1118,8 +1121,10 @@ // Location of files. $config['file_index'] = 'index.html'; - $config['file_page'] = '%d.html'; + $config['file_page'] = '%d.html'; // NB: page is both an index page and a thread $config['file_page50'] = '%d+50.html'; + $config['file_page_slug'] = '%d-%s.html'; + $config['file_page50_slug'] = '%d-%s+50.html'; $config['file_mod'] = 'mod.php'; $config['file_post'] = 'post.php'; $config['file_script'] = 'main.js'; diff --git a/inc/display.php b/inc/display.php index 1a78e19b..65525ab9 100644 --- a/inc/display.php +++ b/inc/display.php @@ -383,7 +383,7 @@ class Post { public function link($pre = '', $page = false) { global $config, $board; - return $this->root . $board['dir'] . $config['dir']['res'] . sprintf(($page ? $page : $config['file_page']), $this->thread) . '#' . $pre . $this->id; + return $this->root . $board['dir'] . $config['dir']['res'] . link_for((array)$this, $page == '50') . '#' . $pre . $this->id; } public function build($index=false) { @@ -438,7 +438,7 @@ class Thread { public function link($pre = '', $page = false) { global $config, $board; - return $this->root . $board['dir'] . $config['dir']['res'] . sprintf(($page ? $page : $config['file_page']), $this->id) . '#' . $pre . $this->id; + return $this->root . $board['dir'] . $config['dir']['res'] . link_for((array)$this, $page == '50') . '#' . $pre . $this->id; } public function add(Post $post) { $this->posts[] = $post; diff --git a/inc/functions.php b/inc/functions.php index 25362a62..79d06f5c 100755 --- a/inc/functions.php +++ b/inc/functions.php @@ -150,7 +150,9 @@ function loadConfig() { preg_quote($config['dir']['res'], '/') . '(' . str_replace('%d', '\d+', preg_quote($config['file_page'], '/')) . '|' . - str_replace('%d', '\d+', preg_quote($config['file_page50'], '/')) . + str_replace('%d', '\d+', preg_quote($config['file_page50'], '/')) . '|' . + str_replace(array('%d', '%s'), array('\d+', '[a-z0-9-]+'), preg_quote($config['file_page_slug'], '/')) . '|' . + str_replace(array('%d', '%s'), array('\d+', '[a-z0-9-]+'), preg_quote($config['file_page50_slug'], '/')) . ')' . '|' . preg_quote($config['file_mod'], '/') . '\?\/.+' . @@ -912,7 +914,7 @@ function insertFloodPost(array $post) { function post(array $post) { global $pdo, $board; - $query = prepare(sprintf("INSERT INTO ``posts_%s`` VALUES ( NULL, :thread, :subject, :email, :name, :trip, :capcode, :body, :body_nomarkup, :time, :time, :files, :num_files, :filehash, :password, :ip, :sticky, :locked, 0, :embed)", $board['uri'])); + $query = prepare(sprintf("INSERT INTO ``posts_%s`` VALUES ( NULL, :thread, :subject, :email, :name, :trip, :capcode, :body, :body_nomarkup, :time, :time, :files, :num_files, :filehash, :password, :ip, :sticky, :locked, 0, :embed, :slug)", $board['uri'])); // Basic stuff if (!empty($post['subject'])) { @@ -981,6 +983,10 @@ function post(array $post) { $query->bindValue(':filehash', null, PDO::PARAM_NULL); } + if ($post['op']) { + $query->bindValue(':slug', slugify($post)); + } + if (!$query->execute()) { undoImage($post); error(db_error($query)); @@ -1094,8 +1100,8 @@ function deletePost($id, $error_if_doesnt_exist=true, $rebuild_after=true) { if (!$post['thread']) { // Delete thread HTML page - file_unlink($board['dir'] . $config['dir']['res'] . sprintf($config['file_page'], $post['id'])); - file_unlink($board['dir'] . $config['dir']['res'] . sprintf($config['file_page50'], $post['id'])); + file_unlink($board['dir'] . $config['dir']['res'] . link_for($post) ); + file_unlink($board['dir'] . $config['dir']['res'] . link_for($post, true) ); // noko50 file_unlink($board['dir'] . $config['dir']['res'] . sprintf('%d.json', $post['id'])); $antispam_query = prepare('DELETE FROM ``antispam`` WHERE `board` = :board AND `thread` = :thread'); @@ -2067,12 +2073,12 @@ function buildThread($id, $return = false, $mod = false) { if ($return) { return $body; } else { - $noko50fn = $board['dir'] . $config['dir']['res'] . sprintf($config['file_page50'], $id); + $noko50fn = $board['dir'] . $config['dir']['res'] . link_for($thread, true); if ($hasnoko50 || file_exists($noko50fn)) { buildThread50($id, $return, $mod, $thread, $antibot); } - file_write($board['dir'] . $config['dir']['res'] . sprintf($config['file_page'], $id), $body); + file_write($board['dir'] . $config['dir']['res'] . link_for($thread), $body); } } @@ -2152,7 +2158,7 @@ function buildThread50($id, $return = false, $mod = false, $thread = null, $anti if ($return) { return $body; } else { - file_write($board['dir'] . $config['dir']['res'] . sprintf($config['file_page50'], $id), $body); + file_write($board['dir'] . $config['dir']['res'] . link_for($thread, true), $body); } } @@ -2421,3 +2427,75 @@ function diceRoller($post) { } } } + +function slugify($post) { + $slug = ""; + + if (isset($post['thread']) && $post['thread']) + $slug = $post['thread']; + elseif (isset ($post['body_nomarkup']) && $post['body_nomarkup']) + $slug = $post['body_nomarkup']; + elseif (isset ($post['body']) && $post['body']) + $slug = strip_html($post['body']); + + // Fix UTF-8 first + $slug = mb_convert_encoding($slug, "UTF-8", "UTF-8"); + + // Transliterate local characters like ΓΌ, I wonder how would it work for weird alphabets :^) + $slug = iconv("UTF-8", "ASCII//TRANSLIT//IGNORE", $slug); + + // Downcase everything + $slug = strtolower($slug); + + // Strip bad characters, alphanumerics should suffice + $slug = preg_replace('/[^a-zA-Z0-9]/', '-', $slug); + + // Replace multiple dashes with single ones + $slug = preg_replace('/-+/', '-', $slug); + + // Strip dashes at the beginning and at the end + $slug = preg_replace('/^-|-$/', '', $slug); + + // Slug should be 200 characters long, at max + $slug = substr($slug, 0, 200); + + // Slug is now ready + return $slug; +} + +function link_for($post, $page50 = false, $foreignlink = false, $thread = false) { + global $config, $board; + + $post = (array)$post; + + // Where do we need to look for OP? + $b = $foreignlink ? $foreignlink : $board; + + $id = (isset($post['thread']) && $post['thread']) ? $post['thread'] : $post['id']; + + $slug = false; + + if ($config['slugify'] && isset($post['thread']) && $post['thread']) { + if (!$thread) { + // Oh fuck, we'd better optimize it ASAP + + $query = prepare(sprintf("SELECT `slug` FROM ``posts_%s`` WHERE `id` = :id", $board['uri'])); + $query->bindValue(':id', $id, PDO::PARAM_INT); + $query->execute() or error(db_error($query)); + + $thread = $query->fetch(PDO::FETCH_ASSOC); + } + $slug = $thread['slug']; + } + elseif ($config['slugify']) { + $slug = $post['slug']; + } + + + if ( $page50 && $slug) $tpl = $config['file_page50_slug']; + else if (!$page50 && $slug) $tpl = $config['file_page_slug']; + else if ( $page50 && !$slug) $tpl = $config['file_page50']; + else if (!$page50 && !$slug) $tpl = $config['file_page']; + + return sprintf($tpl, $id, $slug); +} diff --git a/inc/lib/Twig/Extensions/Extension/Tinyboard.php b/inc/lib/Twig/Extensions/Extension/Tinyboard.php index 81276147..028db438 100644 --- a/inc/lib/Twig/Extensions/Extension/Tinyboard.php +++ b/inc/lib/Twig/Extensions/Extension/Tinyboard.php @@ -45,7 +45,8 @@ class Twig_Extensions_Extension_Tinyboard extends Twig_Extension new Twig_SimpleFunction('hiddenInputsHash', 'hiddenInputsHash'), new Twig_SimpleFunction('ratio', 'twig_ratio_function'), new Twig_SimpleFunction('secure_link_confirm', 'twig_secure_link_confirm'), - new Twig_SimpleFunction('secure_link', 'twig_secure_link') + new Twig_SimpleFunction('secure_link', 'twig_secure_link'), + new Twig_SimpleFunction('link_for', 'link_for') ); } diff --git a/inc/mod/pages.php b/inc/mod/pages.php index c7722763..79d1bf51 100644 --- a/inc/mod/pages.php +++ b/inc/mod/pages.php @@ -1161,7 +1161,7 @@ function mod_move_reply($originBoard, $postID) { $post = $query->fetch(PDO::FETCH_ASSOC); // redirect - header('Location: ?/' . sprintf($config['board_path'], $board['uri']) . $config['dir']['res'] . sprintf($config['file_page'], $post['thread'] ? $post['thread'] : $newID) . '#' . $newID, true, $config['redirect_http']); + header('Location: ?/' . sprintf($config['board_path'], $board['uri']) . $config['dir']['res'] . link_for($post) . '#' . $newID, true, $config['redirect_http']); } else { @@ -1322,6 +1322,8 @@ function mod_move($originBoard, $postID) { // trigger themes rebuildThemes('post', $targetBoard); + $newboard = $board; + // return to original board openBoard($originBoard); @@ -1332,7 +1334,7 @@ function mod_move($originBoard, $postID) { $query->execute() or error(db_error($query)); // leave a reply, linking to the new thread - $post = array( + $spost = array( 'mod' => true, 'subject' => '', 'email' => '', @@ -1346,23 +1348,23 @@ function mod_move($originBoard, $postID) { 'op' => false ); - $post['body'] = $post['body_nomarkup'] = sprintf($config['mod']['shadow_mesage'], '>>>/' . $targetBoard . '/' . $newID); + $spost['body'] = $spost['body_nomarkup'] = sprintf($config['mod']['shadow_mesage'], '>>>/' . $targetBoard . '/' . $newID); - markup($post['body']); + markup($spost['body']); - $botID = post($post); + $botID = post($spost); buildThread($postID); buildIndex(); - header('Location: ?/' . sprintf($config['board_path'], $originBoard) . $config['dir']['res'] .sprintf($config['file_page'], $postID) . + header('Location: ?/' . sprintf($config['board_path'], $originBoard) . $config['dir']['res'] . link_for($post, false, $newboard) . '#' . $botID, true, $config['redirect_http']); } else { deletePost($postID); buildIndex(); openBoard($targetBoard); - header('Location: ?/' . sprintf($config['board_path'], $board['uri']) . $config['dir']['res'] . sprintf($config['file_page'], $newID), true, $config['redirect_http']); + header('Location: ?/' . sprintf($config['board_path'], $board['uri']) . $config['dir']['res'] . link_for($post, false, $newboard), true, $config['redirect_http']); } } @@ -1494,7 +1496,7 @@ function mod_edit_post($board, $edit_raw_html, $postID) { rebuildThemes('post', $board); - header('Location: ?/' . sprintf($config['board_path'], $board) . $config['dir']['res'] . sprintf($config['file_page'], $post['thread'] ? $post['thread'] : $postID) . '#' . $postID, true, $config['redirect_http']); + header('Location: ?/' . sprintf($config['board_path'], $board) . $config['dir']['res'] . link_for($post) . '#' . $postID, true, $config['redirect_http']); } else { if ($config['minify_html']) { $post['body_nomarkup'] = str_replace("\n", ' ', utf8tohtml($post['body_nomarkup'])); diff --git a/install.php b/install.php index 2e050eee..95caf8ec 100644 --- a/install.php +++ b/install.php @@ -1,7 +1,7 @@ vichan upgrade path. query("CREATE TABLE IF NOT EXISTS ``search_queries`` ( `ip` varchar(39) NOT NULL, `time` int(11) NOT NULL, `query` text NOT NULL) ENGINE=MyISAM DEFAULT CHARSET=utf8;") or error(db_error()); diff --git a/mod.php b/mod.php index 384eec88..e06d5db6 100644 --- a/mod.php +++ b/mod.php @@ -105,6 +105,11 @@ $pages = array( str_replace('%d', '(\d+)', preg_quote($config['file_page50'], '!')) => 'view_thread50', '/(\%b)/' . preg_quote($config['dir']['res'], '!') . str_replace('%d', '(\d+)', preg_quote($config['file_page'], '!')) => 'view_thread', + + '/(\%b)/' . preg_quote($config['dir']['res'], '!') . + str_replace(array('%d','%s'), array('(\d+)', '[a-z0-9-]+'), preg_quote($config['file_page50_slug'], '!')) => 'view_thread50', + '/(\%b)/' . preg_quote($config['dir']['res'], '!') . + str_replace(array('%d','%s'), array('(\d+)', '[a-z0-9-]+'), preg_quote($config['file_page_slug'], '!')) => 'view_thread', ); diff --git a/post.php b/post.php index d33c8150..93592590 100644 --- a/post.php +++ b/post.php @@ -85,7 +85,7 @@ if (isset($_POST['delete'])) { } _syslog(LOG_INFO, 'Deleted post: ' . - '/' . $board['dir'] . $config['dir']['res'] . sprintf($config['file_page'], $post['thread'] ? $post['thread'] : $id) . ($post['thread'] ? '#' . $id : '') + '/' . $board['dir'] . $config['dir']['res'] . link_for($post) . ($post['thread'] ? '#' . $id : '') ); } } @@ -142,7 +142,7 @@ if (isset($_POST['delete'])) { if ($config['syslog']) _syslog(LOG_INFO, 'Reported post: ' . - '/' . $board['dir'] . $config['dir']['res'] . sprintf($config['file_page'], $thread ? $thread : $id) . ($thread ? '#' . $id : '') . + '/' . $board['dir'] . $config['dir']['res'] . link_for($post) . ($thread ? '#' . $id : '') . ' for "' . $reason . '"' ); $query = prepare("INSERT INTO ``reports`` VALUES (NULL, :time, :ip, :board, :post, :reason)"); @@ -250,7 +250,7 @@ if (isset($_POST['delete'])) { //Check if thread exists if (!$post['op']) { - $query = prepare(sprintf("SELECT `sticky`,`locked`,`sage` FROM ``posts_%s`` WHERE `id` = :id AND `thread` IS NULL LIMIT 1", $board['uri'])); + $query = prepare(sprintf("SELECT `sticky`,`locked`,`sage`,`slug` FROM ``posts_%s`` WHERE `id` = :id AND `thread` IS NULL LIMIT 1", $board['uri'])); $query->bindValue(':id', $post['thread'], PDO::PARAM_INT); $query->execute() or error(db_error()); @@ -259,6 +259,9 @@ if (isset($_POST['delete'])) { error($config['error']['nonexistant']); } } + else { + $thread = false; + } // Check for an embed field @@ -782,6 +785,7 @@ if (isset($_POST['delete'])) { $post = (object)$post; $post->files = array_map(function($a) { return (object)$a; }, $post->files); + $error = event('post', $post); $post->files = array_map(function($a) { return (array)$a; }, $post->files); @@ -845,19 +849,20 @@ if (isset($_POST['delete'])) { if ($noko) { $redirect = $root . $board['dir'] . $config['dir']['res'] . - sprintf($config['file_page'], $post['op'] ? $id:$post['thread']) . (!$post['op'] ? '#' . $id : ''); + link_for($post, false, false, $thread) . (!$post['op'] ? '#' . $id : ''); if (!$post['op'] && isset($_SERVER['HTTP_REFERER'])) { $regex = array( 'board' => str_replace('%s', '(\w{1,8})', preg_quote($config['board_path'], '/')), 'page' => str_replace('%d', '(\d+)', preg_quote($config['file_page'], '/')), - 'page50' => str_replace('%d', '(\d+)', preg_quote($config['file_page50'], '/')), + 'page50' => '(' . str_replace('%d', '(\d+)', preg_quote($config['file_page50'], '/')) . '|' . + str_replace(array('%d', '%s'), array('(\d+)', '[a-z0-9-]+'), preg_quote($config['file_page50_slug'], '/')) . ')', 'res' => preg_quote($config['dir']['res'], '/'), ); if (preg_match('/\/' . $regex['board'] . $regex['res'] . $regex['page50'] . '([?&].*)?$/', $_SERVER['HTTP_REFERER'])) { $redirect = $root . $board['dir'] . $config['dir']['res'] . - sprintf($config['file_page50'], $post['op'] ? $id:$post['thread']) . (!$post['op'] ? '#' . $id : ''); + link_for($post, true, false, $thread) . (!$post['op'] ? '#' . $id : ''); } } } else { @@ -867,7 +872,7 @@ if (isset($_POST['delete'])) { if ($config['syslog']) _syslog(LOG_INFO, 'New post: /' . $board['dir'] . $config['dir']['res'] . - sprintf($config['file_page'], $post['op'] ? $id : $post['thread']) . (!$post['op'] ? '#' . $id : '')); + link_for($post) . (!$post['op'] ? '#' . $id : '')); if (!$post['mod']) header('X-Associated-Content: "' . $redirect . '"'); diff --git a/templates/main.js b/templates/main.js index ead852fe..e948fd82 100644 --- a/templates/main.js +++ b/templates/main.js @@ -146,6 +146,7 @@ function changeStyle(styleName, link) { {% endraw %} {% if config.stylesheets_board %} {# This is such an unacceptable mess. There needs to be an easier way. #} + {# Needs fix for slugify #} var matches = document.URL.match(/\/(\w+)\/($|{{ config.dir.res|replace({'/': '\\/'}) }}{{ config.file_page|replace({'%d': '\\d+', '.': '\\.'}) }}|{{ config.file_index|replace({'.': '\\.'}) }}|{{ config.file_page|replace({'%d': '\\d+', '.': '\\.'}) }})/); {% raw %} if (matches) { diff --git a/templates/mod/debug/recent_posts.html b/templates/mod/debug/recent_posts.html index e4eb4f3a..ce4cffc4 100644 --- a/templates/mod/debug/recent_posts.html +++ b/templates/mod/debug/recent_posts.html @@ -63,7 +63,7 @@ {% else %} {% set thread = post.id %} {% endif %} - + {{ post.id }} diff --git a/templates/mod/search_results.html b/templates/mod/search_results.html index 0d3c2a10..186ee5db 100644 --- a/templates/mod/search_results.html +++ b/templates/mod/search_results.html @@ -195,7 +195,7 @@ {% else %} {% set thread = post.id %} {% endif %} - + {{ post.id }} @@ -265,4 +265,4 @@ [{{ i + 1 }}] {% endfor %}

-{% endif %} \ No newline at end of file +{% endif %} diff --git a/templates/post_reply.html b/templates/post_reply.html index e7343230..451ff1b9 100644 --- a/templates/post_reply.html +++ b/templates/post_reply.html @@ -12,8 +12,8 @@ {% include 'post/time.html' %} {% include 'post/poster_id.html' %}  - No. - {{ post.id }} + No. + {{ post.id }}

{% include 'post/fileinfo.html' %} {% include 'post/post_controls.html' %} diff --git a/templates/post_thread.html b/templates/post_thread.html index 08b4c4c1..61146491 100644 --- a/templates/post_thread.html +++ b/templates/post_thread.html @@ -15,8 +15,8 @@ {% include 'post/time.html' %} {% include 'post/poster_id.html' %}  - No. - {{ post.id }} + No. + {{ post.id }} {% if post.sticky %} {% if config.font_awesome %} @@ -39,14 +39,14 @@ {% endif %} {% endif %} {% if index %} - [{% trans %}Reply{% endtrans %}] + [{% trans %}Reply{% endtrans %}] {% endif %} {% if isnoko50 %} - [{% trans %}View All{% endtrans %}] + [{% trans %}View All{% endtrans %}] {% endif %} {% if hasnoko50 and not isnoko50 %} {% set lastcount = config.noko50_count %} - [{% trans %}Last 1 Post{% plural lastcount %}Last {{ count }} Posts{% endtrans %}] + [{% trans %}Last 1 Post{% plural lastcount %}Last {{ count }} Posts{% endtrans %}] {% endif %} {% include 'post/post_controls.html' %}

diff --git a/templates/posts.sql b/templates/posts.sql index 9a47c6ad..6b2249ef 100644 --- a/templates/posts.sql +++ b/templates/posts.sql @@ -19,6 +19,7 @@ CREATE TABLE IF NOT EXISTS ``posts_{{ board }}`` ( `locked` int(1) NOT NULL, `sage` int(1) NOT NULL, `embed` text, + `slug` varchar(256) DEFAULT NULL, UNIQUE KEY `id` (`id`), KEY `thread_id` (`thread`,`id`), KEY `filehash` (`filehash`(40)), diff --git a/templates/themes/catalog/theme.php b/templates/themes/catalog/theme.php index 9ba2d27a..b3b7caf7 100644 --- a/templates/themes/catalog/theme.php +++ b/templates/themes/catalog/theme.php @@ -42,7 +42,7 @@ $board_name, $board_name, $board_name, $board_name, $board_name)) or error(db_error()); while ($post = $query->fetch(PDO::FETCH_ASSOC)) { - $post['link'] = $config['root'] . $board['dir'] . $config['dir']['res'] . sprintf($config['file_page'], ($post['thread'] ? $post['thread'] : $post['id'])); + $post['link'] = $config['root'] . $board['dir'] . $config['dir']['res'] . link_for($post)); $post['board_name'] = $board['name']; if ($post['embed'] && preg_match('/^https?:\/\/(\w+\.)?(?:youtube\.com\/watch\?v=|youtu\.be\/)([a-zA-Z0-9\-_]{10,11})(&.+)?$/i', $post['embed'], $matches)) { diff --git a/templates/themes/recent/theme.php b/templates/themes/recent/theme.php index b1153477..2ea6a7b9 100644 --- a/templates/themes/recent/theme.php +++ b/templates/themes/recent/theme.php @@ -62,7 +62,7 @@ // board settings won't be available in the template file, so generate links now $post['link'] = $config['root'] . $board['dir'] . $config['dir']['res'] - . sprintf($config['file_page'], ($post['thread'] ? $post['thread'] : $post['id'])) . '#' . $post['id']; + . link_for($post) . '#' . $post['id']; if ($files) { if ($files[0]->thumb == 'spoiler') { @@ -92,7 +92,7 @@ while ($post = $query->fetch(PDO::FETCH_ASSOC)) { openBoard($post['board']); - $post['link'] = $config['root'] . $board['dir'] . $config['dir']['res'] . sprintf($config['file_page'], ($post['thread'] ? $post['thread'] : $post['id'])) . '#' . $post['id']; + $post['link'] = $config['root'] . $board['dir'] . $config['dir']['res'] . link_for($post) . '#' . $post['id']; if ($post['body'] != "") $post['snippet'] = pm_snippet($post['body'], 30); else diff --git a/templates/themes/sitemap/sitemap.xml b/templates/themes/sitemap/sitemap.xml index 733da607..95dfdd26 100644 --- a/templates/themes/sitemap/sitemap.xml +++ b/templates/themes/sitemap/sitemap.xml @@ -9,11 +9,11 @@ {% for board, thread_list in threads %} {% for thread in thread_list %} - {{ settings.url ~ (config.board_path | format(board)) ~ config.dir.res ~ (config.file_page | format(thread.thread_id)) }} + {{ settings.url ~ (config.board_path | format(board)) ~ config.dir.res ~ link_for(thread) }} {{ thread.lastmod | date('%Y-%m-%dT%H:%M:%S') }}{{ timezone() }} {{ settings.changefreq }} {% endfor %} {% endfor %} -{% endfilter %} \ No newline at end of file +{% endfilter %} diff --git a/templates/themes/sitemap/theme.php b/templates/themes/sitemap/theme.php index 3e048dd5..1cebcdb6 100644 --- a/templates/themes/sitemap/theme.php +++ b/templates/themes/sitemap/theme.php @@ -26,7 +26,7 @@ $threads = array(); foreach ($boards as $board) { - $query = query(sprintf("SELECT `id` AS `thread_id`, (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()); + $query = query(sprintf("SELECT `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); } From f4bba2e9ed7cbce52c1f33c3f6b5bfac6e8261c3 Mon Sep 17 00:00:00 2001 From: czaks Date: Tue, 10 Mar 2015 12:57:06 +0100 Subject: [PATCH 02/16] ... --- inc/functions.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/inc/functions.php b/inc/functions.php index 79d06f5c..b318ff35 100755 --- a/inc/functions.php +++ b/inc/functions.php @@ -986,6 +986,9 @@ function post(array $post) { if ($post['op']) { $query->bindValue(':slug', slugify($post)); } + else { + $query->bindValue(':slug', NULL); + } if (!$query->execute()) { undoImage($post); From 429c9f890f67246a38e1de29436fd37f525393d8 Mon Sep 17 00:00:00 2001 From: czaks Date: Tue, 10 Mar 2015 13:02:38 +0100 Subject: [PATCH 03/16] ... --- inc/functions.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/inc/functions.php b/inc/functions.php index b318ff35..ef7d3b7d 100755 --- a/inc/functions.php +++ b/inc/functions.php @@ -2472,7 +2472,7 @@ function link_for($post, $page50 = false, $foreignlink = false, $thread = false) $post = (array)$post; // Where do we need to look for OP? - $b = $foreignlink ? $foreignlink : $board; + $b = $foreignlink ? $foreignlink : (isset($post['board']) ? array('uri' => $post['board']) : $board); $id = (isset($post['thread']) && $post['thread']) ? $post['thread'] : $post['id']; From fe7e9c5103d1dc2d0b7c1aad2cdd58a552b218fe Mon Sep 17 00:00:00 2001 From: czaks Date: Tue, 10 Mar 2015 13:03:47 +0100 Subject: [PATCH 04/16] ... --- inc/functions.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/inc/functions.php b/inc/functions.php index ef7d3b7d..df5bc426 100755 --- a/inc/functions.php +++ b/inc/functions.php @@ -2482,7 +2482,7 @@ function link_for($post, $page50 = false, $foreignlink = false, $thread = false) if (!$thread) { // Oh fuck, we'd better optimize it ASAP - $query = prepare(sprintf("SELECT `slug` FROM ``posts_%s`` WHERE `id` = :id", $board['uri'])); + $query = prepare(sprintf("SELECT `slug` FROM ``posts_%s`` WHERE `id` = :id", $b['uri'])); $query->bindValue(':id', $id, PDO::PARAM_INT); $query->execute() or error(db_error($query)); From 0062125f5c724183915ec48a9448684cc1b943db Mon Sep 17 00:00:00 2001 From: czaks Date: Tue, 10 Mar 2015 13:09:53 +0100 Subject: [PATCH 05/16] ... --- inc/functions.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/inc/functions.php b/inc/functions.php index df5bc426..2a398ae7 100755 --- a/inc/functions.php +++ b/inc/functions.php @@ -2434,8 +2434,8 @@ function diceRoller($post) { function slugify($post) { $slug = ""; - if (isset($post['thread']) && $post['thread']) - $slug = $post['thread']; + if (isset($post['subject']) && $post['subject']) + $slug = $post['subject']; elseif (isset ($post['body_nomarkup']) && $post['body_nomarkup']) $slug = $post['body_nomarkup']; elseif (isset ($post['body']) && $post['body']) From d690567b44464962b0f99c9c222ce4530b9b716e Mon Sep 17 00:00:00 2001 From: czaks Date: Tue, 10 Mar 2015 13:13:18 +0100 Subject: [PATCH 06/16] ... (minor fix for locales) --- inc/functions.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/inc/functions.php b/inc/functions.php index 2a398ae7..846dbdaa 100755 --- a/inc/functions.php +++ b/inc/functions.php @@ -358,7 +358,7 @@ function rebuildThemes($action, $boardname = false) { // Reload the locale if ($config['locale'] != $current_locale) { $current_locale = $config['locale']; - init_locale($config['locale'], $error); + init_locale($config['locale']); } if (PHP_SAPI === 'cli') { @@ -379,7 +379,7 @@ function rebuildThemes($action, $boardname = false) { // Reload the locale if ($config['locale'] != $current_locale) { $current_locale = $config['locale']; - init_locale($config['locale'], $error); + init_locale($config['locale']); } } From 7d92a05bd8b283a79764e8ed5435661b9a8b27f2 Mon Sep 17 00:00:00 2001 From: czaks Date: Tue, 10 Mar 2015 13:23:40 +0100 Subject: [PATCH 07/16] ... --- templates/themes/catalog/theme.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/templates/themes/catalog/theme.php b/templates/themes/catalog/theme.php index b3b7caf7..f8f1146e 100644 --- a/templates/themes/catalog/theme.php +++ b/templates/themes/catalog/theme.php @@ -42,7 +42,7 @@ $board_name, $board_name, $board_name, $board_name, $board_name)) or error(db_error()); while ($post = $query->fetch(PDO::FETCH_ASSOC)) { - $post['link'] = $config['root'] . $board['dir'] . $config['dir']['res'] . link_for($post)); + $post['link'] = $config['root'] . $board['dir'] . $config['dir']['res'] . link_for($post); $post['board_name'] = $board['name']; if ($post['embed'] && preg_match('/^https?:\/\/(\w+\.)?(?:youtube\.com\/watch\?v=|youtu\.be\/)([a-zA-Z0-9\-_]{10,11})(&.+)?$/i', $post['embed'], $matches)) { From e999955d08274efa5d78fd9bda410648dfb2ae12 Mon Sep 17 00:00:00 2001 From: czaks Date: Tue, 10 Mar 2015 13:28:55 +0100 Subject: [PATCH 08/16] ... --- post.php | 1 + 1 file changed, 1 insertion(+) diff --git a/post.php b/post.php index 93592590..87795aa0 100644 --- a/post.php +++ b/post.php @@ -800,6 +800,7 @@ if (isset($_POST['delete'])) { $post['num_files'] = sizeof($post['files']); $post['id'] = $id = post($post); + $post['slug'] = slugify($post); insertFloodPost($post); From 4aa1d387f8a90d33207d28afc637958759e2ce6e Mon Sep 17 00:00:00 2001 From: czaks Date: Tue, 10 Mar 2015 13:34:02 +0100 Subject: [PATCH 09/16] ... --- templates/themes/sitemap/theme.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/templates/themes/sitemap/theme.php b/templates/themes/sitemap/theme.php index 1cebcdb6..41a7f304 100644 --- a/templates/themes/sitemap/theme.php +++ b/templates/themes/sitemap/theme.php @@ -26,7 +26,7 @@ $threads = array(); foreach ($boards as $board) { - $query = query(sprintf("SELECT `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()); + $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); } From 7623de9e2fe8424f35be70cddb9cf68254e49e3e Mon Sep 17 00:00:00 2001 From: czaks Date: Tue, 10 Mar 2015 13:42:10 +0100 Subject: [PATCH 10/16] ... (cache) --- inc/functions.php | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/inc/functions.php b/inc/functions.php index 846dbdaa..6880ad44 100755 --- a/inc/functions.php +++ b/inc/functions.php @@ -2479,16 +2479,25 @@ function link_for($post, $page50 = false, $foreignlink = false, $thread = false) $slug = false; if ($config['slugify'] && isset($post['thread']) && $post['thread']) { + $cvar = "slug_".$b['uri']."_".$id; if (!$thread) { - // Oh fuck, we'd better optimize it ASAP + $slug = Cache::get($cvar); - $query = prepare(sprintf("SELECT `slug` FROM ``posts_%s`` WHERE `id` = :id", $b['uri'])); - $query->bindValue(':id', $id, PDO::PARAM_INT); - $query->execute() or error(db_error($query)); + if ($slug === false) { + $query = prepare(sprintf("SELECT `slug` FROM ``posts_%s`` WHERE `id` = :id", $b['uri'])); + $query->bindValue(':id', $id, PDO::PARAM_INT); + $query->execute() or error(db_error($query)); - $thread = $query->fetch(PDO::FETCH_ASSOC); + $thread = $query->fetch(PDO::FETCH_ASSOC); + + $slug = $thread['slug']; + + Cache::set($cvar, $slug); + } + } + else { + $slug = $thread['slug']; } - $slug = $thread['slug']; } elseif ($config['slugify']) { $slug = $post['slug']; From 58b60f0aa4ef14589315a66fcf58d749928483c7 Mon Sep 17 00:00:00 2001 From: czaks Date: Tue, 10 Mar 2015 13:46:34 +0100 Subject: [PATCH 11/16] ... --- inc/mod/pages.php | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/inc/mod/pages.php b/inc/mod/pages.php index 79d1bf51..2b7de134 100644 --- a/inc/mod/pages.php +++ b/inc/mod/pages.php @@ -1224,7 +1224,10 @@ function mod_move($originBoard, $postID) { // create the new thread $newID = post($post); - + + $op = $post; + $op['id'] = $newID; + if ($post['has_file']) { // copy image foreach ($post['files'] as $i => &$file) { @@ -1357,14 +1360,14 @@ function mod_move($originBoard, $postID) { buildIndex(); - header('Location: ?/' . sprintf($config['board_path'], $originBoard) . $config['dir']['res'] . link_for($post, false, $newboard) . + header('Location: ?/' . sprintf($config['board_path'], $originBoard) . $config['dir']['res'] . link_for($op, false, $newboard) . '#' . $botID, true, $config['redirect_http']); } else { deletePost($postID); buildIndex(); openBoard($targetBoard); - header('Location: ?/' . sprintf($config['board_path'], $board['uri']) . $config['dir']['res'] . link_for($post, false, $newboard), true, $config['redirect_http']); + header('Location: ?/' . sprintf($config['board_path'], $board['uri']) . $config['dir']['res'] . link_for($op, false, $newboard), true, $config['redirect_http']); } } From 2f7aeec53128b08e000e89bc031f25c0501565f3 Mon Sep 17 00:00:00 2001 From: czaks Date: Tue, 10 Mar 2015 13:48:33 +0100 Subject: [PATCH 12/16] ... --- inc/mod/pages.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/inc/mod/pages.php b/inc/mod/pages.php index 2b7de134..be713f57 100644 --- a/inc/mod/pages.php +++ b/inc/mod/pages.php @@ -1360,14 +1360,14 @@ function mod_move($originBoard, $postID) { buildIndex(); - header('Location: ?/' . sprintf($config['board_path'], $originBoard) . $config['dir']['res'] . link_for($op, false, $newboard) . + header('Location: ?/' . sprintf($config['board_path'], $newboard['uri']) . $config['dir']['res'] . link_for($op, false, $newboard) . '#' . $botID, true, $config['redirect_http']); } else { deletePost($postID); buildIndex(); openBoard($targetBoard); - header('Location: ?/' . sprintf($config['board_path'], $board['uri']) . $config['dir']['res'] . link_for($op, false, $newboard), true, $config['redirect_http']); + header('Location: ?/' . sprintf($config['board_path'], $newboard['uri']) . $config['dir']['res'] . link_for($op, false, $newboard), true, $config['redirect_http']); } } From 9f34d334d3d45471ab88fc90a702999cd2cf6bf5 Mon Sep 17 00:00:00 2001 From: czaks Date: Tue, 10 Mar 2015 13:52:31 +0100 Subject: [PATCH 13/16] .. --- inc/functions.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/inc/functions.php b/inc/functions.php index 6880ad44..1beb7f6e 100755 --- a/inc/functions.php +++ b/inc/functions.php @@ -1085,7 +1085,7 @@ function deletePost($id, $error_if_doesnt_exist=true, $rebuild_after=true) { global $board, $config; // Select post and replies (if thread) in one query - $query = prepare(sprintf("SELECT `id`,`thread`,`files` FROM ``posts_%s`` WHERE `id` = :id OR `thread` = :id", $board['uri'])); + $query = prepare(sprintf("SELECT `id`,`thread`,`files`,`slug` FROM ``posts_%s`` WHERE `id` = :id OR `thread` = :id", $board['uri'])); $query->bindValue(':id', $id, PDO::PARAM_INT); $query->execute() or error(db_error($query)); From 50b80e9e2452cfffadeb6918150736a4dde7834a Mon Sep 17 00:00:00 2001 From: czaks Date: Tue, 10 Mar 2015 14:06:44 +0100 Subject: [PATCH 14/16] ... (slug api) --- inc/api.php | 1 + 1 file changed, 1 insertion(+) diff --git a/inc/api.php b/inc/api.php index c9608f06..351943a3 100644 --- a/inc/api.php +++ b/inc/api.php @@ -33,6 +33,7 @@ class Api { 'sticky' => 'sticky', 'locked' => 'locked', 'bump' => 'last_modified', + 'slug' => 'semantic_url', ); $this->threadsPageFields = array( From a2544bc596465f9915589a47d112fa1475f54720 Mon Sep 17 00:00:00 2001 From: czaks Date: Tue, 10 Mar 2015 14:16:27 +0100 Subject: [PATCH 15/16] ... (cites) --- inc/functions.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/inc/functions.php b/inc/functions.php index 1beb7f6e..4dc516fc 100755 --- a/inc/functions.php +++ b/inc/functions.php @@ -1810,7 +1810,7 @@ function markup(&$body, $track_cites = false) { if (isset($cited_posts[$cite])) { $replacement = '' . + link_for(array('id' => $cite)) . '#' . $cite . '">' . '>>' . $cite . ''; @@ -1876,12 +1876,12 @@ function markup(&$body, $track_cites = false) { if (!empty($clauses)) { $cited_posts[$_board] = array(); - $query = query(sprintf('SELECT `thread`, `id` FROM ``posts_%s`` WHERE ' . + $query = query(sprintf('SELECT `thread`, `id`, `slug` FROM ``posts_%s`` WHERE ' . implode(' OR ', $clauses), $board['uri'])) or error(db_error()); while ($cite = $query->fetch(PDO::FETCH_ASSOC)) { $cited_posts[$_board][$cite['id']] = $config['root'] . $board['dir'] . $config['dir']['res'] . - ($cite['thread'] ? $cite['thread'] : $cite['id']) . '.html#' . $cite['id']; + link_for($cite) . '#' . $cite['id']; } } @@ -2478,7 +2478,7 @@ function link_for($post, $page50 = false, $foreignlink = false, $thread = false) $slug = false; - if ($config['slugify'] && isset($post['thread']) && $post['thread']) { + if ($config['slugify'] && ( (isset($post['thread']) && $post['thread']) || !isset ($post['slug']) ) ) { $cvar = "slug_".$b['uri']."_".$id; if (!$thread) { $slug = Cache::get($cvar); From 4bf525599e7c596e7aec7f483664a091ef0db57b Mon Sep 17 00:00:00 2001 From: czaks Date: Tue, 10 Mar 2015 14:19:36 +0100 Subject: [PATCH 16/16] ... --- inc/functions.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/inc/functions.php b/inc/functions.php index 4dc516fc..704c6600 100755 --- a/inc/functions.php +++ b/inc/functions.php @@ -1810,7 +1810,7 @@ function markup(&$body, $track_cites = false) { if (isset($cited_posts[$cite])) { $replacement = '' . + link_for(array('id' => $cite, 'thread' => $cited_posts[$cite])) . '#' . $cite . '">' . '>>' . $cite . '';