tmgmt_smartling-8.x-4.11/tmgmt_smartling.install

tmgmt_smartling.install
<?php

/**
 * @file
 * Update function for the tmgmt_smartling module.
 */
use Drupal\Core\Config\FileStorage;
use Drupal\Core\Config\InstallStorage;
use Drupal\Core\Database\Database;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Url;
use Drupal\Core\Utility\UpdateException;
use Drupal\Core\Link;
use Drupal\tmgmt_smartling\SmartlingTranslatorUi;

/**
 * Define needed states.
 *
 * Variable 'tmgmt_smartling.bucket_name' must be unique per installation and
 * must not be deleted when module is being uninstalled.
 */
function tmgmt_smartling_install() {
  if (!Drupal::state()->get('tmgmt_smartling.bucket_name', FALSE)) {
    Drupal::state()->set('tmgmt_smartling.bucket_name', uniqid());
  }

  tmgmt_extension_suit_init_default_config_values();
}

/**
 * Implements hook_requirements().
 */
function tmgmt_smartling_requirements($phase) {
  $requirements = [];

  if ($phase == 'runtime') {
    $smartling_provider_configs = \Drupal::getContainer()
      ->get("tmgmt_smartling.smartling_config_manager")
      ->getAvailableConfigs();

    foreach ($smartling_provider_configs as $smartling_provider_config) {
      $name = $smartling_provider_config->get("name");
      $label = $smartling_provider_config->get("label");
      $user_id = $smartling_provider_config->get('settings.user_id');
      $token_secret = $smartling_provider_config->get('settings.token_secret');

      if (empty($user_id) || empty($token_secret)) {
        $missing_settings = [];

        if (empty($user_id)) {
          $missing_settings[] = 'User ID';
        }

        if (empty($token_secret)) {
          $missing_settings[] = 'Token Secret';
        }

        $requirements["tmgmt_smartling_$name"] = [
          'title' => t('Smartling'),
          'description' => t('Please set up missing settings for "@label" Smartling plugin at @page_url: @settings', [
            '@page_url' => Link::fromTextAndUrl(
              t('Smartling provider settings page'),
              Url::fromUri(
                "internal:/admin/tmgmt/translators/manage/$name",
                [
                  'attributes' => [
                    'target' => '_blank',
                  ],
                ]
              ))->toString(),
            '@settings' => implode(', ', $missing_settings),
            '@label' => $label,
          ]),
          'severity' => REQUIREMENT_ERROR,
        ];
      }
    }

    // Check max execution time limit.
    $current = ini_get("max_execution_time");
    $recommended = 300;

    if ((int) $current !== 0 && $current < $recommended) {
      $requirements['tmgmt_smartling_max_execution_time']['severity'] = REQUIREMENT_WARNING;
      $requirements['tmgmt_smartling_max_execution_time']['description'] = t("Background processes might take time to be done");
      $requirements['tmgmt_smartling_max_execution_time']['value'] = t("PHP max_execution_time is recommended to be set at least @recommended. Current value is @current", [
        "@recommended" => $recommended,
        "@current" => $current
      ]);
      $requirements['tmgmt_smartling_max_execution_time']['title'] = t('Smartling PHP max_execution_time');
    }

    $cronLastRunThreshold = 600;
    $queueThreshold = 250;
    $last_cron_run = time() - \Drupal::state()->get('system.cron_last');
    $show_cron_message = FALSE;
    $show_queue_message = FALSE;

    // Check last cron run time.
    if ($last_cron_run > $cronLastRunThreshold) {
      $show_cron_message = TRUE;
    }

    // Check amount of queue items in each queue.
    $queues = SmartlingTranslatorUi::getSmartlingQueuesDefinitions();

    foreach ($queues as $name => $queue_definition) {
      $queue = \Drupal::service('queue')->get($name);
      $items = $queue->numberOfItems();

      if ($items > $queueThreshold) {
        $show_queue_message = TRUE;

        break;
      }
    }

    // Assemble warning message.
    $message = NULL;

    if ($show_cron_message) {
      $message = t('Last cron run happened more than 10 minutes ago.');
    }

    if ($show_queue_message) {
      $sub_message = t('Some of the Smartling cron queues are overflowed.');
      $message = empty($message) ? $sub_message : $message . ' ' . $sub_message;
    }

    if (!empty($message)) {
      $message .= ' ' . t('Configure your cron job to run once per 5-10 minutes in order to process Smartling queues more effectively. Please visit this <a href=":url" target="_blank">page</a> for more information.', [
        ':url' => 'https://www.drupal.org/docs/administering-a-drupal-site/cron-automated-tasks/cron-automated-tasks-overview',
      ]);

      $requirements['tmgmt_smartling_cron']['severity'] = REQUIREMENT_WARNING;
      $requirements['tmgmt_smartling_cron']['description'] = t("Avoid Smartling queues overflow");
      $requirements['tmgmt_smartling_cron']['value']['#markup'] = $message;
      $requirements['tmgmt_smartling_cron']['title'] = t('Smartling cron last run');
    }
  }

  return $requirements;
}

