Simplesitemap.php 5.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
<?php
/**
 * @file
 * Contains \Drupal\simplesitemap\Simplesitemap.
 */

namespace Drupal\simplesitemap;

use Drupal\Core\Url;

/**
 * Simplesitemap class.
 */
class Simplesitemap {

  private $sitemap;
Pawel G's avatar
Pawel G committed
17 18
  private $content_types;
  private $custom;
19 20 21 22 23 24 25
  private $lang;

  function __construct() {
    $this->set_current_lang();
    $this->set_config();
  }

Pawel G's avatar
Pawel G committed
26 27
  private function set_current_lang($language = NULL) {
    $this->lang = is_null($language) ? \Drupal::languageManager()->getCurrentLanguage() : $language;
28 29 30
  }

  private function set_config() {
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
    $this->get_config_from_db();
    $this->get_sitemap_from_db();
  }

  // Get sitemap from database.
  private function get_sitemap_from_db() {
    $result = db_select('simplesitemap', 's')
      ->fields('s', array('sitemap_string'))
      ->condition('language_code', $this->lang->getId())
      ->execute()->fetchAll();
    $this->sitemap = !empty($result[0]->sitemap_string) ? $result[0]->sitemap_string : NULL;
  }

  // Get sitemap settings from configuration storage.
  private function get_config_from_db() {
Pawel G's avatar
Pawel G committed
46 47 48
    $config = \Drupal::config('simplesitemap.settings');
    $this->content_types = $config->get('content_types');
    $this->custom = $config->get('custom');
49 50 51
  }

  private function save_sitemap() {
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80

    //todo: db_merge not working in D8(?), this is why the following queries are needed:
//    db_merge('simplesitemap')
//      ->key(array('language_code', $this->lang->getId()))
//      ->fields(array(
//        'language_code' => $this->lang->getId(),
//        'sitemap_string' => $this->sitemap,
//      ))
//      ->execute();
    $exists_query = db_select('simplesitemap')
      ->condition('language_code', $this->lang->getId())
      ->countQuery()->execute()->fetchField();

    if ($exists_query > 0) {
      db_update('simplesitemap')
        ->fields(array(
          'sitemap_string' => $this->sitemap,
        ))
        ->condition('language_code', $this->lang->getId())
        ->execute();
    }
    else {
      db_insert('simplesitemap')
        ->fields(array(
          'language_code' => $this->lang->getId(),
          'sitemap_string' => $this->sitemap,
        ))
        ->execute();
    }
Pawel G's avatar
Pawel G committed
81 82 83 84 85 86 87 88
  }

  public function save_content_types($content_types) {
    $this->save_config('content_types', $content_types);
  }

  private function save_config($key, $value) {
    \Drupal::service('config.factory')->getEditable('simplesitemap.settings')->set($key, $value)->save();
89 90 91 92 93 94 95 96 97 98 99 100
    $this->set_config();
  }

  private function add_xml_link_markup($url, $priority) {
    return "<url><loc>" . $url . "</loc><priority>" . $priority . "</priority></url>";
  }

  private function generate_sitemap() {

    $output = '';

    // Add custom links according to config file.
101 102
    $custom = $this->custom;
    foreach ($custom as $page) {
103
      if ($page['index']) {
Pawel G's avatar
Pawel G committed
104 105 106 107
        $output .= $this->add_xml_link_markup(Url::fromUserInput($page['path'], array(
          'language' => $this->lang,
          'absolute' => TRUE
        ))->toString(), $page['priority']);
108 109 110 111
      }
    }

    // Add node links according to content type settings.
Pawel G's avatar
Pawel G committed
112
    $content_types = $this->content_types;
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
    if (count($content_types) > 0) {

      //todo: D8 entityQuery doesn't seem to take multiple OR conditions, that's why that ugly db_select.
//        $query = \Drupal::entityQuery('node')
//          ->condition('status', 1)
//          ->condition('type', array_keys($content_types));
//        $nids = $query->execute();
      $query = db_select('node_field_data', 'n')
        ->fields('n', array('nid', 'type'))
        ->condition('status', 1);
      $db_or = db_or();
      foreach ($content_types as $machine_name => $options) {
        if ($options['index']) {
          $db_or->condition('type', $machine_name);
        }
      }
      $query->condition($db_or);
      $nids = $query->execute()->fetchAllAssoc('nid');

      foreach ($nids as $nid => $node) {
Pawel G's avatar
Pawel G committed
133 134 135 136
        $output .= $this->add_xml_link_markup(Url::fromRoute('entity.node.canonical', array('node' => $nid), array(
          'language' => $this->lang,
          'absolute' => TRUE
        ))->toString(), $content_types[$node->type]['priority']);
137 138 139 140 141 142 143 144 145 146 147
      }
    }

    // Add sitemap markup.
    $output = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">" . $output . "</urlset>";

    $this->sitemap = $output;
    $this->save_sitemap();
  }

  public function get_content_types() {
Pawel G's avatar
Pawel G committed
148
    return $this->content_types;
149 150 151 152 153 154 155 156 157 158 159
  }

  public function get_sitemap() {
    if (empty($this->sitemap)) {
      $this->generate_sitemap();
    }
    return $this->sitemap;
  }

  public function generate_all_sitemaps() {
    foreach(\Drupal::languageManager()->getLanguages() as $language) {
Pawel G's avatar
Pawel G committed
160
      $this->set_current_lang($language);
161 162
      $this->generate_sitemap();
    }
163
    //todo: Delete sitemaps the languages of which have been disabled/removed.
164 165
  }

Pawel G's avatar
Pawel G committed
166
  public static function get_priority_select_values() {
167 168 169 170 171 172 173
    foreach(range(0, 10) as $value) {
      $value = $value / 10;
      $options[(string)$value] = (string)$value;
    }
    return $options;
  }

Pawel G's avatar
Pawel G committed
174 175
  public static function get_priority_default() {
    return $priority_default = 0.5;
176 177
  }
}