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