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

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

/**
8 9 10
 * @file
 * Main file for the xmlsitemap module.
 */
11
use Drupal\Component\Utility\Crypt;
12
use Drupal\Core\Language\LanguageInterface;
13
use Drupal\xmlsitemap\XmlSitemapInterface;
14 15
use Drupal\Component\Utility\String;
use Drupal\Core\Render\Element;
16
use Drupal\Core\Entity\EntityForm;
17
use Drupal\Core\Entity\EntityInterface;
18
use Drupal\Core\Session\AnonymousUserSession;
19
use Drupal\Core\Routing\RouteMatchInterface;
20
use Drupal\Core\Database\Query\AlterableInterface;
21
use Drupal\Core\Entity\Query\QueryInterface;
22
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
23
use Symfony\Component\HttpFoundation\Response;
24 25 26 27

/**
 * The maximum number of links in one sitemap chunk file.
 */
28
const XMLSITEMAP_MAX_SITEMAP_LINKS = 50000;
29

30 31 32
/**
 * The maximum filesize of a sitemap chunk file.
 */
33
const XMLSITEMAP_MAX_SITEMAP_FILESIZE = 10485760;
34

35 36 37 38 39 40 41 42 43
/**
 * Xmlsitemap Frequencies
 */
const XMLSITEMAP_FREQUENCY_YEARLY = 31449600; // 60 * 60 * 24 * 7 * 52
const XMLSITEMAP_FREQUENCY_MONTHLY = 2419200; // 60 * 60 * 24 * 7 * 4
const XMLSITEMAP_FREQUENCY_WEEKLY = 604800; // 60 * 60 * 24 * 7
const XMLSITEMAP_FREQUENCY_DAILY = 86400; // 60 * 60 * 24
const XMLSITEMAP_FREQUENCY_HOURLY = 3600; // 60 * 60
const XMLSITEMAP_FREQUENCY_ALWAYS = 60;
44

45 46 47
/**
 * Short lastmod timestamp format.
 */
48
const XMLSITEMAP_LASTMOD_SHORT = 'Y-m-d';
49 50 51 52

/**
 * Medium lastmod timestamp format.
 */
53
const XMLSITEMAP_LASTMOD_MEDIUM = 'Y-m-d\TH:i\Z';
54 55 56 57

/**
 * Long lastmod timestamp format.
 */
58
const XMLSITEMAP_LASTMOD_LONG = 'c';
59

60 61 62
/**
 * The default inclusion status for link types in the sitemaps.
 */
63
const XMLSITEMAP_STATUS_DEFAULT = 0;
64 65 66 67

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

70 71 72 73
/**
 * Implements hook_hook_info().
 */
function xmlsitemap_hook_info() {
74 75 76 77 78 79 80
  $hooks = array(
    'xmlsitemap_link_info',
    'xmlsitemap_link_info_alter',
    'xmlsitemap_link_alter',
    'xmlsitemap_index_links',
    'xmlsitemap_context_info',
    'xmlsitemap_context_info_alter',
81
    'xmlsitemap_context_url_options',
82
    'xmlsitemap_context',
83 84
    'xmlsitemap_sitemap_insert',
    'xmlsitemap_sitemap_update',
85 86 87 88
    'xmlsitemap_sitemap_operations',
    'xmlsitemap_sitemap_delete',
    'xmlsitemap_sitemap_link_url_options_alter',
    'query_xmlsitemap_generate_alter',
89
    'query_xmlsitemap_link_bundle_access_alter',
90
    'form_xmlsitemap_sitemap_edit_form_alter',
91
  );
92 93 94 95 96

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

98 99 100
  return $hooks;
}

101 102
/**
 * Implements hook_help().
Darren Oh's avatar
Darren Oh committed
103
 */
104
function xmlsitemap_help($route_name, RouteMatchInterface $route_match) {
105 106
  $output = '';

107
  switch ($route_name) {
108
    case 'help.page.xmlsitemap':
109 110 111 112
    case 'xmlsitemap.admin_settings':
    case 'xmlsitemap.entities_settings':
    case 'xmlsitemap.admin_edit':
    case 'xmlsitemap.admin_delete':
113
      return;
114
    case 'xmlsitemap.admin_search':
115
      break;
116
    case 'xmlsitemap.admin_search_list':
117
      break;
118
    case 'xmlsitemap.admin_rebuild':
119 120 121
      $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>';
  }

122
  $currentUser = \Drupal::currentUser();
123
  if (strpos($route_name, 'xmlsitemap') !== FALSE && $currentUser->hasPermission('administer xmlsitemap')) {
124
    // Alert the user to any potential problems detected by hook_requirements.
125
    $output .= _xmlsitemap_get_blurb();
Darren Oh's avatar
Darren Oh committed
126
  }
127 128 129 130 131

  return $output;
}

/**
132
 * Implements hook_permission().
133
 */
