Project

General

Profile

Patch #29649 » 0001-Add-a-rake-task-to-dump-whole-wikis.patch

Lorenz Schori, 2018-09-20 13:00

View differences:

lib/tasks/wikidump.rake
1
# Redmine - project management software
2
# Copyright (C) 2006-2017  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
namespace :redmine do
19
  namespace :wikidump do
20
    def export_wiki(project, template, path)
21
      pages = project.wiki.pages.
22
                      order('title').
23
                      includes([:content, {:attachments => :author}]).
24
                      to_a
25

  
26
      assigns = {
27
        :wiki => project.wiki,
28
        :project => project,
29
        :pages => pages,
30
      }
31
      view = ActionView::Base.new(ActionController::Base.view_paths, assigns, ActionController::Base.new)
32

  
33
      view.class_eval do
34
        include Rails.application.routes.url_helpers
35
        include ApplicationHelper
36
        include WikiHelper
37

  
38
        def url_options
39
          {host: Setting.host_name.split('/')[0]}
40
        end
41
      end
42

  
43
      File.open(path, "wb") do |f|
44
        f.write(view.render(:template => template))
45
      end
46
    end
47

  
48
    desc <<-END_DESC
49
Dump the wiki of a project to a PDF file.
50

  
51
Available options:
52
  * project   => id or identifier of project (defaults to first projects)
53
  * directory => output directory (defaults to rails temporary directory)
54

  
55
Example:
56
  rake redmine:wikidump:pdf project=my-project-identifier directory=/tmp RAILS_ENV="production"
57
END_DESC
58

  
59
    task :pdf => :environment do
60
      project = ENV['project'] ? Project.find(ENV['project']) : Project.first
61
      directory = ENV['directory'] || "#{Rails.root}/tmp"
62
      path = File.join(directory, "#{project.identifier}.pdf")
63
      export_wiki(project, 'wiki/export.pdf', path)
64
    end
65

  
66
    desc <<-END_DESC
67
Dump the wiki of a project to a HTML file.
68

  
69
Available options:
70
  * project   => id or identifier of project (defaults to first projects)
71
  * directory => output directory (defaults to rails temporary directory)
72

  
73
Example:
74
  rake redmine:wikidump:html project=my-project-identifier directory=/tmp RAILS_ENV="production"
75
END_DESC
76

  
77
    task :html => :environment do
78
      project = ENV['project'] ? Project.find(ENV['project']) : Project.first
79
      directory = ENV['directory'] || "#{Rails.root}/tmp"
80
      path = File.join(directory, "#{project.identifier}.html")
81
      export_wiki(project, 'wiki/export_multiple.html', path)
82
    end
83
  end
84
end
(1-1/3)