mirror of
https://github.com/discourse/discourse.git
synced 2024-11-27 20:43:40 +08:00
6f76a12e0a
* FEATURE: Let sites add a sitemap.xml file. This PR adds the same features discourse-sitemap provides to core. Sitemaps are only added to the robots.txt file if the `enable_sitemap` setting is enabled and `login_required` disabled. After merging discourse/discourse-sitemap#34, this change will take priority over the sitemap plugin because it will disable itself. We're also using the same sitemaps table, so our migration won't try to create it again using `if_not_exists: true`.
72 lines
2.0 KiB
Ruby
72 lines
2.0 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
class SitemapController < ApplicationController
|
|
layout false
|
|
skip_before_action :preload_json, :check_xhr
|
|
before_action :check_sitemap_enabled
|
|
|
|
def index
|
|
@sitemaps = Sitemap
|
|
.where(enabled: true)
|
|
.where.not(name: Sitemap::NEWS_SITEMAP_NAME)
|
|
|
|
render :index
|
|
end
|
|
|
|
def page
|
|
index = params.require(:page)
|
|
sitemap = Sitemap.find_by(enabled: true, name: index.to_s)
|
|
raise Discourse::NotFound if sitemap.nil?
|
|
|
|
@output = Rails.cache.fetch("sitemap/#{sitemap.name}/#{sitemap.max_page_size}", expires_in: 24.hours) do
|
|
@topics = sitemap.topics
|
|
render :page, content_type: 'text/xml; charset=UTF-8'
|
|
end
|
|
|
|
render plain: @output, content_type: 'text/xml; charset=UTF-8' unless performed?
|
|
end
|
|
|
|
def recent
|
|
sitemap = Sitemap.touch(Sitemap::RECENT_SITEMAP_NAME)
|
|
|
|
@output = Rails.cache.fetch("sitemap/recent/#{sitemap.last_posted_at.to_i}", expires_in: 1.hour) do
|
|
@topics = sitemap.topics
|
|
render :page, content_type: 'text/xml; charset=UTF-8'
|
|
end
|
|
|
|
render plain: @output, content_type: 'text/xml; charset=UTF-8' unless performed?
|
|
end
|
|
|
|
def news
|
|
sitemap = Sitemap.touch(Sitemap::NEWS_SITEMAP_NAME)
|
|
|
|
@output = Rails.cache.fetch("sitemap/news", expires_in: 5.minutes) do
|
|
dlocale = SiteSetting.default_locale.downcase
|
|
@locale = dlocale.gsub(/_.*/, '')
|
|
@locale = dlocale.sub('_', '-') if @locale === "zh"
|
|
@topics = sitemap.topics
|
|
render :news, content_type: 'text/xml; charset=UTF-8'
|
|
end
|
|
|
|
render plain: @output, content_type: 'text/xml; charset=UTF-8' unless performed?
|
|
end
|
|
|
|
private
|
|
|
|
def check_sitemap_enabled
|
|
raise Discourse::NotFound if !SiteSetting.enable_sitemap
|
|
end
|
|
|
|
def build_sitemap_topic_url(slug, id, posts_count = nil)
|
|
base_url = [Discourse.base_url, 't', slug, id].join('/')
|
|
return base_url if posts_count.nil?
|
|
|
|
page, mod = posts_count.divmod(TopicView.chunk_size)
|
|
page += 1 if mod > 0
|
|
|
|
page > 1 ? "#{base_url}?page=#{page}" : base_url
|
|
end
|
|
helper_method :build_sitemap_topic_url
|
|
|
|
end
|