forked from instructure/canvas-lms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcourse_link_validator.rb
295 lines (261 loc) · 9.74 KB
/
course_link_validator.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
require 'nokogiri'
class CourseLinkValidator
TAG = "link_validation"
# retrieves the validation job
def self.current_progress(course)
Progress.where(:tag => TAG, :context_type => 'Course', :context_id => course.id).last
end
# creates a new validation job
def self.queue_course(course)
progress = current_progress(course)
return progress if progress && progress.pending?
progress ||= Progress.new(:tag => TAG, :context => course)
progress.reset!
progress.process_job(self, :process)
progress
end
def self.process(progress)
validator = self.new(progress.context)
validator.check_course(progress)
progress.set_results({:issues => validator.issues, :completed_at => Time.now.utc})
rescue
report_id = Canvas::Errors.capture_exception(:course_link_validation, $ERROR_INFO)[:error_report]
progress.workflow_state = 'failed'
progress.set_results({error_report_id: report_id, completed_at: Time.now.utc})
end
attr_accessor :course, :domain_regex, :issues, :visited_urls
def initialize(course)
self.course = course
domain = course.root_account.domain
self.domain_regex = %r{\w+:?\/\/#{domain}\/} if domain
self.issues = []
self.visited_urls = {}
end
# ****************************************************************
# this is where the magic happens
def check_course(progress)
# Syllabus
find_invalid_links(self.course.syllabus_body) do |links|
self.issues << {:name => I18n.t(:syllabus, "Course Syllabus"), :type => :syllabus,
:content_url => "/courses/#{self.course.id}/assignments/syllabus"}.merge(:invalid_links => links)
end
progress.update_completion! 5
# Assessment questions
self.course.assessment_questions.active.each do |aq|
next if aq.assessment_question_bank.deleted?
check_question(aq)
end
progress.update_completion! 15
# Assignments
self.course.assignments.active.each do |assignment|
next if assignment.quiz || assignment.discussion_topic
find_invalid_links(assignment.description) do |links|
self.issues << {:name => assignment.title, :type => :assignment,
:content_url => "/courses/#{self.course.id}/assignments/#{assignment.id}"}.merge(:invalid_links => links)
end
end
progress.update_completion! 25
# Calendar events
self.course.calendar_events.active.each do |event|
find_invalid_links(event.description) do |links|
self.issues << {:name => event.title, :type => :calendar_event,
:content_url => "/courses/#{self.course.id}/calendar_events/#{event.id}"}.merge(:invalid_links => links)
end
end
progress.update_completion! 35
# Discussion topics
self.course.discussion_topics.active.each do |topic|
find_invalid_links(topic.message) do |links|
self.issues << {:name => topic.title, :type => :discussion_topic,
:content_url => "/courses/#{self.course.id}/discussion_topics/#{topic.id}"}.merge(:invalid_links => links)
end
end
progress.update_completion! 55
# External URL Module items (almost forgot about these)
self.course.context_module_tags.not_deleted.where(:content_type => "ExternalUrl").each do |ct|
find_invalid_link(ct.url) do |invalid_link|
self.issues << {:name => ct.title, :type => :module_item,
:content_url => "/courses/#{self.course.id}/modules"}.merge(:invalid_links => [invalid_link])
end
end
progress.update_completion! 65
# Quizzes
self.course.quizzes.active.each do |quiz|
find_invalid_links(quiz.description) do |links|
self.issues << {:name => quiz.title, :type => :quiz,
:content_url => "/courses/#{self.course.id}/quizzes/#{quiz.id}"}.merge(:invalid_links => links)
end
quiz.quiz_questions.each do |qq|
check_question(qq)
end
end
progress.update_completion! 85
# Wiki pages
self.course.wiki.wiki_pages.not_deleted.each do |page|
find_invalid_links(page.body) do |links|
self.issues << {:name => page.title, :type => :wiki_page,
:content_url => "/courses/#{self.course.id}/pages/#{page.url}"}.merge(:invalid_links => links)
end
end
progress.update_completion! 99
end
def check_question(question)
# Assessment/Quiz Questions
links = []
[:question_text, :correct_comments_html, :incorrect_comments_html, :neutral_comments_html, :more_comments_html].each do |field|
find_invalid_links(question.question_data[field]) do |field_links|
links += field_links
end
end
(question.question_data[:answers] || []).each_with_index do |answer, i|
[:html, :comments_html, :left_html].each do |field|
find_invalid_links(answer[field]) do |field_links|
links += field_links
end
end
end
if links.any?
hash = {:name => question.question_data[:question_name]}.merge(:invalid_links => links)
case question
when AssessmentQuestion
hash[:type] = :assessment_question
hash[:content_url] = "/courses/#{self.course.id}/question_banks/#{question.assessment_question_bank_id}#question_#{question.id}_question_text"
when Quizzes::QuizQuestion
hash[:type] = :quiz_question
hash[:content_url] = "/courses/#{self.course.id}/quizzes/#{question.quiz_id}/take?preview=1#question_#{question.id}"
end
issues << hash
end
end
# pretty much copied from ImportedHtmlConverter
def find_invalid_links(html)
links = []
doc = Nokogiri::HTML(html || "")
attrs = ['rel', 'href', 'src', 'data', 'value']
doc.search("*").each do |node|
attrs.each do |attr|
url = node[attr]
next unless url.present?
if attr == 'value'
next unless node['name'] && node['name'] == 'src'
end
find_invalid_link(url) do |invalid_link|
links << invalid_link
end
end
end
yield links if links.any?
end
ITEM_CLASSES = {
'assignments' => Assignment,
'announcements' => Announcement,
'calendar_events' => CalendarEvent,
'discussion_topics' => DiscussionTopic,
'collaborations' => Collaboration,
'files' => Attachment,
'quizzes' => Quizzes::Quiz,
'groups' => Group,
'wiki' => WikiPage,
'pages' => WikiPage,
'modules' => ContextModule,
'items' => ContentTag
}
# yields a hash containing the url and an error type if the url is invalid
def find_invalid_link(url)
unless result = self.visited_urls[url]
begin
if ImportedHtmlConverter.relative_url?(url) || (self.domain_regex && url.match(self.domain_regex))
if valid_route?(url)
if url.match(/\/courses\/(\d+)/) && self.course.id.to_s != $1
result = :course_mismatch
else
result = check_object_status(url)
end
else
result = :unreachable
end
elsif !url.start_with?('mailto:')
unless reachable_url?(url)
result = :unreachable
end
end
rescue URI::Error
result = :unparsable
end
result ||= :success
self.visited_urls[url] = result
end
unless result == :success
invalid_link = {:url => url, :reason => result}
yield invalid_link
end
end
# checks against the Rails routes to see if the url matches anything
def valid_route?(url)
path = URI.parse(url).path
path = path.chomp("/")
@route_set ||= ::Rails.application.routes.set.routes.select{|r| r.verb === "GET"}
@route_set.any?{|r| r.path.match(path)} || (!Pathname(path).each_filename.include?('..') && File.exists?(File.join(Rails.root, "public", path)))
end
# makes sure that links to course objects exist and are in a visible state
def check_object_status(url)
result = nil
case url
when /\/courses\/\d+\/file_contents\/(.*)/
rel_path = CGI.unescape($1)
unless (att = Folder.find_attachment_in_context_with_path(self.course, rel_path)) && !att.deleted?
result = :missing_file
end
when /\/courses\/\d+\/(pages|wiki)\/([^\s"<'\?\/#]*)/
if obj = self.course.wiki.find_page(CGI.unescape($2))
if obj.workflow_state == 'unpublished'
result = :unpublished_item
end
else
result = :missing_item
end
when /\/courses\/\d+\/(.*)\/(\d+)/
obj_type = $1
obj_id = $2
if obj_class = ITEM_CLASSES[obj_type]
if (obj_class == Attachment) && (obj = self.course.attachments.find_by_id(obj_id)) # attachments.find_by_id uses the replacement hackery
if obj.file_state == 'deleted'
result = :missing_item
elsif obj.locked?
result = :unpublished_item
end
elsif (obj = obj_class.where(:id => obj_id).first)
if obj.workflow_state == 'deleted'
result = :missing_item
elsif obj.workflow_state == 'unpublished'
result = :unpublished_item
end
else
result = :missing_item
end
end
end
result
end
# ping the url and make sure we get a 200
def reachable_url?(url)
begin
response = CanvasHttp.head(url, { "Accept-Encoding" => "gzip" }, redirect_limit: 9)
if %w{404 405}.include?(response.code)
response = CanvasHttp.get(url, { "Accept-Encoding" => "gzip" }, redirect_limit: 9)
end
case response.code
when /^2/ # 2xx code
true
when "401", "403", "503"
# we accept unauthorized and forbidden codes here because sometimes servers refuse to serve our requests
# and someone can link to a site that requires authentication anyway - doesn't necessarily make it invalid
true
else
false
end
rescue
false
end
end
end