<commit_before><?php

/**
 * Collection of upgrade steps
 */
class CRM_Correctaddress_Upgrader extends CRM_Correctaddress_Upgrader_Base {

  // By convention, functions that look like "function upgrade_NNNN()" are
  // upgrade tasks. They are executed in order (like Drupal's hook_update_N).

  /**
   * Example: Run an external SQL script when the module is installed
   *
  public function install() {
    $this->executeSqlFile('sql/myinstall.sql');
  }

  /**
   * Example: Run an external SQL script when the module is uninstalled
   *
  public function uninstall() {
   $this->executeSqlFile('sql/myuninstall.sql');
  }

  /**
   * Example: Run a simple query when a module is enabled
   */
  public function enable() {
    CRM_Core_BAO_Setting::setItem('1000', 'Extension', 'nl.sp.correctaddress:version');
  }

  const BATCH_SIZE = 150;

  public function upgrade_1001() {
    $minId = CRM_Core_DAO::singleValueQuery('SELECT min(id) FROM civicrm_address');
    $maxId = CRM_Core_DAO::singleValueQuery('SELECT max(id) FROM civicrm_address');
    for ($startId = $minId; $startId <= $maxId; $startId += self::BATCH_SIZE) {
      $endId = $startId + self::BATCH_SIZE - 1;
      $title = ts('Correct addresses (%1 / %2)', array(
          1 => $startId,
          2 => $maxId,
      ));
      $this->addTask($title, 'correct', $startId, $endId);
    }

    return true;
  }

  public static function correct($startId, $endId) {
    $address = CRM_Core_DAO::executeQuery("SELECT * FROM `civicrm_address` WHERE `country_id` = 1152 AND `id` BETWEEN %1 AND %2", array(1=>array($startId, 'Integer'), 2=>array($endId,'Integer')), true, 'CRM_Core_DAO_Address');
    while ($address->fetch()) {
      self::correctAddress($address);
    }
    return true;
  }

  protected static function correctAddress($address) {
    $info = civicrm_api3('PostcodeNL', 'get', array('postcode' => $address->postal_code, 'huisnummer' => $address->street_number));
    if (isset($info['values']) && is_array($info['values']) && count($info['values']) > 0) {
      return; //aaddress is found in database
    }

    $sql = "SELECT * FROM `civicrm_postcodenl` WHERE `postcode_letter` = %2 AND `postcode_nr` = %1";
    $postcode = preg_replace('/[^\da-z]/i', '', $address->postal_code);
    $postcodeParams[1] = array(substr($postcode, 0, 4), 'String');
    $postcodeParams[2] = array(substr($postcode, 4, 2), 'String');
    $postcodeDao = CRM_Core_DAO::executeQuery($sql, $postcodeParams);
    while($postcodeDao->fetch()) {
      if (stripos($address->street_address, $postcodeDao->adres)===0 && $postcodeDao->adres != $address->street_name) {
        $housenumber = trim(str_ireplace($postcodeDao->adres, '', $address->street_address));
        if (strlen($address->street_unit)) {
          $housenumber = trim(str_replace($address->street_unit, '', $housenumber));
        }
        if (!is_int($housenumber)) {
          $housenumber = $address->street_number;
        }
        $info = civicrm_api3('PostcodeNL', 'get', array('postcode' => $address->postal_code, 'huisnummer' => $housenumber));
        if (isset($info['values']) && is_array($info['values'])) {
          $params = array();
          $params['street_number'] = $housenumber;
          $params['id'] = $address->id;
          civicrm_api3('Address', 'create', $params);

          $params = array();
          CRM_Core_DAO::storeValues($address, $params);
          $params['street_number'] = $housenumber;
          CRM_Postcodenl_Updater::checkAddress($address->id, $params, true);

          return; //aaddress is found in database
        }
      }
    }

  }

  /**
   * Example: Run a simple query when a module is disabled
   *<commit_msg>final version<commit_after><?php

/**
 * Collection of upgrade steps
 */
class CRM_Correctaddress_Upgrader extends CRM_Correctaddress_Upgrader_Base {

