all repos — h3rald @ 152aea630e0d873c643faaaa2768706e04922227

The sources of https://h3rald.com

tasks/db.rake

 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
require 'rubygems'
require 'extlib'
require 'pathname'
require 'fileutils'
require 'mysql'
require 'sequel'
require 'yaml'


namespace :db do

	task :migrate => [:migrate_contents] do
	end

	task :migrate_contents, :db, :usr, :pwd do |t, args|
		raise RuntimeError, "Please provide :db, :usr, :pass" unless args[:db] && args[:usr] && args[:pwd]
		write_article = lambda do |meta, contents|
			path = (meta['type'] == 'article') ? Pathname.new(Dir.pwd)/"content/articles/" :  Pathname.new(Dir.pwd)/"content/"
			name = "#{meta['permalink']}.#{meta['filter_pre']}"
			path.mkpath
			(path/name).open('w+') do |f|
				f.print "--"
				f.puts meta.to_yaml
				f.puts "-----"
				f.puts contents
			end	
		end
		db = Sequel.mysql args[:db], :user => args[:usr], :password => args[:pwd], :host => 'localhost'
		db[:contents].where("state = 'published' || type = 'Page'").each do |a|
			meta = {}
			meta['tags'] = (a[:keywords]) ? a[:keywords].downcase.split(", ") : []
			meta['filter_pre'] = db[:text_filters].where("id = ?", a[:text_filter_id]).get(:name).downcase
			meta['permalink'] = a[:permalink] || a[:name]
			meta['title'] = a[:title]
			meta['type'] = a[:type].downcase
			write_article.call meta, a[:body]+a[:extended].to_s
		end
	end

end