automatic_updates.module 5.17 KB
Newer Older
1
2
3
4
<?php

/**
 * @file
5
 * Contains automatic_updates.module.
6
7
8
 */

/**
9
 * Implements hook_init().
10
 */
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
function automatic_updates_init() {
  if (arg(0) == 'admin' && user_access('administer site configuration')) {
    $disabled_routes = array(
      'admin/appearance/update',
      'admin/appearance/install',
      'admin/modules/update',
      'admin/modules/install',
      'admin/reports/updates',
      'admin/reports/updates/update',
      'admin/reports/updates/install',
      'admin/reports/updates/settings',
      'admin/reports/status',
      'admin/update/ready',
    );
    // These paths don't need additional nagging.
    if (in_array($_GET['q'], $disabled_routes)) {
27
      return;
28
    }
29
30
31

    $messages = AutomaticUpdatesPsa::getPublicServiceMessages();
    if ($messages) {
32
      drupal_set_message(t('Public service announcements:'), 'error', FALSE);
33
      foreach ($messages as $message) {
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
        drupal_set_message($message, 'error', FALSE);
      }
    }

    $last_check_timestamp = ReadinessCheckerManager::timestamp();
    if (REQUEST_TIME > $last_check_timestamp + 3600 * 24) {
      drupal_set_message(t('Your site has not recently run an update readiness check.'), 'error', FALSE);
    }
    $results = ReadinessCheckerManager::getResults('error');
    if ($results) {
      drupal_set_message(t('Your site is currently failing readiness checks for automatic updates. It cannot be <a href="@readiness_checks">automatically updated</a> until further action is performed:', ['@readiness_checks' => 'https://www.drupal.org/docs/8/update/automatic-updates#readiness-checks']), 'error', FALSE);
      foreach ($results as $message) {
        drupal_set_message($message, 'error', FALSE);
      }
    }
    $results = ReadinessCheckerManager::getResults('warning');
    if ($results) {
      drupal_set_message(t('Your site does not pass some readiness checks for automatic updates. Depending on the nature of the failures, it might effect the eligibility for <a href="@readiness_checks">automatic updates</a>.', ['@readiness_checks' => 'https://www.drupal.org/docs/8/update/automatic-updates#readiness-checks']), 'warning', FALSE);
      foreach ($results as $message) {
        drupal_set_message($message, 'warning', FALSE);
54
      }
55
56
57
    }
  }
}
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72

/**
 * Implements hook_menu().
 */
function automatic_updates_menu() {
  $items = [];

  $items['admin/config/system/automatic_updates'] = [
    'title' => 'Automatic Updates',
    'page callback' => 'drupal_get_form',
    'page arguments' => ['automatic_updates_admin_form'],
    'file' => 'automatic_updates.admin.inc',
    'access arguments' => ['administer software updates'],
    'tab parent' => 'admin/config/system',
  ];
73
74
75
76
  $items['admin/config/system/automatic_updates/readiness'] = [
    'title' => 'Update readiness checking...',
    'page callback' => 'automatic_updates_run_checks',
    'file' => 'automatic_updates.admin.inc',
77
78
79
80
81
82
83
84
85
    'access arguments' => ['administer software updates'],
    'type' => MENU_CALLBACK,
  ];
  $items['automatic_updates/in-place-update/%/%/%/%'] = [
    'title' => 'Update',
    'page callback' => 'automatic_updates_in_place_update',
    'page arguments' => [2, 3, 4, 5],
    'type' => MENU_CALLBACK,
    'file' => 'automatic_updates.pages.inc',
86
87
    'access arguments' => ['administer software updates'],
  ];
88
89
90

  return $items;
}
91
92
93
94
95

/**
 * Implements hook_cron().
 */
function automatic_updates_cron() {
96
  Notify::send();
97
98
99
  foreach (ReadinessCheckerManager::getCategories() as $category) {
    ReadinessCheckerManager::run($category);
  }
100
101
102
103
104
105
  // In-place updates won't function for dev releases of Drupal core.
  if (strpos(VERSION, '-dev') !== FALSE) {
    return;
  }
  if (variable_get('automatic_updates_enable_cron_updates', FALSE)) {
    $available = update_get_available(TRUE);
106
107
108
109
110
111
112
113
114
115
    $projects = update_calculate_project_data($available);
    $not_recommended_version = $projects['drupal']['status'] !== UPDATE_CURRENT;
    $dev_core = strpos(VERSION, '-dev') !== FALSE;
    $security_update = in_array($projects['drupal']['status'], [UPDATE_NOT_SECURE, UPDATE_REVOKED], TRUE);
    $recommended_release = $projects['drupal']['releases'][$projects['drupal']['recommended']];
    $major_upgrade = $recommended_release['version_major'] !== $projects['drupal']['existing_major'];
    // Don't automatically update major version bumps or a dev version of core.
    if ($major_upgrade || $dev_core) {
      return;
    }
116
    if (variable_get('automatic_updates_enable_cron_security_updates', FALSE)) {
117
118
      if ($not_recommended_version && $security_update) {
        InPlaceUpdate::update('drupal', 'core', VERSION, $recommended_release['version']);
119
120
      }
    }
121
122
    elseif ($not_recommended_version) {
      InPlaceUpdate::update('drupal', 'core', VERSION, $recommended_release['version']);
123
124
    }
  }
125
}
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147

/**
 * Implements hook_theme().
 */
function automatic_updates_theme(array $existing, $type, $theme, $path) {
  return [
    'automatic_updates_psa_notify' => [
      'variables' => [
        'messages' => [],
      ],
      'template' => 'automatic-updates-psa-notify',
    ],
  ];
}

/**
 * Implements hook_mail().
 */
function automatic_updates_mail($key, &$message, $params) {
  $message['subject'] = $params['subject'];
  $message['body'][] = drupal_render($params['body']);
}