]> scripts.mit.edu Git - wizard.git/blob - wizard/app/mediawiki.py
Revamp database infrastructure.
[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         sh = shell.Shell()
75         if not os.path.isfile("AdminSettings.php"):
76             sh.call("git", "checkout", "-q", "mediawiki-" + str(version), "--", "AdminSettings.php")
77         try:
78             result = sh.eval("php", "maintenance/update.php", "--quick", log=True)
79         except shell.CallError as e:
80             raise app.UpgradeFailure("Update script returned non-zero exit code\nSTDOUT: %s\nSTDERR: %s" % (e.stdout, e.stderr))
81         results = result.rstrip().split()
82         if not results or not results[-1] == "Done.":
83             raise app.UpgradeFailure(result)
84     def backup(self, deployment, backup_dir, options):
85         app.backup_database(backup_dir, deployment)
86     def restore(self, deployment, backup_dir, options):
87         app.restore_database(backup_dir, deployment)
88     def remove(self, deployment, options):
89         app.remove_database(deployment)
90
91 Application.resolutions = {
92 'LocalSettings.php': [
93     ("""
94 <<<<<<<
95 ***1***
96 =======
97 ## The URL base path to the directory containing the wiki;
98 ## defaults for all runtime URL paths are based off of this.
99 ## For more information on customizing the URLs please see:
100 ## http://www.mediawiki.org/wiki/Manual:Short_URL
101 ***2***
102 $wgScriptExtension  = ".php";
103
104 ## UPO means: this is also a user preference option
105 >>>>>>>
106 """, [-1]),
107     ("""
108 <<<<<<<
109 ***1***
110 =======
111
112 # MySQL specific settings
113 $wgDBprefix         = "";
114 >>>>>>>
115 """, ["\n# MySQL specific settings", 1]),
116     ("""
117 <<<<<<<
118 ## is writable, then uncomment this:
119 ***1***
120 =======
121 ## is writable, then set this to true:
122 $wgEnableUploads       = false;
123 >>>>>>>
124 """, [-1]),
125     ("""
126 <<<<<<<
127 ***1***
128 $wgMathPath         = "{$wgUploadPath}/math";
129 $wgMathDirectory    = "{$wgUploadDirectory}/math";
130 $wgTmpDirectory     = "{$wgUploadDirectory}/tmp";
131 =======
132 $wgUseTeX           = false;
133 >>>>>>>
134 """, [1]),
135     # order of these rules is important
136     ("""
137 <<<<<<<
138 $configdate = gmdate( 'YmdHis', @filemtime( __FILE__ ) );
139 $wgCacheEpoch = max( $wgCacheEpoch, $configdate );
140 ***1***
141 ?>
142 =======
143 $wgCacheEpoch = max( $wgCacheEpoch, gmdate( 'YmdHis', @filemtime( __FILE__ ) ) );
144 >>>>>>>
145 """, [0, 1]),
146     ("""
147 <<<<<<<
148 $configdate = gmdate( 'YmdHis', @filemtime( __FILE__ ) );
149 $wgCacheEpoch = max( $wgCacheEpoch, $configdate );
150 ***1***
151 =======
152 $wgCacheEpoch = max( $wgCacheEpoch, gmdate( 'YmdHis', @filemtime( __FILE__ ) ) );
153 >>>>>>>
154 """, [0, 1]),
155     ("""
156 <<<<<<<
157 ?>
158 =======
159 # When you make changes to this configuration file, this will make
160 # sure that cached pages are cleared.
161 $wgCacheEpoch = max( $wgCacheEpoch, gmdate( 'YmdHis', @filemtime( __FILE__ ) ) );
162 >>>>>>>
163 """, [0]),
164     ("""
165 <<<<<<<
166 ***1***
167 ?>
168 =======
169 # When you make changes to this configuration file, this will make
170 # sure that cached pages are cleared.
171 $wgCacheEpoch = max( $wgCacheEpoch, gmdate( 'YmdHis', @filemtime( __FILE__ ) ) );
172 >>>>>>>
173 """, [1, 0]),
174     ("""
175 <<<<<<<
176 ***1***
177 =======
178 # When you make changes to this configuration file, this will make
179 # sure that cached pages are cleared.
180 $wgCacheEpoch = max( $wgCacheEpoch, gmdate( 'YmdHis', @filemtime( __FILE__ ) ) );
181 >>>>>>>
182 """, [1, 0]),
183     ]
184 }
185