<?php

/**
 * @file
 * Install file for l10n remote updates.
 */

/**
 * Implements hook_schema().
 */
function l10n_update_schema() {
  $schema['l10n_update_project'] = array(
    'description' => 'Update information for project translations.',
    'fields' => array(
      'name' => array(
        'description' => 'A unique short name to identify the project.',
        'type' => 'varchar',
        'length' => '255',
        'not null' => TRUE,
      ),
      'project_type' => array(
        'description' => 'Project type, may be core, module, theme',
        'type' => 'varchar',
        'length' => '50',
        'not null' => TRUE,
      ),
      'core' => array(
        'description' => 'Core compatibility string for this project.',
        'type' => 'varchar',
        'length' => '128',
        'not null' => TRUE,
        'default' => '',
      ),
      'version' => array(
        'description' => 'Human readable name for project used on the interface.',
        'type' => 'varchar',
        'length' => '128',
        'not null' => TRUE,
        'default' => '',
      ),
      'l10n_path' => array(
        'description' => 'Server path this project updates.',
        'type' => 'varchar',
        'length' => '255',
        'not null' => TRUE,
        'default' => '',
      ),
      'status' => array(
        'description' => 'Status flag. If TRUE, translations of this module will be updated.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 1,
      ),
    ),
    'primary key' => array('name'),
  );

  $schema['l10n_update_file'] = array(
    'description' => 'File and download information for project translations.',
    'fields' => array(
      'project' => array(
        'description' => 'A unique short name to identify the project.',
        'type' => 'varchar',
        'length' => '255',
        'not null' => TRUE,
      ),
      'language' => array(
        'description' => 'Reference to the {languages}.language for this translation.',
        'type' => 'varchar',
        'length' => '12',
        'not null' => TRUE,
      ),
      'type' => array(
        'description' => 'File origin: download or localfile',
        'type' => 'varchar',
        'length' => '50',
        'not null' => TRUE,
        'default' => '',
      ),
      'filename' => array(
        'description' => 'Link to translation file for download.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'fileurl' => array(
        'description' => 'Link to translation file for download.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'uri' => array(
        'description' => 'File system path for importing the file.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'timestamp' => array(
        'description' => 'Unix timestamp of the time the file was downloaded or saved to disk. Zero if not yet downloaded',
        'type' => 'int',
        'not null' => FALSE,
        'disp-width' => '11',
        'default' => 0,
      ),
      'version' => array(
        'description' => 'Version tag of the downloaded file.',
        'type' => 'varchar',
        'length' => '128',
        'not null' => TRUE,
        'default' => '',
      ),
      'status' => array(
        'description' => 'Status flag. TBD',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 1,
      ),
      'last_checked' => array(
        'description' => 'Unix timestamp of the last time this translation was downloaded from or checked at remote server and confirmed to be the most recent release available.',
        'type' => 'int',
        'not null' => FALSE,
        'disp-width' => '11',
        'default' => 0,
      ),
    ),
    'primary key' => array('project', 'language'),
  );

  return $schema;
}

/**
 * Implements hook_schema_alter().
 */
function l10n_update_schema_alter(&$schema) {
  $schema['locales_target']['fields']['l10n_status'] = array(
    'type' => 'int',
    'not null' => TRUE,
    'default' => 0,
    'description' => 'Boolean indicating whether the translation is custom to this site.',
  );
}

/**
 * Implements hook_install().
 */
function l10n_update_install() {
  db_add_field('locales_target', 'l10n_status', array('type' => 'int', 'not null' => TRUE, 'default' => 0));
  variable_set('l10n_update_rebuild_projects', 1);

  // Create the translation directory. We try different alternative paths as the
  // default may not always be writable.
  $directories = array(
    variable_get('l10n_update_download_store', L10N_UPDATE_DEFAULT_TRANSLATION_PATH),
    variable_get('file_public_path', conf_path() . '/files') . '/translations',
    'sites/default/files/translations',
  );
  foreach ($directories as $directory) {
    if (file_prepare_directory($directory, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS)) {
      variable_set('l10n_update_download_store', $directory);
      return;
    }
  }
  watchdog('l10n_update', 'The directory %directory does not exist or is not writable.', array('%directory' => $directories[0]), WATCHDOG_ERROR);
  drupal_set_message(t('The directory %directory does not exist or is not writable.', array('%directory' => $directories[0])), 'error');
}

/**
 * Implements hook_uninstall().
 */
