Processes.php 10.6 KB
Newer Older
Hamza Arfaoui's avatar
Hamza Arfaoui committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
<?php
/* vim: set expandtab sw=4 ts=4 sts=4: */
/**
 * functions for displaying processes list
 *
 * @usedby  server_status_processes.php
 *
 * @package PhpMyAdmin
 */
namespace PhpMyAdmin\Server\Status;

use PhpMyAdmin\Message;
use PhpMyAdmin\Server\Status\Data;
use PhpMyAdmin\Util;
use PhpMyAdmin\Url;

/**
 * PhpMyAdmin\Server\Status\Processes class
 *
 * @package PhpMyAdmin
 */
class Processes
{
    /**
     * Prints html for auto refreshing processes list
     *
     * @return string
     */
    public static function getHtmlForProcessListAutoRefresh()
    {
        $notice = Message::notice(
            __(
                'Note: Enabling the auto refresh here might cause '
                . 'heavy traffic between the web server and the MySQL server.'
            )
        )->getDisplay();
        $retval  = $notice . '<div class="tabLinks">';
        $retval .= '<label>' . __('Refresh rate') . ': ';
        $retval .= Data::getHtmlForRefreshList(
            'refreshRate',
            5,
            Array(2, 3, 4, 5, 10, 20, 40, 60, 120, 300, 600, 1200)
        );
        $retval .= '</label>';
        $retval .= '<a id="toggleRefresh" href="#">';
        $retval .= Util::getImage('play') . __('Start auto refresh');
        $retval .= '</a>';
        $retval .= '</div>';
        return $retval;
    }

    /**
     * Prints Server Process list
     *
     * @return string
     */
    public static function getHtmlForServerProcesslist()
    {
        $show_full_sql = ! empty($_POST['full']);

        // This array contains display name and real column name of each
        // sortable column in the table
        $sortable_columns = array(
            array(
                'column_name' => __('ID'),
                'order_by_field' => 'Id'
            ),
            array(
                'column_name' => __('User'),
                'order_by_field' => 'User'
            ),
            array(
                'column_name' => __('Host'),
                'order_by_field' => 'Host'
            ),
            array(
                'column_name' => __('Database'),
                'order_by_field' => 'db'
            ),
            array(
                'column_name' => __('Command'),
                'order_by_field' => 'Command'
            ),
            array(
                'column_name' => __('Time'),
                'order_by_field' => 'Time'
            ),
            array(
                'column_name' => __('Status'),
                'order_by_field' => 'State'
            ),
            array(
                'column_name' => __('Progress'),
                'order_by_field' => 'Progress'
            ),
            array(
                'column_name' => __('SQL query'),
                'order_by_field' => 'Info'
            )
        );
        $sortableColCount = count($sortable_columns);

        $sql_query = $show_full_sql
            ? 'SHOW FULL PROCESSLIST'
            : 'SHOW PROCESSLIST';
        if ((! empty($_POST['order_by_field'])
            && ! empty($_POST['sort_order']))
            || (! empty($_POST['showExecuting']))
        ) {
            $sql_query = 'SELECT * FROM `INFORMATION_SCHEMA`.`PROCESSLIST` ';
        }
        if (! empty($_POST['showExecuting'])) {
            $sql_query .= ' WHERE state != "" ';
        }
        if (!empty($_POST['order_by_field']) && !empty($_POST['sort_order'])) {
            $sql_query .= ' ORDER BY '
                . Util::backquote($_POST['order_by_field'])
                . ' ' . $_POST['sort_order'];
        }

        $result = $GLOBALS['dbi']->query($sql_query);

        $retval = '<div class="responsivetable">';
        $retval .= '<table id="tableprocesslist" '
            . 'class="data clearfloat noclick sortable">';
        $retval .= '<thead>';
        $retval .= '<tr>';
        $retval .= '<th>' . __('Processes') . '</th>';
        foreach ($sortable_columns as $column) {

            $is_sorted = ! empty($_POST['order_by_field'])
                && ! empty($_POST['sort_order'])
                && ($_POST['order_by_field'] == $column['order_by_field']);

            $column['sort_order'] = 'ASC';
            if ($is_sorted && $_POST['sort_order'] === 'ASC') {
                $column['sort_order'] = 'DESC';
            }
            if (isset($_POST['showExecuting'])) {
                $column['showExecuting'] = 'on';
            }

            $retval .= '<th>';
            $columnUrl = Url::getCommon($column);
            $retval .= '<a href="server_status_processes.php" data-post="' . $columnUrl . '" class="sortlink">';

            $retval .= $column['column_name'];

            if ($is_sorted) {
                $asc_display_style = 'inline';
                $desc_display_style = 'none';
                if ($_POST['sort_order'] === 'DESC') {
                    $desc_display_style = 'inline';
                    $asc_display_style = 'none';
                }
                $retval .= '<img class="icon ic_s_desc soimg" alt="'
                    . __('Descending') . '" title="" src="themes/dot.gif" '
                    . 'style="display: ' . $desc_display_style . '" />';
                $retval .= '<img class="icon ic_s_asc soimg hide" alt="'
                    . __('Ascending') . '" title="" src="themes/dot.gif" '
                    . 'style="display: ' . $asc_display_style . '" />';
            }

            $retval .= '</a>';

            if (0 === --$sortableColCount) {
                $url_params = array();
                if ($show_full_sql) {
                    $url_params['full'] = '';
                } else {
                    $url_params['full'] = 1;
                }
                if (isset($_POST['showExecuting'])) {
                    $url_params['showExecuting'] = 'on';
                }
                if (isset($_POST['order_by_field'])) {
                    $url_params['order_by_field'] = $_POST['order_by_field'];
                }
                if (isset($_POST['sort_order'])) {
                    $url_params['sort_order'] = $_POST['sort_order'];
                }
                $retval .= '<a href="server_status_processes.php" data-post="' . Url::getCommon($url_params, '') . '" >';
                if ($show_full_sql) {
                    $retval .= Util::getImage('s_partialtext',
                        __('Truncate Shown Queries'), ['class' => 'icon_fulltext']);
                } else {
                    $retval .= Util::getImage('s_fulltext',
                        __('Show Full Queries'), ['class' => 'icon_fulltext']);
                }
                $retval .= '</a>';
            }
            $retval .= '</th>';
        }

        $retval .= '</tr>';
        $retval .= '</thead>';
        $retval .= '<tbody>';

        while ($process = $GLOBALS['dbi']->fetchAssoc($result)) {
            $retval .= self::getHtmlForServerProcessItem(
                $process,
                $show_full_sql
            );
        }
        $retval .= '</tbody>';
        $retval .= '</table>';
        $retval .= '</div>';

        return $retval;
    }

