|
| 1 | +use std::fs; |
| 2 | +use anyhow::{anyhow, bail}; |
| 3 | +use clap::{Args, ValueEnum}; |
| 4 | +use crate::config::MWUtilConfig; |
| 5 | +use crate::constants::MEDIAWIKI_CONTAINER; |
| 6 | +use crate::modules::{db, opensearch}; |
| 7 | +use crate::modules::opensearch::{OpenSearchArgs, OpenSearchCommand}; |
| 8 | +use crate::modules::run::RunArgs; |
| 9 | +use crate::{run_module, Modules}; |
| 10 | +use crate::utils::SpinnerSequence; |
| 11 | + |
| 12 | +// only supports top-level files in images/ ! |
| 13 | +const EXCLUDED_UPLOADS: [&str; 2] = ["README", ".htaccess"]; |
| 14 | + |
| 15 | +#[derive(Clone, PartialEq, ValueEnum)] |
| 16 | +pub enum ResetActions { |
| 17 | + Database, |
| 18 | + OpenSearch, |
| 19 | + Uploads, |
| 20 | +} |
| 21 | + |
| 22 | +#[derive(Args)] |
| 23 | +pub struct ResetArgs { |
| 24 | + /// The actions to perform |
| 25 | + actions: Vec<ResetActions> |
| 26 | +} |
| 27 | + |
| 28 | +pub fn execute(config: &MWUtilConfig, args: ResetArgs) -> anyhow::Result<()> { |
| 29 | + let actions = args.actions; |
| 30 | + // not using SpinnerSequence::new here so we can init the text dynamically |
| 31 | + let mut spinner = SpinnerSequence { |
| 32 | + cur: 0, |
| 33 | + max: actions.len() as u8, |
| 34 | + last: None, |
| 35 | + }; |
| 36 | + if actions.contains(&ResetActions::Uploads) { |
| 37 | + spinner.next("Resetting uploads"); |
| 38 | + reset_uploads(config)?; |
| 39 | + } |
| 40 | + if actions.contains(&ResetActions::Database) { |
| 41 | + spinner.next("Resetting database"); |
| 42 | + reset_database(config)?; |
| 43 | + } |
| 44 | + if actions.contains(&ResetActions::OpenSearch) { |
| 45 | + spinner.next("Resetting OpenSearch"); |
| 46 | + opensearch::execute(config, OpenSearchArgs { |
| 47 | + command: OpenSearchCommand::Reset, |
| 48 | + })?; |
| 49 | + } |
| 50 | + Ok(()) |
| 51 | +} |
| 52 | + |
| 53 | +pub fn reset_uploads(config: &MWUtilConfig) -> anyhow::Result<()> { |
| 54 | + let upload_dir = config.core_dir.join("images"); |
| 55 | + if !upload_dir.exists() { |
| 56 | + bail!("Upload directory does not exist!"); |
| 57 | + } |
| 58 | + if !upload_dir.is_dir() { |
| 59 | + bail!("Upload directory is not a directory!"); |
| 60 | + } |
| 61 | + if let Ok(files) = fs::read_dir(upload_dir) { |
| 62 | + for entry in files.flatten() { |
| 63 | + if !EXCLUDED_UPLOADS.contains(&entry.file_name().to_string_lossy().as_ref()) { |
| 64 | + let path = entry.path(); |
| 65 | + println!("Removing {}", entry.file_name().to_string_lossy()); |
| 66 | + if path.is_dir() { |
| 67 | + fs::remove_dir_all(path)?; |
| 68 | + } else { |
| 69 | + fs::remove_file(path)?; |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + Ok(()) |
| 75 | +} |
| 76 | + |
| 77 | +pub fn reset_database(config: &MWUtilConfig) -> anyhow::Result<()> { |
| 78 | + db::drop_mw_database(config)?; |
| 79 | + |
| 80 | + let local_settings = config.core_dir.join("LocalSettings.php"); |
| 81 | + let local_settings_tmp = config.core_dir.join("LocalSettings.temp.php"); |
| 82 | + fs::rename(&local_settings, &local_settings_tmp)?; |
| 83 | + |
| 84 | + let install_args = vec![ |
| 85 | + format!("--dbname={}", config.mw_database.clone().ok_or_else(|| anyhow!("MW Database not set!"))?), |
| 86 | + format!("--dbuser={}", config.db_user.clone().ok_or_else(|| anyhow!("DB User not set!"))?), |
| 87 | + format!("--dbpass={}", config.db_password.clone().ok_or_else(|| anyhow!("DB Password not set!"))?), |
| 88 | + format!("--dbserver={}", config.db_type.clone().get_container_name()), |
| 89 | + format!("--server={}", config.mw_server.clone().ok_or_else(|| anyhow!("MW Server not set!"))?), |
| 90 | + format!("--scriptpath={}", config.mw_script_path.clone().ok_or_else(|| anyhow!("MW Script Path not set!"))?), |
| 91 | + format!("--lang={}", config.mw_language.clone().ok_or_else(|| anyhow!("MW Language not set!"))?), |
| 92 | + format!("--pass={}", config.mw_password.clone().ok_or_else(|| anyhow!("MW Password not set!"))?), |
| 93 | + MEDIAWIKI_CONTAINER.to_string(), |
| 94 | + config.mw_user.clone().ok_or_else(|| anyhow!("MW User not set!"))?, |
| 95 | + ]; |
| 96 | + |
| 97 | + let result = run_module(Modules::Run(RunArgs { |
| 98 | + script: "install".to_string(), |
| 99 | + extra_args: install_args, |
| 100 | + }), Some(config)); |
| 101 | + |
| 102 | + fs::rename(local_settings_tmp, local_settings)?; |
| 103 | + |
| 104 | + result?; |
| 105 | + |
| 106 | + run_module(Modules::Update, Some(config))?; |
| 107 | + run_module(Modules::Recreate(Default::default()), Some(config))?; |
| 108 | + |
| 109 | + Ok(()) |
| 110 | +} |
0 commit comments