]> scripts.mit.edu Git - wizard.git/blob - wizard/app/mediawiki.py
When MediaWiki installs fail due to bad user input, give useful errors.
[wizard.git] / wizard / app / mediawiki.py
1 import re
2 import distutils.version
3 import os
4 import lxml.cssselect
5 import lxml.etree
6 import StringIO
7
8 from wizard import app, install, resolve, shell, util
9 from wizard.app import php
10
11 def make_filename_regex(var):
12     return 'LocalSettings.php', php.re_var(var)
13
14 seed = util.dictmap(make_filename_regex, {
15         'WIZARD_IP': 'IP', # obsolete, remove after we're done
16         'WIZARD_SITENAME': 'wgSitename',
17         'WIZARD_SCRIPTPATH': 'wgScriptPath',
18         'WIZARD_EMERGENCYCONTACT': ('wgEmergencyContact', 'wgPasswordSender'),
19         'WIZARD_DBSERVER': 'wgDBserver',
20         'WIZARD_DBNAME': 'wgDBname',
21         'WIZARD_DBUSER': 'wgDBuser',
22         'WIZARD_DBPASSWORD': 'wgDBpassword',
23         'WIZARD_SECRETKEY': ('wgSecretKey', 'wgProxyKey'),
24         })
25
26 class Application(app.Application):
27     parametrized_files = ['LocalSettings.php', 'php.ini']
28     deprecated_keys = set(['WIZARD_IP']) | php.deprecated_keys
29     extractors = app.make_extractors(seed)
30     extractors.update(php.extractors)
31     substitutions = app.make_substitutions(seed)
32     substitutions.update(php.substitutions)
33     install_schema = install.ArgSchema("mysql", "admin", "email", "title")
34     def checkConfig(self, deployment):
35         return os.path.isfile("LocalSettings.php")
36     def detectVersion(self, deployment):
37         contents = deployment.read("includes/DefaultSettings.php")
38         regex = php.re_var("wgVersion")
39         match = regex.search(contents)
40         if not match: return None
41         return distutils.version.LooseVersion(match.group(2)[1:-1])
42     def checkWeb(self, deployment, out=None):
43         page = deployment.fetch("/index.php?title=Main_Page")
44         if type(out) is list:
45             out.append(page)
46         return page.find("<!-- Served") != -1
47     def prepareMerge(self, deployment):
48         resolve.fix_newlines("LocalSettings.php")
49     def install(self, version, options):
50         try:
51             os.unlink("LocalSettings.php")
52         except OSError:
53             pass
54
55         os.chmod("config", 0777) # XXX: vaguely sketchy
56
57         postdata = {
58             'Sitename': options.title,
59             'EmergencyContact': options.email,
60             'LanguageCode': 'en',
61             'DBserver': options.mysql_host,
62             'DBname': options.mysql_db,
63             'DBuser': options.mysql_user,
64             'DBpassword': options.mysql_password,
65             'DBpassword2': options.mysql_password,
66             'defaultEmail': options.email,
67             'SysopName': options.admin_name,
68             'SysopPass': options.admin_password,
69             'SysopPass2': options.admin_password,
70             }
71         result = install.fetch(options, '/config/index.php', post=postdata)
72         result_etree = lxml.etree.parse(StringIO.StringIO(result), lxml.etree.HTMLParser())
73         selector = lxml.cssselect.CSSSelector(".error")
74         error_messages = [e.text for e in selector(result_etree)]
75         if options.verbose or options.debug: print result
76         if result.find("Installation successful") == -1:
77             if not error_messages:
78                 raise install.Failure()
79             raise app.NonfatalFailure(error_messages)
80         os.rename('config/LocalSettings.php', 'LocalSettings.php')
81     def upgrade(self, d, version, options):
82         sh = shell.Shell()
83         if not os.path.isfile("AdminSettings.php"):
84             sh.call("git", "checkout", "-q", "mediawiki-" + str(version), "--", "AdminSettings.php")
85         try:
86             result = sh.eval("php", "maintenance/update.php", "--quick", log=True)
87         except shell.CallError as e:
88             raise app.UpgradeFailure("Update script returned non-zero exit code\nSTDOUT: %s\nSTDERR: %s" % (e.stdout, e.stderr))
89         results = result.rstrip().split()
90         if not results or not results[-1] == "Done.":
91             raise app.UpgradeFailure(result)
92     def backup(self, deployment, backup_dir, options):
93         app.backup_database(backup_dir, deployment)
94     def restore(self, deployment, backup_dir, options):
95         app.restore_database(backup_dir, deployment)
96
97 Application.resolutions = {
98 'LocalSettings.php': [
99     ("""
100 <<<<<<<
101 ***1***
102 =======
103 ## The URL base path to the directory containing the wiki;
104 ## defaults for all runtime URL paths are based off of this.
105 ## For more information on customizing the URLs please see:
106 ## http://www.mediawiki.org/wiki/Manual:Short_URL
107 ***2***
108 $wgScriptExtension  = ".php";
109
110 ## UPO means: this is also a user preference option
111 >>>>>>>
112 """, [-1]),
113     ("""
114 <<<<<<<
115 ***1***
116 =======
117
118 # MySQL specific settings
119 $wgDBprefix         = "";
120 >>>>>>>
121 """, ["\n# MySQL specific settings", 1]),
122     ("""
123 <<<<<<<
124 ## is writable, then uncomment this:
125 ***1***
126 =======
127 ## is writable, then set this to true:
128 $wgEnableUploads       = false;
129 >>>>>>>
130 """, [-1]),
131     ("""
132 <<<<<<<
133 ***1***
134 $wgMathPath         = "{$wgUploadPath}/math";
135 $wgMathDirectory    = "{$wgUploadDirectory}/math";
136 $wgTmpDirectory     = "{$wgUploadDirectory}/tmp";
137 =======
138 $wgUseTeX           = false;
139 >>>>>>>
140 """, [1]),
141     # order of these rules is important
142     ("""
143 <<<<<<<
144 $configdate = gmdate( 'YmdHis', @filemtime( __FILE__ ) );
145 $wgCacheEpoch = max( $wgCacheEpoch, $configdate );
146 ***1***
147 ?>
148 =======
149 $wgCacheEpoch = max( $wgCacheEpoch, gmdate( 'YmdHis', @filemtime( __FILE__ ) ) );
150 >>>>>>>
151 """, [0, 1]),
152     ("""
153 <<<<<<<
154 $configdate = gmdate( 'YmdHis', @filemtime( __FILE__ ) );
155 $wgCacheEpoch = max( $wgCacheEpoch, $configdate );
156 ***1***
157 =======
158 $wgCacheEpoch = max( $wgCacheEpoch, gmdate( 'YmdHis', @filemtime( __FILE__ ) ) );
159 >>>>>>>
160 """, [0, 1]),
161     ("""
162 <<<<<<<
163 ?>
164 =======
165 # When you make changes to this configuration file, this will make
166 # sure that cached pages are cleared.
167 $wgCacheEpoch = max( $wgCacheEpoch, gmdate( 'YmdHis', @filemtime( __FILE__ ) ) );
168 >>>>>>>
169 """, [0]),
170     ("""
171 <<<<<<<
172 ***1***
173 ?>
174 =======
175 # When you make changes to this configuration file, this will make
176 # sure that cached pages are cleared.
177 $wgCacheEpoch = max( $wgCacheEpoch, gmdate( 'YmdHis', @filemtime( __FILE__ ) ) );
178 >>>>>>>
179 """, [1, 0]),
180     ("""
181 <<<<<<<
182 ***1***
183 =======
184 # When you make changes to this configuration file, this will make
185 # sure that cached pages are cleared.
186 $wgCacheEpoch = max( $wgCacheEpoch, gmdate( 'YmdHis', @filemtime( __FILE__ ) ) );
187 >>>>>>>
188 """, [1, 0]),
189     ]
190 }
191