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 make_extractor = app.filename_regex_extractor(make_filename_regex)
16 make_substitution = app.filename_regex_substitution(make_filename_regex)
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'),
30 'LocalSettings.php': [
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
40 $wgScriptExtension = ".php";
42 ## UPO means: this is also a user preference option
50 # MySQL specific settings
53 """, ["\n# MySQL specific settings", 1]),
56 ## is writable, then uncomment this:
59 ## is writable, then set this to true:
60 $wgEnableUploads = false;
66 $wgMathPath = "{$wgUploadPath}/math";
67 $wgMathDirectory = "{$wgUploadDirectory}/math";
68 $wgTmpDirectory = "{$wgUploadDirectory}/tmp";
73 # order of these rules is important
76 $configdate = gmdate( 'YmdHis', @filemtime( __FILE__ ) );
77 $wgCacheEpoch = max( $wgCacheEpoch, $configdate );
81 $wgCacheEpoch = max( $wgCacheEpoch, gmdate( 'YmdHis', @filemtime( __FILE__ ) ) );
86 $configdate = gmdate( 'YmdHis', @filemtime( __FILE__ ) );
87 $wgCacheEpoch = max( $wgCacheEpoch, $configdate );
90 $wgCacheEpoch = max( $wgCacheEpoch, gmdate( 'YmdHis', @filemtime( __FILE__ ) ) );
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__ ) ) );
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__ ) ) );
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__ ) ) );
124 class Application(app.Application):
125 parametrized_files = ['LocalSettings.php', 'php.ini']
126 deprecated_keys = set(['WIZARD_IP']) | php.deprecated_keys
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
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
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"))
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:
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
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)
188 def install(self, version, options):
190 os.unlink("LocalSettings.php")
194 os.chmod("config", 0777) # XXX: vaguely sketchy
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,
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):
217 if not os.path.isfile("AdminSettings.php"):
218 sh.call("git", "checkout", "-q", "mediawiki-" + str(version), "--", "AdminSettings.php")
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):
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):
234 if os.path.exists(outdir):
235 util.safe_unlink(outdir)
237 outfile = os.path.join(outdir, "db.sql")
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)
245 def restore(self, deployment, backup, options):
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)
253 sh.call("mysql", *get_mysql_args(deployment), stdin=sql)
256 def get_mysql_args(d):
257 # XXX: add support for getting these out of options
259 if 'WIZARD_DBNAME' not in vars:
260 raise app.BackupFailure("Could not determine database name")
261 triplet = scripts.get_sql_credentials(vars)
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]