]> scripts.mit.edu Git - wizard.git/blob - wizard/app/wordpress.py
Extract URL from database if available for Wordpress.
[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     def urlFromExtract(self, deployment):
40         try:
41             meta = sql.connect(deployment.dsn)
42             wp_options = meta.tables["wp_options"]
43             return wp_options.select('option_value').where(wp_options.c.option_name == 'home')
44         except sqlalchemy.exc.OperationalError:
45             return None
46     def download(self, version):
47         return "http://wordpress.org/wordpress-%s.tar.gz" % version
48     def checkConfig(self, deployment):
49         return os.path.isfile("wp-config.php")
50     def checkWeb(self, deployment):
51         # XXX: this sucks pretty hard
52         def doCheck():
53             return self.checkWebPage(deployment, "",
54                     outputs=["<html", "WordPress", "feed"],
55                     exclude=["Error establishing a database connection"])
56         if not doCheck():
57             deployment.enableOldStyleUrls()
58             return doCheck()
59         else:
60             return True
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")
65
66         post_setup_config = {
67                 'dbhost': options.dsn.host,
68                 'uname': options.dsn.username,
69                 'dbname': options.dsn.database,
70                 'pwd': options.dsn.password,
71                 'prefix': '',
72                 'submit': 'Submit',
73                 'step': '2',
74                 }
75         post_install = {
76                 'weblog_title': options.title,
77                 'admin_email': options.email,
78                 'submit': 'Continue',
79                 'step': '2',
80                 }
81         old_mode = os.stat(".").st_mode
82         os.chmod(".", 0777) # XXX: squick squick
83
84         # we need to disable the wp_mail function in wp-includes/pluggable[-functions].php
85         pluggable_path = os.path.exists('wp-includes/pluggable.php') and 'wp-includes/pluggable.php' or 'wp-includes/pluggable-functions.php'
86         pluggable = open(pluggable_path, 'r').read()
87         wp_mail_noop = "<?php function wp_mail( $to, $subject, $message, $headers = '', $attachments = array() ) { /*noop*/ } ?> \n\n"
88         pluggable_file = open(pluggable_path,'w')
89         pluggable_file.write(wp_mail_noop)
90         pluggable_file.write(pluggable)
91         pluggable_file.close()
92
93         result = install.fetch(options, "wp-admin/setup-config.php?step=2", post_setup_config)
94         logging.debug("setup-config.php output\n\n" + result)
95         result = install.fetch(options, "wp-admin/install.php?step=2", post_install)
96         logging.debug("install.php output\n\n" + result)
97         os.chmod(".", old_mode)
98         if "Finished" not in result and "Success" not in result:
99             raise app.InstallFailure()
100
101         # not sure what to do about this
102         meta = sql.connect(options.dsn)
103         wp_options = meta.tables["wp_options"]
104         wp_options.update().where(wp_options.c.option_name == 'siteurl').values(option_value=options.web_path).execute()
105         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
106         # should also set the username and admin password
107
108         wp_users = meta.tables["wp_users"]
109         hashed_pass = hashlib.md5(options.admin_password).hexdigest()
110         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()
111         wp_usermeta = meta.tables["wp_usermeta"]
112         wp_usermeta.delete().where(wp_usermeta.c.user_id==1 and wp_usermeta.c.meta_key == "default_password_nag").execute()
113
114         # now we can restore the wp_mail function in wp-includes/pluggable[-functions].php
115         pluggable_file = open(pluggable_path,'w')
116         pluggable_file.write(pluggable)
117         pluggable_file.close()
118
119         php.ini_replace_vars()
120     def upgrade(self, d, version, options):
121         result = d.fetch("wp-admin/upgrade.php?step=1")
122         if "Upgrade Complete" not in result and "No Upgrade Required" not in result:
123             raise app.UpgradeFailure(result)
124     def backup(self, deployment, backup_dir, options):
125         app.backup_database(backup_dir, deployment)
126     def restore(self, deployment, backup_dir, options):
127         app.restore_database(backup_dir, deployment)
128     def remove(self, deployment, options):
129         app.remove_database(deployment)
130
131 Application.resolutions = {
132 'wp-config.php': [
133     ("""
134 <<<<<<<
135
136 /** WordPress absolute path to the Wordpress directory. */
137 |||||||
138 /** WordPress absolute path to the Wordpress directory. */
139 =======
140 /** Absolute path to the WordPress directory. */
141 >>>>>>>
142 """, [0])
143 ],
144 }