discourse/script/discourse

254 lines
7.2 KiB
Plaintext
Raw Normal View History

2013-11-27 13:17:14 -05:00
#!/usr/bin/env ruby
require "thor"
class DiscourseCLI < Thor
desc "remap [--global,--regex] FROM TO", "Remap a string sequence accross all tables"
long_desc <<-LONGDESC
Replace a string sequence FROM with TO across all tables.
With --global option, the remapping is run on ***ALL***
databases. Instead of just running on the current database, run on
every database on this machine. This option is useful for
multi-site setups.
With --regex option, use PostgreSQL function regexp_replace to do
the remapping. Enabling this interprets FROM as a PostgreSQL
regular expression. TO can contain references to captures in the
FROM match. See the "Regular Expression Details" section and
"regexp_replace" documentation in the PostgreSQL manual for more
details.
Examples:
discourse remap talk.foo.com talk.bar.com # renaming a Discourse domain name
discourse remap --regex "\[\/?color(=[^\]]*)*]" "" # removing "color" bbcodes
LONGDESC
2017-07-27 21:20:09 -04:00
option :global, type: :boolean
option :regex, type: :boolean
def remap(from, to)
load_rails
require 'remap'
if options[:regex]
puts "Rewriting all occurences of #{from} to #{to} using regexp_replace"
else
puts "Rewriting all occurences of #{from} to #{to}"
end
puts "THIS TASK WILL REWRITE DATA, ARE YOU SURE (type YES)"
puts "WILL RUN ON ALL #{RailsMultisite::ConnectionManagement.all_dbs.length} DBS" if options[:global]
2014-01-31 00:37:05 -05:00
text = STDIN.gets
if text.strip != "YES"
puts "aborting."
exit
end
if options[:global]
2015-07-23 00:39:38 -04:00
RailsMultisite::ConnectionManagement.each_connection do |db|
2017-07-27 21:20:09 -04:00
puts "", "Remapping tables on #{db}...", ""
do_remap(from, to, options[:regex])
end
2015-07-23 00:39:38 -04:00
else
do_remap(from, to, options[:regex])
end
end
2014-02-12 23:38:47 -05:00
desc "backup", "Backup a discourse forum"
def backup(filename = nil)
load_rails
require "backup_restore/backup_restore"
require "backup_restore/backuper"
2014-02-12 23:38:47 -05:00
puts "Starting backup..."
backuper = BackupRestore::Backuper.new(Discourse.system_user.id)
backup = backuper.run
if filename.present?
puts "Moving '#{backup}' to '#{filename}'"
puts "Including version number into '#{filename}'"
version_string = File.basename(backup)[/-#{BackupRestore::VERSION_PREFIX}\d{14}/]
filename = filename.dup.insert(filename.index('.'), version_string)
FileUtils.mv(backup, filename)
backup = filename
end
puts "Backup done."
puts "Output file is in: #{backup}", ""
exit(1) unless backuper.success
end
2014-02-12 23:38:47 -05:00
desc "export", "Backup a Discourse forum"
def export
backup
end
desc "restore", "Restore a Discourse backup"
2017-07-27 21:20:09 -04:00
def restore(filename = nil)
2017-10-03 17:09:32 -04:00
if File.exist?('/usr/local/bin/discourse')
discourse = 'discourse'
else
discourse = './script/discourse'
end
if !filename
puts "You must provide a filename to restore. Did you mean one of the following?\n\n"
Dir["public/backups/default/*"].sort_by { |path| File.mtime(path) }.reverse.each do |f|
puts "#{discourse} restore #{File.basename(f)}"
end
return
end
load_rails
require "backup_restore/backup_restore"
require "backup_restore/restorer"
begin
puts "Starting restore: #{filename}"
restorer = BackupRestore::Restorer.new(Discourse.system_user.id, filename: filename)
restorer.run
puts 'Restore done.'
rescue BackupRestore::FilenameMissingError
puts '', 'The filename argument was missing.', ''
usage
rescue BackupRestore::RestoreDisabledError
2017-10-03 17:09:32 -04:00
puts '', 'Restores are not allowed.', 'An admin needs to set allow_restore to true in the site settings before restores can be run.'
puts "Enable now with", '', "#{discourse} enable_restore", ''
puts 'Restore cancelled.', ''
end
exit(1) unless restorer.try(:success)
end
2014-02-12 23:38:47 -05:00
desc "import", "Restore a Discourse backup"
def import(filename)
restore(filename)
end
desc "rollback", "Rollback to the previous working state"
def rollback
load_rails
require "backup_restore"
2013-11-27 13:17:14 -05:00
puts 'Rolling back if needed..'
BackupRestore.rollback!
puts 'Done.'
end
desc "enable_restore", "Allow restore operations"
def enable_restore
load_rails
require "site_setting"
SiteSetting.allow_restore = true
puts 'Restore are now permitted. Disable them with `disable_restore`'
end
desc "disable_restore", "Forbid restore operations"
def disable_restore
load_rails
require "site_setting"
2013-11-27 13:17:14 -05:00
SiteSetting.allow_restore = false
puts 'Restore are now forbidden. Enable them with `enable_restore`'
2013-11-27 13:17:14 -05:00
end
2014-02-21 11:10:53 -05:00
desc "enable_readonly", "Enable the readonly mode"
def enable_readonly
load_rails
Discourse.enable_readonly_mode
puts 'The site is now in readonly mode.'
end
desc "disable_readonly", "Disable the readonly mode"
def disable_readonly
load_rails
Discourse.disable_readonly_mode
puts 'The site is now fully operable.'
end
desc "request_refresh", "Ask all clients to refresh the browser"
def request_refresh
load_rails
Discourse.request_refresh!
puts 'Requests sent. Clients will refresh on next navigation.'
end
desc "export_categories", "Export categories, all its topics, and all users who posted in those topics"
def export_categories(*category_ids)
puts "Starting export of categories...", ""
load_rails
load_import_export
ImportExport.export_categories(category_ids)
puts "", "Done", ""
end
desc "export_category", "Export a category, all its topics, and all users who posted in those topics"
def export_category(category_id)
raise "Category id argument is missing!" unless category_id
export_categories([category_id])
end
desc "import_category", "Import a category, its topics and the users from the output of the export_category command"
def import_category(filename)
raise "File name argument missing!" unless filename
puts "Starting import from #{filename}..."
load_rails
load_import_export
ImportExport.import(filename)
puts "", "Done", ""
end
desc "export_topics", "Export topics and all users who posted in that topic. Accepts multiple topic id's"
def export_topics(*topic_ids)
puts "Starting export of topics...", ""
load_rails
load_import_export
ImportExport.export_topics(topic_ids)
puts "", "Done", ""
end
desc "import_topics", "Import topics and their users from the output of the export_topic command"
def import_topics(filename)
raise "File name argument missing!" unless filename
puts "Starting import from #{filename}..."
load_rails
load_import_export
ImportExport.import(filename)
puts "", "Done", ""
end
2013-11-27 13:17:14 -05:00
private
def load_rails
require File.expand_path(File.dirname(__FILE__) + "/../config/environment")
end
def load_import_export
require File.expand_path(File.dirname(__FILE__) + "/../lib/import_export/import_export")
end
2017-07-27 21:20:09 -04:00
def do_remap(from, to, regex = false)
begin
Remap.new(from, to, regex: regex, verbose: true).perform
puts 'Done', ''
rescue => ex
puts "Error: #{ex}"
puts "The remap has only been partially applied due to the error above. Please re-run the script again."
exit(1)
2015-07-23 00:39:38 -04:00
end
end
2013-11-27 13:17:14 -05:00
end
DiscourseCLI.start(ARGV)