diff --git a/scripts/exhibit_service.rb b/scripts/exhibit_service.rb index c644ac24..06b6ba73 100644 --- a/scripts/exhibit_service.rb +++ b/scripts/exhibit_service.rb @@ -4,12 +4,42 @@ # Run rails console: bundle exec rails c # Load the script: load 'scripts/exhibit_service.rb' # List resources with null image: ExhibitService.database_entries _ID_ +load 'scripts/thumbnail_service.rb' class ExhibitService class << self def services puts 'database_entries' puts ' # @param [Integer] exhibit id' + puts 'counts' + puts ' # @param [Integer] exhibit id' + end + + # @param [Integer] exhibit id + def counts(id) + puts "=====================================================" + begin + exhibit = Spotlight::Exhibit.find(id) + puts "Database entries related to exhibit #{id} -- title: #{exhibit.title} slug: #{exhibit.slug} featured_image: #{exhibit.featured_image} masthead: #{exhibit.masthead_id} thumbnail: #{exhibit.thumbnail_id}" + rescue ActiveRecord::RecordNotFound => e + puts "EXHIBIT DOES NOT EXIST: #{id}" + puts "Looking for related database entries that may be hanging around" + end + puts "=====================================================" + count_resources(id) + count_solr_document_sidecars(id) + count_searches(id) + count_filters(id) + count_main_navigations(id) + count_pages(id) + count_attachments(id) + count_blacklight_configurations(id) + count_contact_emails(id) + count_contacts(id) + count_custom_fields(id) + count_reindexing_log_entries(id) + count_slugs(id) + puts "=====================================================" end # @param [Integer] exhibit id @@ -35,10 +65,15 @@ def database_entries(id) list_contacts(id) list_custom_fields(id) list_reindexing_log_entries(id) + list_slugs(id) end private + def count_attachments exhibit_id + puts "Attachments: #{Spotlight::Attachment.where(exhibit_id: exhibit_id).count}" + end + def list_attachments exhibit_id puts 'Attachments:' Spotlight::Attachment.where(exhibit_id: exhibit_id).each do |r| @@ -47,6 +82,10 @@ def list_attachments exhibit_id puts '------------' end + def count_blacklight_configurations exhibit_id + puts "Blacklight Configurations: #{Spotlight::BlacklightConfiguration.where(exhibit_id: exhibit_id).count}" + end + def list_blacklight_configurations exhibit_id puts 'Blacklight Configurations:' Spotlight::BlacklightConfiguration.where(exhibit_id: exhibit_id).each do |r| @@ -55,6 +94,10 @@ def list_blacklight_configurations exhibit_id puts '------------' end + def count_contact_emails exhibit_id + puts "Contact Emails: #{Spotlight::ContactEmail.where(exhibit_id: exhibit_id).count}" + end + def list_contact_emails exhibit_id puts 'Contact Emails:' Spotlight::ContactEmail.where(exhibit_id: exhibit_id).each do |r| @@ -63,6 +106,10 @@ def list_contact_emails exhibit_id puts '------------' end + def count_contacts exhibit_id + puts "Contacts: #{Spotlight::Contact.where(exhibit_id: exhibit_id).count}" + end + def list_contacts exhibit_id puts 'Contacts:' Spotlight::Contact.where(exhibit_id: exhibit_id).each do |r| @@ -71,6 +118,10 @@ def list_contacts exhibit_id puts '------------' end + def count_custom_fields exhibit_id + puts "Custom Fields: #{Spotlight::CustomField.where(exhibit_id: exhibit_id).count}" + end + def list_custom_fields exhibit_id puts 'Custom Fields:' Spotlight::CustomField.where(exhibit_id: exhibit_id).each do |r| @@ -79,6 +130,10 @@ def list_custom_fields exhibit_id puts '------------' end + def count_filters exhibit_id + puts "Filters: #{Spotlight::Filter.where(exhibit_id: exhibit_id).count}" + end + def list_filters exhibit_id puts 'Filters:' Spotlight::Filter.where(exhibit_id: exhibit_id).each do |r| @@ -87,6 +142,10 @@ def list_filters exhibit_id puts '------------' end + def count_main_navigations exhibit_id + puts "Main Navigation: #{Spotlight::MainNavigation.where(exhibit_id: exhibit_id).count}" + end + def list_main_navigations exhibit_id puts 'Main Navigation:' Spotlight::MainNavigation.where(exhibit_id: exhibit_id).each do |r| @@ -95,6 +154,10 @@ def list_main_navigations exhibit_id puts '------------' end + def count_pages exhibit_id + puts "Pages: #{Spotlight::Page.where(exhibit_id: exhibit_id).count}" + end + def list_pages exhibit_id puts 'Pages:' Spotlight::Page.where(exhibit_id: exhibit_id).each do |r| @@ -103,6 +166,10 @@ def list_pages exhibit_id puts '------------' end + def count_reindexing_log_entries exhibit_id + puts "Reindexing Log Entries: #{Spotlight::ReindexingLogEntry.where(exhibit_id: exhibit_id).count}" + end + def list_reindexing_log_entries exhibit_id puts 'Reindexing Log Entries:' Spotlight::ReindexingLogEntry.where(exhibit_id: exhibit_id).each do |r| @@ -111,6 +178,10 @@ def list_reindexing_log_entries exhibit_id puts '------------' end + def count_resources exhibit_id + puts "Resources: #{Spotlight::Resource.where(exhibit_id: exhibit_id).count}" + end + def list_resources exhibit_id puts 'Resources:' Spotlight::Resource.where(exhibit_id: exhibit_id).each do |r| @@ -126,6 +197,10 @@ def list_resources exhibit_id puts '------------' end + def count_searches exhibit_id + puts "Searches: #{Spotlight::Search.where(exhibit_id: exhibit_id).count}" + end + def list_searches exhibit_id puts 'Searches:' Spotlight::Search.where(exhibit_id: exhibit_id).each do |r| @@ -134,6 +209,10 @@ def list_searches exhibit_id puts '------------' end + def count_solr_document_sidecars exhibit_id + puts "Solr Document Sidecars: #{Spotlight::SolrDocumentSidecar.where(exhibit_id: exhibit_id).count}" + end + def list_solr_document_sidecars exhibit_id puts 'Solr Document Sidecars:' Spotlight::SolrDocumentSidecar.where(exhibit_id: exhibit_id).each do |r| @@ -146,5 +225,17 @@ def list_solr_document_sidecars exhibit_id end puts '------------' end + + def count_slugs exhibit_id + puts "Friendly ID Slugs: #{FriendlyId::Slug.where(scope: "exhibit_id:#{exhibit_id}").count}" + end + + def list_slugs exhibit_id + puts 'Friendly ID Slugs:' + FriendlyId::Slug.where(scope: "exhibit_id:#{exhibit_id}").each do |slug| + puts " exhibit: #{slug.scope} id: #{slug.id} slug: #{slug.slug} type: #{slug.sluggable_type}" + end + puts '------------' + end end end diff --git a/scripts/featured_image_delete_service.rb b/scripts/featured_image_delete_service.rb index 6e28a0d4..a3071ae2 100644 --- a/scripts/featured_image_delete_service.rb +++ b/scripts/featured_image_delete_service.rb @@ -4,13 +4,14 @@ # upload and has NULL image: # Run rails console: bundle exec rails c # Load the script: load 'scripts/featured_image_delete_service.rb' -# List featured images with null image: ids = FeaturedImageDeleteService.find_null_images +# List featured images with null image: ids = FeaturedImageDeleteService.list_null_images # Delete all featured images with null image: FeaturedImageDeleteService.delete_featured_images(ids) +load 'scripts/thumbnail_service.rb' class FeaturedImageDeleteService class << self def services - puts 'find_null_images(limit = 1000)' + puts 'list_null_images(limit = 1000)' puts ' # @param [Integer] limit the number of image ids returned (default: 1000)' puts ' # @return [Array] featured_image_ids - list of ids of featured images with image=NULL' puts 'count_null_images' @@ -27,16 +28,16 @@ def services # @param [Integer] limit the number of image ids returned (default: 1000) # @return [Array] featured_image_ids - list of ids of featured images with image=NULL - def find_null_images(limit = 1000) + def list_null_images(limit: 1000, include_thumbnails: false) puts "Featured Images where image is NULL" - featured_image_ids = Spotlight::FeaturedImage.where(image: nil).limit(limit).map(&:id) + featured_image_ids = find_null_images(include_thumbnails: include_thumbnails, limit: limit) delete_featured_images(featured_image_ids, pretest: true) featured_image_ids end # @return [Integer] the number of featured images with image=NULL' def count_null_images - ids = find_null_images + ids = list_null_images ids.count >= 1000 ? puts('> 1000') : puts(ids.count) end @@ -54,19 +55,22 @@ def delete_featured_image(featured_image_id, auto_delete: false, pretest: false) featured_image = find_featured_image(featured_image_id) return if featured_image.blank? + thumbnails = find_thumbnails(featured_image) resources = find_resources(featured_image_id) solr_document_sidecars = find_solr_document_sidecars(resources) bookmarks = find_bookmarks(solr_document_sidecars) - perform_pretest(featured_image, resources, solr_document_sidecars, bookmarks) + perform_pretest(featured_image: featured_image, thumbnails: thumbnails, resources: resources, + solr_document_sidecars: solr_document_sidecars, bookmarks: bookmarks) return if pretest - return unless delete?(featured_image, resources, auto_delete) - perform_deletes(featured_image, resources, solr_document_sidecars, bookmarks) + return unless delete?(featured_image, resources, thumbnails, auto_delete) + perform_deletes(featured_image: featured_image, thumbnails: thumbnails, resources: resources, + solr_document_sidecars: solr_document_sidecars, bookmarks: bookmarks) end private - def perform_deletes(featured_image, resources, solr_document_sidecars, bookmarks) + def perform_deletes(featured_image:, thumbnails: [], resources: [], solr_document_sidecars: [], bookmarks: []) bookmarks.each { |b| b.delete } solr_document_sidecars.each do |d| Blacklight.default_index.connection.delete_by_id d.document_id @@ -74,20 +78,49 @@ def perform_deletes(featured_image, resources, solr_document_sidecars, bookmarks d.delete end resources.each { |r| r.delete } + thumbnails.each { |t| t.delete } featured_image.remove_image! featured_image.delete puts("DELETE Complete!") end - def perform_pretest(featured_image, resources, solr_document_sidecars, bookmarks) + def perform_pretest(featured_image:, thumbnails: [], resources: [], solr_document_sidecars: [], bookmarks: []) puts "-----------------------------------" puts("Will DELETE...") - puts(" featured_image - id: #{featured_image.id} type: #{featured_image.type} image: #{featured_image.image}") + thumbnail_text = test_for_thumbnail(featured_image) + puts(" featured_image - id: #{featured_image.id} type: #{featured_image.type} image: #{featured_image.image} iiif_tilesource: #{featured_image.iiif_tilesource} #{thumbnail_text}") + thumbnails.each { |t| puts(" thumbnail - id: #{t.id} type: #{featured_image.type} image: #{featured_image.image} iiif_tilesource: #{t.iiif_tilesource}") } resources.each { |r| puts(" resource - id: #{r.id} type: #{r.type} upload_id: #{r.upload_id} exhibit_id: #{r.exhibit.id}") } solr_document_sidecars.each { |d| puts(" solr_document_sidecar - id: #{d.id} document_id: #{d.document_id} document_type: #{d.document_type} resource_id: #{d.resource_id} exhibit_id: #{d.exhibit_id}") } bookmarks.each { |b| puts(" bookmark - id: #{b.id}") } end + def test_for_thumbnail(featured_image) + return '' unless ThumbnailService.thumbnail? featured_image + "THUMBNAIL from source featured_image: #{ThumbnailService.thumbnail_source_id(featured_image)}" + end + + # def thumbnail?(featured_image) + # !featured_image.iiif_tilesource.include? featured_image.id.to_s + # end + # + # def thumbnail_source_id(featured_image) + # source_id = featured_image.iiif_tilesource.scan(/.*\/images\/(.*)\/info.json/).flatten.first + # end + + def find_null_images(include_thumbnails: false, limit: 1000) + null_images = Spotlight::FeaturedImage.where(image: nil).limit(limit) + return null_images.map(&:id) if include_thumbnails + non_thumbnail_null_images = null_images.to_a.delete_if do |nfi| + next unless ThumbnailService.thumbnail?(nfi) + source_id = ThumbnailService.thumbnail_source_id(nfi) + next if source_id.blank? + source_image = Spotlight::FeaturedImage.find(source_id) + !source_image.image.nil? + end + non_thumbnail_null_images.map(&:id) + end + def find_featured_image(featured_image_id) featured_image = Spotlight::FeaturedImage.find(featured_image_id) featured_image @@ -96,6 +129,10 @@ def find_featured_image(featured_image_id) puts("featured_image NOT FOUND - id: #{featured_image_id}") if featured_image.blank? end + def find_thumbnails(featured_image) + ThumbnailService.related_thumbnails(featured_image) + end + def find_resources(featured_image_id) resources = Spotlight::Resource.where(upload_id: featured_image_id) return [] if resources.blank? @@ -126,8 +163,8 @@ def find_bookmarks(solr_document_sidecars) all_bookmarks end - def delete? featured_image, resources, auto_delete - return true if auto_delete && resources.blank? && featured_image.type.nil? + def delete? featured_image, resources, thumbnails, auto_delete + return true if auto_delete && resources.blank? && featured_image.type.nil? && thumbnails.blank? puts("Do you want to perform the deletes? [Yes, no]") gets.chomp == 'Yes' end diff --git a/scripts/resource_delete_service.rb b/scripts/resource_delete_service.rb index 1a6b7d1e..14ed2a63 100644 --- a/scripts/resource_delete_service.rb +++ b/scripts/resource_delete_service.rb @@ -5,6 +5,7 @@ # Load the script: load 'scripts/resource_delete_service.rb' # List resources with null image: ids = ResourceDeleteService.find_null_images # Delete all resources with null image: ResourceDeleteService.delete_resources(ids) +load 'scripts/thumbnail_service.rb' class ResourceDeleteService class << self @@ -16,6 +17,7 @@ def services puts 'find_resources_for_exhibit(exhibit:, limit: 1000)' puts ' # @param [Integer] exhibit id' puts ' # @return [Array] resource_ids - list of ids of resources with image=NULL' + puts 'find_resources_without_an_exhibit' puts 'count_null_images' puts ' # @return [Integer] the number of resources with image=NULL' puts 'delete_resources(resource_ids, pretest: false' @@ -47,6 +49,17 @@ def find_resources_for_exhibit(exhibit:, limit: 1000) resource_ids end + # @return [Array] resource_ids - list of ids of resources with image=NULL + def find_resources_without_an_exhibit + puts "Resources without an exhibit" + all_resource_exhibit_ids = Spotlight::Resource.all.map(&:exhibit_id).uniq.sort + all_exhibit_ids = Spotlight::Exhibit.all.map(&:id).sort + resource_exhibits_that_donot_exist = all_resource_exhibit_ids - all_exhibit_ids + resource_exhibits_that_donot_exist.each do |exhibit_id| + ExhibitService.counts exhibit_id + end + end + # @return [Integer] the number of resources with image=NULL' def count_null_images ids = find_null_images @@ -66,35 +79,45 @@ def delete_resource(resource_id, pretest: false) return if resource.blank? featured_image = find_featured_image(resource) + alt_resources_with_image = find_alternate_resource_for_image(resource) + thumbnails = find_thumbnails(featured_image) solr_document_sidecars = find_solr_document_sidecars(resource) bookmarks = find_bookmarks(solr_document_sidecars) - perform_pretest(resource, featured_image, solr_document_sidecars, bookmarks) + perform_pretest(resource: resource, featured_image: featured_image, alternate_resources_for_image: alt_resources_with_image, + thumbnails: thumbnails, solr_document_sidecars: solr_document_sidecars, bookmarks: bookmarks) return if pretest return unless delete? - perform_deletes(resource, featured_image, solr_document_sidecars, bookmarks) + perform_deletes(resource: resource, featured_image: featured_image, thumbnails: thumbnails, + solr_document_sidecars: solr_document_sidecars, bookmarks: bookmarks) end private - def perform_deletes(resource, featured_image, solr_document_sidecars, bookmarks) + def perform_deletes(resource:, featured_image:, thumbnails: [], solr_document_sidecars: [], bookmarks: []) bookmarks.each { |b| b.delete } solr_document_sidecars.each do |d| Blacklight.default_index.connection.delete_by_id d.document_id Blacklight.default_index.connection.commit d.delete end + thumbnails.each { |t| t.delete } featured_image.remove_image! if featured_image featured_image.delete if featured_image resource.delete puts("DELETE Complete!") end - def perform_pretest(resource, featured_image, solr_document_sidecars, bookmarks) + def perform_pretest(resource:, featured_image:, alternate_resources_for_image: [], thumbnails: [], solr_document_sidecars: [], bookmarks: []) puts "-----------------------------------" puts("Will DELETE...") puts(" resource - id: #{resource.id} type: #{resource.type} upload_id: #{resource.upload_id} exhibit_id: #{resource.exhibit_id}") puts(" featured image - id: #{featured_image.id} type: #{featured_image.type} image: #{featured_image.image}") if featured_image + Spotlight::Resource.where(upload_id: resource.upload_id).each do |r| + next if r.id == resource.id || resource.upload_id.blank? + puts(" ALT resource with upload_id: #{resource.upload_id} resource - id: #{r.id} exhibit_id: #{r.exhibit_id}") + end unless resource.upload_id.blank? + thumbnails.each { |t| puts(" thumbnail - id: #{t.id} type: #{t.type} image: #{t.image} iiif_tilesource: #{t.iiif_tilesource}") } solr_document_sidecars.each { |d| puts(" solr_document_sidecar - id: #{d.id} document_id: #{d.document_id} document_type: #{d.document_type} resource_id: #{d.resource_id} exhibit_id: #{d.exhibit_id}") } bookmarks.each { |b| puts(" bookmark - id: #{b.id}") } end @@ -113,6 +136,11 @@ def find_featured_image(resource) Spotlight::FeaturedImage.find(featured_image_id) end + def find_thumbnails(featured_image) + return [] if featured_image.blank? || featured_image.id.blank? + Spotlight::FeaturedImage.where("iiif_tilesource like 'http%/images/#{featured_image.id}/info.json'") + end + def find_solr_document_sidecars(resource) return [] if resource.blank? Spotlight::SolrDocumentSidecar.where(resource_id: resource.id) diff --git a/scripts/thumbnail_service.rb b/scripts/thumbnail_service.rb new file mode 100644 index 00000000..e4fc3076 --- /dev/null +++ b/scripts/thumbnail_service.rb @@ -0,0 +1,40 @@ +# Deletes all parts related to an uploaded file. !!! USE WITH CAUTION. THIS CANNOT BE UNDONE !!! + +# To use to delete all parts related to a featured_image where the file did not successfully +# upload and has NULL image: +# Run rails console: bundle exec rails c +# Load the script: load 'scripts/thumbnail_service.rb' +# List of thumbnails related to a featured image: featured_images = ThumbnailService.related_thumbnails(featured_image) + +class ThumbnailService + class << self + def services + puts 'thumbnail?(test_featured_image)' + puts ' # @param [Spotlight::FeaturedImage] the image to check whether it is a thumbnail' + puts ' # @return [Boolean] true if passed in image is a thumbnail; otherwise, false' + puts 'thumbnail_source_id(thumbnail_featured_image)' + puts ' # @param [Spotlight::FeaturedImage] the thumbnail image' + puts ' # @return [Spotlight::FeaturedImage] the image on which the thumbnail was based' + puts 'related_thumbnails(featured_image)' + puts ' # @param [Spotlight::FeaturedImage] a featured image' + puts ' # @return [Array] any thumbnail images based on the passed in image' + end + + # @param [Spotlight::FeaturedImage] the image to check whether it is a thumbnail + # @return [Boolean] true if passed in image is a thumbnail; otherwise, false + def thumbnail?(test_featured_image) + !test_featured_image.iiif_tilesource.include? test_featured_image.id.to_s + end + + # @param [Spotlight::FeaturedImage] the thumbnail image + # @return [Spotlight::FeaturedImage] the image on which the thumbnail was based + def thumbnail_source_id(thumbnail_featured_image) + thumbnail_featured_image.iiif_tilesource.scan(/.*\/images\/(.*)\/info.json/).flatten.first + end + # @param [Spotlight::FeaturedImage] a featured image + # @return [Array] any thumbnail images based on the passed in image + def related_thumbnails(featured_image) + Spotlight::FeaturedImage.where("iiif_tilesource like 'http%image/#{featured_image.id}/info.json'") + end + end +end