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):
35 fullname = "WordPress"
37 parametrized_files = ['wp-config.php'] + php.parametrized_files
38 extractors = app.make_extractors(seed)
39 extractors.update(php.extractors)
40 substitutions = app.make_substitutions(seed)
41 substitutions.update(php.substitutions)
42 install_schema = install.ArgSchema("db", "admin", "email", "title")
43 deprecated_keys = set(['WIZARD_SECRETKEY'])
47 'WIZARD_SECURE_AUTH_KEY',
48 'WIZARD_LOGGED_IN_KEY',
51 'WIZARD_SECURE_AUTH_SALT',
52 'WIZARD_LOGGED_IN_SALT',
55 random_blacklist = set(['put your unique phrase here'])
56 def urlFromExtract(self, deployment):
58 meta = sql.connect(deployment.dsn)
60 wp_options = meta.tables["wp_options"]
63 query = wp_options.select(wp_options.c.option_name == 'home')
64 return query.execute().fetchone()['option_value']
65 except sqlalchemy.exc.OperationalError:
67 def download(self, version):
68 return "http://wordpress.org/wordpress-%s.tar.gz" % version
69 def checkConfig(self, deployment):
70 return os.path.isfile("wp-config.php")
71 def checkWeb(self, deployment):
72 return self.checkWebPage(deployment, "",
73 outputs=["<html", "WordPress", "feed"],
74 exclude=["Error establishing a database connection", "Account unknown"])
75 def detectVersion(self, deployment):
76 return self.detectVersionFromFile("wp-includes/version.php", php.re_var("wp_version"))
77 def install(self, version, options):
78 util.soft_unlink("wp-config.php")
81 'dbhost': options.dsn.host,
82 'uname': options.dsn.username,
83 'dbname': options.dsn.database,
84 'pwd': options.dsn.password,
85 'prefix': 'wp_', # Changed >= 3.4, now disallows empty prefix
90 'weblog_title': options.title,
91 'admin_email': options.email,
95 'user_name': options.admin_name,
96 'admin_password': options.admin_password,
97 'admin_password2': options.admin_password,
99 old_mode = os.stat(".").st_mode
100 os.chmod(".", 0777) # XXX: squick squick
102 # we need to disable the wp_mail function in wp-includes/pluggable[-functions].php
103 pluggable_path = os.path.exists('wp-includes/pluggable.php') and 'wp-includes/pluggable.php' or 'wp-includes/pluggable-functions.php'
104 pluggable = open(pluggable_path, 'r').read()
105 wp_mail_noop = "<?php function wp_mail( $to, $subject, $message, $headers = '', $attachments = array() ) { /*noop*/ } ?> \n\n"
106 pluggable_file = open(pluggable_path,'w')
107 pluggable_file.write(wp_mail_noop)
108 pluggable_file.write(pluggable)
109 pluggable_file.close()
111 result = install.fetch(options, "wp-admin/setup-config.php?step=2", post_setup_config)
112 logging.debug("setup-config.php output\n\n" + result)
113 result = install.fetch(options, "wp-admin/install.php?step=2", post_install)
114 logging.debug("install.php output\n\n" + result)
115 os.chmod(".", old_mode)
116 # Starting in Wordpress 4.7, install now redirects you directly
117 # to login page. So... don't check.
118 #if "Finished" not in result and "Success" not in result:
119 # raise app.InstallFailure()
121 if version < distutils.version.LooseVersion("3.0"):
122 meta = sql.connect(options.dsn)
123 wp_users = meta.tables["wp_users"]
124 hashed_pass = hashlib.md5(options.admin_password).hexdigest()
125 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()
126 wp_usermeta = meta.tables["wp_usermeta"]
127 wp_usermeta.delete().where(wp_usermeta.c.user_id==1 and wp_usermeta.c.meta_key == "default_password_nag").execute()
129 # now we can restore the wp_mail function in wp-includes/pluggable[-functions].php
130 pluggable_file = open(pluggable_path,'w')
131 pluggable_file.write(pluggable)
132 pluggable_file.close()
134 # replace random variable stubs with real values
135 old_config = open('wp-config.php').read()
136 def replace_with_random(s):
137 return s.replace('put your unique phrase here', util.random_key(), 1)
138 config = replace_with_random(old_config)
139 while config != old_config:
141 config = replace_with_random(config)
142 open('wp-config.php', 'w').write(config)
144 php.ini_replace_vars()
145 def upgrade(self, d, version, options):
146 result = d.fetch("wp-admin/upgrade.php?step=1")
147 if "Upgrade Complete" not in result and "Update Complete" not in result and \
148 "No Upgrade Required" not in result and "No Update Required" not in result:
149 raise app.UpgradeFailure(result)
150 @app.throws_database_errors
151 def backup(self, deployment, backup_dir, options):
152 sql.backup(backup_dir, deployment)
153 @app.throws_database_errors
154 def restore(self, deployment, backup_dir, options):
155 sql.restore(backup_dir, deployment)
156 @app.throws_database_errors
157 def remove(self, deployment, options):
158 sql.drop(deployment.dsn)
160 Application.resolutions = {
165 /** WordPress absolute path to the Wordpress directory. */
167 /** WordPress absolute path to the Wordpress directory. */
169 /** Absolute path to the WordPress directory. */