]> scripts.mit.edu Git - wizard.git/blob - wizard/app/mediawiki.py
Set admin e-mail address properly on MediaWiki >= 1.18.0
[wizard.git] / wizard / app / mediawiki.py
1 import re
2 import distutils.version
3 import os
4 import lxml.cssselect
5 import lxml.etree
6 import StringIO
7 import logging
8
9 from wizard import app, install, resolve, shell, sql, util
10 from wizard.app import php
11
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"
18 # message in stdout.
19
20 def make_filename_regex(var):
21     """See :ref:`versioning config <seed>` for more information."""
22     return 'LocalSettings.php', php.re_var(var)
23
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',
37         })
38
39 class Application(app.Application):
40     fullname = "MediaWiki"
41     database = "mysql"
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)
65         else:
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"):
71                 shell.call("make")
72         try:
73             dbpass_fd, out_fd = os.pipe()
74             os.write(out_fd, options.dsn.password)
75             os.close(out_fd)
76             pass_fd, out_fd = os.pipe()
77             os.write(out_fd, options.admin_password)
78             os.close(out_fd)
79
80             args = (
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,
89             )
90             if has_email:
91                 args += (
92                     "--email", options.email,
93                 )
94             args += (options.title, options.admin_name,)
95             result = shell.eval(
96                 *args,
97                 log=True,
98                 close_fds=False)
99             if not has_email:
100                 shell.eval(
101                     "php", "-c", ".", "maintenance/resetUserEmail.php",
102                     "--no-reset-password",
103                     options.admin_name, options.email,
104                     log=True,
105                 )
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])
113
114     def install_old(self, options):
115         util.soft_unlink("LocalSettings.php")
116         os.chmod("config", 0777) # XXX: vaguely sketchy
117
118         postdata = {
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,
131             }
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()
140             else:
141                 raise app.RecoverableInstallFailure(error_messages)
142         os.rename('config/LocalSettings.php', 'LocalSettings.php')
143
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"):
149                 shell.call("make")
150         try:
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":
174             return added == 0
175         return False
176
177 Application.resolutions = {
178 'LocalSettings.php': [
179     ("""
180 <<<<<<<
181 ***1***
182 =======
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
187 ***2***
188 $wgScriptExtension  = ".php";
189
190 ## UPO means: this is also a user preference option
191 >>>>>>>
192 """, [-1]),
193     ("""
194 <<<<<<<
195 ***1***
196 =======
197
198 # MySQL specific settings
199 $wgDBprefix         = "";
200 >>>>>>>
201 """, ["\n# MySQL specific settings", 1]),
202     ("""
203 <<<<<<<
204 ## is writable, then uncomment this:
205 ***1***
206 =======
207 ## is writable, then set this to true:
208 $wgEnableUploads       = false;
209 >>>>>>>
210 """, [-1]),
211     ("""
212 <<<<<<<
213 ***1***
214 $wgMathPath         = "{$wgUploadPath}/math";
215 $wgMathDirectory    = "{$wgUploadDirectory}/math";
216 $wgTmpDirectory     = "{$wgUploadDirectory}/tmp";
217 =======
218 $wgUseTeX           = false;
219 >>>>>>>
220 """, [1]),
221     # order of these rules is important
222     ("""
223 <<<<<<<
224 $configdate = gmdate( 'YmdHis', @filemtime( __FILE__ ) );
225 $wgCacheEpoch = max( $wgCacheEpoch, $configdate );
226 ***1***
227 ?>
228 =======
229 $wgCacheEpoch = max( $wgCacheEpoch, gmdate( 'YmdHis', @filemtime( __FILE__ ) ) );
230 >>>>>>>
231 """, [0, 1]),
232     ("""
233 <<<<<<<
234 $configdate = gmdate( 'YmdHis', @filemtime( __FILE__ ) );
235 $wgCacheEpoch = max( $wgCacheEpoch, $configdate );
236 ***1***
237 =======
238 $wgCacheEpoch = max( $wgCacheEpoch, gmdate( 'YmdHis', @filemtime( __FILE__ ) ) );
239 >>>>>>>
240 """, [0, 1]),
241     ("""
242 <<<<<<<
243 ?>
244 =======
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__ ) ) );
248 >>>>>>>
249 """, [0]),
250     ("""
251 <<<<<<<
252 ***1***
253 ?>
254 =======
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__ ) ) );
258 >>>>>>>
259 """, [1, 0]),
260     ("""
261 <<<<<<<
262 ***1***
263 =======
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__ ) ) );
267 >>>>>>>
268 """, [1, 0]),
269     ]
270 }
271