/**
 * Prints Server Process list
 *
 * @return string
 */
function PMA_getHtmlForServerProcesslist()
{
    $url_params = array();
    $show_full_sql = !empty($_REQUEST['full']);
    if ($show_full_sql) {
        $url_params['full'] = 1;
        $full_text_link = 'server_status_processes.php' . URL::getCommon(array(), '?');
    } else {
        $full_text_link = 'server_status_processes.php' . URL::getCommon(array('full' => 1));
    }
    // 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($_REQUEST['order_by_field']) && !empty($_REQUEST['sort_order']) || !empty($_REQUEST['showExecuting'])) {
        $sql_query = 'SELECT * FROM `INFORMATION_SCHEMA`.`PROCESSLIST` ';
    }
    if (!empty($_REQUEST['showExecuting'])) {
        $sql_query .= ' WHERE state = "executing" ';
    }
    if (!empty($_REQUEST['order_by_field']) && !empty($_REQUEST['sort_order'])) {
        $sql_query .= ' ORDER BY ' . Util::backquote($_REQUEST['order_by_field']) . ' ' . $_REQUEST['sort_order'];
    }
    $result = $GLOBALS['dbi']->query($sql_query);
    $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($_REQUEST['order_by_field']) && !empty($_REQUEST['sort_order']) && $_REQUEST['order_by_field'] == $column['order_by_field'];
        $column['sort_order'] = 'ASC';
        if ($is_sorted && $_REQUEST['sort_order'] === 'ASC') {
            $column['sort_order'] = 'DESC';
        }
        if (isset($_REQUEST['showExecuting'])) {
            $column['showExecuting'] = 'on';
        }
        $retval .= '<th>';
        $columnUrl = URL::getCommon($column);
        $retval .= '<a href="server_status_processes.php' . $columnUrl . '" ';
        if ($is_sorted) {
            $retval .= 'onmouseout="$(\'.soimg\').toggle()" ' . 'onmouseover="$(\'.soimg\').toggle()"';
        }
        $retval .= '>';
        $retval .= $column['column_name'];
        if ($is_sorted) {
            $asc_display_style = 'inline';
            $desc_display_style = 'none';
            if ($_REQUEST['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) {
            $retval .= '<a href="' . $full_text_link . '">';
            if ($show_full_sql) {
                $retval .= Util::getImage('s_partialtext.png', __('Truncate Shown Queries'));
            } else {
                $retval .= Util::getImage('s_fulltext.png', __('Show Full Queries'));
            }
            $retval .= '</a>';
        }
        $retval .= '</th>';
    }
    $retval .= '</tr>';
    $retval .= '</thead>';
    $retval .= '<tbody>';
    while ($process = $GLOBALS['dbi']->fetchAssoc($result)) {
        $retval .= PMA_getHtmlForServerProcessItem($process, $show_full_sql);
    }
    $retval .= '</tbody>';
    $retval .= '</table>';
    return $retval;
}
 /**
  * Test for PMA_getHtmlForServerProcessItem
  *
  * @return void
  */
 public function testPMAGetHtmlForServerProcessItem()
 {
     //parameters
     $process = array("user" => "User1", "host" => "Host1", "id" => "Id1", "db" => "db1", "command" => "Command1", "info" => "Info1", "state" => "State1", "time" => "Time1");
     $show_full_sql = true;
     $_REQUEST['sort_order'] = "desc";
     $_REQUEST['order_by_field'] = "process";
     $GLOBALS['cfg']['MaxCharactersInDisplayedSQL'] = 12;
     //Call the test function
     $html = PMA_getHtmlForServerProcessItem($process, $show_full_sql);
     //validate 1: $kill_process
     $url_params = array('kill' => $process['id'], 'ajax_request' => true);
     $kill_process = 'server_status_processes.php' . URL::getCommon($url_params);
     $this->assertContains($kill_process, $html);
     $this->assertContains('ajax kill_process', $html);
     $this->assertContains(__('Kill'), $html);
     //validate 2: $process['User']
     $this->assertContains(htmlspecialchars($process['user']), $html);
     //validate 3: $process['Host']
     $this->assertContains(htmlspecialchars($process['host']), $html);
     //validate 4: $process['db']
     $this->assertContains(__('None'), $html);
     //validate 5: $process['Command']
     $this->assertContains(htmlspecialchars($process['command']), $html);
     //validate 6: $process['Time']
     $this->assertContains($process['time'], $html);
     //validate 7: $process['state']
     $this->assertContains($process['state'], $html);
     //validate 8: $process['info']
     $this->assertContains($process['info'], $html);
     unset($process['info']);
     $html = PMA_getHtmlForServerProcessItem($process, $show_full_sql);
     $this->assertContains('---', $html);
 }
