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'])
43 random_keys = set(['WIZARD_SECRETKEY', 'WIZARD_AUTH_KEY', 'WIZARD_SECURE_AUTH_KEY', 'WIZARD_LOGGED_IN_KEY', 'WIZARD_NONCE_KEY'])
44 random_blacklist = set(['put your unique phrase here'])
45 def urlFromExtract(self, deployment):
47 meta = sql.connect(deployment.dsn)
48 wp_options = meta.tables["wp_options"]
49 query = wp_options.select(wp_options.c.option_name == 'home')
50 return query.execute().fetchone()['option_value']
51 except sqlalchemy.exc.OperationalError:
53 def download(self, version):
54 return "http://wordpress.org/wordpress-%s.tar.gz" % version
55 def checkConfig(self, deployment):
56 return os.path.isfile("wp-config.php")
57 def checkWeb(self, deployment):
58 return self.checkWebPage(deployment, "",
59 outputs=["<html", "WordPress", "feed"],
60 exclude=["Error establishing a database connection"])
61 def detectVersion(self, deployment):
62 return self.detectVersionFromFile("wp-includes/version.php", php.re_var("wp_version"))
63 def install(self, version, options):
64 util.soft_unlink("wp-config.php")
67 'dbhost': options.dsn.host,
68 'uname': options.dsn.username,
69 'dbname': options.dsn.database,
70 'pwd': options.dsn.password,
76 'weblog_title': options.title,
77 'admin_email': options.email,
81 'user_name': options.admin_name,
82 'admin_password': options.admin_password,
83 'admin_password2': options.admin_password,
85 old_mode = os.stat(".").st_mode
86 os.chmod(".", 0777) # XXX: squick squick
88 # we need to disable the wp_mail function in wp-includes/pluggable[-functions].php
89 pluggable_path = os.path.exists('wp-includes/pluggable.php') and 'wp-includes/pluggable.php' or 'wp-includes/pluggable-functions.php'
90 pluggable = open(pluggable_path, 'r').read()
91 wp_mail_noop = "<?php function wp_mail( $to, $subject, $message, $headers = '', $attachments = array() ) { /*noop*/ } ?> \n\n"
92 pluggable_file = open(pluggable_path,'w')
93 pluggable_file.write(wp_mail_noop)
94 pluggable_file.write(pluggable)
95 pluggable_file.close()
97 result = install.fetch(options, "wp-admin/setup-config.php?step=2", post_setup_config)
98 logging.debug("setup-config.php output\n\n" + result)
99 result = install.fetch(options, "wp-admin/install.php?step=2", post_install)
100 logging.debug("install.php output\n\n" + result)
101 os.chmod(".", old_mode)
102 if "Finished" not in result and "Success" not in result:
103 raise app.InstallFailure()
105 # not sure what to do about this
106 meta = sql.connect(options.dsn)
107 wp_options = meta.tables["wp_options"]
108 wp_options.update().where(wp_options.c.option_name == 'siteurl').values(option_value=options.web_path).execute()
109 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
111 if version < distutils.version.LooseVersion("3.0"):
112 wp_users = meta.tables["wp_users"]
113 hashed_pass = hashlib.md5(options.admin_password).hexdigest()
114 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()
115 wp_usermeta = meta.tables["wp_usermeta"]
116 wp_usermeta.delete().where(wp_usermeta.c.user_id==1 and wp_usermeta.c.meta_key == "default_password_nag").execute()
118 # now we can restore the wp_mail function in wp-includes/pluggable[-functions].php
119 pluggable_file = open(pluggable_path,'w')
120 pluggable_file.write(pluggable)
121 pluggable_file.close()
123 # replace random variable stubs with real values
124 old_config = open('wp-config.php').read()
125 def replace_with_random(s):
126 return s.replace('put your unique phrase here', util.random_key(), 1)
127 config = replace_with_random(old_config)
128 while config != old_config:
130 config = replace_with_random(config)
131 open('wp-config.php', 'w').write(config)
133 php.ini_replace_vars()
134 def upgrade(self, d, version, options):
135 result = d.fetch("wp-admin/upgrade.php?step=1")
136 if "Upgrade Complete" not in result and "No Upgrade Required" not in result:
137 raise app.UpgradeFailure(result)
138 def backup(self, deployment, backup_dir, options):
139 app.backup_database(backup_dir, deployment)
140 def restore(self, deployment, backup_dir, options):
141 app.restore_database(backup_dir, deployment)
142 def remove(self, deployment, options):
143 app.remove_database(deployment)
145 Application.resolutions = {
150 /** WordPress absolute path to the Wordpress directory. */
152 /** WordPress absolute path to the Wordpress directory. */
154 /** Absolute path to the WordPress directory. */