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

Ajax

开发平台:

Others

  1. class EmailNotifier < ActiveRecord::Observer
  2.   observe Article, Page, Comment
  3.   
  4.   def after_save(content)
  5.     true
  6.   end
  7. end
  8. class WebNotifier < ActiveRecord::Observer
  9.   observe Article
  10.   def after_save(article)
  11.   end
  12. end
  13. class BaseConverter
  14.   @@new_user_password = 'typomigrator'
  15.   cattr_accessor :new_user_password
  16.   def initialize(options = {})
  17.     @count   = {:users => 0, :articles => 0, :comments => 0, :pages => 0}
  18.     @options = options
  19.   end
  20.   # Converts the source blog to typo.  Should resemble something like this:
  21.   #
  22.   #   converter = new(options)
  23.   #   converter.import_users do |other_user|
  24.   #     # build Typo User object from other user
  25.   #     ::User.new ...
  26.   #   end
  27.   #   
  28.   #   convert.import_pages do |other_page|
  29.   #     # build Typo Page object from other page
  30.   #     ::Page.new ...
  31.   #   end
  32.   #   
  33.   #   convert.import_articles do |other_article|
  34.   #     # build Typo Article object from other article
  35.   #     ::Article.new ...
  36.   #   end
  37.   #   
  38.   #   convert.import_comments do |other_comment|
  39.   #     # build Typo Comment object from other comment
  40.   #     ::Comment.new ...
  41.   #   end
  42.   def self.convert(options = {})
  43.     raise NotImplementedError
  44.   end
  45.   # override this to provide array of all posts to migrate.
  46.   #
  47.   #   @old_articles ||= Article.find(:all)
  48.   def old_articles
  49.     raise NotImplementedError
  50.   end
  51.   # override this to provide array of all pages to migrate.
  52.   #
  53.   #   @old_pages ||= Page.find(:all)
  54.   def old_pages
  55.     [] # don't raise an error; some sources won't have pages
  56.   end
  57.   # override this to find all users from the source database
  58.   # save them to @old_users, indexed by :login
  59.   # 
  60.   #   @old_users ||= User.find(:all).index_by(&:login)
  61.   def old_users
  62.     raise NotImplementedError
  63.   end
  64.   
  65.   # override this to retrieve the login name from the source user model
  66.   def get_login(other_user)
  67.     other_user.login
  68.   end
  69.   # override this to retrieve all comments from the source article model
  70.   def comments_for(other_article)
  71.     other_article.comments
  72.   end
  73.   # Resets an invalid email for a new user
  74.   def handle_bad_user_email(other_user, email)
  75.     other_user.email = email
  76.   end
  77.   # Resets the author email for a bad comment from the source site.
  78.   def handle_bad_comment_author_email(other_comment, email)
  79.     other_comment.author_email = email
  80.   end
  81.   # Resets the author url for a bad comment from the source site.
  82.   def handle_bad_comment_author_url(other_comment, url)
  83.     other_comment.author_url = url
  84.   end
  85.   
  86.   # Resets the author name for a bad comment from the source site.
  87.   def handle_bad_comment_author(other_comment, author)
  88.     other_comment.author = author
  89.   end
  90.   
  91.   # Resets the content for a bad comment from the source site.
  92.   def handle_bad_comment_content(other_comment, content)
  93.     other_comment.content = content
  94.   end
  95.   # Returns the destination site for the migrated content.  Uses the :site => 5 option to specify a site by id.
  96.   def blog
  97.     @blog ||= ::Blog.find(@options[:blog] || 1)
  98.   end
  99.   # Return the Typo filter None
  100.   def filter
  101.     @filter ||= ::TextFilter.find 1
  102.   end
  103.   # Returns all the users from the current Typo site, in a hash indexed by login name.
  104.   def users
  105.     @users        = ::User.find(:all)
  106.     @default_user = @users.first
  107.     @users.index_by(&:login)
  108.   end
  109.   # Returns the default user for new articles if one is not set in the source site.
  110.   def default_user
  111.     users if @users.nil?
  112.     @default_user
  113.   end
  114.   # Returns all Category, in a hash indexed by the category name.
  115.   def categories
  116.     @categories ||= ::Category.find(:all).index_by(&:name)
  117.   end
  118.   # Returns all Tag in a hash indexed by the tag name
  119.   def tags
  120.     @tags ||= ::Tag.find(:all).index_by(&:name)
  121.   end
  122.   def import_users(&block)
  123.     puts "start migrating of user..."
  124.     old_users.each do |login, other_user|
  125.       import_user(other_user, &block)
  126.     end
  127.     print "n"
  128.     puts "migrated #{@count[:users]} user(s)..."
  129.   end
  130.   
  131.   def import_user(other_user, &block)
  132.     unless other_user && users[get_login(other_user)]
  133.       ActiveRecord::Base.logger.info "Creating new user for #{get_login(other_user)}"
  134.       new_user = block.call(other_user)
  135.       new_user.save!
  136.       print '.'
  137.       @count[:users] += 1
  138.       new_user
  139.     end
  140.   rescue ActiveRecord::RecordInvalid
  141.     if $!.record.errors.on :email
  142.       puts "  Retrying with new email"
  143.       handle_bad_user_email other_user, "#{$!.record.login}@nodomain.com"
  144.       retry
  145.     else
  146.       raise
  147.     end
  148.   end
  149.   def create_article(other_article, &block)
  150.     (article, *categories) = block.call(other_article)
  151.     if article
  152.       tags = Array.new(article.tags)
  153.       article.allow_comments = true
  154.       article.allow_pings    = false
  155.       article.published      = true
  156.       article.user           ||= default_user
  157.       article.text_filter    ||= filter
  158.       article.save!
  159.       print '.'
  160.       categories.each_with_index do |c, i|
  161.         article.categories << c
  162.       end
  163.       tags.each { |tag|
  164.         tag.articles << article
  165.         tag.save!
  166.       }
  167.       @article_index[other_article] = article
  168.       @count[:articles] += 1
  169.     end
  170.   rescue ActiveRecord::RecordInvalid
  171.     puts "Invalid Article: %s " % $!.record.errors.full_messages.join(' ')
  172.     puts $!.record.inspect
  173.     raise
  174.   end
  175.   def create_page(other_page, &block)
  176.     page = block.call(other_page)
  177.     if page
  178.       page.allow_comments = true
  179.       page.allow_pings    = false
  180.       page.published      = true
  181.       page.user           ||= default_user
  182.       page.text_filter    ||= filter
  183.       page.save!
  184.       print '.'
  185.       @count[:pages] += 1
  186.     end
  187.   rescue ActiveRecord::RecordInvalid
  188.     puts "Invalid Page: %s " % $!.record.errors.full_messages.join(' ')
  189.     puts $!.record.inspect
  190.     raise
  191.   end
  192.   def create_comment(article, other_comment, &block)
  193.     ActiveRecord::Base.logger.info "adding comment"
  194.     returning block.call(other_comment) do |comment|
  195.       comment.article  = article
  196.       comment.text_filter ||= filter
  197.       comment.ip ||= '127.0.0.1'
  198.       comment.save!
  199.       print '.'
  200.       @count[:comments] += 1
  201.     end
  202.   rescue ActiveRecord::RecordInvalid
  203.     if $!.record.errors.on :author_email
  204.       puts "  Retrying with new email"
  205.       handle_bad_comment_author_email other_comment, "invalid@nodomain.com"
  206.       retry
  207.     elsif $!.record.errors.on :author_url
  208.       puts "  Retrying with new URL"
  209.       handle_bad_comment_author_url other_comment, "http://nowhere.com/"
  210.       retry
  211.     elsif $!.record.errors.on :author
  212.       puts "  Retrying with new author name"
  213.       handle_bad_comment_author other_comment, "unknown"
  214.       retry
  215.     elsif $!.record.errors.on :body
  216.       puts "  Retrying with blank body"
  217.       handle_bad_comment_content other_comment, "empty"
  218.       retry
  219.     end
  220.     puts "Invalid Comment: %s " % $!.record.errors.full_messages.join(' ')
  221.     puts $!.record.inspect
  222.     raise
  223.   end
  224.   def create_categories(libelle)
  225.     @categories[libelle] = Category.find_or_create_by_name libelle 
  226.   end
  227.   # Create a tag with a libelle
  228.   # Add the tag in Hash value in attribute of tags
  229.   def create_tag(libelle)
  230.     @tags[libelle] = Tag.get libelle 
  231.   end
  232.   
  233.   def import_articles(&block)
  234.     puts "started articles migration..."
  235.     @article_index = {}
  236.     old_articles.each do |other_article|
  237.       create_article other_article, &block
  238.     end
  239.     print "n"
  240.     puts "migrated #{@count[:articles]} article(s)..."
  241.   end
  242.   
  243.   def import_pages(&block)
  244.     puts "started pages migration..."
  245.     old_pages.each do |other_page|
  246.       create_page other_page, &block
  247.     end
  248.     print "n"
  249.     puts "migrated #{@count[:pages]} page(s)..."
  250.   end
  251.   
  252.   def import_comments(&block)
  253.     puts "started comment migration..."
  254.     old_articles.each do |other_article|
  255.       ActiveRecord::Base.logger.info "Creating article comments"
  256.       comments_for(other_article).each do |other_comment|
  257.         create_comment(@article_index[other_article], other_comment, &block)
  258.       end
  259.     end
  260.     print "n"
  261.     puts "migrated #{@count[:comments]} comment(s)..."
  262.   end
  263. end