Plugin Directory

Changeset 3203290


Ignore:
Timestamp:
12/05/2024 09:43:57 PM (15 months ago)
Author:
codecompiled
Message:

Updated UI for displaying DB details

File:
1 edited

Legend:

Unmodified
Added
Removed
  • wp-settings/trunk/wp-settings.php

    r3202735 r3203290  
    44Plugin URI: https://wordpress.org/plugins/wp-settings/
    55Description:Displays the important information about WordPress installation such as important wordpress settings,database settings,theme details and php information.You can generate DB Backup Script for restoring the database and for keeping database backups.
    6 Version: 2.5
     6Version: 2.6
    77Author: CodeCompiled
    88Author URI: http://www.codecompiled.com
     
    9090add_action('wp_print_scripts', 'wpsettings_load_scripts_styles');
    9191
    92 function wpsettings_getMySqlDetails() {
     92function wpsettings_getMySqlDetails($topN = 20) {
    9393    global $wpdb;
    9494
    95     // Query for MySQL version
    96     $results = $wpdb->get_results('SELECT VERSION() as mysqlversion');
    97     $sqlInfoVer = '';
    98     foreach ($results as $result) {
    99         $sqlInfoVer = $result->mysqlversion;
    100     }
    101 
    102     // Table schema name (DB_NAME) for information_schema queries
    103     $tableSchema = DB_NAME;
    104 
    105     // Fetching MySQL details
     95    // Ensure $topN is a positive integer and within a reasonable range
     96    $topN = (is_numeric($topN) && $topN > 0 && $topN <= 100) ? intval($topN) : 20;
     97
    10698    $ws_mysqldetails = array();
    107     $ws_mysqldetails["VERSION"] = $sqlInfoVer;
    108     $ws_mysqldetails["DATABASE NAME"] = DB_NAME;
    109     $ws_mysqldetails["DATABASE USER NAME"] = DB_USER;
    110     $ws_mysqldetails["DATABASE HOST"] = DB_HOST;
    111     $ws_mysqldetails["DATABASE SIZE (MB)"] = $wpdb->get_var(
    112         $wpdb->prepare(
    113             "SELECT SUM(data_length + index_length) / 1024 / 1024 AS dbsize
    114              FROM information_schema.TABLES
    115              WHERE table_schema = %s",
    116             $tableSchema
    117         )
    118     );
    119     $ws_mysqldetails["NO. OF TABLES"] = $wpdb->get_var(
    120         $wpdb->prepare(
    121             "SELECT COUNT(*)
    122              FROM information_schema.TABLES
    123              WHERE table_schema = %s",
    124             $tableSchema
    125         )
    126     );
    127 
    128     // Fetching top 5 largest tables by size and their row counts
    129     $topTables = $wpdb->get_results(
    130         $wpdb->prepare(
    131             "SELECT table_name AS TableName,
    132                     ROUND((data_length + index_length) / 1024 / 1024, 2) AS TableSizeMB,
    133                     table_rows AS TableRows
    134              FROM information_schema.TABLES
    135              WHERE table_schema = %s
    136              ORDER BY TableSizeMB DESC
    137              LIMIT 20",
    138             $tableSchema
    139         )
    140     );
    141 
    142     // Prepare rows with table name, size, and row count
    143     $tableDetails = array();
    144     foreach ($topTables as $table) {
    145         $tableDetails[] = array(
    146             "Table Name" => $table->TableName,
    147             "Size (MB)" => $table->TableSizeMB,
    148             "Rows" => $table->TableRows
     99
     100    try {
     101        // Query for MySQL version
     102        $results = $wpdb->get_results('SELECT VERSION() as mysqlversion');
     103        $ws_mysqldetails["VERSION"] = $results[0]->mysqlversion ?? 'Unknown';
     104
     105        // Set the database schema (DB_NAME) for queries
     106        $tableSchema = DB_NAME;
     107
     108        // Basic database details
     109        $ws_mysqldetails["DATABASE NAME"] = DB_NAME ?: 'Not Set';
     110        $ws_mysqldetails["DATABASE USER NAME"] = DB_USER ?: 'Not Set';
     111        $ws_mysqldetails["DATABASE HOST"] = DB_HOST ?: 'Not Set';
     112
     113        // Fetch database size in MB
     114        $dbSize = $wpdb->get_var(
     115            $wpdb->prepare(
     116                "SELECT SUM(data_length + index_length) / 1024 / 1024 AS dbsize
     117                 FROM information_schema.TABLES
     118                 WHERE table_schema = %s",
     119                $tableSchema
     120            )
    149121        );
    150     }
    151 
    152     // Assign the top 5 tables as rows to TABLE DETAILS
    153     $ws_mysqldetails["TABLE DETAILS"] = $tableDetails;
     122        $ws_mysqldetails["DATABASE SIZE (MB)"] = $dbSize !== null ? round($dbSize, 2) : 'Unknown';
     123
     124        // Fetch the total number of tables
     125        $tableCount = $wpdb->get_var(
     126            $wpdb->prepare(
     127                "SELECT COUNT(*)
     128                 FROM information_schema.TABLES
     129                 WHERE table_schema = %s",
     130                $tableSchema
     131            )
     132        );
     133        $ws_mysqldetails["NO. OF TABLES"] = $tableCount !== null ? intval($tableCount) : 'Unknown';
     134
     135        // Fetch top N largest tables by size
     136        $topTables = $wpdb->get_results(
     137            $wpdb->prepare(
     138                "SELECT table_name AS TableName,
     139                        ROUND((data_length + index_length) / 1024 / 1024, 2) AS TableSizeMB,
     140                        table_rows AS TableRows
     141                 FROM information_schema.TABLES
     142                 WHERE table_schema = %s
     143                 ORDER BY TableSizeMB DESC
     144                 LIMIT %d",
     145                $tableSchema,
     146                $topN
     147            )
     148        );
     149
     150        // Handle empty or null results gracefully
     151        $tableDetails = array();
     152        if (!empty($topTables)) {
     153            foreach ($topTables as $table) {
     154                $tableDetails[] = array(
     155                    "Table Name" => $table->TableName ?? 'Unknown',
     156                    "Size (MB)" => $table->TableSizeMB !== null ? $table->TableSizeMB : 'Unknown',
     157                    "Rows" => $table->TableRows !== null ? $table->TableRows : 'Unknown'
     158                );
     159            }
     160        } else {
     161            $tableDetails[] = array(
     162                "Table Name" => 'No Data',
     163                "Size (MB)" => 'No Data',
     164                "Rows" => 'No Data'
     165            );
     166        }
     167
     168        // Assign the table details to the result
     169        $ws_mysqldetails["TABLE DETAILS"] = $tableDetails;
     170
     171    } catch (Exception $e) {
     172        // Handle exceptions by providing default values and optionally logging errors
     173        error_log('Error fetching MySQL details: ' . $e->getMessage());
     174        $ws_mysqldetails = array(
     175            "VERSION" => "Unknown",
     176            "DATABASE NAME" => "Unknown",
     177            "DATABASE USER NAME" => "Unknown",
     178            "DATABASE HOST" => "Unknown",
     179            "DATABASE SIZE (MB)" => "Unknown",
     180            "NO. OF TABLES" => "Unknown",
     181            "TABLE DETAILS" => array(
     182                array(
     183                    "Table Name" => "No Data",
     184                    "Size (MB)" => "No Data",
     185                    "Rows" => "No Data"
     186                )
     187            )
     188        );
     189    }
    154190
    155191    return $ws_mysqldetails;
    156192}
     193
    157194function wpsettings_bloginfo_array() {
    158195    $fields = array('name', 'description', 'wpurl', 'url', 'admin_email', 'version','categories','pages','pingback_url', 'language');
     
    353390
    354391function getDatabaseContent() {
     392    // Check which action was triggered
     393    $action = isset($_POST['action']) ? sanitize_text_field($_POST['action']) : '';
     394    $topN = 20; // Default value for top tables
     395
     396    if ($action === 'update') {
     397        // Handle "Update" button action
     398        $topN = isset($_POST['top_n']) ? intval($_POST['top_n']) : 20;
     399        $topN = ($topN > 0) ? $topN : 20; // Fallback to 20 if invalid value provided
     400    } elseif ($action === 'download') {
     401        // Handle "Download" button action
     402        handleDatabaseDownload(); // Call a function to handle the download (separate logic for clarity)
     403        return; // Stop further execution after download
     404    }
     405
     406    // Fetch database details with the specified top N
     407    $sqldetails = wpsettings_getMySqlDetails($topN);
    355408    ?>
    356409    <form action="" method="POST">
     
    362415            </tr>
    363416            <?php
    364             $sqldetails = wpsettings_getMySqlDetails();
    365            
    366             // Loop through general details
     417            // Display general database details
    367418            foreach ($sqldetails as $sqlKey => $sqlValue) {
    368419                if ($sqlKey !== "TABLE DETAILS") { // Exclude table details for now
     
    380431            <tr>
    381432                <td colspan="5" style="text-align: left; padding: 10px; font-weight: bold; background-color: #0073aa; color: #fff;">
    382                     Tables(sorted by size,top 20):
     433                    Tables (sorted by size, top <?php echo $topN; ?>):
    383434                </td>
    384435            </tr>
     
    404455            <tr>
    405456                <td colspan="5" style="text-align: left; padding: 10px;">
    406                     Click the download button to take a backup of the database
    407                     <input type="submit" value="Download" name="submit_btn" style="margin-left: 10px; padding: 5px 15px; background-color: #0073aa; color: #fff; border: none; cursor: pointer;">
     457                    <!-- "Download" Button -->
     458                    <input type="submit" name="action" value="download" style="margin-right: 20px; padding: 5px 15px; background-color: #0073aa; color: #fff; border: none; cursor: pointer;">
     459
     460                    <!-- Dropdown and "Update" Button -->
     461                    Display top
     462                    <select name="top_n" style="margin-right: 10px;">
     463                        <option value="5" <?php echo ($topN == 5) ? 'selected' : ''; ?>>5</option>
     464                        <option value="10" <?php echo ($topN == 10) ? 'selected' : ''; ?>>10</option>
     465                        <option value="20" <?php echo ($topN == 20) ? 'selected' : ''; ?>>20</option>
     466                        <option value="50" <?php echo ($topN == 50) ? 'selected' : ''; ?>>50</option>
     467                    </select>
     468                    tables
     469                    <input type="submit" name="action" value="update" style="padding: 5px 15px; background-color: #28a745; color: #fff; border: none; cursor: pointer;">
    408470                </td>
    409471            </tr>
    410472        </table>
    411         <input type="hidden" name="names" id="names">
    412473    </form>
    413474    <?php
    414475}
     476
     477/**
     478 * Handles database download functionality.
     479 */
     480function handleDatabaseDownload() {
     481    // Logic to generate and serve the database backup file
     482    // Example placeholder (replace with actual backup generation code):
     483    header('Content-Type: application/sql');
     484    header('Content-Disposition: attachment; filename="database_backup.sql"');
     485    echo "-- Database backup content here --";
     486    exit;
     487}
     488
     489
    415490
    416491
Note: See TracChangeset for help on using the changeset viewer.