Merge remote-tracking branch 'origin/master'
[phpmyadmin.git] / libraries / server_databases.lib.php
blobcb32d22197e4d2b2f3fc738779e5b28d9fdedd43
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 = $html . $notice;
362 $html .= '<ul><li id="li_switch_dbstats"><strong>' . "\n";
363 $html .= '<a href="server_databases.php' . $url_query . '&amp;dbstats=1"'
364 . ' title="' . __('Enable Statistics') . '">' . "\n"
365 . ' ' . __('Enable Statistics');
366 $html .= '</a></strong><br />' . "\n";
367 $html .= '</li>' . "\n" . '</ul>' . "\n";
369 return $html;
373 * Returns the html for database replication types
375 * @param bool $is_superuser User status
376 * @param Array $replication_types replication types
377 * @param bool $cfg_iconic cfg about Properties Iconic
379 * @return string
381 function PMA_getHtmlForReplicationType(
382 $is_superuser, $replication_types, $cfg_iconic
384 $html = '';
385 foreach ($replication_types as $type) {
386 if ($type == "master") {
387 $name = __('Master replication');
388 } elseif ($type == "slave") {
389 $name = __('Slave replication');
392 if ($GLOBALS['replication_info'][$type]['status']) {
393 $html .= ' <th>' . $name . '</th>' . "\n";
397 if ($is_superuser && ! PMA_DRIZZLE) {
398 $html .= ' <th>' . ($cfg_iconic ? '' : __('Action')) . "\n"
399 . ' </th>' . "\n";
401 return $html;
405 * Returns the array about $sort_order and $sort_by
407 * @return Array
409 function PMA_getListForSortDatabase()
411 if (empty($_REQUEST['sort_by'])) {
412 $sort_by = 'SCHEMA_NAME';
413 } else {
414 $sort_by_whitelist = array(
415 'SCHEMA_NAME',
416 'DEFAULT_COLLATION_NAME',
417 'SCHEMA_TABLES',
418 'SCHEMA_TABLE_ROWS',
419 'SCHEMA_DATA_LENGTH',
420 'SCHEMA_INDEX_LENGTH',
421 'SCHEMA_LENGTH',
422 'SCHEMA_DATA_FREE'
424 if (in_array($_REQUEST['sort_by'], $sort_by_whitelist)) {
425 $sort_by = $_REQUEST['sort_by'];
426 } else {
427 $sort_by = 'SCHEMA_NAME';
431 if (isset($_REQUEST['sort_order'])
432 && /*overload*/mb_strtolower($_REQUEST['sort_order']) == 'desc'
434 $sort_order = 'desc';
435 } else {
436 $sort_order = 'asc';
439 return array($sort_by, $sort_order);
443 * Deal with Drops multiple databases
445 * @return null
447 function PMA_dropMultiDatabases()
449 if (! isset($_REQUEST['selected_dbs']) && ! isset($_REQUEST['query_type'])) {
450 $message = PMA_Message::error(__('No databases selected.'));
451 } else {
452 $action = 'server_databases.php';
453 $submit_mult = 'drop_db';
454 $err_url = 'server_databases.php' . PMA_URL_getCommon();
455 if (isset($_REQUEST['selected_dbs'])
456 && !isset($_REQUEST['is_js_confirmed'])
458 $selected_db = $_REQUEST['selected_dbs'];
460 if (isset($_REQUEST['is_js_confirmed'])) {
461 $_REQUEST = array(
462 'query_type' => $submit_mult,
463 'selected' => $_REQUEST['selected_dbs'],
464 'mult_btn' => __('Yes'),
465 'db' => $GLOBALS['db'],
466 'table' => $GLOBALS['table']);
468 //the following variables will be used on mult_submits.inc.php
469 global $query_type, $selected, $mult_btn;
471 include 'libraries/mult_submits.inc.php';
472 unset($action, $submit_mult, $err_url, $selected_db, $GLOBALS['db']);
473 if (empty($message)) {
474 if ($mult_btn == __('Yes')) {
475 $number_of_databases = count($selected);
476 } else {
477 $number_of_databases = 0;
479 $message = PMA_Message::success(
480 _ngettext(
481 '%1$d database has been dropped successfully.',
482 '%1$d databases have been dropped successfully.',
483 $number_of_databases
486 $message->addParam($number_of_databases);
489 if ($GLOBALS['is_ajax_request'] && $message instanceof PMA_Message) {
490 $response = PMA_Response::getInstance();
491 $response->isSuccess($message->isSuccess());
492 $response->addJSON('message', $message);
493 exit;