import re
import distutils.version
import os
+import lxml.cssselect
+import lxml.etree
+import StringIO
+import logging
-from wizard import app, deploy, install, shell, util
+from wizard import app, install, resolve, shell, sql, util
from wizard.app import php
+# Note: Maintenance script exit codes
+# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+# MediaWiki has notoriously spotty support for exit codes. It has
+# gotten better, but there are still always cases that slip through,
+# such as <https://bugzilla.wikimedia.org/show_bug.cgi?id=29588>.
+# As a result, we check both for exit codes AND for the "success"
+# message in stdout.
+
def make_filename_regex(var):
- return 'LocalSettings.php', re.compile('^(\$' + app.expand_re(var) + r'''\s*=\s*)(.*)(;)''', re.M)
+ """See :ref:`versioning config <seed>` for more information."""
+ return 'LocalSettings.php', php.re_var(var)
-make_extractor = app.filename_regex_extractor(make_filename_regex)
-make_substitution = app.filename_regex_substitution(make_filename_regex)
-seed = {
+seed = util.dictmap(make_filename_regex, {
'WIZARD_IP': 'IP', # obsolete, remove after we're done
'WIZARD_SITENAME': 'wgSitename',
+ 'WIZARD_META_NAMESPACE': 'wgMetaNamespace',
'WIZARD_SCRIPTPATH': 'wgScriptPath',
+ 'WIZARD_SERVER': 'wgServer',
'WIZARD_EMERGENCYCONTACT': ('wgEmergencyContact', 'wgPasswordSender'),
'WIZARD_DBSERVER': 'wgDBserver',
'WIZARD_DBNAME': 'wgDBname',
'WIZARD_DBUSER': 'wgDBuser',
'WIZARD_DBPASSWORD': 'wgDBpassword',
'WIZARD_SECRETKEY': ('wgSecretKey', 'wgProxyKey'),
- }
+ 'WIZARD_UPGRADEKEY': 'wgUpgradeKey',
+ })
-class Application(deploy.Application):
- parametrized_files = ['LocalSettings.php', 'php.ini']
+class Application(app.Application):
+ fullname = "MediaWiki"
+ database = "mysql"
+ parametrized_files = ['LocalSettings.php'] + php.parametrized_files
deprecated_keys = set(['WIZARD_IP']) | php.deprecated_keys
- @property
- def extractors(self):
- if not self._extractors:
- self._extractors = util.dictmap(make_extractor, seed)
- self._extractors.update(php.extractors)
- return self._extractors
- @property
- def substitutions(self):
- if not self._substitutions:
- self._substitutions = util.dictkmap(make_substitution, seed)
- self._substitutions.update(php.substitutions)
- return self._substitutions
- @property
- def install_handler(self):
- handler = install.ArgHandler("mysql", "admin", "email")
- handler.add(install.Arg("title", help="Title of your new MediaWiki install"))
- return handler
+ extractors = app.make_extractors(seed)
+ extractors.update(php.extractors)
+ substitutions = app.make_substitutions(seed)
+ substitutions.update(php.substitutions)
+ install_schema = install.ArgSchema("db", "admin", "email", "title")
+ random_keys = set(['WIZARD_SECRETKEY', 'WIZARD_UPGRADEKEY'])
+ def download(self, version):
+ series = ".".join(str(version).split(".")[:2])
+ return "http://download.wikimedia.org/mediawiki/%s/mediawiki-%s.tar.gz" % (series, version)
def checkConfig(self, deployment):
- return os.path.isfile(os.path.join(deployment.location, "LocalSettings.php"))
+ return os.path.isfile("LocalSettings.php")
def detectVersion(self, deployment):
- contents = deployment.read("includes/DefaultSettings.php")
- regex = make_filename_regex("wgVersion")[1]
- match = regex.search(contents)
- if not match: return None
- return distutils.version.LooseVersion(match.group(2)[1:-1])
+ return self.detectVersionFromFile("includes/DefaultSettings.php", php.re_var("wgVersion"))
+ def checkWeb(self, deployment):
+ return self.checkWebPage(deployment, "/index.php?title=Main_Page", outputs=["<!-- Served"])
def install(self, version, options):
+ php.ini_replace_vars()
+ if version >= distutils.version.LooseVersion("1.17.0"):
+ self.install_1_17_0(options)
+ else:
+ self.install_old(options)
+ def install_1_17_0(self, options):
+ util.soft_unlink("LocalSettings.php")
+ if os.path.exists("math"):
+ with util.ChangeDirectory("math"):
+ shell.call("make")
try:
- os.unlink("LocalSettings.php")
- except OSError:
- pass
+ result = shell.eval(
+ "php", "-c", ".", "maintenance/install.php",
+ "--dbname", options.dsn.database,
+ "--dbpass", options.dsn.password,
+ "--dbserver", options.dsn.host,
+ "--dbuser", options.dsn.username,
+ "--email", options.email,
+ "--pass", options.admin_password,
+ "--server", "https://" + options.web_host,
+ "--scriptpath", options.web_path,
+ options.title, options.admin_name,
+ log=True)
+ except shell.CallError as e:
+ raise app.RecoverableInstallFailure(["Install script returned non-zero exit code\nSTDOUT: %s\nSTDERR: %s" % (e.stdout, e.stderr)])
+ logging.debug("Install script output:\n\n" + result)
+ # See [Note: Maintenance script exit codes]
+ results = result.rstrip().split()
+ if not results or not results[-1] == "done":
+ raise app.RecoverableInstallFailure([result])
+ def install_old(self, options):
+ util.soft_unlink("LocalSettings.php")
os.chmod("config", 0777) # XXX: vaguely sketchy
postdata = {
'Sitename': options.title,
'EmergencyContact': options.email,
'LanguageCode': 'en',
- 'DBserver': options.mysql_host,
- 'DBname': options.mysql_db,
- 'DBuser': options.mysql_user,
- 'DBpassword': options.mysql_password,
- 'DBpassword2': options.mysql_password,
+ 'DBserver': options.dsn.host,
+ 'DBname': options.dsn.database,
+ 'DBuser': options.dsn.username,
+ 'DBpassword': options.dsn.password,
+ 'DBpassword2': options.dsn.password,
'defaultEmail': options.email,
'SysopName': options.admin_name,
'SysopPass': options.admin_password,
'SysopPass2': options.admin_password,
}
- result = install.fetch(options, 'config/index.php', post=postdata)
- if options.verbose: print result
+ result = install.fetch(options, '/config/index.php', post=postdata)
+ result_etree = lxml.etree.parse(StringIO.StringIO(result), lxml.etree.HTMLParser())
+ selector = lxml.cssselect.CSSSelector(".error")
+ error_messages = [e.text for e in selector(result_etree)]
+ logging.debug("Installation output:\n\n" + result)
if result.find("Installation successful") == -1:
- raise install.Failure()
+ if not error_messages:
+ raise app.InstallFailure()
+ else:
+ raise app.RecoverableInstallFailure(error_messages)
os.rename('config/LocalSettings.php', 'LocalSettings.php')
- def upgrade(self, version, options):
- sh = shell.Shell()
+
+ def upgrade(self, d, version, options):
if not os.path.isfile("AdminSettings.php"):
- sh.call("git", "checkout", "mediawiki-" + str(version), "--", "AdminSettings.php")
- result = sh.eval("php", "maintenance/update.php", "--quick", log=True)
- if not result.rstrip().split()[-1] == "Done.":
+ shell.call("git", "checkout", "-q", "mediawiki-" + str(version), "--", "AdminSettings.php")
+ if os.path.exists("math"):
+ with util.ChangeDirectory("math"):
+ shell.call("make")
+ try:
+ result = shell.eval("php", "maintenance/update.php", "--quick", log=True)
+ except shell.CallError as e:
+ raise app.UpgradeFailure("Update script returned non-zero exit code\nSTDOUT: %s\nSTDERR: %s" % (e.stdout, e.stderr))
+ logging.debug("Upgrade script output:\n\n" + result)
+ # See [Note: Maintenance script exit codes]
+ results = result.rstrip().split()
+ if not results or not results[-1] == "Done.":
raise app.UpgradeFailure(result)
+ @app.throws_database_errors
+ def backup(self, deployment, backup_dir, options):
+ sql.backup(backup_dir, deployment)
+ @app.throws_database_errors
+ def restore(self, deployment, backup_dir, options):
+ sql.restore(backup_dir, deployment)
+ @app.throws_database_errors
+ def remove(self, deployment, options):
+ sql.drop(deployment.dsn)
+ def researchFilter(self, filename, added, deleted):
+ if filename == "LocalSettings.php":
+ return added == deleted == 10 or added == deleted == 9
+ elif filename == "AdminSettings.php":
+ return added == 0 and deleted == 20
+ elif filename == "config/index.php" or filename == "config/index.php5":
+ return added == 0
+ return False
+
+Application.resolutions = {
+'LocalSettings.php': [
+ ("""
+<<<<<<<
+***1***
+=======
+## The URL base path to the directory containing the wiki;
+## defaults for all runtime URL paths are based off of this.
+## For more information on customizing the URLs please see:
+## http://www.mediawiki.org/wiki/Manual:Short_URL
+***2***
+$wgScriptExtension = ".php";
+
+## UPO means: this is also a user preference option
+>>>>>>>
+""", [-1]),
+ ("""
+<<<<<<<
+***1***
+=======
+
+# MySQL specific settings
+$wgDBprefix = "";
+>>>>>>>
+""", ["\n# MySQL specific settings", 1]),
+ ("""
+<<<<<<<
+## is writable, then uncomment this:
+***1***
+=======
+## is writable, then set this to true:
+$wgEnableUploads = false;
+>>>>>>>
+""", [-1]),
+ ("""
+<<<<<<<
+***1***
+$wgMathPath = "{$wgUploadPath}/math";
+$wgMathDirectory = "{$wgUploadDirectory}/math";
+$wgTmpDirectory = "{$wgUploadDirectory}/tmp";
+=======
+$wgUseTeX = false;
+>>>>>>>
+""", [1]),
+ # order of these rules is important
+ ("""
+<<<<<<<
+$configdate = gmdate( 'YmdHis', @filemtime( __FILE__ ) );
+$wgCacheEpoch = max( $wgCacheEpoch, $configdate );
+***1***
+?>
+=======
+$wgCacheEpoch = max( $wgCacheEpoch, gmdate( 'YmdHis', @filemtime( __FILE__ ) ) );
+>>>>>>>
+""", [0, 1]),
+ ("""
+<<<<<<<
+$configdate = gmdate( 'YmdHis', @filemtime( __FILE__ ) );
+$wgCacheEpoch = max( $wgCacheEpoch, $configdate );
+***1***
+=======
+$wgCacheEpoch = max( $wgCacheEpoch, gmdate( 'YmdHis', @filemtime( __FILE__ ) ) );
+>>>>>>>
+""", [0, 1]),
+ ("""
+<<<<<<<
+?>
+=======
+# When you make changes to this configuration file, this will make
+# sure that cached pages are cleared.
+$wgCacheEpoch = max( $wgCacheEpoch, gmdate( 'YmdHis', @filemtime( __FILE__ ) ) );
+>>>>>>>
+""", [0]),
+ ("""
+<<<<<<<
+***1***
+?>
+=======
+# When you make changes to this configuration file, this will make
+# sure that cached pages are cleared.
+$wgCacheEpoch = max( $wgCacheEpoch, gmdate( 'YmdHis', @filemtime( __FILE__ ) ) );
+>>>>>>>
+""", [1, 0]),
+ ("""
+<<<<<<<
+***1***
+=======
+# When you make changes to this configuration file, this will make
+# sure that cached pages are cleared.
+$wgCacheEpoch = max( $wgCacheEpoch, gmdate( 'YmdHis', @filemtime( __FILE__ ) ) );
+>>>>>>>
+""", [1, 0]),
+ ]
+}