]> scripts.mit.edu Git - wizard.git/blob - wizard/app/mediawiki.py
Move Application/ApplicationVersion to wizard.app
[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 make_extractor = app.filename_regex_extractor(make_filename_regex)
16 make_substitution = app.filename_regex_substitution(make_filename_regex)
17 seed = {
18         'WIZARD_IP': 'IP', # obsolete, remove after we're done
19         'WIZARD_SITENAME': 'wgSitename',
20         'WIZARD_SCRIPTPATH': 'wgScriptPath',
21         'WIZARD_EMERGENCYCONTACT': ('wgEmergencyContact', 'wgPasswordSender'),
22         'WIZARD_DBSERVER': 'wgDBserver',
23         'WIZARD_DBNAME': 'wgDBname',
24         'WIZARD_DBUSER': 'wgDBuser',
25         'WIZARD_DBPASSWORD': 'wgDBpassword',
26         'WIZARD_SECRETKEY': ('wgSecretKey', 'wgProxyKey'),
27         }
28
29 resolutions = {
30 'LocalSettings.php': [
31     ("""
32 <<<<<<<
33 ***1***
34 =======
35 ## The URL base path to the directory containing the wiki;
36 ## defaults for all runtime URL paths are based off of this.
37 ## For more information on customizing the URLs please see:
38 ## http://www.mediawiki.org/wiki/Manual:Short_URL
39 ***2***
40 $wgScriptExtension  = ".php";
41
42 ## UPO means: this is also a user preference option
43 >>>>>>>
44 """, [-1]),
45     ("""
46 <<<<<<<
47 ***1***
48 =======
49
50 # MySQL specific settings
51 $wgDBprefix         = "";
52 >>>>>>>
53 """, ["\n# MySQL specific settings", 1]),
54     ("""
55 <<<<<<<
56 ## is writable, then uncomment this:
57 ***1***
58 =======
59 ## is writable, then set this to true:
60 $wgEnableUploads       = false;
61 >>>>>>>
62 """, [-1]),
63     ("""
64 <<<<<<<
65 ***1***
66 $wgMathPath         = "{$wgUploadPath}/math";
67 $wgMathDirectory    = "{$wgUploadDirectory}/math";
68 $wgTmpDirectory     = "{$wgUploadDirectory}/tmp";
69 =======
70 $wgUseTeX           = false;
71 >>>>>>>
72 """, [1]),
73     # order of these rules is important
74     ("""
75 <<<<<<<
76 $configdate = gmdate( 'YmdHis', @filemtime( __FILE__ ) );
77 $wgCacheEpoch = max( $wgCacheEpoch, $configdate );
78 ***1***
79 ?>
80 =======
81 $wgCacheEpoch = max( $wgCacheEpoch, gmdate( 'YmdHis', @filemtime( __FILE__ ) ) );
82 >>>>>>>
83 """, [0, 1]),
84     ("""
85 <<<<<<<
86 $configdate = gmdate( 'YmdHis', @filemtime( __FILE__ ) );
87 $wgCacheEpoch = max( $wgCacheEpoch, $configdate );
88 ***1***
89 =======
90 $wgCacheEpoch = max( $wgCacheEpoch, gmdate( 'YmdHis', @filemtime( __FILE__ ) ) );
91 >>>>>>>
92 """, [0, 1]),
93     ("""
94 <<<<<<<
95 ?>
96 =======
97 # When you make changes to this configuration file, this will make
98 # sure that cached pages are cleared.
99 $wgCacheEpoch = max( $wgCacheEpoch, gmdate( 'YmdHis', @filemtime( __FILE__ ) ) );
100 >>>>>>>
101 """, [0]),
102     ("""
103 <<<<<<<
104 ***1***
105 ?>
106 =======
107 # When you make changes to this configuration file, this will make
108 # sure that cached pages are cleared.
109 $wgCacheEpoch = max( $wgCacheEpoch, gmdate( 'YmdHis', @filemtime( __FILE__ ) ) );
110 >>>>>>>
111 """, [1, 0]),
112     ("""
113 <<<<<<<
114 ***1***
115 =======
116 # When you make changes to this configuration file, this will make
117 # sure that cached pages are cleared.
118 $wgCacheEpoch = max( $wgCacheEpoch, gmdate( 'YmdHis', @filemtime( __FILE__ ) ) );
119 >>>>>>>
120 """, [1, 0]),
121     ]
122 }
123
124 class Application(app.Application):
125     parametrized_files = ['LocalSettings.php', 'php.ini']
126     deprecated_keys = set(['WIZARD_IP']) | php.deprecated_keys
127     @property
128     def extractors(self):
129         if not self._extractors:
130             self._extractors = util.dictmap(make_extractor, seed)
131             self._extractors.update(php.extractors)
132         return self._extractors
133     @property
134     def substitutions(self):
135         if not self._substitutions:
136             self._substitutions = util.dictkmap(make_substitution, seed)
137             self._substitutions.update(php.substitutions)
138         return self._substitutions
139     @property
140     def install_handler(self):
141         handler = install.ArgHandler("mysql", "admin", "email")
142         handler.add(install.Arg("title", help="Title of your new MediaWiki install"))
143         return handler
144     def checkConfig(self, deployment):
145         return os.path.isfile(os.path.join(deployment.location, "LocalSettings.php"))
146     def detectVersion(self, deployment):
147         contents = deployment.read("includes/DefaultSettings.php")
148         regex = make_filename_regex("wgVersion")[1]
149         match = regex.search(contents)
150         if not match: return None
151         return distutils.version.LooseVersion(match.group(2)[1:-1])
152     def checkWeb(self, d, out=None):
153         page = d.fetch("/index.php?title=Main_Page")
154         if type(out) is list:
155             out.append(page)
156         return page.find("<!-- Served") != -1
157     def prepareMerge(self, dir):
158         # XXX: this should be factored out
159         old_contents = open("LocalSettings.php", "r").read()
160         contents = old_contents
161         while "\r\n" in contents:
162             contents = contents.replace("\r\n", "\n")
163         contents = contents.replace("\r", "\n")
164         if contents != old_contents:
165             logging.info("Converted LocalSettings.php to UNIX file endings")
166             open("LocalSettings.php", "w").write(contents)
167     def resolveConflicts(self, dir):
168         # XXX: this is pretty generic
169         resolved = True
170         sh = shell.Shell()
171         for status in sh.eval("git", "ls-files", "--unmerged").splitlines():
172             file = status.split()[-1]
173             if file in resolutions:
174                 contents = open(file, "r").read()
175                 for spec, result in resolutions[file]:
176                     old_contents = contents
177                     contents = resolve.resolve(contents, spec, result)
178                     if old_contents != contents:
179                         logging.info("Did resolution with spec:\n" + spec)
180                 open(file, "w").write(contents)
181                 if not resolve.is_conflict(contents):
182                     sh.call("git", "add", file)
183                 else:
184                     resolved = False
185             else:
186                 resolved = False
187         return resolved
188     def install(self, version, options):
189         try:
190             os.unlink("LocalSettings.php")
191         except OSError:
192             pass
193
194         os.chmod("config", 0777) # XXX: vaguely sketchy
195
196         postdata = {
197             'Sitename': options.title,
198             'EmergencyContact': options.email,
199             'LanguageCode': 'en',
200             'DBserver': options.mysql_host,
201             'DBname': options.mysql_db,
202             'DBuser': options.mysql_user,
203             'DBpassword': options.mysql_password,
204             'DBpassword2': options.mysql_password,
205             'defaultEmail': options.email,
206             'SysopName': options.admin_name,
207             'SysopPass': options.admin_password,
208             'SysopPass2': options.admin_password,
209             }
210         result = install.fetch(options, '/config/index.php', post=postdata)
211         if options.verbose: print result
212         if result.find("Installation successful") == -1:
213             raise install.Failure()
214         os.rename('config/LocalSettings.php', 'LocalSettings.php')
215     def upgrade(self, d, version, options):
216         sh = shell.Shell()
217         if not os.path.isfile("AdminSettings.php"):
218             sh.call("git", "checkout", "-q", "mediawiki-" + str(version), "--", "AdminSettings.php")
219         try:
220             result = sh.eval("php", "maintenance/update.php", "--quick", log=True)
221         except shell.CallError as e:
222             raise app.UpgradeFailure("Update script returned non-zero exit code\nSTDOUT: %s\nSTDERR: %s" % (e.stdout, e.stderr))
223         results = result.rstrip().split()
224         if not results or not results[-1] == "Done.":
225             raise app.UpgradeFailure(result)
226     def backup(self, deployment, options):
227         sh = shell.Shell()
228         # XXX: duplicate code, refactor, also, race condition
229         backupdir = os.path.join(".scripts", "backups")
230         backup = str(deployment.version) + "-" + datetime.date.today().isoformat()
231         outdir = os.path.join(backupdir, backup)
232         if not os.path.exists(backupdir):
233             os.mkdir(backupdir)
234         if os.path.exists(outdir):
235             util.safe_unlink(outdir)
236         os.mkdir(outdir)
237         outfile = os.path.join(outdir, "db.sql")
238         try:
239             sh.call("mysqldump", "--compress", "-r", outfile, *get_mysql_args(deployment))
240             sh.call("gzip", "--best", outfile)
241         except shell.CallError as e:
242             shutil.rmtree(outdir)
243             raise app.BackupFailure(e.stderr)
244         return backup
245     def restore(self, deployment, backup, options):
246         sh = shell.Shell()
247         backup_dir = os.path.join(".scripts", "backups", backup)
248         if not os.path.exists(backup_dir):
249             raise app.RestoreFailure("Backup %s doesn't exist", backup)
250         sql = open(os.path.join(backup_dir, "db.sql"), 'w+')
251         sh.call("gunzip", "-c", os.path.join(backup_dir, "db.sql.gz"), stdout=sql)
252         sql.seek(0)
253         sh.call("mysql", *get_mysql_args(deployment), stdin=sql)
254         sql.close()
255
256 def get_mysql_args(d):
257     # XXX: add support for getting these out of options
258     vars = d.extract()
259     if 'WIZARD_DBNAME' not in vars:
260         raise app.BackupFailure("Could not determine database name")
261     triplet = scripts.get_sql_credentials(vars)
262     args = []
263     if triplet is not None:
264         server, user, password = triplet
265         args += ["-h", server, "-u", user, "-p" + password]
266     name = shlex.split(vars['WIZARD_DBNAME'])[0]
267     args.append(name)
268     return args