    /**
     * Returns the html for the list filter
     *
     * @return string
     */
    public static function getHtmlForProcessListFilter()
    {
        $showExecuting = '';
        if (! empty($_POST['showExecuting'])) {
            $showExecuting = ' checked="checked"';
        }

        $url_params = array(
            'ajax_request' => true,
            'full' => (isset($_POST['full']) ? $_POST['full'] : ''),
            'column_name' => (isset($_POST['column_name']) ? $_POST['column_name'] : ''),
            'order_by_field'
                => (isset($_POST['order_by_field']) ? $_POST['order_by_field'] : ''),
            'sort_order' => (isset($_POST['sort_order']) ? $_POST['sort_order'] : ''),
        );

        $retval  = '';
        $retval .= '<fieldset id="tableFilter">';
        $retval .= '<legend>' . __('Filters') . '</legend>';
        $retval .= '<form action="server_status_processes.php" method="post">';
        $retval .= Url::getHiddenInputs($url_params);
        $retval .= '<input type="submit" value="' . __('Refresh') . '" />';
        $retval .= '<div class="formelement">';
        $retval .= '<input' . $showExecuting . ' type="checkbox" name="showExecuting"'
            . ' id="showExecuting" class="autosubmit"/>';
        $retval .= '<label for="showExecuting">';
        $retval .= __('Show only active');
        $retval .= '</label>';
        $retval .= '</div>';
        $retval .= '</form>';
        $retval .= '</fieldset>';

        return $retval;
    }

    /**
     * Prints Every Item of Server Process
     *
     * @param array $process       data of Every Item of Server Process
     * @param bool  $show_full_sql show full sql or not
     *
     * @return string
     */
    public static function getHtmlForServerProcessItem(array $process, $show_full_sql)
    {
        // Array keys need to modify due to the way it has used
        // to display column values
        if ((! empty($_POST['order_by_field']) && ! empty($_POST['sort_order']))
            || (! empty($_POST['showExecuting']))
        ) {
            foreach (array_keys($process) as $key) {
                $new_key = ucfirst(mb_strtolower($key));
                if ($new_key !== $key) {
                    $process[$new_key] = $process[$key];
                    unset($process[$key]);
                }
            }
        }

        $retval  = '<tr>';
        $retval .= '<td><a class="ajax kill_process" href="server_status_processes.php"'
            . ' data-post="' . Url::getCommon(['kill' => $process['Id']], '') . '">'
            . __('Kill') . '</a></td>';
        $retval .= '<td class="value">' . $process['Id'] . '</td>';
        $retval .= '<td>' . htmlspecialchars($process['User']) . '</td>';
        $retval .= '<td>' . htmlspecialchars($process['Host']) . '</td>';
        $retval .= '<td>' . ((! isset($process['db'])
                || strlen($process['db']) === 0)
                ? '<i>' . __('None') . '</i>'
                : htmlspecialchars($process['db'])) . '</td>';
        $retval .= '<td>' . htmlspecialchars($process['Command']) . '</td>';
        $retval .= '<td class="value">' . $process['Time'] . '</td>';
        $processStatusStr = empty($process['State']) ? '---' : $process['State'];
        $retval .= '<td>' . $processStatusStr . '</td>';
        $processProgress = empty($process['Progress']) ? '---' : $process['Progress'];
        $retval .= '<td>' . $processProgress . '</td>';
        $retval .= '<td>';

        if (empty($process['Info'])) {
            $retval .= '---';
        } else {
            $retval .= Util::formatSql($process['Info'], ! $show_full_sql);
        }
        $retval .= '</td>';
        $retval .= '</tr>';

        return $retval;
    }
}