/**
 * Set default custom_regexp_placeholder string.
 */
function tmgmt_smartling_update_8001() {

  $custom_regexp_placeholder = \Drupal::config('tmgmt.translator.smartling')->get('settings.custom_regexp_placeholder');

  // Update custom_regexp_placeholder if not already set.
  if (empty($custom_regexp_placeholder)) {
    \Drupal::configFactory()
      ->getEditable('tmgmt.translator.smartling')
      ->set('settings.custom_regexp_placeholder', '(@|%|!)[\w-]+')
      ->save(TRUE);
  }
}

/**
 * Add file name into job entries.
 */
function tmgmt_smartling_update_8002() {
  drupal_flush_all_caches();

  // Apply new field to a job entity.
  Drupal::service('entity.definition_update_manager')->applyUpdates();

  Drupal::database()->update('tmgmt_job')
    ->expression(
      'job_file_name',
      'CONCAT(:job, tmgmt_job.tjid, :underscore, tmgmt_job.source_language, :underscore, tmgmt_job.target_language, :extension)',
      [
        ':job' => 'JobID',
        ':underscore' => '_',
        ':extension' => '.xlf',
      ]
    )
    ->execute();

  $translator_ids = \Drupal::configFactory()->listAll('tmgmt.translator');
  foreach ($translator_ids as $id) {
    $config = \Drupal::configFactory()->getEditable($id);

    if ($config && $config->get('plugin') === 'smartling') {
        $config->set('settings.export_format', 'xlf')->save(TRUE);
    }
  }
}

/**
 * Enable needed dependency.
 */
function tmgmt_smartling_update_8003() {
  // Check if dependency exists. Not enabled/disabled, but exists or not.
  $dependency_exists = \Drupal::moduleHandler()->loadInclude('tmgmt_extension_suit', 'module');

  if (!$dependency_exists) {
    throw new UpdateException('Unresolved dependency: tmgmt_extension_suit (missing).');
  }

  // Enable module if it's not enabled yet.
  if (!Drupal::service('module_handler')->moduleExists('tmgmt_extension_suit')) {
    Drupal::service('module_installer')->install(['tmgmt_extension_suit']);
  }
}

/**
 * Migrate users from fake xml (html) to new xml format.
 */
function tmgmt_smartling_update_8004() {
  $translator_ids = \Drupal::configFactory()->listAll('tmgmt.translator');

  foreach ($translator_ids as $id) {
    $config = \Drupal::configFactory()->getEditable($id);

    if ($config && $config->get('plugin') === 'smartling' && $config->get('settings')['export_format'] === 'html') {
      $config->set('settings.export_format', 'xml')->save(TRUE);
    }
  }

  // There are might be affected titles with encoded special chars. Fix it.
  \Drupal::database()->update('node_field_data', [
    'allow_delimiter_in_query' => TRUE,
  ])
    ->expression('title', "REPLACE(title, '&amp;', '&')")
    ->condition('title', '%&amp;%', 'LIKE')
    ->condition('langcode', 'en', '!=')
    ->execute();

  \Drupal::database()->update('node_field_revision', [
    'allow_delimiter_in_query' => TRUE,
  ])
    ->expression('title', 'REPLACE(title, \'&amp;\', \'&\')')
    ->condition('title', '%&amp;%', 'LIKE')
    ->condition('langcode', 'en', '!=')
    ->execute();
}

