]> scripts.mit.edu Git - wizard.git/blob - wizard/app/wordpress.py
Add human readable names to applications.
[wizard.git] / wizard / app / wordpress.py
1 import os
2 import re
3 import logging
4 import distutils
5 import distutils.version
6 import urlparse
7 import hashlib
8 import sqlalchemy.exc
9
10 from wizard import app, install, resolve, sql, util
11 from wizard.app import php
12
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)
16
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',
32     })
33
34 class Application(app.Application):
35     fullname = "WordPress"
36     database = "mysql"
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'])
44     random_keys = set([
45         'WIZARD_SECRETKEY',
46         'WIZARD_AUTH_KEY',
47         'WIZARD_SECURE_AUTH_KEY',
48         'WIZARD_LOGGED_IN_KEY',
49         'WIZARD_NONCE_KEY',
50         'WIZARD_AUTH_SALT',
51         'WIZARD_SECURE_AUTH_SALT',
52         'WIZARD_LOGGED_IN_SALT',
53         'WIZARD_NONCE_SALT',
54         ])
55     random_blacklist = set(['put your unique phrase here'])
56     def urlFromExtract(self, deployment):
57         try:
58             meta = sql.connect(deployment.dsn)
59             try:
60                 wp_options = meta.tables["wp_options"]
61             except KeyError:
62                 return None
63             query = wp_options.select(wp_options.c.option_name == 'home')
64             return query.execute().fetchone()['option_value']
65         except sqlalchemy.exc.OperationalError:
66             return None
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")
79
80         post_setup_config = {
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
86                 'submit': 'Submit',
87                 'step': '2',
88                 }
89         post_install = {
90                 'weblog_title': options.title,
91                 'admin_email': options.email,
92                 'submit': 'Continue',
93                 'step': '2',
94                 # Version >= 3.0
95                 'user_name': options.admin_name,
96                 'admin_password': options.admin_password,
97                 'admin_password2': options.admin_password,
98                 }
99         old_mode = os.stat(".").st_mode
100         os.chmod(".", 0777) # XXX: squick squick
101
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()
110
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         if "Finished" not in result and "Success" not in result:
117             raise app.InstallFailure()
118
119         if version < distutils.version.LooseVersion("3.0"):
120             meta = sql.connect(options.dsn)
121             wp_users = meta.tables["wp_users"]
122             hashed_pass = hashlib.md5(options.admin_password).hexdigest()
123             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()
124             wp_usermeta = meta.tables["wp_usermeta"]
125             wp_usermeta.delete().where(wp_usermeta.c.user_id==1 and wp_usermeta.c.meta_key == "default_password_nag").execute()
126
127         # now we can restore the wp_mail function in wp-includes/pluggable[-functions].php
128         pluggable_file = open(pluggable_path,'w')
129         pluggable_file.write(pluggable)
130         pluggable_file.close()
131
132         # replace random variable stubs with real values
133         old_config = open('wp-config.php').read()
134         def replace_with_random(s):
135             return s.replace('put your unique phrase here', util.random_key(), 1)
136         config = replace_with_random(old_config)
137         while config != old_config:
138             old_config = config
139             config = replace_with_random(config)
140         open('wp-config.php', 'w').write(config)
141
142         php.ini_replace_vars()
143     def upgrade(self, d, version, options):
144         result = d.fetch("wp-admin/upgrade.php?step=1")
145         if "Upgrade Complete" not in result and "Update Complete" not in result and \
146                 "No Upgrade Required" not in result and "No Update Required" not in result:
147             raise app.UpgradeFailure(result)
148     @app.throws_database_errors
149     def backup(self, deployment, backup_dir, options):
150         sql.backup(backup_dir, deployment)
151     @app.throws_database_errors
152     def restore(self, deployment, backup_dir, options):
153         sql.restore(backup_dir, deployment)
154     @app.throws_database_errors
155     def remove(self, deployment, options):
156         sql.drop(deployment.dsn)
157
158 Application.resolutions = {
159 'wp-config.php': [
160     ("""
161 <<<<<<<
162
163 /** WordPress absolute path to the Wordpress directory. */
164 |||||||
165 /** WordPress absolute path to the Wordpress directory. */
166 =======
167 /** Absolute path to the WordPress directory. */
168 >>>>>>>
169 """, [0])
170 ],
171 }