simple_sitemap.install 15.7 KB
Newer Older
gbyte.co's avatar
gbyte.co committed
1
<?php
gbyte.co's avatar
gbyte.co committed
2

gbyte.co's avatar
gbyte.co committed
3 4 5 6 7 8 9
/**
 * @file
 * Module install and update procedures.
 */

/**
 * Implements hook_requirements().
10 11 12
 *
 * @param $phase
 * @return array
gbyte.co's avatar
gbyte.co committed
13 14
 */
function simple_sitemap_requirements($phase) {
15
  $requirements = [];
gbyte.co's avatar
gbyte.co committed
16 17

  if (!extension_loaded('xmlwriter')) {
18
    $requirements['simple_sitemap_php_extensions'] = [
gbyte.co's avatar
gbyte.co committed
19 20
      'title' => t('Simple XML sitemap PHP extensions'),
      'value' => t('Missing PHP xmlwriter extension'),
gbyte.co's avatar
gbyte.co committed
21
      '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.'),
gbyte.co's avatar
gbyte.co committed
22
      'severity' => REQUIREMENT_ERROR,
23
    ];
gbyte.co's avatar
gbyte.co committed
24 25
  }

gbyte.co's avatar
gbyte.co committed
26
  switch ($phase) {
27

gbyte.co's avatar
gbyte.co committed
28
    case 'runtime':
29 30

      $generator = \Drupal::service('simple_sitemap.generator');
31
      $generated_ago = $generator->getGeneratedAgo('default'); //todo
32 33
      $cron_generation = $generator->getSetting('cron_generate');

gbyte.co's avatar
gbyte.co committed
34 35
      if (!$generated_ago) {
        $value = t('Not available');
36
        $description = t($cron_generation
gbyte.co's avatar
gbyte.co committed
37 38
          ? 'Run cron, or <a href="@generate">generate</a> the sitemap manually.'
          : 'Generation on cron run is disabled. <a href="@generate">Generate</a> the sitemap manually.', [
39 40 41
            '@generate' => $GLOBALS['base_url'] . '/admin/config/search/simplesitemap'
          ]
        );
gbyte.co's avatar
gbyte.co committed
42 43 44
        $severity = REQUIREMENT_WARNING;
      }
      else {
45 46
        $value = t('XML sitemaps are available');
        $description = t('The last generation finished @ago ago.'
47
          . ' ' . ($cron_generation
48 49
            ? 'Run cron, or <a href="@generate">regenerate</a> the sitemaps manually.'
            : 'Generation on cron run is disabled. <a href="@generate">Regenerate</a> the sitemaps manually.'), [
50 51 52 53
              '@ago' => $generated_ago,
              '@generate' => $GLOBALS['base_url'] . '/admin/config/search/simplesitemap'
            ]
          );
gbyte.co's avatar
gbyte.co committed
54 55
        $severity = REQUIREMENT_INFO;
      }
56

57
      $requirements['simple_sitemap_generated'] = [
gbyte.co's avatar
gbyte.co committed
58 59 60 61
        'title' => 'Simple XML sitemap',
        'value' => $value,
        'description' => $description,
        'severity' => $severity,
62
      ];
gbyte.co's avatar
gbyte.co committed
63 64 65 66 67 68 69 70 71
      break;
  }
  return $requirements;
}

/**
 * Implements hook_schema().
 */
