You are here

simplesitemap.install in Simple XML sitemap 8

Module install and update procedures.

File

simplesitemap.install
View source
<?php

/**
 * @file
 * Module install and update procedures.
 */

/**
 * 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;
}

/**
 * Implements hook_schema().
 */
function simplesitemap_schema() {
  $schema['simplesitemap'] = array(
    'description' => 'Holds XML sitemaps as strings for quick retrieval.',
    'fields' => array(
      'id' => array(
        'description' => 'Sitemap chunk unique identifier.',
        'type' => 'int',
        'size' => 'small',
        'not null' => TRUE,
      ),
      'sitemap_string' => array(
        'description' => 'XML sitemap chunk string.',
        'type' => 'text',
        'size' => 'big',
        'not null' => TRUE,
      ),
      'sitemap_created' => array(
        'description' => 'Timestamp of sitemap chunk generation.',
        'type' => 'int',
        'default' => 0,
        'not null' => TRUE,
      ),
    ),
    'primary key' => array(
      'id',
    ),
  );
  return $schema;
}

/**
 * Implements hook_install().
 */
function simplesitemap_install() {
  $base_url = $GLOBALS['base_url'];
  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",
  )));
}

/**
 * Update: Altering the {simplesitemap} language_code table field to hold longer strings.
 */
function simplesitemap_update_8101() {
  db_change_field('simplesitemap', 'language_code', 'language_code', array(
    'type' => 'varchar',
    'not null' => TRUE,
    'length' => 12,
  ));
}

/**
 * Changing config settings according to new config structure.
 */
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']);
  }
  \Drupal::service('config.factory')
    ->getEditable('simplesitemap.settings')
    ->set('entity_types', $entity_types)
    ->save();
}

/**
 * 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',
    ),
  ));
  return t('Before the sitemap can be accessed, it must be regenerated manually or via cron run.');
}

/**
 * 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,
  ));
  \Drupal::service('config.factory')
    ->getEditable('simplesitemap.settings')
    ->set('settings', array(
    'max_links' => 2000,
  ))
    ->save();
  return t('Before the sitemap can be accessed, it must be regenerated manually or via cron run.');
}

/**
 * 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,
  ));
}

/**
 * Changing column name to 'sitemap_created', as 'generated' is a reserved word since MySQL 5.7.6.
 */
function simplesitemap_update_8106() {

  // 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");
}

/**
 * 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();
}

Functions

Namesort descending Description
simplesitemap_install Implements hook_install().
simplesitemap_requirements Implements hook_requirements().
simplesitemap_schema Implements hook_schema().
simplesitemap_update_8101 Update: Altering the {simplesitemap} language_code table field to hold longer strings.
simplesitemap_update_8102 Changing config settings according to new config structure.
simplesitemap_update_8103 Modifying database structure to hold one multilingual sitemap.
simplesitemap_update_8104 Modifying database structure to accommodate sitemap chunks.
simplesitemap_update_8105 Modifying database structure to accommodate sitemap chunk timestamps.
simplesitemap_update_8106 Changing column name to 'sitemap_created', as 'generated' is a reserved word since MySQL 5.7.6.
simplesitemap_update_8107 Setting the new cron generation setting to TRUE by default.