Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion web/modules/custom/server_general/drush.services.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
services:
server_general.commands:
class: Drupal\server_general\Commands\ServerGeneralCommands
arguments: ['@entity_type.manager', '@config.factory']
arguments: ['@entity_type.manager', '@config.factory', '@server_general.ai_bot_range_updater']
tags:
- { name: drush.command }
9 changes: 9 additions & 0 deletions web/modules/custom/server_general/server_general.services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,12 @@ services:
arguments: ['@current_route_match', '@entity_type.manager', '@server_general.locked_pages']
tags:
- { name: event_subscriber }
logger.channel.server_general:
parent: logger.channel_base
arguments: ['server_general']
server_general.ai_bot_range_updater:
class: Drupal\server_general\Service\AiBotRangeUpdater
arguments: ['@http_client', '@file_system', '@logger.channel.server_general']
Drupal\server_general\Hook\AiBotRangeCronHook:
class: Drupal\server_general\Hook\AiBotRangeCronHook
arguments: ['@server_general.ai_bot_range_updater']
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\node\NodeInterface;
use Drupal\server_general\Service\AiBotRangeUpdater;
use Drush\Commands\DrushCommands;

/**
Expand All @@ -28,18 +29,28 @@ class ServerGeneralCommands extends DrushCommands {
*/
protected ConfigFactoryInterface $configFactory;

/**
* AI-bot IP-range updater service.
*
* @var \Drupal\server_general\Service\AiBotRangeUpdater
*/
protected AiBotRangeUpdater $aiBotRangeUpdater;

/**
* Constructor.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager service.
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The config factory service.
* @param \Drupal\server_general\Service\AiBotRangeUpdater $ai_bot_range_updater
* The AI-bot IP-range updater service.
*/
public function __construct(EntityTypeManagerInterface $entity_type_manager, ConfigFactoryInterface $config_factory) {
public function __construct(EntityTypeManagerInterface $entity_type_manager, ConfigFactoryInterface $config_factory, AiBotRangeUpdater $ai_bot_range_updater) {
parent::__construct();
$this->entityTypeManager = $entity_type_manager;
$this->configFactory = $config_factory;
$this->aiBotRangeUpdater = $ai_bot_range_updater;
}

/**
Expand Down Expand Up @@ -75,4 +86,23 @@ public function setHomepageAfterInstall(): void {
]));
}

/**
* Fetches the latest AI-bot IP ranges and rewrites the verification cache.
*
* Cron refreshes this automatically; use this to seed or force a refresh.
*
* @command server_general:refresh-ai-bot-ranges
* @aliases refresh-ai-bot-ranges
* @usage drush refresh-ai-bot-ranges
* Writes the verification cache from the vendor IP-range lists.
*/
public function refreshAiBotRanges(): void {
$count = $this->aiBotRangeUpdater->refresh();
/** @var \Drush\Log\DrushLoggerManager|null $logger */
$logger = $this->logger();
$logger->success(dt('AI-bot range cache written with @count verifiable user-agent tokens.', [
'@count' => $count,
]));
}

}
34 changes: 34 additions & 0 deletions web/modules/custom/server_general/src/Hook/AiBotRangeCronHook.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace Drupal\server_general\Hook;

use Drupal\Core\Hook\Attribute\Hook;
use Drupal\server_general\Service\AiBotRangeUpdater;

/**
* Cron hook that refreshes the AI-bot IP-range verification cache.
*/
final class AiBotRangeCronHook {

public function __construct(
protected readonly AiBotRangeUpdater $aiBotRangeUpdater,
) {
}

/**
* Implements hook_cron().
*
* Refreshes the cache for the opt-in verification snippet. No-op unless that
* snippet is enabled, which defines AI_BOT_VERIFICATION_CACHE_FILE.
*/
#[Hook('cron')]
public function refreshAiBotRanges(): void {
if (!defined('AI_BOT_VERIFICATION_CACHE_FILE')) {
return;
}
$this->aiBotRangeUpdater->refreshIfStale();
}

}
Loading
Loading