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