function l10n_update_uninstall() {
  db_drop_field('locales_target', 'l10n_status');

  variable_del('l10n_update_check_disabled');
  variable_del('l10n_update_default_filename');
  variable_del('l10n_update_default_update_url');
  variable_del('l10n_update_import_enabled');
  variable_del('l10n_update_import_mode');
  variable_del('l10n_update_check_frequency');
  variable_del('l10n_update_last_check');
  variable_del('l10n_update_download_store');
  variable_del('l10n_update_check_mode');
}

/**
 * Implements hook_requirements().
 */
function l10n_update_requirements($phase) {
  $requirements = array();
  if ($phase == 'runtime') {
    $available_updates = array();
    $untranslated = array();
    $languages = l10n_update_translatable_language_list();

    if ($languages) {
      // Determine the status of the translation updates per language.
      $status = l10n_update_get_status();
      if ($status) {
        foreach ($status as $project) {
          foreach ($project as $langcode => $project_info) {
            if (empty($project_info->type)) {
              $untranslated[$langcode] = $languages[$langcode];
            }
            elseif ($project_info->type == L10N_UPDATE_LOCAL || $project_info->type == L10N_UPDATE_REMOTE) {
              $available_updates[$langcode] = $languages[$langcode];
            }
          }
        }

        if ($available_updates || $untranslated) {
          if ($available_updates) {
            $requirements['l10n_update'] = array(
              'title' => 'Translation update status',
              'value' => l(t('Updates available'), 'admin/config/regional/translate/update'),
              'severity' => REQUIREMENT_WARNING,
              'description' => t('Updates available for: @languages. See the <a href="!updates">Translate interface update</a> page for more information.', array('@languages' => implode(', ', $available_updates), '!updates' => url('admin/config/regional/translate/update'))),
            );
          }
          else {
            $requirements['l10n_update'] = array(
              'title' => 'Translation update status',
              'value' => t('Missing translations'),
              'severity' => REQUIREMENT_INFO,
              'description' => t('Missing translations for: @languages. See the <a href="!updates">Translate interface update</a> page for more information.', array('@languages' => implode(', ', $untranslated), '!updates' => url('admin/config/regional/translate/update'))),
            );
          }
        }
        else {
          $requirements['l10n_update'] = array(
            'title' => 'Translation update status',
            'value' => t('Up to date'),
            'severity' => REQUIREMENT_OK,
          );
        }
      }
      else {
        $requirements['locale_translation'] = array(
          'title' => 'Translation update status',
          'value' => l(t('Can not determine status'), 'admin/config/regional/translate/update'),
          'severity' => REQUIREMENT_WARNING,
          'description' => t('No translation status is available. See the <a href="!updates">Translate interface update</a> page for more information.', array('!updates' => url('admin/config/regional/translate/update'))),
        );
      }
    }
  }
  if ($phase == 'update') {
    // Make sure the 'translations' stream wrapper class gets registered.
    // This is needed when upgrading to 7.x-2.x.
    if (!class_exists('TranslationsStreamWrapper')) {
      registry_rebuild();
    }
  }
  return $requirements;
}

/**
 * Converts D8 style translations strings into D7 format.
 *
 * Drupal 8 stores plurals (both for source and translation strings) as one
 * string, where the plural forms are separated by a special character. By
 * mistake this plural handling was also back ported. This function converts any
 * D8 style plural into a D7 style plural. It used the following strategy.
 * - Any locales_source record that contains the separator character is
 *   converted.
 * - All these records are deleted, except those of which the translation was
 *   edited.
 * - Existing translations will only be overwritten by converted translations
 *   if the import behaviour setting (admin/config/regional/language/update)
 *   allows that.
 *
 * @param integer $limit
 *   The maximum number of records to process. 0 is all records.
 * @param array $report
 *   Array containing import results.
 *
 * @return boolean
 *   Returns true conversion
 * a keyed array of log messages. Available keys:
 *   - message: Translated message
 *   - status: Message status. See drush_log() for supported values.
 */
