From 1d52a786ccd0258f1f9cf48f7c12e92529551526 Mon Sep 17 00:00:00 2001
From: Lorenz Schori <lo@znerol.ch>
Date: Thu, 20 Sep 2018 12:52:15 +0200
Subject: [PATCH] Add a rake task to dump whole wikis

---
 lib/tasks/wikidump.rake | 84 +++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 84 insertions(+)
 create mode 100644 lib/tasks/wikidump.rake

diff --git a/lib/tasks/wikidump.rake b/lib/tasks/wikidump.rake
new file mode 100644
index 000000000..c3e93b177
--- /dev/null
+++ b/lib/tasks/wikidump.rake
@@ -0,0 +1,84 @@
+# Redmine - project management software
+# Copyright (C) 2006-2017  Jean-Philippe Lang
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+
+namespace :redmine do
+  namespace :wikidump do
+    def export_wiki(project, template, path)
+      pages = project.wiki.pages.
+                      order('title').
+                      includes([:content, {:attachments => :author}]).
+                      to_a
+
+      assigns = {
+        :wiki => project.wiki,
+        :project => project,
+        :pages => pages,
+      }
+      view = ActionView::Base.new(ActionController::Base.view_paths, assigns, ActionController::Base.new)
+
+      view.class_eval do
+        include Rails.application.routes.url_helpers
+        include ApplicationHelper
+        include WikiHelper
+
+        def url_options
+          {host: Setting.host_name.split('/')[0]}
+        end
+      end
+
+      File.open(path, "wb") do |f|
+        f.write(view.render(:template => template))
+      end
+    end
+
+    desc <<-END_DESC
+Dump the wiki of a project to a PDF file.
+
+Available options:
+  * project   => id or identifier of project (defaults to first projects)
+  * directory => output directory (defaults to rails temporary directory)
+
+Example:
+  rake redmine:wikidump:pdf project=my-project-identifier directory=/tmp RAILS_ENV="production"
+END_DESC
+
+    task :pdf => :environment do
+      project = ENV['project'] ? Project.find(ENV['project']) : Project.first
+      directory = ENV['directory'] || "#{Rails.root}/tmp"
+      path = File.join(directory, "#{project.identifier}.pdf")
+      export_wiki(project, 'wiki/export.pdf', path)
+    end
+
+    desc <<-END_DESC
+Dump the wiki of a project to a HTML file.
+
+Available options:
+  * project   => id or identifier of project (defaults to first projects)
+  * directory => output directory (defaults to rails temporary directory)
+
+Example:
+  rake redmine:wikidump:html project=my-project-identifier directory=/tmp RAILS_ENV="production"
+END_DESC
+
+    task :html => :environment do
+      project = ENV['project'] ? Project.find(ENV['project']) : Project.first
+      directory = ENV['directory'] || "#{Rails.root}/tmp"
+      path = File.join(directory, "#{project.identifier}.html")
+      export_wiki(project, 'wiki/export_multiple.html', path)
+    end
+  end
+end
-- 
2.11.0

