]> scripts.mit.edu Git - wizard.git/blob - wizard/app/mediawiki.py
MediaWiki 1.17.0 grew wgUpgradeKey parameter.
[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_SCRIPTPATH': 'wgScriptPath',
28         'WIZARD_EMERGENCYCONTACT': ('wgEmergencyContact', 'wgPasswordSender'),
29         'WIZARD_DBSERVER': 'wgDBserver',
30         'WIZARD_DBNAME': 'wgDBname',
31         'WIZARD_DBUSER': 'wgDBuser',
32         'WIZARD_DBPASSWORD': 'wgDBpassword',
33         'WIZARD_SECRETKEY': ('wgSecretKey', 'wgProxyKey'),
34         'WIZARD_UPGRADEKEY': 'wgUpgradeKey',
35         })
36
37 class Application(app.Application):
38     database = "mysql"
39     parametrized_files = ['LocalSettings.php'] + php.parametrized_files
40     deprecated_keys = set(['WIZARD_IP']) | php.deprecated_keys
41     extractors = app.make_extractors(seed)
42     extractors.update(php.extractors)
43     substitutions = app.make_substitutions(seed)
44     substitutions.update(php.substitutions)
45     install_schema = install.ArgSchema("db", "admin", "email", "title")
46     random_keys = set(['WIZARD_SECRETKEY', 'WIZARD_UPGRADEKEY'])
47     def download(self, version):
48         series = ".".join(str(version).split(".")[:2])
49         return "http://download.wikimedia.org/mediawiki/%s/mediawiki-%s.tar.gz" % (series, version)
50     def checkConfig(self, deployment):
51         return os.path.isfile("LocalSettings.php")
52     def detectVersion(self, deployment):
53         return self.detectVersionFromFile("includes/DefaultSettings.php", php.re_var("wgVersion"))
54     def checkWeb(self, deployment):
55         return self.checkWebPage(deployment, "/index.php?title=Main_Page", outputs=["<!-- Served"])
56     def install(self, version, options):
57         if version >= distutils.version.LooseVersion("1.17.0"):
58             self.install_1_17_0(options)
59         else:
60             self.install_old(options)
61     def install_1_17_0(self, options):
62         util.soft_unlink("LocalSettings.php")
63         try:
64             result = shell.eval(
65                     "php", "maintenance/install.php",
66                     "--dbname", options.dsn.database,
67                     "--dbpass", options.dsn.password,
68                     "--dbserver", options.dsn.host,
69                     "--dbuser", options.dsn.username,
70                     "--email", options.email,
71                     "--pass", options.admin_password,
72                     options.title, options.admin_name,
73                     log=True)
74         except shell.CallError as e:
75             raise app.RecoverableInstallFailure("Install script returned non-zero exit code\nSTDOUT: %s\nSTDERR: %s" % (e.stdout, e.stderr))
76         logging.debug("Install script output:\n\n" + result)
77         # See [Note: Maintenance script exit codes]
78         results = result.rstrip().split()
79         if not results or not results[-1] == "done":
80             raise app.RecoverableInstallFailure(result)
81
82     def install_old(self, options):
83         util.soft_unlink("LocalSettings.php")
84         os.chmod("config", 0777) # XXX: vaguely sketchy
85
86         postdata = {
87             'Sitename': options.title,
88             'EmergencyContact': options.email,
89             'LanguageCode': 'en',
90             'DBserver': options.dsn.host,
91             'DBname': options.dsn.database,
92             'DBuser': options.dsn.username,
93             'DBpassword': options.dsn.password,
94             'DBpassword2': options.dsn.password,
95             'defaultEmail': options.email,
96             'SysopName': options.admin_name,
97             'SysopPass': options.admin_password,
98             'SysopPass2': options.admin_password,
99             }
100         result = install.fetch(options, '/config/index.php', post=postdata)
101         result_etree = lxml.etree.parse(StringIO.StringIO(result), lxml.etree.HTMLParser())
102         selector = lxml.cssselect.CSSSelector(".error")
103         error_messages = [e.text for e in selector(result_etree)]
104         logging.debug("Installation output:\n\n" + result)
105         if result.find("Installation successful") == -1:
106             if not error_messages:
107                 raise app.InstallFailure()
108             else:
109                 raise app.RecoverableInstallFailure(error_messages)
110         os.rename('config/LocalSettings.php', 'LocalSettings.php')
111         php.ini_replace_vars()
112
113     def upgrade(self, d, version, options):
114         if not os.path.isfile("AdminSettings.php"):
115             shell.call("git", "checkout", "-q", "mediawiki-" + str(version), "--", "AdminSettings.php")
116         try:
117             result = shell.eval("php", "maintenance/update.php", "--quick", log=True)
118         except shell.CallError as e:
119             raise app.UpgradeFailure("Update script returned non-zero exit code\nSTDOUT: %s\nSTDERR: %s" % (e.stdout, e.stderr))
120         logging.debug("Upgrade script output:\n\n" + result)
121         # See [Note: Maintenance script exit codes]
122         results = result.rstrip().split()
123         if not results or not results[-1] == "Done.":
124             raise app.UpgradeFailure(result)
125     @app.throws_database_errors
126     def backup(self, deployment, backup_dir, options):
127         sql.backup(backup_dir, deployment)
128     @app.throws_database_errors
129     def restore(self, deployment, backup_dir, options):
130         sql.restore(backup_dir, deployment)
131     @app.throws_database_errors
132     def remove(self, deployment, options):
133         sql.drop(deployment.dsn)
134     def researchFilter(self, filename, added, deleted):
135         if filename == "LocalSettings.php":
136             return added == deleted == 10 or added == deleted == 9
137         elif filename == "AdminSettings.php":
138             return added == 0 and deleted == 20
139         elif filename == "config/index.php" or filename == "config/index.php5":
140             return added == 0
141         return False
142
143 Application.resolutions = {
144 'LocalSettings.php': [
145     ("""
146 <<<<<<<
147 ***1***
148 =======
149 ## The URL base path to the directory containing the wiki;
150 ## defaults for all runtime URL paths are based off of this.
151 ## For more information on customizing the URLs please see:
152 ## http://www.mediawiki.org/wiki/Manual:Short_URL
153 ***2***
154 $wgScriptExtension  = ".php";
155
156 ## UPO means: this is also a user preference option
157 >>>>>>>
158 """, [-1]),
159     ("""
160 <<<<<<<
161 ***1***
162 =======
163
164 # MySQL specific settings
165 $wgDBprefix         = "";
166 >>>>>>>
167 """, ["\n# MySQL specific settings", 1]),
168     ("""
169 <<<<<<<
170 ## is writable, then uncomment this:
171 ***1***
172 =======
173 ## is writable, then set this to true:
174 $wgEnableUploads       = false;
175 >>>>>>>
176 """, [-1]),
177     ("""
178 <<<<<<<
179 ***1***
180 $wgMathPath         = "{$wgUploadPath}/math";
181 $wgMathDirectory    = "{$wgUploadDirectory}/math";
182 $wgTmpDirectory     = "{$wgUploadDirectory}/tmp";
183 =======
184 $wgUseTeX           = false;
185 >>>>>>>
186 """, [1]),
187     # order of these rules is important
188     ("""
189 <<<<<<<
190 $configdate = gmdate( 'YmdHis', @filemtime( __FILE__ ) );
191 $wgCacheEpoch = max( $wgCacheEpoch, $configdate );
192 ***1***
193 ?>
194 =======
195 $wgCacheEpoch = max( $wgCacheEpoch, gmdate( 'YmdHis', @filemtime( __FILE__ ) ) );
196 >>>>>>>
197 """, [0, 1]),
198     ("""
199 <<<<<<<
200 $configdate = gmdate( 'YmdHis', @filemtime( __FILE__ ) );
201 $wgCacheEpoch = max( $wgCacheEpoch, $configdate );
202 ***1***
203 =======
204 $wgCacheEpoch = max( $wgCacheEpoch, gmdate( 'YmdHis', @filemtime( __FILE__ ) ) );
205 >>>>>>>
206 """, [0, 1]),
207     ("""
208 <<<<<<<
209 ?>
210 =======
211 # When you make changes to this configuration file, this will make
212 # sure that cached pages are cleared.
213 $wgCacheEpoch = max( $wgCacheEpoch, gmdate( 'YmdHis', @filemtime( __FILE__ ) ) );
214 >>>>>>>
215 """, [0]),
216     ("""
217 <<<<<<<
218 ***1***
219 ?>
220 =======
221 # When you make changes to this configuration file, this will make
222 # sure that cached pages are cleared.
223 $wgCacheEpoch = max( $wgCacheEpoch, gmdate( 'YmdHis', @filemtime( __FILE__ ) ) );
224 >>>>>>>
225 """, [1, 0]),
226     ("""
227 <<<<<<<
228 ***1***
229 =======
230 # When you make changes to this configuration file, this will make
231 # sure that cached pages are cleared.
232 $wgCacheEpoch = max( $wgCacheEpoch, gmdate( 'YmdHis', @filemtime( __FILE__ ) ) );
233 >>>>>>>
234 """, [1, 0]),
235     ]
236 }
237