Project

General

Profile

Feature #1224 » migrate_from_mediawiki.rake

Carl Nygard, 2008-06-20 20:28

 
1
# redMine - project management software
2
# Copyright (C) 2006-2007  Jean-Philippe Lang
3
#
4
# This program is free software; you can redistribute it and/or
5
# modify it under the terms of the GNU General Public License
6
# as published by the Free Software Foundation; either version 2
7
# of the License, or (at your option) any later version.
8
# 
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
# GNU General Public License for more details.
13
# 
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17

    
18
require 'active_record'
19
require 'iconv'
20
require 'pp'
21

    
22
namespace :redmine do
23
  desc 'MediaWiki migration script'
24
  task :migrate_from_mediawiki => :environment do
25
    
26
    module MWMigrate
27

    
28
     TRACKER_BUG = Tracker.find_by_position(1)
29
     TRACKER_FEATURE = Tracker.find_by_position(2)
30
     TRACKER_SUPPORT = Tracker.find_by_position(3)
31

    
32
      class MWText < ActiveRecord::Base
33
        set_table_name :text
34
        set_primary_key :old_id
35
      end
36

    
37
      class MWRev < ActiveRecord::Base
38
        set_table_name :revision
39
        set_primary_key :rev_id
40
        belongs_to :page, :class_name => "MWPage", :foreign_key => :rev_page
41
        belongs_to :text, :class_name => "MWText", :foreign_key => :rev_text_id
42
      end
43

    
44
      class MWPage < ActiveRecord::Base
45
        set_table_name :page
46
        set_primary_key :page_id
47
        has_many :revisions, :class_name => "MWRev", :foreign_key => :rev_page, :order => "rev_timestamp DESC"
48
      end
49

    
50
      def self.find_or_create_user(email, project_member = false)
51
        u = User.find_by_mail(email)
52
        if !u
53
          u = User.find_by_mail(@@mw_default_user)
54
        end
55
        if(!u)
56
          # Create a new user if not found
57
          mail = email[0,limit_for(User, 'mail')]
58
          mail = "#{mail}@fortna.com" unless mail.include?("@")
59
          name = email[0,email.index("@")];
