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