]> scripts.mit.edu Git - wizard.git/blob - wizard/app/mediawiki.py
Move generic functionality out of wizard.app.mediawiki.
[wizard.git] / wizard / app / mediawiki.py
1 import re
2 import distutils.version
3 import os
4 import datetime
5 import logging
6 import shlex
7 import shutil
8
9 from wizard import app, deploy, install, resolve, scripts, shell, util
10 from wizard.app import php
11
12 def make_filename_regex(var):
13     return 'LocalSettings.php', re.compile('^(\$' + app.expand_re(var) + r'''\s*=\s*)(.*)(;)''', re.M)
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.ini']
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     @property
35     def install_handler(self):
36         handler = install.ArgHandler("mysql", "admin", "email")
37         handler.add(install.Arg("title", help="Title of your new MediaWiki install"))
38         return handler
39     def checkConfig(self, deployment):
40         return os.path.isfile(os.path.join(deployment.location, "LocalSettings.php"))
41     def detectVersion(self, deployment):
42         contents = deployment.read("includes/DefaultSettings.php")
43         regex = make_filename_regex("wgVersion")[1]
44         match = regex.search(contents)
45         if not match: return None
46         return distutils.version.LooseVersion(match.group(2)[1:-1])
47     def checkWeb(self, deployment, out=None):
48         page = deployment.fetch("/index.php?title=Main_Page")
49         if type(out) is list:
50             out.append(page)
51         return page.find("<!-- Served") != -1
52     def prepareMerge(self, deployment):
53         resolve.fix_newlines("LocalSettings.php")
54     def install(self, version, options):
55         try:
56             os.unlink("LocalSettings.php")
57         except OSError:
58             pass
59
60         os.chmod("config", 0777) # XXX: vaguely sketchy
61
62         postdata = {
63             'Sitename': options.title,
64             'EmergencyContact': options.email,
65             'LanguageCode': 'en',
66             'DBserver': options.mysql_host,
67             'DBname': options.mysql_db,
68             'DBuser': options.mysql_user,
69             'DBpassword': options.mysql_password,
70             'DBpassword2': options.mysql_password,
71             'defaultEmail': options.email,
72             'SysopName': options.admin_name,
73             'SysopPass': options.admin_password,
74             'SysopPass2': options.admin_password,
75             }
76         result = install.fetch(options, '/config/index.php', post=postdata)
77         if options.verbose or options.debug: print result
78         if result.find("Installation successful") == -1:
79             raise install.Failure()
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