60
          u = User.new :firstname => name[0,limit_for(User, 'firstname')].gsub(/[^\w\s\'\-]/i, '-'),
61
                       :lastname => '-',
62
                       :mail => mail.gsub(/[^-@a-z0-9\.]/i, '-')
63
          u.login = email[0,limit_for(User, 'login')].gsub(/[^a-z0-9_\-@\.]/i, '-')
64
          u.password = 'bugzilla'
65
          u.admin = false
66
          # finally, a default user is used if the new user is not valid
67
          puts "Created User: "+ u.to_yaml
68
          u = User.find(:first) unless u.save
69
        else
70
#          puts "Found User: " + u.to_yaml
71
        end
72
        # Make sure he is a member of the project
73
##        if project_member && !u.member_of?(@target_project)
74
##          role = ROLE_MAPPING['developer']
75
##          Member.create(:user => u, :project => @target_project, :role => role)
76
##          u.reload
77
##        end
78
        u
79
      end
80
      
81
      
82
      # Basic wiki syntax conversion
83
      def self.convert_wiki_text(text)
84
        # Titles
85
        text = text.gsub(/^(\=+)\s*([^=]+)\s*\=+\s*$/) {|s| "\nh#{$1.length}. #{$2}\n"}
86

    
87
        # Internal links
88
        text = text.gsub(/\[\[(.*)\s+\|(.*)\]\]/) {|s| "[[#{$1}|#{$2}]]"}
89

    
90
        # External Links
91
        text = text.gsub(/\[(http[^\s]+)\s+([^\]]+)\]/) {|s| "\"#{$2}\":#{$1}"}
92
        text = text.gsub(/\[(http[^\s]+)\]/) {|s| "#{$1}"}
93

    
94
        # Highlighting
95
        text = text.gsub(/'''''([^\s])/, '_*\1')
96
        text = text.gsub(/([^\s])'''''/, '\1*_')
97
        text = text.gsub(/'''([^\s])/, '*\1')
98
        text = text.gsub(/([^\s])'''/, '\1*')
99
        text = text.gsub(/''([^\s])/, '_*\1')
100
        text = text.gsub(/([^\s])''/, '\1*_')
101

    
102
        # code
103
        text = text.gsub(/((^ [^\n]*\n)+)/m) { |s| "<pre>\n#{$1}</pre>\n" }
104
#        text = text.gsub(/(^\n^ .*?$)/m) { |s| "<pre><code>#{$1}" }
105
#        text = text.gsub(/(^ .*?\n)\n/m) { |s| "#{$1}</pre></code>\n" }
106

    
107
        # Tables
108
        # Half-assed attempt
109
        # First strip off the table formatting
110
        text = text.gsub(/^\![^\|]*/, '')
111
        text = text.gsub(/^\{\|[^\|]*$/, '{|')
112

    
113
        # Now congeal the rows
114
        while( text.gsub!(/(\|-.*)\n(\|\w.*)$/m, '\1\2'))
115
        end
116

    
117
        # Now congeal the headers
118
        while( text.gsub!(/(\{\|.*)\n(\|\w.*)$/m, '\1\2'))
119
        end
120

    
121
        # format the headers properly
122
        while( text.gsub!(/(\{\|.*)\|([^_].*)$/, '\1|_. \2'))
123
        end
124

    
125
        # get rid of leading '{|'
126
        text = text.gsub(/^\{\|(.*)$/) { |s| "table(stdtbl)\n#{$1}|" }
127

    
128
        # get rid of leading '|-'
129
        text = text.gsub(/^\|-(.*)$/, '\1|')
130

    
131
        # get rid of trailing '|}'
132
        text = text.gsub(/^\|\}.*$/, '')
133

    
134
        # Internal Links
135
        text = text.gsub(/\[\[Image:([^\s]+)\]\]/) { |s| "!#{$1}!" }
136

    
137
        # Wiki page separator ':'
138
        while( text.gsub!(/(\[\[\s*\w+):(\w+)/, '\1_\2') )
139
        end
140

    
141
        text
142
      end
143
    
144
      def self.migrate
145
        establish_connection
146

    
147
        # Quick database test
148
        pages = MWPage.count
149
                
150
        migrated_wiki_edits = 0
151

    
152
        puts "No wiki defined" unless @target_project.wiki
153
        wiki = @target_project.wiki || 
154
               Wiki.new(:project => @target_project, 
155
                        :start_page => @target_project.name)
156

    
157
  
158
        # Wiki      
159
        puts "Migrating #{mw_page_title}, 1 of #{pages} pages"
160
        pages = MWPage.find(:all, 
161
                            :conditions => ["page_title = ?", mw_page_title])
162

    
163
        if((pages.size > 0) && (@@mw_whole_namespace == "y" || @@mw_whole_namespace == "Y"))
164
          pages = MWPage.find(:all,
165
                              :conditions => ["page_namespace = ?", pages[0].page_namespace])
166
        end
167

    
168
        pages.each do |page|
169
          print "Translate #{page.page_title} (y/N)? "
170
          next unless STDIN.gets.match(/^[yY]$/i)  
171

    
172
          STDOUT.flush
173
          new_title = page.page_title.gsub(/:/, "_")
174
          p = wiki.find_or_new_page(new_title)
175
          p.content = WikiContent.new(:page => p) if p.new_record?
176
          p.content.text = convert_wiki_text(page.revisions[0].text.old_text)
177
          p.content.author = User.find_by_mail(@@mw_default_user)
178
          p.content.comments = page.revisions[0].rev_comment
179
          puts "Record: " + p.content.to_s
180
          puts "  Text: " + p.content.text
181
          print "Save translated page (y/N)? "
182
          next unless STDIN.gets.match(/^[yY]$/i)  
183

    
184
          p.new_record? ? p.save : p.content.save
185
          migrated_wiki_edits += 1 unless p.content.new_record?
186
        end
187
       
188
        puts
189
        puts "Wiki edits:      #{migrated_wiki_edits}/#{MWPage.count}"
190
      end
191
      
192
      def self.limit_for(klass, attribute)
193
        klass.columns_hash[attribute.to_s].limit
194
      end
195
      
196
      def self.encoding(charset)
197
        @ic = Iconv.new('UTF-8', charset)
198
      rescue Iconv::InvalidEncoding
199
        puts "Invalid encoding!"
200
        return false
201
      end
202
      
203
      def self.set_mw_directory(path)
204
        @@bz_directory = path
205
        raise "This directory doesn't exist!" unless File.directory?(path)
206
        @@bz_directory
207
      rescue Exception => e
208
        puts e
209
        return false
210
      end
211

    
212
      def self.mw_directory
213
        @@mw_directory
214
      end
215

    
216
      def self.set_mw_adapter(adapter)
217
        return false if adapter.blank?
218
        raise "Unknown adapter: #{adapter}!" unless %w(sqlite sqlite3 mysql postgresql).include?(adapter)
219
        # If adapter is sqlite or sqlite3, make sure that mw.db exists
220
        raise "#{mw_db_path} doesn't exist!" if %w(sqlite sqlite3).include?(adapter) && !File.exist?(mw_db_path)
221
        @@mw_adapter = adapter
222
      rescue Exception => e
223
        puts e
224
        return false
225
      end
226
      
227
      def self.set_mw_db_host(host)
228
        return nil if host.blank?
229
        @@mw_db_host = host
230
      end
231

    
232
      def self.set_mw_db_port(port)
233
        return nil if port.to_i == 0
234
        @@mw_db_port = port.to_i
235
      end
236
      
237
      def self.set_mw_db_socket(sock)
238
        @@mw_db_socket = sock
239
      end
240
      
241
      def self.set_mw_db_name(name)
242
        return nil if name.blank?
243
        @@mw_db_name = name
244
      end
245

    
246
      def self.set_mw_db_username(username)
247
        @@mw_db_username = username
248
      end
249
      
250
      def self.set_mw_db_password(password)
251
        @@mw_db_password = password
252
      end
253
      
254
      def self.set_mw_default_user(username)
255
        @@mw_default_user = username
256
      end
257

    
258
      def self.set_mw_page_title(name)
259
        @@mw_page_title = name
260
      end
261

    
262
      def self.set_mw_whole_namespace(flag)
263
        @@mw_whole_namespace = flag
264
      end
265

    
266
      mattr_reader :mw_directory, :mw_adapter, :mw_db_host, :mw_db_port, :mw_db_name, :mw_db_username, :mw_db_password, :mw_db_socket, :mw_page_title, :mw_whole_namespace
267
      
268
     
269
      def self.mw_db_path; "#{mw_directory}/db/wiki.db" end
270
      def self.mw_attachments_directory; "#{mw_directory}/attachments" end
271

    
272
      def self.target_project_identifier(identifier)
273
        project = Project.find_by_identifier(identifier)
274
        if !project
275
          # create the target project
276
          project = Project.new :name => identifier.humanize,
277
                                :description => identifier.humanize
278
          project.identifier = identifier
279
          puts "Created Project: "+ project.to_s
280
          puts "Unable to create a project with identifier '#{identifier}'!" unless project.save
281
          # enable issues and wiki for the created project
282
          project.enabled_module_names = ['issue_tracking', 'wiki']
283
          project.trackers << TRACKER_BUG
284
          project.trackers << TRACKER_FEATURE
285
          project.trackers << TRACKER_SUPPORT
286
        else
287
          puts "Found Project: " + project.to_yaml
288
        end        
289
        @target_project = project.new_record? ? nil : project
290
      end
291
      
292
      
293
      def self.connection_params
294
        if %w(sqlite sqlite3).include?(mw_adapter)
295
          {:adapter => mw_adapter, 
296
           :database => mw_db_path}
297
        else
298
          {:adapter => mw_adapter,
299
           :database => mw_db_name,
300
           :host => mw_db_host,
301
           :port => mw_db_port,
302
           :socket => mw_db_socket,
303
           :username => mw_db_username,
304
           :password => mw_db_password}
305
        end
306
      end
307
      
308
      def self.establish_connection
309
        constants.each do |const|
310
          klass = const_get(const)
311
          next unless klass.respond_to? 'establish_connection'
312
          klass.establish_connection connection_params
313
        end
314
      end
315
      
316
    private
317
      def self.encode(text)
318
        @ic.iconv text
319
      rescue
320
        text
321
      end
322
    end
323
    
324
    puts
325
    puts "WARNING: a new project will be added to Redmine during this process."
326
    print "Are you sure you want to continue ? [y/N] "
327
    break unless STDIN.gets.match(/^[yY]$/i)  
328
    puts
329

    
330
    def prompt(text, options = {}, &block)
331
      default = options[:default] || ''
332
      while true
333
        print "#{text} [#{default}]: "
334
        value = STDIN.gets.chomp!
335
        value = default if value.blank?
336
        break if yield value
337
      end
338
    end
339
    
340
    DEFAULT_PORTS = {'mysql' => 3306, 'postgresl' => 5432}
341
    DEFAULT_SOCKETS = {'mysql' => '/var/lib/mysql/mysql.sock'}
342
    
343
    prompt('MW directory',:default => '/var/www/html/mediawiki-1.8.2') {|directory| MWMigrate.set_mw_directory directory}
344
    prompt('MW database adapter (sqlite, sqlite3, mysql, postgresql)', :default => 'mysql') {|adapter| MWMigrate.set_mw_adapter adapter}
345
    unless %w(sqlite sqlite3).include?(MWMigrate.mw_adapter)
346
      prompt('MW database host', :default => 'localhost') {|host| MWMigrate.set_mw_db_host host}
347
      prompt('MW database port', :default => DEFAULT_PORTS[MWMigrate.mw_adapter]) {|port| MWMigrate.set_mw_db_port port}
348
      prompt('MW database socket', :default => DEFAULT_SOCKETS[MWMigrate.mw_adapter]) {|sock| MWMigrate.set_mw_db_socket sock}
349
      prompt('MW database name', :default => 'wikidb') {|name| MWMigrate.set_mw_db_name name}
350
      prompt('MW database username', :default => 'wiki') {|username| MWMigrate.set_mw_db_username username}
351
      prompt('MW database password', :default => 'wikidb') {|password| MWMigrate.set_mw_db_password password}
352
    end
353
    prompt('MW database encoding', :default => 'UTF-8') {|encoding| MWMigrate.encoding encoding}
354
    prompt('Target project identifier', :default => 'CommCore') {|identifier| MWMigrate.target_project_identifier identifier}
355
    prompt('MW Page Title', :default => 'CommCore:Devel:XMLDB') {|identifier| MWMigrate.set_mw_page_title identifier}
356
    prompt('Page Author', :default => 'carlnygard@fortna.com') {|identifier| MWMigrate.set_mw_default_user identifier}
357
    prompt('Whole namespace (Y/n)?', :default => 'y') {|flag| MWMigrate.set_mw_whole_namespace flag}
358
    puts
359
    
360
    MWMigrate.migrate
361
  end
362
end
(2-2/2)