<commit_before># -*- encoding: utf-8 -*-
class ManifestationsController < ApplicationController
  load_and_authorize_resource except: :index
  authorize_resource only: :index
  before_filter :authenticate_user!, only: :edit
  before_filter :get_agent, :get_manifestation, except: [:create, :update, :destroy]
  before_filter :get_expression, only: :new
  if defined?(EnjuSubject)
    before_filter :get_subject, except: [:create, :update, :destroy]
  end
  before_filter :get_series_statement, only: [:index, :new, :edit]
  before_filter :get_item, :get_libraries, only: :index
  before_filter :prepare_options, only: [:new, :edit]
  before_filter :get_version, only: [:show]
  after_filter :solr_commit, only: :destroy
  after_filter :convert_charset, only: :index

  # GET /manifestations
  # GET /manifestations.json
  def index
    mode = params[:mode]
    if mode == 'add'
      unless current_user.try(:has_role?, 'Librarian')
        access_denied; return
      end
    end

    @seconds = Benchmark.realtime do
      if defined?(EnjuOai)
        @oai = check_oai_params(params)
        next if @oai[:need_not_to_search]
        if params[:format] == 'oai'
          oai_search = true
          from_and_until_times = set_from_and_until(Manifestation, params[:from], params[:until])
          from_time = @from_time = from_and_until_times[:from]
          until_time = @until_time = from_and_until_times[:until]
          if params[:verb] == 'GetRecord' && params[:identifier]
            begin
              @manifestation = Manifestation.find_by_oai_identifier(params[:identifier])
            rescue ActiveRecord::RecordNotFound
              @oai[:errors] << "idDoesNotExist"
              render formats: :oai, layout: false
              return
            end
            render template: 'manifestations/show', formats: :oai, layout: false
            return
          end
        end
      end

      set_reservable if defined?(EnjuCirculation)

      sort, @count = {}, {}
      query = ""

      if params[:format] == 'txt'
        per_page = 65534
      end

      if params[:format] == 'sru'
        if params[:operation] == 'searchRetrieve'
          sru = Sru.new(params)
          query = sru.cql.to_sunspot
          sort = sru.sort_by
        else
          render template: 'manifestations/explain', layout: false
          return
        end
      else
        if params[:api] == 'openurl'
          openurl = Openurl.new(params)
          @manifestations = openurl.search
          query = openurl.query_text
          sort = set_search_result_order(params[:sort_by], params[:order])
        else
          query = make_query(params[:query], params)
          sort = set_search_result_order(params[:sort_by], params[:order])
        end
      end

      # 絞り込みを行わない状態のクエリ
      @query = query.dup
      query = query.gsub('　', ' ')

      includes = [:root_series_statement]
      includes << :classifications if defined?(EnjuSubject)
      includes << :bookmarks if defined?(EnjuBookmark)
      search = Manifestation.search(include: includes)
      case @reservable
      when 'true'
        reservable = true
      when 'false'
        reservable = false
      else
        reservable = nil
      end

      agent = get_index_agent
      @index_agent = agent
      manifestation = @manifestation if @manifestation
      series_statement = @series_statement if @series_statement
      parent = @parent = Manifestation.where(id: params[:parent_id]).first if params[:parent_id].present?

      if defined?(EnjuSubject)
        subject = @subject if @subject
      end

      unless mode == 'add'
        search.build do
          with(:creator_ids).equal_to agent[:creator].id if agent[:creator]
          with(:contributor_ids).equal_to agent[:contributor].id if agent[:contributor]
          with(:publisher_ids).equal_to agent[:publisher].id if agent[:publisher]
          with(:series_statement_ids).equal_to series_statement.id if series_statement
          with(:parent_ids).equal_to parent.id if parent
        end
      end

      search.build do
        fulltext query unless query.blank?
        order_by sort[:sort_by], sort[:order] unless oai_search
        order_by :updated_at, :desc if oai_search
        if defined?(EnjuSubject)
          with(:subject_ids).equal_to subject.id if subject
        end
        unless parent
          if params[:serial].to_s.downcase == "true"
            with(:series_master).equal_to true unless parent
            with(:serial).equal_to true
            #if series_statement.serial?
            #  if mode != 'add'
            #    order_by :volume_number, sort[:order]
            #    order_by :issue_number, sort[:order]
            #    order_by :serial_number, sort[:order]
            #  end
            #else
            #  with(:serial).equal_to false
            #end
          else
            if mode != 'add'
              with(:resource_master).equal_to true
            end
          end
        end
        facet :reservable if defined?(EnjuCirculation)
      end
      search = make_internal_query(search)
      search.data_accessor_for(Manifestation).select = [
        :id,
        :original_title,
        :title_transcription,
        :required_role_id,
        :carrier_type_id,
        :access_address,
        :volume_number_string,
        :issue_number_string,
        :serial_number_string,
        :date_of_publication,
        :pub_date,
        :language_id,
        :created_at,
        :updated_at,
        :volume_number_string,
        :volume_number,
        :issue_number_string,
        :issue_number,
        :serial_number,
        :edition_string,
        :edition,
        :serial,
        :statement_of_responsibility
      ] if params[:format] == 'html' or params[:format].nil?
      all_result = search.execute
      @count[:query_result] = all_result.total
      @reservable_facet = all_result.facet(:reservable).rows if defined?(EnjuCirculation)
      max_number_of_results = @library_group.settings[:max_number_of_results].to_i
      if defined?(EnjuOai)
        # OAI-PMHのデフォルトの件数
        oai_per_page = 200
        if params[:resumptionToken]
          token = params[:resumptionToken].split(',')
          if token.size == 3
            @cursor = token.reverse.first.to_i
            if @cursor <= @count[:query_result]
              page = (@cursor.to_i + oai_per_page).div(oai_per_page)
            else
              @oai[:errors] << 'badResumptionToken'
            end
          else
            @oai[:errors] << 'badResumptionToken'
          end
        end
        page ||= 1
      end

      if max_number_of_results == 0
        @max_number_of_results = count[:query_result]
      else
        @max_number_of_results = max_number_of_results
      end

      if session[:search_params]
        unless search.query.to_params == session[:search_params]
          clear_search_sessions
        end
      else
        clear_search_sessions
        session[:params] = params<commit_msg>fix pagination with enju_oai next-l/enju_leaf#709<commit_after># -*- encoding: utf-8 -*-
