2 import distutils.version
9 from wizard import app, deploy, install, scripts, shell, util
10 from wizard.app import php
12 def make_filename_regex(var):
13 return 'LocalSettings.php', re.compile('^(\$' + app.expand_re(var) + r'''\s*=\s*)(.*)(;)''', re.M)
15 make_extractor = app.filename_regex_extractor(make_filename_regex)
16 make_substitution = app.filename_regex_substitution(make_filename_regex)
18 'WIZARD_IP': 'IP', # obsolete, remove after we're done
19 'WIZARD_SITENAME': 'wgSitename',
20 'WIZARD_SCRIPTPATH': 'wgScriptPath',
21 'WIZARD_EMERGENCYCONTACT': ('wgEmergencyContact', 'wgPasswordSender'),
22 'WIZARD_DBSERVER': 'wgDBserver',
23 'WIZARD_DBNAME': 'wgDBname',
24 'WIZARD_DBUSER': 'wgDBuser',
25 'WIZARD_DBPASSWORD': 'wgDBpassword',
26 'WIZARD_SECRETKEY': ('wgSecretKey', 'wgProxyKey'),
29 class Application(deploy.Application):
30 parametrized_files = ['LocalSettings.php', 'php.ini']
31 deprecated_keys = set(['WIZARD_IP']) | php.deprecated_keys
34 if not self._extractors:
35 self._extractors = util.dictmap(make_extractor, seed)
36 self._extractors.update(php.extractors)
37 return self._extractors
39 def substitutions(self):
40 if not self._substitutions:
41 self._substitutions = util.dictkmap(make_substitution, seed)
42 self._substitutions.update(php.substitutions)
43 return self._substitutions
45 def install_handler(self):
46 handler = install.ArgHandler("mysql", "admin", "email")
47 handler.add(install.Arg("title", help="Title of your new MediaWiki install"))
49 def checkConfig(self, deployment):
50 return os.path.isfile(os.path.join(deployment.location, "LocalSettings.php"))
51 def detectVersion(self, deployment):
52 contents = deployment.read("includes/DefaultSettings.php")
53 regex = make_filename_regex("wgVersion")[1]
54 match = regex.search(contents)
55 if not match: return None
56 return distutils.version.LooseVersion(match.group(2)[1:-1])
57 def checkWeb(self, d):
58 page = d.fetch("index.php?title=Special:Version")
59 return page.find("MediaWiki is free software") != -1
60 def install(self, version, options):
62 os.unlink("LocalSettings.php")
66 os.chmod("config", 0777) # XXX: vaguely sketchy
69 'Sitename': options.title,
70 'EmergencyContact': options.email,
72 'DBserver': options.mysql_host,
73 'DBname': options.mysql_db,
74 'DBuser': options.mysql_user,
75 'DBpassword': options.mysql_password,
76 'DBpassword2': options.mysql_password,
77 'defaultEmail': options.email,
78 'SysopName': options.admin_name,
79 'SysopPass': options.admin_password,
80 'SysopPass2': options.admin_password,
82 result = install.fetch(options, 'config/index.php', post=postdata)
83 if options.verbose: print result
84 if result.find("Installation successful") == -1:
85 raise install.Failure()
86 os.rename('config/LocalSettings.php', 'LocalSettings.php')
87 def upgrade(self, version, options):
89 if not os.path.isfile("AdminSettings.php"):
90 sh.call("git", "checkout", "mediawiki-" + str(version), "--", "AdminSettings.php")
92 result = sh.eval("php", "maintenance/update.php", "--quick", log=True)
93 except shell.CallError as e:
94 raise app.UpgradeFailure("Update script returned non-zero exit code\nSTDOUT: %s\nSTDERR: %s" % (e.stdout, e.stderr))
95 results = result.rstrip().split()
96 if not results or not results[-1] == "Done.":
97 raise app.UpgradeFailure(result)
98 def backup(self, deployment, options):
100 # XXX: duplicate code, refactor, also, race condition
101 backupdir = os.path.join(".scripts", "backups")
102 backup = str(deployment.version) + "-" + datetime.date.today().isoformat()
103 outdir = os.path.join(backupdir, backup)
104 if not os.path.exists(backupdir):
106 if os.path.exists(outdir):
107 util.safe_unlink(outdir)
109 outfile = os.path.join(outdir, "db.sql")
111 sh.call("mysqldump", "--compress", "-r", outfile, *get_mysql_args(deployment))
112 sh.call("gzip", "--best", outfile)
113 except shell.CallError as e:
114 raise app.BackupFailure(e.stderr)
116 def restore(self, deployment, backup, options):
118 backup_dir = os.path.join(".scripts", "backups", backup)
119 if not os.path.exists(backup_dir):
120 raise app.RestoreFailure("Backup %s doesn't exist", backup)
121 sql = open(os.path.join(backup_dir, "db.sql"), 'w+')
122 sh.call("gunzip", "-c", os.path.join(backup_dir, "db.sql.gz"), stdout=sql)
124 sh.call("mysql", *get_mysql_args(deployment), stdin=sql)
127 def get_mysql_args(d):
128 # XXX: add support for getting these out of options
130 if 'WIZARD_DBNAME' not in vars:
131 raise app.BackupFailure("Could not determine database name")
132 triplet = scripts.get_sql_credentials()
134 if triplet is not None:
135 server, user, password = triplet
136 args += ["-h", server, "-u", user, "-p" + password]
137 name = shlex.split(vars['WIZARD_DBNAME'])[0]