2 import distutils.version
8 from wizard import app, deploy, install, scripts, shell, util
9 from wizard.app import php
11 def make_filename_regex(var):
12 return 'LocalSettings.php', re.compile('^(\$' + app.expand_re(var) + r'''\s*=\s*)(.*)(;)''', re.M)
14 make_extractor = app.filename_regex_extractor(make_filename_regex)
15 make_substitution = app.filename_regex_substitution(make_filename_regex)
17 'WIZARD_IP': 'IP', # obsolete, remove after we're done
18 'WIZARD_SITENAME': 'wgSitename',
19 'WIZARD_SCRIPTPATH': 'wgScriptPath',
20 'WIZARD_EMERGENCYCONTACT': ('wgEmergencyContact', 'wgPasswordSender'),
21 'WIZARD_DBSERVER': 'wgDBserver',
22 'WIZARD_DBNAME': 'wgDBname',
23 'WIZARD_DBUSER': 'wgDBuser',
24 'WIZARD_DBPASSWORD': 'wgDBpassword',
25 'WIZARD_SECRETKEY': ('wgSecretKey', 'wgProxyKey'),
28 class Application(deploy.Application):
29 parametrized_files = ['LocalSettings.php', 'php.ini']
30 deprecated_keys = set(['WIZARD_IP']) | php.deprecated_keys
33 if not self._extractors:
34 self._extractors = util.dictmap(make_extractor, seed)
35 self._extractors.update(php.extractors)
36 return self._extractors
38 def substitutions(self):
39 if not self._substitutions:
40 self._substitutions = util.dictkmap(make_substitution, seed)
41 self._substitutions.update(php.substitutions)
42 return self._substitutions
44 def install_handler(self):
45 handler = install.ArgHandler("mysql", "admin", "email")
46 handler.add(install.Arg("title", help="Title of your new MediaWiki install"))
48 def checkConfig(self, deployment):
49 return os.path.isfile(os.path.join(deployment.location, "LocalSettings.php"))
50 def detectVersion(self, deployment):
51 contents = deployment.read("includes/DefaultSettings.php")
52 regex = make_filename_regex("wgVersion")[1]
53 match = regex.search(contents)
54 if not match: return None
55 return distutils.version.LooseVersion(match.group(2)[1:-1])
56 def install(self, version, options):
58 os.unlink("LocalSettings.php")
62 os.chmod("config", 0777) # XXX: vaguely sketchy
65 'Sitename': options.title,
66 'EmergencyContact': options.email,
68 'DBserver': options.mysql_host,
69 'DBname': options.mysql_db,
70 'DBuser': options.mysql_user,
71 'DBpassword': options.mysql_password,
72 'DBpassword2': options.mysql_password,
73 'defaultEmail': options.email,
74 'SysopName': options.admin_name,
75 'SysopPass': options.admin_password,
76 'SysopPass2': options.admin_password,
78 result = install.fetch(options, 'config/index.php', post=postdata)
79 if options.verbose: print result
80 if result.find("Installation successful") == -1:
81 raise install.Failure()
82 os.rename('config/LocalSettings.php', 'LocalSettings.php')
83 def upgrade(self, version, options):
85 if not os.path.isfile("AdminSettings.php"):
86 sh.call("git", "checkout", "mediawiki-" + str(version), "--", "AdminSettings.php")
87 result = sh.eval("php", "maintenance/update.php", "--quick", log=True)
88 if not result.rstrip().split()[-1] == "Done.":
89 raise app.UpgradeFailure(result)
90 def backup(self, deployment, options):
92 vars = deployment.extract()
93 if any(vars[i] is None for i in ['WIZARD_DBSERVER', 'WIZARD_DBNAME', 'WIZARD_DBUSER', 'WIZARD_DBPASSWORD']):
94 raise app.BackupFailure("Missing WIZARD variables from configuration files")
95 # XXX: duplicate code, refactor, also, race condition
96 backupdir = os.path.join(".scripts", "backups")
97 outdir = os.path.join(backupdir, str(deployment.version) + "-" + datetime.date.today().isoformat())
98 if not os.path.exists(backupdir):
100 if os.path.exists(outdir):
101 util.safe_unlink(outdir)
103 outfile = os.path.join(outdir, "db.sql")
104 # XXX: add support for getting these out of options
105 triplet = scripts.get_sql_credentials()
106 args = ["mysqldump", "--compress", "-r", outfile];
107 if triplet is not None:
108 server, user, password = triplet
109 args += ["-h", server, "-u", user, "-p" + password]
110 name = shlex.split(vars['WIZARD_DBNAME'])[0]
114 sh.call("gzip", "--best", outfile)
115 except shell.CallError as e:
116 raise app.BackupFailure(e.stderr)