class ManifestationsController < ApplicationController
  load_and_authorize_resource except: :index
  authorize_resource only: :index
  before_filter :authenticate_user!, only: :edit
  before_filter :get_agent, :get_manifestation, except: [:create, :update, :destroy]
  before_filter :get_expression, only: :new
  if defined?(EnjuSubject)
    before_filter :get_subject, except: [:create, :update, :destroy]
  end
  before_filter :get_series_statement, only: [:index, :new, :edit]
  before_filter :get_item, :get_libraries, only: :index
  before_filter :prepare_options, only: [:new, :edit]
  before_filter :get_version, only: [:show]
  after_filter :solr_commit, only: :destroy
  after_filter :convert_charset, only: :index

  # GET /manifestations
  # GET /manifestations.json
  def index
    mode = params[:mode]
    if mode == 'add'
      unless current_user.try(:has_role?, 'Librarian')
        access_denied; return
      end
    end

    @seconds = Benchmark.realtime do
      if defined?(EnjuOai)
        @oai = check_oai_params(params)
        next if @oai[:need_not_to_search]
        if params[:format] == 'oai'
          oai_search = true
          from_and_until_times = set_from_and_until(Manifestation, params[:from], params[:until])
          from_time = @from_time = from_and_until_times[:from]
          until_time = @until_time = from_and_until_times[:until]
          if params[:verb] == 'GetRecord' && params[:identifier]
            begin
              @manifestation = Manifestation.find_by_oai_identifier(params[:identifier])
            rescue ActiveRecord::RecordNotFound
              @oai[:errors] << "idDoesNotExist"
              render formats: :oai, layout: false
              return
            end
            render template: 'manifestations/show', formats: :oai, layout: false
            return
          end
        end
      end

      set_reservable if defined?(EnjuCirculation)

      sort, @count = {}, {}
      query = ""

      if params[:format] == 'txt'
        per_page = 65534
      end

      if params[:format] == 'sru'
        if params[:operation] == 'searchRetrieve'
          sru = Sru.new(params)
          query = sru.cql.to_sunspot
          sort = sru.sort_by
        else
          render template: 'manifestations/explain', layout: false
          return
        end
      else
        if params[:api] == 'openurl'
          openurl = Openurl.new(params)
          @manifestations = openurl.search
          query = openurl.query_text
          sort = set_search_result_order(params[:sort_by], params[:order])
        else
          query = make_query(params[:query], params)
          sort = set_search_result_order(params[:sort_by], params[:order])
        end
      end

      # 絞り込みを行わない状態のクエリ
      @query = query.dup
      query = query.gsub('　', ' ')

      includes = [:root_series_statement]
      includes << :classifications if defined?(EnjuSubject)
      includes << :bookmarks if defined?(EnjuBookmark)
      search = Manifestation.search(include: includes)
      case @reservable
      when 'true'
        reservable = true
      when 'false'
        reservable = false
      else
        reservable = nil
      end

      agent = get_index_agent
      @index_agent = agent
      manifestation = @manifestation if @manifestation
      series_statement = @series_statement if @series_statement
      parent = @parent = Manifestation.where(id: params[:parent_id]).first if params[:parent_id].present?

      if defined?(EnjuSubject)
        subject = @subject if @subject
      end

      unless mode == 'add'
        search.build do
          with(:creator_ids).equal_to agent[:creator].id if agent[:creator]
          with(:contributor_ids).equal_to agent[:contributor].id if agent[:contributor]
          with(:publisher_ids).equal_to agent[:publisher].id if agent[:publisher]
          with(:series_statement_ids).equal_to series_statement.id if series_statement
          with(:parent_ids).equal_to parent.id if parent
        end
      end

      search.build do
        fulltext query unless query.blank?
        order_by sort[:sort_by], sort[:order] unless oai_search
        order_by :updated_at, :desc if oai_search
        if defined?(EnjuSubject)
          with(:subject_ids).equal_to subject.id if subject
        end
        unless parent
          if params[:serial].to_s.downcase == "true"
            with(:series_master).equal_to true unless parent
            with(:serial).equal_to true
            #if series_statement.serial?
            #  if mode != 'add'
            #    order_by :volume_number, sort[:order]
            #    order_by :issue_number, sort[:order]
            #    order_by :serial_number, sort[:order]
            #  end
            #else
            #  with(:serial).equal_to false
            #end
          else
            if mode != 'add'
              with(:resource_master).equal_to true
            end
          end
        end
        facet :reservable if defined?(EnjuCirculation)
      end
      search = make_internal_query(search)
      search.data_accessor_for(Manifestation).select = [
        :id,
        :original_title,
        :title_transcription,
        :required_role_id,
        :carrier_type_id,
        :access_address,
        :volume_number_string,
        :issue_number_string,
        :serial_number_string,
        :date_of_publication,
        :pub_date,
        :language_id,
        :created_at,
        :updated_at,
        :volume_number_string,
        :volume_number,
        :issue_number_string,
        :issue_number,
        :serial_number,
        :edition_string,
        :edition,
        :serial,
        :statement_of_responsibility
      ] if params[:format] == 'html' or params[:format].nil?
      all_result = search.execute
      @count[:query_result] = all_result.total
      @reservable_facet = all_result.facet(:reservable).rows if defined?(EnjuCirculation)
      max_number_of_results = @library_group.settings[:max_number_of_results].to_i
      if defined?(EnjuOai)
        # OAI-PMHのデフォルトの件数
        oai_per_page = 200
        if params[:resumptionToken]
          token = params[:resumptionToken].split(',')
          if token.size == 3
            @cursor = token.reverse.first.to_i
            if @cursor <= @count[:query_result]
              page = (@cursor.to_i + oai_per_page).div(oai_per_page)
            else
              @oai[:errors] << 'badResumptionToken'
            end
          else
            @oai[:errors] << 'badResumptionToken'
          end
        end
        page ||= 1 if params[:format] == 'oai'
      end

      if max_number_of_results == 0
        @max_number_of_results = count[:query_result]
      else
        @max_number_of_results = max_number_of_results
      end

      if session[:search_params]
        unless search.query.to_params == session[:search_params]
          clear_search_sessions
        end
      else
        clear_search_sessions
        session[:params] = params
{'fraction_non_alphanumeric': 0.09645390070921986, 'fraction_numerical': 0.0019148936170212765, 'mean_word_length': 2.301568719269492, 'pattern_counts': {'":': 0, '<': 27, '<?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': '1205121', 'n_tokens_mistral': 4235, 'n_tokens_neox': 4168, 'n_words': 874}