Created
January 22, 2012 18:01
-
-
Save akitsukada/1657913 to your computer and use it in GitHub Desktop.
wordpress_dummy_data
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# -*- coding: utf-8 -*- | |
require 'active_record' | |
require 'pp' | |
require 'cgi' | |
class Configs# {{{ | |
attr_reader :table_prefix, :db_adapter, :db_host, | |
:db_user, :db_password, :db_name, :charset, | |
:category_count, :tag_count, :entry_count, :comment_count, :user_count | |
def initialize# {{{ | |
#configs | |
@table_prefix = 'WpInfrapp' | |
@db_adapter = 'mysql' | |
@db_host = 'localhost' | |
@db_user = 'wordpress' | |
@db_password = 'wordpress' | |
@db_name = 'wordpress' | |
@charset = 'utf8' | |
@category_count = 30 | |
@tag_count = 500 | |
@entry_count = 100000 | |
#@entry_count = 10 | |
@comment_count = 500 | |
@user_count = 3000 | |
#@user_count = 30 | |
end# }}} | |
end# }}} | |
class Initializer | |
def initialize | |
load_config | |
@authors = [] | |
@category_usage= {} | |
@tag_usage = {} | |
end | |
def load_config | |
@conf = Configs.new | |
end | |
def run# {{{ | |
conn | |
define_models | |
set_up_tables | |
end# }}} | |
def set_up_tables# {{{ | |
clean_tables | |
begin | |
ActiveRecord::Base.transaction do | |
set_up_categories | |
set_up_tags | |
set_up_users | |
end | |
set_up_posts | |
ActiveRecord::Base.transaction do | |
apply_category_counts | |
apply_tag_counts | |
end | |
rescue => e | |
pp e | |
end | |
end# }}} | |
def clean_tables | |
WpInfrappComment.delete_all('comment_ID > 10') | |
WpInfrappPost.delete_all('id > 10') | |
WpInfrappTerm.delete_all('term_id > 10') | |
WpInfrappTermTaxonomy.delete_all('term_id > 10') | |
WpInfrappUser.delete_all('id > 10') | |
WpInfrappUsermetum.delete_all('user_id > 10') | |
WpInfrappTermRelationship.delete_all('term_taxonomy_id > 10') | |
end | |
def set_up_categories# {{{ | |
puts "#{__method__} => " + Time.now.to_s | |
category_ids = [] | |
@conf.category_count.times do |i| | |
old_cat = WpInfrappTerm.where("slug = 'cat_#{i}'").first | |
old_cat.destroy if old_cat | |
new_cat = WpInfrappTerm.new( | |
:name => "カテゴリー#{i}", | |
:slug => "cat_#{i}", | |
:term_group => 0 | |
) | |
new_cat.save! | |
parent_id = (i > 0 && rand > 0.7) ? | |
category_ids[rand(category_ids.length)] : 0 | |
category_ids << new_cat.term_id | |
new_cat.wp_infrapp_term_taxonomy.create( | |
:term_id => new_cat.id, | |
:taxonomy => 'category', | |
:description => "カテゴリー#{i}", | |
:parent => parent_id, | |
:count => 0 | |
) | |
@category_usage[new_cat.id] = 0 | |
puts "saved:#{i}" if i % 5 == 0 | |
end | |
end# }}} | |
def set_up_tags# {{{ | |
puts "#{__method__} => " + Time.now.to_s | |
@conf.tag_count.times do |i| | |
old_tag = WpInfrappTerm.where("slug = 'tag_#{i}'").first | |
old_tag.destroy if old_tag | |
new_tag = WpInfrappTerm.new( | |
:name => "タグ#{i}", | |
:slug => "tag_#{i}", | |
:term_group => 0 | |
) | |
new_tag.save! | |
new_tag.wp_infrapp_term_taxonomy.create( | |
:term_id => new_tag.id, | |
:taxonomy => 'post_tag', | |
:description => "タグ#{i}", | |
:parent => 0, | |
:count => 0 | |
) | |
@tag_usage[new_tag.id] = 0 | |
puts "saved:#{i}" if i % 100 == 0 | |
end | |
end# }}} | |
def set_up_users# {{{ | |
puts "#{__method__} => " + Time.now.to_s | |
@conf.user_count.times do |i| | |
old_user = WpInfrappUser.where("user_login = 'user_#{i}'").first; | |
old_user.destroy if old_user | |
new_user = WpInfrappUser.new( | |
:user_login => "user_#{i}", | |
:user_pass => "$P$BUHcTwB3QO36RfUPIxKOSc6WcyyUiM.", # == hash('password') | |
:user_nicename => "user_#{i}", | |
:user_email => "user_#{i}@#{i}.example.com", | |
:user_url => "http://user_#{i}.example.com", | |
:user_registered => Time.now - (rand(50000000)), | |
:user_activation_key => "", | |
:user_status => "0", | |
:display_name => "ユーザー#{i}", | |
) | |
new_user.save! | |
rand_by_3 = rand(3) | |
meta_datas = { | |
:first_name => "user_#{i}_first_name", | |
:last_name => "user_#{i}_first_name", | |
:nickname => "user_#{i}_nickname", | |
:description => "user_#{i}_description", | |
:rich_editing => ["true", "true", "false"][rand_by_3], | |
:comment_shortcuts => "false", | |
:admin_color => "fresh", | |
:use_ssl => "0", | |
:show_admin_bar_front => "true", | |
:wp_infrapp_capabilities => user_capability(rand_by_3)[:data], | |
:wp_infrapp_user_level => [7, 2, 0][rand_by_3], | |
:dismissed_wp_pointers => "wp330_toolbar,wp330_media_uploader,wp330_saving_widgets" | |
} | |
@authors << new_user if user_capability(rand_by_3)[:key] == 'author' | |
meta_datas.each do |key, value| | |
new_user.wp_infrapp_usermeta.create( | |
:user_id => new_user.id, | |
:meta_key => key, | |
:meta_value => value | |
) | |
end | |
puts "saved:#{i}" if i % 100 == 0 | |
end | |
end# }}} | |
def user_capability (num)# {{{ | |
[ | |
{:key => 'editor', :data => 'a:1:{s:6:"editor";s:1:"1";}'}, | |
{:key => 'author', :data => 'a:1:{s:6:"author";s:1:"1";}'}, | |
{:key => 'subscriber', :data => 'a:1:{s:10:"subscriber";s:1:"1";}'} | |
][num] | |
end# }}} | |
def set_up_posts# {{{ | |
puts "#{__method__} => " + Time.now.to_s | |
categories = WpInfrappTerm.where("slug LIKE 'cat_%'") | |
tags = WpInfrappTerm.where("slug LIKE 'tag_%'") | |
@conf.entry_count.times do |i| | |
begin | |
ActiveRecord::Base.transaction do | |
puts "◆#{__method__}.entory_count: [#{i}] #=> " + Time.now.to_s | |
author = @authors[rand(@authors.length)] | |
post_date = create_post_date(author.user_registered) | |
new_post = WpInfrappPost.new( | |
:post_author => author.id, | |
:post_date => post_date, | |
:post_date_gmt => post_date.gmtime, | |
:post_content => ("<p>" + "記事投稿内容#{i}" * (1 + rand(20)) + "</p>" ) * (1 + rand(100)), | |
:post_title => "記事タイトル#{i} " * (1 + rand(3)), | |
:post_excerpt => '', | |
:post_status => post_status, | |
:comment_status => comment_status, | |
:ping_status => ['open', 'closed'][rand(2)], | |
:post_password => '', | |
:post_name => CGI.escape("タイトル_#{i}"), | |
:to_ping => '', | |
:pinged => '', | |
:post_modified => post_date, | |
:post_modified_gmt => post_date.gmtime, | |
:post_content_filtered => '', | |
:post_parent => 0, | |
:guid => '', | |
:menu_order => 0, | |
:post_type => 'post', | |
:post_mime_type => '', | |
:comment_count => rand(@conf.comment_count) | |
) | |
new_post.save! | |
new_post.guid = "#{siteurl}/?p=#{new_post.id}" | |
new_post.save! | |
set_up_post_comments(new_post) | |
set_up_post_categories(new_post, categories) | |
set_up_post_tags(new_post, tags) | |
end | |
rescue => e | |
pp e | |
end | |
end | |
end# }}} | |
def set_up_post_comments(post)# {{{ | |
puts " #{__method__} => " + Time.now.to_s | |
approved_comment_count = post.comment_count > 10 ? post.comment_count - rand(10) : 0 | |
approved_comment = [] | |
post.comment_count.times do |i| | |
comment_date = post.post_date + i | |
approved = i < approved_comment_count | |
new_comment = WpInfrappComment.new( | |
:comment_post_ID => post.id, | |
:comment_author => 'コメント投稿者名', | |
:comment_author_email => '[email protected]', | |
:comment_author_url => 'http://bit.ly/infrapp2012', | |
:comment_author_IP => '127.0.0.1', | |
:comment_date => comment_date, | |
:comment_date_gmt => comment_date.gmtime, | |
:comment_content => "コメント#{i}です。" * (1 + rand(100)), | |
:comment_karma => 0, | |
:comment_approved => approved ? '1' : '0', | |
:comment_agent => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_2) AppleWebKit/535.7 (KHTML, like Gecko) Chrome/16.0.912.75 Safari/535.7', | |
:comment_type => '', | |
:comment_parent => 0, | |
:user_id => 0 | |
) | |
new_comment.save! | |
approved_comment << new_comment if approved | |
if approved && i > 0 | |
decide_parent_comment(new_comment, approved_comment) | |
end | |
end | |
puts " saved:#{post.comment_count}" | |
end# }}} | |
def set_up_post_categories(post, categories)# {{{ | |
puts " #{__method__} => " + Time.now.to_s | |
i = 0 | |
categories.shuffle.each do |cat| | |
break if rand < 0.5 && i > 0 | |
post.wp_infrapp_term_relationships.create( | |
:object_id => post.ID, | |
:term_taxonomy_id => cat.wp_infrapp_term_taxonomy.first.term_taxonomy_id, | |
:term_order => 0 | |
) | |
@category_usage[cat.term_id] += 1 if @category_usage[cat.term_id] | |
i += 1 | |
end | |
puts " saved:#{i}" | |
end# }}} | |
def set_up_post_tags(post, tags)# {{{ | |
puts " #{__method__} => " + Time.now.to_s | |
i = 0 | |
tags.shuffle.each do |tag| | |
break if rand < 0.4 && i > 0 | |
post.wp_infrapp_term_relationships.create( | |
:object_id => post.ID, | |
:term_taxonomy_id => tag.wp_infrapp_term_taxonomy.first.term_taxonomy_id, | |
:term_order => 0 | |
) | |
@tag_usage[tag.term_id] += 1 if @tag_usage[tag.term_id] | |
i += 1 | |
end | |
puts " saved:#{i}" | |
end# }}} | |
def apply_category_counts# {{{ | |
@category_usage.each do |cat_id, count| | |
cat = WpInfrappTermTaxonomy.where(:term_id => cat_id).first | |
cat.count = count | |
cat.save! | |
end | |
end# }}} | |
def apply_tag_counts# {{{ | |
@tag_usage.each do |tag_id, count| | |
tag = WpInfrappTermTaxonomy.where(:term_id => tag_id).first | |
tag.count = count | |
tag.save! | |
end | |
end# }}} | |
def decide_parent_comment(comment, parents)# {{{ | |
if rand > 0.7 | |
comment.comment_parent = | |
parents[rand(parents.length)].comment_ID | |
comment.save! | |
end | |
end# }}} | |
def siteurl# {{{ | |
result = WpInfrappOption.where("option_name = 'siteurl'").first | |
result.option_value | |
end# }}} | |
def comment_status# {{{ | |
statuses = ['open','open','open', 'open', 'closed', 'registered_only'] | |
statuses[rand(statuses.length)] | |
end# }}} | |
def post_status# {{{ | |
['publish', 'pending', 'draft', 'private', 'future'][rand(5)] | |
end# }}} | |
def create_post_date(user_registered_date)# {{{ | |
diff = Time.now - user_registered_date | |
user_registered_date + rand(diff) | |
end# }}} | |
def conn# {{{ | |
ActiveRecord::Base.establish_connection( | |
:adapter => @conf.db_adapter, | |
:host => @conf.db_host, | |
:username => @conf.db_user, | |
:password => @conf.db_password, | |
:database => @conf.db_name, | |
:encoding => @conf.charset | |
) | |
end# }}} | |
def define_models# {{{ | |
pf = @conf.table_prefix | |
pf_snake = pf.to_snake | |
eval " | |
class #{pf}User < ActiveRecord::Base | |
has_many :#{pf_snake}_usermeta, :foreign_key => 'user_id', :dependent => :delete_all | |
has_many :#{pf_snake}_posts, :foreign_key => 'post_author', :dependent => :delete_all | |
end" | |
eval " | |
class #{pf}Usermetum < ActiveRecord::Base; | |
set_primary_key :umeta_id | |
end" | |
eval " | |
class #{pf}Comment < ActiveRecord::Base; | |
has_many :#{pf_snake}_commentmeta, :foreign_key => 'comment_id', :dependent => :delete_all | |
set_primary_key :comment_ID | |
end" | |
eval " | |
class #{pf}Commentmeta < ActiveRecord::Base; | |
end" | |
eval " | |
class #{pf}Link < ActiveRecord::Base; | |
end" | |
eval " | |
class #{pf}Option < ActiveRecord::Base; | |
end" | |
eval " | |
class #{pf}Post < ActiveRecord::Base; | |
has_many :#{pf_snake}_comments, :foreign_key => 'comment_post_ID', :dependent => :delete_all | |
has_many :#{pf_snake}_term_relationships, :foreign_key => 'object_id', :dependent => :delete_all | |
end" | |
eval " | |
class #{pf}Postmeta < ActiveRecord::Base; | |
end" | |
eval " | |
class #{pf}Term < ActiveRecord::Base; | |
has_many :#{pf_snake}_term_taxonomy, :foreign_key => 'term_taxonomy_id', :dependent => :delete_all | |
end" | |
eval " | |
class #{pf}TermRelationship < ActiveRecord::Base; | |
end" | |
eval " | |
class #{pf}TermTaxonomy < ActiveRecord::Base; | |
set_table_name :#{pf_snake}_term_taxonomy | |
end" | |
end# }}} | |
end | |
class String# {{{ | |
def to_snake | |
self.split(/(?![a-z])(?=[A-Z])/).map{|s| s.downcase}.join('_') | |
end | |
def to_camel | |
self.split('_').map{|s| s.capitalize}.join('') | |
end | |
end# }}} | |
Initializer.new.run |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment