Current location

narf Source control manager Git

summaryrefslogtreecommitdiff
blob: b00264a5d257e34c5914cf8db1b83e50d5e97690 (plain)
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
#!/usr/bin/env ruby19

require 'rubygems'
require 'yaml'
require 'active_record'
require 'sqlite3'
require 'mysql'


# Models generated by RMRE [0]
# [0] https://github.com/bosko/rmre
settings_file = './database.yml'
# The yaml file should look like that
# 
#  blogsum:
#    adapter: sqlite3
#     database: /PATH/TO/site.db
#  
#  wordpress:
#    adapter: mysql
#    encoding: utf8
#    username: wordpress
#    password:
#    database: blog
#    
exit unless File.exists?(settings_file)
$settings = YAML.load_file(settings_file)

# Blogsum
class Article < ActiveRecord::Base
  establish_connection $settings["blogsum"]
  has_many :comment
end
class Comment < ActiveRecord::Base
  establish_connection $settings["blogsum"]
  belongs_to :article
end

# Wordpress
class WpComment < ActiveRecord::Base
  establish_connection $settings["wordpress"]
  self.primary_key = :comment_ID
  has_many :wp_commentmeta
  belongs_to :wp_post
end
class WpCommentmetum < ActiveRecord::Base
  establish_connection $settings["wordpress"]
  self.primary_key = :meta_id
  belongs_to :wp_comment, foreign_key: :comment_id
end
class WpPost < ActiveRecord::Base
  establish_connection $settings["wordpress"]
  self.primary_key = :ID
  has_many :wp_postmeta, foreign_key: :post_id
  has_many :wp_comment, foreign_key: :comment_post_ID
  has_many :wp_term_relationships, foreign_key: :object_id
  has_many :wp_term_taxonomy, through: :wp_term_relationships
  has_many :wp_term, through: :wp_term_taxonomy
end
class WpPostmetum < ActiveRecord::Base
  establish_connection $settings["wordpress"]
  self.primary_key = :meta_id
  belongs_to :wp_post
end
class WpTerm < ActiveRecord::Base
  establish_connection $settings["wordpress"]
  self.primary_key = :term_id
  has_one :wp_term_taxonomy
end
class WpTermRelationship < ActiveRecord::Base
  establish_connection $settings["wordpress"]
  belongs_to :wp_post
  belongs_to :wp_term_taxonomy, foreign_key: :term_taxonomy_id
end
class WpTermTaxonomy < ActiveRecord::Base
  establish_connection $settings["wordpress"]
  self.table_name = 'wp_term_taxonomy'
  self.primary_key = :term_taxonomy_id
  belongs_to :wp_term, foreign_key: :term_id
  has_many :wp_term_relationships
  has_many :wp_post, through: :wp_term_relationships
end

# Blogsum
puts Article.find(1).title
puts Article.find(3).comment[0].comment

#Wordpress
puts WpPost.find(1).post_title
puts WpPost.find(1).wp_comment[0].comment_content
puts WpPost.find(1).wp_term_taxonomy
puts WpPost.find(7).wp_term.map {|t| "#{t.name}, "}