8 from wizard import app, install, resolve, sql, util
9 from wizard.app import php
11 def make_filename_regex_define(var):
12 """See :ref:`versioning config <seed>` for more information."""
13 return 'wp-config.php', php.re_define(var)
15 seed = util.dictmap(make_filename_regex_define, {
16 # these funny names are due to convention set by MediaWiki
17 'WIZARD_DBSERVER': 'DB_HOST',
18 'WIZARD_DBNAME': 'DB_NAME',
19 'WIZARD_DBUSER': 'DB_USER',
20 'WIZARD_DBPASSWORD': 'DB_PASSWORD',
21 'WIZARD_SECRETKEY': 'SECRET_KEY',
22 'WIZARD_AUTH_KEY': 'AUTH_KEY',
23 'WIZARD_SECURE_AUTH_KEY': 'SECURE_AUTH_KEY',
24 'WIZARD_LOGGED_IN_KEY': 'LOGGED_IN_KEY',
25 'WIZARD_NONCE_KEY': 'NONCE_KEY',
28 class Application(app.Application):
30 parametrized_files = ['wp-config.php'] + php.parametrized_files
31 extractors = app.make_extractors(seed)
32 extractors.update(php.extractors)
33 substitutions = app.make_substitutions(seed)
34 substitutions.update(php.substitutions)
35 install_schema = install.ArgSchema("db", "admin", "email", "title")
36 deprecated_keys = set(['WIZARD_SECRETKEY'])
37 random_keys = set(['WIZARD_SECRETKEY', 'WIZARD_AUTH_KEY', 'WIZARD_SECURE_AUTH_KEY', 'WIZARD_LOGGED_IN_KEY', 'WIZARD_NONCE_KEY'])
38 def download(self, version):
39 return "http://wordpress.org/wordpress-%s.tar.gz" % version
40 def checkConfig(self, deployment):
41 return os.path.isfile("wp-config.php")
42 def checkWeb(self, deployment):
43 # XXX: this sucks pretty hard
44 return self.checkWebPage(deployment, "",
45 outputs=["<html", "WordPress", "feed"],
46 exclude=["Error establishing a database connection"])
47 def detectVersion(self, deployment):
48 return self.detectVersionFromFile("wp-includes/version.php", php.re_var("wp_version"))
49 def install(self, version, options):
50 util.soft_unlink("wp-config.php")
53 'dbhost': options.dsn.host,
54 'uname': options.dsn.username,
55 'dbname': options.dsn.database,
56 'pwd': options.dsn.password,
62 'weblog_title': options.title,
63 'admin_email': options.email,
67 old_mode = os.stat(".").st_mode
68 os.chmod(".", 0777) # XXX: squick squick
70 # we need to disable the wp_mail function in wp-includes/pluggable[-functions].php
71 pluggable_path = os.path.exists('wp-includes/pluggable.php') and 'wp-includes/pluggable.php' or 'wp-includes/pluggable-functions.php'
72 pluggable = open(pluggable_path, 'r').read()
73 wp_mail_noop = "<?php function wp_mail( $to, $subject, $message, $headers = '', $attachments = array() ) { /*noop*/ } ?> \n\n"
74 pluggable_file = open(pluggable_path,'w')
75 pluggable_file.write(wp_mail_noop)
76 pluggable_file.write(pluggable)
77 pluggable_file.close()
79 result = install.fetch(options, "wp-admin/setup-config.php?step=2", post_setup_config)
80 logging.debug("setup-config.php output\n\n" + result)
81 result = install.fetch(options, "wp-admin/install.php?step=2", post_install)
82 logging.debug("install.php output\n\n" + result)
83 os.chmod(".", old_mode)
84 if "Finished" not in result and "Success" not in result:
85 raise app.InstallFailure()
87 # not sure what to do about this
88 meta = sql.connect(options.dsn)
89 wp_options = meta.tables["wp_options"]
90 wp_options.update().where(wp_options.c.option_name == 'siteurl').values(option_value=options.web_path).execute()
91 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
92 # should also set the username and admin password
94 wp_users = meta.tables["wp_users"]
95 hashed_pass = hashlib.md5(options.admin_password).hexdigest()
96 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()
97 wp_usermeta = meta.tables["wp_usermeta"]
98 wp_usermeta.delete().where(wp_usermeta.c.user_id==1 and wp_usermeta.c.meta_key == "default_password_nag").execute()
100 # now we can restore the wp_mail function in wp-includes/pluggable[-functions].php
101 pluggable_file = open(pluggable_path,'w')
102 pluggable_file.write(pluggable)
103 pluggable_file.close()
105 php.ini_replace_vars()
106 def upgrade(self, d, version, options):
107 result = d.fetch("wp-admin/upgrade.php?step=1")
108 if "Upgrade Complete" not in result and "No Upgrade Required" not in result:
109 raise app.UpgradeFailure(result)
110 def backup(self, deployment, backup_dir, options):
111 app.backup_database(backup_dir, deployment)
112 def restore(self, deployment, backup_dir, options):
113 app.restore_database(backup_dir, deployment)
114 def remove(self, deployment, options):
115 app.remove_database(deployment)
117 Application.resolutions = {
122 /** WordPress absolute path to the Wordpress directory. */
124 /** WordPress absolute path to the Wordpress directory. */
126 /** Absolute path to the WordPress directory. */