2 import distutils.version
9 from wizard import app, install, resolve, shell, sql, util
10 from wizard.app import php
12 # Note: Maintenance script exit codes
13 # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
14 # MediaWiki has notoriously spotty support for exit codes. It has
15 # gotten better, but there are still always cases that slip through,
16 # such as <https://bugzilla.wikimedia.org/show_bug.cgi?id=29588>.
17 # As a result, we check both for exit codes AND for the "success"
20 def make_filename_regex(var):
21 """See :ref:`versioning config <seed>` for more information."""
22 return 'LocalSettings.php', php.re_var(var)
24 seed = util.dictmap(make_filename_regex, {
25 'WIZARD_IP': 'IP', # obsolete, remove after we're done
26 'WIZARD_SITENAME': 'wgSitename',
27 'WIZARD_META_NAMESPACE': 'wgMetaNamespace',
28 'WIZARD_SCRIPTPATH': 'wgScriptPath',
29 'WIZARD_SERVER': 'wgServer',
30 'WIZARD_EMERGENCYCONTACT': ('wgEmergencyContact', 'wgPasswordSender'),
31 'WIZARD_DBSERVER': 'wgDBserver',
32 'WIZARD_DBNAME': 'wgDBname',
33 'WIZARD_DBUSER': 'wgDBuser',
34 'WIZARD_DBPASSWORD': 'wgDBpassword',
35 'WIZARD_SECRETKEY': ('wgSecretKey', 'wgProxyKey'),
36 'WIZARD_UPGRADEKEY': 'wgUpgradeKey',
39 class Application(app.Application):
40 fullname = "MediaWiki"
42 parametrized_files = ['LocalSettings.php'] + php.parametrized_files
43 deprecated_keys = set(['WIZARD_IP']) | php.deprecated_keys
44 extractors = app.make_extractors(seed)
45 extractors.update(php.extractors)
46 substitutions = app.make_substitutions(seed)
47 substitutions.update(php.substitutions)
48 install_schema = install.ArgSchema("db", "admin", "email", "title")
49 random_keys = set(['WIZARD_SECRETKEY', 'WIZARD_UPGRADEKEY'])
50 def download(self, version):
51 series = ".".join(str(version).split(".")[:2])
52 return "http://download.wikimedia.org/mediawiki/%s/mediawiki-%s.tar.gz" % (series, version)
53 def checkConfig(self, deployment):
54 return os.path.isfile("LocalSettings.php")
55 def detectVersion(self, deployment):
56 return self.detectVersionFromFile("includes/DefaultSettings.php", php.re_var("wgVersion"))
57 def checkWeb(self, deployment):
58 return self.checkWebPage(deployment, "/index.php?title=Main_Page", outputs=["<!-- Served"])
59 def install(self, version, options):
60 php.ini_replace_vars()
61 if version >= distutils.version.LooseVersion("1.17.0"):
62 # The --email option was added in 1.16.3 and removed in 1.18.0
63 has_email = version < distutils.version.LooseVersion("1.18.0")
64 self.install_1_17_0(options, has_email)
66 self.install_old(options)
67 def install_1_17_0(self, options, has_email):
68 util.soft_unlink("LocalSettings.php")
69 if os.path.exists("math"):
70 with util.ChangeDirectory("math"):
73 dbpass_fd, out_fd = os.pipe()
74 os.write(out_fd, options.dsn.password)
76 pass_fd, out_fd = os.pipe()
77 os.write(out_fd, options.admin_password)
81 "php", "-c", ".", "maintenance/install.php",
82 "--dbname", options.dsn.database,
83 "--dbpassfile", "php://fd/%d" % (dbpass_fd,),
84 "--dbserver", options.dsn.host,
85 "--dbuser", options.dsn.username,
86 "--passfile", "php://fd/%d" % (pass_fd,),
87 "--server", "https://" + options.web_host,
88 "--scriptpath", options.web_path,
92 "--email", options.email,
94 args += (options.title, options.admin_name,)
101 "php", "-c", ".", "maintenance/resetUserEmail.php",
102 "--no-reset-password",
103 options.admin_name, options.email,
106 except shell.CallError as e:
107 raise app.RecoverableInstallFailure(["Install script returned non-zero exit code\nSTDOUT: %s\nSTDERR: %s" % (e.stdout, e.stderr)])
108 logging.debug("Install script output:\n\n" + result)
109 # See [Note: Maintenance script exit codes]
110 results = result.rstrip().split()
111 if not results or not results[-1] == "done":
112 raise app.RecoverableInstallFailure([result])
114 def install_old(self, options):
115 util.soft_unlink("LocalSettings.php")
116 os.chmod("config", 0777) # XXX: vaguely sketchy
119 'Sitename': options.title,
120 'EmergencyContact': options.email,
121 'LanguageCode': 'en',
122 'DBserver': options.dsn.host,
123 'DBname': options.dsn.database,
124 'DBuser': options.dsn.username,
125 'DBpassword': options.dsn.password,
126 'DBpassword2': options.dsn.password,
127 'defaultEmail': options.email,
128 'SysopName': options.admin_name,
129 'SysopPass': options.admin_password,
130 'SysopPass2': options.admin_password,
132 result = install.fetch(options, '/config/index.php', post=postdata)
133 result_etree = lxml.etree.parse(StringIO.StringIO(result), lxml.etree.HTMLParser())
134 selector = lxml.cssselect.CSSSelector(".error")
135 error_messages = [e.text for e in selector(result_etree)]
136 logging.debug("Installation output:\n\n" + result)
137 if result.find("Installation successful") == -1:
138 if not error_messages:
139 raise app.InstallFailure()
141 raise app.RecoverableInstallFailure(error_messages)
142 os.rename('config/LocalSettings.php', 'LocalSettings.php')
144 def upgrade(self, d, version, options):
145 if not os.path.isfile("AdminSettings.php"):
146 shell.call("git", "checkout", "-q", "mediawiki-" + str(version), "--", "AdminSettings.php")
147 if os.path.exists("math"):
148 with util.ChangeDirectory("math"):
151 result = shell.eval("php", "maintenance/update.php", "--quick", log=True)
152 except shell.CallError as e:
153 raise app.UpgradeFailure("Update script returned non-zero exit code\nSTDOUT: %s\nSTDERR: %s" % (e.stdout, e.stderr))
154 logging.debug("Upgrade script output:\n\n" + result)
155 # See [Note: Maintenance script exit codes]
156 results = result.rstrip().split()
157 if not results or not results[-1] == "Done.":
158 raise app.UpgradeFailure(result)
159 @app.throws_database_errors
160 def backup(self, deployment, backup_dir, options):
161 sql.backup(backup_dir, deployment)
162 @app.throws_database_errors
163 def restore(self, deployment, backup_dir, options):
164 sql.restore(backup_dir, deployment)
165 @app.throws_database_errors
166 def remove(self, deployment, options):
167 sql.drop(deployment.dsn)
168 def researchFilter(self, filename, added, deleted):
169 if filename == "LocalSettings.php":
170 return added == deleted == 10 or added == deleted == 9
171 elif filename == "AdminSettings.php":
172 return added == 0 and deleted == 20
173 elif filename == "config/index.php" or filename == "config/index.php5":
177 Application.resolutions = {
178 'LocalSettings.php': [
183 ## The URL base path to the directory containing the wiki;
184 ## defaults for all runtime URL paths are based off of this.
185 ## For more information on customizing the URLs please see:
186 ## http://www.mediawiki.org/wiki/Manual:Short_URL
188 $wgScriptExtension = ".php";
190 ## UPO means: this is also a user preference option
198 # MySQL specific settings
201 """, ["\n# MySQL specific settings", 1]),
204 ## is writable, then uncomment this:
207 ## is writable, then set this to true:
208 $wgEnableUploads = false;
214 $wgMathPath = "{$wgUploadPath}/math";
215 $wgMathDirectory = "{$wgUploadDirectory}/math";
216 $wgTmpDirectory = "{$wgUploadDirectory}/tmp";
221 # order of these rules is important
224 $configdate = gmdate( 'YmdHis', @filemtime( __FILE__ ) );
225 $wgCacheEpoch = max( $wgCacheEpoch, $configdate );
229 $wgCacheEpoch = max( $wgCacheEpoch, gmdate( 'YmdHis', @filemtime( __FILE__ ) ) );
234 $configdate = gmdate( 'YmdHis', @filemtime( __FILE__ ) );
235 $wgCacheEpoch = max( $wgCacheEpoch, $configdate );
238 $wgCacheEpoch = max( $wgCacheEpoch, gmdate( 'YmdHis', @filemtime( __FILE__ ) ) );
245 # When you make changes to this configuration file, this will make
246 # sure that cached pages are cleared.
247 $wgCacheEpoch = max( $wgCacheEpoch, gmdate( 'YmdHis', @filemtime( __FILE__ ) ) );
255 # When you make changes to this configuration file, this will make
256 # sure that cached pages are cleared.
257 $wgCacheEpoch = max( $wgCacheEpoch, gmdate( 'YmdHis', @filemtime( __FILE__ ) ) );
264 # When you make changes to this configuration file, this will make
265 # sure that cached pages are cleared.
266 $wgCacheEpoch = max( $wgCacheEpoch, gmdate( 'YmdHis', @filemtime( __FILE__ ) ) );