FIX: permalink normalization not applied at constraint

implement permalink import for lithium
This commit is contained in:
Sam 2015-07-22 13:40:45 +10:00
parent 90b85e5b23
commit 3a54923116
4 changed files with 41 additions and 22 deletions

View file

@ -2,7 +2,7 @@ class PermalinksController < ApplicationController
skip_before_filter :check_xhr, :preload_json skip_before_filter :check_xhr, :preload_json
def show def show
url = request.fullpath[1..-1] url = request.fullpath
permalink = Permalink.find_by_url(url) permalink = Permalink.find_by_url(url)

View file

@ -1,7 +1,7 @@
class PermalinkConstraint class PermalinkConstraint
def matches?(request) def matches?(request)
Permalink.where(url: request.fullpath[1..-1]).exists? Permalink.where(url: Permalink.normalize_url(request.fullpath)).exists?
end end
end end

View file

@ -53,6 +53,7 @@ class ImportScripts::Lithium < ImportScripts::Base
def execute def execute
SiteSetting.allow_html_tables = true
# import_users # import_users
# import_categories # import_categories
# import_topics # import_topics
@ -486,6 +487,38 @@ class ImportScripts::Lithium < ImportScripts::Base
puts "done importing accepted answers" puts "done importing accepted answers"
end 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 # find the uploaded file information from the db
def find_upload(post, attachment_id) def find_upload(post, attachment_id)
sql = "SELECT a.attachmentid attachment_id, a.userid user_id, a.filedataid file_id, a.filename filename, 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
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 def post_process_posts
puts "", "Postprocessing posts..." puts "", "Postprocessing posts..."

View file

@ -19,6 +19,12 @@ describe PermalinksController do
expect(response).to redirect_to('/topic/100') expect(response).to redirect_to('/topic/100')
expect(response.status).to eq(301) expect(response.status).to eq(301)
SiteSetting.permalink_normalizations = "/(.*)\\?.*/\\1X"
get :show, url: permalink.url, test: "hello"
expect(response.status).to eq(404)
end end
it 'return 404 if permalink record does not exist' do it 'return 404 if permalink record does not exist' do