Dave Reid's avatar
Dave Reid committed
134
function xmlsitemap_permission() {
135 136
  $permissions['administer xmlsitemap'] = array(
    'title' => t('Administer XML sitemap settings.'),
Dave Reid's avatar
Dave Reid committed
137
  );
138
  return $permissions;
Darren Oh's avatar
Darren Oh committed
139 140
}

141 142 143 144 145 146 147 148 149 150 151 152
/**
 * Implements hook_theme().
 */
function xmlsitemap_theme() {
  return array(
    'xmlsitemap_content_settings_table' => array(
      'render element' => 'element',
      'file' => 'xmlsitemap.module',
    ),
  );
}

153 154
/**
 * Menu access callback; determines if the user can use the rebuild links page.
155 156 157
 *
 * @return bool
 *   Returns TRUE if current user can access rebuild form. FALSE otherwise.
158 159 160
 */
function _xmlsitemap_rebuild_form_access() {
  $rebuild_types = xmlsitemap_get_rebuildable_link_types();
161
  return !empty($rebuild_types) && \Drupal::currentUser()->hasPermission('administer xmlsitemap');
162 163
}

Darren Oh's avatar
Darren Oh committed
164
/**
165
 * Implements hook_cron().
166 167 168
 *
 * @todo Use new Queue system. Need to add {sitemap}.queued.
 * @todo Regenerate one at a time?
Darren Oh's avatar
Darren Oh committed
169
 */
170
function xmlsitemap_cron() {
171
  // If there were no new or changed links, skip.
172
  if (!\Drupal::state()->get('xmlsitemap_regenerate_needed')) {
173 174 175
    return;
  }

176
  // If the minimum sitemap lifetime hasn't been passed, skip.
177
  $lifetime = REQUEST_TIME - \Drupal::state()->get('xmlsitemap_generated_last');
178
  if ($lifetime < \Drupal::config('xmlsitemap.settings')->get('minimum_lifetime')) {
179 180
    return;
  }
181
  xmlsitemap_xmlsitemap_index_links(\Drupal::config('xmlsitemap.settings')->get('batch_limit'));
182
  // Regenerate the sitemap XML files.
183
  xmlsitemap_run_unprogressive_batch('xmlsitemap_regenerate_batch');
Darren Oh's avatar
Darren Oh committed
184 185
}

186 187 188 189 190 191 192 193 194 195 196 197 198 199
/**
 * Implements hook_modules_enabled().
 */
function xmlsitemap_modules_enabled(array $modules) {
  cache_clear_all('xmlsitemap:', 'cache', TRUE);
}

/**
 * Implements hook_modules_disabled().
 */
function xmlsitemap_modules_disabled(array $modules) {
  cache_clear_all('xmlsitemap:', 'cache', TRUE);
}

Darren Oh's avatar
Darren Oh committed
200
/**
201
 * Implements hook_robotstxt().
Darren Oh's avatar
Darren Oh committed
202
 */
203
function xmlsitemap_robotstxt() {
204 205
  $sitemap_storage = \Drupal::entityManager()->getStorage('xmlsitemap');
  if ($sitemap = $sitemap_storage->loadByContext()) {
206 207
    $uri = xmlsitemap_sitemap_uri($sitemap);
    $robotstxt[] = 'Sitemap: ' . url($uri['path'], $uri['options']);
208
    return $robotstxt;
209
  }
Darren Oh's avatar
Darren Oh committed
210 211
}

212
/**
213
 * Internal default variables config for xmlsitemap_var().
214 215 216
 *
 * @return array
 *   Array with config variables of xmlsitemap.settings config object.
217
 */
218
function xmlsitemap_config_variables() {
219
  return array(
220
    'minimum_lifetime' => 0,
221 222 223 224 225 226 227 228 229
    '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,
230
    // Removed variables are set to NULL so they can still be deleted.
231 232 233 234 235 236
    'regenerate_last' => NULL,
    'custom_links' => NULL,
    'priority_default' => NULL,
    'languages' => NULL,
    'max_chunks' => NULL,
    'max_filesize' => NULL,
237 238 239
  );
}

240 241
/**
 * Internal default variables state for xmlsitemap_var().
242 243 244
 *
 * @return array
 *   Array with state variables defined by xmlsitemap module.
245 246 247
 */
function xmlsitemap_state_variables() {
  return array(
248 249 250 251 252
    'xmlsitemap_rebuild_needed' => FALSE,
    'xmlsitemap_regenerate_needed' => TRUE,
    'xmlsitemap_base_url' => '',
    'xmlsitemap_generated_last' => 0,
    'xmlsitemap_developer_mode' => 0
253 254 255
  );
}

256 257 258 259 260 261
/**
 * Internal implementation of variable_get().
 */
