<commit_before>module PagesCore
  module Frontend
    class PagesController < ::FrontendController
      include PagesCore::FrontendHelper
      include PagesCore::Templates::ControllerActions
      include PagesCore::HeadTagsHelper

      include PagesCore::PreviewPagesController
      include PagesCore::RssController

      caches_page :index if PagesCore.config(:page_cache)

      before_action :load_root_pages
      before_action :find_page_by_path, only: [:show]
      before_action :find_page, only: %i[show preview]
      before_action :require_page, only: %i[show preview]
      before_action :canonicalize_url, only: [:show]
      after_action :cache_page_request, only: [:show]

      def index
        respond_to do |format|
          format.html { render_published_page(root_pages.try(&:first)) }
          format.rss { render_rss(all_feed_items) }
        end
      end

      def show
        respond_to do |format|
          format.html { render_published_page(@page) }
          format.rss { render_rss(@page.pages.limit(20), title: @page.name) }
          format.json { render json: @page }
        end
      end

      private

      def canonical_path(page)
        if page.redirects?
          page.redirect_path(locale: page.locale)
        else
          page_path(page.locale, page)
        end
      end

      def canonicalize_url
        return if @page.redirects?
        return if request.path == canonical_path(@page)
        # Don't canonicalize if any unknown params are present
        return if (params.keys - %w[controller action path locale id]).any?
        redirect_to(canonical_path(@page), status: :moved_permanently)
      end

      def render(*args)
        @already_rendered = true
        super
      end

      def redirect_to(*args)
        @already_rendered = true
        super
      end

      def page_template(page)
        if PagesCore::Templates.names.include?(page.template)
          page.template
        else
          "index"
        end
      end

      def render_page
        return if redirect_page(@page)

        unless document_title?
          document_title(@page.meta_title? ? @page.meta_title : @page.name)
        end

        template = page_template(@page)
        run_template_actions_for(template, @page)
        return if @already_rendered
        render template: "pages/templates/#{template}"
      end

      # Cache pages by hand. This is dirty, but it works.
      def cache_page_request
        status_code = response.status.try(&:to_i)
        unless status_code == 200 &&
               PagesCore.config(:page_cache) &&
               @page && @locale
          return
        end

        self.class.cache_page response.body, request.path
      end<commit_msg>Page caching can be disabled per request<commit_after>module PagesCore
  module Frontend
    class PagesController < ::FrontendController
      include PagesCore::FrontendHelper
      include PagesCore::Templates::ControllerActions
      include PagesCore::HeadTagsHelper

      include PagesCore::PreviewPagesController
      include PagesCore::RssController

      before_action :load_root_pages
      before_action :find_page_by_path, only: [:show]
      before_action :find_page, only: %i[show preview]
      before_action :require_page, only: %i[show preview]
      before_action :canonicalize_url, only: [:show]
      after_action :cache_page_request, only: %i[index show]

      def index
        respond_to do |format|
          format.html { render_published_page(root_pages.try(&:first)) }
          format.rss { render_rss(all_feed_items) }
        end
      end

      def show
        respond_to do |format|
          format.html { render_published_page(@page) }
          format.rss { render_rss(@page.pages.limit(20), title: @page.name) }
          format.json { render json: @page }
        end
      end

      private

      def canonical_path(page)
        if page.redirects?
          page.redirect_path(locale: page.locale)
        else
          page_path(page.locale, page)
        end
      end

      def canonicalize_url
        return if @page.redirects?
        return if request.path == canonical_path(@page)
        # Don't canonicalize if any unknown params are present
        return if (params.keys - %w[controller action path locale id]).any?
        redirect_to(canonical_path(@page), status: :moved_permanently)
      end

      def disable_page_cache!
        @page_cache_disabled = true
      end

      def render(*args)
        @already_rendered = true
        super
      end

      def redirect_to(*args)
        @already_rendered = true
        super
      end

      def page_template(page)
        if PagesCore::Templates.names.include?(page.template)
          page.template
        else
          "index"
        end
      end

      def render_page
        return if redirect_page(@page)

        unless document_title?
          document_title(@page.meta_title? ? @page.meta_title : @page.name)
        end

        template = page_template(@page)
        run_template_actions_for(template, @page)
        return if @already_rendered
        render template: "pages/templates/#{template}"
      end

      # Cache pages by hand. This is dirty, but it works.
      def cache_page_request
        return if @page_cache_disabled || !PagesCore.config(:page_cache)
        return if response.status&.to_i != 200
        return unless @page && @locale

        self.class.cache_page(response.body)
      end
{'fraction_non_alphanumeric': 0.0803928701345944, 'fraction_numerical': 0.0018188432157148053, 'mean_word_length': 2.3206521739130435, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5136477', 'n_tokens_mistral': 1581, 'n_tokens_neox': 1505, 'n_words': 373}