| 1 |
<?php |
| 2 |
/** |
| 3 |
* Suppress "error - 0 - No summary was found for this file" on phpdoc generation |
| 4 |
* |
| 5 |
* @package WPDataProjects |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace WPDataProjects\Utilities { |
| 9 |
|
| 10 |
use WPDataAccess\WPDA; |
| 11 |
use WPDataAccess\Data_Dictionary\WPDA_List_Columns_Cache; |
| 12 |
|
| 13 |
/** |
| 14 |
* Class WPDP_Export_Project |
| 15 |
* |
| 16 |
* @package WPDataProjects\Utilities |
| 17 |
* @author Peter Schulz |
| 18 |
* @since 2.0.0 |
| 19 |
*/ |
| 20 |
class WPDP_Export_Project { |
| 21 |
|
| 22 |
/** |
| 23 |
* @var null |
| 24 |
*/ |
| 25 |
protected $project_id = null; |
| 26 |
|
| 27 |
/** |
| 28 |
* Main method to start export |
| 29 |
* |
| 30 |
* This method checks arguments and starts the export according to the arguments provided. |
| 31 |
* |
| 32 |
* @since 1.0.0 |
| 33 |
*/ |
| 34 |
public function export() { |
| 35 |
|
| 36 |
// Check access rights. |
| 37 |
if ( WPDA::get_option( WPDA::OPTION_BE_EXPORT_TABLES ) !== 'on' ) { |
| 38 |
// Exporting tables is not allowed. |
| 39 |
wp_die(); |
| 40 |
} |
| 41 |
|
| 42 |
|
| 43 |
// Get arguments. |
| 44 |
$this->project_id = isset( $_REQUEST['project_id'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['project_id'] ) ) : null; // input var okay. |
| 45 |
|
| 46 |
if ( null !== $this->project_id ) { |
| 47 |
// Check if export is allowed. |
| 48 |
$wp_nonce = isset( $_REQUEST['wpnonce'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['wpnonce'] ) ) : '?'; // input var okay. |
| 49 |
if ( ! wp_verify_nonce( $wp_nonce, 'wpdp-export-project-' . $this->project_id ) ) { |
| 50 |
wp_die(); |
| 51 |
} |
| 52 |
$this->export_tables(); |
| 53 |
} else { |
| 54 |
$this->wrong_arguments(); |
| 55 |
} |
| 56 |
|
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Export project tables. |
| 61 |
* |
| 62 |
* @since 2.0.0 |
| 63 |
*/ |
| 64 |
protected function export_tables() { |
| 65 |
$this->header( $this->project_id ); |
| 66 |
$this->db_begin(); |
| 67 |
|
| 68 |
global $wpdb; |
| 69 |
$this->insert_rows( $wpdb->prefix . 'wpdp_project', 'where project_id = ' . $this->project_id ); |
| 70 |
$this->insert_rows( $wpdb->prefix . 'wpdp_page', 'where project_id = ' . $this->project_id ); |
| 71 |
// For now table options have to be exported seperately. |
| 72 |
// $this->insert_rows( $wpdb->prefix . 'wpdp_table' ); |
| 73 |
|
| 74 |
$this->db_end(); |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Set export header (filename) |
| 79 |
* |
| 80 |
* @since 2.0.0 |
| 81 |
*/ |
| 82 |
protected function header( $project_id ) { |
| 83 |
header( 'Content-type: text/plain; charset=utf-8' ); |
| 84 |
header( "Content-Disposition: attachment; filename=wpdp_project_$project_id.sql" ); |
| 85 |
header( 'Pragma: no-cache' ); |
| 86 |
header( 'Expires: 0' ); |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Set MySQL environment |
| 91 |
* |
| 92 |
* @since 2.0.0 |
| 93 |
*/ |
| 94 |
protected function db_begin() { |
| 95 |
global $wpdb; |
| 96 |
|
| 97 |
echo "/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;\n"; |
| 98 |
echo "/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;\n"; |
| 99 |
echo "/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;\n"; |
| 100 |
echo '/*!40101 SET NAMES ' . esc_attr( $wpdb->charset ) . " */;\n\n"; |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Write insert into statement |
| 105 |
* |
| 106 |
* @since 2.0.0 |
| 107 |
* |
| 108 |
* @param string $table_name Database table name. |
| 109 |
* @param string $where SQL where clause. |
| 110 |
*/ |
| 111 |
public function insert_rows( $table_name, $where = '' ) { |
| 112 |
|
| 113 |
global $wpdb; |
| 114 |
|
| 115 |
$save_suppress_errors = $wpdb->suppress_errors; |
| 116 |
$wpdb->suppress_errors = true; |
| 117 |
|
| 118 |
$query = "select * from $table_name $where"; |
| 119 |
$rows = $wpdb->get_results( $query, 'ARRAY_A' ); // WPCS: unprepared SQL OK; db call ok; no-cache ok. |
| 120 |
|
| 121 |
if ( $wpdb->num_rows > 0 ) { |
| 122 |
// Prepare row export: get column names and data types. |
| 123 |
$wpda_list_columns = WPDA_List_Columns_Cache::get_list_columns( '', $table_name ); |
| 124 |
$table_columns = $wpda_list_columns->get_table_columns(); |
| 125 |
|
| 126 |
// Create array for fast column_name based access. |
| 127 |
$column_data_types = $this->column_data_types( $table_columns ); |
| 128 |
|
| 129 |
// Exports rows. |
| 130 |
echo "--\n"; |
| 131 |
echo '-- Export table `' . esc_attr( $table_name ) . "`\n"; |
| 132 |
echo "--\n"; |
| 133 |
|
| 134 |
echo 'INSERT INTO `' . esc_attr( $table_name ) . '` '; |
| 135 |
|
| 136 |
$process_first_row = true; |
| 137 |
foreach ( $rows[0] as $column_name => $column_value ) { |
| 138 |
if ( |
| 139 |
! ( $wpdb->prefix . 'wpdp_project' === $table_name && 'project_id' === $column_name ) && |
| 140 |
! ( $wpdb->prefix . 'wpdp_page' === $table_name && 'page_id' === $column_name ) |
| 141 |
) { |
| 142 |
echo $process_first_row ? '(' : ', '; |
| 143 |
echo '`' . esc_attr( $column_name ) . '`'; |
| 144 |
|
| 145 |
$process_first_row = false; |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
echo ') VALUES '; |
| 150 |
|
| 151 |
$first_row = true; |
| 152 |
foreach ( $rows as $row ) { |
| 153 |
if ( ! $first_row ) { |
| 154 |
echo ','; |
| 155 |
} else { |
| 156 |
$first_row = false; |
| 157 |
} |
| 158 |
echo "\n("; |
| 159 |
|
| 160 |
$keys = array_keys( $row ); |
| 161 |
$last_column = end( $keys ); |
| 162 |
foreach ( $row as $column_name => $column_value ) { |
| 163 |
if ( |
| 164 |
! ( $wpdb->prefix . 'wpdp_project' === $table_name && 'project_id' === $column_name ) && |
| 165 |
! ( $wpdb->prefix . 'wpdp_page' === $table_name && 'page_id' === $column_name ) |
| 166 |
) { |
| 167 |
if ( $this->is_numeric( $column_data_types[ $column_name ] ) ) { |
| 168 |
if ( $wpdb->prefix . 'wpdp_page' === $table_name && 'project_id' === $column_name ) { |
| 169 |
echo '@PROJECT_ID'; |
| 170 |
} else { |
| 171 |
echo esc_attr( $column_value ); |
| 172 |
} |
| 173 |
} else { |
| 174 |
echo "'" . esc_sql( $column_value ) . "'"; |
| 175 |
} |
| 176 |
if ( $column_name !== $last_column ) { |
| 177 |
echo ','; |
| 178 |
} |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
echo ')'; |
| 183 |
} |
| 184 |
|
| 185 |
echo ';'; |
| 186 |
if ( $wpdb->prefix . 'wpdp_project' === $table_name ) { |
| 187 |
echo "\n"; |
| 188 |
echo 'SET @PROJECT_ID = LAST_INSERT_ID();'; |
| 189 |
} |
| 190 |
echo "\n\n"; |
| 191 |
} else { |
| 192 |
// Empty table, nothing to export. |
| 193 |
echo "--\n"; |
| 194 |
echo '-- No rows to export from empty table `' . esc_attr( $table_name ) . "`\n"; |
| 195 |
echo "--\n\n"; |
| 196 |
} |
| 197 |
|
| 198 |
$wpdb->suppress_errors = $save_suppress_errors; |
| 199 |
|
| 200 |
} |
| 201 |
|
| 202 |
/** |
| 203 |
* Save column data types |
| 204 |
* |
| 205 |
* This method creates a named array for all column names of a table in form: |
| 206 |
* 'column_name' => 'data_type' |
| 207 |
* |
| 208 |
* Argument $table_columns can be retrieved from WPDA_List_Columns->set_table_columns(). It must be prepared |
| 209 |
* however with the idea that the instance of WPDA_List_Columns can be reused for best performance. |
| 210 |
* |
| 211 |
* In fact this is just an array conversion. |
| 212 |
* |
| 213 |
* @since 2.0.0 |
| 214 |
* |
| 215 |
* @param array $table_columns Column_names and data_types of a table (table name not used here). |
| 216 |
* @return array Named array 'column_name' => 'data_type' for all columns in the table. |
| 217 |
*/ |
| 218 |
protected function column_data_types( $table_columns ) { |
| 219 |
|
| 220 |
$column_data_types = []; |
| 221 |
|
| 222 |
foreach ( $table_columns as $column_value ) { |
| 223 |
$column_data_types[ $column_value['column_name'] ] = $column_value['data_type']; |
| 224 |
} |
| 225 |
|
| 226 |
return $column_data_types; |
| 227 |
|
| 228 |
} |
| 229 |
|
| 230 |
/** |
| 231 |
* Check if data type is numeric |
| 232 |
* |
| 233 |
* @since 2.0.0 |
| 234 |
* |
| 235 |
* @param string $data_type Data type (simple). |
| 236 |
* @return bool |
| 237 |
*/ |
| 238 |
protected function is_numeric( $data_type ) { |
| 239 |
|
| 240 |
return ( 'number' === WPDA::get_type( $data_type ) ); |
| 241 |
|
| 242 |
} |
| 243 |
|
| 244 |
/** |
| 245 |
* Set back MySQL environment |
| 246 |
* |
| 247 |
* @since 2.0.0 |
| 248 |
*/ |
| 249 |
protected function db_end() { |
| 250 |
|
| 251 |
echo "/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;\n"; |
| 252 |
echo "/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;\n"; |
| 253 |
echo '/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;'; |
| 254 |
|
| 255 |
} |
| 256 |
|
| 257 |
/** |
| 258 |
* Processing on invalid arguments |
| 259 |
* |
| 260 |
* @since 2.0.0 |
| 261 |
*/ |
| 262 |
protected function wrong_arguments() { |
| 263 |
|
| 264 |
wp_die(); |
| 265 |
|
| 266 |
} |
| 267 |
|
| 268 |
} |
| 269 |
|
| 270 |
} |
| 271 |
|