From 362c7d2fab80088b7fe7f3bc3e7bb89c25fd1074 Mon Sep 17 00:00:00 2001 From: Adrian Malacoda Date: Sun, 11 Oct 2020 04:20:30 -0500 Subject: [PATCH] Add ability to directly pass in database info (for database backup) if we fail to get it from LocalSettings.php --- update-wiki | 68 +++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 55 insertions(+), 13 deletions(-) diff --git a/update-wiki b/update-wiki index b78a93f..e1abd4f 100644 --- a/update-wiki +++ b/update-wiki @@ -4,6 +4,7 @@ import json import os from datetime import datetime from subprocess import check_call, check_output +import argparse RELEASE_URL = "https://releases.wikimedia.org/mediawiki/{}/mediawiki-{}.tar.gz" @@ -18,10 +19,6 @@ include("$IP/LocalSettings.php"); print json_encode([%s]); """ -if len(sys.argv) < 3: - print("Please supply the path to the wiki and the MediaWiki version to update to") - exit(1) - def download_version (version): release_version = version[:version.rfind(".")] url = RELEASE_URL.format(release_version, version) @@ -61,25 +58,70 @@ def unpack_tarfile (tarfile, wiki_path): def update_database (wiki_path): check_call(["php", "maintenance/update.php"], cwd=wiki_path) -WIKI_PATH = sys.argv[1] -TARGET_VERSION = sys.argv[2] +parser = argparse.ArgumentParser(description="MediaWiki backup and update script") +parser.add_argument( + "--path", + dest="path", + required=True, + help="Path to wiki" +) +parser.add_argument( + "--version", + dest="version", + required=True, + help="Version to update to" +) +parser.add_argument( + "--dbserver", + dest="dbserver", + default="127.0.0.1", + help="Database host" +) +parser.add_argument( + "--dbname", + dest="dbname", + help="Database name" +) +parser.add_argument( + "--dbuser", + dest="dbuser", + help="Database username" +) +parser.add_argument( + "--dbpassword", + dest="dbpassword", + help="Database password" +) +args = parser.parse_args() + +wiki_database = { + "wgDBtype": "mysql", + "wgDBserver": args.dbserver, + "wgDBname": args.dbname, + "wgDBuser": args.dbuser, + "wgDBpassword": args.dbpassword +} +wiki_database.update(get_wiki_globals(args.path, "wgDBtype", "wgDBserver", "wgDBname", "wgDBuser", "wgDBpassword") or {}) +if not (wiki_database['wgDBserver'] and wiki_database['wgDBname'] and wiki_database['wgDBuser']): + print("Failed to determine wiki database settings! You may need to explicitly tell the script.") + sys.exit(1) + BACKUP_KEY = "backup-{}".format(datetime.now()).replace(" ", "_").replace(":","-") os.makedirs(BACKUP_KEY) print(">> Backup directory is: {}".format(BACKUP_KEY)) print(">> Backing up database") -wiki_database = get_wiki_globals(WIKI_PATH, "wgDBtype", "wgDBserver", "wgDBname", "wgDBuser", "wgDBpassword") backup_database(wiki_database, BACKUP_KEY) print(">> Backing up wiki source") -backup_source(WIKI_PATH, BACKUP_KEY) +backup_source(args.path, BACKUP_KEY) -print(">> Downloading MediaWiki {}".format(TARGET_VERSION)) -tarfile = download_version(TARGET_VERSION) +print(">> Downloading MediaWiki {}".format(args.version)) +tarfile = download_version(args.version) -print(">> Unpacking {} over {}".format(tarfile, WIKI_PATH)) -unpack_tarfile(tarfile, WIKI_PATH) +print(">> Unpacking {} over {}".format(tarfile, args.path)) +unpack_tarfile(tarfile, args.path) print(">> Running database update") -update_database(WIKI_PATH) +update_database(args.path)