drupal-exporter 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (57) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +39 -0
  3. data/.rspec +3 -0
  4. data/.travis.yml +8 -0
  5. data/CHANGELOG.md +4 -0
  6. data/Gemfile +7 -0
  7. data/Gemfile.lock +72 -0
  8. data/LICENSE +22 -0
  9. data/README.md +251 -0
  10. data/Rakefile +9 -0
  11. data/bin/drupal-exporter +37 -0
  12. data/drupal_exporter.gemspec +34 -0
  13. data/drupal_settings/boolean_columns.yml +2 -0
  14. data/drupal_settings/drupal_content_types.json +47 -0
  15. data/drupal_settings/drupal_settings.yml +23 -0
  16. data/lib/cli.rb +13 -0
  17. data/lib/configuration.rb +46 -0
  18. data/lib/converters/content_types_structure_creator.rb +60 -0
  19. data/lib/converters/contentful_model_to_json.rb +109 -0
  20. data/lib/drupal/content_type.rb +151 -0
  21. data/lib/drupal/export.rb +69 -0
  22. data/lib/drupal/file_managed.rb +42 -0
  23. data/lib/drupal/tag.rb +52 -0
  24. data/lib/drupal/user.rb +46 -0
  25. data/lib/drupal/vocabulary.rb +42 -0
  26. data/lib/migrator.rb +28 -0
  27. data/lib/version.rb +3 -0
  28. data/spec/fixtures/database_rows/content_type_article.json +14 -0
  29. data/spec/fixtures/database_rows/image.json +10 -0
  30. data/spec/fixtures/database_rows/node_content_type_article.json +15 -0
  31. data/spec/fixtures/database_rows/node_content_type_blog.json +15 -0
  32. data/spec/fixtures/database_rows/tag.json +8 -0
  33. data/spec/fixtures/database_rows/user.json +18 -0
  34. data/spec/fixtures/database_rows/vocabulary.json +9 -0
  35. data/spec/fixtures/drupal/assets/file/file_4.json +6 -0
  36. data/spec/fixtures/drupal/entries/article/article_5.json +24 -0
  37. data/spec/fixtures/drupal/entries/tag/tag_1.json +9 -0
  38. data/spec/fixtures/drupal/entries/user/user_1.json +6 -0
  39. data/spec/fixtures/drupal/entries/vocabulary/vocabulary_3.json +6 -0
  40. data/spec/fixtures/json_responses/article.json +24 -0
  41. data/spec/fixtures/json_responses/image.json +6 -0
  42. data/spec/fixtures/json_responses/tag.json +9 -0
  43. data/spec/fixtures/json_responses/vocabulary.json +6 -0
  44. data/spec/fixtures/settings/boolean_columns.yml +2 -0
  45. data/spec/fixtures/settings/drupal_content_types.json +47 -0
  46. data/spec/fixtures/settings/drupal_settings.yml +17 -0
  47. data/spec/lib/configuration_spec.rb +18 -0
  48. data/spec/lib/drupal/content_type_spec.rb +123 -0
  49. data/spec/lib/drupal/export_spec.rb +33 -0
  50. data/spec/lib/drupal/file_managed_spec.rb +52 -0
  51. data/spec/lib/drupal/tag_spec.rb +60 -0
  52. data/spec/lib/drupal/user_spec.rb +49 -0
  53. data/spec/lib/drupal/vocabulary_spec.rb +51 -0
  54. data/spec/spec_helper.rb +11 -0
  55. data/spec/support/db_rows_json.rb +15 -0
  56. data/spec/support/shared_configuration.rb +20 -0
  57. metadata +297 -0
