2015-04-09 01:21:08 +08:00
|
|
|
require 'nokogiri'
|
|
|
|
|
2013-04-06 06:06:59 +08:00
|
|
|
module DataFixup::FixBrokenFileLinksInAssignments
|
2013-04-17 11:25:00 +08:00
|
|
|
|
2013-04-13 05:47:24 +08:00
|
|
|
def self.broken_assignment_scope
|
2013-04-17 11:25:00 +08:00
|
|
|
# This date is the date that g/16823 was deployed to beta and production
|
2013-04-06 06:06:59 +08:00
|
|
|
Assignment.where(["context_type = ? AND updated_at > ? AND
|
|
|
|
(description LIKE ? OR description LIKE ? OR description LIKE ?)",
|
2013-04-13 05:47:24 +08:00
|
|
|
"Course", "2013-03-08", "%verifier%", "%'/files/%", "%\"/files/%"])
|
|
|
|
end
|
2013-04-17 11:25:00 +08:00
|
|
|
|
2013-04-13 05:47:24 +08:00
|
|
|
def self.run
|
|
|
|
broken_assignment_scope.find_in_batches do |assignments|
|
2013-04-17 11:25:00 +08:00
|
|
|
# When the topic is saved it can't find the assignment because the
|
|
|
|
# find_in_batches scope is still in effect, make it have an exclusive scope
|
2013-07-17 00:27:51 +08:00
|
|
|
assignments.each do |assignment|
|
|
|
|
check_and_fix_assignment_description(assignment)
|
2013-04-06 06:06:59 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
COURSE_FILES_REGEX = %r{\A/courses/\d+/files/(\d+)/download}
|
|
|
|
FILES_REGEX = %r{\A/files/(\d+)/download}
|
|
|
|
|
2013-04-13 05:47:24 +08:00
|
|
|
# see spec for examples of how the urls are changed
|
2013-04-06 06:06:59 +08:00
|
|
|
def self.check_and_fix_assignment_description(assignment)
|
|
|
|
attrs = ['href', 'src']
|
|
|
|
changed = false
|
|
|
|
doc = Nokogiri::HTML(assignment.description)
|
|
|
|
|
|
|
|
doc.search("*").each do |node|
|
|
|
|
attrs.each do |attr|
|
|
|
|
if node[attr]
|
|
|
|
course_id = nil
|
|
|
|
file_id = nil
|
|
|
|
|
|
|
|
if node[attr] =~ COURSE_FILES_REGEX
|
|
|
|
file_id = $1
|
|
|
|
elsif node[attr] =~ FILES_REGEX
|
|
|
|
file_id = $1
|
|
|
|
end
|
|
|
|
|
|
|
|
if file_id
|
2014-09-18 01:29:06 +08:00
|
|
|
if att = Attachment.where(id: file_id).first
|
2013-04-13 05:47:24 +08:00
|
|
|
# this find returns the passed in att if nothing found in the context
|
|
|
|
# and sometimes URI.unescape errors so ignore that
|
|
|
|
att = att.context.attachments.find(att.id) rescue att
|
2013-04-06 06:06:59 +08:00
|
|
|
if att.context_type == "Course" && att.context_id == assignment.context_id
|
|
|
|
course_id = assignment.context_id
|
|
|
|
file_id = att.id
|
2014-09-18 01:29:06 +08:00
|
|
|
elsif att.cloned_item_id && cloned_att = assignment.context.attachments.where(cloned_item_id: att.cloned_item_id).first
|
2013-04-06 06:06:59 +08:00
|
|
|
course_id = assignment.context_id
|
2013-04-13 05:47:24 +08:00
|
|
|
file_id = assignment.context.attachments.find(cloned_att.id).id rescue cloned_att.id
|
2013-04-06 06:06:59 +08:00
|
|
|
elsif att.context_type == "Course"
|
|
|
|
course_id = att.context_id
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if course_id
|
|
|
|
if assignment.context_id == course_id.to_i
|
|
|
|
node[attr] = "/courses/#{course_id}/files/#{file_id}/download?wrap=1"
|
|
|
|
changed = true
|
|
|
|
elsif node[attr] =~ FILES_REGEX
|
|
|
|
node[attr] = node[attr].sub("/files/#{file_id}", "/courses/#{course_id}/files/#{file_id}")
|
|
|
|
changed = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if changed
|
2013-04-17 11:25:00 +08:00
|
|
|
Assignment.where(:id => assignment).update_all(:description => doc.at_css('body').inner_html)
|
2013-04-06 06:06:59 +08:00
|
|
|
end
|
2013-04-17 11:25:00 +08:00
|
|
|
rescue
|
2013-04-13 05:47:24 +08:00
|
|
|
Rails.logger.error "FixBrokenFileLinksInAssignments couldn't fix #{assignment.id}"
|
2013-04-06 06:06:59 +08:00
|
|
|
end
|
|
|
|
end
|