Project

General

Profile

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

Karel Pičman, 2022-06-02 15:25

View differences:

lib/tasks/wikydump.rake (working copy)
1
namespace :redmine do
2
  namespace :wikidump do
3

  
4
    def export_wiki(project, template, path)
5
      pages = project.wiki.pages.
6
        order('title').
7
        includes([:content, { attachments: :author }]).
8
        to_a
9
      assigns = {
10
        wiki: project.wiki,
11
        project: project,
12
        pages: pages,
13
        action_name: 'export_multiple'
14
      }
15
      lookup_context = ActionView::LookupContext.new(ActionController::Base.view_paths)
16
      view = ActionView::Base.with_empty_template_cache.new(lookup_context, assigns, ActionController::Base.new)
17
      view.class_eval do
18
        include Rails.application.routes.url_helpers
19
        include ApplicationHelper
20
        include WikiHelper
21
        include Redmine::I18n
22
        def url_options
23
          { host: Setting.host_name.split('/')[0] }
24
        end
25
      end
26
      File.open(path, 'w') do |f|
27
        f.write view.render(template: template)
28
      end
29
    end
30

  
31
    desc <<-END_DESC
32
    Dump the wiki of a project to a PDF file.
33
    Available options:
34
      * project   => id or identifier of project (defaults to first projects)
35
      * directory => output directory (defaults to rails temporary directory)
36
    Example:
37
      rake redmine:wikidump:pdf project=my-project-identifier directory=/tmp RAILS_ENV="production"
38
    END_DESC
39

  
40
    task pdf: :environment do
41
      project = ENV['project'] ? Project.find(ENV['project']) : Project.first
42
      directory = ENV['directory'] || File.join(Rails.root, 'tmp')
43
      path = File.join(directory, "#{project.identifier}.pdf")
44
      export_wiki(project, 'wiki/export.pdf', path)
45
    end
46

  
47
    desc <<-END_DESC
48
    Dump the wiki of a project to a HTML file.
49
    Available options:
50
      * project   => id or identifier of project (defaults to first projects)
51
      * directory => output directory (defaults to rails temporary directory)
52
    Example:
53
      rake redmine:wikidump:html project=my-project-identifier directory=/tmp RAILS_ENV="production"
54
    END_DESC
55

  
56
    task html: :environment do
57
      project = ENV['project'] ? Project.find(ENV['project']) : Project.first
58
      directory = ENV['directory'] || File.join(Rails.root, 'tmp')
59
      path = File.join(directory, "#{project.identifier}.html")
60
      export_wiki project, 'wiki/export_multiple.html', path
61
    end
62

  
63
  end
64

  
65
end
(3-3/3)