function xmlsitemap_var($name, $default = NULL) {
  $defaults = &drupal_static(__FUNCTION__);
  if (!isset($defaults)) {
262 263
    $defaults = xmlsitemap_config_variables();
    $defaults += xmlsitemap_state_variables();
264 265
  }

266 267 268
  // @todo Remove when stable.
  if (!isset($defaults[$name])) {
    trigger_error(strtr('Default variable for %variable not found.', array('%variable' => drupal_placeholder($name))));
269 270
  }

271 272 273 274
  if (\Drupal::state()->get($name, NULL) === NULL) {
    return \Drupal::config('xmlsitemap.settings')->get($name);
  }
  return \Drupal::state()->get($name);
275 276
}

Darren Oh's avatar
Darren Oh committed
277
/**
278
 * @defgroup xmlsitemap_api XML sitemap API.
279
 * @{
280 281
 * 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
282
 */
283 284 285 286 287 288

/**
 * Load an XML sitemap array from the database.
 *
 * @param $smid
 *   An XML sitemap ID.
289 290 291
 *
 * @return
 *   The XML sitemap object.
292 293 294 295
 */
function xmlsitemap_sitemap_load($smid) {
  $sitemap = xmlsitemap_sitemap_load_multiple(array($smid));
  return $sitemap ? reset($sitemap) : FALSE;
Darren Oh's avatar
Darren Oh committed
296 297 298
}

/**
299 300 301
 * Load multiple XML sitemaps from the database.
 *
 * @param $smids
302 303 304
 *   An array of XML sitemap IDs, or FALSE to load all XML sitemaps.
 * @param $conditions
 *   An array of conditions in the form 'field' => $value.
305 306 307
 *
 * @return
 *   An array of XML sitemap objects.
Darren Oh's avatar
Darren Oh committed
308
 */
309 310 311 312
function xmlsitemap_sitemap_load_multiple($smids = array(), array $conditions = array()) {
  if ($smids !== FALSE) {
    $conditions['smid'] = $smids;
  }
313
  else {
314
    $conditions['smid'] = NULL;
315
  }
316
  $storage = Drupal::entityManager()->getStorage('xmlsitemap');
317

318
  $sitemaps = $storage->loadMultiple($conditions['smid']);
319 320 321 322 323 324
  if (count($sitemaps) <= 0) {
    return array();
  }
  foreach ($sitemaps as &$sitemap) {
    $uri = xmlsitemap_sitemap_uri($sitemap);
    $sitemap->uri = $uri;
Darren Oh's avatar
Darren Oh committed
325
  }
326

327
  return $sitemaps;
328 329
}

Darren Oh's avatar
Darren Oh committed
330
/**
331
 * Save changes to an XML sitemap or add a new XML sitemap.
332
 *
333
 * @param $sitemap
334
 *   The XML sitemap array to be saved. If $sitemap->smid is omitted, a new
335 336 337
 *   XML sitemap will be added.
 *
 * @todo Save the sitemap's URL as a column?
Darren Oh's avatar
Darren Oh committed
338
 */
339
function xmlsitemap_sitemap_save(XmlSitemapInterface $sitemap) {
340
  $context = $sitemap->context;
341
  if (!isset($context) || !$context) {
342
    $sitemap->context = array();
343 344
  }

345
  // Make sure context is sorted before saving the hash.
346
  $sitemap->setOriginalId($sitemap->isNew() ? NULL : $sitemap->getId());
347
  $sitemap->setId(xmlsitemap_sitemap_get_context_hash($context));
348
  // If the context was changed, we need to perform additional actions.
349
  if (!$sitemap->isNew() && $sitemap->getId() != $sitemap->getOriginalId()) {
350
    // Rename the files directory so the sitemap does not break.
351
    $old_sitemap = (object) array('smid' => $sitemap->old_smid);
352
    $old_dir = xmlsitemap_get_directory($old_sitemap);
353 354 355 356
    $new_dir = xmlsitemap_get_directory($sitemap);
    xmlsitemap_directory_move($old_dir, $new_dir);

    // Mark the sitemaps as needing regeneration.
357
    \Drupal::state()->set('xmlsitemap_regenerate_needed', TRUE);
358
  }
359
  $sitemap->save();
360 361

  return $sitemap;
362 363 364
}

/**
365
 * Delete an XML sitemap.
366
 *
367
 * @param string $smid
368
 *   An XML sitemap ID.
369
 */
370 371
function xmlsitemap_sitemap_delete($smid) {
  xmlsitemap_sitemap_delete_multiple(array($smid));
372 373
}

Darren Oh's avatar
Darren Oh committed
374
/**
375
 * Delete multiple XML sitemaps.
376
 *
377
 * @param array $smids
378
 *   An array of XML sitemap IDs.
Darren Oh's avatar
Darren Oh committed
379
 */
380 381 382 383 384
function xmlsitemap_sitemap_delete_multiple(array $smids) {
  if (!empty($smids)) {
    $sitemaps = xmlsitemap_sitemap_load_multiple($smids);
    foreach ($sitemaps as $sitemap) {
      xmlsitemap_clear_directory($sitemap, TRUE);
385
      $sitemap->delete();
386
      \Drupal::moduleHandler()->invokeAll('xmlsitemap_sitemap_delete', array($sitemap));
387
    }
388
  }
389
}
390

391 392 393 394
/**
 * Return the expected file path for a specific sitemap chunk.
 *
 * @param $sitemap
395
 *   An XmlSitemapInterface sitemap object.
396 397
 * @param $chunk
 *   An optional specific chunk in the sitemap. Defaults to the index page.
398 399 400
 *
 * @return string
 *   File path for a specific sitemap chunk.
401
 */
402
function xmlsitemap_sitemap_get_file(XmlSitemapInterface $sitemap, $chunk = 'index') {
403
  return xmlsitemap_get_directory($sitemap) . "/{$chunk}.xml";
Darren Oh's avatar
Darren Oh committed
404 405
}

406 407 408
/**
 * Find the maximum file size of all a sitemap's XML files.
 *
409 410
 * @param \Drupal\xmlsitemap\XmlSitemapInterface $sitemap
 *   The XML sitemap object.
411
 */
412
function xmlsitemap_sitemap_get_max_filesize(XmlSitemapInterface $sitemap) {
413
  $dir = xmlsitemap_get_directory($sitemap);
414
  $sitemap->setMaxFileSize(0);
415
  foreach (file_scan_directory($dir, '/\.xml$/') as $file) {
416
    $sitemap->setMaxFileSize(max($sitemap->getMaxFileSize(), filesize($file->uri)));
417
  }
418
  return $sitemap->getMaxFileSize();
419 420
}

421 422 423 424 425 426 427 428
/**
 * Returns the hash string for a context.
 *
 * @param array $context
 *   Context to be hashed.
 * @return string
 *   Hash string for the context.
 */
429 430
function xmlsitemap_sitemap_get_context_hash(array &$context) {
  asort($context);
431
  return Crypt::hashBase64(serialize($context));
432 433
}

Darren Oh's avatar
Darren Oh committed
434
/**
435
 * Returns the uri elements of an XML sitemap.
436
 *
437 438
 * @param \Drupal\xmlsitemap\XmlSitemapInterface $sitemap
 *   The sitemap represented by and XmlSitemapInterface object.
439
 * @return
440 441
 *   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
442
 */
443
function xmlsitemap_sitemap_uri(XmlSitemapInterface $sitemap) {
444
  $uri['path'] = 'sitemap.xml';
445 446
  $uri['options'] = \Drupal::moduleHandler()->invokeAll('xmlsitemap_context_url_options', array($sitemap->context));
  $context = $sitemap->context;
447
  \Drupal::moduleHandler()->alter('xmlsitemap_context_url_options', $uri['options'], $context);
448 449
  $uri['options'] += array(
    'absolute' => TRUE,
450
    'base_url' => \Drupal::state()->get('xmlsitemap_base_url')
451 452 453
  );
  return $uri;
}
454

Darren Oh's avatar
Darren Oh committed
455
/**
456
 * @} End of "defgroup xmlsitemap_api"
457
 */
458
function xmlsitemap_get_directory(XmlSitemapInterface $sitemap = NULL) {
459 460
  $directory = &drupal_static(__FUNCTION__);
  if (!isset($directory)) {
461
    $directory = \Drupal::config('xmlsitemap.settings')->get('path');
Darren Oh's avatar
Darren Oh committed
462
  }
463

464
  if ($sitemap != NULL && !empty($sitemap->id)) {
465
    return file_build_uri($directory . '/' . $sitemap->id);
466 467 468 469
  }
  else {
    return file_build_uri($directory);
  }
Darren Oh's avatar
Darren Oh committed
470 471 472
}

/**
473
 * Check that the sitemap files directory exists and is writable.
Darren Oh's avatar
Darren Oh committed
474
 */
475
function xmlsitemap_check_directory(XmlSitemapInterface $sitemap = NULL) {
476 477 478 479 480 481 482 483
  $directory = xmlsitemap_get_directory($sitemap);
  $result = file_prepare_directory($directory, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);
  if (!$result) {
    watchdog('file system', 'The directory %directory does not exist or is not writable.', array('%directory' => $directory), WATCHDOG_ERROR);
  }
  return $result;
}

484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505
function xmlsitemap_check_all_directories() {
  $directories = array();

  $sitemaps = xmlsitemap_sitemap_load_multiple(FALSE);
  foreach ($sitemaps as $smid => $sitemap) {
    $directory = xmlsitemap_get_directory($sitemap);
    $directories[$directory] = $directory;
  }

  foreach ($directories as $directory) {
    $result = file_prepare_directory($directory, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);
    if ($result) {
      $directories[$directory] = TRUE;
    }
    else {
      $directories[$directory] = FALSE;
    }
  }

  return $directories;
}

