Class: Moodle2CC::Moodle2::Parsers::FileParser

Inherits:
Object
  • Object
show all
Includes:
ParserHelper
Defined in:
lib/moodle2cc/moodle2/parsers/file_parser.rb

Constant Summary collapse

FILES_XML =
'files.xml'
NULL_XML_VALUE =
'$@NULL@$'
FILES_DIR =
'files'

Constants included from ParserHelper

ParserHelper::IMS_FILEBASE_TOKEN, ParserHelper::MODULE_XML, ParserHelper::MOODLE_FILEBASE_TOKEN, ParserHelper::SLASH_TOKEN, ParserHelper::XML_NULL_VALUE

Instance Method Summary collapse

Methods included from ParserHelper

#activity_directories, #parse_boolean, #parse_module, #parse_text

Constructor Details

#initialize(work_dir) ⇒ FileParser

Returns a new instance of FileParser.



9
10
11
# File 'lib/moodle2cc/moodle2/parsers/file_parser.rb', line 9

def initialize(work_dir)
  @work_dir = work_dir
end

Instance Method Details

#parseObject



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
# File 'lib/moodle2cc/moodle2/parsers/file_parser.rb', line 13

def parse
  xml = Nokogiri::XML(File.read(File.join(@work_dir, FILES_XML)))
  file_nodes = xml./('files/file')
  files = []
  missing_files = []
  file_nodes.each do |node|
    file = Moodle2CC::Moodle2::Models::Moodle2File.new
    file.id = node.at_xpath('@id').value
    file.content_hash = parse_text(node, 'contenthash')
    file.context_id = parse_text(node, 'contextid')
    file.component = parse_text(node, 'component')
    file.file_area = parse_text(node, 'filearea')
    file.item_id = parse_text(node, 'itemid')
    file.file_path = parse_text(node, 'filepath')
    file.file_name = parse_text(node, 'filename')
    file.user_id = parse_text(node, 'userid')
    file.file_size = parse_text(node, 'filesize')
    file.mime_type = parse_text(node, 'mimetype')
    file.status = parse_text(node, 'status')
    file.time_created = parse_text(node, 'timecreated')
    file.time_modified = parse_text(node, 'timemodified')
    file.source = parse_text(node, 'source')
    file.author = parse_text(node, 'author')
    file.license = parse_text(node, 'license')
    file.sort_order = parse_text(node, 'sortorder')
    file.repository_type = parse_text(node, 'repositorytype')
    file.repository_id = parse_text(node, 'repositoryid')
    file.reference = parse_text(node, 'reference')
    file.file_location = File.join(@work_dir, FILES_DIR, file.content_hash[0..1], file.content_hash)
    if file.file_size > 0 && File.exist?(file.file_location)
      files << file
    else
      missing_files << file
    end
  end
  [files, missing_files]
end