xmlsitemap.module 82.2 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\Query\QueryInterface;
24
use Drupal\Core\File\Exception\FileException;
25
use Drupal\Core\File\FileSystemInterface;
26
use Drupal\Core\Form\FormStateInterface;
27
use Drupal\Core\Language\LanguageInterface;
28 29
use Drupal\Core\Link;
use Drupal\Core\Routing\RouteMatchInterface;
30
use Drupal\Core\Site\Settings;
31
use Drupal\Core\Url;
32 33
use Drupal\xmlsitemap\Entity\XmlSitemap;
use Drupal\xmlsitemap\XmlSitemapInterface;
34
use Symfony\Component\HttpFoundation\Request;
35
use Symfony\Component\HttpFoundation\Response;
36
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
37

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

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

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

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

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

/**
 * Long lastmod timestamp format.
 */
76
const XMLSITEMAP_LASTMOD_LONG = 'c';
77

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

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

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

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

119 120 121
  return $hooks;
}

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

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

136
    case 'xmlsitemap.admin_search':
137
      break;
138

139
    case 'xmlsitemap.admin_search_list':
140
      break;
141

142
    case 'xmlsitemap.admin_rebuild':
143 144 145
      $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>';
  }

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

  return $output;
}

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

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

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

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

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

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

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

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

231
/**
232
 * Internal default variables config for xmlsitemap_var().
233 234 235
 *
 * @return array
 *   Array with config variables of xmlsitemap.settings config object.
236
 */
237
function xmlsitemap_config_variables() {
238
  return [
239
    'minimum_lifetime' => 0,
240 241 242 243 244 245 246 247 248
    '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,
249
    'disable_cron_regeneration' => FALSE,
250
  ];
251 252
}

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

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

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

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

Darren Oh's avatar
Darren Oh committed
292
/**
293
 * @defgroup xmlsitemap_api XML sitemap API.
294
 * @{
295 296
 * 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
297
 */
298 299 300 301

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

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

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

339
  return $sitemaps;
340 341
}

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

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

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

  return $sitemap;
374 375 376
}

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

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

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

417 418 419
/**
 * Find the maximum file size of all a sitemap's XML files.
 *
420 421
 * @param \Drupal\xmlsitemap\XmlSitemapInterface $sitemap
 *   The XML sitemap object.
422 423 424
 *
 * @return int
 *   Maximum file size in the directory.
425
 */
426
function xmlsitemap_sitemap_get_max_filesize(XmlSitemapInterface $sitemap) {
427
  $dir = xmlsitemap_get_directory($sitemap);
428
  $sitemap->setMaxFileSize(0);
429 430
  /** @var \Drupal\Core\File\FileSystemInterface $file_system */
  $file_system = \Drupal::service('file_system');
431 432 433 434 435 436 437 438
  try {
    $files = $file_system->scanDirectory($dir, '/\.xml$/');
  }
  catch (FileException $e) {
    // Ignore and return empty array for BC.
    $files = [];
  }
  foreach ($files as $file) {
439
    $sitemap->setMaxFileSize(max($sitemap->getMaxFileSize(), filesize($file->uri)));
440
  }
441
  return $sitemap->getMaxFileSize();
442 443
}

444 445 446 447 448
/**
 * Returns the hash string for a context.
 *
 * @param array $context
 *   Context to be hashed.
449
 *
450 451 452
 * @return string
 *   Hash string for the context.
 */
453
function xmlsitemap_sitemap_get_context_hash(array &$context) {
454
  ksort($context);
455
  return Crypt::hashBase64(serialize($context));
456 457
}

Darren Oh's avatar
Darren Oh committed
458
/**
459
 * Returns the uri elements of an XML sitemap.
460
 *
461 462
 * @param \Drupal\xmlsitemap\XmlSitemapInterface $sitemap
 *   The sitemap represented by and XmlSitemapInterface object.
463 464
 *
 * @return array
465 466
 *   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
467
 */
468
function xmlsitemap_sitemap_uri(XmlSitemapInterface $sitemap) {
469
  $uri['path'] = 'sitemap.xml';
470
  $uri['options'] = \Drupal::moduleHandler()->invokeAll('xmlsitemap_context_url_options', [$sitemap->context]);
471
  $context = $sitemap->context;
472
  \Drupal::moduleHandler()->alter('xmlsitemap_context_url_options', $uri['options'], $context);
473
  $uri['options'] += [
474
    'absolute' => TRUE,
475
    'base_url' => Settings::get('xmlsitemap_base_url', \Drupal::state()->get('xmlsitemap_base_url')),
476
  ];
477 478
  return $uri;
}
479

Darren Oh's avatar
Darren Oh committed
480
/**
481
 * @} End of "defgroup xmlsitemap_api"
482
 */
483
function xmlsitemap_get_directory(XmlSitemapInterface $sitemap = NULL) {
484 485
  $directory = &drupal_static(__FUNCTION__);
  if (!isset($directory)) {
486
    $directory = \Drupal::config('xmlsitemap.settings')->get('path');
Darren Oh's avatar
Darren Oh committed
487
  }
488

489 490 491 492
  if (empty($directory)) {
    return FALSE;
  }
  elseif ($sitemap != NULL && !empty($sitemap->id)) {
493
    return file_build_uri($directory . '/' . $sitemap->id);
494 495 496 497
  }
  else {
    return file_build_uri($directory);
  }
Darren Oh's avatar
Darren Oh committed
498 499 500
}

/**
501
 * Check that the sitemap files directory exists and is writable.
Darren Oh's avatar
Darren Oh committed
502
 */
503
function xmlsitemap_check_directory(XmlSitemapInterface $sitemap = NULL) {
504
  $directory = xmlsitemap_get_directory($sitemap);
505 506 507
  /** @var \Drupal\Core\File\FileSystemInterface $filesystem */
  $filesystem = \Drupal::service('file_system');
  $result = $filesystem->prepareDirectory($directory, $filesystem::CREATE_DIRECTORY | $filesystem::MODIFY_PERMISSIONS);
508
  if (!$result) {
509
    \Drupal::logger('file system')->error('The directory %directory does not exist or is not writable.', ['%directory' => $directory]);
510 511 512 513
  }
  return $result;
}

514 515 516
/**
 * Check all directories.
 */
517
function xmlsitemap_check_all_directories() {
518
  $directories = [];
519 520

  $sitemaps = xmlsitemap_sitemap_load_multiple(FALSE);
521
  foreach ($sitemaps as $sitemap) {
522 523 524 525
    $directory = xmlsitemap_get_directory($sitemap);
    $directories[$directory] = $directory;
  }

526 527 528
  /** @var \Drupal\Core\File\FileSystemInterface $filesystem */
  $filesystem = \Drupal::service('file_system');

529
  foreach ($directories as $directory) {
530
    $result = $filesystem->prepareDirectory($directory, $filesystem::CREATE_DIRECTORY | $filesystem::MODIFY_PERMISSIONS);
531 532 533 534 535 536 537 538 539 540 541
    if ($result) {
      $directories[$directory] = TRUE;
    }
    else {
      $directories[$directory] = FALSE;
    }
  }

  return $directories;
}

542 543 544 545 546 547 548 549 550 551 552
/**
 * 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.
 */
553
function xmlsitemap_clear_directory(XmlSitemapInterface $sitemap = NULL, $delete = FALSE) {
554 555 556 557 558 559
  if ($directory = xmlsitemap_get_directory($sitemap)) {
    return _xmlsitemap_delete_recursive($directory, $delete);
  }
  else {
    return FALSE;
  }
560 561
}

562 563 564
/**
 * Move a directory to a new location.
 *
565
 * @param string $old_dir
566
 *   A string specifying the filepath or URI of the original directory.
567
 * @param string $new_dir
568
 *   A string specifying the filepath or URI of the new directory.
569 570
 * @param int $replace
 *   Behavior when the destination file already exists.
571 572
 *   Replace behavior when the destination file already exists.
 *
573
 * @return bool
574 575
 *   TRUE if the directory was moved successfully. FALSE otherwise.
 */
576 577 578 579 580
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);
581

582 583
  $old_path = $filesystem->realpath($old_dir);
  $new_path = $filesystem->realpath($new_dir);
584 585 586 587
  if (!is_dir($old_path) || !is_dir($new_path) || !$success) {
    return FALSE;
  }

588
  $files = $filesystem->scanDirectory($old_dir, '/.*/');
589 590
  foreach ($files as $file) {
    $file->uri_new = $new_dir . '/' . basename($file->filename);
591
    $success &= (bool) $filesystem->move($file->uri, $file->uri_new, $replace);
592 593 594
  }

  // The remove the directory.
595
  $success &= $filesystem->rmdir($old_dir);
596 597 598
  return $success;
}

599 600 601
/**
 * Recursively delete all files and folders in the specified filepath.
 *
602
 * This is a backport of Drupal 8's file_unmanaged_delete_recursive().
603 604 605
 *
 * Note that this only deletes visible files with write permission.
 *
606
 * @param string $path
607
 *   A filepath relative to the Drupal root directory.
608
 * @param bool $delete_root
609
 *   A boolean if TRUE will delete the $path directory afterwards.
610 611 612
 *
 * @return bool
 *   TRUE if operation was successful, FALSE otherwise.
613 614
 */
function _xmlsitemap_delete_recursive($path, $delete_root = FALSE) {
615 616 617
  /** @var \Drupal\Core\File\FileSystemInterface $filesystem */
  $filesystem = \Drupal::service('file_system');

618
  // Resolve streamwrapper URI to local path.
619
  $path = $filesystem->realpath($path);
620 621 622 623 624 625 626
  if (is_dir($path)) {
    $dir = dir($path);
    while (($entry = $dir->read()) !== FALSE) {
      if ($entry == '.' || $entry == '..') {
        continue;
      }
      $entry_path = $path . '/' . $entry;
627
      $filesystem->deleteRecursive($entry_path);
628 629
    }
    $dir->close();
630
    return $delete_root ? $filesystem->rmdir($path) : TRUE;
631
  }
632
  return $filesystem->delete($path);
Darren Oh's avatar
Darren Oh committed
633 634 635
}

/**
636 637
 * Returns information about supported sitemap link types.
 *
638
 * @param mixed $type
639 640
 *   (optional) The link type to return information for. If omitted,
 *   information for all link types is returned.
641
 * @param mixed $reset
642 643 644
 *   (optional) Boolean whether to reset the static cache and do nothing. Only
 *   used for tests.
 *
645 646 647
 * @return array
 *   Info about sitemap link.
 *
648 649
 * @see hook_xmlsitemap_link_info()
 * @see hook_xmlsitemap_link_info_alter()
Darren Oh's avatar
Darren Oh committed
650
 */
651
function xmlsitemap_get_link_info($type = NULL, $reset = FALSE) {
652
  $language = \Drupal::languageManager()->getCurrentLanguage();
653
  $link_info = &drupal_static(__FUNCTION__);
654

655 656
  if ($reset) {
    $link_info = NULL;
657 658 659
    foreach (\Drupal::languageManager()->getLanguages() as $lang) {
      \Drupal::cache()->delete('xmlsitemap:link_info:' . $lang->getId());
    }
660 661
  }

662
  if (!isset($link_info)) {
663
    $cid = 'xmlsitemap:link_info:' . $language->getId();
664
    if ($cache = \Drupal::cache()->get($cid)) {
665
      $link_info = $cache->data;
666
    }
667
    else {
668
      $link_info = [];
669
      $entity_types = \Drupal::entityTypeManager()->getDefinitions();
670
      foreach ($entity_types as $key => $entity_type) {
671
        $link_info[$key] = [
672 673 674
          'label' => $entity_type->getLabel(),
          'type' => $entity_type->id(),
          'base table' => $entity_type->getBaseTable(),
675
          'bundles' => \Drupal::service('entity_type.bundle.info')->getBundleInfo($entity_type->id()),
676
        ];
677 678
        $uri_callback = $entity_type->getUriCallback();
        if (empty($uri_callback) || !isset($entity_type->xmlsitemap)) {
679
          // Remove any non URL-able or XML sitemap un-supported entities.
680
        }
681
        foreach (\Drupal::service('entity_type.bundle.info')->getBundleInfo($entity_type->id()) as $bundle) {
682 683 684 685
          if (!isset($bundle['xmlsitemap'])) {
            // Remove any un-supported entity bundles.
          }
        }
686
      }
687
      $link_info = array_merge($link_info, \Drupal::moduleHandler()->invokeAll('xmlsitemap_link_info'));
688
      foreach ($link_info as $key => &$info) {
689
        $info += [
690 691
          'type' => $key,
          'base table' => FALSE,
692
          'bundles' => [],
693 694 695
          'xmlsitemap' => [
            'process callback' => 'xmlsitemap_xmlsitemap_process_entity_links',
          ],
696
        ];
697
        if (!isset($info['xmlsitemap']['rebuild callback']) && !empty($info['base table']) && $entity_types[$key]->getKey('id')) {
698 699 700
          $info['xmlsitemap']['rebuild callback'] = 'xmlsitemap_rebuild_batch_fetch';
        }
        foreach ($info['bundles'] as $bundle => &$bundle_info) {
701 702 703
          $bundle_info += [
            'xmlsitemap' => [],
          ];
704 705
          $bundle_info['xmlsitemap'] += xmlsitemap_link_bundle_load($key, $bundle, FALSE);
        }
706
      }
707
      \Drupal::moduleHandler()->alter('xmlsitemap_link_info', $link_info);
708 709
      ksort($link_info);
      // Cache by language since this info contains translated strings.
710
      \Drupal::cache()->set($cid, $link_info, Cache::PERMANENT, ['xmlsitemap']);
711
    }
Darren Oh's avatar
Darren Oh committed
712
  }
713 714 715 716 717 718

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

  return $link_info;
Darren Oh's avatar
Darren Oh committed
719 720
}

721 722 723 724 725 726 727 728 729
/**
 * Returns enabled bundles of an entity type.
 *
 * @param string $entity_type
 *   Entity type id.
 *
 * @return array
 *   Array with entity bundles info.
 */
730
function xmlsitemap_get_link_type_enabled_bundles($entity_type) {
731
  $bundles = [];
732 733
  $info = xmlsitemap_get_link_info($entity_type);
  foreach ($info['bundles'] as $bundle => $bundle_info) {
734
    $settings = xmlsitemap_link_bundle_load($entity_type, $bundle);
735
    if (!empty($settings['status'])) {
736 737 738 739 740 741
      $bundles[] = $bundle;
    }
  }
  return $bundles;
}

742 743 744
/**
 * Returns statistics about specific entity links.
 *
745
 * @param string $entity_type_id
746 747
 *   Entity type id.
 * @param string $bundle
748
 *   Bundle id.
749 750 751 752
 *
 * @return array
 *   Array with statistics.
 */
753 754
function xmlsitemap_get_link_type_indexed_status($entity_type_id, $bundle = '') {
  $info = xmlsitemap_get_link_info($entity_type_id);
755
  $database = \Drupal::database();
756
  $entity_type = \Drupal::entityTypeManager()->getDefinition($entity_type_id);
757

758 759
  $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();
760

761
  try {
762 763 764 765 766 767 768 769 770 771 772 773 774
    $query = \Drupal::entityQuery($entity_type_id);

    if ($bundle) {
      $query->condition($entity_type->getKey('bundle'), $bundle, '=');
    }

    $query->addTag('xmlsitemap_link_bundle_access');
    $query->addTag('xmlsitemap_link_indexed_status');
    $query->addMetaData('entity_type_id', $entity_type_id);
    $query->addMetaData('bundle', $bundle);
    $query->addMetaData('entity_info', $info);
    $query->count();
    $status['total'] = $query->execute();
775 776
    return $status;
  }
777
  catch (\Exception $e) {
778 779
    $status['total'] = 0;
  }
780 781
  return $status;
}
782

783 784 785 786 787 788 789 790 791 792 793 794
/**
 * Saves xmlsitemap settings for a specific bundle.
 *
 * @param string $entity
 *   Entity type id.
 * @param string $bundle
 *   Bundle id.
 * @param array $settings
 *   Settings to be saved.
 * @param bool $update_links
 *   Update bundle links after settings are saved.
 */
795 796
function xmlsitemap_link_bundle_settings_save($entity, $bundle, array $settings, $update_links = TRUE) {
  if ($update_links) {
797
    $old_settings = xmlsitemap_link_bundle_load($entity, $bundle);
798
    if ($settings['status'] != $old_settings['status']) {
799 800 801 802 803
      \Drupal::service('xmlsitemap.link_storage')->updateMultiple(['status' => $settings['status']], [
        'type' => $entity,
        'subtype' => $bundle,
        'status_override' => 0,
      ]);
804 805
    }
    if ($settings['priority'] != $old_settings['priority']) {
806 807 808 809 810
      \Drupal::service('xmlsitemap.link_storage')->updateMultiple(['priority' => $settings['priority']], [
        'type' => $entity,
        'subtype' => $bundle,
        'priority_override' => 0,
      ]);
811 812 813
    }
  }

814
  foreach ($settings as $key => $value) {
815
    \Drupal::configFactory()->getEditable("xmlsitemap.settings.{$entity}.{$bundle}")->set($key, $value)->save();
816 817
  }

818
  foreach (\Drupal::languageManager()->getLanguages() as $lang) {
819
    \Drupal::cache()->delete('xmlsitemap:link_info:' . $lang->getId());
820 821
  }
  xmlsitemap_get_link_info(NULL, TRUE);
822 823
}

824 825 826 827 828 829 830 831 832 833
/**
 * Renames a bundle.
 *
 * @param string $entity
 *   Entity type id.
 * @param string $bundle_old
 *   Old bundle name.
 * @param string $bundle_new
 *   New bundle name.
 */
834 835
function xmlsitemap_link_bundle_rename($entity, $bundle_old, $bundle_new) {
  if ($bundle_old != $bundle_new) {
836 837
    if (!\Drupal::config("xmlsitemap.settings.{$entity}.{$bundle_old}")->isNew()) {
      $settings = xmlsitemap_link_bundle_load($entity, $bundle_old);
838
      \Drupal::configFactory()->getEditable("xmlsitemap.settings.{$entity}.{$bundle_old}")->delete();
839
      xmlsitemap_link_bundle_settings_save($entity, $bundle_new, $settings, FALSE);
840
      \Drupal::service('xmlsitemap.link_storage')->updateMultiple(['subtype' => $bundle_new], ['type' => $entity, 'subtype' => $bundle_old]);
841
    }
842 843 844
  }
}

845 846 847 848 849 850 851 852 853
/**
 * Loads link bundle info.
 *
 * @param string $entity
 *   Entity type id.
 * @param string $bundle
 *   Bundle info.
 * @param bool $load_bundle_info
 *   If TRUE, loads bundle info.
854
 *
855 856 857
 * @return array
 *   Info about a bundle.
 */
858
function xmlsitemap_link_bundle_load($entity, $bundle, $load_bundle_info = TRUE) {
859
  $info = [
860 861
    'entity' => $entity,
    'bundle' => $bundle,
862
  ];
863 864
  if ($load_bundle_info) {
    $entity_info = xmlsitemap_get_link_info($entity);
865 866 867
    if (isset($entity_info['bundles'][$bundle])) {
      $info['info'] = $entity_info['bundles'][$bundle];
    }
868
  }
869
  $bundle_settings = \Drupal::config("xmlsitemap.settings.{$entity}.{$bundle}")->get();
870 871 872
  if ($bundle_settings) {
    $info += $bundle_settings;
  }
873
  $info += [
874 875
    'status' => XMLSITEMAP_STATUS_DEFAULT,
    'priority' => XMLSITEMAP_PRIORITY_DEFAULT,
876
    'changefreq' => 0,
877
  ];
878
  return $info;
879 880
}

881 882 883 884 885 886 887 888 889 890
/**
 * Deletes all links of a specific bundle.
 *
 * @param string $entity
 *   Entity type id.
 * @param string $bundle
 *   Bundle id.
 * @param bool $delete_links
 *   If TRUE, deletes bundle links from {xmlsitemap} table.
 */
891
function xmlsitemap_link_bundle_delete($entity, $bundle, $delete_links = TR