diff --git a/admin/admin.php b/admin/admin.php index e2067d5..9e56b46 100644 --- a/admin/admin.php +++ b/admin/admin.php @@ -356,7 +356,7 @@ function gcmi_update_table( $fname ) { } $tmp_table_name = $database_file_info[ $id ]['table_name'] . '_tmp'; GCMI_Activator::create_db_table( $database_file_info[ $id ]['name'], $tmp_table_name ); - $csv_file_path = $download_temp_dir . '/' . $database_file_info[ $id ]['featured_csv']; + $csv_file_path = $download_temp_dir . $database_file_info[ $id ]['featured_csv']; GCMI_Activator::convert_file_charset( $csv_file_path, $database_file_info[ $id ]['orig_encoding'] ); GCMI_Activator::prepare_file( $csv_file_path ); GCMI_Activator::populate_db_table( @@ -369,7 +369,7 @@ function gcmi_update_table( $fname ) { $error_title = esc_html__( 'Error importing data into database', 'campi-moduli-italiani' ); $error_message = '

' . $error_title . '


'; /* translators: %1$s: the data name; %2$s: the db table name. */ - $error_message .= esc_html( sprintf( __( 'Unable to import %1$s into %2$s', 'campi-moduli-italiani' ), $csv_file_path, self::$database_file_info[ $i ]['table_name'] ) ) . '
'; + $error_message .= esc_html( sprintf( __( 'Unable to import %1$s into %2$s', 'campi-moduli-italiani' ), $csv_file_path, GCMI_Activator::$database_file_info[ $id ]['table_name'] ) ) . '
'; $str = htmlspecialchars( print_r( $wpdb->last_error, true ), ENT_QUOTES ) . '
' . esc_html__( 'Last executed query:', 'campi-moduli-italiani' ); $query = htmlspecialchars( $wpdb->last_query, ENT_QUOTES ); diff --git a/admin/class-gcmi-activator.php b/admin/class-gcmi-activator.php index 9b64f3a..131bb0c 100644 --- a/admin/class-gcmi-activator.php +++ b/admin/class-gcmi-activator.php @@ -657,7 +657,7 @@ public static function make_tmp_dwld_dir() { if ( ! wp_mkdir_p( "$tmp_dir" ) ) { return false; } else { - return $tmp_dir; + return $tmp_dir . '/'; } } @@ -890,18 +890,18 @@ public static function create_db_table( $name, $table ) { 'i_den_unita_territoriale varchar(255) NOT NULL, ' . 'i_cod_tipo_unita_territoriale TINYINT(1) NOT NULL, ' . 'i_flag_capoluogo TINYINT(1) NOT NULL, ' . - 'i_sigla_automobilistica varchar(10) NOT NULL, ' . + 'i_sigla_automobilistica char(2) NOT NULL, ' . 'i_cod_comune_num int(6) NOT NULL, ' . 'i_cod_comune_num_2010_2016 int(6) NOT NULL, ' . 'i_cod_comune_num_2006_2009 int(6) NOT NULL, ' . 'i_cod_comune_num_1995_2005 int(6) NOT NULL, ' . 'i_cod_catastale char(4) NOT NULL, ' . - 'i_nuts1_2010 char(3) NOT NULL, ' . - 'i_nuts2_2010 char(4) NOT NULL, ' . - 'i_nuts3_2010 char(5) NOT NULL, ' . 'i_nuts1_2021 char(3) NOT NULL, ' . 'i_nuts2_2021 char(4) NOT NULL, ' . 'i_nuts3_2021 char(5) NOT NULL, ' . + 'i_nuts1_2024 char(3) NOT NULL, ' . + 'i_nuts2_2024 char(4) NOT NULL, ' . + 'i_nuts3_2024 char(5) NOT NULL, ' . 'PRIMARY KEY (id), ' . 'INDEX `i_cod_comune` (`i_cod_comune`) ' . ') %2$s', @@ -917,7 +917,7 @@ public static function create_db_table( $name, $table ) { 'CREATE TABLE IF NOT EXISTS %1$s ( ' . 'id INT(11) NOT NULL AUTO_INCREMENT, ' . 'i_anno_var YEAR(4) NOT NULL, ' . - 'i_sigla_automobilistica varchar(10) NOT NULL, ' . + 'i_sigla_automobilistica char(2) NOT NULL, ' . 'i_cod_unita_territoriale char(3) NOT NULL, ' . 'i_cod_comune char(6) NOT NULL, ' . 'i_denominazione_full varchar(255) NOT NULL, ' . @@ -1138,6 +1138,20 @@ private static function add_trailing_zeroes( $string, $len ) { } return sprintf( '%0' . strval( $len ) . 's', $string ); } + + /** + * Tronca le stringhe ad un numero massimo di caratteri + * + * @param string $string La stringa + * @param int $len Numero massimo di caratteri ammesso + * @return string + */ + private static function truncate( $string, $len ) { + if ( $len < strlen( $string ) ) { + return substr( $string, 0, $len ); + } + return $string; + } /** * Populates a db table. @@ -1222,18 +1236,18 @@ public static function populate_db_table( $name, $csv_file_path, $table ) { 'i_den_unita_territoriale' => trim( gcmi_safe_strval( $gcmi_dati_line[11] ) ), 'i_cod_tipo_unita_territoriale' => $gcmi_dati_line[12], 'i_flag_capoluogo' => $gcmi_dati_line[13], - 'i_sigla_automobilistica' => trim( gcmi_safe_strval( $gcmi_dati_line[14] ) ), + 'i_sigla_automobilistica' => self::truncate( trim( gcmi_safe_strval( $gcmi_dati_line[14] ) ), 2), 'i_cod_comune_num' => $gcmi_dati_line[15], 'i_cod_comune_num_2010_2016' => $gcmi_dati_line[16], 'i_cod_comune_num_2006_2009' => $gcmi_dati_line[17], 'i_cod_comune_num_1995_2005' => $gcmi_dati_line[18], - 'i_cod_catastale' => $gcmi_dati_line[19], - 'i_nuts1_2010' => $gcmi_dati_line[20], - 'i_nuts2_2010' => $gcmi_dati_line[21], - 'i_nuts3_2010' => $gcmi_dati_line[22], - 'i_nuts1_2021' => $gcmi_dati_line[23], - 'i_nuts2_2021' => $gcmi_dati_line[24], - 'i_nuts3_2021' => $gcmi_dati_line[25], + 'i_cod_catastale' => self::truncate( $gcmi_dati_line[19], 4 ), + 'i_nuts1_2021' => self::truncate( $gcmi_dati_line[20], 3 ), + 'i_nuts2_2021' => self::truncate( $gcmi_dati_line[21], 4 ), + 'i_nuts3_2021' => self::truncate( $gcmi_dati_line[22], 5 ), + 'i_nuts1_2024' => self::truncate( $gcmi_dati_line[23], 3 ), + 'i_nuts2_2024' => self::truncate( $gcmi_dati_line[24], 4 ), + 'i_nuts3_2024' => self::truncate( $gcmi_dati_line[25], 5 ), ), array( '%s', diff --git a/settings.php b/settings.php index e81b78f..18a6377 100644 --- a/settings.php +++ b/settings.php @@ -158,9 +158,10 @@ function gcmi_upgrade() { return; } if ( version_compare( $old_ver, '2.1.5', '<' ) ) { - gcmi_update_db_2024(); + gcmi_update_db_2024_1(); } if ( version_compare( $old_ver, '2.2.0', '<' ) ) { + gcmi_update_db_2024_2(); gcmi_add_index_on_tables(); gcmi_create_unfiltered_views_on_plugin_update(); } @@ -300,8 +301,7 @@ function gcmi_delete_all_views(): void { * * @global type $wpdb */ -function gcmi_update_db_2024() { - global $wpdb; +function gcmi_update_db_2024_1() { require_once ABSPATH . 'wp-admin/includes/upgrade.php'; $queries = array( 'ALTER TABLE ' . GCMI_TABLE_PREFIX . 'comuni_attuali RENAME COLUMN i_nuts1 to i_nuts1_2010', @@ -314,6 +314,23 @@ function gcmi_update_db_2024() { dbDelta( $queries, true ); } +/** + * Aggiorna la tabella comuni_attuali al formato dei dati distribuiti nel 2024 + * + * @global type $wpdb + */ +function gcmi_update_db_2024_2() { + require_once ABSPATH . 'wp-admin/includes/upgrade.php'; + $queries = array( + 'ALTER TABLE ' . GCMI_TABLE_PREFIX . 'comuni_attuali RENAME COLUMN i_nuts1_2021 to i_nuts1_2024', + 'ALTER TABLE ' . GCMI_TABLE_PREFIX . 'comuni_attuali RENAME COLUMN i_nuts2_2021 to i_nuts2_2024', + 'ALTER TABLE ' . GCMI_TABLE_PREFIX . 'comuni_attuali RENAME COLUMN i_nuts3_2021 to i_nuts3_2024', + 'ALTER TABLE ' . GCMI_TABLE_PREFIX . 'comuni_attuali RENAME COLUMN i_nuts1_2010 to i_nuts1_2021', + 'ALTER TABLE ' . GCMI_TABLE_PREFIX . 'comuni_attuali RENAME COLUMN i_nuts2_2010 to i_nuts2_2021', + 'ALTER TABLE ' . GCMI_TABLE_PREFIX . 'comuni_attuali RENAME COLUMN i_nuts3_2010 to i_nuts3_2021', + ); + dbDelta( $queries, true ); +} /** * Adds extra links to the plugin activation page *