<commit_before>class FeaturesController < ApplicationController
  #
  #
  #
  def index
    set_common_variables(session)
    
    @feature = Feature.find(session[:interface][:context_id]) unless session[:interface][:context_id].blank? 
    
    #if params[:context_id] && params[:context_id].match(/^[\d]+$/)
    #  redirect_to :action => 'index', :anchor => params[:context_id]
    #  return false
    #end

    # In the event that a Blurb with this code doesn't exist, fail gracefully
    @intro_blurb = Blurb.find_by_code('homepage.intro') || Blurb.new
        
    respond_to do |format|
      format.html
      format.xml do
        render :action => 'index'
      end
    end
  end

  #
  #
  #
  def show
    set_common_variables(session)
    
    @feature = Feature.get_by_fid(params[:id])
    session[:interface][:context_id] = @feature.id unless @feature.nil?
    
    respond_to do |format|
      format.html
      format.xml
      format.csv
    end
  end 

  #
  #
  #
  def iframe
    @feature = Feature.find(params[:id])
    render :action => 'iframe', :layout => 'iframe'
  end
  
  def by_geo_code
    geo_code_type_str = params[:geo_code_type]
    geo_code_type = GeoCodeType.get_by_code(geo_code_type_str)
    @feature = nil
    if !geo_code_type.nil?
      geo_code = FeatureGeoCode.first(:conditions => {:geo_code_type_id => geo_code_type.id, :geo_code_value => params[:geo_code]})
      @feature = geo_code.feature if !geo_code.nil?
    end
    respond_to do |format|
      format.html { render :action => 'show' }
      format.xml  { render :action => 'show' }
    end
  end
    
  #
  #
  #
  def by_fid
    feature_array = params[:fids].split(/\D+/)
    feature_array.shift if feature_array.size>0 && feature_array.first.blank?
    @features =  feature_array.collect{|feature_id| Feature.get_by_fid(feature_id.to_i)}.find_all{|f| f && f.is_public==1}
    @view = params[:view_code].nil? ? nil : View.get_by_code(params[:view_code])
    respond_to do |format|
      format.html { render :action => 'staff_show' }
      format.xml  { render :action => 'index' }
      format.json { render :json => Hash.from_xml(render_to_string(:action => 'index.xml.builder')) }
    end
  end

  #
  #
  #
  def by_old_pid
    @features = params[:old_pids].split(/\D+/).find_all{|p| p && !p.blank?}.collect{|p| Feature.find_by_old_pid("f#{p}")}.find_all{|f| f}
    @view = params[:view_code].nil? ? nil : View.get_by_code(params[:view_code])
    respond_to do |format|
      format.html { render :action => 'staff_show' }
      format.xml  { render :action => 'index' }
      format.json { render :json => Hash.from_xml(render_to_string(:action => 'index.xml.builder')) }
    end
  end
  
  def by_name
    params[:filter] = params[:query]
    options={
      :page => params[:page] || 1,
      :per_page => params[:per_page] || 15,
      :conditions => {:is_public => 1}
    }
    search_options = {
      :scope => params[:scope] || 'name',
      :match => params[:match]
    }
    @view = params[:view_code].nil? ? nil : View.get_by_code(params[:view_code])
    joins = []
    if !params[:feature_type].blank?
      joins << "LEFT JOIN cumulative_category_feature_associations ccfa ON ccfa.feature_id = features.id"
      options[:conditions]['ccfa.category_id'] = params[:feature_type].split(',')
      options[:conditions]['features.is_public'] = 1
      options[:conditions].delete(:is_public)
    end
    if !params[:characteristic_id].blank?
      joins << "LEFT JOIN category_features cf ON cf.feature_id = features.id"
      options[:conditions]['cf.category_id'] = params[:characteristic_id].split(',')
      options[:conditions]['cf.type'] = nil
      options[:conditions]['features.is_public'] = 1
      options[:conditions].delete(:is_public)
    end  
    options[:joins] = joins.join(' ') unless joins.empty?
    options[:select] = "features.*, DISTINCT feature.id" unless joins.empty?
    perform_global_search(options, search_options)

    respond_to do |format|
      format.html { render :action => 'paginated_show' }
      format.xml  { render :action => 'paginated_show' }
      format.json { render :json => Hash.from_xml(render_to_string(:action => 'paginated_show.xml.builder')) }
    end
  end
  
  def characteristics_list
    @kmaps_characteristics = CategoryFeature.find(:all, :select => "DISTINCT category_id", :conditions => "type IS NULL")
    render :json => @kmaps_characteristics.collect{|c| {:id => c.category_id, :name => c.to_s.strip}}.sort_by{|a| a[:name].downcase.strip}
  end
  
  def gis_resources
    fids = params[:fids].split(/\D+/)
    fids.shift if fids.size>0 && fids.first.blank?
    features = fids.collect{|fid| Feature.get_by_fid(fid.to_i)}.find_all{|f| f && f.is_public==1}
    fids = features.collect{|f| f.pid.gsub(/[^\d]/, '')}
    if params[:contained] && params[:contained] == '1'
      contained_fids = features.collect{|feature| feature.descendants.collect{|f|f.pid.gsub(/[^\d]/, '')}}.flatten
      fids = fids | contained_fids
    end
    fids.collect!{|fid| "fid="+fid }
    render :text => "Sorry, this request includes too many features for us to currently be able to provide this data." and return if fids.length > 300
    cql_filter = fids.join("+OR+")
    
  	general_params = "version=1.0.0&typename=thl:roman_popular_poly,thl:roman_popular_pt&layers=thl:roman_popular_poly,thl:roman_popular_pt&styles=thl_noscale,thl_noscale&projection=EPSG%3A4326&srs=EPSG%3A4326&cql_filter=("+cql_filter+");("+cql_filter+")"
  	
    case params[:format]
    when 'gml'
      service = 'wfs'
      params = "&service=wfs&request=GetFeature&outputformat=GML2"
      type = 'text/xml'
      name = 'thl_gis.gml'
    when 'kml'
      service = 'wms'
      params = "&service=wms&request=GetMap&width=1600&height=750&bbox=-180.0,-90.0,180.0,90.0&format=application/vnd.google-earth.kml%20XML"
      type = 'text/xml'
      name = 'thl_gis.kml'
    when 'kmz'
      service = 'wms'
      params = "&service=wms&request=GetMap&width=1600&height=750&bbox=-180.0,-90.0,180.0,90.0&format=application/vnd.google-earth.kmz%20XML"
      type = 'application/vnd.google-earth.kmz'
      name = 'thl_gis.kmz'
    when 'shp'
      service = 'wfs'
      params = "&service=wfs&request=GetFeature&outputformat=shape-zip"
      type = 'application/zip'
      name = 'thl_gis.zip'
    end
    
    # Find the proper instance of GeoServer, based on the current environment
    hostname = Socket.gethostname.downcase
    if hostname == 'dev.thlib.org'
     geoserver_base = 'http://localhost:8080/thlib-geoserver/'
    elsif hostname =~ /sds[7-8].itc.virginia.edu/
     geoserver_base = 'http://localhost:8080/thdl-geoserver/'
    else
     geoserver_base = 'http://www.thlib.org:8080/thdl-geoserver/'
    end
    
    url = geoserver_base+service+"?"+general_params+params
    send_data(open(url).read, :filename => name, :type => type, :disposition => 'attachment')
  end
  
  def search
    options = {
      :page => params[:page] || 1,
      :per_page => 15,
      :conditions => {:is_public => 1}
    }
    search_options = {
      :scope => params[:scope],
      :match => params[:match]
    }
    search_scope = params[:search_scope].blank? ? 'global' : params[:search_scope]
    @features = nil
    if !search_scope.blank?
      case search_scope
      when 'fid'
        feature = Feature.find(:first, :conditions => {:is_public => 1, :fid => params[:filter].gsub(/[^\d]/, '').to_i})
        if !feature.id.nil?
          render :url => {:action => 'expand_and_show',  :id => '59' }, :layout => false
        else
        end
      when 'contextual'
        if !params[:object_type].blank?
          options[:joins] = "LEFT JOIN cumulative_category_feature_associations ccfa ON ccfa.feature_id = features.id"
          options[:conditions]['ccfa.category_id'] = params[:object_type].split(',')
          options[:conditions]['features.is_public'] = 1
          options[:conditions].delete(:is_public)
        end
        if params[:context_id].blank?<commit_msg>Reducing the number of search results per page from 15 to 10<commit_after>class FeaturesController < ApplicationController
  #
  #
  #
  def index
    set_common_variables(session)
    
    @feature = Feature.find(session[:interface][:context_id]) unless session[:interface][:context_id].blank? 
    
    #if params[:context_id] && params[:context_id].match(/^[\d]+$/)
    #  redirect_to :action => 'index', :anchor => params[:context_id]
    #  return false
    #end

    # In the event that a Blurb with this code doesn't exist, fail gracefully
    @intro_blurb = Blurb.find_by_code('homepage.intro') || Blurb.new
        
    respond_to do |format|
      format.html
      format.xml do
        render :action => 'index'
      end
    end
  end

  #
  #
  #
  def show
    set_common_variables(session)
    
    @feature = Feature.get_by_fid(params[:id])
    session[:interface][:context_id] = @feature.id unless @feature.nil?
    
    respond_to do |format|
      format.html
      format.xml
      format.csv
    end
  end 

  #
  #
  #
  def iframe
    @feature = Feature.find(params[:id])
    render :action => 'iframe', :layout => 'iframe'
  end
  
  def by_geo_code
    geo_code_type_str = params[:geo_code_type]
    geo_code_type = GeoCodeType.get_by_code(geo_code_type_str)
    @feature = nil
    if !geo_code_type.nil?
      geo_code = FeatureGeoCode.first(:conditions => {:geo_code_type_id => geo_code_type.id, :geo_code_value => params[:geo_code]})
      @feature = geo_code.feature if !geo_code.nil?
    end
    respond_to do |format|
      format.html { render :action => 'show' }
      format.xml  { render :action => 'show' }
    end
  end
    
  #
  #
  #
  def by_fid
    feature_array = params[:fids].split(/\D+/)
    feature_array.shift if feature_array.size>0 && feature_array.first.blank?
    @features =  feature_array.collect{|feature_id| Feature.get_by_fid(feature_id.to_i)}.find_all{|f| f && f.is_public==1}
    @view = params[:view_code].nil? ? nil : View.get_by_code(params[:view_code])
    respond_to do |format|
      format.html { render :action => 'staff_show' }
      format.xml  { render :action => 'index' }
      format.json { render :json => Hash.from_xml(render_to_string(:action => 'index.xml.builder')) }
    end
  end

  #
  #
  #
  def by_old_pid
    @features = params[:old_pids].split(/\D+/).find_all{|p| p && !p.blank?}.collect{|p| Feature.find_by_old_pid("f#{p}")}.find_all{|f| f}
    @view = params[:view_code].nil? ? nil : View.get_by_code(params[:view_code])
    respond_to do |format|
      format.html { render :action => 'staff_show' }
      format.xml  { render :action => 'index' }
      format.json { render :json => Hash.from_xml(render_to_string(:action => 'index.xml.builder')) }
    end
  end
  
  def by_name
    params[:filter] = params[:query]
    options={
      :page => params[:page] || 1,
      :per_page => params[:per_page] || 15,
      :conditions => {:is_public => 1}
    }
    search_options = {
      :scope => params[:scope] || 'name',
      :match => params[:match]
    }
    @view = params[:view_code].nil? ? nil : View.get_by_code(params[:view_code])
    joins = []
    if !params[:feature_type].blank?
      joins << "LEFT JOIN cumulative_category_feature_associations ccfa ON ccfa.feature_id = features.id"
      options[:conditions]['ccfa.category_id'] = params[:feature_type].split(',')
      options[:conditions]['features.is_public'] = 1
      options[:conditions].delete(:is_public)
    end
    if !params[:characteristic_id].blank?
      joins << "LEFT JOIN category_features cf ON cf.feature_id = features.id"
      options[:conditions]['cf.category_id'] = params[:characteristic_id].split(',')
      options[:conditions]['cf.type'] = nil
      options[:conditions]['features.is_public'] = 1
      options[:conditions].delete(:is_public)
    end  
    options[:joins] = joins.join(' ') unless joins.empty?
    options[:select] = "features.*, DISTINCT feature.id" unless joins.empty?
    perform_global_search(options, search_options)

    respond_to do |format|
      format.html { render :action => 'paginated_show' }
      format.xml  { render :action => 'paginated_show' }
      format.json { render :json => Hash.from_xml(render_to_string(:action => 'paginated_show.xml.builder')) }
    end
  end
  
  def characteristics_list
    @kmaps_characteristics = CategoryFeature.find(:all, :select => "DISTINCT category_id", :conditions => "type IS NULL")
    render :json => @kmaps_characteristics.collect{|c| {:id => c.category_id, :name => c.to_s.strip}}.sort_by{|a| a[:name].downcase.strip}
  end
  
  def gis_resources
    fids = params[:fids].split(/\D+/)
    fids.shift if fids.size>0 && fids.first.blank?
    features = fids.collect{|fid| Feature.get_by_fid(fid.to_i)}.find_all{|f| f && f.is_public==1}
    fids = features.collect{|f| f.pid.gsub(/[^\d]/, '')}
    if params[:contained] && params[:contained] == '1'
      contained_fids = features.collect{|feature| feature.descendants.collect{|f|f.pid.gsub(/[^\d]/, '')}}.flatten
      fids = fids | contained_fids
    end
    fids.collect!{|fid| "fid="+fid }
    render :text => "Sorry, this request includes too many features for us to currently be able to provide this data." and return if fids.length > 300
    cql_filter = fids.join("+OR+")
    
  	general_params = "version=1.0.0&typename=thl:roman_popular_poly,thl:roman_popular_pt&layers=thl:roman_popular_poly,thl:roman_popular_pt&styles=thl_noscale,thl_noscale&projection=EPSG%3A4326&srs=EPSG%3A4326&cql_filter=("+cql_filter+");("+cql_filter+")"
  	
    case params[:format]
    when 'gml'
      service = 'wfs'
      params = "&service=wfs&request=GetFeature&outputformat=GML2"
      type = 'text/xml'
      name = 'thl_gis.gml'
    when 'kml'
      service = 'wms'
      params = "&service=wms&request=GetMap&width=1600&height=750&bbox=-180.0,-90.0,180.0,90.0&format=application/vnd.google-earth.kml%20XML"
      type = 'text/xml'
      name = 'thl_gis.kml'
    when 'kmz'
      service = 'wms'
      params = "&service=wms&request=GetMap&width=1600&height=750&bbox=-180.0,-90.0,180.0,90.0&format=application/vnd.google-earth.kmz%20XML"
      type = 'application/vnd.google-earth.kmz'
      name = 'thl_gis.kmz'
    when 'shp'
      service = 'wfs'
      params = "&service=wfs&request=GetFeature&outputformat=shape-zip"
      type = 'application/zip'
      name = 'thl_gis.zip'
    end
    
    # Find the proper instance of GeoServer, based on the current environment
    hostname = Socket.gethostname.downcase
    if hostname == 'dev.thlib.org'
     geoserver_base = 'http://localhost:8080/thlib-geoserver/'
    elsif hostname =~ /sds[7-8].itc.virginia.edu/
     geoserver_base = 'http://localhost:8080/thdl-geoserver/'
    else
     geoserver_base = 'http://www.thlib.org:8080/thdl-geoserver/'
    end
    
    url = geoserver_base+service+"?"+general_params+params
    send_data(open(url).read, :filename => name, :type => type, :disposition => 'attachment')
  end
  
  def search
    options = {
      :page => params[:page] || 1,
      :per_page => 10,
      :conditions => {:is_public => 1}
    }
    search_options = {
      :scope => params[:scope],
      :match => params[:match]
    }
    search_scope = params[:search_scope].blank? ? 'global' : params[:search_scope]
    @features = nil
    if !search_scope.blank?
      case search_scope
      when 'fid'
        feature = Feature.find(:first, :conditions => {:is_public => 1, :fid => params[:filter].gsub(/[^\d]/, '').to_i})
        if !feature.id.nil?
          render :url => {:action => 'expand_and_show',  :id => '59' }, :layout => false
        else
        end
      when 'contextual'
        if !params[:object_type].blank?
          options[:joins] = "LEFT JOIN cumulative_category_feature_associations ccfa ON ccfa.feature_id = features.id"
          options[:conditions]['ccfa.category_id'] = params[:object_type].split(',')
          options[:conditions]['features.is_public'] = 1
          options[:conditions].delete(:is_public)
        end
        if params[:context_id].blank?
{'fraction_non_alphanumeric': 0.15242954022273378, 'fraction_numerical': 0.012194363217818702, 'mean_word_length': 3.2955638695884555, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 109, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 28}, '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': '351923', 'n_tokens_mistral': 5762, 'n_tokens_neox': 5528, 'n_words': 990}