]> scripts.mit.edu Git - wizard.git/blob - wizard/app/mediawiki.py
Implement download for MediaWiki.
[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, 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     def backup(self, deployment, backup_dir, options):
88         app.backup_database(backup_dir, deployment)
89     def restore(self, deployment, backup_dir, options):
90         app.restore_database(backup_dir, deployment)
91     def remove(self, deployment, options):
92         app.remove_database(deployment)
93     def researchFilter(self, filename, added, deleted):
94         if filename == "LocalSettings.php":
95             return added == deleted == 10 or added == deleted == 9
96         elif filename == "AdminSettings.php":
97             return added == 0 and deleted == 20
98         elif filename == "config/index.php" or filename == "config/index.php5":
99             return added == 0
100         return False
101
102 Application.resolutions = {
103 'LocalSettings.php': [
104     ("""
105 <<<<<<<
106 ***1***
107 =======
108 ## The URL base path to the directory containing the wiki;
109 ## defaults for all runtime URL paths are based off of this.
110 ## For more information on customizing the URLs please see:
111 ## http://www.mediawiki.org/wiki/Manual:Short_URL
112 ***2***
113 $wgScriptExtension  = ".php";
114
115 ## UPO means: this is also a user preference option
116 >>>>>>>
117 """, [-1]),
118     ("""
119 <<<<<<<
120 ***1***
121 =======
122
123 # MySQL specific settings
124 $wgDBprefix         = "";
125 >>>>>>>
126 """, ["\n# MySQL specific settings", 1]),
127     ("""
128 <<<<<<<
129 ## is writable, then uncomment this:
130 ***1***
131 =======
132 ## is writable, then set this to true:
133 $wgEnableUploads       = false;
134 >>>>>>>
135 """, [-1]),
136     ("""
137 <<<<<<<
138 ***1***
139 $wgMathPath         = "{$wgUploadPath}/math";
140 $wgMathDirectory    = "{$wgUploadDirectory}/math";
141 $wgTmpDirectory     = "{$wgUploadDirectory}/tmp";
142 =======
143 $wgUseTeX           = false;
144 >>>>>>>
145 """, [1]),
146     # order of these rules is important
147     ("""
148 <<<<<<<
149 $configdate = gmdate( 'YmdHis', @filemtime( __FILE__ ) );
150 $wgCacheEpoch = max( $wgCacheEpoch, $configdate );
151 ***1***
152 ?>
153 =======
154 $wgCacheEpoch = max( $wgCacheEpoch, gmdate( 'YmdHis', @filemtime( __FILE__ ) ) );
155 >>>>>>>
156 """, [0, 1]),
157     ("""
158 <<<<<<<
159 $configdate = gmdate( 'YmdHis', @filemtime( __FILE__ ) );
160 $wgCacheEpoch = max( $wgCacheEpoch, $configdate );
161 ***1***
162 =======
163 $wgCacheEpoch = max( $wgCacheEpoch, gmdate( 'YmdHis', @filemtime( __FILE__ ) ) );
164 >>>>>>>
165 """, [0, 1]),
166     ("""
167 <<<<<<<
168 ?>
169 =======
170 # When you make changes to this configuration file, this will make
171 # sure that cached pages are cleared.
172 $wgCacheEpoch = max( $wgCacheEpoch, gmdate( 'YmdHis', @filemtime( __FILE__ ) ) );
173 >>>>>>>
174 """, [0]),
175     ("""
176 <<<<<<<
177 ***1***
178 ?>
179 =======
180 # When you make changes to this configuration file, this will make
181 # sure that cached pages are cleared.
182 $wgCacheEpoch = max( $wgCacheEpoch, gmdate( 'YmdHis', @filemtime( __FILE__ ) ) );
183 >>>>>>>
184 """, [1, 0]),
185     ("""
186 <<<<<<<
187 ***1***
188 =======
189 # When you make changes to this configuration file, this will make
190 # sure that cached pages are cleared.
191 $wgCacheEpoch = max( $wgCacheEpoch, gmdate( 'YmdHis', @filemtime( __FILE__ ) ) );
192 >>>>>>>
193 """, [1, 0]),
194     ]
195 }
196