<commit_before>require 'zip'

module RailsAdmin
  module Config
    module Actions
      class ImportSchema < RailsAdmin::Config::Actions::Base

        register_instance_option :only do
          Setup::Schema
        end

        register_instance_option :collection do
          true
        end

        register_instance_option :http_methods do
          [:get, :post]
        end

        register_instance_option :controller do
          proc do

            if params[:_save] && data = params[:import_schema_data]
              library = library = Setup::Library.where(id: data[:library_id]).first
              file = data[:file]
              base_uri = data[:base_uri]
              if (@object = ImportSchemaData.new(library: library, file: file, base_uri: base_uri)).valid?
                if (i = (name = file.original_filename).rindex('.')) && name.from(i) == '.zip'
                  schemas = []
                  begin
                    Zip::InputStream.open(StringIO.new(file.read)) do |zis|
                      while is_ok && entry = zis.get_next_entry
                        unless (schema = entry.get_input_stream.read).blank?
                          entry_uri = base_uri.blank? ? entry.name : "#{base_uri}/#{entry.name}"
                          schema = Setup::Schema.new(library: library, uri: entry_uri, schema: schema)
                          if schema.save
                            schemas << schema
                          else
                            @object.errors.add(:file, "contains invalid schema in zip entry #{entry.name}: #{schema.errors.full_messages.join(', ')}")
                          end
                        end
                      end
                    end
                  rescue Exception => ex
                    @object.errors.add(:file, "Zip file format error: #{ex.message}")
                  end
                  if @object.errors.blank?
                    dts = 0;
                    schemas.each { |schema| dts += schema.data_types.size }
                    flash[:success] = "#{schemas.length} schemas and #{dts} data types successfully imported"
                    redirect_to back_or_index<commit_msg>Bug fixed (undefined variable is_ok).<commit_after>require 'zip'

module RailsAdmin
  module Config
    module Actions
      class ImportSchema < RailsAdmin::Config::Actions::Base

        register_instance_option :only do
          Setup::Schema
        end

        register_instance_option :collection do
          true
        end

        register_instance_option :http_methods do
          [:get, :post]
        end

        register_instance_option :controller do
          proc do

            if params[:_save] && data = params[:import_schema_data]
              library = library = Setup::Library.where(id: data[:library_id]).first
              file = data[:file]
              base_uri = data[:base_uri]
              if (@object = ImportSchemaData.new(library: library, file: file, base_uri: base_uri)).valid?
                if (i = (name = file.original_filename).rindex('.')) && name.from(i) == '.zip'
                  schemas = []
                  begin
                    Zip::InputStream.open(StringIO.new(file.read)) do |zis|
                      while @object.errors.blank? && entry = zis.get_next_entry
                        unless (schema = entry.get_input_stream.read).blank?
                          entry_uri = base_uri.blank? ? entry.name : "#{base_uri}/#{entry.name}"
                          schema = Setup::Schema.new(library: library, uri: entry_uri, schema: schema)
                          if schema.save
                            schemas << schema
                          else
                            @object.errors.add(:file, "contains invalid schema in zip entry #{entry.name}: #{schema.errors.full_messages.join(', ')}")
                          end
                        end
                      end
                    end
                  rescue Exception => ex
                    @object.errors.add(:file, "Zip file format error: #{ex.message}")
                  end
                  if @object.errors.blank?
                    dts = 0;
                    schemas.each { |schema| dts += schema.data_types.size }
                    flash[:success] = "#{schemas.length} schemas and #{dts} data types successfully imported"
                    redirect_to back_or_index
{'fraction_non_alphanumeric': 0.09774266365688487, 'fraction_numerical': 0.0004514672686230248, 'mean_word_length': 1.4534883720930232, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 5, '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': '7383687', 'n_tokens_mistral': 1124, 'n_tokens_neox': 1062, 'n_words': 259}