]> scripts.mit.edu Git - wizard.git/blob - wizard/app/mediawiki.py
Various refinements from our mass-upgrade run.
[wizard.git] / wizard / app / mediawiki.py
1 import re
2 import distutils.version
3 import os
4
5 from wizard import app, deploy, install, shell, util
6 from wizard.app import php
7
8 def make_filename_regex(var):
9     return 'LocalSettings.php', re.compile('^(\$' + app.expand_re(var) + r'''\s*=\s*)(.*)(;)''', re.M)
10
11 make_extractor = app.filename_regex_extractor(make_filename_regex)
12 make_substitution = app.filename_regex_substitution(make_filename_regex)
13 seed = {
14         'WIZARD_IP': 'IP', # obsolete, remove after we're done
15         'WIZARD_SITENAME': 'wgSitename',
16         'WIZARD_SCRIPTPATH': 'wgScriptPath',
17         'WIZARD_EMERGENCYCONTACT': ('wgEmergencyContact', 'wgPasswordSender'),
18         'WIZARD_DBSERVER': 'wgDBserver',
19         'WIZARD_DBNAME': 'wgDBname',
20         'WIZARD_DBUSER': 'wgDBuser',
21         'WIZARD_DBPASSWORD': 'wgDBpassword',
22         'WIZARD_SECRETKEY': ('wgSecretKey', 'wgProxyKey'),
23         }
24
25 class Application(deploy.Application):
26     parametrized_files = ['LocalSettings.php', 'php.ini']
27     deprecated_keys = set(['WIZARD_IP']) | php.deprecated_keys
28     @property
29     def extractors(self):
30         if not self._extractors:
31             self._extractors = util.dictmap(make_extractor, seed)
32             self._extractors.update(php.extractors)
33         return self._extractors
34     @property
35     def substitutions(self):
36         if not self._substitutions:
37             self._substitutions = util.dictkmap(make_substitution, seed)
38             self._substitutions.update(php.substitutions)
39         return self._substitutions
40     @property
41     def install_handler(self):
42         handler = install.ArgHandler("mysql", "admin", "email")
43         handler.add(install.Arg("title", help="Title of your new MediaWiki install"))
44         return handler
45     def checkConfig(self, deployment):
46         return os.path.isfile(os.path.join(deployment.location, "LocalSettings.php"))
47     def detectVersion(self, deployment):
48         contents = deployment.read("includes/DefaultSettings.php")
49         regex = make_filename_regex("wgVersion")[1]
50         match = regex.search(contents)
51         if not match: return None
52         return distutils.version.LooseVersion(match.group(2)[1:-1])
53     def install(self, version, options):
54         try:
55             os.unlink("LocalSettings.php")
56         except OSError:
57             pass
58
59         os.chmod("config", 0777) # XXX: vaguely sketchy
60
61         postdata = {
62             'Sitename': options.title,
63             'EmergencyContact': options.email,
64             'LanguageCode': 'en',
65             'DBserver': options.mysql_host,
66             'DBname': options.mysql_db,
67             'DBuser': options.mysql_user,
68             'DBpassword': options.mysql_password,
69             'DBpassword2': options.mysql_password,
70             'defaultEmail': options.email,
71             'SysopName': options.admin_name,
72             'SysopPass': options.admin_password,
73             'SysopPass2': options.admin_password,
74             }
75         result = install.fetch(options, 'config/index.php', post=postdata)
76         if options.verbose: print result
77         if result.find("Installation successful") == -1:
78             raise install.Failure()
79         os.rename('config/LocalSettings.php', 'LocalSettings.php')
80     def upgrade(self, version, options):
81         sh = shell.Shell()
82         if not os.path.isfile("AdminSettings.php"):
83             sh.call("git", "checkout", "mediawiki-" + str(version), "--", "AdminSettings.php")
84         result = sh.eval("php", "maintenance/update.php", "--quick", log=True)
85         if not result.rstrip().split()[-1] == "Done.":
86             raise app.UpgradeFailure(result)
87