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