You are here

class DbHandler in DB Maintenance 8

Same name and namespace in other branches
  1. 7.2 src/Module/Db/DbHandler.php \Drupal\db_maintenance\Module\Db\DbHandler
  2. 2.0.x src/Module/Db/DbHandler.php \Drupal\db_maintenance\Module\Db\DbHandler

DbHandler class.

Hierarchy

  • class \Drupal\db_maintenance\Module\Db\DbHandler

Expanded class hierarchy of DbHandler

3 files declare their use of DbHandler
CommonHookHandler.php in src/Module/Hook/CommonHookHandler.php
CommonHookHandler class.
DbMaintenanceAdminSettings.php in src/Form/DbMaintenanceAdminSettings.php
Contains \Drupal\db_maintenance\Form\DbMaintenanceAdminSettings.
DefaultController.php in src/Controller/DefaultController.php
Contains \Drupal\db_maintenance\Controller\DefaultController.

File

src/Module/Db/DbHandler.php, line 20
DbHandler class.

Namespace

Drupal\db_maintenance\Module\Db
View source
class DbHandler {

  /**
   * Performs the maintenance.
   */
  public static function optimizeTables() {
    $dbs = self::getDatabases();
    foreach ($dbs as $db => $connection) {
      $db_name = $connection['default']['database'];
      $all_tables = ConfigHandler::getProcessAllTables();
      if ($all_tables) {
        $config_tables = self::listTables($db);
      }
      else {
        $config_tables = ConfigHandler::getTableList($db_name);
      }

      // Only proceed if tables are selected for this database.
      if (is_array($config_tables) && count($config_tables) > 0) {
        foreach ($config_tables as $key => $table_name) {

          // Set the database to query.
          $previous = Database::setActiveConnection($db);
          $table_clear = PrefixHandler::clearPrefix($table_name);
          if (Database::getConnection()
            ->schema()
            ->tableExists($table_clear)) {
            $handler = DbServerHandlerFactory::getDbServerHandler();
            $handler
              ->optimizeTable($table_name);
          }
          else {
            WatchdogAdapter::watchdog('db_maintenance', '@table table in @db database was configured to be optimized but does not exist.', array(
              '@db' => $db_name,
              '@table' => $table_name,
            ), LogLevel::NOTICE);
          }

          // Return to the previously set database.
          Database::setActiveConnection($previous);
          WatchdogAdapter::watchdog('db_maintenance', 'Optimized @table table in @db database.', array(
            '@db' => $db_name,
            '@table' => $table_name,
          ), LogLevel::DEBUG);
        }
        if (ConfigHandler::getWriteLog()) {
          $tables = implode(', ', $config_tables);
          WatchdogAdapter::watchdog('db_maintenance', 'Optimized tables in @db database: @tables', array(
            '@db' => $db_name,
            '@tables' => $tables,
          ), LogLevel::INFO);
        }
      }
    }
    ConfigHandler::setCronLastRun(\Drupal::time()
      ->getRequestTime());
  }

  /**
   * Gets a list of all the tables in a database.
   *
   * @param string $db
   *   The name of the database connection to query for tables.
   *
   * @return array
   *   Array representing the tables in the specified database.
   */
  public static function listTables($db) {
    $table_names = array();

    // Set the database to query.
    $previous = Database::setActiveConnection($db);
    $handler = DbServerHandlerFactory::getDbServerHandler();
    $result = $handler
      ->listTables();

    // Return to the previously set database.
    Database::setActiveConnection($previous);
    foreach ($result as $table_name) {
      $table_name = current($table_name);
      $table_names[$table_name] = $table_name;
    }
    return $table_names;
  }

  /**
   * Gets the list of all databases.
   *
   * Use result of this function instead of global $databases.
   */
  public static function getDatabases() {
    $dbs = Database::getAllConnectionInfo();
    return $dbs;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DbHandler::getDatabases public static function Gets the list of all databases.
DbHandler::listTables public static function Gets a list of all the tables in a database.
DbHandler::optimizeTables public static function Performs the maintenance.