2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2014-08-28 15:09:36 -04:00
|
|
|
class Permalink < ActiveRecord::Base
|
|
|
|
belongs_to :topic
|
|
|
|
belongs_to :post
|
|
|
|
belongs_to :category
|
2020-05-25 05:48:54 -04:00
|
|
|
belongs_to :tag
|
2014-08-28 15:09:36 -04:00
|
|
|
|
|
|
|
before_validation :normalize_url
|
|
|
|
|
2015-07-15 01:32:35 -04:00
|
|
|
class Normalizer
|
|
|
|
attr_reader :source
|
|
|
|
|
|
|
|
def initialize(source)
|
|
|
|
@source = source
|
|
|
|
if source.present?
|
|
|
|
@rules = source.split("|").map do |rule|
|
|
|
|
parse_rule(rule)
|
|
|
|
end.compact
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def parse_rule(rule)
|
|
|
|
return unless rule =~ /\/.*\//
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
if regex.length > 1
|
2018-03-28 04:20:08 -04:00
|
|
|
[Regexp.new(regex[1..-1]), sub[1..-1] || ""]
|
2015-07-15 01:32:35 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
def normalize(url)
|
|
|
|
return url unless @rules
|
2017-07-27 21:20:09 -04:00
|
|
|
@rules.each do |(regex, sub)|
|
|
|
|
url = url.sub(regex, sub)
|
2015-07-15 01:32:35 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
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
|
2017-07-27 21:20:09 -04:00
|
|
|
url = url[1..-1] if url[0, 1] == '/'
|
2015-07-15 01:32:35 -04:00
|
|
|
end
|
|
|
|
|
2017-07-27 21:20:09 -04:00
|
|
|
normalizations = SiteSetting.permalink_normalizations
|
2015-07-15 01:32:35 -04:00
|
|
|
|
|
|
|
@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
|
|
|
|
|
|
|
|
def normalize_url
|
|
|
|
self.url = Permalink.normalize_url(url) if url
|
2014-08-28 15:09:36 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def target_url
|
2014-10-08 11:50:44 -04:00
|
|
|
return external_url if external_url
|
2015-10-12 16:48:32 -04:00
|
|
|
return "#{Discourse::base_uri}#{post.url}" if post
|
2014-08-28 15:09:36 -04:00
|
|
|
return topic.relative_url if topic
|
2020-03-20 18:06:20 -04:00
|
|
|
return "#{category.url}/#{category.id}" if category
|
2020-05-25 05:48:54 -04:00
|
|
|
return tag.full_url if tag
|
2014-08-28 15:09:36 -04:00
|
|
|
nil
|
|
|
|
end
|
2015-07-17 06:23:48 -04:00
|
|
|
|
2017-07-27 21:20:09 -04:00
|
|
|
def self.filter_by(url = nil)
|
2015-07-17 06:23:48 -04:00
|
|
|
permalinks = Permalink
|
2020-05-25 05:48:54 -04:00
|
|
|
.includes(:topic, :post, :category, :tag)
|
2017-07-27 21:20:09 -04:00
|
|
|
.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
|
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
|
2014-10-08 11:50:44 -04:00
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
|
|
|
# index_permalinks_on_url (url) UNIQUE
|
|
|
|
#
|