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