From f4812957fda2c27ef1747317c705518a48604838 Mon Sep 17 00:00:00 2001
From: "Frederic G. MARAND" <fgm@osinet.fr>
Date: Tue, 5 Jun 2018 14:40:26 +0200
Subject: [PATCH] Issue #2968172 by WidgetsBurritos,fgm: Support Drush 9.

- Fix security-related warning in routing files about (needed) open access.
---
 composer.json                                 |  33 +++++
 drush.services.yml                            |   8 ++
 src/Commands/XmlsitemapCommands.php           | 131 ++++++++++++++++++
 xmlsitemap.routing.yml                        |   2 +
 .../xmlsitemap_engines_test.routing.yml       |   1 +
 5 files changed, 175 insertions(+)
 create mode 100644 composer.json
 create mode 100644 drush.services.yml
 create mode 100644 src/Commands/XmlsitemapCommands.php

diff --git a/composer.json b/composer.json
new file mode 100644
index 0000000..dc5c936
--- /dev/null
+++ b/composer.json
@@ -0,0 +1,33 @@
+{
+  "name": "drupal/xmlsitemap",
+  "description": "Creates a sitemap that conforms to the sitemaps.org specification.",
+  "type": "drupal-module",
+  "homepage": "http://drupal.org/project/xmlsitemap",
+  "authors": [
+    {
+      "name": "Dave Reid",
+      "email": "weitzman@tejasa.com",
+      "homepage": "http://www.davereid.net/",
+      "role": "Maintainer"
+    },
+    {
+      "name": "See contributors",
+      "homepage": "https://www.drupal.org/node/190839/committers"
+    }
+  ],
+  "support": {
+    "issues": "https://www.drupal.org/project/issues/xmlsitemap?categories=All",
+    "irc": "irc://irc.freenode.org/drupal-contribute",
+    "source": "https://cgit.drupalcode.org/xmlsitemap/log/?h=8.x-1.x"
+  },
+  "license": "GPL-2.0+",
+  "minimum-stability": "dev",
+  "require": {},
+  "extra": {
+    "drush": {
+      "services": {
+        "drush.services.yml": "^9"
+      }
+    }
+  }
+}
diff --git a/drush.services.yml b/drush.services.yml
new file mode 100644
index 0000000..fe4a77e
--- /dev/null
+++ b/drush.services.yml
@@ -0,0 +1,8 @@
+services:
+  xmlsitemap.commands:
+    class: \Drupal\xmlsitemap\Commands\XmlsitemapCommands
+    arguments:
+      - '@config.factory'
+      - '@module_handler'
+    tags:
+      - { name: drush.command }
diff --git a/src/Commands/XmlsitemapCommands.php b/src/Commands/XmlsitemapCommands.php
new file mode 100644
index 0000000..9ba3cdd
--- /dev/null
+++ b/src/Commands/XmlsitemapCommands.php
@@ -0,0 +1,131 @@
+<?php
+
+namespace Drupal\xmlsitemap\Commands;
+
+use Drupal\Component\Utility\Timer;
+use Drupal\Core\Config\ConfigFactoryInterface;
+use Drupal\Core\Extension\ModuleHandlerInterface;
+use Drush\Commands\DrushCommands;
+
+/**
+ * A Drush commandfile.
+ *
+ * In addition to this file, you need a drush.services.yml
+ * in root of your module, and a composer.json file that provides the name
+ * of the services file to use.
+ *
+ * See these files for an example of injecting Drupal services:
+ *   - http://cgit.drupalcode.org/devel/tree/src/Commands/DevelCommands.php
+ *   - http://cgit.drupalcode.org/devel/tree/drush.services.yml
+ */
+class XmlsitemapCommands extends DrushCommands {
+
+  /**
+   * The config.factory service.
+   *
+   * @var \Drupal\Core\Config\ConfigFactoryInterface
+   */
+  protected $config;
+
+  /**
+   * The module_handler service.
+   *
+   * @var \Drupal\Core\Extension\ModuleHandlerInterface
+   */
+  protected $moduleHandler;
+
+  /**
+   * XmlsitemapCommands constructor.
+   *
+   * @param \Drupal\Core\Config\ConfigFactoryInterface $configFactory
+   *   The config.factory service.
+   * @param \Drupal\Core\Extension\ModuleHandlerInterface $moduleHandler
+   *   The module_handler service.
+   */
+  public function __construct(
+    ConfigFactoryInterface $configFactory,
+    ModuleHandlerInterface $moduleHandler
+  ) {
+    parent::__construct();
+    $this->config = $configFactory->get('xmlsitemap.settings');
+    $this->moduleHandler = $moduleHandler;
+  }
+
+  /**
+   * Regenerate the XML sitemap files.
+   *
+   * @validate-module-enabled xmlsitemap
+   *
+   * @command xmlsitemap:regenerate
+   * @aliases xmlsitemap-regenerate
+   */
+  public function regenerate() {
+    // Run the batch process.
+    Timer::start('xmlsitemap_regenerate');
+    xmlsitemap_run_unprogressive_batch('xmlsitemap_regenerate_batch');
+
+    $vars = [
+      '@timer' => Timer::read('xmlsitemap_regenerate'),
+      '@memory-peak' => format_size(memory_get_peak_usage(TRUE)),
+    ];
+    $this->output()->writeln(dt('XML sitemap files regenerated in @timer ms. Peak memory usage: @memory-peak.', $vars));
+    Timer::stop('xmlsitemap_regenerate');
+  }
+
+  /**
+   * Dump and re-process all possible XML sitemap data, then regenerate files.
+   *
+   * @validate-module-enabled xmlsitemap
+   *
+   * @command xmlsitemap:rebuild
+   * @aliases xmlsitemap-rebuild
+   */
+  public function rebuild() {
+    // Build a list of rebuildable link types.
+    $rebuild_types = xmlsitemap_get_rebuildable_link_types();
+    if (empty($rebuild_types)) {
+      throw new \Exception("No link types are rebuildable.");
+    }
+
+    // Run the batch process.
+    Timer::start('xmlsitemap_rebuild');
+    xmlsitemap_run_unprogressive_batch('xmlsitemap_rebuild_batch', $rebuild_types, TRUE);
+
+    $vars = [
+      '@timer' => Timer::read('xmlsitemap_rebuild'),
+      '@memory-peak' => format_size(memory_get_peak_usage(TRUE)),
+    ];
+    $this->output()->writeln(dt('XML sitemap files rebuilt in @timer ms. Peak memory usage: @memory-peak.', $vars));
+    Timer::stop('xmlsitemap_rebuild');
+  }
+
+  /**
+   * Process un-indexed XML sitemap links.
+   *
+   * @param array $options
+   *   An associative array of options obtained from cli, aliases, config, etc.
+   *
+   * @option limit
+   *   The limit of links of each type to process.
+   * @validate-module-enabled xmlsitemap
+   *
+   * @command xmlsitemap:index
+   * @aliases xmlsitemap-index
+   */
+  public function index(array $options = ['limit' => NULL]) {
+    $limit = (int) ($options['limit'] ?: $this->config->get('batch_limit'));
+    $count_before = db_select('xmlsitemap', 'x')->countQuery()->execute()->fetchField();
+
+    $this->moduleHandler->invokeAll('xmlsitemap_index_links', ['limit' => $limit]);
+
+    $count_after = db_select('xmlsitemap', 'x')->countQuery()->execute()->fetchField();
+
+    if ($count_after == $count_before) {
+      $this->output()->writeln(dt('No new XML sitemap links to index.'));
+    }
+    else {
+      $this->output()->writeln(dt('Indexed @count new XML sitemap links.', ['@count' => $count_after - $count_before]));
+    }
+  }
+
+}
diff --git a/xmlsitemap.routing.yml b/xmlsitemap.routing.yml
index bb03655..e93d19b 100644
--- a/xmlsitemap.routing.yml
+++ b/xmlsitemap.routing.yml
@@ -76,6 +76,7 @@ xmlsitemap.sitemap_xml:
     _controller: '\Drupal\xmlsitemap\Controller\XmlSitemapController::renderSitemapXml'
     _title: 'Sitemap XML'
   requirements:
+    # Access is open because crawlers need to be able to access the sitemap.
     _access: 'TRUE'
 
 xmlsitemap.sitemap_xsl:
@@ -84,4 +85,5 @@ xmlsitemap.sitemap_xsl:
     _controller: '\Drupal\xmlsitemap\Controller\XmlSitemapController::renderSitemapXsl'
     _title: 'Sitemap XSL'
   requirements:
+    # Access is open because crawlers need to be able to access the sitemap XSL.
     _access: 'TRUE'
diff --git a/xmlsitemap_engines/tests/modules/xmlsitemap_engines_test/xmlsitemap_engines_test.routing.yml b/xmlsitemap_engines/tests/modules/xmlsitemap_engines_test/xmlsitemap_engines_test.routing.yml
index 1717da3..07be05f 100644
--- a/xmlsitemap_engines/tests/modules/xmlsitemap_engines_test/xmlsitemap_engines_test.routing.yml
+++ b/xmlsitemap_engines/tests/modules/xmlsitemap_engines_test/xmlsitemap_engines_test.routing.yml
@@ -4,4 +4,5 @@ xmlsitemap_engines_test.ping:
     _controller: '\Drupal\xmlsitemap_engines_test\Controller\XmlSitemapEnginesTestController::render'
     _title: 'Ping'
   requirements:
+    # Access is open because crawlers need to be able to access the ping URL.
     _access: 'TRUE'
-- 
2.17.0

