Commit 0cc279d4 authored by gbyte.co's avatar gbyte.co Committed by gbyte.co

Adding ability to disable cron sitemap generation. Cleaning up some code.

parent 90e680ec
......@@ -7,3 +7,4 @@ entity_types: []
settings:
max_links: 2000
cron_generate: true
<?php
/**
* @file
* Module install and update procedures.
......
......@@ -53,7 +53,7 @@ function simplesitemap_form_alter(&$form, $form_state, $form_id) {
$sitemap = new Simplesitemap;
// Get current entity type sitemap settings.
$entity_types = $sitemap->get_entity_types();
$entity_types = $sitemap->get_config('entity_types');
$form['simplesitemap'] = array(
'#group' => 'additional_settings',
......@@ -108,13 +108,13 @@ function simplesitemap_entity_form_submit($form, &$form_state) {
$bundle_name = $form['#simplesitemap']['bundle_name'];
// Get current entity type sitemap settings.
$entity_types = $sitemap->get_entity_types();
$entity_types = $sitemap->get_config('entity_types');
$entity_types[$entity_type_id][$bundle_name]['index'] = $values['simplesitemap_index_content'];
$entity_types[$entity_type_id][$bundle_name]['priority'] = $values['simplesitemap_priority'];
// Save new entity type settings.
$sitemap->save_entity_types($entity_types);
$sitemap->save_config('entity_types', $entity_types);
}
// Regenerate sitemaps according to user setting.
......@@ -127,10 +127,11 @@ function simplesitemap_entity_form_submit($form, &$form_state) {
* Implements hook_cron.
*/
function simplesitemap_cron() {
// Regenerate sitemap for all languages.
$sitemap = new Simplesitemap;
$sitemap->generate_sitemap();
if ($sitemap->get_setting('cron_generate')) {
// Regenerate sitemap for all languages.
$sitemap->generate_sitemap();
}
}
/**
......@@ -140,12 +141,12 @@ function simplesitemap_cron() {
*/
function simplesitemap_entity_bundle_delete($entity_type_id, $bundle) {
$sitemap = new Simplesitemap;
$entity_types = $sitemap->get_entity_types();
$entity_types = $sitemap->get_config('entity_types');
$bundle_entity_type_id = \Drupal::entityManager()->getDefinition($entity_type_id)->getBundleEntityType();
if (isset($entity_types[$bundle_entity_type_id][$bundle])) {
unset($entity_types[$bundle_entity_type_id][$bundle]);
$sitemap->save_entity_types($entity_types);
$sitemap->save_config('entity_types', $entity_types);
drupal_set_message(t("You may want to <a href='@url'>rebuild</a> your XML sitemap now.
Otherwise it will be rebuilt on the next cron run.",
array('@url' => $GLOBALS['base_url'] . '/admin/config/search/simplesitemap')));
......
......@@ -19,6 +19,7 @@ class CustomLinkGenerator {
* @param array $custom_paths
*
* @return array $urls
*
*/
public function get_custom_paths($custom_paths) {
$paths = array();
......@@ -26,7 +27,8 @@ class CustomLinkGenerator {
if (!isset($custom_path['index']) || $custom_path['index']) {
$paths[$i]['path'] = substr($custom_path['path'], 1);
$paths[$i]['priority'] = isset($custom_path['priority']) ? $custom_path['priority'] : NULL;
$paths[$i]['lastmod'] = NULL; //todo: implement
$paths[$i]['lastmod'] = NULL; //todo: implement lastmod
//todo: get url parameters and page fragment into $paths[$i]['settings'] so that future hook implementations can alter the array instead of having to do string processing.
}
}
return $paths;
......
......@@ -37,7 +37,7 @@ class SimplesitemapCustomLinksForm extends ConfigFormBase {
$sitemap = new Simplesitemap;
$setting_string = '';
foreach ($sitemap->get_custom_links() as $custom_link) {
foreach ($sitemap->get_config('custom') as $custom_link) {
// todo: remove this statement after removing the index key from the configuration.
if (isset($custom_link['index']) && $custom_link['index'] == 0)
......@@ -112,7 +112,7 @@ class SimplesitemapCustomLinksForm extends ConfigFormBase {
$custom_link_config[$key]['priority'] = number_format((float)$line_settings[1], 1, '.', '');
}
}
$sitemap->save_custom_links($custom_link_config);
$sitemap->save_config('custom', $custom_link_config);
parent::submitForm($form, $form_state);
......
......@@ -36,7 +36,6 @@ class SimplesitemapSettingsForm extends ConfigFormBase {
public function buildForm(array $form, FormStateInterface $form_state) {
$sitemap = new Simplesitemap;
$settings = $sitemap->get_settings();
$form['simplesitemap_settings']['rebuild'] = array(
'#title' => t('Rebuild sitemap'),
......@@ -63,7 +62,14 @@ class SimplesitemapSettingsForm extends ConfigFormBase {
'#type' => 'textfield',
'#maxlength' => 5,
'#size' => 5,
'#default_value' => $settings['max_links'],
'#default_value' => $sitemap->get_setting('max_links'),
);
$form['simplesitemap_settings']['settings']['cron_generate'] = array(
'#type' => 'checkbox',
'#title' => t('Regenerate the sitemap on every cron run'),
'#description' => t('Uncheck this if you intend to only regenerate the sitemap manually or via drush.'),
'#default_value' => $sitemap->get_setting('cron_generate'),
);
return parent::buildForm($form, $form_state);
......@@ -86,9 +92,8 @@ class SimplesitemapSettingsForm extends ConfigFormBase {
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$sitemap = new Simplesitemap;
$settings = $sitemap->get_settings();
$settings['max_links'] = $form_state->getValue('max_links');
$sitemap->save_settings($settings);
$sitemap->save_setting('max_links', $form_state->getValue('max_links'));
$sitemap->save_setting('cron_generate', $form_state->getValue('cron_generate'));
parent::submitForm($form, $form_state);
}
......
......@@ -20,10 +20,16 @@ class Simplesitemap {
$this->initialize();
}
private function initialize() {
$this->get_config_from_db();
$this->get_sitemap_from_db();
}
/**
* Returns an the form entity object.
*
* @param object $form_state
*
* @return object $entity or FALSE if non-existent.
*/
public static function get_form_entity($form_state) {
......@@ -35,50 +41,36 @@ class Simplesitemap {
return FALSE;
}
private function initialize() {
$this->get_config_from_db();
$this->get_sitemap_from_db();
}
// Get sitemap from database.
/**
* Gets sitemap from db.
*/
private function get_sitemap_from_db() {
$this->sitemap = db_query("SELECT * FROM {simplesitemap}")->fetchAllAssoc('id');
}
/**
* Gets sitemap settings from configuration storage.
* Gets sitemap settings from the configuration storage.
*/
private function get_config_from_db() {
$this->config = \Drupal::config('simplesitemap.settings');
}
/**
* Saves entity type sitemap settings to db.
*
* @param array $entity_types
*/
public function save_entity_types($entity_types) {
$this->save_config('entity_types', $entity_types);
}
/**
* Saves the sitemap custom links settings to db.
*
* @param array $custom_links
* Gets a specific sitemap configuration from the configuration storage.
*/
public function save_custom_links($custom_links) {
$this->save_config('custom', $custom_links);
public function get_config($key) {
return $this->config->get($key);
}
/**
* Saves other sitemap settings to db.
* Saves a specific sitemap configuration to db.
*
* @param array $settings
* @param string $key
* Configuration key, like 'entity_links'.
* @param mixed $value
* The configuration to be saved.
*/
public function save_settings($settings) {
$this->save_config('settings', $settings);
}
private function save_config($key, $value) {
public function save_config($key, $value) {
\Drupal::service('config.factory')->getEditable('simplesitemap.settings')
->set($key, $value)->save();
$this->initialize();
......@@ -124,10 +116,9 @@ class Simplesitemap {
*/
public function generate_sitemap() {
$generator = new SitemapGenerator();
$generator->set_custom_links($this->config->get('custom'));
$generator->set_entity_types($this->config->get('entity_types'));
$settings = $this->get_settings();
$this->sitemap = $generator->generate_sitemap($settings['max_links']);
$generator->set_custom_links($this->get_config('custom'));
$generator->set_entity_types($this->get_config('entity_types'));
$this->sitemap = $generator->generate_sitemap($this->get_setting('max_links'));
$this->save_sitemap();
drupal_set_message(t("The <a href='@url' target='_blank'>XML sitemap</a> has been regenerated for all languages.",
array('@url' => $GLOBALS['base_url'] . '/sitemap.xml')));
......@@ -156,7 +147,10 @@ class Simplesitemap {
}
/**
* Gets the sitemap index.
* Generates and returns the sitemap index as string.
*
* @return string
* The sitemap index.
*/
private function get_sitemap_index() {
$generator = new SitemapGenerator();
......@@ -164,23 +158,30 @@ class Simplesitemap {
}
/**
* Gets the sitemap entity type settings.
*/
public function get_entity_types() {
return $this->config->get('entity_types');
}
/**
* Gets the sitemap custom links settings.
* Gets a specific sitemap setting.
*
* @param string $name
* Name of the setting, like 'max_links'.
*
* @return mixed
* The current setting from db or FALSE if setting does not exist.
*/
public function get_custom_links() {
return $this->config->get('custom');
public function get_setting($name) {
$settings = $this->get_config('settings');
return isset($settings[$name]) ? $settings[$name] : FALSE;
}
/**
* Gets other sitemap settings.
* Saves a specific sitemap setting to db.
*
* @param $name
* Setting name, like 'max_links'.
* @param $setting
* The setting to be saved.
*/
public function get_settings() {
return $this->config->get('settings');
public function save_setting($name, $setting) {
$settings = $this->get_config('settings');
$settings[$name] = $setting;
$this->save_config('settings', $settings);
}
}
......@@ -30,6 +30,6 @@ class SimplesitemapManager extends DefaultPluginManager {
parent::__construct('Plugin/LinkGenerator', $namespaces, $module_handler, 'Drupal\simplesitemap\LinkGeneratorInterface', 'Drupal\simplesitemap\Annotation\LinkGenerator');
$this->alterInfo('simplesitemap_link_generators_info');
$this->setCacheBackend($cache_backend, 'simplesitemap_flavors');
$this->setCacheBackend($cache_backend, 'simplesitemap_link_generators');
}
}
......@@ -72,7 +72,6 @@ class SitemapGenerator {
$this->generate_entity_paths();
$this->generate_urls_from_paths();
$timestamp = time();
$sitemaps = array();
// Create sitemap chunks according to the max_links setting.
......@@ -80,7 +79,7 @@ class SitemapGenerator {
foreach(array_chunk($this->links, $max_links) as $sitemap_id => $sitemap_links) {
$sitemaps[] = (object)[
'sitemap_string' => $this->generate_sitemap_chunk($sitemap_links),
'sitemap_created' => $timestamp,
'sitemap_created' => REQUEST_TIME,
];
}
}
......@@ -88,7 +87,7 @@ class SitemapGenerator {
else {
$sitemaps[] = (object)[
'sitemap_string' => $this->generate_sitemap_chunk($this->links),
'sitemap_created' => $timestamp,
'sitemap_created' => REQUEST_TIME,
];
}
return $sitemaps;
......@@ -199,13 +198,14 @@ class SitemapGenerator {
}
/**
* Makes all entity type link generating plugins add their paths.
* Lets all simple sitemap plugins add their paths to the sitemap.
*/
private function generate_entity_paths() {
$manager = \Drupal::service('plugin.manager.simplesitemap');
$plugins = $manager->getDefinitions();
// Let all simplesitemap plugins generate add their links to the sitemap.
foreach ($plugins as $link_generator_plugin) {
if (isset($this->entity_types[$link_generator_plugin['id']])) {
$instance = $manager->createInstance($link_generator_plugin['id']);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment