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