PluginProbe
WP Data Access – App Builder for Tables, Forms, Charts, Maps & Dashboards / 5.5.75
WP Data Access – App Builder for Tables, Forms, Charts, Maps & Dashboards v5.5.75
5.5.83 5.5.82 5.5.81 5.5.80 5.5.79 5.5.77 5.5.76 5.5.75 5.5.73 5.5.72 5.5.22 5.5.23 5.5.29 5.5.3 5.5.31 5.5.32 5.5.34 5.5.35 5.5.36 5.5.37 5.5.4 5.5.40 5.5.41 5.5.42 5.5.43 All 159 releases
wp-data-access / WPDataProjects / Utilities / WPDP_Export_Project.php

WPDP_Export_Project.php in WP Data Access – App Builder for Tables, Forms, Charts, Maps & Dashboards 5.5.75, at WPDataProjects/Utilities/WPDP_Export_Project.php

379 lines 10.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Suppress "error - 0 - No summary was found for this file" on phpdoc generation
5 *
6 * @package WPDataProjects\Utilities
7 */
8
9 namespace WPDataProjects\Utilities {
10
11 use WPDataAccess\WPDA;
12 use WPDataAccess\Data_Dictionary\WPDA_List_Columns_Cache;
13 use WPDataAccess\Plugin_Table_Models\WPDP_Project_Model;
14 use WPDataAccess\Plugin_Table_Models\WPDP_Page_Model;
15 use WPDataAccess\Plugin_Table_Models\WPDP_Project_Design_Table_Model;
16
17 /**
18 * Class WPDP_Export_Project
19 *
20 * @author Peter Schulz
21 * @since 2.0.0
22 */
23 class WPDP_Export_Project {
24
25 /**
26 * Project ID
27 *
28 * @var string
29 */
30 protected $project_id = null;
31
32 /**
33 * Options set to be exported
34 *
35 * @var array
36 */
37 protected $optionsets = array();
38
39 /**
40 * Main method to start export
41 *
42 * This method checks arguments and starts the export according to the arguments provided.
43 *
44 * @since 1.0.0
45 */
46 public function export() {
47
48 // Get arguments.
49 $this->project_id = isset( $_REQUEST['project_id'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['project_id'] ) ) : null; // input var okay.
50
51 if ( null !== $this->project_id ) {
52 // Check if export is allowed.
53 $wp_nonce = isset( $_REQUEST['wpnonce'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['wpnonce'] ) ) : '?'; // input var okay.
54 if ( ! wp_verify_nonce( $wp_nonce, 'wpdp-export-project-' . $this->project_id ) ) {
55 wp_die();
56 }
57 $this->export_tables();
58 } else {
59 $this->wrong_arguments();
60 }
61
62 }
63
64 /**
65 * Export project tables
66 *
67 * @since 2.0.0
68 */
69 protected function export_tables() {
70 $this->header( $this->project_id );
71 $this->db_begin();
72
73 global $wpdb;
74 $this->insert_rows(
75 WPDP_Project_Model::get_base_table_name(),
76 $wpdb->prepare( 'where project_id = %d', array( $this->project_id ) ),
77 WPDP_Project_Model::BASE_TABLE_NAME
78 );
79 $this->insert_rows(
80 WPDP_Page_Model::get_base_table_name(),
81 $wpdb->prepare( 'where project_id = %d', array( $this->project_id ) ),
82 WPDP_Page_Model::BASE_TABLE_NAME
83 );
84
85 if ( count( $this->optionsets ) > 0 ) {//phpcs:ignore - 8.1 proof
86 $this->get_child_optionsets();
87 $this->insert_optionsets();
88 }
89
90 $this->db_end();
91 }
92
93 /**
94 * Set export header (filename)
95 *
96 * @param $project_id
97 */
98 protected function header( $project_id ) {
99 WPDA::sent_header( 'text/plain; charset=utf-8', null, "wpda_project_{$project_id}.sql" );
100 }
101
102 /**
103 * Set MySQL environment
104 *
105 * @since 2.0.0
106 */
107 protected function db_begin() {
108 global $wpdb;
109
110 echo "/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;\n";
111 echo "/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;\n";
112 echo "/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;\n";
113 echo '/*!40101 SET NAMES ' . esc_attr( $wpdb->charset ) . " */;\n\n";
114 }
115
116 /**
117 * Write insert into statement
118 *
119 * @param string $table_name Database table name.
120 * @param string $where SQL where clause.
121 *
122 * @since 2.0.0
123 */
124 public function insert_rows( $table_name, $where, $table_name_without_prefix ) {
125
126 global $wpdb;
127
128 $save_suppress_errors = $wpdb->suppress_errors;
129 $wpdb->suppress_errors = true;
130
131 $query = "select * from $table_name $where";
132 $rows = $wpdb->get_results( $query, 'ARRAY_A' ); // phpcs:ignore WordPress.DB.PreparedSQL
133
134 if ( $wpdb->num_rows > 0 ) {
135 // Prepare row export: get column names and data types.
136 $wpda_list_columns = WPDA_List_Columns_Cache::get_list_columns( '', $table_name ); // repository tables only
137 $table_columns = $wpda_list_columns->get_table_columns();
138
139 // Create array for fast column_name based access.
140 $column_data_types = $this->column_data_types( $table_columns );
141
142 // Exports rows.
143 echo "--\n";
144 echo '-- Export table `' . esc_attr( $table_name ) . "`\n";
145 echo "--\n";
146
147 echo "INSERT INTO `{wp_prefix}{$table_name_without_prefix}` "; // phpcs:ignore WordPress.Security.EscapeOutput
148
149 $process_first_row = true;
150 foreach ( $rows[0] as $column_name => $column_value ) {
151 if (
152 ! ( WPDP_Project_Model::get_base_table_name() === $table_name && 'project_id' === $column_name ) &&
153 ! ( WPDP_Page_Model::get_base_table_name() === $table_name && 'page_id' === $column_name )
154 ) {
155 echo $process_first_row ? '(' : ', ';
156 echo '`' . esc_attr( $column_name ) . '`';
157
158 $process_first_row = false;
159 }
160 }
161
162 echo ') VALUES ';
163
164 $first_row = true;
165 foreach ( $rows as $row ) {
166 if ( ! $first_row ) {
167 echo ',';
168 } else {
169 $first_row = false;
170 }
171 echo "\n(";
172
173 $keys = array_keys( $row );//phpcs:ignore - 8.1 proof
174 $last_column = end( $keys );//phpcs:ignore - 8.1 proof
175 foreach ( $row as $column_name => $column_value ) {
176 if (
177 ! ( WPDP_Project_Model::get_base_table_name() === $table_name && 'project_id' === $column_name ) &&
178 ! ( WPDP_Page_Model::get_base_table_name() === $table_name && 'page_id' === $column_name )
179 ) {
180 if ( $this->is_numeric( $column_data_types[ $column_name ] ) ) {
181 if ( WPDP_Page_Model::get_base_table_name() === $table_name && 'project_id' === $column_name ) {
182 echo '@PROJECT_ID';
183 } else {
184 if ( null === $column_value ) {
185 echo 'null';
186 } else {
187 echo esc_attr( $column_value );
188 }
189 }
190 } else {
191 if (
192 (
193 (
194 WPDP_Page_Model::get_base_table_name() === $table_name &&
195 'page_schema_name' === $column_name
196 ) ||
197 (
198 WPDP_Project_Design_Table_Model::get_base_table_name() === $table_name &&
199 'wpda_schema_name' === $column_name
200 )
201 ) &&
202 ( '' === $column_value || $wpdb->dbname === $column_value )
203 ) {
204 echo "'{wp_schema}'";
205 } else {
206 if ( null === $column_value ) {
207 echo 'null';
208 } else {
209 echo "'" . esc_sql( $column_value ) . "'";
210 }
211 }
212 }
213 if ( $column_name !== $last_column ) {
214 echo ',';
215 }
216 }
217 }
218
219 echo ')';
220
221 if ( WPDP_Page_Model::get_base_table_name() === $table_name ) {
222 $this->add_optionset(
223 $row['page_schema_name'],
224 $row['page_table_name'],
225 $row['page_setname']
226 );
227 }
228 }
229
230 echo ';';
231 if ( WPDP_Project_Model::get_base_table_name() === $table_name ) {
232 echo "\n";
233 echo 'SET @PROJECT_ID = LAST_INSERT_ID();';
234 }
235 echo "\n\n";
236 } else {
237 // Empty table, nothing to export.
238 echo "--\n";
239 echo '-- No rows to export from empty table `' . esc_attr( $table_name ) . "`\n";
240 echo "--\n\n";
241 }
242
243 $wpdb->suppress_errors = $save_suppress_errors;
244
245 }
246
247 protected function add_optionset( $schema_name, $table_name, $setname ) {
248 if ( ! isset( $this->optionsets[ $schema_name ][ $table_name ][ $setname ] ) ) {
249 $this->optionsets[ $schema_name ][ $table_name ][ $setname ] = true;
250 }
251 }
252
253 protected function insert_optionsets() {
254 $where = 'where ';
255 foreach ( $this->optionsets as $schema_name => $tables ) {
256 $where .= "(`wpda_schema_name`='{$schema_name}' and (";
257 $first_optionset = true;
258 foreach ( $tables as $table_name => $optionsets ) {
259 foreach ( $optionsets as $optionset => $dummy ) {
260 if ( ! $first_optionset ) {
261 $where .= ' or ';
262 }
263 $where .= "(`wpda_table_name`='{$table_name}' and `wpda_table_setname`='$optionset')";
264 $first_optionset = false;
265 }
266 }
267 $where .= '))';
268 }
269
270 $this->insert_rows(
271 WPDP_Project_Design_Table_Model::get_base_table_name(),
272 $where,
273 WPDP_Project_Design_Table_Model::BASE_TABLE_NAME
274 );
275 }
276
277 protected function get_child_optionsets() {
278 foreach ( $this->optionsets as $schema_name => $tables ) {
279 foreach ( $tables as $table_name => $optionsets ) {
280 foreach ( $optionsets as $optionset => $dummy ) {
281 $set = WPDP_Project_Design_Table_Model::static_query( $schema_name, $table_name, $optionset );
282 if ( isset( $set->relationships ) ) {
283 foreach ( $set->relationships as $relationship ) {
284 if ( isset( $relationship->relation_type ) ) {
285 if ( '1n' === $relationship->relation_type ) {
286 if ( isset( $relationship->target_table_name ) ) {
287 $this->add_optionset(
288 $schema_name,
289 $relationship->target_table_name,
290 $optionset
291 );
292 }
293 } elseif ( 'nm' === $relationship->relation_type ) {
294 if ( isset( $relationship->relation_table_name ) ) {
295 $this->add_optionset(
296 $schema_name,
297 $relationship->relation_table_name,
298 $optionset
299 );
300 }
301 }
302 }
303 }
304 }
305 }
306 }
307 }
308 }
309
310 /**
311 * Save column data types
312 *
313 * This method creates a named array for all column names of a table in form:
314 * 'column_name' => 'data_type'
315 *
316 * Argument $table_columns can be retrieved from WPDA_List_Columns->set_table_columns(). It must be prepared
317 * however with the idea that the instance of WPDA_List_Columns can be reused for best performance.
318 *
319 * In fact this is just an array conversion.
320 *
321 * @param array $table_columns Column_names and data_types of a table (table name not used here).
322 *
323 * @return array Named array 'column_name' => 'data_type' for all columns in the table.
324 * @since 2.0.0
325 */
326 protected function column_data_types( $table_columns ) {
327
328 $column_data_types = array();
329
330 foreach ( $table_columns as $column_value ) {
331 $column_data_types[ $column_value['column_name'] ] = $column_value['data_type'];
332 }
333
334 return $column_data_types;
335
336 }
337
338 /**
339 * Check if data type is numeric
340 *
341 * @param string $data_type Data type (simple).
342 *
343 * @return bool
344 * @since 2.0.0
345 */
346 protected function is_numeric( $data_type ) {
347
348 return ( 'number' === WPDA::get_type( $data_type ) );
349
350 }
351
352 /**
353 * Set back MySQL environment
354 *
355 * @since 2.0.0
356 */
357 protected function db_end() {
358
359 echo "/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;\n";
360 echo "/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;\n";
361 echo "/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;\n";
362
363 }
364
365 /**
366 * Processing on invalid arguments
367 *
368 * @since 2.0.0
369 */
370 protected function wrong_arguments() {
371
372 wp_die();
373
374 }
375
376 }
377
378 }
379