PluginProbe
WP Data Access – App Builder for Tables, Forms, Charts, Maps & Dashboards / 5.5.76
WP Data Access – App Builder for Tables, Forms, Charts, Maps & Dashboards v5.5.76
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.76, at WPDataAccess/Data_Dictionary/WPDA_Dictionary_Lists.php

436 lines 12.7 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_ajax() {
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 $table_info = self::get_table_widget_info( $schema_name, $table_name );
219 }
220 }
221
222 echo json_encode( $table_info );
223 }
224
225 public static function get_table_widget_info( $schema_name, $table_name ) {
226 $wpdadb = WPDADB::get_db_connection( $schema_name );
227
228 if ( $wpdadb !== null ) {
229 $query = $wpdadb->prepare(
230 '
231 SELECT column_name AS column_name,
232 column_type AS column_type
233 FROM information_schema.columns
234 WHERE table_schema = %s
235 AND table_name = %s
236 ORDER BY ordinal_position
237 ',
238 array(
239 $wpdadb->dbname,
240 $table_name,
241 )
242 );
243 $columns = $wpdadb->get_results($query, 'ARRAY_A');
244
245 $schema_name = $wpdadb->dbname;
246 $table_name = str_replace('`', '', $table_name);
247 $indexes_dbs = $wpdadb->get_results(
248 "show indexes from `{$schema_name}`.`{$table_name}`",
249 'ARRAY_A'
250 );
251
252 $indexes = array();
253 foreach ($indexes_dbs as $index_dbs) {
254 $indexes[] = array_change_key_case((array)$index_dbs); //phpcs:ignore - 8.1 proof
255 }
256
257 return array(
258 'columns' => $columns,
259 'indexes' => $indexes,
260 );
261 }
262 }
263
264 /**
265 * List of columns for a specific table
266 *
267 * @param string $table_name Database table name
268 * @param string $schema_name Database schema name
269 *
270 * @return Column in $table_name
271 * @since 2.0.10
272 */
273 public static function get_table_columns( $table_name, $schema_name ) {
274 $wpdadb = WPDADB::get_db_connection( $schema_name );
275 if ( $wpdadb === null ) {
276 return array();
277 } else {
278 $query = $wpdadb->prepare(
279 '
280 SELECT column_name AS column_name
281 FROM information_schema.columns
282 WHERE table_schema = %s
283 AND table_name = %s
284 ORDER BY ordinal_position
285 ',
286 array(
287 $wpdadb->dbname,
288 $table_name,
289 )
290 );
291
292 return $wpdadb->get_results( $query, 'ARRAY_A' ); // phpcs:ignore Standard.Category.SniffName.ErrorCode
293 }
294 }
295
296 /**
297 * List of database schemas available to user
298 *
299 * @param bool $incl_remote_dbs Include remote database connections? (for backward compatibility)
300 *
301 * @return array
302 *
303 * @since 1.6.0
304 */
305 public static function get_db_schemas( $incl_remote_dbs = true ) {
306 global $wpdb;
307 $schemas = $wpdb->get_results(
308 'SELECT schema_name AS schema_name
309 FROM information_schema.schemata
310 ORDER BY schema_name',
311 'ARRAY_A'
312 );
313
314 $remote = array();
315 if ( $incl_remote_dbs ) {
316 $rdb = WPDADB::get_remote_databases();
317 foreach ( $rdb as $key => $val ) {
318 $remote[] = array(
319 'schema_name' => $key,
320 );
321 }
322 asort( $remote );//phpcs:ignore - 8.1 proof
323 }
324
325 return array_merge( $schemas, $remote );//phpcs:ignore - 8.1 proof
326 }
327
328 /**
329 * List of available engines
330 *
331 * @param string $schema_name Database schema name (default = WordPress schema)
332 *
333 * @return array
334 *
335 * @since 1.6.0
336 */
337 public static function get_engines( $schema_name = '' ) {
338 $wpdadb = WPDADB::get_db_connection( $schema_name );
339 if ( $wpdadb === null ) {
340 return array();
341 } else {
342 return $wpdadb->get_results(
343 'SELECT engine AS engine,
344 support AS support
345 FROM information_schema.engines
346 ORDER BY engine',
347 'ARRAY_A'
348 );
349 }
350 }
351
352 /**
353 * List of available collations
354 *
355 * @param string $schema_name Database schema name (default = WordPress schema)
356 *
357 * @return array
358 *
359 * @since 1.6.0
360 */
361 public static function get_collations( $schema_name = '' ) {
362 $wpdadb = WPDADB::get_db_connection( $schema_name );
363 if ( $wpdadb === null ) {
364 return array();
365 } else {
366 return $wpdadb->get_results(
367 'SELECT character_set_name AS character_set_name,
368 collation_name AS collation_name
369 FROM information_schema.collations
370 ORDER BY character_set_name, collation_name',
371 'ARRAY_A'
372 );
373 }
374 }
375
376 /**
377 * Returns default collation
378 *
379 * @param string $schema_name Database schema name (default = WordPress schema)
380 *
381 * @return array
382 *
383 * @since 1.6.0
384 */
385 public static function get_default_collation( $schema_name = '' ) {
386 $wpdadb = WPDADB::get_db_connection( $schema_name );
387 if ( $wpdadb === null ) {
388 return array();
389 } else {
390 return $wpdadb->get_results(
391 $wpdadb->prepare(
392 'SELECT default_character_set_name AS default_character_set_name,
393 default_collation_name AS default_collation_name
394 FROM information_schema.schemata
395 WHERE schema_name = %s
396 GROUP BY schema_name',
397 array(
398 $wpdadb->dbname,
399 )
400 ),
401 'ARRAY_A'
402 );
403 }
404 }
405
406 public static function get_engine( $schema_name, $table_name ) {
407 $wpdadb = WPDADB::get_db_connection( $schema_name );
408 if ( $wpdadb === null ) {
409 return null;
410 } else {
411 $row = $wpdadb->get_results(
412 $wpdadb->prepare(
413 'SELECT engine AS engine
414 FROM information_schema.tables
415 WHERE table_schema = %s
416 AND table_name = %s
417 ',
418 array(
419 $wpdadb->dbname,
420 $table_name,
421 )
422 ),
423 'ARRAY_A'
424 );
425 if ( 1 === count( $row ) ) {//phpcs:ignore - 8.1 proof
426 return $row[0]['engine'];
427 } else {
428 return null;
429 }
430 }
431 }
432
433 }
434
435 }
436