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