function l10n_update_d8_plural_conversion($start = 0, $length = 0, array &$report) {
  module_load_include('inc', 'l10n_update', 'l10n_update.translation');
  require_once DRUPAL_ROOT . '/includes/locale.inc';
  $processed = array();

  $plurals = l10n_update_get_d8_plural_strings($start, $length);
  $finished = count($plurals) < $length;
  if (empty($plurals)) {
    return $finished;
  }

  $mode = variable_get('l10n_update_import_mode', LOCALE_IMPORT_OVERWRITE);
  $writer = new PoDatabaseWriter();
  $writer->setOptions(array(
    'overwrite_options' => array(
      'not_customized' => $mode != LOCALE_IMPORT_KEEP,
      'customized' => $mode == LOCALE_IMPORT_OVERWRITE,
    ),
    'customized' => L10N_UPDATE_NOT_CUSTOMIZED
  ));
  $writer_report = isset($report['writer']) ? $report['writer'] : array();
  $writer->setReport($writer_report);

  foreach ($plurals as $plural) {

    // Extract data from source and target strings.
    $sources = explode(L10N_UPDATE_PLURAL_DELIMITER, $plural->source);
    $translations = explode(L10N_UPDATE_PLURAL_DELIMITER, $plural->translation);

    $writer->setLangcode($plural->language);
    $item = new PoItem();
    $item->setContext($plural->context);
    $item->setSource($sources);
    $item->setTranslation($translations);
    $item->setPlural(TRUE);
    $item->setLangcode($plural->langcode);
    $item->setTextgroup($plural->textgroup);
    $writer->writeItem($item);

    // Collect plurals to be deleted. In the rare case that a translation was
    // modified, we will convert it but not delete the custom translation.
    if (!$plural->l10n_status) {
      $report['results']['lids'][] = $plural->lid;
    }
    $report['results']['languages'][$plural->language] = $plural->language;
  }

  $report['writer'] = $writer->getReport();
  unset($report['writer']['strings']);

  return $finished;
}

/**
 * Gets D8 plural style strings.
 *
 * @param integer $start
 *   Offset of records to load.
 * @param integer $length
 *   Maximum number of records to load.
 *
 * @return array|bool
 *   An array of data from source and translation strings of which the source
 *   string contains a Drupal 8 style plural delimiter. Returns false if not
 *   found.
 */
function l10n_update_get_d8_plural_strings($start = 0, $length = 0) {
  $translation_strings = FALSE;

  $query = db_select('locales_source', 'ls');
  $query->fields('ls', array('lid', 'location', 'textgroup', 'source', 'context', 'version'));
  $query->fields('lt', array('translation', 'language', 'plid', 'plural', 'l10n_status'));
  $query->innerJoin('locales_target', 'lt', 'ls.lid = lt.lid');
  $query->condition('source','%' . db_like(L10N_UPDATE_PLURAL_DELIMITER) . '%', 'LIKE');
  if ($length) {
    $query->range($start, $length);
  }
  $results = $query->execute();

  if ($results->rowCount()) {
    foreach ($results as $result) {
      $translation_strings[] = $result;
    }
  }

  return $translation_strings;
}

/**
 * Clean-up after plural string conversion.
 *
 * @param $results
 *   Batch results array.
 */
function l10n_update_finish_d8_plural_strings($results) {
  require_once DRUPAL_ROOT . '/includes/locale.inc';

  if ($results) {
    // Delete converted D8 style translations.
    if (isset($results['lids'])) {
      foreach ($results['lids'] as $lids) {
        db_delete('locales_source')->condition('lid', $lids)->execute();
        db_delete('locales_target')->condition('lid', $lids)->execute();
      }
    }

    // Clear caches if translations are modified.
    if (isset($results['languages'])) {
      cache_clear_all('locale:', 'cache', TRUE);
      foreach ($results['languages'] as $langcode) {
        _locale_invalidate_js($langcode);
      }
    }
  }
}

/**
 * Rename filepath to uri in {l10n_update_file} table.
 */
function l10n_update_update_7001() {
  // Only do this update if the field exists from D6.
  // If it doesn't, we've got a pure D7 site that doesn't need it.
  if (db_field_exists('l10n_update_file', 'filepath')) {
    db_change_field('l10n_update_file', 'filepath', 'uri', array(
      'description' => 'File system path for importing the file.',
      'type' => 'varchar',
      'length' => 255,
      'not null' => TRUE,
      'default' => '',
    ));
  }
}

/**
 * Delete 'last_updated' field from {l10n_update_file} table.
 */
function l10n_update_update_7002() {
  db_drop_field('l10n_update_file', 'last_updated');
}

/**
 * Delete 'import_date' field from {l10n_update_file} table.
 */
function l10n_update_update_7003() {
  db_drop_field('l10n_update_file', 'import_date');
}

/**
 * Create {cache_l10n_update} table.
 */
function l10n_update_update_7004() {
  // Code removed. Table {cache_l10n_update} is no longer used.
}

/**
 * Migration to 7.x-2.x branch.
 */
