2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2014-08-28 15:09:36 -04:00
|
|
|
class Permalink < ActiveRecord::Base
|
2024-11-13 18:03:58 -05:00
|
|
|
attr_accessor :permalink_type, :permalink_type_value
|
|
|
|
|
2014-08-28 15:09:36 -04:00
|
|
|
belongs_to :topic
|
|
|
|
belongs_to :post
|
|
|
|
belongs_to :category
|
2020-05-25 05:48:54 -04:00
|
|
|
belongs_to :tag
|
2024-02-05 11:31:31 -05:00
|
|
|
belongs_to :user
|
2014-08-28 15:09:36 -04:00
|
|
|
|
2024-11-13 18:03:58 -05:00
|
|
|
before_validation :clear_associations
|
2024-05-27 16:33:30 -04:00
|
|
|
before_validation :normalize_url, :encode_url
|
2024-11-13 18:03:58 -05:00
|
|
|
before_validation :set_association_value
|
2014-08-28 15:09:36 -04:00
|
|
|
|
2021-03-29 14:36:59 -04:00
|
|
|
validates :url, uniqueness: true
|
|
|
|
|
2024-11-13 18:03:58 -05:00
|
|
|
validates :topic_id, presence: true, if: Proc.new { |permalink| permalink.topic_type? }
|
|
|
|
validates :post_id, presence: true, if: Proc.new { |permalink| permalink.post_type? }
|
|
|
|
validates :category_id, presence: true, if: Proc.new { |permalink| permalink.category_type? }
|
|
|
|
validates :tag_id, presence: true, if: Proc.new { |permalink| permalink.tag_type? }
|
|
|
|
validates :user_id, presence: true, if: Proc.new { |permalink| permalink.user_type? }
|
|
|
|
validates :external_url, presence: true, if: Proc.new { |permalink| permalink.external_url_type? }
|
|
|
|
|
|
|
|
%i[topic post category tag user external_url].each do |association|
|
|
|
|
define_method("#{association}_type?") { self.permalink_type == association.to_s }
|
|
|
|
end
|
|
|
|
|
2015-07-15 01:32:35 -04:00
|
|
|
class Normalizer
|
|
|
|
attr_reader :source
|
|
|
|
|
|
|
|
def initialize(source)
|
|
|
|
@source = source
|
|
|
|
@rules = source.split("|").map { |rule| parse_rule(rule) }.compact if source.present?
|
|
|
|
end
|
|
|
|
|
|
|
|
def parse_rule(rule)
|
|
|
|
return unless rule =~ %r{/.*/}
|
|
|
|
|
|
|
|
escaping = false
|
2019-05-02 18:17:27 -04:00
|
|
|
regex = +""
|
|
|
|
sub = +""
|
2015-07-15 01:32:35 -04: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
|
|
|
|
|
2018-03-28 04:20:08 -04:00
|
|
|
[Regexp.new(regex[1..-1]), sub[1..-1] || ""] if regex.length > 1
|
2015-07-15 01:32:35 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def normalize(url)
|
|
|
|
return url unless @rules
|
|
|
|
@rules.each { |(regex, sub)| url = url.sub(regex, sub) }
|
|
|
|
|
|
|
|
url
|
2014-08-28 15:09:36 -04:00
|
|
|
end
|
2015-07-15 01:32:35 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.normalize_url(url)
|
|
|
|
if url
|
|
|
|
url = url.strip
|
|
|
|
url = url[1..-1] if url[0, 1] == "/"
|
|
|
|
end
|
|
|
|
|
|
|
|
normalizations = SiteSetting.permalink_normalizations
|
|
|
|
|
|
|
|
@normalizer = Normalizer.new(normalizations) unless @normalizer &&
|
|
|
|
@normalizer.source == normalizations
|
|
|
|
@normalizer.normalize(url)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.find_by_url(url)
|
|
|
|
find_by(url: normalize_url(url))
|
|
|
|
end
|
|
|
|
|
2014-08-28 15:09:36 -04:00
|
|
|
def target_url
|
2024-05-27 15:31:51 -04:00
|
|
|
return relative_external_url if external_url
|
2024-05-27 15:30:19 -04:00
|
|
|
return post.relative_url if post
|
2014-08-28 15:09:36 -04:00
|
|
|
return topic.relative_url if topic
|
2024-05-27 15:30:19 -04:00
|
|
|
return category.relative_url if category
|
|
|
|
return tag.relative_url if tag
|
|
|
|
return user.relative_url if user
|
2014-08-28 15:09:36 -04:00
|
|
|
nil
|
|
|
|
end
|
2015-07-17 06:23:48 -04:00
|
|
|
|
|
|
|
def self.filter_by(url = nil)
|
|
|
|
permalinks =
|
2024-02-05 11:31:31 -05:00
|
|
|
Permalink.includes(:topic, :post, :category, :tag, :user).order("permalinks.created_at desc")
|
2015-07-17 06:23:48 -04:00
|
|
|
|
|
|
|
permalinks.where!("url ILIKE :url OR external_url ILIKE :url", url: "%#{url}%") if url.present?
|
|
|
|
permalinks.limit!(100)
|
|
|
|
permalinks.to_a
|
|
|
|
end
|
2024-05-27 16:33:30 -04:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def normalize_url
|
|
|
|
self.url = Permalink.normalize_url(url) if url
|
|
|
|
end
|
|
|
|
|
|
|
|
def encode_url
|
|
|
|
self.url = UrlHelper.encode(url) if url
|
|
|
|
end
|
|
|
|
|
|
|
|
def relative_external_url
|
|
|
|
external_url.match?(%r{\A/[^/]}) ? "#{Discourse.base_path}#{external_url}" : external_url
|
|
|
|
end
|
2024-11-13 18:03:58 -05:00
|
|
|
|
|
|
|
def clear_associations
|
|
|
|
self.topic_id = nil
|
|
|
|
self.post_id = nil
|
|
|
|
self.category_id = nil
|
|
|
|
self.user_id = nil
|
|
|
|
self.tag_id = nil
|
|
|
|
self.external_url = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def set_association_value
|
|
|
|
self.topic_id = self.permalink_type_value if self.topic_type?
|
|
|
|
self.post_id = self.permalink_type_value if self.post_type?
|
|
|
|
self.user_id = self.permalink_type_value if self.user_type?
|
|
|
|
self.category_id = self.permalink_type_value if self.category_type?
|
|
|
|
self.external_url = self.permalink_type_value if self.external_url_type?
|
|
|
|
self.tag_id = Tag.where(name: self.permalink_type_value).first&.id if self.tag_type?
|
|
|
|
end
|
2014-08-28 15:09:36 -04:00
|
|
|
end
|
2014-10-08 11:50:44 -04: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 14:29:56 -05:00
|
|
|
# created_at :datetime not null
|
|
|
|
# updated_at :datetime not null
|
2014-10-08 11:50:44 -04:00
|
|
|
# external_url :string(1000)
|
2020-05-25 05:48:54 -04:00
|
|
|
# tag_id :integer
|
2024-02-05 11:31:31 -05:00
|
|
|
# user_id :integer
|
2014-10-08 11:50:44 -04:00
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
|
|
|
# index_permalinks_on_url (url) UNIQUE
|
|
|
|
#
|