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(deploy.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 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
171 with util.ChangeDirectory(dir):
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)
190 def install(self, version, options):
192 os.unlink("LocalSettings.php")
196 os.chmod("config", 0777) # XXX: vaguely sketchy
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,
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):
219 if not os.path.isfile("AdminSettings.php"):
220 sh.call("git", "checkout", "-q", "mediawiki-" + str(version), "--", "AdminSettings.php")
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):
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):
236 if os.path.exists(outdir):
237 util.safe_unlink(outdir)
239 outfile = os.path.join(outdir, "db.sql")
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)
247 def restore(self, deployment, backup, options):
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)
255 sh.call("mysql", *get_mysql_args(deployment), stdin=sql)
258 def get_mysql_args(d):
259 # XXX: add support for getting these out of options
261 if 'WIZARD_DBNAME' not in vars:
262 raise app.BackupFailure("Could not determine database name")
263 triplet = scripts.get_sql_credentials(vars)
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]