function simple_sitemap_schema() {
72
  $schema['simple_sitemap'] = [
gbyte.co's avatar
gbyte.co committed
73
    'description' => 'Holds XML sitemaps as strings for quick retrieval.',
74 75
    'fields' => [
      'id' => [
gbyte.co's avatar
gbyte.co committed
76 77 78 79
        'description' => 'Sitemap chunk unique identifier.',
        'type' => 'int',
        'size' => 'small',
        'not null' => TRUE,
80
        'unsigned' => TRUE,
81
      ],
82 83 84 85 86 87 88 89 90 91 92 93 94 95
      'type' => [
        'description' => 'Type of sitemap this chunk belongs to.',
        'type' => 'varchar',
        'length' => 50,
        'not null' => TRUE,
        'default' => '',
      ],
      'delta' => [
        'description' => 'Delta of the chunk within the type scope.',
        'type' => 'int',
        'size' => 'small',
        'not null' => TRUE,
        'unsigned' => TRUE,
      ],
96
      'sitemap_string' => [
gbyte.co's avatar
gbyte.co committed
97 98 99 100
        'description' => 'XML sitemap chunk string.',
        'type' => 'text',
        'size' => 'big',
        'not null' => TRUE,
101 102
      ],
      'sitemap_created' => [
gbyte.co's avatar
gbyte.co committed
103 104 105 106
        'description' => 'Timestamp of sitemap chunk generation.',
        'type' => 'int',
        'default' => 0,
        'not null' => TRUE,
107 108 109 110 111 112
        'unsigned' => TRUE,
      ],
    ],
    'primary key' => ['id'],
  ];

113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
//  $schema['simple_sitemap_index'] = [
//    'description' => 'Holds XML sitemaps indexes as strings for quick retrieval.',
//    'fields' => [
//      'id' => [
//        'description' => 'Sitemap index unique identifier.',
//        'type' => 'varchar',
//        'length' => 50,
//        'not null' => TRUE,
//        'default' => '',
//      ],
//      'sitemap_string' => [
//        'description' => 'XML sitemap index string.',
//        'type' => 'text',
//        'size' => 'big',
//        'not null' => TRUE,
//      ]
//    ],
//    'primary key' => ['id'],
//  ];

133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
  $schema['simple_sitemap_entity_overrides'] = [
    'description' => 'Holds sitemap settings overridden by entities.',
    'fields' => [
      'id' => [
        'description' => 'Override unique identifier.',
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ],
      'entity_type' => [
        'description' => 'Entity type of the overriding entity.',
        'type' => 'varchar',
        'length' => 32,
        'not null' => TRUE,
      ],
      'entity_id' => [
        'description' => 'ID of the overriding entity.',
150 151
        'type' => 'varchar',
        'length' => 32,
152 153 154 155 156
        'not null' => TRUE,
      ],
      'inclusion_settings' => [
        'description' => 'Setting for the overriding entity.',
        'type' => 'blob',
157 158 159 160
      ],
    ],
    'primary key' => ['id'],
  ];
gbyte.co's avatar
gbyte.co committed
161 162 163 164 165 166 167 168
  return $schema;
}

/**
 * Implements hook_install().
 */
function simple_sitemap_install() {
  $base_url = $GLOBALS['base_url'];
169
  drupal_set_message(t("You can now include content into the sitemap by visiting the corresponding entity type edit pages (e.g. <a href='@content_type_url'>node type edit pages</a>).<br/>Support for additional entity types and custom links can be added on <a href='@config_url'>the module's configuration pages</a>.", ['@content_type_url' => "$base_url/admin/structure/types", '@config_url' => "$base_url/admin/config/search/simplesitemap"]));
170 171 172 173 174 175 176
}

/**
 * Changing the data structure of the module's configuration.
 */
function simple_sitemap_update_8201() {
  $entity_types = \Drupal::config('simple_sitemap.settings')->get('entity_types');
177
  $entity_types = is_array($entity_types) ? $entity_types : [];
178 179 180 181 182 183 184
  $naming_changes = [
    'node_type' => 'node',
    'taxonomy_vocabulary' => 'taxonomy_term',
    'menu' => 'menu_link_content',
    'commerce_product_type' => 'commerce_product',
    'media_bundle' => 'media',
  ];
gbyte.co's avatar
gbyte.co committed
185
  foreach ($entity_types as $entity_type_name => $settings) {
186 187 188 189 190 191 192
    if (isset($naming_changes[$entity_type_name])) {
      $entity_types[$naming_changes[$entity_type_name]] = $entity_types[$entity_type_name];
      unset($entity_types[$entity_type_name]);
    }
  }
  \Drupal::service('config.factory')->getEditable('simple_sitemap.settings')
    ->set('entity_types', $entity_types)->save();
gbyte.co's avatar
gbyte.co committed
193
}
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238

/**
 * Moving entity overrides from configuration to database table.
 */
function simple_sitemap_update_8202() {
  $database = \Drupal::database();

  // Create database table.
  if (!$database->schema()->tableExists('simple_sitemap_entity_overrides')) {
    $database->schema()->createTable('simple_sitemap_entity_overrides', [
      'description' => 'Holds sitemap settings overridden by entities.',
      'fields' => [
        'id' => [
          'description' => 'Override unique identifier.',
          'type' => 'serial',
          'unsigned' => TRUE,
          'not null' => TRUE,
        ],
        'entity_type' => [
          'description' => 'Entity type of the overriding entity.',
          'type' => 'varchar',
          'length' => 32,
          'not null' => TRUE,
        ],
        'entity_id' => [
          'description' => 'ID of the overriding entity.',
          'type' => 'int',
          'unsigned' => TRUE,
          'not null' => TRUE,
        ],
        'inclusion_settings' => [
          'description' => 'Setting for the overriding entity.',
          'type' => 'blob',
        ],
      ],
      'primary key' => ['id'],
    ]);
  }

  // Populate database table with config values.
  $entity_types = \Drupal::config('simple_sitemap.settings')->get('entity_types');
  $entity_types = is_array($entity_types) ? $entity_types : [];

  foreach ($entity_types as $entity_type_name => &$entity_type) {
    if (is_array($entity_type)) {
gbyte.co's avatar
gbyte.co committed
239
      foreach ($entity_type as $bundle_name => &$bundle) {
240
        if (isset($bundle['entities'])) {
gbyte.co's avatar
gbyte.co committed
241
          foreach ($bundle['entities'] as $entity_id => $entity_settings) {
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
            $database->insert('simple_sitemap_entity_overrides')
              ->fields([
                'entity_type' => $entity_type_name,
                'entity_id' => $entity_id,
                'inclusion_settings' => serialize($entity_settings),
              ])
              ->execute();
          }
          // Remove entity overrides from configuration.
          unset($bundle['entities']);
        }
      }
    }
  }

  \Drupal::service('config.factory')->getEditable('simple_sitemap.settings')
    ->set('entity_types', $entity_types)->save();
}

