xmlsitemap.module 82.7 KB
Newer Older
Darren Oh's avatar
Darren Oh committed
1 2 3
<?php

/**
4
 * @file
5
 * @defgroup xmlsitemap XML sitemap
Darren Oh's avatar
Darren Oh committed
6 7 8
 */

/**
9 10 11
 * @file
 * Main file for the xmlsitemap module.
 */
12

13
use Drupal\Component\Utility\Crypt;
14
use Drupal\Component\Utility\Environment;
15
use Drupal\Component\Utility\UrlHelper;
16 17
use Drupal\Core\Cache\Cache;
use Drupal\Core\Database\Query\AlterableInterface;
18
use Drupal\Core\Database\Query\Condition;
19
use Drupal\Core\Entity\ContentEntityFormInterface;
20
use Drupal\Core\Entity\ContentEntityInterface;
21
use Drupal\Core\Entity\ContentEntityTypeInterface;
22
use Drupal\Core\Entity\EntityInterface;
23
use Drupal\Core\Entity\EntityTypeInterface;
24
use Drupal\Core\File\FileSystemInterface;
25
use Drupal\Core\Form\FormStateInterface;
26
use Drupal\Core\Language\LanguageInterface;
27 28
use Drupal\Core\Link;
use Drupal\Core\Routing\RouteMatchInterface;
29
use Drupal\Core\Site\Settings;
30
use Drupal\Core\Url;
31
use Drupal\xmlsitemap\Controller\XmlSitemapController;
32 33
use Drupal\xmlsitemap\Entity\XmlSitemap;
use Drupal\xmlsitemap\XmlSitemapInterface;
34
use Symfony\Component\HttpFoundation\Request;
35

36 37 38
/**
 * The maximum number of links in one sitemap chunk file.
 */
39
const XMLSITEMAP_MAX_SITEMAP_LINKS = 50000;
40

41 42 43
/**
 * The maximum filesize of a sitemap chunk file.
 */
44
const XMLSITEMAP_MAX_SITEMAP_FILESIZE = 52528800;
45

46
/**
47
 * Xmlsitemap Frequencies.
48
 */
49
const XMLSITEMAP_FREQUENCY_YEARLY = 31449600;
50
// 60 * 60 * 24 * 7 * 52.
51
const XMLSITEMAP_FREQUENCY_MONTHLY = 2419200;
52
// 60 * 60 * 24 * 7 * 4.
53
const XMLSITEMAP_FREQUENCY_WEEKLY = 604800;
54
// 60 * 60 * 24 * 7.
55
const XMLSITEMAP_FREQUENCY_DAILY = 86400;
56
// 60 * 60 * 24.
57
const XMLSITEMAP_FREQUENCY_HOURLY = 3600;
58
// 60 * 60.
59
const XMLSITEMAP_FREQUENCY_ALWAYS = 60;
60

61 62 63
/**
 * Short lastmod timestamp format.
 */
64
const XMLSITEMAP_LASTMOD_SHORT = 'Y-m-d';
65 66 67 68

/**
 * Medium lastmod timestamp format.
 */
69
const XMLSITEMAP_LASTMOD_MEDIUM = 'Y-m-d\TH:i\Z';
70 71 72 73

/**
 * Long lastmod timestamp format.
 */
74
const XMLSITEMAP_LASTMOD_LONG = 'c';
75

76 77 78
/**
 * The default inclusion status for link types in the sitemaps.
 */
79
const XMLSITEMAP_STATUS_DEFAULT = 0;
80 81 82 83

/**
 * The default priority for link types in the sitemaps.
 */
84
const XMLSITEMAP_PRIORITY_DEFAULT = 0.5;
85

86 87 88 89
/**
 * Implements hook_hook_info().
 */
