Unverified Commit 5159f17c authored by Dave Reid's avatar Dave Reid Committed by Dave Reid

Issue #2968172 by m4olivei, Dave Reid, fgm, WidgetsBurritos, hanoii: Added support for Drush 9.

parent 8ac0bbc5
......@@ -13,5 +13,15 @@
},
"require-dev": {
"drupal/robotstxt": "^1.0"
},
"suggest": {
"drush/drush": "Command-line regenerating and rebuilding sitemaps."
},
"extra": {
"drush": {
"services": {
"drush.services.yml": "^9"
}
}
}
}
services:
xmlsitemap.commands:
class: \Drupal\xmlsitemap\Commands\XmlSitemapCommands
arguments:
- '@config.factory'
- '@module_handler'
- '@database'
tags:
- { name: drush.command }
<?php
namespace Drupal\xmlsitemap\Commands;
use Drupal\Component\Utility\Timer;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Database\Connection;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drush\Commands\DrushCommands;
/**
* Drush commands for XML sitemap.
*/
class XmlSitemapCommands extends DrushCommands {
/**
* The config.factory service.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
protected $configFactory;
/**
* The module_handler service.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface
*/
protected $moduleHandler;
/**
* Default database connection.
*
* @var \Drupal\Core\Database\Connection
*/
protected $connection;
/**
* XmlSitemapCommands constructor.
*
* @param \Drupal\Core\Config\ConfigFactoryInterface $configFactory
* The config.factory service.
* @param \Drupal\Core\Extension\ModuleHandlerInterface $moduleHandler
* The module_handler service.
* @param \Drupal\Core\Database\Connection $connection
* Default database connection.
*/
public function __construct(ConfigFactoryInterface $configFactory, ModuleHandlerInterface $moduleHandler, Connection $connection) {
parent::__construct();
$this->configFactory = $configFactory;
$this->moduleHandler = $moduleHandler;
$this->connection = $connection;
}
/**
* 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->configFactory->get('xmlsitemap.settings')->get('batch_limit'));
$count_before = $this->connection->select('xmlsitemap', 'x')->countQuery()->execute()->fetchField();
$this->moduleHandler->invokeAll('xmlsitemap_index_links', ['limit' => $limit]);
$count_after = $this->connection->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]));
}
}
}
......@@ -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'
......@@ -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'
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