]> scripts.mit.edu Git - wizard.git/blob - wizard/app/mediawiki.py
More bugfixes from running live.
[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(deploy.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         with util.ChangeDirectory(dir):
159             # XXX: this should be factored out
160             old_contents = open("LocalSettings.php", "r").read()
161             contents = old_contents
162             while "\r\n" in contents:
163                 contents = contents.replace("\r\n", "\n")
164             contents = contents.replace("\r", "\n")
165             if contents != old_contents:
166                 logging.info("Converted LocalSettings.php to UNIX file endings")
167                 open("LocalSettings.php", "w").write(contents)
168     def resolveConflicts(self, dir):
169         # XXX: this is pretty generic
170         resolved = True
171         with util.ChangeDirectory(dir):
172             sh = shell.Shell()
173             for status in sh.eval("git", "ls-files", "--unmerged").splitlines():
174                 file = status.split()[-1]
175                 if file in resolutions:
176                     contents = open(file, "r").read()
177                     for spec, result in resolutions[file]:
178                         old_contents = contents
179                         contents = resolve.resolve(contents, spec, result)
180                         if old_contents != contents:
181                             logging.info("Did resolution with spec:\n" + spec)
182                     open(file, "w").write(contents)
183                     if not resolve.is_conflict(contents):
184                         sh.call("git", "add", file)
185                     else:
186                         resolved = False
187                 else:
188                     resolved = False
189         return resolved
190     def install(self, version, options):
191         try:
192             os.unlink("LocalSettings.php")
193         except OSError:
194             pass
195
196         os.chmod("config", 0777) # XXX: vaguely sketchy
197
198         postdata = {
199             'Sitename': options.title,
200             'EmergencyContact': options.email,
201             'LanguageCode': 'en',
202             'DBserver': options.mysql_host,
203             'DBname': options.mysql_db,
204             'DBuser': options.mysql_user,
205             'DBpassword': options.mysql_password,
206             'DBpassword2': options.mysql_password,
207             'defaultEmail': options.email,
208             'SysopName': options.admin_name,
209             'SysopPass': options.admin_password,
210             'SysopPass2': options.admin_password,
211             }
212         result = install.fetch(options, '/config/index.php', post=postdata)
213         if options.verbose: print result
214         if result.find("Installation successful") == -1:
215             raise install.Failure()
216         os.rename('config/LocalSettings.php', 'LocalSettings.php')
217     def upgrade(self, d, version, options):
218         sh = shell.Shell()
219         if not os.path.isfile("AdminSettings.php"):
220             sh.call("git", "checkout", "-q", "mediawiki-" + str(version), "--", "AdminSettings.php")
221         try:
222             result = sh.eval("php", "maintenance/update.php", "--quick", log=True)
223         except shell.CallError as e:
224             raise app.UpgradeFailure("Update script returned non-zero exit code\nSTDOUT: %s\nSTDERR: %s" % (e.stdout, e.stderr))
225         results = result.rstrip().split()
226         if not results or not results[-1] == "Done.":
227             raise app.UpgradeFailure(result)
228     def backup(self, deployment, options):
229         sh = shell.Shell()
230         # XXX: duplicate code, refactor, also, race condition
231         backupdir = os.path.join(".scripts", "backups")
232         backup = str(deployment.version) + "-" + datetime.date.today().isoformat()
233         outdir = os.path.join(backupdir, backup)
234         if not os.path.exists(backupdir):
235             os.mkdir(backupdir)
236         if os.path.exists(outdir):
237             util.safe_unlink(outdir)
238         os.mkdir(outdir)
239         outfile = os.path.join(outdir, "db.sql")
240         try:
241             sh.call("mysqldump", "--compress", "-r", outfile, *get_mysql_args(deployment))
242             sh.call("gzip", "--best", outfile)
243         except shell.CallError as e:
244             shutil.rmtree(outdir)
245             raise app.BackupFailure(e.stderr)
246         return backup
247     def restore(self, deployment, backup, options):
248         sh = shell.Shell()
249         backup_dir = os.path.join(".scripts", "backups", backup)
250         if not os.path.exists(backup_dir):
251             raise app.RestoreFailure("Backup %s doesn't exist", backup)
252         sql = open(os.path.join(backup_dir, "db.sql"), 'w+')
253         sh.call("gunzip", "-c", os.path.join(backup_dir, "db.sql.gz"), stdout=sql)
254         sql.seek(0)
255         sh.call("mysql", *get_mysql_args(deployment), stdin=sql)
256         sql.close()
257
258 def get_mysql_args(d):
259     # XXX: add support for getting these out of options
260     vars = d.extract()
261     if 'WIZARD_DBNAME' not in vars:
262         raise app.BackupFailure("Could not determine database name")
263     triplet = scripts.get_sql_credentials(vars)
264     args = []
265     if triplet is not None:
266         server, user, password = triplet
267         args += ["-h", server, "-u", user, "-p" + password]
268     name = shlex.split(vars['WIZARD_DBNAME'])[0]
269     args.append(name)
270     return args