<commit_before>#!/usr/bin/env ruby

# TextPattern 1.x converter for typo by <NAME> <<EMAIL>>
# 
# MAKE BACKUPS OF EVERYTHING BEFORE RUNNING THIS SCRIPT!
# THIS SCRIPT IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND

require File.dirname(__FILE__) + '/../../config/environment'
require 'optparse'

class TXPMigrate
  attr_accessor :options
  
  def initialize
    self.options = {}
    self.parse_options
    self.convert_categories
    self.convert_entries
    self.convert_prefs
  end

  def convert_categories
    txp_categories = ActiveRecord::Base.connection.select_all(%{
      SELECT name
      FROM `#{self.options[:txp_db]}`.txp_category
      WHERE parent = 'root'
      AND type = 'article'
    })

    puts "Converting #{txp_categories.size} categories.."
    
    txp_categories.each do |cat|
      Category.create(cat) unless Category.find_by_name(cat['name'])
    end
  end
  
  def convert_entries
    txp_entries = ActiveRecord::Base.connection.select_all(%{
      SELECT
        ID,
        Annotate AS allow_comments,
        1 AS allow_pings,
        Title AS title,
        (CASE LENGTH(Body) WHEN 0 THEN Excerpt ELSE Body END) AS body,
        Excerpt AS excerpt,
        Keywords AS keywords,
        Posted AS created_at,
        LastMod AS updated_at,
        AuthorID AS author,
        (CASE textile_body WHEN '1' THEN 'textile' ELSE 'none' END) AS text_filter,
        (CASE Status WHEN '1' THEN '0' ELSE '1' END) AS published,
        Category1, Category2
      FROM `#{self.options[:txp_db]}`.textpattern
    })
    
    puts "Converting #{txp_entries.size} entries.."
    
    txp_entries.each do |entry|
      a = Article.new
      a.attributes = entry.reject { |k,v| k =~ /^(Category|ID)/ }
      a.save
      
      # Assign categories
      puts "Assign primary category for entry #{entry['ID']}"
      a.categories.push_with_attributes(Category.find_by_name(entry['Category1']), :is_primary => 1) rescue nil
      puts "Assign secondary category for entry #{entry['ID']}"
      a.categories.push_with_attributes(Category.find_by_name(entry['Category2']), :is_primary => 0) rescue nil
      
      # Fetch comments
      ActiveRecord::Base.connection.select_all(%{
        SELECT
          name AS author,
          email AS email,
          web AS url,
          message AS body,
          posted AS created_at,
          ip AS ip
        FROM `#{self.options[:txp_db]}`.txp_discuss
        WHERE parentid = #{entry['ID']}
      }).each do |c|
        a.comments.create(c)
      end
      
    end
  end

  def convert_prefs
    puts "Converting prefs"
    
    ActiveRecord::Base.connection.select_all(%{
      SELECT
        (CASE name
          WHEN 'sitename' THEN 'blog_name'
          WHEN 'comments_on_default' THEN 'default_allow_comments'
          WHEN 'use_textile' THEN 'text_filter'<commit_msg>Brings textpattern converter up with newest Textpattern release<commit_after>#!/usr/bin/env ruby

# TextPattern 1.x converter for typo by <NAME> <<EMAIL>>
# 
# MAKE BACKUPS OF EVERYTHING BEFORE RUNNING THIS SCRIPT!
# THIS SCRIPT IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND

require File.dirname(__FILE__) + '/../../config/environment'
require 'optparse'

class TXPMigrate
  attr_accessor :options
  
  def initialize
    self.options = {}
    self.parse_options
    self.convert_categories
    self.convert_entries
    self.convert_prefs
  end

  def convert_categories
    txp_categories = ActiveRecord::Base.connection.select_all(%{
      SELECT name
      FROM `#{self.options[:txp_db]}`.txp_category
      WHERE parent = 'root'
      AND type = 'article'
    })

    puts "Converting #{txp_categories.size} categories.."
    
    txp_categories.each do |cat|
      Category.create(cat) unless Category.find_by_name(cat['name'])
    end
  end
  
  def convert_entries
    txp_entries = ActiveRecord::Base.connection.select_all(%{
      SELECT
        ID,
        Annotate AS allow_comments,
        1 AS allow_pings,
        Title AS title,
        (CASE LENGTH(Body) WHEN 0 THEN Excerpt ELSE Body END) AS body,
        Body_html AS body_html,
        Excerpt AS excerpt,
        Keywords AS keywords,
        Posted AS created_at,
        LastMod AS updated_at,
        AuthorID AS author,
        (CASE textile_body WHEN '1' THEN 'textile' ELSE 'none' END) AS text_filter,
        (CASE Status WHEN '1' THEN '0' ELSE '1' END) AS published,
        Category1, Category2
      FROM `#{self.options[:txp_db]}`.textpattern
    })
    
    puts "Converting #{txp_entries.size} entries.."
    
    txp_entries.each do |entry|
      a = Article.new
      a.attributes = entry.reject { |k,v| k =~ /^(Category|ID)/ }
      a.save
      
      # Assign categories
      puts "Assign primary category for entry #{entry['ID']}"
      a.categories.push_with_attributes(Category.find_by_name(entry['Category1']), :is_primary => 1) rescue nil
      puts "Assign secondary category for entry #{entry['ID']}"
      a.categories.push_with_attributes(Category.find_by_name(entry['Category2']), :is_primary => 0) rescue nil
      
      # Fetch comments
      ActiveRecord::Base.connection.select_all(%{
        SELECT
          name AS author,
          email AS email,
          web AS url,
          message AS body,
          message as body_html,
          posted AS created_at,
          ip AS ip
        FROM `#{self.options[:txp_db]}`.txp_discuss
        WHERE parentid = #{entry['ID']}
      }).each do |c|
        a.comments.create(c)
      end
      
    end
  end

  def convert_prefs
    puts "Converting prefs"
    
    ActiveRecord::Base.connection.select_all(%{
      SELECT
        (CASE name
          WHEN 'sitename' THEN 'blog_name'
          WHEN 'comments_on_default' THEN 'default_allow_comments'
          WHEN 'use_textile' THEN 'text_filter'
{'fraction_non_alphanumeric': 0.09996552912788693, 'fraction_numerical': 0.0044812133746983795, 'mean_word_length': 2.5298053527980535, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5974175', 'n_tokens_mistral': 1848, 'n_tokens_neox': 1770, 'n_words': 482}