@@ -0,0 +1,69 @@
1
+ require 'fileutils'
2
+ require 'json'
3
+ require 'yaml'
4
+
5
+ require_relative 'tag'
6
+ require_relative 'vocabulary'
7
+ require_relative 'user'
8
+ require_relative 'content_type'
9
+ require_relative 'file_managed'
10
+
11
+ module Contentful
12
+ module Exporter
13
+ module Drupal
14
+ class Export
15
+
16
+ attr_reader :config, :boolean_columns
17
+
18
+ def initialize(settings)
19
+ @config = settings
20
+ @boolean_columns = []
21
+ end
22
+
23
+ def save_data_as_json
24
+ boolean_columns << YAML.load_file(config.config['drupal_boolean_columns']) if config.config['drupal_boolean_columns']
25
+ tags
26
+ vocabularies
27
+ users
28
+ content_types
29
+ files
30
+ end
31
+
32
+ def write_json_to_file(path, data)
33
+ File.open(path, 'w') do |file|
34
+ file.write(JSON.pretty_generate(data))
35
+ end
36
+ end
37
+
38
+ def create_directory(path)
39
+ FileUtils.mkdir_p(path) unless File.directory?(path)
40
+ end
41
+
42
+ private
43
+
44
+ def tags
45
+ Tag.new(self, config).save_tags_as_json
46
+ end
47
+
48
+ def vocabularies
49
+ Vocabulary.new(self, config).save_vocabularies_as_json
50
+ end
51
+
52
+ def users
53
+ User.new(self, config).save_users_as_json
54
+ end
55
+
56
+ def files
57
+ FileManaged.new(self, config).save_files_as_json
58
+ end
59
+
60
+ def content_types
61
+ config.drupal_content_types.each do |content_type, schema|
62
+ ContentType.new(self, config, content_type, schema).save_content_types_as_json
63
+ end
64
+ end
65
+
66
+ end
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,42 @@
1
+ module Contentful
2
+ module Exporter
3
+ module Drupal
4
+ class FileManaged
5
+
6
+ attr_reader :exporter, :config
7
+
8
+ def initialize(exporter, config)
9
+ @exporter, @config = exporter, config
10
+ end
11
+
12
+ def save_files_as_json
13
+ exporter.create_directory("#{config.assets_dir}/file")
14
+ config.db[:file_managed].each do |file_row|
15
+ extract_data(file_row)
16
+ end
17
+ end
18
+
19
+ private
20
+
21
+ def extract_data(file_row)
22
+ puts "Saving file - id: #{file_row[:fid]}"
23
+ db_object = map_fields(file_row)
24
+ exporter.write_json_to_file("#{config.assets_dir}/file/#{db_object[:id]}.json", db_object)
25
+ end
26
+
27
+ def map_fields(row, result = {})
28
+ result[:id] = id(row[:fid])
29
+ result[:title] = row[:filename]
30
+ result[:description] = row[:description]
31
+ result[:url] = "#{config.drupal_base_url}/#{row[:uri].gsub(' ', '%20').gsub('public://','sites/default/files/')}"
32
+ result
33
+ end
34
+
35
+ def id(file_id)
36
+ "file_#{file_id}"
37
+ end
38
+
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,52 @@
1
+ module Contentful
2
+ module Exporter
3
+ module Drupal
4
+ class Tag
5
+
6
+ attr_reader :exporter, :config
7
+
8
+ def initialize(exporter, config)
9
+ @exporter, @config = exporter, config
10
+ end
11
+
12
+ def save_tags_as_json
13
+ exporter.create_directory("#{config.entries_dir}/tag")
14
+ config.db[:taxonomy_term_data].each do |tag_row|
15
+ extract_data(tag_row)
16
+ end
17
+ end
18
+
19
+ private
20
+
21
+ def extract_data(tag_row)
22
+ puts "Saving tag - id: #{tag_row[:tid]}"
23
+ db_object = map_fields(tag_row)
24
+ exporter.write_json_to_file("#{config.entries_dir}/tag/#{db_object[:id]}.json", db_object)
25
+ end
26
+
27
+ def map_fields(row, result = {})
28
+ result[:id] = id(row[:tid])
29
+ result[:name] = row[:name]
30
+ result[:description] = row[:description]
31
+ result[:vocabulary] = vocabulary(row[:vid])
32
+ result
33
+ end
34
+
35
+ def id(tag_id)
36
+ 'tag_' + tag_id.to_s
37
+ end
38
+
39
+ def vocabulary(tag_vocabulary_id)
40
+ vocabulary = tag_vocabulary(tag_vocabulary_id)
41
+ {type: 'EntryVocabulary', id: "vocabulary_#{vocabulary[:vid]}"}
42
+ end
43
+
44
+ def tag_vocabulary(tag_vocabulary_id)
45
+ config.db[:taxonomy_vocabulary].where(vid: tag_vocabulary_id).first
46
+ end
47
+
48
+ end
49
+ end
50
+ end
51
+ end
52
+
@@ -0,0 +1,46 @@
1
+ module Contentful
2
+ module Exporter
3
+ module Drupal
4
+ class User
5
+
6
+ attr_reader :exporter, :config
7
+
8
+ def initialize(exporter, config)
9
+ @exporter, @config = exporter, config
10
+ end
11
+
12
+ def save_users_as_json
13
+ exporter.create_directory("#{config.entries_dir}/user")
14
+ config.db[:users].each do |user_row|
15
+ extract_data(user_row)
16
+ end
17
+ end
18
+
19
+ private
20
+
21
+ def extract_data(user_row)
22
+ puts "Saving user - id: #{user_row[:uid]}"
23
+ db_object = map_fields(user_row)
24
+ exporter.write_json_to_file("#{config.entries_dir}/user/#{db_object[:id]}.json", db_object)
25
+ end
26
+
27
+ def map_fields(row, result = {})
28
+ result[:id] = id(row[:uid])
29
+ result[:name] = row[:name]
30
+ result[:email] = row[:mail]
31
+ result[:created_at] = created_at(row[:created])
32
+ result
33
+ end
34
+
35
+ def id(user_id)
36
+ "user_#{user_id}"
37
+ end
38
+
39
+ def created_at(timestamp)
40
+ Time.at(timestamp).to_datetime
41
+ end
42
+
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,42 @@
1
+ module Contentful
2
+ module Exporter
3
+ module Drupal
4
+ class Vocabulary
5
+
6
+ attr_reader :exporter, :config
7
+
8
+ def initialize(exporter, config)
9
+ @exporter, @config = exporter, config
10
+ end
11
+
12
+ def save_vocabularies_as_json
13
+ exporter.create_directory("#{config.entries_dir}/vocabulary")
14
+ config.db[:taxonomy_vocabulary].each do |vocabulary_row|
15
+ extract_data(vocabulary_row)
16
+ end
17
+ end
18
+
19
+ private
20
+
21
+ def extract_data(vocabulary_row)
22
+ puts "Saving vocabulary - id: #{vocabulary_row[:vid]}"
23
+ db_object = map_fields(vocabulary_row)
24
+ exporter.write_json_to_file("#{config.entries_dir}/vocabulary/#{db_object[:id]}.json", db_object)
25
+ end
26
+
27
+ def map_fields(row, result = {})
28
+ result[:id] = id(row[:vid])
29
+ result[:name] = row[:name]
30
+ result[:description] = row[:description]
31
+ result[:machine_name] = row[:machine_name]
32
+ result
33
+ end
34
+
35
+ def id(vocabulary_id)
36
+ "vocabulary_#{vocabulary_id}"
37
+ end
38
+
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,28 @@
1
+ require_relative 'drupal/export'
2
+ require_relative 'converters/contentful_model_to_json'
3
+ require_relative 'configuration'
4
+
5
+
6
+ class Migrator
7
+
8
+ attr_reader :exporter, :config, :converter
9
+
10
+ def initialize(settings)
11
+ @config = Contentful::Configuration.new(settings)
12
+ @exporter = Contentful::Exporter::Drupal::Export.new(config)
13
+ @converter = Contentful::Converter::ContentfulModelToJson.new(config)
14
+ end
15
+
16
+ def run(action)
17
+ case action.to_s
18
+ when '--extract-to-json'
19
+ exporter.save_data_as_json
20
+ when '--convert-content-model-to-json'
21
+ converter.convert_to_import_form
22
+ when '--create-contentful-model-from-json'
23
+ converter.create_content_type_json
24
+ else
25
+ fail ArgumentError, 'You have entered incorrect action! View README'
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,3 @@
1
+ module Version
2
+ VERSION = '0.0.1'
3
+ end
@@ -0,0 +1,14 @@
1
+ {
2
+ "entity_type": "node",
3
+ "bundle": "article",
4
+ "deleted": 0,
5
+ "entity_id": 5,
6
+ "revision_id": 5,
7
+ "language": "und",
8
+ "delta": 0,
9
+ "field_image_fid": 1,
10
+ "field_image_alt": "alternate text",
11
+ "field_image_title": "",
12
+ "field_image_width": 449,
13
+ "field_image_height": 441
14
+ }
@@ -0,0 +1,10 @@
1
+ {
2
+ "fid": 1,
3
+ "uid": 1,
4
+ "filename": "Screen Shot 2014-11-27 at 12.34.47 PM.png",
5
+ "uri": "public://field/image/Screen Shot 2014-11-27 at 12.34.47 PM.png",
6
+ "filemime": "image/png",
7
+ "filesize": 251362,
8
+ "status": 1,
9
+ "timestamp": 1417520179
10
+ }
@@ -0,0 +1,15 @@
1
+ {
2
+ "nid": 5,
3
+ "vid": 5,
4
+ "type": "article",
5
+ "language": "und",
6
+ "title": "Article first title",
7
+ "uid": 1,
8
+ "status": 1,
9
+ "created": 1417520179,
10
+ "changed": 1417545589,
11
+ "promote": 1,
12
+ "sticky": 0,
13
+ "tnid": 0,
14
+ "translate": 0
15
+ }
@@ -0,0 +1,15 @@
1
+ {
2
+ "nid": 2,
3
+ "vid": 2,
4
+ "type": "blog",
5
+ "language": "und",
6
+ "title": "Blog first entry",
7
+ "uid": 1,
8
+ "status": 1,
9
+ "created": 1417519531,
10
+ "changed": 1417519531,
11
+ "promote": 1,
12
+ "sticky": 0,
13
+ "tnid": 0,
14
+ "translate": 0
15
+ }
@@ -0,0 +1,8 @@
1
+ {
2
+ "tid": 1,
3
+ "vid": 1,
4
+ "name": "article tag",
5
+ "description": "desc",
6
+ "format": null,
7
+ "weight": 0
8
+ }
@@ -0,0 +1,18 @@
1
+ {
2
+ "uid": 1,
3
+ "name": "username",
4
+ "pass": "password",
5
+ "mail": "useremail@gmail.com",
6
+ "theme": "",
7
+ "signature": "",
8
+ "signature_format": null,
9
+ "created": 1417517150,
10
+ "access": 1418026959,
11
+ "login": 1417605965,
12
+ "status": 1,
13
+ "timezone": "Europe/Berlin",
14
+ "language": "",
15
+ "picture": 0,
16
+ "init": "useremail@gmail.com",
17
+ "data": "b:0;"
18
+ }
@@ -0,0 +1,9 @@
1
+ {
2
+ "vid": 3,
3
+ "name": "tag_name",
4
+ "machine_name": "bad",
5
+ "description": "Very bad tag",
6
+ "hierarchy": 0,
7
+ "module": "taxonomy",
8
+ "weight": 0
9
+ }
@@ -0,0 +1,6 @@
1
+ {
2
+ "id": "file_4",
3
+ "title": "ruby.png",
4
+ "description": null,
5
+ "url": "http://example_hostname.com/sites/default/files/ruby.png"
6
+ }
@@ -0,0 +1,24 @@
1
+ {
2
+ "id": "article_5",
3
+ "title": "Article first title",
4
+ "author": {
5
+ "type": "Author",
6
+ "id": "user_1"
7
+ },
8
+ "tags": [
9
+ {
10
+ "type": "EntryTag",
11
+ "id": "tag_1"
12
+ },
13
+ {
14
+ "type": "EntryTag",
15
+ "id": "tag_2"
16
+ }
17
+ ],
18
+ "created_at": "2014-12-02T12:36:19+01:00",
19
+ "body": "Article first body!",
20
+ "image": {
21
+ "type": "File",
22
+ "id": "file_1"
23
+ }
24
+ }
@@ -0,0 +1,9 @@
1
+ {
2
+ "id": "tag_1",
3
+ "name": "article tag",
4
+ "description": "desc",
5
+ "vocabulary": {
6
+ "type": "EntryVocabulary",
7
+ "id": "vocabulary_1"
8
+ }
9
+ }
@@ -0,0 +1,6 @@
1
+ {
2
+ "id": "user_1",
3
+ "name": "username",
4
+ "email": "useremail@gmail.com",
5
+ "created_at": "2014-12-02T11:45:50+01:00"
6
+ }
@@ -0,0 +1,6 @@
1
+ {
2
+ "id": "vocabulary_3",
3
+ "name": "tag_name",
4
+ "description": "Very bad tag",
5
+ "machine_name": "bad"
6
+ }