506 507 508 509 510 511 512 513 514 515 516
/**
 * 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.
 */
517
function xmlsitemap_clear_directory(XmlSitemapInterface $sitemap = NULL, $delete = FALSE) {
518 519 520 521
  $directory = xmlsitemap_get_directory($sitemap);
  return _xmlsitemap_delete_recursive($directory, $delete);
}

522 523 524
/**
 * Move a directory to a new location.
 *
525
 * @param string $old_dir
526
 *   A string specifying the filepath or URI of the original directory.
527
 * @param string $new_dir
528
 *   A string specifying the filepath or URI of the new directory.
529
 * @param integer $replace
530 531
 *   Replace behavior when the destination file already exists.
 *
532
 * @return bool
533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554
 *   TRUE if the directory was moved successfully. FALSE otherwise.
 */
function xmlsitemap_directory_move($old_dir, $new_dir, $replace = FILE_EXISTS_REPLACE) {
  $success = file_prepare_directory($new_dir, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);

  $old_path = drupal_realpath($old_dir);
  $new_path = drupal_realpath($new_dir);
  if (!is_dir($old_path) || !is_dir($new_path) || !$success) {
    return FALSE;
  }

  $files = file_scan_directory($old_dir, '/.*/');
  foreach ($files as $file) {
    $file->uri_new = $new_dir . '/' . basename($file->filename);
    $success &= (bool) file_unmanaged_move($file->uri, $file->uri_new, $replace);
  }

  // The remove the directory.
  $success &= drupal_rmdir($old_dir);
  return $success;
}

555 556 557
/**
 * Recursively delete all files and folders in the specified filepath.
 *
558
 * This is a backport of Drupal 8's file_unmanaged_delete_recursive().
559 560 561
 *
 * Note that this only deletes visible files with write permission.
 *
562
 * @param string $path
563
 *   A filepath relative to the Drupal root directory.
564
 * @param bool $delete_root
565
 *   A boolean if TRUE will delete the $path directory afterwards.
566 567 568
 *
 * @return bool
 *   TRUE if operation was successful, FALSE otherwise.
569 570 571 572 573 574 575 576 577 578 579
 */
function _xmlsitemap_delete_recursive($path, $delete_root = FALSE) {
  // Resolve streamwrapper URI to local path.
  $path = drupal_realpath($path);
  if (is_dir($path)) {
    $dir = dir($path);
    while (($entry = $dir->read()) !== FALSE) {
      if ($entry == '.' || $entry == '..') {
        continue;
      }
      $entry_path = $path . '/' . $entry;
580
      file_unmanaged_delete_recursive($entry_path, NULL);
581 582
    }
    $dir->close();
583
    return $delete_root ? drupal_rmdir($path) : TRUE;
584 585
  }
  return file_unmanaged_delete($path);
Darren Oh's avatar
Darren Oh committed
586 587 588
}

/**
589 590 591 592 593 594 595 596 597
 * Returns information about supported sitemap link types.
 *
 * @param $type
 *   (optional) The link type to return information for. If omitted,
 *   information for all link types is returned.
 * @param $reset
 *   (optional) Boolean whether to reset the static cache and do nothing. Only
 *   used for tests.
 *
598 599 600
 * @return array
 *   Info about sitemap link.
 *
601 602
 * @see hook_xmlsitemap_link_info()
 * @see hook_xmlsitemap_link_info_alter()
Darren Oh's avatar
Darren Oh committed
603
 */