/**
 * Add new field to a job entity.
 */
function tmgmt_smartling_update_8005() {
  drupal_flush_all_caches();

  Drupal::service('entity.definition_update_manager')->applyUpdates();
}

/**
 * Rebuild the container as services changed.
 */
function tmgmt_smartling_update_8006() {
  drupal_flush_all_caches();
}

/**
 * Rebuild the container as services changed.
 */
function tmgmt_smartling_update_8007() {
  drupal_flush_all_caches();
}

/**
 * Set format to xml.
 */
function tmgmt_smartling_update_8401() {
  $translator_ids = \Drupal::configFactory()->listAll('tmgmt.translator');

  foreach ($translator_ids as $id) {
    $config = \Drupal::configFactory()->getEditable($id);

    if ($config && $config->get('plugin') === 'smartling') {
      $config->set('settings.export_format', 'xml')->save(TRUE);
    }
  }

  Drupal::state()->set('tmgmt_smartling.bucket_name', uniqid());
}

/**
 * Add missing job_file_content_hash column to tmgmt_job table if does not exist yet.
 */
function tmgmt_smartling_update_8402() {
    $entity_definition_manager = Drupal::entityDefinitionUpdateManager();
    $schema = Database::getConnection()->schema();

    if (!$schema->fieldExists("tmgmt_job", "job_file_content_hash")) {
        $field_storage_definition = BaseFieldDefinition::create('string')
            ->setLabel(t('File content hash (md5)'))
            ->setSetting('max_length', 32)
            ->setTranslatable(FALSE);

        $entity_definition_manager
            ->installFieldStorageDefinition('job_file_content_hash', 'tmgmt_job', 'tmgmt_smartling', $field_storage_definition);
    }
}

/**
 * Set default translatable_attributes string.
 */
function tmgmt_smartling_update_8403() {
    $translator_ids = \Drupal::configFactory()->listAll('tmgmt.translator');

    foreach ($translator_ids as $id) {
        $config = \Drupal::configFactory()->getEditable($id);

        if ($config && $config->get('plugin') === 'smartling') {
            $config->set('settings.translatable_attributes', 'title, alt')->save(TRUE);
        }
    }
}

/**
 * Create download by job items action plugin.
 */
function tmgmt_smartling_update_8804() {
  try {
    $plugin_id = 'tmgmt_smartling_download_by_job_items_job_action';
    $plugin_name = 'system.action.tmgmt_smartling_download_by_job_items_job_action';
    $entity_type_manager = \Drupal::entityTypeManager();
    $module_handler = \Drupal::moduleHandler();
    $config_install_path = $module_handler->getModule('tmgmt_smartling')
        ->getPath() . '/' . InstallStorage::CONFIG_INSTALL_DIRECTORY;
    $action_storage = $entity_type_manager->getStorage('action');
    $action = $action_storage->load($plugin_id);

    // Create action if it doesn't exist.
    if (!$action) {
      $storage = new FileStorage($config_install_path);
      $read_file = $storage->read($plugin_name);

      if ($read_file) {
        $entity_type_manager
          ->getStorage('action')
          ->create($read_file)
          ->save();
      }
    }
  }
  catch (Exception $e) {
    throw new UpdateException('Unable to create "tmgmt_smartling_download_by_job_items_job_action" action.');
  }
}

/**
 * Set default exclude_translatable_attributes string.
 */
function tmgmt_smartling_update_8805() {
  $translator_ids = \Drupal::configFactory()->listAll('tmgmt.translator');

  foreach ($translator_ids as $id) {
    $config = \Drupal::configFactory()->getEditable($id);

    if ($config && $config->get('plugin') === 'smartling') {
      $config->set('settings.exclude_translatable_attributes', '')->save(TRUE);
    }
  }
}

/**
 * Set default force_block_for_tags string.
 */
function tmgmt_smartling_update_8806() {
  $translator_ids = \Drupal::configFactory()->listAll('tmgmt.translator');

  foreach ($translator_ids as $id) {
    $config = \Drupal::configFactory()->getEditable($id);

    if ($config && $config->get('plugin') === 'smartling') {
      $config->set('settings.force_block_for_tags', '')->save(TRUE);
    }
  }
}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc