UPDATE 4.4.0.0
[phpmyadmin.git] / libraries / server_databases.lib.php
blob17fafca94c4d30960790e243b33cd6d77ad93052
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 string $replication_types replication types
27 * @param string $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 Array $url_query Url query
198 * @param string $column_order column order
199 * @param string $replication_types replication types
200 * @param string $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 bool $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 //we should put notice above database list
362 $html = $notice . $html;
363 $html .= '<ul><li id="li_switch_dbstats"><strong>' . "\n";
364 $html .= '<a href="server_databases.php' . $url_query . '&amp;dbstats=1"'
365 . ' title="' . __('Enable Statistics') . '">' . "\n"
366 . ' ' . __('Enable Statistics');
367 $html .= '</a></strong><br />' . "\n";
368 $html .= '</li>' . "\n" . '</ul>' . "\n";
370 return $html;
374 * Returns the html for database replication types
376 * @param bool $is_superuser User status
377 * @param Array $replication_types replication types
378 * @param bool $cfg_iconic cfg about Properties Iconic
380 * @return string
382 function PMA_getHtmlForReplicationType(
383 $is_superuser, $replication_types, $cfg_iconic
385 $html = '';
386 foreach ($replication_types as $type) {
387 if ($type == "master") {
388 $name = __('Master replication');
389 } elseif ($type == "slave") {
390 $name = __('Slave replication');
393 if ($GLOBALS['replication_info'][$type]['status']) {
394 $html .= ' <th>' . $name . '</th>' . "\n";
398 if ($is_superuser && ! PMA_DRIZZLE) {
399 $html .= ' <th>' . ($cfg_iconic ? '' : __('Action')) . "\n"
400 . ' </th>' . "\n";
402 return $html;
406 * Returns the array about $sort_order and $sort_by
408 * @return Array
410 function PMA_getListForSortDatabase()
412 if (empty($_REQUEST['sort_by'])) {
413 $sort_by = 'SCHEMA_NAME';
414 } else {
415 $sort_by_whitelist = array(
416 'SCHEMA_NAME',
417 'DEFAULT_COLLATION_NAME',
418 'SCHEMA_TABLES',
419 'SCHEMA_TABLE_ROWS',
420 'SCHEMA_DATA_LENGTH',
421 'SCHEMA_INDEX_LENGTH',
422 'SCHEMA_LENGTH',
423 'SCHEMA_DATA_FREE'
425 if (in_array($_REQUEST['sort_by'], $sort_by_whitelist)) {
426 $sort_by = $_REQUEST['sort_by'];
427 } else {
428 $sort_by = 'SCHEMA_NAME';
432 if (isset($_REQUEST['sort_order'])
433 && /*overload*/mb_strtolower($_REQUEST['sort_order']) == 'desc'
435 $sort_order = 'desc';
436 } else {
437 $sort_order = 'asc';
440 return array($sort_by, $sort_order);
444 * Deal with Drops multiple databases
446 * @return null
448 function PMA_dropMultiDatabases()
450 if (! isset($_REQUEST['selected_dbs']) && ! isset($_REQUEST['query_type'])) {
451 $message = PMA_Message::error(__('No databases selected.'));
452 } else {
453 $action = 'server_databases.php';
454 $submit_mult = 'drop_db';
455 $err_url = 'server_databases.php' . PMA_URL_getCommon();
456 if (isset($_REQUEST['selected_dbs'])
457 && !isset($_REQUEST['is_js_confirmed'])
459 $selected_db = $_REQUEST['selected_dbs'];
461 if (isset($_REQUEST['is_js_confirmed'])) {
462 $_REQUEST = array(
463 'query_type' => $submit_mult,
464 'selected' => $_REQUEST['selected_dbs'],
465 'mult_btn' => __('Yes'),
466 'db' => $GLOBALS['db'],
467 'table' => $GLOBALS['table']);
469 //the following variables will be used on mult_submits.inc.php
470 global $query_type, $selected, $mult_btn;
472 include 'libraries/mult_submits.inc.php';
473 unset($action, $submit_mult, $err_url, $selected_db, $GLOBALS['db']);
474 if (empty($message)) {
475 if ($mult_btn == __('Yes')) {
476 $number_of_databases = count($selected);
477 } else {
478 $number_of_databases = 0;
480 $message = PMA_Message::success(
481 _ngettext(
482 '%1$d database has been dropped successfully.',
483 '%1$d databases have been dropped successfully.',
484 $number_of_databases
487 $message->addParam($number_of_databases);
490 if ($GLOBALS['is_ajax_request'] && $message instanceof PMA_Message) {
491 $response = PMA_Response::getInstance();
492 $response->isSuccess($message->isSuccess());
493 $response->addJSON('message', $message);
494 exit;