From 3a54923116d899fbbd4b37560aeb2c6dddd22b9b Mon Sep 17 00:00:00 2001 From: Sam Date: Wed, 22 Jul 2015 13:40:45 +1000 Subject: [PATCH] FIX: permalink normalization not applied at constraint implement permalink import for lithium --- app/controllers/permalinks_controller.rb | 2 +- lib/permalink_constraint.rb | 2 +- script/import_scripts/lithium.rb | 53 ++++++++++++------- .../controllers/permalinks_controller_spec.rb | 6 +++ 4 files changed, 41 insertions(+), 22 deletions(-) diff --git a/app/controllers/permalinks_controller.rb b/app/controllers/permalinks_controller.rb index 4211c85fb..f78256da0 100644 --- a/app/controllers/permalinks_controller.rb +++ b/app/controllers/permalinks_controller.rb @@ -2,7 +2,7 @@ class PermalinksController < ApplicationController skip_before_filter :check_xhr, :preload_json def show - url = request.fullpath[1..-1] + url = request.fullpath permalink = Permalink.find_by_url(url) diff --git a/lib/permalink_constraint.rb b/lib/permalink_constraint.rb index 0abf395e2..8fc952029 100644 --- a/lib/permalink_constraint.rb +++ b/lib/permalink_constraint.rb @@ -1,7 +1,7 @@ class PermalinkConstraint def matches?(request) - Permalink.where(url: request.fullpath[1..-1]).exists? + Permalink.where(url: Permalink.normalize_url(request.fullpath)).exists? end end diff --git a/script/import_scripts/lithium.rb b/script/import_scripts/lithium.rb index aac628217..6d82aa051 100644 --- a/script/import_scripts/lithium.rb +++ b/script/import_scripts/lithium.rb @@ -53,6 +53,7 @@ class ImportScripts::Lithium < ImportScripts::Base def execute + SiteSetting.allow_html_tables = true # import_users # import_categories # import_topics @@ -486,6 +487,38 @@ class ImportScripts::Lithium < ImportScripts::Base puts "done importing accepted answers" end + + def create_permalinks + puts "Creating permalinks" + + SiteSetting.permalink_normalizations = '/t5\\/.*p\\/(\\d+).*//p/\\1' + + sql = <<-SQL + INSERT INTO permalinks (url, topic_id, created_at, updated_at) + SELECT '/p/' || value, p.topic_id, current_timestamp, current_timestamp + FROM post_custom_fields f + JOIN posts p on f.post_id = p.id AND post_number = 1 + LEFT JOIN permalinks pm ON url = '/p/' || value + WHERE pm.id IS NULL AND f.name = 'import_unique_id' +SQL + + r = Permalink.exec_sql sql + puts "#{r.cmd_tuples} permalinks to topics added!" + + sql = <<-SQL + INSERT INTO permalinks (url, post_id, created_at, updated_at) + SELECT '/p/' || value, p.id, current_timestamp, current_timestamp + FROM post_custom_fields f + JOIN posts p on f.post_id = p.id AND post_number <> 1 + LEFT JOIN permalinks pm ON url = '/p/' || value + WHERE pm.id IS NULL AND f.name = 'import_unique_id' +SQL + + r = Permalink.exec_sql sql + puts "#{r.cmd_tuples} permalinks to posts added!" + + end + # find the uploaded file information from the db def find_upload(post, attachment_id) sql = "SELECT a.attachmentid attachment_id, a.userid user_id, a.filedataid file_id, a.filename filename, @@ -559,26 +592,6 @@ class ImportScripts::Lithium < ImportScripts::Base end end - def close_topics - puts "", "Closing topics..." - - sql = <<-SQL - WITH closed_topic_ids AS ( - SELECT t.id AS topic_id - FROM post_custom_fields pcf - JOIN posts p ON p.id = pcf.post_id - JOIN topics t ON t.id = p.topic_id - WHERE pcf.name = 'import_id' - AND pcf.value IN (?) - ) - UPDATE topics - SET closed = true - WHERE id IN (SELECT topic_id FROM closed_topic_ids) - SQL - - Topic.exec_sql(sql, @closed_topic_ids) - end - def post_process_posts puts "", "Postprocessing posts..." diff --git a/spec/controllers/permalinks_controller_spec.rb b/spec/controllers/permalinks_controller_spec.rb index 02d175461..d90b3de69 100644 --- a/spec/controllers/permalinks_controller_spec.rb +++ b/spec/controllers/permalinks_controller_spec.rb @@ -19,6 +19,12 @@ describe PermalinksController do expect(response).to redirect_to('/topic/100') expect(response.status).to eq(301) + + SiteSetting.permalink_normalizations = "/(.*)\\?.*/\\1X" + + get :show, url: permalink.url, test: "hello" + + expect(response.status).to eq(404) end it 'return 404 if permalink record does not exist' do