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