You are here

class XmlSitemapCommands in XML sitemap 8

Same name and namespace in other branches
  1. 2.x src/Commands/XmlSitemapCommands.php \Drupal\xmlsitemap\Commands\XmlSitemapCommands

Drush commands for XML sitemap.

Hierarchy

  • class \Drupal\xmlsitemap\Commands\XmlSitemapCommands extends \Drush\Commands\DrushCommands

Expanded class hierarchy of XmlSitemapCommands

1 string reference to 'XmlSitemapCommands'
drush.services.yml in ./drush.services.yml
drush.services.yml
1 service uses XmlSitemapCommands
xmlsitemap.commands in ./drush.services.yml
\Drupal\xmlsitemap\Commands\XmlSitemapCommands

File

src/Commands/XmlSitemapCommands.php, line 13

Namespace

Drupal\xmlsitemap\Commands
View source
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() {
    $batch = xmlsitemap_regenerate_batch();
    batch_set($batch);
    drush_backend_batch_process();
  }

  /**
   * 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)) {
      $this
        ->logger()
        ->warning(dt('No link types are rebuildable.'));
    }
    $batch = xmlsitemap_rebuild_batch($rebuild_types, TRUE);
    batch_set($batch);
    drush_backend_batch_process();
  }

  /**
   * 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,
      ]));
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
XmlSitemapCommands::$configFactory protected property The config.factory service.
XmlSitemapCommands::$connection protected property Default database connection.
XmlSitemapCommands::$moduleHandler protected property The module_handler service.
XmlSitemapCommands::index public function Process un-indexed XML sitemap links.
XmlSitemapCommands::rebuild public function Dump and re-process all possible XML sitemap data, then regenerate files.
XmlSitemapCommands::regenerate public function Regenerate the XML sitemap files.
XmlSitemapCommands::__construct public function XmlSitemapCommands constructor.