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