Merge branch 'QA_4_4' into QA_4_5
[phpmyadmin.git] / libraries / server_databases.lib.php
blob7af2e057b2c94ae0f697ec9a9f00421054c95812
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 /**
5 * functions for displaying server databases
7 * @usedby server_databases.php
9 * @package PhpMyAdmin
11 if (! defined('PHPMYADMIN')) {
12 exit;
15 /**
16 * Returns the html for Database List
18 * @param Array $databases GBI return databases
19 * @param int $databases_count database count
20 * @param int $pos display pos
21 * @param Array $dbstats database status
22 * @param string $sort_by sort by string
23 * @param string $sort_order sort order string
24 * @param bool $is_superuser User status
25 * @param Array $cfg configuration
26 * @param array $replication_types replication types
27 * @param array $replication_info replication info
28 * @param string $url_query url query
30 * @return string
32 function PMA_getHtmlForDatabase(
33 $databases, $databases_count, $pos, $dbstats,
34 $sort_by, $sort_order, $is_superuser, $cfg,
35 $replication_types, $replication_info, $url_query
36 ) {
37 $html = '<div id="tableslistcontainer">';
38 reset($databases);
39 $first_database = current($databases);
40 // table col order
41 $column_order = PMA_getColumnOrder();
43 $_url_params = array(
44 'pos' => $pos,
45 'dbstats' => $dbstats,
46 'sort_by' => $sort_by,
47 'sort_order' => $sort_order,
50 $html .= PMA_Util::getListNavigator(
51 $databases_count, $pos, $_url_params, 'server_databases.php',
52 'frame_content', $GLOBALS['cfg']['MaxDbList']
55 $_url_params['pos'] = $pos;
57 $html .= '<form class="ajax" action="server_databases.php" ';
58 $html .= 'method="post" name="dbStatsForm" id="dbStatsForm">' . "\n";
59 $html .= PMA_URL_getHiddenInputs($_url_params);
61 $_url_params['sort_by'] = 'SCHEMA_NAME';
62 $_url_params['sort_order']
63 = ($sort_by == 'SCHEMA_NAME' && $sort_order == 'asc') ? 'desc' : 'asc';
65 $html .= '<table id="tabledatabases" class="data">' . "\n"
66 . '<thead>' . "\n"
67 . '<tr>' . "\n";
69 $html .= PMA_getHtmlForColumnOrderWithSort(
70 $is_superuser,
71 $cfg['AllowUserDropDatabase'],
72 $_url_params,
73 $sort_by,
74 $sort_order,
75 $column_order,
76 $first_database
79 $html .= PMA_getHtmlForReplicationType(
80 $is_superuser,
81 $replication_types,
82 $cfg['ActionLinksMode']
85 $html .= '</tr>' . "\n"
86 . '</thead>' . "\n";
88 list($output, $column_order) = PMA_getHtmlAndColumnOrderForDatabaseList(
89 $databases,
90 $is_superuser,
91 $url_query,
92 $column_order,
93 $replication_types,
94 $replication_info
96 $html .= $output;
97 unset($output);
99 $html .= PMA_getHtmlForTableFooter(
100 $cfg['AllowUserDropDatabase'],
101 $is_superuser,
102 $databases_count,
103 $column_order,
104 $replication_types,
105 $first_database
108 $html .= '</table>' . "\n";
110 $html .= PMA_getHtmlForTableFooterButtons(
111 $cfg['AllowUserDropDatabase'],
112 $is_superuser
115 if (empty($dbstats)) {
116 //we should put notice above database list
117 $html = PMA_getHtmlForNoticeEnableStatistics($url_query, $html);
119 $html .= '</form>';
120 $html .= '</div>';
122 return $html;
126 * Returns the html for Table footer buttons
128 * @param bool $is_allowUserDropDb Allow user drop database
129 * @param bool $is_superuser User status
131 * @return string
133 function PMA_getHtmlForTableFooterButtons($is_allowUserDropDb, $is_superuser)
135 if (!$is_superuser && !$is_allowUserDropDb) {
136 return '';
139 $html = PMA_Util::getWithSelected(
140 $GLOBALS['pmaThemeImage'], $GLOBALS['text_dir'], "dbStatsForm"
142 $html .= PMA_Util::getButtonOrImage(
144 'mult_submit' . ' ajax',
145 'drop_selected_dbs',
146 __('Drop'), 'b_deltbl.png'
149 return $html;
153 * Returns the html for Table footer
155 * @param bool $is_allowUserDropDb Allow user drop database
156 * @param bool $is_superuser User status
157 * @param int $databases_count Database count
158 * @param string $column_order column order
159 * @param array $replication_types replication types
160 * @param string $first_database First database
162 * @return string
164 function PMA_getHtmlForTableFooter(
165 $is_allowUserDropDb, $is_superuser,
166 $databases_count, $column_order,
167 $replication_types, $first_database
169 $html = '<tfoot><tr>' . "\n";
170 if ($is_superuser || $is_allowUserDropDb) {
171 $html .= ' <th></th>' . "\n";
173 $html .= ' <th>' . __('Total') . ': <span id="databases_count">'
174 . $databases_count . '</span></th>' . "\n";
176 $html .= PMA_getHtmlForColumnOrder($column_order, $first_database);
178 foreach ($replication_types as $type) {
179 if ($GLOBALS['replication_info'][$type]['status']) {
180 $html .= ' <th></th>' . "\n";
184 if ($is_superuser) {
185 $html .= ' <th></th>' . "\n";
187 $html .= '</tr>' . "\n";
188 $html .= '</tfoot>' . "\n";
189 return $html;
193 * Returns the html for Database List and Column order
195 * @param array $databases GBI return databases
196 * @param bool $is_superuser User status
197 * @param string $url_query Url query
198 * @param array $column_order column order
199 * @param array $replication_types replication types
200 * @param array $replication_info replication info
202 * @return Array
204 function PMA_getHtmlAndColumnOrderForDatabaseList(
205 $databases, $is_superuser, $url_query,
206 $column_order, $replication_types, $replication_info
208 $odd_row = true;
209 $html = '<tbody>' . "\n";
211 foreach ($databases as $current) {
212 $tr_class = $odd_row ? 'odd' : 'even';
213 if ($GLOBALS['dbi']->isSystemSchema($current['SCHEMA_NAME'], true)) {
214 $tr_class .= ' noclick';
216 $html .= '<tr class="' . $tr_class . '">' . "\n";
217 $odd_row = ! $odd_row;
219 list($column_order, $generated_html) = PMA_buildHtmlForDb(
220 $current,
221 $is_superuser,
222 $url_query,
223 $column_order,
224 $replication_types,
225 $replication_info
228 $html .= $generated_html;
230 $html .= '</tr>' . "\n";
231 } // end foreach ($databases as $key => $current)
232 unset($current, $odd_row);
233 $html .= '</tbody>';
234 return array($html, $column_order);
238 * Returns the html for Column Order
240 * @param array $column_order Column order
241 * @param array $first_database The first display database
243 * @return string
245 function PMA_getHtmlForColumnOrder($column_order, $first_database)
247 $html = "";
248 // avoid execution path notice
249 $unit = "";
250 foreach ($column_order as $stat_name => $stat) {
251 if (array_key_exists($stat_name, $first_database)) {
252 if ($stat['format'] === 'byte') {
253 list($value, $unit)
254 = PMA_Util::formatByteDown($stat['footer'], 3, 1);
255 } elseif ($stat['format'] === 'number') {
256 $value = PMA_Util::formatNumber($stat['footer'], 0);
257 } else {
258 $value = htmlentities($stat['footer'], 0);
260 $html .= ' <th class="value">';
261 if (isset($stat['description_function'])) {
262 $html .= '<dfn title="'
263 . $stat['description_function']($stat['footer']) . '">';
265 $html .= $value;
266 if (isset($stat['description_function'])) {
267 $html .= '</dfn>';
269 $html .= '</th>' . "\n";
270 if ($stat['format'] === 'byte') {
271 $html .= ' <th class="unit">' . $unit . '</th>' . "\n";
276 return $html;
281 * Returns the html for Column Order with Sort
283 * @param bool $is_superuser User status
284 * @param bool $is_allowUserDropDb Allow user drop database
285 * @param Array $_url_params Url params
286 * @param string $sort_by sort column name
287 * @param string $sort_order order
288 * @param array $column_order column order
289 * @param array $first_database database to show
291 * @return string
293 function PMA_getHtmlForColumnOrderWithSort(
294 $is_superuser, $is_allowUserDropDb,
295 $_url_params, $sort_by, $sort_order,
296 $column_order, $first_database
298 $html = ($is_superuser || $is_allowUserDropDb
299 ? ' <th></th>' . "\n"
300 : '')
301 . ' <th><a href="server_databases.php'
302 . PMA_URL_getCommon($_url_params) . '">' . "\n"
303 . ' ' . __('Database') . "\n"
304 . ($sort_by == 'SCHEMA_NAME'
305 ? ' ' . PMA_Util::getImage(
306 's_' . $sort_order . '.png',
307 ($sort_order == 'asc' ? __('Ascending') : __('Descending'))
308 ) . "\n"
309 : ''
311 . ' </a></th>' . "\n";
312 $table_columns = 3;
313 foreach ($column_order as $stat_name => $stat) {
314 if (!array_key_exists($stat_name, $first_database)) {
315 continue;
318 if ($stat['format'] === 'byte') {
319 $table_columns += 2;
320 $colspan = ' colspan="2"';
321 } else {
322 $table_columns++;
323 $colspan = '';
325 $_url_params['sort_by'] = $stat_name;
326 $_url_params['sort_order']
327 = ($sort_by == $stat_name && $sort_order == 'desc') ? 'asc' : 'desc';
328 $html .= ' <th' . $colspan . '>'
329 . '<a href="server_databases.php'
330 . PMA_URL_getCommon($_url_params) . '">' . "\n"
331 . ' ' . $stat['disp_name'] . "\n"
332 . ($sort_by == $stat_name
333 ? ' ' . PMA_Util::getImage(
334 's_' . $sort_order . '.png',
335 ($sort_order == 'asc' ? __('Ascending') : __('Descending'))
336 ) . "\n"
337 : ''
339 . ' </a></th>' . "\n";
341 return $html;
346 * Returns the html for Enable Statistics
348 * @param string $url_query Url query
349 * @param string $html html for database list
351 * @return string
353 function PMA_getHtmlForNoticeEnableStatistics($url_query, $html)
355 $notice = PMA_Message::notice(
357 'Note: Enabling the database statistics here might cause '
358 . 'heavy traffic between the web server and the MySQL server.'
360 )->getDisplay();
361 $html .= $notice;
363 $items = array();
364 $items[] = array(
365 'content' => '<strong>' . "\n"
366 . __('Enable statistics')
367 . '</strong><br />' . "\n",
368 'class' => 'li_switch_dbstats',
369 'url' => array(
370 'href' => 'server_databases.php' . $url_query . '&amp;dbstats=1',
371 'title' => __('Enable statistics')
375 include_once './libraries/Template.class.php';
376 $html .= PMA\Template::get('list/unordered')->render(
377 array('items' => $items,)
380 return $html;
384 * Returns the html for database replication types
386 * @param bool $is_superuser User status
387 * @param Array $replication_types replication types
388 * @param bool $cfg_iconic cfg about Properties Iconic
390 * @return string
392 function PMA_getHtmlForReplicationType(
393 $is_superuser, $replication_types, $cfg_iconic
395 $html = '';
396 foreach ($replication_types as $type) {
397 if ($type == "master") {
398 $name = __('Master replication');
399 } elseif ($type == "slave") {
400 $name = __('Slave replication');
403 if ($GLOBALS['replication_info'][$type]['status']) {
404 $html .= ' <th>' . $name . '</th>' . "\n";
408 if ($is_superuser && ! PMA_DRIZZLE) {
409 $html .= ' <th>' . ($cfg_iconic ? '' : __('Action')) . "\n"
410 . ' </th>' . "\n";
412 return $html;
416 * Returns the array about $sort_order and $sort_by
418 * @return Array
420 function PMA_getListForSortDatabase()
422 if (empty($_REQUEST['sort_by'])) {
423 $sort_by = 'SCHEMA_NAME';
424 } else {
425 $sort_by_whitelist = array(
426 'SCHEMA_NAME',
427 'DEFAULT_COLLATION_NAME',
428 'SCHEMA_TABLES',
429 'SCHEMA_TABLE_ROWS',
430 'SCHEMA_DATA_LENGTH',
431 'SCHEMA_INDEX_LENGTH',
432 'SCHEMA_LENGTH',
433 'SCHEMA_DATA_FREE'
435 if (in_array($_REQUEST['sort_by'], $sort_by_whitelist)) {
436 $sort_by = $_REQUEST['sort_by'];
437 } else {
438 $sort_by = 'SCHEMA_NAME';
442 if (isset($_REQUEST['sort_order'])
443 && /*overload*/mb_strtolower($_REQUEST['sort_order']) == 'desc'
445 $sort_order = 'desc';
446 } else {
447 $sort_order = 'asc';
450 return array($sort_by, $sort_order);
454 * Deal with Drops multiple databases
456 * @return null
458 function PMA_dropMultiDatabases()
460 if (! isset($_REQUEST['selected_dbs']) && ! isset($_REQUEST['query_type'])) {
461 $message = PMA_Message::error(__('No databases selected.'));
462 } else {
463 $action = 'server_databases.php';
464 $submit_mult = 'drop_db';
465 $err_url = 'server_databases.php' . PMA_URL_getCommon();
466 if (isset($_REQUEST['selected_dbs'])
467 && !isset($_REQUEST['is_js_confirmed'])
469 $selected_db = $_REQUEST['selected_dbs'];
471 if (isset($_REQUEST['is_js_confirmed'])) {
472 $_REQUEST = array(
473 'query_type' => $submit_mult,
474 'selected' => $_REQUEST['selected_dbs'],
475 'mult_btn' => __('Yes'),
476 'db' => $GLOBALS['db'],
477 'table' => $GLOBALS['table']);
479 //the following variables will be used on mult_submits.inc.php
480 global $query_type, $selected, $mult_btn;
482 include 'libraries/mult_submits.inc.php';
483 unset($action, $submit_mult, $err_url, $selected_db, $GLOBALS['db']);
484 if (empty($message)) {
485 if ($mult_btn == __('Yes')) {
486 $number_of_databases = count($selected);
487 } else {
488 $number_of_databases = 0;
490 $message = PMA_Message::success(
491 _ngettext(
492 '%1$d database has been dropped successfully.',
493 '%1$d databases have been dropped successfully.',
494 $number_of_databases
497 $message->addParam($number_of_databases);
500 if ($GLOBALS['is_ajax_request'] && $message instanceof PMA_Message) {
501 $response = PMA_Response::getInstance();
502 $response->isSuccess($message->isSuccess());
503 $response->addJSON('message', $message);
504 exit;