function l10n_update_update_7200() {
  // Make sure the 'translations' stream wrapper class gets registered.
  if (!class_exists('TranslationsStreamWrapper')) {
    registry_rebuild();
  }

  // If no translation directory was set, set one here. We try different
  // alternative paths as the default may not always be writable.
  if (!variable_get('l10n_update_download_store', '')) {
    $directories = array(
      variable_get('l10n_update_download_store', L10N_UPDATE_DEFAULT_TRANSLATION_PATH),
      variable_get('file_public_path', conf_path() . '/files') . '/translations',
      'sites/default/files/translations',
    );
    $directory_created = FALSE;
    foreach ($directories as $directory) {
      if (file_prepare_directory($directory, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS)) {
        variable_set('l10n_update_download_store', $directory);
        $directory_created = TRUE;
        break;
      }
    }
    if (!$directory_created) {
      watchdog('l10n_update', 'The directory %directory does not exist or is not writable.', array('%directory' => $directories[0]), WATCHDOG_ERROR);
      drupal_set_message(t('The directory %directory does not exist or is not writable.', array('%directory' => $directories[0])), 'error');
    }
  }

  // Translation source 'Remote server only' is no longer supported. Use 'Remote
  // and local' instead.
  $mode = variable_get('l10n_update_check_mode', 3);
  // L10N_UPDATE_USE_SOURCE_REMOTE_AND_LOCAL
  if ($mode == 1) {
    variable_set('l10n_update_check_mode', 3);
    // L10N_UPDATE_USE_SOURCE_REMOTE_AND_LOCAL
  }

  // Daily cron updates are no longer supported. Use weekly instead.
  $frequency = variable_get('l10n_update_check_frequency', '0');
  if ($frequency == '1') {
    variable_set('l10n_update_check_frequency', '7');
  }

  // Clean up deprecated variables.
  variable_del('l10n_update_default_server');
  variable_del('l10n_update_default_server_url');
  variable_del('l10n_update_rebuild_projects');
}

/**
 * Sets the default translation files directory.
 */
function l10n_update_update_7201() {
  if (!variable_get('l10n_update_download_store', '')) {
    variable_set('l10n_update_download_store', 'sites/all/translations');
  }
}

/**
 * Removes table {cache_l10n_update}.
 */
function l10n_update_update_7202() {
  if (db_table_exists('cache_l10n_update')) {
    db_drop_table('cache_l10n_update');
  }
}

/**
 * Removes the field 'l10n_server' from table {cache_l10n_update}.
 */
function l10n_update_update_7203() {
  if (db_field_exists('l10n_update_project', 'l10n_server')) {
    db_drop_field('l10n_update_project', 'l10n_server');
  }
}

/**
 * Increase the length of the name column.
 */
function l10n_update_update_7204() {
  $schema = l10n_update_schema();
  db_change_field('l10n_update_project', 'name', 'name', $schema['l10n_update_project']['fields']['name']);
}

/**
 * Increase the length of the name column.
 */
function l10n_update_update_7205() {
  $schema = l10n_update_schema();
  db_change_field('l10n_update_file', 'project', 'project', $schema['l10n_update_file']['fields']['project']);
}

/**
 * Remove 'headers' field from cache table.
 */
function l10n_update_update_7206() {
  if (db_field_exists('cache_l10n_update', 'headers')) {
    db_drop_field('cache_l10n_update', 'headers');
  }
}

/**
 * Migrate D8 style plurals to D7 style.
 */
function l10n_update_update_7207(&$sandbox) {
  $message = NULL;
  $batch_size = 50;

  if (!isset($sandbox['progress'])) {
    $sandbox['progress'] = 0;
    $sandbox['max'] = count(l10n_update_get_d8_plural_strings());
    $sandbox['report'] = array();
  }

  if ($sandbox['max'] == 0) {
    $sandbox['#finished'] = 1;
    return NULL;
  }

  $finished = l10n_update_d8_plural_conversion($sandbox['progress'], $batch_size, $sandbox['report']);
  $sandbox['progress'] += $batch_size;

  // Whether the batch is finished is determined by the result of
  // l10n_update_d8_plural_conversion(). The progress (percentage) is calculated
  if (empty($sandbox['max']) || $finished) {
    $sandbox['#finished'] =  1;
  }
  else {
    $sandbox['#finished'] = $sandbox['progress'] / $sandbox['max'];
  }

  if ($sandbox['#finished'] == 1) {
    l10n_update_finish_d8_plural_strings($sandbox['report']['results']);

    if($sandbox['report']['writer']['skips']) {
      $message = t('Some problems were reported during plural string migration. See <a href="/admin/reports/dblog">Recent log messages</a> for details.');
    }
  }

  return $message;
}

/**
 * Remove the status variable.
 */
function l10n_update_update_7208() {
  $status = variable_get('l10n_update_translation_status');
  cache_set('l10n_update_status', $status);
  variable_del('l10n_update_translation_status');
}