function xmlsitemap_hook_info() {
90
  $hooks = [
91 92 93 94 95 96
    'xmlsitemap_link_info',
    'xmlsitemap_link_info_alter',
    'xmlsitemap_link_alter',
    'xmlsitemap_index_links',
    'xmlsitemap_context_info',
    'xmlsitemap_context_info_alter',
97
    'xmlsitemap_context_url_options',
98
    'xmlsitemap_context',
99 100
    'xmlsitemap_element_alter',
    'xmlsitemap_root_attributes_alter',
101 102
    'xmlsitemap_sitemap_insert',
    'xmlsitemap_sitemap_update',
103 104 105 106
    'xmlsitemap_sitemap_operations',
    'xmlsitemap_sitemap_delete',
    'xmlsitemap_sitemap_link_url_options_alter',
    'query_xmlsitemap_generate_alter',
107
    'query_xmlsitemap_index_links_alter',
108
    'form_xmlsitemap_sitemap_edit_form_alter',
109
    'xmlsitemap_rebuild_clear',
110
  ];
111 112 113

  $hooks = array_combine($hooks, $hooks);
  foreach ($hooks as $hook => $info) {
114
    $hooks[$hook] = ['group' => 'xmlsitemap'];
115
  }
116

117 118 119
  return $hooks;
}

120 121
/**
 * Implements hook_help().
Darren Oh's avatar
Darren Oh committed
122
 */
123
function xmlsitemap_help($route_name, RouteMatchInterface $route_match) {
124 125
  $output = '';

126
  switch ($route_name) {
127
    case 'help.page.xmlsitemap':
128 129
    case 'xmlsitemap.admin_settings':
    case 'xmlsitemap.entities_settings':
130 131
    case 'entity.xmlsitemap.edit_form':
    case 'entity.xmlsitemap.delete_form':
132
      return;
133

134
    case 'xmlsitemap.admin_search':
135
      break;
136

137
    case 'xmlsitemap.admin_search_list':
138
      break;
139

140
    case 'xmlsitemap.admin_rebuild':
141 142 143
      $output .= '<p>' . t("This action rebuilds your site's XML sitemap and regenerates the cached files, and may be a lengthy process. If you just installed XML sitemap, this can be helpful to import all your site's content into the sitemap. Otherwise, this should only be used in emergencies.") . '</p>';
  }

144
  $currentUser = \Drupal::currentUser();
145
  if (strpos($route_name, 'xmlsitemap') !== FALSE && $currentUser->hasPermission('administer xmlsitemap')) {
146
    // Alert the user to any potential problems detected by hook_requirements.
147
    $output .= _xmlsitemap_get_blurb();
Darren Oh's avatar
Darren Oh committed
148
  }
149 150 151 152

  return $output;
}

153 154 155 156
/**
 * Implements hook_theme().
 */
function xmlsitemap_theme() {
157 158
  return [
    'xmlsitemap_content_settings_table' => [
159 160
      'render element' => 'element',
      'file' => 'xmlsitemap.module',
161 162
    ],
  ];
163 164
}

165 166
/**
 * Menu access callback; determines if the user can use the rebuild links page.
167 168 169
 *
 * @return bool
 *   Returns TRUE if current user can access rebuild form. FALSE otherwise.
170 171 172
 */
function _xmlsitemap_rebuild_form_access() {
  $rebuild_types = xmlsitemap_get_rebuildable_link_types();
173
  return !empty($rebuild_types) && \Drupal::currentUser()->hasPermission('administer xmlsitemap');
174 175
}

Darren Oh's avatar
Darren Oh committed
176
/**
177
 * Implements hook_cron().
178 179 180
 *
 * @todo Use new Queue system. Need to add {sitemap}.queued.
 * @todo Regenerate one at a time?
Darren Oh's avatar
Darren Oh committed
181
 */
182
function xmlsitemap_cron() {
183 184 185 186 187
  // If cron sitemap file regeneration is disabled, stop.
  if (\Drupal::config('xmlsitemap.settings')->get('disable_cron_regeneration')) {
    return;
  }

188
  // If there were no new or changed links, skip.
189
  if (!\Drupal::state()->get('xmlsitemap_regenerate_needed')) {
190 191 192
    return;
  }

193
  // If the minimum sitemap lifetime hasn't been passed, skip.
194
  $lifetime = \Drupal::time()->getRequestTime() - \Drupal::state()->get('xmlsitemap_generated_last');
195
  if ($lifetime < \Drupal::config('xmlsitemap.settings')->get('minimum_lifetime')) {
196 197
    return;
  }
198
  xmlsitemap_xmlsitemap_index_links(\Drupal::config('xmlsitemap.settings')->get('batch_limit'));
199
  // Regenerate the sitemap XML files.
200
  xmlsitemap_run_unprogressive_batch('xmlsitemap_regenerate_batch');
Darren Oh's avatar
Darren Oh committed
201 202
}

203
/**
204
 * Implements hook_modules_installed().
205
 */
206
function xmlsitemap_modules_installed(array $modules) {
207
  Cache::invalidateTags(['xmlsitemap']);
208 209 210
}

/**
211
 * Implements hook_modules_uninstalled().
212
 */
213
function xmlsitemap_modules_uninstalled(array $modules) {
214
  Cache::invalidateTags(['xmlsitemap']);
215 216
}

Darren Oh's avatar
Darren Oh committed
217
/**
218
 * Implements hook_robotstxt().
Darren Oh's avatar
Darren Oh committed
219
 */
220
function xmlsitemap_robotstxt() {
221
  if ($sitemap = XmlSitemap::loadByContext()) {
222
    $uri = xmlsitemap_sitemap_uri($sitemap);
223 224
    $path = UrlHelper::isExternal($uri['path']) ? $uri['path'] : 'base://' . $uri['path'];
    $robotstxt[] = 'Sitemap: ' . Url::fromUri($path, $uri['options'])->toString();
225
    return $robotstxt;
226
  }
Darren Oh's avatar
Darren Oh committed
227 228
}

229
/**
230
 * Internal default variables config for xmlsitemap_var().
231 232 233
 *
 * @return array
 *   Array with config variables of xmlsitemap.settings config object.
234
 */
235
function xmlsitemap_config_variables() {
236
  return [
237
    'minimum_lifetime' => 0,
238 239 240 241 242 243 244 245 246
    'xsl' => 1,
    'prefetch_aliases' => 1,
    'chunk_size' => 'auto',
    'batch_limit' => 100,
    'path' => 'xmlsitemap',
    'frontpage_priority' => 1.0,
    'frontpage_changefreq' => XMLSITEMAP_FREQUENCY_DAILY,
    'lastmod_format' => XMLSITEMAP_LASTMOD_MEDIUM,
    'gz' => FALSE,
247
    'disable_cron_regeneration' => FALSE,
248
  ];
249 250
}

251 252
/**
 * Internal default variables state for xmlsitemap_var().
253 254 255
 *
 * @return array
 *   Array with state variables defined by xmlsitemap module.
256 257
 */
function xmlsitemap_state_variables() {
258
  return [
259 260 261 262
    'xmlsitemap_rebuild_needed' => FALSE,
    'xmlsitemap_regenerate_needed' => TRUE,
    'xmlsitemap_base_url' => '',
    'xmlsitemap_generated_last' => 0,
263 264
    'xmlsitemap_developer_mode' => 0,
    'max_chunks' => NULL,
265
    'max_filesize' => NULL,
266
  ];
267 268
}

269 270 271 272 273 274
/**
 * Internal implementation of variable_get().
 */
function xmlsitemap_var($name, $default = NULL) {
  $defaults = &drupal_static(__FUNCTION__);
  if (!isset($defaults)) {
275 276
    $defaults = xmlsitemap_config_variables();
    $defaults += xmlsitemap_state_variables();
277 278
  }

279 280
  // @todo Remove when stable.
  if (!isset($defaults[$name])) {
281
    trigger_error("Default variable for $name not found.");
282 283
  }

284 285 286 287
  if (\Drupal::state()->get($name, NULL) === NULL) {
    return \Drupal::config('xmlsitemap.settings')->get($name);
  }
  return \Drupal::state()->get($name);
288 289
}

Darren Oh's avatar
Darren Oh committed
290
/**
291
 * @defgroup xmlsitemap_api XML sitemap API.
292
 * @{
293 294
 * This is the XML sitemap API to be used by modules wishing to work with
 * XML sitemap and/or link data.
Darren Oh's avatar
Darren Oh committed
295
 */
296 297 298 299

/**
 * Load an XML sitemap array from the database.
 *
300
 * @param mixed $smid
301
 *   An XML sitemap ID.
302
 *
303
 * @return \Drupal\xmlsitemap\XmlSitemapInterface
304
 *   The XML sitemap object.
305 306
 */
function xmlsitemap_sitemap_load($smid) {
307
  $sitemap = xmlsitemap_sitemap_load_multiple([$smid]);
308
  return $sitemap ? reset($sitemap) : FALSE;
Darren Oh's avatar
Darren Oh committed
309 310 311
}

/**
312 313
 * Load multiple XML sitemaps from the database.
 *
314
 * @param array|bool $smids
315
 *   An array of XML sitemap IDs, or FALSE to load all XML sitemaps.
316
 * @param array $conditions
317
 *   An array of conditions in the form 'field' => $value.
318
 *
319
 * @return \Drupal\xmlsitemap\XmlSitemapInterface[]
320
 *   An array of XML sitemap objects.
Darren Oh's avatar
Darren Oh committed
321
 */
322
function xmlsitemap_sitemap_load_multiple($smids = [], array $conditions = []) {
323 324 325
  if ($smids !== FALSE) {
    $conditions['smid'] = $smids;
  }
326
  else {
327
    $conditions['smid'] = NULL;
328
  }
329
  $storage = Drupal::entityTypeManager()->getStorage('xmlsitemap');
330

331
  /** @var \Drupal\xmlsitemap\XmlSitemapInterface[] $sitemaps */
332
  $sitemaps = $storage->loadMultiple($conditions['smid']);
333
  if (count($sitemaps) <= 0) {
334
    return [];
335
  }
336

337
  return $sitemaps;
338 339
}

Darren Oh's avatar
Darren Oh committed
340
/**
341
 * Save changes to an XML sitemap or add a new XML sitemap.
342
 *
343
 * @param Drupal\xmlsitemap\XmlSitemapInterface $sitemap
344
 *   The XML sitemap array to be saved. If $sitemap->smid is omitted, a new
345 346 347
 *   XML sitemap will be added.
 *
 * @todo Save the sitemap's URL as a column?
Darren Oh's avatar
Darren Oh committed
348
 */
349
function xmlsitemap_sitemap_save(XmlSitemapInterface $sitemap) {
350
  $context = $sitemap->context;
351
  if (!isset($context) || !$context) {
352
    $sitemap->context = [];
353 354
  }

355
  // Make sure context is sorted before saving the hash.
356
  $sitemap->setOriginalId($sitemap->isNew() ? NULL : $sitemap->getId());
357
  $sitemap->setId(xmlsitemap_sitemap_get_context_hash($context));
358
  // If the context was changed, we need to perform additional actions.
359
  if (!$sitemap->isNew() && $sitemap->getId() != $sitemap->getOriginalId()) {
360
    // Rename the files directory so the sitemap does not break.
361
    $old_sitemap = xmlsitemap_sitemap_load($sitemap->getOriginalId());
362
    $old_dir = xmlsitemap_get_directory($old_sitemap);
363 364 365 366
    $new_dir = xmlsitemap_get_directory($sitemap);
    xmlsitemap_directory_move($old_dir, $new_dir);

    // Mark the sitemaps as needing regeneration.
367
    \Drupal::state()->set('xmlsitemap_regenerate_needed', TRUE);
368
  }
369
  $sitemap->save();
370 371

  return $sitemap;
372 373 374
}

/**
375
 * Delete an XML sitemap.
376
 *
377
 * @param string $smid
378
 *   An XML sitemap ID.
379
 */
380
function xmlsitemap_sitemap_delete($smid) {
381
  xmlsitemap_sitemap_delete_multiple([$smid]);
382 383
}

Darren Oh's avatar
Darren Oh committed
384
/**
385
 * Delete multiple XML sitemaps.
386
 *
387
 * @param array $smids
388
 *   An array of XML sitemap IDs.
Darren Oh's avatar
Darren Oh committed
389
 */
390 391 392 393
function xmlsitemap_sitemap_delete_multiple(array $smids) {
  if (!empty($smids)) {
    $sitemaps = xmlsitemap_sitemap_load_multiple($smids);
    foreach ($sitemaps as $sitemap) {
394
      $sitemap->delete();
395
      \Drupal::moduleHandler()->invokeAll('xmlsitemap_sitemap_delete', [$sitemap]);
396
    }
397
  }
398
}
399

400 401 402
/**
 * Return the expected file path for a specific sitemap chunk.
 *
403
 * @param Drupal\xmlsitemap\XmlSitemapInterface $sitemap
404
 *   An XmlSitemapInterface sitemap object.
405
 * @param string $chunk
406
 *   An optional specific chunk in the sitemap. Defaults to the index page.
407 408 409
 *
 * @return string
 *   File path for a specific sitemap chunk.
410
 */
411
function xmlsitemap_sitemap_get_file(XmlSitemapInterface $sitemap, $chunk = 'index') {
412
  return xmlsitemap_get_directory($sitemap) . "/{$chunk}.xml";
Darren Oh's avatar
Darren Oh committed
413 414
}

415 416 417
/**
 * Find the maximum file size of all a sitemap's XML files.
 *
418 419
 * @param \Drupal\xmlsitemap\XmlSitemapInterface $sitemap
 *   The XML sitemap object.
420 421 422
 *
 * @return int
 *   Maximum file size in the directory.
423
 */
424
function xmlsitemap_sitemap_get_max_filesize(XmlSitemapInterface $sitemap) {
425
  $dir = xmlsitemap_get_directory($sitemap);
426
  $sitemap->setMaxFileSize(0);
427 428
  /** @var \Drupal\Core\File\FileSystemInterface $file_system */
  $file_system = \Drupal::service('file_system');
429
  $files = $file_system->scanDirectory($dir, '/\.xml$/');
430
  foreach ($files as $file) {
431
    $sitemap->setMaxFileSize(max($sitemap->getMaxFileSize(), filesize($file->uri)));
432
  }
433
  return $sitemap->getMaxFileSize();
434 435
}

436 437 438 439 440
/**
 * Returns the hash string for a context.
 *
 * @param array $context
 *   Context to be hashed.
441
 *
442 443 444
 * @return string
 *   Hash string for the context.
 */
445
function xmlsitemap_sitemap_get_context_hash(array &$context) {
446
  ksort($context);
447
  return Crypt::hashBase64(serialize($context));
448 449
}

Darren Oh's avatar
Darren Oh committed
450
/**
451
 * Returns the uri elements of an XML sitemap.
452
 *
453 454
 * @param \Drupal\xmlsitemap\XmlSitemapInterface $sitemap
 *   The sitemap represented by and XmlSitemapInterface object.
455 456
 *
 * @return array
457 458
 *   An array containing the 'path' and 'options' keys used to build the uri of
 *   the XML sitemap, and matching the signature of url().
Darren Oh's avatar
Darren Oh committed
459
 */
460
function xmlsitemap_sitemap_uri(XmlSitemapInterface $sitemap) {
461
  $uri['path'] = 'sitemap.xml';
462
  $uri['options'] = \Drupal::moduleHandler()->invokeAll('xmlsitemap_context_url_options', [$sitemap->context]);
463
  $context = $sitemap->context;
464
  \Drupal::moduleHandler()->alter('xmlsitemap_context_url_options', $uri['options'], $context);
465
  $uri['options'] += [
466
    'absolute' => TRUE,
467
    'base_url' => Settings::get('xmlsitemap_base_url', \Drupal::state()->get('xmlsitemap_base_url')),
468
  ];
469 470
  return $uri;
}
471

Darren Oh's avatar
Darren Oh committed
472
/**
473
 * @} End of "defgroup xmlsitemap_api"
474
 */
475
function xmlsitemap_get_directory(XmlSitemapInterface $sitemap = NULL) {
476 477
  $directory = &drupal_static(__FUNCTION__);
  if (!isset($directory)) {
478
    $directory = \Drupal::config('xmlsitemap.settings')->get('path') ?: 'xmlsitemap';
Darren Oh's avatar
Darren Oh committed
479
  }
480

481
  if ($sitemap != NULL && !empty($sitemap->id)) {
482
    return file_build_uri($directory . '/' . $sitemap->id);
483 484 485 486
  }
  else {
    return file_build_uri($directory);
  }
Darren Oh's avatar
Darren Oh committed
487 488 489
}

/**
490
 * Check that the sitemap files directory exists and is writable.
Darren Oh's avatar
Darren Oh committed
491
 */
492
function xmlsitemap_check_directory(XmlSitemapInterface $sitemap = NULL) {
493
  $directory = xmlsitemap_get_directory($sitemap);
494 495
  /** @var \Drupal\Core\File\FileSystemInterface $filesystem */
  $filesystem = \Drupal::service('file_system');
496
  $result = $filesystem->prepareDirectory($directory, FileSystemInterface::CREATE_DIRECTORY | FileSystemInterface::MODIFY_PERMISSIONS);
497
  if (!$result) {
498
    \Drupal::logger('file system')->error('The directory %directory does not exist or is not writable.', ['%directory' => $directory]);
499 500 501 502
  }
  return $result;
}

503 504 505
/**
 * Check all directories.
 */
506
function xmlsitemap_check_all_directories() {
507
  $directories = [];
508 509

  $sitemaps = xmlsitemap_sitemap_load_multiple(FALSE);
510
  foreach ($sitemaps as $sitemap) {
511 512 513 514
    $directory = xmlsitemap_get_directory($sitemap);
    $directories[$directory] = $directory;
  }

515 516 517
  /** @var \Drupal\Core\File\FileSystemInterface $filesystem */
  $filesystem = \Drupal::service('file_system');

518
  foreach ($directories as $directory) {
519
    $result = $filesystem->prepareDirectory($directory, $filesystem::CREATE_DIRECTORY | $filesystem::MODIFY_PERMISSIONS);
520 521 522 523 524 525 526 527 528 529 530
    if ($result) {
      $directories[$directory] = TRUE;
    }
    else {
      $directories[$directory] = FALSE;
    }
  }

  return $directories;
}

531 532 533 534 535 536 537 538 539 540 541
/**
 * Clears sitemap directory.
 *
 * @param \Drupal\xmlsitemap\XmlSitemapInterface $sitemap
 *   Sitemap entity.
 * @param bool $delete
 *   If TRUE, delete the path directory afterwards.
 *
 * @return bool
 *   Returns TRUE is operation was successful, FALSE otherwise.
 */
542
function xmlsitemap_clear_directory(XmlSitemapInterface $sitemap = NULL, $delete = FALSE) {
543 544
  $directory = xmlsitemap_get_directory($sitemap);
  return _xmlsitemap_delete_recursive($directory, $delete);
545 546
}

547 548 549
/**
 * Move a directory to a new location.
 *
550
 * @param string $old_dir
551
 *   A string specifying the filepath or URI of the original directory.
552
 * @param string $new_dir
553
 *   A string specifying the filepath or URI of the new directory.
554 555
 * @param int $replace
 *   Behavior when the destination file already exists.
556 557
 *   Replace behavior when the destination file already exists.
 *
558
 * @return bool
559 560
 *   TRUE if the directory was moved successfully. FALSE otherwise.
 */
561 562 563 564 565
function xmlsitemap_directory_move($old_dir, $new_dir, $replace = FileSystemInterface::EXISTS_REPLACE) {
  /** @var \Drupal\Core\File\FileSystemInterface $filesystem */
  $filesystem = \Drupal::service('file_system');

  $success = $filesystem->prepareDirectory($new_dir, $filesystem::CREATE_DIRECTORY | $filesystem::MODIFY_PERMISSIONS);
566

567 568
  $old_path = $filesystem->realpath($old_dir);
  $new_path = $filesystem->realpath($new_dir);
569 570 571 572
  if (!is_dir($old_path) || !is_dir($new_path) || !$success) {
    return FALSE;
  }

573
  $files = $filesystem->scanDirectory($old_dir, '/.*/');
574 575
  foreach ($files as $file) {
    $file->uri_new = $new_dir . '/' . basename($file->filename);
576
    $success &= (bool) $filesystem->move($file->uri, $file->uri_new, $replace);
577 578 579
  }

  // The remove the directory.
580
  $success &= $filesystem->rmdir($old_dir);
581 582 583
  return $success;
}

584 585 586
/**
 * Recursively delete all files and folders in the specified filepath.
 *
587
 * This is a backport of Drupal 8's file_unmanaged_delete_recursive().
588 589 590
 *
 * Note that this only deletes visible files with write permission.
 *
591
 * @param string $path
592
 *   A filepath relative to the Drupal root directory.
593
 * @param bool $delete_root
594
 *   A boolean if TRUE will delete the $path directory afterwards.
595 596 597
 *
 * @return bool
 *   TRUE if operation was successful, FALSE otherwise.
598 599
 */
function _xmlsitemap_delete_recursive($path, $delete_root = FALSE) {
600 601 602
  /** @var \Drupal\Core\File\FileSystemInterface $filesystem */
  $filesystem = \Drupal::service('file_system');

603
  // Resolve streamwrapper URI to local path.
604
  $path = $filesystem->realpath($path);
605 606 607
  if (is_dir($path)) {
    $dir = dir($path);
    while (($entry = $dir->read()) !== FALSE) {
608
      if ($entry === '.' || $entry === '..') {
609 610 611
        continue;
      }
      $entry_path = $path . '/' . $entry;
612
      $filesystem->deleteRecursive($entry_path);
613 614
    }
    $dir->close();
615
    return $delete_root ? $filesystem->rmdir($path) : TRUE;
616
  }
617
  return $filesystem->delete($path);
Darren Oh's avatar
Darren Oh committed
618 619
}

620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687
/**
 * Implements hook_entity_type_build().
 */
function xmlsitemap_entity_type_build(array &$entity_types) {
  // Mark some specific core entity types as not supported by XML sitemap.
  // If a site wants to undo this, they may use hook_entity_type_alter().
  $unsupported_types = [
    // Custom blocks.
    'block_content',
    // Comments.
    'comment',
    // Shortcut items.
    'shortcut',
    // Custom Token module.
    // @see https://www.drupal.org/project/token_custom/issues/3150038
    'token_custom',
  ];

  /** @var $entity_types \Drupal\Core\Entity\EntityTypeInterface[] */
  foreach ($unsupported_types as $entity_type_id) {
    if (isset($entity_types[$entity_type_id])) {
      $entity_types[$entity_type_id]->set('xmlsitemap', FALSE);
    }
  }
}

/**
 * Determines if an entity type can be listed in the XML sitemap as links.
 *
 * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
 *   The entity type.
 *
 * @return bool
 *   TRUE if the entity type can be used, or FALSE otherwise.
 */
function xmlsitemap_is_entity_type_supported(EntityTypeInterface $entity_type) {
  // If the XML sitemap status in the entity type annotation has been set then
  // return that first. This will allow modules to bypass the logic below if
  // needed.
  $status = $entity_type->get('xmlsitemap');
  if ($status !== NULL) {
    return $status;
  }

  // Skip if the entity type is not a content entity type.
  if (!($entity_type instanceof ContentEntityTypeInterface)) {
    return FALSE;
  }

  // Skip if the entity type is internal (and not considered public).
  if ($entity_type->isInternal()) {
    return FALSE;
  }

  // Skip if the entity type does not have a canonical URL.
  if (!$entity_type->hasLinkTemplate('canonical') && !$entity_type->getUriCallback()) {
    return FALSE;
  }

  // Skip if the entity type as a bundle entity type but does not yet have
  // any bundles created.
  if ($entity_type->getBundleEntityType() && !\Drupal::service('entity_type.bundle.info')->getBundleInfo($entity_type->id())) {
    return FALSE;
  }

  return TRUE;
}

Darren Oh's avatar
Darren Oh committed
688
/**
689 690
 * Returns information about supported sitemap link types.
 *
691
 * @param mixed $type
692 693
 *   (optional) The link type to return information for. If omitted,
 *   information for all link types is returned.
694
 * @param mixed $reset
695 696 697
 *   (optional) Boolean whether to reset the static cache and do nothing. Only
 *   used for tests.
 *
698 699 700
 * @return array
 *   Info about sitemap link.
 *
701 702
 * @see hook_xmlsitemap_link_info()
 * @see hook_xmlsitemap_link_info_alter()
Darren Oh's avatar
Darren Oh committed
703
 */
704
function xmlsitemap_get_link_info($type = NULL, $reset = FALSE) {
705
  $language = \Drupal::languageManager()->getCurrentLanguage();
706
  $link_info = &drupal_static(__FUNCTION__);
707

708 709
  if ($reset) {
    $link_info = NULL;
710
    \Drupal::service('cache_tags.invalidator')->invalidateTags(['xmlsitemap']);
711 712
  }

713
  if (!isset($link_info)) {
714
    $cid = 'xmlsitemap:link_info:' . $language->getId();
715
    if ($cache = \Drupal::cache()->get($cid)) {
716
      $link_info = $cache->data;
717
    }
718
    else {
719
      $link_info = [];
720
      $entity_types = \Drupal::entityTypeManager()->getDefinitions();
721

722
      foreach ($entity_types as $key => $entity_type) {
723 724 725 726
        if (!xmlsitemap_is_entity_type_supported($entity_type)) {
          continue;
        }

727
        $link_info[$key] = [
728 729 730
          'label' => $entity_type->getLabel(),
          'type' => $entity_type->id(),
          'base table' => $entity_type->getBaseTable(),
731
          'bundles' => \Drupal::service('entity_type.bundle.info')->getBundleInfo($entity_type->id()),
732 733 734 735 736
          'bundle label' => $entity_type->getBundleLabel(),
          'entity keys' => [
            'id' => $entity_type->getKey('id'),
            'bundle' => $entity_type->getKey('bundle'),
          ],
737 738 739 740 741
          'xmlsitemap' => [
            // Add in the default callbacks for entity types.
            'process callback' => $entity_type->get('xmlsitemap')['process callback'] ?? 'xmlsitemap_xmlsitemap_process_entity_links',
            'rebuild callback' => $entity_type->get('xmlsitemap')['process callback'] ?? 'xmlsitemap_rebuild_batch_fetch',
          ],
742
        ];
743
      }
744

745
      $link_info = array_merge($link_info, \Drupal::moduleHandler()->invokeAll('xmlsitemap_link_info'));
746
      foreach ($link_info as $key => &$info) {
747
        $info += [
748 749
          'type' => $key,
          'base table' => FALSE,
750 751
          'bundles' => [],
        ];
752
        foreach ($info['bundles'] as $bundle => &$bundle_info) {
753 754 755
          $bundle_info += [
            'xmlsitemap' => [],
          ];
756 757
          $bundle_info['xmlsitemap'] += xmlsitemap_link_bundle_load($key, $bundle, FALSE);
        }
758
      }
759
      \Drupal::moduleHandler()->alter('xmlsitemap_link_info', $link_info);
760 761 762 763 764 765 766 767 768 769 770 771 772

      // Sort the entity types by label.
      uasort($link_info, function ($a, $b) {
        // Put frontpage first.
        if ($a['type'] === 'frontpage') {
          return -1;
        }
        if ($b['type'] === 'frontpage') {
          return 1;
        }
        return strnatcmp($a['label'], $b['label']);
      });

773
      // Cache by language since this info contains translated strings.
774 775 776 777 778 779 780 781 782 783 784 785
      // Also include entity type tags since this is tied to entity and bundle
      // information.
      \Drupal::cache()->set(
        $cid,
        $link_info,
        Cache::PERMANENT,
        [
          'xmlsitemap',
          'entity_types',
          'entity_bundles',
        ]
      );
786
    }
Darren Oh's avatar
Darren Oh committed
787
  }
788 789 790 791 792 793

  if (isset($type)) {
    return isset($link_info[$type]) ? $link_info[$type] : NULL;
  }

  return $link_info;
Darren Oh's avatar
Darren Oh committed
794 795
}

796 797 798 799 800 801 802 803 804
/**
 * Returns enabled bundles of an entity type.
 *
 * @param string $entity_type
 *   Entity type id.
 *
 * @return array
 *   Array with entity bundles info.
 */
805
function xmlsitemap_get_link_type_enabled_bundles($entity_type) {
806
  $bundles = [];
807 808
  $info = xmlsitemap_get_link_info($entity_type);
  foreach ($info['bundles'] as $bundle => $bundle_info) {
809
    $settings = xmlsitemap_link_bundle_load($entity_type, $bundle);
810
    if (!empty($settings['status'])) {
811 812 813 814 815 816
      $bundles[] = $bundle;
    }
  }
  return $bundles;
}

817 818 819
/**
 * Returns statistics about specific entity links.
 *
820
 * @param string $entity_type_id
821 822
 *   Entity type id.
 * @param string $bundle
823
 *   Bundle id.
824 825 826 827
 *
 * @return array
 *   Array with statistics.
 */
828 829
function xmlsitemap_get_link_type_indexed_status($entity_type_id, $bundle = '') {
  $info = xmlsitemap_get_link_info($entity_type_id);
830
  $database = \Drupal::database();
831
  $entity_type = \Drupal::entityTypeManager()->getDefinition($entity_type_id);
832

833 834
  $status['indexed'] = $database->query("SELECT COUNT(id) FROM {xmlsitemap} WHERE type = :entity AND subtype = :bundle", [':entity' => $entity_type_id, ':bundle' => $bundle])->fetchField();
  $status['visible'] = $database->query("SELECT COUNT(id) FROM {xmlsitemap} WHERE type = :entity AND subtype = :bundle AND status = 1 AND access = 1", [':entity' => $entity_type_id, ':bundle' => $bundle])->fetchField();