<commit_before>module Alchemy
  class PagesController < Alchemy::BaseController
    include Alchemy::FerretSearch

    # We need to include this helper because we need the breadcrumb method.
    # And we cannot define the breadcrump method as helper_method, because rspec does not see helper_methods.
    # Not the best solution, but's working.
    # Anyone with a better idea please provide a patch.
    include Alchemy::BaseHelper

    rescue_from ActionController::RoutingError, :with => :render_404

    before_filter :render_page_or_redirect, :only => [:show, :sitemap]
    before_filter :perform_search, :only => :show, :if => proc { configuration(:ferret) }

    filter_access_to :show, :attribute_check => true, :model => Alchemy::Page, :load_method => :load_page

    caches_action(:show,
      :cache_path => proc { @page.cache_key(request) },
      :if => proc {
        if @page && Alchemy::Config.get(:cache_pages)
          pagelayout = PageLayout.get(@page.page_layout)
          if (pagelayout['cache'].nil? || pagelayout['cache']) && pagelayout['searchresults'] != true
            true
          end
        else
          false
        end
      }, :layout => false)

    layout :layout_for_page

    # Showing page from params[:urlname]
    # @page is fetched via before filter
    # @root_page is fetched via before filter
    # @language fetched via before_filter in alchemy_controller
    # querying for search results if any query is present via before_filter
    def show
      respond_to do |format|
        format.html { render }
        format.rss {
          if @page.contains_feed?
            render :action => "show", :layout => false, :handlers => [:builder]
          else
            render :xml => {:error => 'Not found'}, :status => 404
          end
        }
      end
    end

    # Renders a Google conform sitemap in xml
    def sitemap
      @pages = Page.find_all_by_sitemap_and_public(true, true)
      respond_to do |format|
        format.xml { render :layout => "sitemap" }
      end
    end

  private

    def load_page
      # we need this, because of a dec_auth bug (it calls this method after the before_filter again).
      return @page if @page
      if params[:urlname].blank?
        @page = Page.language_root_for(Language.get_default.id)
      else
        if params[:lang].blank?
          @page = Page.contentpages.find_by_urlname(params[:urlname])
          store_language_in_session(@page.language) if @page.present?
          return @page
        else
          @page = Page.contentpages.where(
            :urlname => params[:urlname],
            :language_id => session[:language_id],  # Make sure that the current language
            :language_code => params[:lang]         # matches the requested language code.
          ).first
        end
      end
    end

    def render_page_or_redirect
      @page ||= load_page
      if User.admins.count == 0 && @page.nil?
        redirect_to signup_path
      elsif @page.blank?
        raise_not_found_error
      elsif multi_language? && params[:lang].blank?
        redirect_page(:lang => session[:language_code])
      elsif multi_language? && params[:urlname].blank? && !params[:lang].blank? && configuration(:redirect_index)
        redirect_page(:lang => params[:lang])<commit_msg>Urgent fix for page loading in multi-site environments.<commit_after>module Alchemy
  class PagesController < Alchemy::BaseController
    include Alchemy::FerretSearch

    # We need to include this helper because we need the breadcrumb method.
    # And we cannot define the breadcrump method as helper_method, because rspec does not see helper_methods.
    # Not the best solution, but's working.
    # Anyone with a better idea please provide a patch.
    include Alchemy::BaseHelper

    rescue_from ActionController::RoutingError, :with => :render_404

    before_filter :render_page_or_redirect, :only => [:show, :sitemap]
    before_filter :perform_search, :only => :show, :if => proc { configuration(:ferret) }

    filter_access_to :show, :attribute_check => true, :model => Alchemy::Page, :load_method => :load_page

    caches_action(:show,
      :cache_path => proc { @page.cache_key(request) },
      :if => proc {
        if @page && Alchemy::Config.get(:cache_pages)
          pagelayout = PageLayout.get(@page.page_layout)
          if (pagelayout['cache'].nil? || pagelayout['cache']) && pagelayout['searchresults'] != true
            true
          end
        else
          false
        end
      }, :layout => false)

    layout :layout_for_page

    # Showing page from params[:urlname]
    # @page is fetched via before filter
    # @root_page is fetched via before filter
    # @language fetched via before_filter in alchemy_controller
    # querying for search results if any query is present via before_filter
    def show
      respond_to do |format|
        format.html { render }
        format.rss {
          if @page.contains_feed?
            render :action => "show", :layout => false, :handlers => [:builder]
          else
            render :xml => {:error => 'Not found'}, :status => 404
          end
        }
      end
    end

    # Renders a Google conform sitemap in xml
    def sitemap
      @pages = Page.find_all_by_sitemap_and_public(true, true)
      respond_to do |format|
        format.xml { render :layout => "sitemap" }
      end
    end

  private

    def load_page
      # we need this, because of a dec_auth bug (it calls this method after the before_filter again).
      return @page if @page
      if params[:urlname].blank?
        @page = Page.language_root_for(Language.get_default.id)
      else
        if params[:lang].blank?
          @page = Page.contentpages.where(urlname: params[:urlname], language_id: Language.get_default).first
          store_language_in_session(@page.language) if @page.present?
          return @page
        else
          @page = Page.contentpages.where(
            :urlname => params[:urlname],
            :language_id => session[:language_id],  # Make sure that the current language
            :language_code => params[:lang]         # matches the requested language code.
          ).first
        end
      end
    end

    def render_page_or_redirect
      @page ||= load_page
      if User.admins.count == 0 && @page.nil?
        redirect_to signup_path
      elsif @page.blank?
        raise_not_found_error
      elsif multi_language? && params[:lang].blank?
        redirect_page(:lang => session[:language_code])
      elsif multi_language? && params[:urlname].blank? && !params[:lang].blank? && configuration(:redirect_index)
        redirect_page(:lang => params[:lang])
{'fraction_non_alphanumeric': 0.10008976660682226, 'fraction_numerical': 0.002094554159186116, 'mean_word_length': 2.6430517711171664, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 47, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 6}, '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': '3422764', 'n_tokens_mistral': 1931, 'n_tokens_neox': 1875, 'n_words': 547}