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