5 import distutils.version
10 from wizard import app, install, resolve, sql, util
11 from wizard.app import php
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)
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',
34 class Application(app.Application):
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'])
46 'WIZARD_SECURE_AUTH_KEY',
47 'WIZARD_LOGGED_IN_KEY',
50 'WIZARD_SECURE_AUTH_SALT',
51 'WIZARD_LOGGED_IN_SALT',
54 random_blacklist = set(['put your unique phrase here'])
55 def urlFromExtract(self, deployment):
57 meta = sql.connect(deployment.dsn)
59 wp_options = meta.tables["wp_options"]
62 query = wp_options.select(wp_options.c.option_name == 'home')
63 return query.execute().fetchone()['option_value']
64 except sqlalchemy.exc.OperationalError:
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")
80 'dbhost': options.dsn.host,
81 'uname': options.dsn.username,
82 'dbname': options.dsn.database,
83 'pwd': options.dsn.password,
89 'weblog_title': options.title,
90 'admin_email': options.email,
94 'user_name': options.admin_name,
95 'admin_password': options.admin_password,
96 'admin_password2': options.admin_password,
98 old_mode = os.stat(".").st_mode
99 os.chmod(".", 0777) # XXX: squick squick
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()
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()
118 if version < distutils.version.LooseVersion("3.0"):
119 meta = sql.connect(options.dsn)
120 wp_users = meta.tables["wp_users"]
121 hashed_pass = hashlib.md5(options.admin_password).hexdigest()
122 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()
123 wp_usermeta = meta.tables["wp_usermeta"]
124 wp_usermeta.delete().where(wp_usermeta.c.user_id==1 and wp_usermeta.c.meta_key == "default_password_nag").execute()
126 # now we can restore the wp_mail function in wp-includes/pluggable[-functions].php
127 pluggable_file = open(pluggable_path,'w')
128 pluggable_file.write(pluggable)
129 pluggable_file.close()
131 # replace random variable stubs with real values
132 old_config = open('wp-config.php').read()
133 def replace_with_random(s):
134 return s.replace('put your unique phrase here', util.random_key(), 1)
135 config = replace_with_random(old_config)
136 while config != old_config:
138 config = replace_with_random(config)
139 open('wp-config.php', 'w').write(config)
141 php.ini_replace_vars()
142 def upgrade(self, d, version, options):
143 result = d.fetch("wp-admin/upgrade.php?step=1")
144 if "Upgrade Complete" not in result and "Update Complete" not in result and \
145 "No Upgrade Required" not in result and "No Update Required" not in result:
146 raise app.UpgradeFailure(result)
147 @app.throws_database_errors
148 def backup(self, deployment, backup_dir, options):
149 sql.backup(backup_dir, deployment)
150 @app.throws_database_errors
151 def restore(self, deployment, backup_dir, options):
152 sql.restore(backup_dir, deployment)
153 @app.throws_database_errors
154 def remove(self, deployment, options):
155 sql.drop(deployment.dsn)
157 Application.resolutions = {
162 /** WordPress absolute path to the Wordpress directory. */
164 /** WordPress absolute path to the Wordpress directory. */
166 /** Absolute path to the WordPress directory. */