]> scripts.mit.edu Git - wizard.git/blob - wizard/app/wordpress.py
Notes, and upgrade Wordpress upgrade script.
[wizard.git] / wizard / app / wordpress.py
1 import os
2 import re
3 import logging
4 import distutils
5 import distutils.version
6 import urlparse
7 import hashlib
8 import sqlalchemy.exc
9
10 from wizard import app, install, resolve, sql, util
11 from wizard.app import php
12
13 def make_filename_regex_define(var):
14     """See :ref:`versioning config <seed>` for more information."""
15     return 'wp-config.php', php.re_define(var)
16
17 seed = util.dictmap(make_filename_regex_define, {
18     # these funny names are due to convention set by MediaWiki
19     'WIZARD_DBSERVER': 'DB_HOST',
20     'WIZARD_DBNAME': 'DB_NAME',
21     'WIZARD_DBUSER': 'DB_USER',
22     'WIZARD_DBPASSWORD': 'DB_PASSWORD',
23     'WIZARD_SECRETKEY': 'SECRET_KEY',
24     'WIZARD_AUTH_KEY': 'AUTH_KEY',
25     'WIZARD_SECURE_AUTH_KEY': 'SECURE_AUTH_KEY',
26     'WIZARD_LOGGED_IN_KEY': 'LOGGED_IN_KEY',
27     'WIZARD_NONCE_KEY': 'NONCE_KEY',
28     'WIZARD_AUTH_SALT': 'AUTH_SALT',
29     'WIZARD_SECURE_AUTH_SALT': 'SECURE_AUTH_SALT',
30     'WIZARD_LOGGED_IN_SALT': 'LOGGED_IN_SALT',
31     'WIZARD_NONCE_SALT': 'NONCE_SALT',
32     })
33
34 class Application(app.Application):
35     database = "mysql"
36     parametrized_files = ['wp-config.php'] + php.parametrized_files
37     extractors = app.make_extractors(seed)
38     extractors.update(php.extractors)
39     substitutions = app.make_substitutions(seed)
40     substitutions.update(php.substitutions)
41     install_schema = install.ArgSchema("db", "admin", "email", "title")
42     deprecated_keys = set(['WIZARD_SECRETKEY'])
43     random_keys = set([
44         'WIZARD_SECRETKEY',
45         'WIZARD_AUTH_KEY',
46         'WIZARD_SECURE_AUTH_KEY',
47         'WIZARD_LOGGED_IN_KEY',
48         'WIZARD_NONCE_KEY',
49         'WIZARD_AUTH_SALT',
50         'WIZARD_SECURE_AUTH_SALT',
51         'WIZARD_LOGGED_IN_SALT',
52         'WIZARD_NONCE_SALT',
53         ])
54     random_blacklist = set(['put your unique phrase here'])
55     def urlFromExtract(self, deployment):
56         try:
57             meta = sql.connect(deployment.dsn)
58             try:
59                 wp_options = meta.tables["wp_options"]
60             except KeyError:
61                 return None
62             query = wp_options.select(wp_options.c.option_name == 'home')
63             return query.execute().fetchone()['option_value']
64         except sqlalchemy.exc.OperationalError:
65             return None
66     def download(self, version):
67         return "http://wordpress.org/wordpress-%s.tar.gz" % version
68     def checkConfig(self, deployment):
69         return os.path.isfile("wp-config.php")
70     def checkWeb(self, deployment):
71         return self.checkWebPage(deployment, "",
72                 outputs=["<html", "WordPress", "feed"],
73                 exclude=["Error establishing a database connection", "Account unknown"])
74     def detectVersion(self, deployment):
75         return self.detectVersionFromFile("wp-includes/version.php", php.re_var("wp_version"))
76     def install(self, version, options):
77         util.soft_unlink("wp-config.php")
78
79         post_setup_config = {
80                 'dbhost': options.dsn.host,
81                 'uname': options.dsn.username,
82                 'dbname': options.dsn.database,
83                 'pwd': options.dsn.password,
84                 'prefix': '',
85                 'submit': 'Submit',
86                 'step': '2',
87                 }
88         post_install = {
89                 'weblog_title': options.title,
90                 'admin_email': options.email,
91                 'submit': 'Continue',
92                 'step': '2',
93                 # Version >= 3.0
94                 'user_name': options.admin_name,
95                 'admin_password': options.admin_password,
96                 'admin_password2': options.admin_password,
97                 }
98         old_mode = os.stat(".").st_mode
99         os.chmod(".", 0777) # XXX: squick squick
100
101         # we need to disable the wp_mail function in wp-includes/pluggable[-functions].php
102         pluggable_path = os.path.exists('wp-includes/pluggable.php') and 'wp-includes/pluggable.php' or 'wp-includes/pluggable-functions.php'
103         pluggable = open(pluggable_path, 'r').read()
104         wp_mail_noop = "<?php function wp_mail( $to, $subject, $message, $headers = '', $attachments = array() ) { /*noop*/ } ?> \n\n"
105         pluggable_file = open(pluggable_path,'w')
106         pluggable_file.write(wp_mail_noop)
107         pluggable_file.write(pluggable)
108         pluggable_file.close()
109
110         result = install.fetch(options, "wp-admin/setup-config.php?step=2", post_setup_config)
111         logging.debug("setup-config.php output\n\n" + result)
112         result = install.fetch(options, "wp-admin/install.php?step=2", post_install)
113         logging.debug("install.php output\n\n" + result)
114         os.chmod(".", old_mode)
115         if "Finished" not in result and "Success" not in result:
116             raise app.InstallFailure()
117
118         # not sure what to do about this
119         meta = sql.connect(options.dsn)
120         wp_options = meta.tables["wp_options"]
121         wp_options.update().where(wp_options.c.option_name == 'siteurl').values(option_value=options.web_path).execute()
122         wp_options.update().where(wp_options.c.option_name == 'home').values(option_value="http://%s%s" % (options.web_host, options.web_path)).execute() # XXX: what if missing leading slash; this should be put in a function
123
124         if version < distutils.version.LooseVersion("3.0"):
125             wp_users = meta.tables["wp_users"]
126             hashed_pass = hashlib.md5(options.admin_password).hexdigest()
127             wp_users.update().where(wp_users.c.ID == 1).values(user_login=options.admin_name,user_nicename=options.admin_name,display_name=options.admin_name,user_pass=hashed_pass).execute()
128             wp_usermeta = meta.tables["wp_usermeta"]
129             wp_usermeta.delete().where(wp_usermeta.c.user_id==1 and wp_usermeta.c.meta_key == "default_password_nag").execute()
130
131         # now we can restore the wp_mail function in wp-includes/pluggable[-functions].php
132         pluggable_file = open(pluggable_path,'w')
133         pluggable_file.write(pluggable)
134         pluggable_file.close()
135
136         # replace random variable stubs with real values
137         old_config = open('wp-config.php').read()
138         def replace_with_random(s):
139             return s.replace('put your unique phrase here', util.random_key(), 1)
140         config = replace_with_random(old_config)
141         while config != old_config:
142             old_config = config
143             config = replace_with_random(config)
144         open('wp-config.php', 'w').write(config)
145
146         php.ini_replace_vars()
147     def upgrade(self, d, version, options):
148         result = d.fetch("wp-admin/upgrade.php?step=1")
149         if "Upgrade Complete" not in result and "Update Complete" not in result and "No Upgrade Required" not in result:
150             raise app.UpgradeFailure(result)
151     @app.throws_database_errors
152     def backup(self, deployment, backup_dir, options):
153         sql.backup(backup_dir, deployment)
154     @app.throws_database_errors
155     def restore(self, deployment, backup_dir, options):
156         sql.restore(backup_dir, deployment)
157     @app.throws_database_errors
158     def remove(self, deployment, options):
159         sql.drop(deployment.dsn)
160
161 Application.resolutions = {
162 'wp-config.php': [
163     ("""
164 <<<<<<<
165
166 /** WordPress absolute path to the Wordpress directory. */
167 |||||||
168 /** WordPress absolute path to the Wordpress directory. */
169 =======
170 /** Absolute path to the WordPress directory. */
171 >>>>>>>
172 """, [0])
173 ],
174 }