Verified Commit dc71ae5f authored by Dave Long's avatar Dave Long
Browse files

refactor: #3560398 Move _dblog_get_message_types and dblog_filters to a...

refactor: #3560398 Move _dblog_get_message_types and dblog_filters to a DbLogFilters service and deprecate them

By: nicxvan
By: mstrelan
By: smustgrave
By: dww
By: dagmar
By: longwave
By: dcam
(cherry picked from commit 51c8bbe6)
parent b8243e09
Loading
Loading
Loading
Loading
Loading
+3 −36
Original line number Diff line number Diff line
@@ -2,42 +2,9 @@

/**
 * @file
 */

use Drupal\Core\Logger\RfcLogLevel;

/**
 * Creates a list of database log administration filters that can be applied.
 *
 * @return array
 *   Associative array of filters. The top-level keys are used as the form
 *   element names for the filters, and the values are arrays with the following
 *   elements:
 *   - title: Title of the filter.
 *   - where: The filter condition.
 *   - options: Array of options for the select list for the filter.
 * @deprecated in drupal:11.4.0 and is removed from drupal:12.0.0. Use \Drupal::service(\Drupal\dblog\DbLogFilters::class)->filters() instead.
 * @see https://www.drupal.org/node/3560399
 */
function dblog_filters(): array {
  $filters = [];

  foreach (_dblog_get_message_types() as $type) {
    // phpcs:ignore Drupal.Semantics.FunctionT.NotLiteralString
    $types[$type] = t($type);
  }

  if (!empty($types)) {
    $filters['type'] = [
      'title' => t('Type'),
      'field' => 'w.type',
      'options' => $types,
    ];
  }

  $filters['severity'] = [
    'title' => t('Severity'),
    'field' => 'w.severity',
    'options' => RfcLogLevel::getLevels(),
  ];

  return $filters;
}
@trigger_error(__FILE__ . ' is deprecated in drupal:11.4.0 and is removed from drupal:12.0.0. Use \Drupal::service(\Drupal\dblog\DbLogFilters::class)->filters() instead. See https://www.drupal.org/node/3560399', E_USER_DEPRECATED);
+26 −2
Original line number Diff line number Diff line
@@ -4,13 +4,37 @@
 * @file
 */

use Drupal\dblog\DbLogFilters;

/**
 * Gathers a list of uniquely defined database log message types.
 *
 * @return array
 *   List of uniquely defined database log message types.
 *
 * @deprecated in drupal:11.4.0 and is removed from drupal:13.0.0. Use \Drupal::service(\Drupal\dblog\DbLogFilters::class)->getMessageTypes() instead.
 * @see https://www.drupal.org/node/3560399
 */
function _dblog_get_message_types() {
  return \Drupal::database()->query('SELECT DISTINCT([type]) FROM {watchdog} ORDER BY [type]')
    ->fetchAllKeyed(0, 0);
  @trigger_error(__FUNCTION__ . '() is deprecated in drupal:11.4.0 and is removed from drupal:13.0.0. Use \Drupal::service(\Drupal\dblog\DbLogFilters::class)->getMessageTypes() instead. See https://www.drupal.org/node/3560399', E_USER_DEPRECATED);
  return \Drupal::service(DbLogFilters::class)->getMessageTypes();
}

/**
 * Creates a list of database log administration filters that can be applied.
 *
 * @return array
 *   Associative array of filters. The top-level keys are used as the form
 *   element names for the filters, and the values are arrays with the following
 *   elements:
 *   - title: Title of the filter.
 *   - where: The filter condition.
 *   - options: Array of options for the select list for the filter.
 *
 * @deprecated in drupal:11.4.0 and is removed from drupal:13.0.0. Use \Drupal::service(\Drupal\dblog\DbLogFilters::class)->filters() instead.
 * @see https://www.drupal.org/node/3560399
 */
function dblog_filters(): array {
  @trigger_error(__FUNCTION__ . '() is deprecated in drupal:11.4.0 and is removed from drupal:13.0.0. Use \Drupal::service(\Drupal\dblog\DbLogFilters::class)->filters() instead. See https://www.drupal.org/node/3560399', E_USER_DEPRECATED);
  return \Drupal::service(DbLogFilters::class)->filters();
}
+2 −0
Original line number Diff line number Diff line
@@ -10,3 +10,5 @@ services:
    tags:
      - { name: logger }
      - { name: backend_overridable }
  Drupal\dblog\DbLogFilters:
    autowire: true
+8 −20
Original line number Diff line number Diff line
@@ -13,13 +13,13 @@
use Drupal\Core\Database\Query\SelectInterface;
use Drupal\Core\Database\Query\TableSortExtender;
use Drupal\Core\Datetime\DateFormatterInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Form\FormBuilderInterface;
use Drupal\Core\Logger\RfcLogLevel;
use Drupal\Core\Url;
use Drupal\user\Entity\User;
use Symfony\Component\HttpFoundation\Request;
use Drupal\Core\Link;
use Drupal\dblog\DbLogFilters;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

/**
@@ -48,21 +48,13 @@ class DbLogController extends ControllerBase {
   */
  protected $userStorage;

  /**
   * Constructs a DbLogController object.
   *
   * @param \Drupal\Core\Database\Connection $database
   *   A database connection.
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
   *   A module handler.
   * @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
   *   The date formatter service.
   * @param \Drupal\Core\Form\FormBuilderInterface $form_builder
   *   The form builder service.
   */
  public function __construct(Connection $database, ModuleHandlerInterface $module_handler, DateFormatterInterface $date_formatter, FormBuilderInterface $form_builder) {
  public function __construct(
    Connection $database,
    DateFormatterInterface $date_formatter,
    FormBuilderInterface $form_builder,
    protected readonly DbLogFilters $dbLogFilters,
  ) {
    $this->database = $database;
    $this->moduleHandler = $module_handler;
    $this->dateFormatter = $date_formatter;
    $this->formBuilder = $form_builder;
    $this->userStorage = $this->entityTypeManager()->getStorage('user');
@@ -109,8 +101,6 @@ public function overview(Request $request) {

    $classes = static::getLogLevelClassMap();

    $this->moduleHandler()->loadInclude('dblog', 'admin.inc');

    $build['dblog_filter_form'] = $this->formBuilder()->getForm('Drupal\dblog\Form\DblogFilterForm');

    $header = [
@@ -320,9 +310,7 @@ protected function addFilterToQuery(Request $request, SelectInterface &$query):
      return;
    }

    $this->moduleHandler()->loadInclude('dblog', 'admin.inc');

    $filters = dblog_filters();
    $filters = $this->dbLogFilters->filters();

    // Build the condition.
    $condition_and = $query->getConnection()->condition('AND');
+67 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\dblog;

use Drupal\Core\Database\Connection;
use Drupal\Core\Logger\RfcLogLevel;
use Drupal\Core\StringTranslation\StringTranslationTrait;

/**
 * Filter methods for the dblog module.
 */
class DbLogFilters {

  use StringTranslationTrait;

  public function __construct(
    protected readonly Connection $connection,
  ) {}

  /**
   * Gathers a list of uniquely defined database log message types.
   *
   * @return array
   *   List of uniquely defined database log message types.
   */
  public function getMessageTypes(): array {
    return $this->connection->query('SELECT DISTINCT([type]) FROM {watchdog} ORDER BY [type]')
      ->fetchAllKeyed(0, 0);
  }

  /**
   * Creates a list of database log administration filters that can be applied.
   *
   * @return array
   *   Associative array of filters. The top-level keys are used as the form
   *   element names for the filters, and the values are arrays with the
   *   following elements:
   *   - title: Title of the filter.
   *   - where: The filter condition.
   *   - options: Array of options for the select list for the filter.
   */
  public function filters(): array {
    $filters = [];

    foreach ($this->getMessageTypes() as $type) {
      // phpcs:ignore Drupal.Semantics.FunctionT.NotLiteralString
      $types[$type] = $this->t($type);
    }

    if (!empty($types)) {
      $filters['type'] = [
        'title' => $this->t('Type'),
        'field' => 'w.type',
        'options' => $types,
      ];
    }

    $filters['severity'] = [
      'title' => $this->t('Severity'),
      'field' => 'w.severity',
      'options' => RfcLogLevel::getLevels(),
    ];

    return $filters;
  }

}
Loading