2 import distutils.version
9 from wizard import app, deploy, install, resolve, scripts, shell, util
10 from wizard.app import php
12 def make_filename_regex(var):
13 return 'LocalSettings.php', re.compile('^(\$' + app.expand_re(var) + r'''\s*=\s*)(.*)(;)''', re.M)
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'),
28 'LocalSettings.php': [
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
38 $wgScriptExtension = ".php";
40 ## UPO means: this is also a user preference option
48 # MySQL specific settings
51 """, ["\n# MySQL specific settings", 1]),
54 ## is writable, then uncomment this:
57 ## is writable, then set this to true:
58 $wgEnableUploads = false;
64 $wgMathPath = "{$wgUploadPath}/math";
65 $wgMathDirectory = "{$wgUploadDirectory}/math";
66 $wgTmpDirectory = "{$wgUploadDirectory}/tmp";
71 # order of these rules is important
74 $configdate = gmdate( 'YmdHis', @filemtime( __FILE__ ) );
75 $wgCacheEpoch = max( $wgCacheEpoch, $configdate );
79 $wgCacheEpoch = max( $wgCacheEpoch, gmdate( 'YmdHis', @filemtime( __FILE__ ) ) );
84 $configdate = gmdate( 'YmdHis', @filemtime( __FILE__ ) );
85 $wgCacheEpoch = max( $wgCacheEpoch, $configdate );
88 $wgCacheEpoch = max( $wgCacheEpoch, gmdate( 'YmdHis', @filemtime( __FILE__ ) ) );
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__ ) ) );
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__ ) ) );
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__ ) ) );
122 class Application(app.Application):
123 parametrized_files = ['LocalSettings.php', 'php.ini']
124 deprecated_keys = set(['WIZARD_IP']) | php.deprecated_keys
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
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
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"))
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:
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
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)
186 def install(self, version, options):
188 os.unlink("LocalSettings.php")
192 os.chmod("config", 0777) # XXX: vaguely sketchy
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,
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):
215 if not os.path.isfile("AdminSettings.php"):
216 sh.call("git", "checkout", "-q", "mediawiki-" + str(version), "--", "AdminSettings.php")
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):
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):
232 if os.path.exists(outdir):
233 util.safe_unlink(outdir)
235 outfile = os.path.join(outdir, "db.sql")
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)
243 def restore(self, deployment, backup, options):
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)
251 sh.call("mysql", *get_mysql_args(deployment), stdin=sql)
254 def get_mysql_args(d):
255 # XXX: add support for getting these out of options
257 if 'WIZARD_DBNAME' not in vars:
258 raise app.BackupFailure("Could not determine database name")
259 triplet = scripts.get_sql_credentials(vars)
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]