install_6.inc

Rebuild all the caches

Functions

Namesort descending Description
install_create_admin_user
install_mail
install_main
install_send_welcome_mail
install_verify_drupal Verify if Drupal is installed.
install_verify_settings Verify existing settings.php
_install_module_batch Batch callback for batch installation of modules.

Constants

Namesort descending Description
MAINTENANCE_MODE

File

platform/drupal/install_6.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Rebuild all the caches
  5. */
  6. $GLOBALS['url'] = d()->uri;
  7. $GLOBALS['profile'] = d()->profile;
  8. $GLOBALS['install_locale'] = d()->language;
  9. $GLOBALS['base_url'] = provision_get_base_url();
  10. // @TODO Document how/why this works.
  11. require_once 'includes/install.inc'; // From the Drupal platform.
  12. require_once 'install.inc'; // From Provision.
  13. define('MAINTENANCE_MODE', 'install');
  14. /**
  15. * Verify if Drupal is installed.
  16. */
  17. function install_verify_drupal() {
  18. $result = @db_query("SELECT name FROM {system} WHERE name = 'system'");
  19. return $result && db_result($result) == 'system';
  20. }
  21. /**
  22. * Verify existing settings.php
  23. */
  24. function install_verify_settings() {
  25. global $db_prefix, $db_type, $db_url;
  26. // Verify existing settings (if any).
  27. if (!empty($db_url)) {
  28. // We need this because we want to run form_get_errors.
  29. // @todo why are we setting those variables if they are not
  30. // reachable outside this scope?!
  31. $url = parse_url(is_array($db_url) ? $db_url['default'] : $db_url);
  32. if (!empty($url['user']) && !empty($url['pass']) && !empty($url['host'])) {
  33. $db_user = urldecode($url['user']);
  34. $db_pass = urldecode($url['pass']);
  35. $db_host = urldecode($url['host']);
  36. $db_port = isset($url['port']) ? urldecode($url['port']) : '3306';
  37. $db_path = ltrim(urldecode($url['path']), '/');
  38. $settings_file = './'. conf_path() .'/settings.php';
  39. return TRUE;
  40. }
  41. else {
  42. return FALSE;
  43. }
  44. }
  45. return FALSE;
  46. }
  47. function install_create_admin_user($client_email) {
  48. $name = drush_get_option('admin_user', PROVISION_DEFAULT_ADMIN_USER);
  49. if ($error = user_validate_name($name)) {
  50. drush_log(dt('Error setting UID username to `!user`: ', array('!user' => $name)) . $error . dt(' Using default (!default) instead.', array('!default' => PROVISION_DEFAULT_ADMIN_USER)), 'warning');
  51. $name = PROVISION_DEFAULT_ADMIN_USER;
  52. }
  53. // create the admin account
  54. $account = user_load(1);
  55. $edit['name'] = $name;
  56. $edit['pass'] = user_password();
  57. $edit['mail'] = $client_email;
  58. $edit['status'] = 1;
  59. // temporarily disable drupal's default mail notification
  60. $prev = variable_get('user_mail_status_activated_notify', TRUE);
  61. variable_set('user_mail_status_activated_notify', FALSE);
  62. $account = user_save($account, $edit);
  63. variable_set('user_mail_status_activated_notify', $prev);
  64. return $account;
  65. }
  66. function install_send_welcome_mail($url, $account, $profile, $language, $client_email, $onetime) {
  67. global $base_url;
  68. // Mail one time login URL and instructions.
  69. $from = variable_get('site_mail', ini_get('sendmail_from'));
  70. $mail_params['variables'] = array(
  71. '!username' => $account->name, '!site' => variable_get('site_name', 'Drupal'), '!login_url' => $onetime,
  72. '!uri' => $base_url, '!uri_brief' => preg_replace('!^https?://!', '', $base_url), '!mailto' => $account->mail,
  73. '!date' => format_date(time()), '!login_uri' => url('user', array('absolute' => TRUE)),
  74. '!edit_uri' => url('user/'. $account->uid .'/edit', array('absolute' => TRUE)));
  75. $mail_success = drupal_mail('install', 'welcome-admin', $client_email, user_preferred_language($account), $mail_params, $from, TRUE);
  76. if ($mail_success) {
  77. drush_log(dt('Sent welcome mail to @client', array('@client' => $client_email)), 'success');
  78. }
  79. else {
  80. drush_log(dt('Could not send welcome mail to @client', array('@client' => $client_email)), 'warning');
  81. }
  82. }
  83. function install_mail($key, &$message, $params) {
  84. global $profile;
  85. switch ($key) {
  86. case 'welcome-admin':
  87. // allow the profile to override welcome email text
  88. if (file_exists("./profiles/$profile/provision_welcome_mail.inc")) {
  89. require_once "./profiles/$profile/provision_welcome_mail.inc";
  90. $custom = TRUE;
  91. }
  92. elseif (file_exists(dirname(__FILE__) . '/../provision_welcome_mail.inc')) {
  93. /** use the module provided welcome email
  94. * We can not use drupal_get_path here,
  95. * as we are connected to the provisioned site's database
  96. */
  97. require_once dirname(__FILE__) . '/../provision_welcome_mail.inc';
  98. $custom = TRUE;
  99. }
  100. else {
  101. // last resort use the user-pass mail text
  102. $custom = FALSE;
  103. }
  104. if ($custom) {
  105. $message['subject'] = dt($mail['subject'], $params['variables']);
  106. $message['body'] = dt($mail['body'], $params['variables']);
  107. }
  108. else {
  109. $message['subject'] = _user_mail_text('pass_subject', $params['variables']);
  110. $message['body'] = _user_mail_text('pass_body', $params['variables']);
  111. }
  112. break;
  113. }
  114. }
  115. function install_main() {
  116. require_once './includes/bootstrap.inc';
  117. drush_bootstrap(DRUSH_BOOTSTRAP_DRUPAL_CONFIGURATION);
  118. // This must go after drupal_bootstrap(), which unsets globals!
  119. global $profile, $install_locale, $conf, $url;
  120. require_once './modules/system/system.install';
  121. require_once './includes/file.inc';
  122. require_once './includes/database.inc';
  123. require_once './includes/lock.inc';
  124. // Ensure the proper DB is active;
  125. db_set_active();
  126. // Ensure correct page headers are sent (e.g. caching)
  127. drupal_page_header();
  128. // Set up $language, so t() caller functions will still work.
  129. drupal_init_language();
  130. // Load module basics (needed for hook invokes).
  131. include_once './includes/module.inc';
  132. $module_list['system']['filename'] = 'modules/system/system.module';
  133. $module_list['filter']['filename'] = 'modules/filter/filter.module';
  134. module_list(TRUE, FALSE, FALSE, $module_list);
  135. drupal_load('module', 'system');
  136. drupal_load('module', 'filter');
  137. // Set up theme system for the maintenance page.
  138. drupal_maintenance_theme(); // Check existing settings.php.
  139. $verify = install_verify_settings();
  140. // Drupal may already be installed.
  141. if ($verify) {
  142. // Since we have a database connection, we use the normal cache system.
  143. // This is important, as the installer calls into the Drupal system for
  144. // the clean URL checks, so we should maintain the cache properly.
  145. require_once './includes/cache.inc';
  146. $conf['cache_inc'] = './includes/cache.inc';
  147. drush_bootstrap(DRUSH_BOOTSTRAP_DRUPAL_DATABASE);
  148. // Check if Drupal is installed.
  149. if (install_verify_drupal()) {
  150. drush_set_error('PROVISION_DRUPAL_SITE_INSTALLED', st('Site is already installed'));
  151. return FALSE;
  152. }
  153. }
  154. else {
  155. drush_set_error('PROVISION_CONFIG_NOT_VALID', st('Config file could not be loaded'));
  156. return FALSE;
  157. }
  158. drush_log(dt("Installing Drupal schema"), 'install');
  159. // Load the profile.
  160. require_once "./profiles/$profile/$profile.profile";
  161. drush_log(dt("Loading @profile install profile", array("@profile" => $profile)), 'install');
  162. drush_log(dt("Installing translation : @locale", array("@locale" => $install_locale)), 'install');
  163. /**
  164. * Handles requirement checking
  165. *
  166. * This code is based on install_check_requirements in install.php
  167. * We separate this out because we want to avoid all the user interface
  168. * code in this function, so we only use the relevant part of it.
  169. */
  170. $requirements = drupal_check_profile($profile);
  171. $severity = drupal_requirements_severity($requirements);
  172. // If there are issues, report them.
  173. if ($severity == REQUIREMENT_ERROR) {
  174. foreach ($requirements as $requirement) {
  175. if (isset($requirement['severity']) && $requirement['severity'] == REQUIREMENT_ERROR) {
  176. drush_log($requirement['description'] .' ('. st('Currently using !item !version', array('!item' => $requirement['title'], '!version' => $requirement['value'])) .')', 'error');
  177. }
  178. }
  179. $missing_requirement = TRUE;
  180. }
  181. if ($severity == REQUIREMENT_WARNING) {
  182. foreach ($requirements as $requirement) {
  183. if (isset($requirement['severity']) && $requirement['severity'] == REQUIREMENT_WARNING) {
  184. $message = $requirement['description'];
  185. if (isset($requirement['value']) && $requirement['value']) {
  186. $message .= ' ('. st('Currently using !item !version', array('!item' => $requirement['title'], '!version' => $requirement['value'])) .')';
  187. }
  188. drush_log($message, 'warning');
  189. }
  190. }
  191. }
  192. if (isset($missing_requirement)) {
  193. return drush_set_error('PROVISION_INSTALL_MISSING_REQUIREMENTS');
  194. }
  195. // Verify existence of all required modules.
  196. $modules = drupal_verify_profile($profile, $install_locale);
  197. if (!$modules) {
  198. return drush_set_error('PROVISION_DRUPAL_INSTALL_FAILED');
  199. }
  200. $system_path = dirname(drupal_get_filename('module', 'system', NULL));
  201. require_once './'. $system_path .'/system.install';
  202. module_invoke('system', 'install');
  203. $system_versions = drupal_get_schema_versions('system');
  204. $system_version = $system_versions ? max($system_versions) : SCHEMA_INSTALLED;
  205. db_query("INSERT INTO {system} (filename, name, type, owner, status, throttle, bootstrap, schema_version) VALUES('%s', '%s', '%s', '%s', %d, %d, %d, %d)", $system_path .'/system.module', 'system', 'module', '', 1, 0, 0, $system_version);
  206. // Now that we've installed things properly, bootstrap the full Drupal environment
  207. module_rebuild_cache();
  208. drush_bootstrap(DRUSH_BOOTSTRAP_DRUPAL_FULL);
  209. $modules = array_diff($modules, array('system'));
  210. if ($install_locale != 'en') {
  211. $modules = array_merge($modules, array('locale'));
  212. }
  213. /**
  214. * Further installation tasks
  215. *
  216. * This code is based on install_tasks() in install.php
  217. * It has been modified to remove any calls to the user interface,
  218. * and run all batches in the same process, instead of in a single page
  219. * load.
  220. */
  221. // profile-install and profile-install-batch tasks
  222. $files = module_rebuild_cache();
  223. foreach ($modules as $module) {
  224. _drupal_install_module($module);
  225. module_enable(array($module));
  226. drush_log(dt("Installed @module module.",
  227. array("@module" => $files[$module]->info['name'])));
  228. }
  229. module_invoke_all('init');
  230. drush_log("Initial locale import");
  231. // locale-initial-import and locale-inintial-batch tasks
  232. if (!empty($install_locale) && ($install_locale != 'en')) {
  233. include_once 'includes/locale.inc';
  234. // Enable installation language as default site language.
  235. locale_add_language($install_locale, NULL, NULL, NULL, NULL, NULL, 1, TRUE);
  236. // Collect files to import for this language.
  237. $batch = locale_batch_by_language($install_locale);
  238. if (!empty($batch)) {
  239. $install_locale_batch_components = $batch['#components'];
  240. batch_set($batch);
  241. $batch =& batch_get();
  242. $batch['progressive'] = FALSE;
  243. batch_process();
  244. }
  245. }
  246. // configure-task
  247. variable_set('site_name', $url);
  248. variable_set('site_mail', 'webmaster@' . $url);
  249. variable_set('clean_url', TRUE);
  250. variable_set('install_time', time());
  251. // Get the timezone offset from system time
  252. $tz_offset = date('Z');
  253. variable_set('date_default_timezone', $tz_offset);
  254. menu_rebuild();
  255. // profile task
  256. // @TODO support install profiles with multiple additional tasks
  257. $task = 'profile';
  258. $function = $profile .'_profile_tasks';
  259. if (function_exists($function)) {
  260. while (!in_array($task, array('profile-finished', 'finished'))) {
  261. drush_log(dt("Running profile specific task : !task", array('!task' => $task)));
  262. module_rebuild_cache();
  263. module_load_all();
  264. // stop batch api from re-directing. ever.
  265. $batch =& batch_get();
  266. $batch['progressive'] = FALSE;
  267. $batch['redirect'] = null;
  268. $batch['operations'] = array();
  269. $batch['url'] = 'batch';
  270. batch_set($batch);
  271. // The profile needs to run more code, maybe even more tasks.
  272. // $task is sent through as a reference and may be changed!
  273. $output = $function($task, $url);
  274. $task = variable_get('install_task', $task);
  275. variable_del('install_task');
  276. // If the profile doesn't move on to a new task we assume
  277. // that it is done.
  278. if ($task == 'profile') {
  279. $task = 'profile-finished';
  280. }
  281. }
  282. }
  283. if ($task == 'profile-finished') {
  284. // profile-finished task
  285. // Secondary locale import
  286. if (!empty($install_locale) && ($install_locale != 'en')) {
  287. // Collect files to import for this language. Skip components
  288. // already covered in the initial batch set.
  289. $batch = locale_batch_by_language($install_locale, NULL, $install_locale_batch_components);
  290. if (!empty($batch)) {
  291. // Start a batch, switch to 'locale-remaining-batch' task. We need to
  292. // set the variable here, because batch_process() redirects.
  293. batch_set($batch);
  294. $batch =& batch_get();
  295. $batch['progressive'] = FALSE;
  296. batch_process();
  297. }
  298. }
  299. }
  300. // done task
  301. // Rebuild menu to get content type links registered by the profile,
  302. // and possibly any other menu items created through the tasks.
  303. menu_rebuild();
  304. // Register actions declared by any modules.
  305. actions_synchronize();
  306. // Randomize query-strings on css/js files, to hide the fact that
  307. // this is a new install, not upgraded yet.
  308. _drupal_flush_css_js();
  309. cache_clear_all();
  310. variable_set('install_profile', $profile);
  311. $client_email = install_validate_client_email(drush_get_option('client_email', FALSE));
  312. $account = install_create_admin_user($client_email);
  313. $onetime = user_pass_reset_url($account);
  314. // Store the one time login link in an option so the front end can direct the user to their new site.
  315. drush_set_option('login_link', $onetime . '/login');
  316. drush_log(dt('Login url: !onetime', array('!onetime' => $onetime . '/login')), 'success');
  317. if ($client_email) {
  318. install_send_welcome_mail($url, $account, $profile, $install_locale, $client_email, $onetime);
  319. }
  320. variable_set('install_task', 'done');
  321. }
  322. /**
  323. * Batch callback for batch installation of modules.
  324. */
  325. function _install_module_batch($module, $module_name, &$context) {
  326. drush_log(dt("Enabling module : !module", array("!module" => $module)), 'success');
  327. _drupal_install_module($module);
  328. // We enable the installed module right away, so that the module will be
  329. // loaded by drupal_bootstrap in subsequent batch requests, and other
  330. // modules possibly depending on it can safely perform their installation
  331. // steps.
  332. module_enable(array($module));
  333. $context['results'][] = $module;
  334. $context['message'] = dt('Installed %module module.', array('%module' => $module_name));
  335. }
  336. install_main();