2019-05-02 17:17:27 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2014-08-28 14:09:36 -05:00
|
|
|
class Permalink < ActiveRecord::Base
|
|
|
|
belongs_to :topic
|
|
|
|
belongs_to :post
|
|
|
|
belongs_to :category
|
2020-05-25 04:48:54 -05:00
|
|
|
belongs_to :tag
|
2024-02-05 10:31:31 -06:00
|
|
|
belongs_to :user
|
2014-08-28 14:09:36 -05:00
|
|
|
|
|
|
|
before_validation :normalize_url
|
|
|
|
|
2021-03-29 13:36:59 -05:00
|
|
|
validates :url, uniqueness: true
|
|
|
|
|
2015-07-15 00:32:35 -05:00
|
|
|
class Normalizer
|
|
|
|
attr_reader :source
|
|
|
|
|
|
|
|
def initialize(source)
|
|
|
|
@source = source
|
2023-01-09 06:20:10 -06:00
|
|
|
@rules = source.split("|").map { |rule| parse_rule(rule) }.compact if source.present?
|
2015-07-15 00:32:35 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def parse_rule(rule)
|
2023-01-09 06:20:10 -06:00
|
|
|
return unless rule =~ %r{/.*/}
|
2015-07-15 00:32:35 -05:00
|
|
|
|
|
|
|
escaping = false
|
2019-05-02 17:17:27 -05:00
|
|
|
regex = +""
|
|
|
|
sub = +""
|
2015-07-15 00:32:35 -05:00
|
|
|
c = 0
|
|
|
|
|
|
|
|
rule.chars.each do |l|
|
|
|
|
c += 1 if !escaping && l == "/"
|
|
|
|
escaping = l == "\\"
|
|
|
|
|
|
|
|
if c > 1
|
|
|
|
sub << l
|
|
|
|
else
|
|
|
|
regex << l
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-01-09 06:20:10 -06:00
|
|
|
[Regexp.new(regex[1..-1]), sub[1..-1] || ""] if regex.length > 1
|
2015-07-15 00:32:35 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def normalize(url)
|
|
|
|
return url unless @rules
|
2023-01-09 06:20:10 -06:00
|
|
|
@rules.each { |(regex, sub)| url = url.sub(regex, sub) }
|
2015-07-15 00:32:35 -05:00
|
|
|
|
|
|
|
url
|
2014-08-28 14:09:36 -05:00
|
|
|
end
|
2015-07-15 00:32:35 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.normalize_url(url)
|
|
|
|
if url
|
|
|
|
url = url.strip
|
2023-01-09 06:20:10 -06:00
|
|
|
url = url[1..-1] if url[0, 1] == "/"
|
2015-07-15 00:32:35 -05:00
|
|
|
end
|
|
|
|
|
2017-07-27 20:20:09 -05:00
|
|
|
normalizations = SiteSetting.permalink_normalizations
|
2015-07-15 00:32:35 -05:00
|
|
|
|
2023-01-09 06:20:10 -06:00
|
|
|
@normalizer = Normalizer.new(normalizations) unless @normalizer &&
|
|
|
|
@normalizer.source == normalizations
|
2015-07-15 00:32:35 -05:00
|
|
|
@normalizer.normalize(url)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.find_by_url(url)
|
|
|
|
find_by(url: normalize_url(url))
|
|
|
|
end
|
|
|
|
|
|
|
|
def normalize_url
|
|
|
|
self.url = Permalink.normalize_url(url) if url
|
2014-08-28 14:09:36 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def target_url
|
2014-10-08 10:50:44 -05:00
|
|
|
return external_url if external_url
|
2020-10-09 06:51:24 -05:00
|
|
|
return "#{Discourse.base_path}#{post.url}" if post
|
2014-08-28 14:09:36 -05:00
|
|
|
return topic.relative_url if topic
|
2020-06-18 03:32:14 -05:00
|
|
|
return category.url if category
|
2020-05-25 04:48:54 -05:00
|
|
|
return tag.full_url if tag
|
2024-02-05 10:31:31 -06:00
|
|
|
return user.full_url if user
|
2014-08-28 14:09:36 -05:00
|
|
|
nil
|
|
|
|
end
|
2015-07-17 05:23:48 -05:00
|
|
|
|
2017-07-27 20:20:09 -05:00
|
|
|
def self.filter_by(url = nil)
|
2023-01-09 06:20:10 -06:00
|
|
|
permalinks =
|
2024-02-05 10:31:31 -06:00
|
|
|
Permalink.includes(:topic, :post, :category, :tag, :user).order("permalinks.created_at desc")
|
2015-07-17 05:23:48 -05:00
|
|
|
|
2023-01-09 06:20:10 -06:00
|
|
|
permalinks.where!("url ILIKE :url OR external_url ILIKE :url", url: "%#{url}%") if url.present?
|
2015-07-17 05:23:48 -05:00
|
|
|
permalinks.limit!(100)
|
|
|
|
permalinks.to_a
|
|
|
|
end
|
2014-08-28 14:09:36 -05:00
|
|
|
end
|
2014-10-08 10:50:44 -05:00
|
|
|
|
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: permalinks
|
|
|
|
#
|
|
|
|
# id :integer not null, primary key
|
|
|
|
# url :string(1000) not null
|
|
|
|
# topic_id :integer
|
|
|
|
# post_id :integer
|
|
|
|
# category_id :integer
|
2019-01-11 13:29:56 -06:00
|
|
|
# created_at :datetime not null
|
|
|
|
# updated_at :datetime not null
|
2014-10-08 10:50:44 -05:00
|
|
|
# external_url :string(1000)
|
2020-05-25 04:48:54 -05:00
|
|
|
# tag_id :integer
|
2024-02-05 10:31:31 -06:00
|
|
|
# user_id :integer
|
2014-10-08 10:50:44 -05:00
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
|
|
|
# index_permalinks_on_url (url) UNIQUE
|
|
|
|
#
|