/**
 * Splitting simple_sitemap.settings configuration into simple_sitemap.settings,
 * simple_sitemap.entity_types and simple_sitemap.custom.
 */
function simple_sitemap_update_8203() {
266 267
  $old_config = $config = \Drupal::config('simple_sitemap.settings');
  foreach (['entity_types', 'custom'] as $config_name) {
268 269 270
    if (!$config = $old_config->get($config_name)) {
      continue;
    }
271 272 273 274 275 276
    \Drupal::service('config.factory')->getEditable("simple_sitemap.$config_name")
      ->setData($config)->save();
  }
  $settings = $old_config->get('settings');
  \Drupal::service('config.factory')->getEditable("simple_sitemap.settings")
    ->setData($settings)->save();
277
}
278 279

/**
280
 * Removing entity type settings for entity types which do not have the canonical
281 282 283 284 285 286
 * link template.
 */
function simple_sitemap_update_8204() {
  $sitemap_entity_types = \Drupal::service('entity_type.manager')->getDefinitions();
  $entity_types = \Drupal::config('simple_sitemap.entity_types')->get();
  unset($entity_types['_core']);
gbyte.co's avatar
gbyte.co committed
287
  foreach ($entity_types as $entity_type_id => $entity_type) {
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
    if (!isset($sitemap_entity_types[$entity_type_id])
      || !$sitemap_entity_types[$entity_type_id]->hasLinkTemplate('canonical')) {

      // Delete entity overrides.
      \Drupal::database()->delete('simple_sitemap_entity_overrides')
        ->condition('entity_type', $entity_type_id)
        ->execute();

      // Delete entity type settings.
      unset($entity_types[$entity_type_id]);
    }
  }
  \Drupal::service('config.factory')->getEditable("simple_sitemap.entity_types")
    ->setData($entity_types)->save();
}
303 304 305 306 307 308 309 310 311

/**
 * Splitting simple_sitemap.entity_types into individual configuration objects
 * for each bundle.
 */
function simple_sitemap_update_8205() {
  $entity_types = \Drupal::config('simple_sitemap.entity_types')->get();
  unset($entity_types['_core']);
  $enabled_entity_types = [];
gbyte.co's avatar
gbyte.co committed
312
  foreach ($entity_types as $entity_type_id => $bundles) {
313
    $enabled_entity_types[] = $entity_type_id;
gbyte.co's avatar
gbyte.co committed
314
    foreach ($bundles as $bundle_name => $bundle_settings) {
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
      \Drupal::service('config.factory')
        ->getEditable("simple_sitemap.bundle_settings.$entity_type_id.$bundle_name")
        ->setData($bundle_settings)->save();
    }
  }

  // Add enabled entity type settings.
  \Drupal::service('config.factory')
    ->getEditable('simple_sitemap.settings')
    ->set('enabled_entity_types', $enabled_entity_types)
    ->save();

  // Remove old configuration object.
  \Drupal::service('config.factory')
    ->getEditable('simple_sitemap.entity_types')
    ->delete();
}
332 333 334 335 336 337

/**
 * Placing custom links in a subkey of simple_sitemap.custom configuration.
 */
function simple_sitemap_update_8206() {
  $custom_links = \Drupal::config('simple_sitemap.custom')->get();
gbyte.co's avatar
gbyte.co committed
338
  foreach ($custom_links as $i => $custom_link) {
339 340 341 342
    if (!isset($custom_link['path'])) {
      unset($custom_links[$i]);
    }
  }
gbyte.co's avatar
gbyte.co committed
343
  \Drupal::service('config.factory')->getEditable('simple_sitemap.custom')
344 345
    ->setData(['links' => $custom_links])->save();
}
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361

