Exporting redmine wiki pages

Posted on 2011-06-04

I wanted to export all wiki pages of a redmine project as plain text (the original textile code) with a small yaml header:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
~/redmine $ RAILS_ENV=production ./script/console -s
Loading production environment in sandbox (Rails 2.3.11)
Any modifications you make will be rolled back on exit
NOTE: SourceIndex.new(hash) is deprecated; From /usr/lib/ruby/gems/1.8/gems/rails-2.3.11/lib/rails/vendor_gem_source_index.rb:100:in `new'.
>> def export_text(p)
>>   c = p.content_for_version(nil)
>>   "---\ntitle: #{p.title}\nupdated_on: #{c.updated_on}\n---\n\n#{c.text}"
>> end
=> nil
>> def export_wiki(dir, wiki)
>>   dir = dir + "/" + wiki.project.identifier
>>   Dir.mkdir(dir)
>>   wiki.pages.each { |p|File.open(dir + "/" + p.title + ".textile", "w") { |f| f.write(export_text(p)) } }
>>   true
>> end
=> nil
>> Project.all
=> [#<Project id: 2, name: "Debian Server", ... , identifier: "debianserver", ...
>> export_wiki("/tmp", Project.find(2).wiki)
=> true

Now “/tmp/debianserver/” contains all the wiki pages.

The ruby functions without irb shell:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
def export_text(p)
  c = p.content_for_version(nil)
  "---\ntitle: #{p.title}\nupdated_on: #{c.updated_on}\n---\n\n#{c.text}"
end

def export_wiki(dir, wiki)
  dir = dir + "/" + wiki.project.identifier
  Dir.mkdir(dir)
  wiki.pages.each { |p|File.open(dir + "/" + p.title + ".textile", "w") { |f| f.write(export_text(p)) } }
  true
end

Generated using nanoc and bootstrap - Last content change: 2013-08-16 14:47