dotclear_2.rb
上传用户:netsea168
上传日期:2022-07-22
资源大小:4652k
文件大小:4k
源码类别:

Ajax

开发平台:

Others

  1. require 'converters/dotclear_2/post'
  2. require 'converters/dotclear_2/comment'
  3. require 'converters/dotclear_2/category'
  4. require 'converters/dotclear_2/user'
  5. require 'converters/dotclear_2/tag'
  6. class Dotclear2Converter < BaseConverter
  7.   def self.convert(options = {})
  8.     converter = new(options)
  9.     
  10.     unless (options[:prefix].nil?)
  11.       Dotclear2::Post.prefix = options[:prefix]
  12.       Dotclear2::Comment.prefix = options[:prefix]
  13.       Dotclear2::User.prefix = options[:prefix]
  14.       Dotclear2::Tag.prefix = options[:prefix]
  15.       Dotclear2::Category.prefix = options[:prefix]
  16.     end
  17.     
  18.     converter.import_users do |dc_user|
  19.       ::User.new 
  20.         :name => dc_user.user_displayname,
  21.         :email => dc_user.user_email || "#{dc_user.user_id}@notfound.com",
  22.         :login => dc_user.user_id,
  23.         :password => new_user_password,
  24.         :password_confirmation => new_user_password
  25.     end
  26.     converter.import_articles do |dc_article|
  27.       unless dc_article.post_content.blank? || dc_article.post_title.blank?
  28.         user = dc_article.user_id.nil? ? nil : converter.users[Dotclear2::User.find(dc_article.user_id.to_i).user_id]
  29.         
  30.         excerpt, body = !dc_article.post_excerpt.blank? ?
  31.           [dc_article.post_excerpt, dc_article.post_content]:
  32.           [nil, dc_article.post_content]
  33.         
  34.         a = ::Article.new 
  35.           :title        => CGI::unescapeHTML(dc_article.post_title),
  36.           :body         => body,
  37.           :created_at   => dc_article.post_creadt,
  38.           :published_at => dc_article.post_creadt,
  39.           :updated_at   => dc_article.post_upddt,
  40.           :author       => user,
  41.           :tags         => converter.find_or_create_tags(dc_article.tags)
  42.         [a, converter.find_or_create_categories(dc_article)]
  43.       end
  44.     end
  45.     
  46.     converter.import_comments do |dc_comment|
  47.       ::Comment.new 
  48.         :body         => dc_comment.comment_content,
  49.         :created_at   => dc_comment.comment_dt,
  50.         :updated_at   => dc_comment.comment_upddt,
  51.         :published_at => dc_comment.comment_dt,
  52.         :author       => dc_comment.comment_author,
  53.         :url          => dc_comment.comment_site,
  54.         :email        => dc_comment.comment_email,
  55.         :ip           => dc_comment.comment_ip
  56.     end
  57.   end
  58.   def old_articles
  59.     if @options.has_key?(:categories)
  60.       @old_article ||= Dotclear2::Post.find(:all, 
  61.                                            :include => :categorie, 
  62.                                            :conditions => ["post_status = ? AND cat_title IN (?)", true, @options[:categories]])
  63.     else
  64.       @old_article ||= Dotclear2::Post.find_all_by_post_status true
  65.     end
  66.     @old_article
  67.   end
  68.   def old_users
  69.     @old_users ||= Dotclear2::User.find(:all).index_by &:user_id
  70.   end
  71.   def get_login(dc_user)
  72.     dc_user.user_id
  73.   end
  74.   def handle_bad_user_email(dc_user, email)
  75.     dc_user.user_email = email
  76.   end
  77.   def handle_bad_comment_author_email(dc_comment, email)
  78.     dc_comment.comment_email = email
  79.   end
  80.   
  81.   def handle_bad_comment_author_url(dc_comment, url)
  82.     dc_comment.comment_site = url
  83.   end
  84.   
  85.   def handle_bad_comment_author(dc_comment, author)
  86.     dc_comment.comment_author = author
  87.   end
  88.   
  89.   def handle_bad_comment_content(dc_comment, content)
  90.     dc_comment.comment_content = content
  91.   end
  92.  
  93.   def create_sections(libelle)
  94.     @sections[libelle] = site.sections.create!(:name => libelle, :path => libelle)
  95.     @sections[libelle]
  96.   end
  97.   def find_or_create_categories(dc_article)
  98.     cat = dc_article.categorie
  99.     create_categories(cat.cat_title) if categories[cat.cat_title].nil?
  100.     categories[cat.cat_title]
  101.   end
  102.   
  103.   # with the tags'libelle in params search or
  104.   # create the Tag objet in Typo
  105.   def find_or_create_tags(dc_tags)
  106.     tags_post = []
  107.     dc_tags.each { |tag|
  108.       create_tag(tag.meta_id) if tags[tag.meta_id].nil?
  109.       tags_post << tags[tag.meta_id]
  110.     }
  111.     tags_post
  112.   end
  113. end