/**
 * Updating entity_id field of simple_sitemap_entity_overrides table to varchar(32).
 */
function simple_sitemap_update_8207() {
  \Drupal::database()->schema()->changeField(
    'simple_sitemap_entity_overrides',
    'entity_id',
    'entity_id', [
      'description' => 'ID of the overriding entity.',
      'type' => 'varchar',
      'length' => 32,
      'not null' => TRUE,
    ]
  );
}
362 363

/**
364
 * Adding changefreq setting to all existing bundle and entity instance settings.
365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
 */
function simple_sitemap_update_8208() {

  // Update existing bundle settings.
  $config_factory = \Drupal::service('config.factory');
  $entity_types = $config_factory->listAll('simple_sitemap.bundle_settings.');

  foreach ($entity_types as $entity_type) {
    $config = $config_factory->get($entity_type)->get();
    if (!isset($config['changefreq'])) {
      $config_factory->getEditable($entity_type)
        ->setData($config + ['changefreq' => ''])
        ->save();
    }
  }

  // Update existing entity override data.
  $results = \Drupal::database()->select('simple_sitemap_entity_overrides', 'o')
    ->fields('o', ['id', 'inclusion_settings'])
    ->execute()->fetchAll(\PDO::FETCH_OBJ);

  foreach ($results as $row) {
    $settings = unserialize($row->inclusion_settings);
    if (!isset($settings['changefreq'])) {
      \Drupal::database()->update('simple_sitemap_entity_overrides')
        ->fields(['inclusion_settings' => serialize($settings + ['changefreq' => '']),])
        ->condition('id', $row->id)
        ->execute();
    }
  }
395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432

  return t('You may now want to configure the new changefreq setting for sitemap entities and custom links.');
}

/**
 * Adding image inclusion setting to all existing bundle and entity instance settings.
 */
function simple_sitemap_update_8209() {

  // Update existing bundle settings.
  $config_factory = \Drupal::service('config.factory');
  $entity_types = $config_factory->listAll('simple_sitemap.bundle_settings.');

  foreach ($entity_types as $entity_type) {
    $config = $config_factory->get($entity_type)->get();
    if (!isset($config['include_images'])) {
      $config_factory->getEditable($entity_type)
        ->setData($config + ['include_images' => 0])
        ->save();
    }
  }

  // Update existing entity override data.
  $results = \Drupal::database()->select('simple_sitemap_entity_overrides', 'o')
    ->fields('o', ['id', 'inclusion_settings'])
    ->execute()->fetchAll(\PDO::FETCH_OBJ);

  foreach ($results as $row) {
    $settings = unserialize($row->inclusion_settings);
    if (!isset($settings['include_images'])) {
      \Drupal::database()->update('simple_sitemap_entity_overrides')
        ->fields(['inclusion_settings' => serialize($settings + ['include_images' => 0]),])
        ->condition('id', $row->id)
        ->execute();
    }
  }

  return t('You may now want to configure your sitemap entities to include images.');
433
}
434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494

/**
 * Adding simple_sitemap_index table and altering the simple_sitemap table.
 */
function simple_sitemap_update_8210() {

  $database = \Drupal::database();

  // Create simple_sitemap_index table.
//  if (!$database->schema()->tableExists('simple_sitemap_index')) {
//    $database->schema()->createTable('simple_sitemap_index', [
//      'description' => 'Holds XML sitemaps indexes as strings for quick retrieval.',
//      'fields' => [
//        'id' => [
//          'description' => 'Sitemap index unique identifier.',
//          'type' => 'varchar',
//          'length' => 50,
//          'not null' => TRUE,
//          'default' => '',
//        ],
//        'sitemap_string' => [
//          'description' => 'XML sitemap index string.',
//          'type' => 'text',
//          'size' => 'big',
//          'not null' => TRUE,
//        ]
//      ],
//      'primary key' => ['id'],
//    ]);
//  }

    // Alter simple_sitemap table.
    $database->truncate('simple_sitemap')->execute();

    if (!$database->schema()->fieldExists('simple_sitemap', 'type')) {
      $database->schema()->addField(
        'simple_sitemap',
        'type', [
          'description' => 'Type of sitemap this chunk belongs to.',
          'type' => 'varchar',
          'length' => 50,
          'not null' => TRUE,
          'default' => '',
        ]
      );
    }
    if (!$database->schema()->fieldExists('simple_sitemap', 'delta')) {
      $database->schema()->addField(
        'simple_sitemap',
        'delta', [
          'description' => 'Delta of the chunk within the type scope.',
          'type' => 'int',
          'size' => 'small',
          'not null' => TRUE,
          'unsigned' => TRUE,
        ]
      );
    }
}