simplesitemap.install 6.26 KB
Newer Older
1 2 3
<?php
/**
 * @file
4
 * Module install and update procedures.
5 6
 */

7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
/**
 * Implements hook_requirements().
 */
function simplesitemap_requirements($phase) {
  $requirements = array();

  if (!extension_loaded('xmlwriter')) {
    $requirements['simplesitemap_php_extensions'] = array(
      'title' => t('Simple XML sitemap PHP extensions'),
      'value' => t('Missing PHP xmlwriter extension'),
      'description' => t("In order to be able to generate sitemaps, the Simple XML sitemap module requires the <em>xmlwriter</em> PHP extension to be enabled."),
      'severity' => REQUIREMENT_ERROR,
    );
  }

  switch($phase) {
    case 'runtime':
      $sitemap = new \Drupal\simplesitemap\Simplesitemap();
      $generated_ago = $sitemap->get_generated_ago();
      if (!$generated_ago) {
        $value = t('Not available');
        $description = t("The sitemap has not been generated yet. Run cron, or <a href='@generate'>generate</a> the sitemap manually.", array('@generate' => $GLOBALS['base_url'] . '/admin/config/search/simplesitemap'));
        $severity = REQUIREMENT_WARNING;
      }
      else {
        $value = t('XML sitemap is available');
        $description = t("The <a href='@sitemap'>XML sitemap</a> was generated @ago ago. <a href='@generate'>Regenerate</a>.", array('@ago' => $generated_ago, '@sitemap' => $GLOBALS['base_url'] . '/sitemap.xml', '@generate' => $GLOBALS['base_url'] . '/admin/config/search/simplesitemap'));
        $severity = REQUIREMENT_INFO;
      }
      $requirements['simplesitemap_generated'] = array(
        'title' => 'Simple XML sitemap',
        'value' => $value,
        'description' => $description,
        'severity' => $severity,
      );
      break;
  }
  return $requirements;
}

47 48 49 50 51 52 53
/**
 * Implements hook_schema().
 */
function simplesitemap_schema() {
  $schema['simplesitemap'] = array(
    'description' => 'Holds XML sitemaps as strings for quick retrieval.',
    'fields' => array(
54 55
      'id' => array(
        'description' => 'Sitemap chunk unique identifier.',
56 57
        'type' => 'int',
        'size' => 'small',
58 59 60
        'not null' => TRUE,
      ),
      'sitemap_string' => array(
61
        'description' => 'XML sitemap chunk string.',
62 63 64 65
        'type' => 'text',
        'size' => 'big',
        'not null' => TRUE,
      ),
66
      'sitemap_created' => array(
67 68 69 70 71
        'description' => 'Timestamp of sitemap chunk generation.',
        'type' => 'int',
        'default' => 0,
        'not null' => TRUE,
      ),
72
    ),
73
    'primary key' => array('id'),
74 75 76
  );
  return $schema;
}
77

78 79 80 81 82
/**
 * Implements hook_install().
 */
function simplesitemap_install() {
  $base_url = $GLOBALS['base_url'];
83
  drupal_set_message(t("You can now include content into the <a href='@sitemap_url' target='_blank'>XML sitemap</a> by visiting the corresponding entity type edit pages (e.g. <a href='@content_type_url' target='_blank'>content type</a>, <a href='@taxonomy_vocabulary_url' target='_blank'>taxonomy vocabulary</a>, <a href='@menu_url' target='_blank'>menu</a> and <a href='@user_url' target='_blank'>user</a> pages.)<br/>Custom links can be added on <a href='@config_url' target='_blank'>this configuration page</a>.", array('@sitemap_url' => "$base_url/sitemap.xml", '@content_type_url' => "$base_url/admin/structure/types", '@taxonomy_vocabulary_url' => "$base_url/admin/structure/taxonomy", '@menu_url' => "$base_url/admin/structure/menu", '@user_url' => "$base_url/admin/config/people/accounts", '@config_url' => "$base_url/admin/config/search/simplesitemap/custom")));
84 85
}

86
/**
87
 * Update: Altering the {simplesitemap} language_code table field to hold longer strings.
88 89 90 91 92 93 94
 */
function simplesitemap_update_8101() {
  db_change_field('simplesitemap', 'language_code', 'language_code', array(
    'type' => 'varchar',
    'not null' => TRUE,
    'length' => 12,));
}
95 96

/**
97
 * Changing config settings according to new config structure.
98 99 100 101 102 103 104 105 106 107 108 109
 */
function simplesitemap_update_8102() {
  $entity_types = \Drupal::config('simplesitemap.settings')->get('entity_types');
  $entity_types = is_array($entity_types) ? $entity_types : array();
  if (isset($entity_types['node'])) {
    $entity_types['node_type'] = $entity_types['node'];
    unset($entity_types['node']);
  }
  if (isset($entity_types['taxonomy_term'])) {
    $entity_types['taxonomy_vocabulary'] = $entity_types['taxonomy_term'];
    unset($entity_types['taxonomy_term']);
  }
110 111
  \Drupal::service('config.factory')->getEditable('simplesitemap.settings')
    ->set('entity_types', $entity_types)->save();
112
}
113 114 115 116 117 118 119 120 121 122 123

/**
 * Modifying database structure to hold one multilingual sitemap.
 */
function simplesitemap_update_8103() {
  db_truncate('simplesitemap')->execute();
  db_drop_primary_key('simplesitemap');
  db_change_field('simplesitemap', 'language_code', 'id', array(
    'type' => 'serial',
    'not null' => TRUE
  ), array('primary key' => array('id')));
gbyte.co's avatar
gbyte.co committed
124
  return t('Before the sitemap can be accessed, it must be regenerated manually or via cron run.');
125
}
126 127 128 129 130 131 132 133 134 135 136

/**
 * Modifying database structure to accommodate sitemap chunks.
 */
function simplesitemap_update_8104() {
  db_truncate('simplesitemap')->execute();
  db_change_field('simplesitemap', 'id', 'id', array(
    'type' => 'int',
    'size' => 'small',
    'not null' => TRUE,
  ));
137 138
  \Drupal::service('config.factory')->getEditable('simplesitemap.settings')
    ->set('settings', array('max_links' => 2000))->save();
139 140
  return t('Before the sitemap can be accessed, it must be regenerated manually or via cron run.');
}
141 142 143 144 145 146 147 148 149 150 151 152

/**
 * Modifying database structure to accommodate sitemap chunk timestamps.
 */
function simplesitemap_update_8105() {
  db_add_field('simplesitemap', 'generated', array(
    'description' => 'Timestamp of sitemap chunk generation.',
    'type' => 'int',
    'default' => 0,
    'not null' => TRUE,
  ));
}
153 154 155 156 157

/**
 * Changing column name to 'sitemap_created', as 'generated' is a reserved word since MySQL 5.7.6.
 */
function simplesitemap_update_8106() {
158 159 160
  // Omitting the high level DB API to be able to escape the column name.
  Drupal\Core\Database\Database::getConnection()
    ->query("alter table {simplesitemap} CHANGE `generated` sitemap_created int(11) NOT NULL");
161 162
}

163 164 165 166 167 168 169 170
/**
 * Setting the new cron generation setting to TRUE by default.
 */
function simplesitemap_update_8107() {
  \Drupal::service('config.factory')->getEditable('simplesitemap.settings')
    ->set('settings', array('cron_generate' => TRUE))->save();
}