604
function xmlsitemap_get_link_info($type = NULL, $reset = FALSE) {
605
  $language = \Drupal::languageManager()->getCurrentLanguage();
606
  $link_info = &drupal_static(__FUNCTION__);
607

608 609
  if ($reset) {
    $link_info = NULL;
610 611 612
    foreach (\Drupal::languageManager()->getLanguages() as $lang) {
      \Drupal::cache()->delete('xmlsitemap:link_info:' . $lang->getId());
    }
613 614
  }

615
  if (!isset($link_info)) {
616
    $cid = 'xmlsitemap:link_info:' . $language->getId();
617
    if ($cache = \Drupal::cache()->get($cid)) {
618
      $link_info = $cache->data;
619
    }
620
    else {
621
      \Drupal::entityManager()->clearCachedDefinitions();
622 623 624 625 626 627 628 629 630 631 632
      $link_info = array();
      $entity_types = \Drupal::entityManager()->getDefinitions();
      foreach ($entity_types as $key => $entity_type) {
        $link_info[$key] = array(
          'label' => $entity_type->getLabel(),
          'type' => $entity_type->id(),
          'base table' => $entity_type->getBaseTable(),
          'bundles' => \Drupal::entityManager()->getBundleInfo($entity_type->id())
        );
        $uri_callback = $entity_type->getUriCallback();
        if (empty($uri_callback) || !isset($entity_type->xmlsitemap)) {
633 634
          // Remove any non URL-able or XML sitemap un-supported entites.
        }
635
        foreach (\Drupal::entityManager()->getBundleInfo($entity_type->id()) as $bundle_key => $bundle) {
636 637 638 639
          if (!isset($bundle['xmlsitemap'])) {
            // Remove any un-supported entity bundles.
          }
        }
640
      }
641
      $link_info = array_merge($link_info, \Drupal::moduleHandler()->invokeAll('xmlsitemap_link_info'));
642 643 644 645 646
      foreach ($link_info as $key => &$info) {
        $info += array(
          'type' => $key,
          'base table' => FALSE,
          'bundles' => array(),
647 648
          'xmlsitemap' => array(),
        );
649 650 651 652 653 654 655 656 657
        if (!isset($info['xmlsitemap']['rebuild callback']) && !empty($info['base table']) && !empty($info['entity keys']['id']) && !empty($info['xmlsitemap']['process callback'])) {
          $info['xmlsitemap']['rebuild callback'] = 'xmlsitemap_rebuild_batch_fetch';
        }
        foreach ($info['bundles'] as $bundle => &$bundle_info) {
          $bundle_info += array(
            'xmlsitemap' => array(),
          );
          $bundle_info['xmlsitemap'] += xmlsitemap_link_bundle_load($key, $bundle, FALSE);
        }
658
      }
659
      \Drupal::moduleHandler()->alter('xmlsitemap_link_info', $link_info);
660 661
      ksort($link_info);
      // Cache by language since this info contains translated strings.
662
      \Drupal::cache()->set($cid, $link_info);
663
    }
Darren Oh's avatar
Darren Oh committed
664
  }
665 666 667 668 669 670

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

  return $link_info;
Darren Oh's avatar
Darren Oh committed
671 672
}

673 674 675 676 677 678 679 680 681
/**
 * Returns enabled bundles of an entity type.
 *
 * @param string $entity_type
 *   Entity type id.
 *
 * @return array
 *   Array with entity bundles info.
 */
682 683 684 685
function xmlsitemap_get_link_type_enabled_bundles($entity_type) {
  $bundles = array();
  $info = xmlsitemap_get_link_info($entity_type);
  foreach ($info['bundles'] as $bundle => $bundle_info) {
686
    $settings = xmlsitemap_link_bundle_load($entity_type, $bundle);
687
    if (!empty($settings['status'])) {
688 689 690 691 692 693
      $bundles[] = $bundle;
    }
  }
  return $bundles;
}

694 695 696 697 698 699 700 701 702 703 704
/**
 * Returns statistics about specific entity links.
 *
 * @param string $entity_type
 *   Entity type id.
 * @param string $bundle
 *   Bundle id;
 *
 * @return array
 *   Array with statistics.
 */
705 706 707
function xmlsitemap_get_link_type_indexed_status($entity_type, $bundle = '') {
  $info = xmlsitemap_get_link_info($entity_type);

708 709
  $status['indexed'] = db_query("SELECT COUNT(id) FROM {xmlsitemap} WHERE type = :entity AND subtype = :bundle", array(':entity' => $entity_type, ':bundle' => $bundle))->fetchField();
  $status['visible'] = db_query("SELECT COUNT(id) FROM {xmlsitemap} WHERE type = :entity AND subtype = :bundle AND status = 1 AND access = 1", array(':entity' => $entity_type, ':bundle' => $bundle))->fetchField();
710

711 712 713 714 715 716 717 718 719 720 721 722 723 724
  try {
    $entity_query_object = \Drupal::entityQuery($entity_type);
    $entity_query_object->addTag('xmlsitemap_link_bundle_access');
    $entity_query_object->addTag('xmlsitemap_link_indexed_status');
    $entity_query_object->addMetaData('entity', $entity_type);
    $entity_query_object->addMetaData('type', $bundle);
    $entity_query_object->addMetaData('entity_info', $info);
    $entity_query_object->count();
    $status['total'] = $entity_query_object->execute();
    return $status;
  }
  catch (Exception $e) {
    $status['total'] = 0;
  }
725 726
  return $status;
}
727

728 729 730 731 732 733 734 735 736 737 738 739 740 741
/**
 * Implements hook_entity_query_alter().
 *
 * @todo Remove when http://drupal.org/node/1054168 is fixed.
 */
function xmlsitemap_entity_query_alter($query) {
  $conditions = &$query->entityConditions;

  // Alter user entity queries only.
  if (isset($conditions['entity_type']) && $conditions['entity_type']['value'] == 'user' && isset($conditions['bundle'])) {
    unset($conditions['bundle']);
  }
}

742 743 744 745 746 747 748 749 750 751 752 753 754 755 756
/**
 * 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.
 *
 * @return array
 *   Info about sitemap link.
 */
757 758
function xmlsitemap_link_bundle_settings_save($entity, $bundle, array $settings, $update_links = TRUE) {
  if ($update_links) {
759
    $old_settings = xmlsitemap_link_bundle_load($entity, $bundle);
760
    if ($settings['status'] != $old_settings['status']) {
761
      \Drupal::service('xmlsitemap.link_storage')->updateMultiple(array('status' => $settings['status']), array('type' => $entity, 'subtype' => $bundle, 'status_override' => 0));
762 763
    }
    if ($settings['priority'] != $old_settings['priority']) {
764
      \Drupal::service('xmlsitemap.link_storage')->updateMultiple(array('priority' => $settings['priority']), array('type' => $entity, 'subtype' => $bundle, 'priority_override' => 0));
765 766 767
    }
  }

768
  \Drupal::config('xmlsitemap.settings')->set("xmlsitemap_settings_{$entity}_{$bundle}", $settings)->save();
769
  foreach (\Drupal::languageManager()->getLanguages() as $lang) {
770
    \Drupal::cache()->delete('xmlsitemap:link_info:' . $lang->getId());
771 772
  }
  xmlsitemap_get_link_info(NULL, TRUE);
773 774
}

775 776 777 778 779 780 781 782 783 784
/**
 * Renames a bundle.
 *
 * @param string $entity
 *   Entity type id.
 * @param string $bundle_old
 *   Old bundle name.
 * @param string $bundle_new
 *   New bundle name.
 */
785 786
function xmlsitemap_link_bundle_rename($entity, $bundle_old, $bundle_new) {
  if ($bundle_old != $bundle_new) {
787
    $settings = xmlsitemap_link_bundle_load($entity, $bundle_old);
788 789 790 791
    \Drupal::config('xmlsitemap.settings')->clear("xmlsitemap_settings_{$entity}_{$bundle_old}");
    $old_bundle_value = \Drupal::config('xmlsitemap.settings')->get("xmlsitemap_entity_{$entity}_bundle_{$bundle_old}");
    \Drupal::config('xmlsitemap.settings')->clear("xmlsitemap_entity_{$entity}_bundle_{$bundle_old}");
    \Drupal::config('xmlsitemap.settings')->set("xmlsitemap_entity_{$entity}_bundle_{$bundle_new}", $old_bundle_value)->save();
792
    xmlsitemap_link_bundle_settings_save($entity, $bundle_new, $settings, FALSE);
793
    \Drupal::service('xmlsitemap.link_storage')->updateMultiple(array('subtype' => $bundle_new), array('type' => $entity, 'subtype' => $bundle_old));
794 795 796
  }
}

797
/**
798 799 800 801 802 803 804 805
 * Renames a link type.
 *
 * @param string $entity_old
 *   Old entity type id.
 * @param type $entity_new
 *   New entity type id.
 * @param array $bundles
 *   Bundles to be updated.
806 807
 */
function xmlsitemap_link_type_rename($entity_old, $entity_new, $bundles = NULL) {
808 809 810 811 812 813
  $variables = \Drupal::config('xmlsitemap.settings')->get();
  foreach ($variables as $key => $value) {
    if (!strpos($key, "xmlsitemap_settings_{$entity_old}") && !strpos($key, "xmlsitemap_entity_{$entity_old}")) {
      continue;
    }
    \Drupal::config('xmlsitemap.settings')->clear($key);
814
    if (isset($value)) {
815
      $variable_new = str_replace('xmlsitemap_settings_' . $entity_old, 'xmlsitemap_settings_' . $entity_new, $key);
816
      \Drupal::config('xmlsitemap.settings')->set($variable_new, $value)->save();
817 818 819
    }
  }

820
  \Drupal::service('xmlsitemap.link_storage')->updateMultiple(array('type' => $entity_new), array('type' => $entity_old), FALSE);
821 822 823
  xmlsitemap_get_link_info(NULL, TRUE);
}

824 825 826 827 828 829 830 831 832 833 834 835
/**
 * 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.
 * @return array
 *   Info about a bundle.
 */
836 837 838 839 840 841 842
function xmlsitemap_link_bundle_load($entity, $bundle, $load_bundle_info = TRUE) {
  $info = array(
    'entity' => $entity,
    'bundle' => $bundle,
  );
  if ($load_bundle_info) {
    $entity_info = xmlsitemap_get_link_info($entity);
843 844 845
    if (isset($entity_info['bundles'][$bundle])) {
      $info['info'] = $entity_info['bundles'][$bundle];
    }
846
  }
847 848 849 850
  $bundle_settings = \Drupal::config('xmlsitemap.settings')->get("xmlsitemap_settings_{$entity}_{$bundle}");
  if ($bundle_settings) {
    $info += $bundle_settings;
  }
851 852 853
  $info += array(
    'status' => XMLSITEMAP_STATUS_DEFAULT,
    'priority' => XMLSITEMAP_PRIORITY_DEFAULT,
854
    'changefreq' => 0
855 856
  );
  return $info;
857 858
}

859 860 861 862 863 864 865 866 867 868
/**
 * 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.
 */
869
function xmlsitemap_link_bundle_delete($entity, $bundle, $delete_links = TRUE) {
870
  \Drupal::config('xmlsitemap.settings')->clear("xmlsitemap_settings_{$entity}_{$bundle}");
871
  \Drupal::config('xmlsitemap.settings')->clear("xmlsitemap_entity_{$entity}_bundle_{$bundle}");
872
  if ($delete_links) {
873
    \Drupal::service('xmlsitemap.link_storage')->deleteMultiple(array('type' => $entity, 'subtype' => $bundle));
874
  }
875
  xmlsitemap_get_link_info(NULL, TRUE);
876 877
}

878 879 880 881 882 883 884 885 886 887
/**
 * Checks access for a bundle.
 *
 * @param string $entity
 *   Entity type id.
 * @param string $bundle
 *   Bundle id.
 * @return bool
 *   If TRUE, access is allowed, FALSE otherwise.
 */
888 889 890 891 892 893 894
function xmlsitemap_link_bundle_access($entity, $bundle = NULL) {
  if (is_array($entity) && !isset($bundle)) {
    $bundle = $entity;
  }
  else {
    $bundle = xmlsitemap_link_bundle_load($entity, $bundle);
  }
895

896 897
  if (isset($bundle['info']['admin'])) {
    $admin = $bundle['info']['admin'];
898 899 900 901 902 903 904 905
    $admin += array('access arguments' => array());

    if (!isset($admin['access callback']) && count($admin['access arguments']) == 1) {
      $admin['access callback'] = 'user_access';
    }

    if (!empty($admin['access callback'])) {
      return call_user_func_array($admin['access callback'], $admin['access arguments']);
906 907 908 909 910
    }
  }

  return FALSE;
}
911

912 913 914 915 916 917 918 919 920 921 922
/**
 * Get path of a bundle.
 *
 * @param string $entity
 *   Entity type id.
 * @param string $bundle
 *   Bundle id.
 *
 * @return
 *   Path of bundle, or FALSE if it does not exist.
 */
923 924 925 926 927 928 929 930 931 932 933 934 935
function xmlsitemap_get_bundle_path($entity, $bundle) {
  $info = xmlsitemap_get_link_info($entity);

  if (!empty($info['bundles'][$bundle]['admin']['real path'])) {
    return $info['bundles'][$bundle]['admin']['real path'];
  }
  elseif (!empty($info['bundles'][$bundle]['admin']['path'])) {
    return $info['bundles'][$bundle]['admin']['path'];
  }
  else {
    return FALSE;
  }
}
936

937
/**
938
 * Implements hook_entity_bundle_rename().
939
 */
940 941
function xmlsitemap_entity_bundle_rename($entity_type_id, $bundle_old, $bundle_new) {
  xmlsitemap_link_bundle_rename($entity_type_id, $bundle_old, $bundle_new);
942 943 944
}

/**
945
 * Implements hook_entity_bundle_delete().
946
 */
947
function xmlsitemap_entity_bundle_delete($entity_type, $bundle, $instances) {
948 949 950
  xmlsitemap_link_bundle_delete($entity_type, $bundle, TRUE);
}

951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975
/**
 * Determine the frequency of updates to a link.
 *
 * @param $interval
 *   An interval value in seconds.
 * @return
 *   A string representing the update frequency according to the sitemaps.org
 *   protocol.
 */
function xmlsitemap_get_changefreq($interval) {
  if ($interval <= 0 || !is_numeric($interval)) {
    return FALSE;
  }

  foreach (xmlsitemap_get_changefreq_options() as $value => $frequency) {
    if ($interval <= $value) {
      return $frequency;
    }
  }

  return 'never';
}

/**
 * Get the current number of sitemap chunks.
976 977 978 979 980 981 982 983
 *
 * @static int $chunks
 *   Number of chunks.
 * @param int $reset
 *   If TRUE, reset number of chunks.
 *
 * @return integer
 *   Number of chunks.
984 985 986 987 988 989 990 991 992 993 994 995
 */
function xmlsitemap_get_chunk_count($reset = FALSE) {
  static $chunks;
  if (!isset($chunks) || $reset) {
    $count = max(xmlsitemap_get_link_count($reset), 1);
    $chunks = ceil($count / xmlsitemap_get_chunk_size($reset));
  }
  return $chunks;
}

/**
 * Get the current number of sitemap links.
996 997 998 999 1000 1001 1002 1003
 *
 * @static int $count
 *   Current number of sitemap links.
 * @param bool $reset
 *   If TRUE, update current number of sitemap links.
 *
 * @return integer
 *   Returns current number of sitemap links.
1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021