Hi,
I'd like to share a tip of a way to import data from an other application. In my case this was from docman in an old joomla 1.0 installation.
I made a Ruby script that connects to the old database and then posts the data to the frontend content submission form.
Code:
require "rubygems"
require "active_record"
require 'rest_client'
require 'iconv'
require 'htmlentities'
Skold = {
:adapter => "mysql",
:host => "localhost",
:port => 8888,
:database => "skold",
:username => "root",
:password => "",
:socket => "/Applications/MAMP/tmp/mysql/mysql.sock",
:encoding => "latin1"
}
#Skold - Old database
class Docman < ActiveRecord::Base
establish_connection Skold
set_table_name 'jos_docman'
set_primary_key 'id'
belongs_to :category, :foreign_key => 'catid'
end
class Category < ActiveRecord::Base
establish_connection Skold
set_table_name 'jos_categories'
set_primary_key 'id'
has_many :docmans, :foreign_key => 'catid'
end
path = '/Users/nils/Sites/skold/dmdocuments/'
charset = Iconv.new('UTF-8','LATIN1')
html = HTMLEntities.new
Docman.all.each do |l|
response = RestClient.post( 'http://localhost:8888/smartklubben/index.php?option=com_flexicontent&view=items&layout=form&typeid=2&Itemid=3',
:task => 'save',
:vstate => 2,
:state => 1,
:created_by => 62,
:created => l.dmdate_published,
:id => 0,
:title => charset.iconv(l.dmname),
:text => html.decode(charset.iconv(l.dmdescription)), #Decode html entities and convert LATIN1 to UTF-8
:'oldcat[]' => l.category.title,
:arskurs_from => -1,
:arskurs_tom => 13,
:'cid[]' => 2,
:lesson => File.exist?(path + l.dmfilename)? File.new(path + l.dmfilename, 'rb'):''
)
puts "Import ok" if response.code == 200
puts "File #{l.dmfilename} skipped for #{l.id}" if !File.exist?(path + l.dmfilename)
end
oldcat[] and arskurs_from is examples of custom fields.
In order to make it easy I installed this plugin:
extensions.joomla.org/extensions
... tion/10391
to automatically have a admin user session active. I also added these lines to the plugin at line 47 to circumvent the token check.
Code:
$token = JUtility::getToken();
$_POST[$token] = 1;
Remember to uninstall the "Hack me" plugin when you are done!
I used Ruby because it was the easiest way for me to do it but you can rewrite the script in php if you want.
/Nils