/**
 * Prints Server Process list
 *
 * @return string
 */
function PMA_getHtmlForServerProcesslist()
{
    $url_params = array();
    $show_full_sql = !empty($_REQUEST['full']);
    if ($show_full_sql) {
        $url_params['full'] = 1;
        $full_text_link = 'server_status_processes.php' . PMA_URL_getCommon(array(), 'html', '?');
    } else {
        $full_text_link = 'server_status_processes.php' . PMA_URL_getCommon(array('full' => 1));
    }
    // 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' => __('SQL query'), 'order_by_field' => 'Info'));
    $sortableColCount = count($sortable_columns);
    if (PMA_DRIZZLE) {
        $left_str = 'left(p.info, ' . (int) $GLOBALS['cfg']['MaxCharactersInDisplayedSQL'] . ')';
        $sql_query = "SELECT\n                p.id       AS Id,\n                p.username AS User,\n                p.host     AS Host,\n                p.db       AS db,\n                p.command  AS Command,\n                p.time     AS Time,\n                p.state    AS State," . ($show_full_sql ? 's.query' : $left_str) . " AS Info FROM data_dictionary.PROCESSLIST p " . ($show_full_sql ? 'LEFT JOIN data_dictionary.SESSIONS s ON s.session_id = p.id' : '');
        if (!empty($_REQUEST['order_by_field']) && !empty($_REQUEST['sort_order'])) {
            $sql_query .= ' ORDER BY p.' . $_REQUEST['order_by_field'] . ' ' . $_REQUEST['sort_order'];
        }
    } else {
        $sql_query = $show_full_sql ? 'SHOW FULL PROCESSLIST' : 'SHOW PROCESSLIST';
        if (!empty($_REQUEST['order_by_field']) && !empty($_REQUEST['sort_order'])) {
            $sql_query = 'SELECT * FROM `INFORMATION_SCHEMA`.`PROCESSLIST` ' . 'ORDER BY `' . $_REQUEST['order_by_field'] . '` ' . $_REQUEST['sort_order'];
        }
    }
    $result = $GLOBALS['dbi']->query($sql_query);
    $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($_REQUEST['order_by_field']) && !empty($_REQUEST['sort_order']) && $_REQUEST['order_by_field'] == $column['order_by_field'];
        $column['sort_order'] = 'ASC';
        if ($is_sorted && $_REQUEST['sort_order'] === 'ASC') {
            $column['sort_order'] = 'DESC';
        }
        if ($is_sorted) {
            if ($_REQUEST['sort_order'] == 'ASC') {
                $asc_display_style = 'inline';
                $desc_display_style = 'none';
            } elseif ($_REQUEST['sort_order'] == 'DESC') {
                $desc_display_style = 'inline';
                $asc_display_style = 'none';
            }
        }
        $retval .= '<th>';
        $columnUrl = PMA_URL_getCommon($column);
        $retval .= '<a href="server_status_processes.php' . $columnUrl . '" ';
        if ($is_sorted) {
            $retval .= 'onmouseout="$(\'.soimg\').toggle()" ' . 'onmouseover="$(\'.soimg\').toggle()"';
        }
        $retval .= '>';
        $retval .= $column['column_name'];
        if ($is_sorted) {
            $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 (!PMA_DRIZZLE && 0 === --$sortableColCount) {
            $retval .= '<a href="' . $full_text_link . '">';
            if ($show_full_sql) {
                $retval .= PMA_Util::getImage('s_partialtext.png', __('Truncate Shown Queries'));
            } else {
                $retval .= PMA_Util::getImage('s_fulltext.png', __('Show Full Queries'));
            }
            $retval .= '</a>';
        }
        $retval .= '</th>';
    }
    $retval .= '</tr>';
    $retval .= '</thead>';
    $retval .= '<tbody>';
    $odd_row = true;
    while ($process = $GLOBALS['dbi']->fetchAssoc($result)) {
        $retval .= PMA_getHtmlForServerProcessItem($process, $odd_row, $show_full_sql);
        $odd_row = !$odd_row;
    }
    $retval .= '</tbody>';
    $retval .= '</table>';
    return $retval;
}