]> scripts.mit.edu Git - wizard.git/blob - wizard/app/mediawiki.py
Convert ad hoc shell calls to singleton instance; fix upgrade bug.
[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     database = "mysql"
29     parametrized_files = ['LocalSettings.php'] + php.parametrized_files
30     deprecated_keys = set(['WIZARD_IP']) | php.deprecated_keys
31     extractors = app.make_extractors(seed)
32     extractors.update(php.extractors)
33     substitutions = app.make_substitutions(seed)
34     substitutions.update(php.substitutions)
35     install_schema = install.ArgSchema("db", "admin", "email", "title")
36     def checkConfig(self, deployment):
37         return os.path.isfile("LocalSettings.php")
38     def detectVersion(self, deployment):
39         return self.detectVersionFromFile("includes/DefaultSettings.php", php.re_var("wgVersion"))
40     def checkWeb(self, deployment):
41         return self.checkWebPage(deployment, "/index.php?title=Main_Page", "<!-- Served")
42     def prepareMerge(self, deployment):
43         resolve.fix_newlines("LocalSettings.php")
44     def install(self, version, options):
45         util.soft_unlink("LocalSettings.php")
46         os.chmod("config", 0777) # XXX: vaguely sketchy
47
48         postdata = {
49             'Sitename': options.title,
50             'EmergencyContact': options.email,
51             'LanguageCode': 'en',
52             'DBserver': options.dsn.host,
53             'DBname': options.dsn.database,
54             'DBuser': options.dsn.username,
55             'DBpassword': options.dsn.password,
56             'DBpassword2': options.dsn.password,
57             'defaultEmail': options.email,
58             'SysopName': options.admin_name,
59             'SysopPass': options.admin_password,
60             'SysopPass2': options.admin_password,
61             }
62         result = install.fetch(options, '/config/index.php', post=postdata)
63         result_etree = lxml.etree.parse(StringIO.StringIO(result), lxml.etree.HTMLParser())
64         selector = lxml.cssselect.CSSSelector(".error")
65         error_messages = [e.text for e in selector(result_etree)]
66         logging.debug("Installation output:\n\n" + result)
67         if result.find("Installation successful") == -1:
68             if not error_messages:
69                 raise app.InstallFailure()
70             else:
71                 raise app.RecoverableInstallFailure(error_messages)
72         os.rename('config/LocalSettings.php', 'LocalSettings.php')
73     def upgrade(self, d, version, options):
74         if not os.path.isfile("AdminSettings.php"):
75             shell.call("git", "checkout", "-q", "mediawiki-" + str(version), "--", "AdminSettings.php")
76         try:
77             result = shell.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     def remove(self, deployment, options):
88         app.remove_database(deployment)
89     def researchFilter(self, filename, added, deleted):
90         if filename == "LocalSettings.php":
91             return added == deleted == 10 or added == deleted == 9
92         elif filename == "AdminSettings.php":
93             return added == 0 and deleted == 20
94         elif filename == "config/index.php" or filename == "config/index.php5":
95             return added == 0
96         return False
97
98 Application.resolutions = {
99 'LocalSettings.php': [
100     ("""
101 <<<<<<<
102 ***1***
103 =======
104 ## The URL base path to the directory containing the wiki;
105 ## defaults for all runtime URL paths are based off of this.
106 ## For more information on customizing the URLs please see:
107 ## http://www.mediawiki.org/wiki/Manual:Short_URL
108 ***2***
109 $wgScriptExtension  = ".php";
110
111 ## UPO means: this is also a user preference option
112 >>>>>>>
113 """, [-1]),
114     ("""
115 <<<<<<<
116 ***1***
117 =======
118
119 # MySQL specific settings
120 $wgDBprefix         = "";
121 >>>>>>>
122 """, ["\n# MySQL specific settings", 1]),
123     ("""
124 <<<<<<<
125 ## is writable, then uncomment this:
126 ***1***
127 =======
128 ## is writable, then set this to true:
129 $wgEnableUploads       = false;
130 >>>>>>>
131 """, [-1]),
132     ("""
133 <<<<<<<
134 ***1***
135 $wgMathPath         = "{$wgUploadPath}/math";
136 $wgMathDirectory    = "{$wgUploadDirectory}/math";
137 $wgTmpDirectory     = "{$wgUploadDirectory}/tmp";
138 =======
139 $wgUseTeX           = false;
140 >>>>>>>
141 """, [1]),
142     # order of these rules is important
143     ("""
144 <<<<<<<
145 $configdate = gmdate( 'YmdHis', @filemtime( __FILE__ ) );
146 $wgCacheEpoch = max( $wgCacheEpoch, $configdate );
147 ***1***
148 ?>
149 =======
150 $wgCacheEpoch = max( $wgCacheEpoch, gmdate( 'YmdHis', @filemtime( __FILE__ ) ) );
151 >>>>>>>
152 """, [0, 1]),
153     ("""
154 <<<<<<<
155 $configdate = gmdate( 'YmdHis', @filemtime( __FILE__ ) );
156 $wgCacheEpoch = max( $wgCacheEpoch, $configdate );
157 ***1***
158 =======
159 $wgCacheEpoch = max( $wgCacheEpoch, gmdate( 'YmdHis', @filemtime( __FILE__ ) ) );
160 >>>>>>>
161 """, [0, 1]),
162     ("""
163 <<<<<<<
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 """, [0]),
171     ("""
172 <<<<<<<
173 ***1***
174 ?>
175 =======
176 # When you make changes to this configuration file, this will make
177 # sure that cached pages are cleared.
178 $wgCacheEpoch = max( $wgCacheEpoch, gmdate( 'YmdHis', @filemtime( __FILE__ ) ) );
179 >>>>>>>
180 """, [1, 0]),
181     ("""
182 <<<<<<<
183 ***1***
184 =======
185 # When you make changes to this configuration file, this will make
186 # sure that cached pages are cleared.
187 $wgCacheEpoch = max( $wgCacheEpoch, gmdate( 'YmdHis', @filemtime( __FILE__ ) ) );
188 >>>>>>>
189 """, [1, 0]),
190     ]
191 }
192