PluginProbe
WP Data Access – App Builder for Tables, Forms, Charts, Maps & Dashboards / 5.5.23
WP Data Access – App Builder for Tables, Forms, Charts, Maps & Dashboards v5.5.23
5.5.84 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 All 160 releases
wp-data-access / WPDataAccess / Data_Dictionary / WPDA_Dictionary_Lists.php

WPDA_Dictionary_Lists.php in WP Data Access – App Builder for Tables, Forms, Charts, Maps & Dashboards 5.5.23, at WPDataAccess/Data_Dictionary/WPDA_Dictionary_Lists.php

432 lines 12.1 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 WPDataAccess\Data_Dictionary
7 */
8
9 namespace WPDataAccess\Data_Dictionary {
10
11 use WPDataAccess\Connection\WPDADB;
12 use WPDataAccess\WPDA;
13
14 /**
15 * Class WPDA_Dictionary_Lists
16 *
17 * @author Peter Schulz
18 * @since 1.0.0
19 */
20 class WPDA_Dictionary_Lists {
21
22 /**
23 * List of tables for setting pages
24 *
25 * Returns an array including all tables and views in the WordPress database.
26 *
27 * Do NOT use table access control here! This list is used in the settings forms and must show ALL tables in
28 * the WordPress database.
29 *
30 * @param string $schema_name Database schema name
31 * @param boolean $show_views TRUE = show views, FALSE = hide views.
32 *
33 * @return array List of database tables (and views).
34 * @since 1.0.0
35 */
36 public static function get_tables( $show_views = true, $schema_name = '' ) {
37 $wpdadb = WPDADB::get_db_connection( $schema_name );
38 if ( $wpdadb === null ) {
39 return array();
40 }
41
42 if ( false === $show_views ) {
43 $and = " and table_type != 'VIEW' ";
44 } else {
45 $and = '';
46 }
47
48 $query = $wpdadb->prepare(
49 "
50 select table_name AS table_name,
51 create_time AS create_time,
52 table_rows AS table_rows
53 from information_schema.tables
54 where table_schema = %s
55 $and
56 order by table_name
57 ",
58 array(
59 $wpdadb->dbname,
60 )
61 );
62
63 return $wpdadb->get_results( $query, 'ARRAY_A' ); // phpcs:ignore Standard.Category.SniffName.ErrorCode
64 }
65
66 public static function get_table_row_count_ajax() {
67 if (
68 isset( $_POST['wpda_wpnonce'] ) &&
69 isset( $_POST['wpdaschema_name'] ) &&
70 isset( $_POST['wpdatable_name'] )
71 ) {
72 $wpnonce = sanitize_text_field( wp_unslash( $_REQUEST['wpda_wpnonce'] ) ); // input var okay.
73 $schema_name = sanitize_text_field( wp_unslash( $_REQUEST['wpdaschema_name'] ) ); // input var okay.
74 $table_name = sanitize_text_field( wp_unslash( $_REQUEST['wpdatable_name'] ) ); // input var okay.
75
76 if ( ! wp_verify_nonce( $wpnonce, "wpda-get-row-count-{$table_name}" ) ) {
77 echo json_encode( array() );
78 } else {
79 $wpdadb = WPDADB::get_db_connection( $schema_name );
80
81 if ( $wpdadb === null ) {
82 echo json_encode( array() );
83 } else {
84 $query = 'select count(*) as row_count from `' . str_replace( '`', '', $table_name ) . '`';
85 echo json_encode( $wpdadb->get_results( $query, 'ARRAY_A' ) ); // phpcs:ignore Standard.Category.SniffName.ErrorCode
86 }
87 }
88 } else {
89 echo json_encode( array() );
90 }
91 }
92
93 /**
94 * List all tables in a specific schema
95 *
96 * jQuery usage: action=wpda_get_tables
97 *
98 * @return array
99 */
100 public static function get_tables_ajax() {
101 if ( isset( $_REQUEST['wpdaschema_name'] ) && isset( $_REQUEST['wpda_wpnonce'] ) ) {
102 $wpnonce = sanitize_text_field( wp_unslash( $_REQUEST['wpda_wpnonce'] ) ); // input var okay.
103 if ( ! wp_verify_nonce( $wpnonce, 'wpda-getdata-access-' . WPDA::get_current_user_login() ) ) {
104 echo json_encode( array() );
105 } else {
106 $schema_name = sanitize_text_field( wp_unslash( $_REQUEST['wpdaschema_name'] ) ); // input var okay.
107 $wpdadb = WPDADB::get_db_connection( $schema_name );
108
109 if ( $wpdadb === null ) {
110 echo json_encode( array() );
111 } else {
112 global $wpdb;
113 if ( $wpdb->dbname === $schema_name ) {
114 $table_access = WPDA::get_option( WPDA::OPTION_BE_TABLE_ACCESS );
115 } else {
116 $table_access = get_option( WPDA::BACKEND_OPTIONNAME_DATABASE_ACCESS . $schema_name );
117 if ( false === $table_access ) {
118 $table_access = 'show';
119 }
120 }
121
122 $and = '';
123 if ( 'hide' === $table_access ) {
124 $wp_tables = $wpdb->tables( 'all', true );
125 $and = " and `table_name` not in ('" . implode( "','", $wp_tables ) . "') ";
126 } else if ( 'show' !== $table_access ) {
127 // Show only selected tables and views
128 if ( $wpdb->dbname === $schema_name ) {
129 $tables = WPDA::get_option( WPDA::OPTION_BE_TABLE_ACCESS_SELECTED );
130 } else {
131 $tables = get_option( WPDA::BACKEND_OPTIONNAME_DATABASE_SELECTED . $schema_name );
132 if ( false === $tables ) {
133 $tables = array();
134 }
135 }
136 if ( count( $tables ) > 0 ) {//phpcs:ignore - 8.1 proof
137 $and = " and `table_name` in ('" . implode( "','", $tables ) . "') ";
138 }
139 }
140
141 $hide_views = isset( $_REQUEST['hideviews'] ) && 'TRUE' === $_REQUEST['hideviews'];
142 if ( $hide_views ) {
143 $and .= " and `table_type` != 'VIEW' ";
144 }
145
146 $query = $wpdadb->prepare(
147 "
148 select `table_name` AS table_name
149 from `information_schema`.`tables`
150 where `table_schema` = %s
151 $and
152 order by `table_name`
153 ",
154 array(
155 $wpdadb->dbname,
156 )
157 );
158
159 echo json_encode( $wpdadb->get_results( $query, 'ARRAY_A' ) ); // phpcs:ignore Standard.Category.SniffName.ErrorCode
160 }
161 }
162 } else {
163 echo json_encode( array() );
164 }
165 }
166
167 /**
168 * List of columns for a specific table
169 *
170 * jQuery usage: action=wpda_get_columns
171 *
172 * @return array List of column for a specific table.
173 * @since 1.6.10
174 */
175 public static function get_columns() {
176 if ( isset( $_REQUEST['wpdaschema_name'] ) && isset( $_REQUEST['table_name'] ) && isset( $_REQUEST['wpda_wpnonce'] ) ) {
177 $wpnonce = sanitize_text_field( wp_unslash( $_REQUEST['wpda_wpnonce'] ) ); // input var okay.
178 if ( ! wp_verify_nonce( $wpnonce, 'wpda-getdata-access-' . WPDA::get_current_user_login() ) ) {
179 echo json_encode( array() );
180 } else {
181 $schema_name = isset( $_REQUEST['wpdaschema_name'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['wpdaschema_name'] ) ) : ''; // input var okay.
182 $table_name = sanitize_text_field( wp_unslash( $_REQUEST['table_name'] ) ); // input var okay.
183 $wpdadb = WPDADB::get_db_connection( $schema_name );
184
185 if ( $wpdadb === null ) {
186 echo json_encode( array() );
187 } else {
188 $query = $wpdadb->prepare(
189 '
190 SELECT column_name AS column_name
191 FROM information_schema.columns
192 WHERE table_schema = %s
193 AND table_name = %s
194 ORDER BY ordinal_position
195 ',
196 array(
197 $wpdadb->dbname,
198 $table_name,
199 )
200 );
201
202 echo json_encode( $wpdadb->get_results( $query, 'ARRAY_A' ) ); // phpcs:ignore Standard.Category.SniffName.ErrorCode
203 }
204 }
205 } else {
206 echo json_encode( array() );
207 }
208 }
209
210 public static function get_table_widget_info() {
211 $table_info = array();
212
213 if ( isset( $_POST['wpdaschema_name'] ) && isset( $_POST['table_name'] ) && isset( $_POST['wpda_wpnonce'] ) ) {
214 $wpnonce = sanitize_text_field( wp_unslash( $_POST['wpda_wpnonce'] ) ); // input var okay.
215 if ( wp_verify_nonce( $wpnonce, 'wpda-getdata-access-' . WPDA::get_current_user_login() ) ) {
216 $schema_name = isset( $_POST['wpdaschema_name'] ) ? sanitize_text_field( wp_unslash( $_POST['wpdaschema_name'] ) ) : ''; // input var okay.
217 $table_name = sanitize_text_field( wp_unslash( $_POST['table_name'] ) ); // input var okay.
218 $wpdadb = WPDADB::get_db_connection( $schema_name );
219
220 if ( $wpdadb !== null ) {
221 $query = $wpdadb->prepare(
222 '
223 SELECT column_name AS column_name,
224 column_type AS column_type
225 FROM information_schema.columns
226 WHERE table_schema = %s
227 AND table_name = %s
228 ORDER BY ordinal_position
229 ',
230 array(
231 $wpdadb->dbname,
232 $table_name,
233 )
234 );
235 $columns = $wpdadb->get_results( $query, 'ARRAY_A' );
236
237 $schema_name = $wpdadb->dbname;
238 $table_name = str_replace( '`', '', $table_name );
239 $indexes_dbs = $wpdadb->get_results(
240 "show indexes from `{$schema_name}`.`{$table_name}`",
241 'ARRAY_A'
242 );
243
244 $indexes = array();
245 foreach ( $indexes_dbs as $index_dbs ) {
246 $indexes[] = array_change_key_case( (array) $index_dbs ); //phpcs:ignore - 8.1 proof
247 }
248
249 $table_info = array(
250 'columns' => $columns,
251 'indexes' => $indexes,
252 );
253 }
254 }
255 }
256
257 echo json_encode( $table_info );
258 }
259
260 /**
261 * List of columns for a specific table
262 *
263 * @param string $table_name Database table name
264 * @param string $schema_name Database schema name
265 *
266 * @return Column in $table_name
267 * @since 2.0.10
268 */
269 public static function get_table_columns( $table_name, $schema_name ) {
270 $wpdadb = WPDADB::get_db_connection( $schema_name );
271 if ( $wpdadb === null ) {
272 return array();
273 } else {
274 $query = $wpdadb->prepare(
275 '
276 SELECT column_name AS column_name
277 FROM information_schema.columns
278 WHERE table_schema = %s
279 AND table_name = %s
280 ORDER BY ordinal_position
281 ',
282 array(
283 $wpdadb->dbname,
284 $table_name,
285 )
286 );
287
288 return $wpdadb->get_results( $query, 'ARRAY_A' ); // phpcs:ignore Standard.Category.SniffName.ErrorCode
289 }
290 }
291
292 /**
293 * List of database schemas available to user
294 *
295 * @param bool $incl_remote_dbs Include remote database connections? (for backward compatibility)
296 *
297 * @return array
298 *
299 * @since 1.6.0
300 */
301 public static function get_db_schemas( $incl_remote_dbs = true ) {
302 global $wpdb;
303 $schemas = $wpdb->get_results(
304 'SELECT schema_name AS schema_name
305 FROM information_schema.schemata
306 ORDER BY schema_name',
307 'ARRAY_A'
308 );
309
310 $remote = array();
311 if ( $incl_remote_dbs ) {
312 $rdb = WPDADB::get_remote_databases();
313 foreach ( $rdb as $key => $val ) {
314 $remote[] = array(
315 'schema_name' => $key,
316 );
317 }
318 asort( $remote );//phpcs:ignore - 8.1 proof
319 }
320
321 return array_merge( $schemas, $remote );//phpcs:ignore - 8.1 proof
322 }
323
324 /**
325 * List of available engines
326 *
327 * @param string $schema_name Database schema name (default = WordPress schema)
328 *
329 * @return array
330 *
331 * @since 1.6.0
332 */
333 public static function get_engines( $schema_name = '' ) {
334 $wpdadb = WPDADB::get_db_connection( $schema_name );
335 if ( $wpdadb === null ) {
336 return array();
337 } else {
338 return $wpdadb->get_results(
339 'SELECT engine AS engine,
340 support AS support
341 FROM information_schema.engines
342 ORDER BY engine',
343 'ARRAY_A'
344 );
345 }
346 }
347
348 /**
349 * List of available collations
350 *
351 * @param string $schema_name Database schema name (default = WordPress schema)
352 *
353 * @return array
354 *
355 * @since 1.6.0
356 */
357 public static function get_collations( $schema_name = '' ) {
358 $wpdadb = WPDADB::get_db_connection( $schema_name );
359 if ( $wpdadb === null ) {
360 return array();
361 } else {
362 return $wpdadb->get_results(
363 'SELECT character_set_name AS character_set_name,
364 collation_name AS collation_name
365 FROM information_schema.collations
366 ORDER BY character_set_name, collation_name',
367 'ARRAY_A'
368 );
369 }
370 }
371
372 /**
373 * Returns default collation
374 *
375 * @param string $schema_name Database schema name (default = WordPress schema)
376 *
377 * @return array
378 *
379 * @since 1.6.0
380 */
381 public static function get_default_collation( $schema_name = '' ) {
382 $wpdadb = WPDADB::get_db_connection( $schema_name );
383 if ( $wpdadb === null ) {
384 return array();
385 } else {
386 return $wpdadb->get_results(
387 $wpdadb->prepare(
388 'SELECT default_character_set_name AS default_character_set_name,
389 default_collation_name AS default_collation_name
390 FROM information_schema.schemata
391 WHERE schema_name = %s
392 GROUP BY schema_name',
393 array(
394 $wpdadb->dbname,
395 )
396 ),
397 'ARRAY_A'
398 );
399 }
400 }
401
402 public static function get_engine( $schema_name, $table_name ) {
403 $wpdadb = WPDADB::get_db_connection( $schema_name );
404 if ( $wpdadb === null ) {
405 return null;
406 } else {
407 $row = $wpdadb->get_results(
408 $wpdadb->prepare(
409 'SELECT engine AS engine
410 FROM information_schema.tables
411 WHERE table_schema = %s
412 AND table_name = %s
413 ',
414 array(
415 $wpdadb->dbname,
416 $table_name,
417 )
418 ),
419 'ARRAY_A'
420 );
421 if ( 1 === count( $row ) ) {//phpcs:ignore - 8.1 proof
422 return $row[0]['engine'];
423 } else {
424 return null;
425 }
426 }
427 }
428
429 }
430
431 }
432