  // By convention, functions that look like "function upgrade_NNNN()" are
  // upgrade tasks. They are executed in order (like Drupal's hook_update_N).

  /**
   * Example: Run an external SQL script when the module is installed
   *
  public function install() {
    $this->executeSqlFile('sql/myinstall.sql');
  }

  /**
   * Example: Run an external SQL script when the module is uninstalled
   *
  public function uninstall() {
   $this->executeSqlFile('sql/myuninstall.sql');
  }

  /**
   * Example: Run a simple query when a module is enabled
   */
  public function enable() {
    CRM_Core_BAO_Setting::setItem('1000', 'Extension', 'nl.sp.correctaddress:version');
  }

  const BATCH_SIZE = 150;

  public function upgrade_1001() {
    $minId = CRM_Core_DAO::singleValueQuery('SELECT min(id) FROM civicrm_address');
    $maxId = CRM_Core_DAO::singleValueQuery('SELECT max(id) FROM civicrm_address');
    for ($startId = $minId; $startId <= $maxId; $startId += self::BATCH_SIZE) {
      $endId = $startId + self::BATCH_SIZE - 1;
      $title = ts('Correct addresses (%1 / %2)', array(
          1 => $startId,
          2 => $maxId,
      ));
      $this->addTask($title, 'correct', $startId, $endId);
    }

    return true;
  }

  public static function correct($startId, $endId) {
    $address = CRM_Core_DAO::executeQuery("SELECT * FROM `civicrm_address` WHERE `country_id` = 1152 AND `id` BETWEEN %1 AND %2", array(1=>array($startId, 'Integer'), 2=>array($endId,'Integer')), true, 'CRM_Core_DAO_Address');
    while ($address->fetch()) {
      self::correctAddress($address);
    }
    return true;
  }

  protected static function correctAddress($address) {
    $info = civicrm_api3('PostcodeNL', 'get', array('postcode' => $address->postal_code, 'huisnummer' => $address->street_number));
    if (isset($info['values']) && is_array($info['values']) && count($info['values']) > 0) {
      return; //aaddress is found in database
    }

    $sql = "SELECT * FROM `civicrm_postcodenl` WHERE `postcode_letter` = %2 AND `postcode_nr` = %1";
    $postcode = preg_replace('/[^\da-z]/i', '', $address->postal_code);
    $postcodeParams[1] = array(substr($postcode, 0, 4), 'String');
    $postcodeParams[2] = array(substr($postcode, 4, 2), 'String');
    $postcodeDao = CRM_Core_DAO::executeQuery($sql, $postcodeParams);
    while($postcodeDao->fetch()) {
      if (stripos($address->street_address, $postcodeDao->adres)===0 && $postcodeDao->adres != $address->street_name) {
        $housenumber = trim(str_ireplace($postcodeDao->adres, '', $address->street_address));
        if (strlen($address->street_unit)) {
          $housenumber = trim(str_replace($address->street_unit, '', $housenumber));
        }
        if (!is_numeric($housenumber)) {
          $housenumber = $address->street_number;
        }
        $info = civicrm_api3('PostcodeNL', 'get', array('postcode' => $address->postal_code, 'huisnummer' => $housenumber));
        if (isset($info['values']) && is_array($info['values'])) {
          $params = array();
          $params['street_number'] = $housenumber;
          $params['id'] = $address->id;
          civicrm_api3('Address', 'create', $params);

          $params = array();
          CRM_Core_DAO::storeValues($address, $params);
          $params['street_number'] = $housenumber;
          CRM_Postcodenl_Updater::checkAddress($address->id, $params, true);

          return; //aaddress is found in database
        }
      }
    }

  }

  /**
   * Example: Run a simple query when a module is disabled
   *
{'fraction_non_alphanumeric': 0.1634199134199134, 'fraction_numerical': 0.01001082251082251, 'mean_word_length': 3.586228287841191, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 61, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5876151', 'n_tokens_mistral': 2484, 'n_tokens_neox': 2385, 'n_words': 511}