Translated using Weblate (Kurdish Sorani)
[phpmyadmin.git] / libraries / tbl_tracking.lib.php
bloba48b9d0a2b177cbc99bf9b52933f2ca848c7a0e7
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Functions used to generate table tracking
6 * @package PhpMyAdmin
7 */
9 /**
10 * Filters tracking entries
12 * @param array $data the entries to filter
13 * @param string $filter_ts_from "from" date
14 * @param string $filter_ts_to "to" date
15 * @param string $filter_users users
17 * @return array filtered entries
19 function PMA_filterTracking(
20 $data, $filter_ts_from, $filter_ts_to, $filter_users
21 ) {
22 $tmp_entries = array();
23 $id = 0;
24 foreach ($data as $entry) {
25 $timestamp = strtotime($entry['date']);
26 $filtered_user = in_array($entry['username'], $filter_users);
27 if ($timestamp >= $filter_ts_from
28 && $timestamp <= $filter_ts_to
29 && (in_array('*', $filter_users) || $filtered_user)
30 ) {
31 $tmp_entries[] = array(
32 'id' => $id,
33 'timestamp' => $timestamp,
34 'username' => $entry['username'],
35 'statement' => $entry['statement']
38 $id++;
40 return($tmp_entries);
43 /**
44 * Function to get html for data definition and data manipulation statements
46 * @param string $url_query url query
47 * @param int $last_version last version
49 * @return string
51 function PMA_getHtmlForDataDefinitionAndManipulationStatements($url_query,
52 $last_version
53 ) {
54 $html = '<div id="div_create_version">';
55 $html .= '<form method="post" action="tbl_tracking.php?' . $url_query . '">';
56 $html .= PMA_URL_getHiddenInputs($GLOBALS['db'], $GLOBALS['table']);
57 $html .= '<fieldset>';
58 $html .= '<legend>';
59 $html .= sprintf(
60 __('Create version %1$s of %2$s'),
61 ($last_version + 1),
62 htmlspecialchars($GLOBALS['db'] . '.' . $GLOBALS['table'])
64 $html .= '</legend>';
65 $html .= '<input type="hidden" name="version" value="' . ($last_version + 1)
66 . '" />';
67 $html .= '<p>' . __('Track these data definition statements:')
68 . '</p>';
69 $html .= '<input type="checkbox" name="alter_table" value="true"'
70 . ' checked="checked" /> ALTER TABLE<br/>';
71 $html .= '<input type="checkbox" name="rename_table" value="true"'
72 . ' checked="checked" /> RENAME TABLE<br/>';
73 $html .= '<input type="checkbox" name="create_table" value="true"'
74 . ' checked="checked" /> CREATE TABLE<br/>';
75 $html .= '<input type="checkbox" name="drop_table" value="true"'
76 . ' checked="checked" /> DROP TABLE<br/>';
77 $html .= '<br/>';
78 $html .= '<input type="checkbox" name="create_index" value="true"'
79 . ' checked="checked" /> CREATE INDEX<br/>';
80 $html .= '<input type="checkbox" name="drop_index" value="true"'
81 . ' checked="checked" /> DROP INDEX<br/>';
82 $html .= '<p>' . __('Track these data manipulation statements:') . '</p>';
83 $html .= '<input type="checkbox" name="insert" value="true"'
84 . ' checked="checked" /> INSERT<br/>';
85 $html .= '<input type="checkbox" name="update" value="true"'
86 . ' checked="checked" /> UPDATE<br/>';
87 $html .= '<input type="checkbox" name="delete" value="true"'
88 . ' checked="checked" /> DELETE<br/>';
89 $html .= '<input type="checkbox" name="truncate" value="true"'
90 . ' checked="checked" /> TRUNCATE<br/>';
91 $html .= '</fieldset>';
93 $html .= '<fieldset class="tblFooters">';
94 $html .= '<input type="hidden" name="submit_create_version" value="1" />';
95 $html .= '<input type="submit" value="' . __('Create version') . '" />';
96 $html .= '</fieldset>';
98 $html .= '</form>';
99 $html .= '</div>';
101 return $html;
105 * Function to get html for activate tracking
107 * @param string $url_query url query
108 * @param int $last_version last version
110 * @return string
112 function PMA_getHtmlForActivateTracking($url_query, $last_version)
114 $html = '<div id="div_activate_tracking">';
115 $html .= '<form method="post" action="tbl_tracking.php?' . $url_query . '">';
116 $html .= '<fieldset>';
117 $html .= '<legend>';
118 $html .= sprintf(
119 __('Activate tracking for %s'),
120 htmlspecialchars($GLOBALS['db'] . '.' . $GLOBALS['table'])
122 $html .= '</legend>';
123 $html .= '<input type="hidden" name="version" value="' . $last_version . '" />';
124 $html .= '<input type="hidden" name="submit_activate_now" value="1" />';
125 $html .= '<input type="submit" value="' . __('Activate now') . '" />';
126 $html .= '</fieldset>';
127 $html .= '</form>';
128 $html .= '</div>';
130 return $html;
134 * Function to get html for deactivating tracking
136 * @param string $url_query url query
137 * @param int $last_version last version
139 * @return string
141 function PMA_getHtmlForDeactivateTracking($url_query, $last_version)
143 $html = '<div id="div_deactivate_tracking">';
144 $html .= '<form method="post" action="tbl_tracking.php?' . $url_query . '">';
145 $html .= '<fieldset>';
146 $html .= '<legend>';
147 $html .= sprintf(
148 __('Deactivate tracking for %s'),
149 htmlspecialchars($GLOBALS['db'] . '.' . $GLOBALS['table'])
151 $html .= '</legend>';
152 $html .= '<input type="hidden" name="version" value="' . $last_version . '" />';
153 $html .= '<input type="hidden" name="submit_deactivate_now" value="1" />';
154 $html .= '<input type="submit" value="' . __('Deactivate now') . '" />';
155 $html .= '</fieldset>';
156 $html .= '</form>';
157 $html .= '</div>';
159 return $html;
163 * Function to get the list versions of the table
165 * @return array
167 function PMA_getListOfVersionsOfTable()
169 $sql_query = " SELECT * FROM " .
170 PMA_Util::backquote($GLOBALS['cfg']['Server']['pmadb']) . "." .
171 PMA_Util::backquote($GLOBALS['cfg']['Server']['tracking']) .
172 " WHERE db_name = '" . PMA_Util::sqlAddSlashes($_REQUEST['db']) . "' ".
173 " AND table_name = '" . PMA_Util::sqlAddSlashes($_REQUEST['table']) ."' ".
174 " ORDER BY version DESC ";
176 return PMA_queryAsControlUser($sql_query);
180 * Function to get html for displaying last version number
182 * @param array $sql_result sql result
183 * @param int $last_version last version
184 * @param array $url_params url parameters
185 * @param string $url_query url query
187 * @return string
189 function PMA_getHtmlForTableVersionDetails($sql_result, $last_version, $url_params,
190 $url_query
192 $html = '<table id="versions" class="data">';
193 $html .= '<thead>';
194 $html .= '<tr>';
195 $html .= '<th>' . __('Database') . '</th>';
196 $html .= '<th>' . __('Table') . '</th>';
197 $html .= '<th>' . __('Version') . '</th>';
198 $html .= '<th>' . __('Created') . '</th>';
199 $html .= '<th>' . __('Updated') . '</th>';
200 $html .= '<th>' . __('Status') . '</th>';
201 $html .= '<th>' . __('Show') . '</th>';
202 $html .= '</tr>';
203 $html .= '</thead>';
204 $html .= '<tbody>';
206 $style = 'odd';
207 $GLOBALS['dbi']->dataSeek($sql_result, 0);
208 while ($version = $GLOBALS['dbi']->fetchArray($sql_result)) {
209 if ($version['tracking_active'] == 1) {
210 $version_status = __('active');
211 } else {
212 $version_status = __('not active');
214 if ($version['version'] == $last_version) {
215 if ($version['tracking_active'] == 1) {
216 $tracking_active = true;
217 } else {
218 $tracking_active = false;
221 $html .= '<tr class="noclick ' . $style . '">';
222 $html .= '<td>' . htmlspecialchars($version['db_name']) . '</td>';
223 $html .= '<td>' . htmlspecialchars($version['table_name']) . '</td>';
224 $html .= '<td>' . htmlspecialchars($version['version']) . '</td>';
225 $html .= '<td>' . htmlspecialchars($version['date_created']) . '</td>';
226 $html .= '<td>' . htmlspecialchars($version['date_updated']) . '</td>';
227 $html .= '<td>' . $version_status . '</td>';
228 $html .= '<td><a href="tbl_tracking.php';
229 $html .= PMA_URL_getCommon(
230 $url_params + array(
231 'report' => 'true', 'version' => $version['version']
234 $html .= '">' . __('Tracking report') . '</a>';
235 $html .= '| <a href="tbl_tracking.php';
236 $html .= PMA_URL_getCommon(
237 $url_params + array(
238 'snapshot' => 'true', 'version' => $version['version']
241 $html .= '">' . __('Structure snapshot') . '</a>';
242 $html .= '</td>';
243 $html .= '</tr>';
245 if ($style == 'even') {
246 $style = 'odd';
247 } else {
248 $style = 'even';
252 $html .= '</tbody>';
253 $html .= '</table>';
255 if ($tracking_active) {
256 $html .= PMA_getHtmlForDeactivateTracking($url_query, $last_version);
257 } else {
258 $html .= PMA_getHtmlForActivateTracking($url_query, $last_version);
261 return $html;
265 * Function to get the last version number of a table
267 * @param array $sql_result sql result
269 * @return int
271 function PMA_getTableLastVersionNumber($sql_result)
273 $maxversion = $GLOBALS['dbi']->fetchArray($sql_result);
274 $last_version = $maxversion['version'];
276 return $last_version;
280 * Function to get sql results for selectable tables
282 * @return array
284 function PMA_getSQLResultForSelectableTables()
286 include_once 'libraries/relation.lib.php';
288 $sql_query = " SELECT DISTINCT db_name, table_name FROM " .
289 PMA_Util::backquote($GLOBALS['cfg']['Server']['pmadb']) . "." .
290 PMA_Util::backquote($GLOBALS['cfg']['Server']['tracking']) .
291 " WHERE db_name = '" . PMA_Util::sqlAddSlashes($GLOBALS['db']) . "' " .
292 " ORDER BY db_name, table_name";
294 return PMA_queryAsControlUser($sql_query);
298 * Function to get html for selectable table rows
300 * @param array $selectable_tables_sql_result sql results for selectable rows
301 * @param string $url_query url query
303 * @return string
305 function PMA_getHtmlForSelectableTables($selectable_tables_sql_result, $url_query)
307 $html = '<form method="post" action="tbl_tracking.php?' . $url_query . '">';
308 $html .= '<select name="table">';
309 while ($entries = $GLOBALS['dbi']->fetchArray($selectable_tables_sql_result)) {
310 if (PMA_Tracker::isTracked($entries['db_name'], $entries['table_name'])) {
311 $status = ' (' . __('active') . ')';
312 } else {
313 $status = ' (' . __('not active') . ')';
315 if ($entries['table_name'] == $_REQUEST['table']) {
316 $s = ' selected="selected"';
317 } else {
318 $s = '';
320 $html .= '<option value="' . htmlspecialchars($entries['table_name'])
321 . '"' . $s . '>' . htmlspecialchars($entries['db_name']) . ' . '
322 . htmlspecialchars($entries['table_name']) . $status . '</option>'
323 . "\n";
325 $html .= '</select>';
326 $html .= '<input type="hidden" name="show_versions_submit" value="1" />';
327 $html .= '<input type="submit" value="' . __('Show versions') . '" />';
328 $html .= '</form>';
330 return $html;
334 * Function to get html for tracking report and tracking report export
336 * @param string $url_query url query
337 * @param array $data data
338 * @param array $url_params url params
339 * @param array $selection_schema selection schema
340 * @param array $selection_data selection data
341 * @param bool $selection_both selection both
342 * @param int $filter_ts_to filter time stamp from
343 * @param int $filter_ts_from filter time stamp tp
344 * @param array $filter_users filter users
346 * @return string
348 function PMA_getHtmlForTrackingReport($url_query, $data, $url_params,
349 $selection_schema, $selection_data, $selection_both, $filter_ts_to,
350 $filter_ts_from, $filter_users
352 $html = '<h3>' . __('Tracking report')
353 . ' [<a href="tbl_tracking.php?' . $url_query . '">' . __('Close')
354 . '</a>]</h3>';
356 $html .= '<small>' . __('Tracking statements') . ' '
357 . htmlspecialchars($data['tracking']) . '</small><br/>';
358 $html .= '<br/>';
360 $html .= '<form method="post" action="tbl_tracking.php'
361 . PMA_URL_getCommon(
362 $url_params + array(
363 'report' => 'true', 'version' => $_REQUEST['version']
366 . '">';
368 $str1 = '<select name="logtype">'
369 . '<option value="schema"'
370 . ($selection_schema ? ' selected="selected"' : '') . '>'
371 . __('Structure only') . '</option>'
372 . '<option value="data"'
373 . ($selection_data ? ' selected="selected"' : ''). '>'
374 . __('Data only') . '</option>'
375 . '<option value="schema_and_data"'
376 . ($selection_both ? ' selected="selected"' : '') . '>'
377 . __('Structure and data') . '</option>'
378 . '</select>';
379 $str2 = '<input type="text" name="date_from" value="'
380 . htmlspecialchars($_REQUEST['date_from']) . '" size="19" />';
381 $str3 = '<input type="text" name="date_to" value="'
382 . htmlspecialchars($_REQUEST['date_to']) . '" size="19" />';
383 $str4 = '<input type="text" name="users" value="'
384 . htmlspecialchars($_REQUEST['users']) . '" />';
385 $str5 = '<input type="hidden" name="list_report" value="1" />'
386 . '<input type="submit" value="' . __('Go') . '" />';
388 $html .= sprintf(
389 __('Show %1$s with dates from %2$s to %3$s by user %4$s %5$s'),
390 $str1, $str2, $str3, $str4, $str5
393 // Prepare delete link content here
394 $drop_image_or_text = '';
395 if (PMA_Util::showIcons('ActionsLinksMode')) {
396 $drop_image_or_text .= PMA_Util::getImage(
397 'b_drop.png', __('Delete tracking data row from report')
400 if (PMA_Util::showText('ActionLinksMode')) {
401 $drop_image_or_text .= __('Delete');
405 * First, list tracked data definition statements
407 if (count($data['ddlog']) == 0 && count($data['dmlog']) == 0) {
408 $msg = PMA_Message::notice(__('No data'));
409 $msg->display();
412 if ($selection_schema || $selection_both && count($data['ddlog']) > 0) {
413 list($temp, $ddlog_count) = PMA_getHtmlForDataDefinitionStatements(
414 $data, $filter_users, $filter_ts_from, $filter_ts_to, $url_params,
415 $drop_image_or_text
417 $html .= $temp;
418 unset($temp);
419 } //endif
422 * Secondly, list tracked data manipulation statements
424 if (($selection_data || $selection_both) && count($data['dmlog']) > 0) {
425 $html .= PMA_getHtmlForDataManipulationStatements(
426 $data, $filter_users, $filter_ts_from, $filter_ts_to, $url_params,
427 $ddlog_count, $drop_image_or_text
430 $html .= '</form>';
431 $html .= '<form method="post" action="tbl_tracking.php'
432 . PMA_URL_getCommon(
433 $url_params + array(
434 'report' => 'true', 'version' => $_REQUEST['version']
437 . '">';
438 $html .= sprintf(
439 __('Show %1$s with dates from %2$s to %3$s by user %4$s %5$s'),
440 $str1, $str2, $str3, $str4, $str5
443 $str_export1 = '<select name="export_type">'
444 . '<option value="sqldumpfile">' . __('SQL dump (file download)')
445 . '</option>'
446 . '<option value="sqldump">' . __('SQL dump') . '</option>'
447 . '<option value="execution" onclick="alert(\''
448 . PMA_escapeJsString(
449 __('This option will replace your table and contained data.')
451 .'\')">' . __('SQL execution') . '</option>' . '</select>';
453 $str_export2 = '<input type="hidden" name="report_export" value="1" />'
454 . '<input type="submit" value="' . __('Go') .'" />';
455 $html .= '</form>';
456 $html .= '<form class="disableAjax" method="post" action="tbl_tracking.php'
457 . PMA_URL_getCommon(
458 $url_params
459 + array('report' => 'true', 'version' => $_REQUEST['version'])
461 . '">';
462 $html .= '<input type="hidden" name="logtype" value="'
463 . htmlspecialchars($_REQUEST['logtype']) . '" />';
464 $html .= '<input type="hidden" name="date_from" value="'
465 . htmlspecialchars($_REQUEST['date_from']) . '" />';
466 $html .= '<input type="hidden" name="date_to" value="'
467 . htmlspecialchars($_REQUEST['date_to']) . '" />';
468 $html .= '<input type="hidden" name="users" value="'
469 . htmlspecialchars($_REQUEST['users']) . '" />';
470 $html .= "<br/>" . sprintf(__('Export as %s'), $str_export1)
471 . $str_export2 . "<br/>";
472 $html .= '</form>';
473 $html .= "<br/><br/><hr/><br/>\n";
475 return $html;
478 * Function to get html for data manipulation statements
480 * @param array $data data
481 * @param array $filter_users filter users
482 * @param int $filter_ts_from filter time staml from
483 * @param int $filter_ts_to filter time stamp to
484 * @param array $url_params url parameters
485 * @param int $ddlog_count data definition log count
486 * @param string $drop_image_or_text drop image or text
488 * @return string
490 function PMA_getHtmlForDataManipulationStatements($data, $filter_users,
491 $filter_ts_from, $filter_ts_to, $url_params, $ddlog_count,
492 $drop_image_or_text
494 $i = $ddlog_count;
495 $html = '<table id="dml_versions" class="data" width="100%">';
496 $html .= '<thead>';
497 $html .= '<tr>';
498 $html .= '<th width="18">#</th>';
499 $html .= '<th width="100">' . __('Date') . '</th>';
500 $html .= '<th width="60">' . __('Username') . '</th>';
501 $html .= '<th>' . __('Data manipulation statement') . '</th>';
502 $html .= '<th>' . __('Delete') . '</th>';
503 $html .= '</tr>';
504 $html .= '</thead>';
505 $html .= '<tbody>';
507 $style = 'odd';
508 foreach ($data['dmlog'] as $entry) {
509 $html .= PMA_getHtmlForDataManipulationStatement(
510 $entry, $filter_users, $filter_ts_from, $filter_ts_to, $style, $i,
511 $url_params, $ddlog_count, $drop_image_or_text
513 if ($style == 'even') {
514 $style = 'odd';
515 } else {
516 $style = 'even';
518 $i++;
520 $html .= '</tbody>';
521 $html .= '</table>';
523 return $html;
527 * Function to get html for one data manipulation statement
529 * @param array $entry entry
530 * @param array $filter_users filter users
531 * @param int $filter_ts_from filter time stamp from
532 * @param int $filter_ts_to filter time stamp to
533 * @param string $style style
534 * @param int $i field number
535 * @param array $url_params url parameters
536 * @param int $ddlog_count data definition log count
537 * @param string $drop_image_or_text drop image or text
539 * @return string
541 function PMA_getHtmlForDataManipulationStatement($entry, $filter_users,
542 $filter_ts_from, $filter_ts_to, $style, $i, $url_params, $ddlog_count,
543 $drop_image_or_text
545 $statement = PMA_Util::formatSql($entry['statement'], true);
546 $timestamp = strtotime($entry['date']);
547 $filtered_user = in_array($entry['username'], $filter_users);
548 if ($timestamp >= $filter_ts_from
549 && $timestamp <= $filter_ts_to
550 && (in_array('*', $filter_users) || $filtered_user)
552 $html = '<tr class="noclick ' . $style . '">';
553 $html .= '<td><small>' . $i . '</small></td>';
554 $html .= '<td><small>'
555 . htmlspecialchars($entry['date']) . '</small></td>';
556 $html .= '<td><small>'
557 . htmlspecialchars($entry['username']) . '</small></td>';
558 $html .= '<td>' . $statement . '</td>';
559 $html .= '<td class="nowrap"><a href="tbl_tracking.php?'
560 . PMA_URL_getCommon(
561 $url_params + array(
562 'report' => 'true',
563 'version' => $_REQUEST['version'],
564 'delete_dmlog' => ($i - $ddlog_count),
567 . '">'
568 . $drop_image_or_text
569 . '</a></td>';
570 $html .= '</tr>';
573 return $html;
576 * Function to get html for data definition statements in schema snapshot
578 * @param array $data data
579 * @param array $filter_users filter users
580 * @param int $filter_ts_from filter time stamp from
581 * @param int $filter_ts_to filter time stamp to
582 * @param array $url_params url parameters
583 * @param string $drop_image_or_text drop image or text
585 * @return string
587 function PMA_getHtmlForDataDefinitionStatements($data, $filter_users,
588 $filter_ts_from, $filter_ts_to, $url_params, $drop_image_or_text
590 $i = 1;
591 $html = '<table id="ddl_versions" class="data" width="100%">';
592 $html .= '<thead>';
593 $html .= '<tr>';
594 $html .= '<th width="18">#</th>';
595 $html .= '<th width="100">' . __('Date') . '</th>';
596 $html .= '<th width="60">' . __('Username') . '</th>';
597 $html .= '<th>' . __('Data definition statement') . '</th>';
598 $html .= '<th>' . __('Delete') . '</th>';
599 $html .= '</tr>';
600 $html .= '</thead>';
601 $html .= '<tbody>';
603 $style = 'odd';
604 foreach ($data['ddlog'] as $entry) {
605 $html .= PMA_getHtmlForDataDefinitionStatement(
606 $entry, $filter_users, $filter_ts_from, $filter_ts_to, $style, $i,
607 $url_params, $drop_image_or_text
609 if ($style == 'even') {
610 $style = 'odd';
611 } else {
612 $style = 'even';
614 $i++;
616 $html .= '</tbody>';
617 $html .= '</table>';
619 return array($html, $i);
622 * Function to get html for a data definition statement in schema snapshot
624 * @param array $entry entry
625 * @param array $filter_users filter users
626 * @param int $filter_ts_from filter time stamp from
627 * @param int $filter_ts_to filter time stamp to
628 * @param string $style style
629 * @param int $i column number
630 * @param array $url_params url parameters
631 * @param string $drop_image_or_text drop image or text
633 * @return string
635 function PMA_getHtmlForDataDefinitionStatement($entry, $filter_users,
636 $filter_ts_from, $filter_ts_to, $style, $i, $url_params, $drop_image_or_text
638 $statement = PMA_Util::formatSql($entry['statement'], true);
639 $timestamp = strtotime($entry['date']);
640 $filtered_user = in_array($entry['username'], $filter_users);
641 if ($timestamp >= $filter_ts_from
642 && $timestamp <= $filter_ts_to
643 && (in_array('*', $filter_users) || $filtered_user)
645 $html = '<tr class="noclick ' . $style . '">';
646 $html .= '<td><small>' . $i . '</small></td>';
647 $html .= '<td><small>'
648 . htmlspecialchars($entry['date']) . '</small></td>';
649 $html .= '<td><small>'
650 . htmlspecialchars($entry['username']) . '</small></td>';
651 $html .= '<td>' . $statement . '</td>';
652 $html .= '<td class="nowrap"><a href="tbl_tracking.php'
653 . PMA_URL_getCommon(
654 $url_params + array(
655 'report' => 'true',
656 'version' => $_REQUEST['version'],
657 'delete_ddlog' => ($i - 1),
660 . '">' . $drop_image_or_text
661 . '</a></td>';
662 $html .= '</tr>';
665 return $html;
668 * Function to get html for schema snapshot
670 * @param string $url_query url query
672 * @return string
674 function PMA_getHtmlForSchemaSnapshot($url_query)
676 $html = '<h3>' . __('Structure snapshot')
677 . ' [<a href="tbl_tracking.php?' . $url_query . '">' . __('Close')
678 . '</a>]</h3>';
679 $data = PMA_Tracker::getTrackedData(
680 $_REQUEST['db'], $_REQUEST['table'], $_REQUEST['version']
683 // Get first DROP TABLE/VIEW and CREATE TABLE/VIEW statements
684 $drop_create_statements = $data['ddlog'][0]['statement'];
686 if (strstr($data['ddlog'][0]['statement'], 'DROP TABLE')
687 || strstr($data['ddlog'][0]['statement'], 'DROP VIEW')
689 $drop_create_statements .= $data['ddlog'][1]['statement'];
691 // Print SQL code
692 $html .= PMA_Util::getMessage(
693 sprintf(
694 __('Version %s snapshot (SQL code)'),
695 htmlspecialchars($_REQUEST['version'])
697 $drop_create_statements
700 // Unserialize snapshot
701 $temp = unserialize($data['schema_snapshot']);
702 $columns = $temp['COLUMNS'];
703 $indexes = $temp['INDEXES'];
704 $html .= PMA_getHtmlForColumns($columns);
706 if (count($indexes) > 0) {
707 $html .= PMA_getHtmlForIndexes($indexes);
708 } // endif
709 $html .= '<br /><hr /><br />';
711 return $html;
715 * Function to get html for displaying columns in the schema snapshot
717 * @param array $columns columns
719 * @return string
721 function PMA_getHtmlForColumns($columns)
723 $html = '<h3>' . __('Structure') . '</h3>';
724 $html .= '<table id="tablestructure" class="data">';
725 $html .= '<thead>';
726 $html .= '<tr>';
727 $html .= '<th>' . __('Column') . '</th>';
728 $html .= '<th>' . __('Type') . '</th>';
729 $html .= '<th>' . __('Collation') . '</th>';
730 $html .= '<th>' . __('Null') . '</th>';
731 $html .= '<th>' . __('Default') . '</th>';
732 $html .= '<th>' . __('Extra') . '</th>';
733 $html .= '<th>' . __('Comment') . '</th>';
734 $html .= '</tr>';
735 $html .= '</thead>';
736 $html .= '<tbody>';
737 $style = 'odd';
738 foreach ($columns as $field) {
739 $html .= PMA_getHtmlForField($field, $style);
740 if ($style == 'even') {
741 $style = 'odd';
742 } else {
743 $style = 'even';
747 $html .= '</tbody>';
748 $html .= '</table>';
750 return $html;
754 * Function to get html for field
756 * @param array $field field
757 * @param string $style style
759 * @return string
761 function PMA_getHtmlForField($field, $style)
763 $html = '<tr class="noclick ' . $style . '">';
764 if ($field['Key'] == 'PRI') {
765 $html .= '<td><b><u>' . htmlspecialchars($field['Field']) . '</u></b></td>';
766 } else {
767 $html .= '<td><b>' . htmlspecialchars($field['Field']) . '</b></td>';
769 $html .= "\n";
770 $html .= '<td>' . htmlspecialchars($field['Type']) . '</td>';
771 $html .= '<td>' . htmlspecialchars($field['Collation']) . '</td>';
772 $html .= '<td>' . (($field['Null'] == 'YES') ? __('Yes') : __('No')) . '</td>';
773 $html .= '<td>';
774 if (isset($field['Default'])) {
775 $extracted_columnspec = PMA_Util::extractColumnSpec($field['Type']);
776 if ($extracted_columnspec['type'] == 'bit') {
777 // here, $field['Default'] contains something like b'010'
778 $html .= PMA_Util::convertBitDefaultValue($field['Default']);
779 } else {
780 $html .= htmlspecialchars($field['Default']);
782 } else {
783 if ($field['Null'] == 'YES') {
784 $html .= '<i>NULL</i>';
785 } else {
786 $html .= '<i>' . _pgettext('None for default', 'None') . '</i>';
789 $html .= '</td>';
790 $html .= '<td>' . htmlspecialchars($field['Extra']) . '</td>';
791 $html .= '<td>' . htmlspecialchars($field['Comment']) . '</td>';
792 $html .= '</tr>';
794 return $html;
798 * Fuunction to get html for the indexes in schema snapshot
800 * @param array $indexes indexes
802 * @return string
804 function PMA_getHtmlForIndexes($indexes)
806 $html = '<h3>' . __('Indexes') . '</h3>';
807 $html .= '<table id="tablestructure_indexes" class="data">';
808 $html .= '<thead>';
809 $html .= '<tr>';
810 $html .= '<th>' . __('Keyname') . '</th>';
811 $html .= '<th>' . __('Type') . '</th>';
812 $html .= '<th>' . __('Unique') . '</th>';
813 $html .= '<th>' . __('Packed') . '</th>';
814 $html .= '<th>' . __('Column') . '</th>';
815 $html .= '<th>' . __('Cardinality') . '</th>';
816 $html .= '<th>' . __('Collation') . '</th>';
817 $html .= '<th>' . __('Null') . '</th>';
818 $html .= '<th>' . __('Comment') . '</th>';
819 $html .= '</tr>';
820 $html .= '<tbody>';
822 $style = 'odd';
823 foreach ($indexes as $index) {
824 $html .= PMA_getHtmlForIndex($index, $style);
825 if ($style == 'even') {
826 $style = 'odd';
827 } else {
828 $style = 'even';
831 $html .= '</tbody>';
832 $html .= '</table>';
833 return $html;
837 * Funtion to get html for an index in schema snapshot
839 * @param array $index index
840 * @param string $style style
842 * @return string
844 function PMA_getHtmlForIndex($index, $style)
846 if ($index['Non_unique'] == 0) {
847 $str_unique = __('Yes');
848 } else {
849 $str_unique = __('No');
851 if ($index['Packed'] != '') {
852 $str_packed = __('Yes');
853 } else {
854 $str_packed = __('No');
857 $html = '<tr class="noclick ' . $style . '">';
858 $html .= '<td><b>' . htmlspecialchars($index['Key_name']) . '</b></td>';
859 $html .= '<td>' . htmlspecialchars($index['Index_type']) . '</td>';
860 $html .= '<td>' . $str_unique . '</td>';
861 $html .= '<td>' . $str_packed . '</td>';
862 $html .= '<td>' . htmlspecialchars($index['Column_name']) . '</td>';
863 $html .= '<td>' . htmlspecialchars($index['Cardinality']) . '</td>';
864 $html .= '<td>' . htmlspecialchars($index['Collation']) . '</td>';
865 $html .= '<td>' . htmlspecialchars($index['Null']) . '</td>';
866 $html .= '<td>' . htmlspecialchars($index['Comment']) . '</td>';
867 $html .= '</tr>';
869 return $html;
873 * Function to handle the tracking report
875 * @param array &$data tracked data
877 * @return void
879 function PMA_deleteTrackingReportRows(&$data)
881 if (isset($_REQUEST['delete_ddlog'])) {
882 // Delete ddlog row data
883 PMA_handleDeleteDataDefinitionsLog($data);
886 if (isset($_REQUEST['delete_dmlog'])) {
887 // Delete dmlog row data
888 PMA_handleDeleteDataManipulationLog($data);
893 * Function to handle the delete ddlog row data
895 * @param array &$data tracked data
897 * @return void
899 function PMA_handleDeleteDataDefinitionsLog(&$data)
901 $delete_id = $_REQUEST['delete_ddlog'];
903 // Only in case of valable id
904 if ($delete_id == (int)$delete_id) {
905 unset($data['ddlog'][$delete_id]);
907 $successfullyDeleted = PMA_Tracker::changeTrackingData(
908 $_REQUEST['db'], $_REQUEST['table'],
909 $_REQUEST['version'], 'DDL', $data['ddlog']
911 if ($successfullyDeleted) {
912 $msg = PMA_Message::success(
913 __('Tracking data definition successfully deleted')
915 } else {
916 $msg = PMA_Message::rawError(__('Query error'));
918 $msg->display();
923 * Function to handle the delete of fmlog rows
925 * @param array &$data tracked data
927 * @return void
929 function PMA_handleDeleteDataManipulationLog(&$data)
931 $delete_id = $_REQUEST['delete_dmlog'];
933 // Only in case of valable id
934 if ($delete_id == (int)$delete_id) {
935 unset($data['dmlog'][$delete_id]);
937 $successfullyDeleted = PMA_Tracker::changeTrackingData(
938 $_REQUEST['db'], $_REQUEST['table'],
939 $_REQUEST['version'], 'DML', $data['dmlog']
941 if ($successfullyDeleted) {
942 $msg = PMA_Message::success(
943 __('Tracking data manipulation successfully deleted')
945 } else {
946 $msg = PMA_Message::rawError(__('Query error'));
948 $msg->display();
953 * Function to export as sql dump
955 * @param array $entries entries
957 * @return void
959 function PMA_exportAsSQLDump($entries)
961 $new_query = "# "
962 . __(
963 'You can execute the dump by creating and using a temporary database. '
964 . 'Please ensure that you have the privileges to do so.'
966 . "\n"
967 . "# " . __('Comment out these two lines if you do not need them.') . "\n"
968 . "\n"
969 . "CREATE database IF NOT EXISTS pma_temp_db; \n"
970 . "USE pma_temp_db; \n"
971 . "\n";
973 foreach ($entries as $entry) {
974 $new_query .= $entry['statement'];
976 $msg = PMA_Message::success(
977 __('SQL statements exported. Please copy the dump or execute it.')
979 $msg->display();
981 $db_temp = $GLOBALS['db'];
982 $table_temp = $GLOBALS['table'];
984 $GLOBALS['db'] = $GLOBALS['table'] = '';
985 include_once './libraries/sql_query_form.lib.php';
987 PMA_getHtmlForSqlQueryForm($new_query, 'sql');
989 $GLOBALS['db'] = $db_temp;
990 $GLOBALS['table'] = $table_temp;
994 * Function to export as sql execution
996 * @param array $entries entries
998 * @return array
1000 function PMA_exportAsSQLExecution($entries)
1002 foreach ($entries as $entry) {
1003 $sql_result = $GLOBALS['dbi']->query("/*NOTRACK*/\n" . $entry['statement']);
1005 $msg = PMA_Message::success(__('SQL statements executed.'));
1006 $msg->display();
1008 return $sql_result;
1012 * Function to export as entries
1014 * @param array $entries entries
1016 * @return void
1018 function PMA_exportAsFileDownload($entries)
1020 @ini_set('url_rewriter.tags', '');
1022 $dump = "# " . sprintf(
1023 __('Tracking report for table `%s`'), htmlspecialchars($_REQUEST['table'])
1025 . "\n" . "# " . date('Y-m-d H:i:s') . "\n";
1026 foreach ($entries as $entry) {
1027 $dump .= $entry['statement'];
1029 $filename = 'log_' . htmlspecialchars($_REQUEST['table']) . '.sql';
1030 PMA_downloadHeader($filename, 'text/x-sql', strlen($dump));
1032 $response = PMA_Response::getInstance();
1033 $response->addHTML($dump);
1035 exit();
1039 * Function to activate tracking
1041 * @return void
1043 function PMA_activateTracking()
1045 $activated = PMA_Tracker::activateTracking(
1046 $GLOBALS['db'], $GLOBALS['table'], $_REQUEST['version']
1048 if ($activated) {
1049 $msg = PMA_Message::success(
1050 sprintf(
1051 __('Tracking for %1$s was activated at version %2$s.'),
1052 htmlspecialchars($GLOBALS['db'] . '.' . $GLOBALS['table']),
1053 htmlspecialchars($_REQUEST['version'])
1056 $msg->display();
1061 * Function to deactivate tracking
1063 * @return void
1065 function PMA_deactivateTracking()
1067 $deactivated = PMA_Tracker::deactivateTracking(
1068 $GLOBALS['db'], $GLOBALS['table'], $_REQUEST['version']
1070 if ($deactivated) {
1071 $msg = PMA_Message::success(
1072 sprintf(
1073 __('Tracking for %1$s was deactivated at version %2$s.'),
1074 htmlspecialchars($GLOBALS['db'] . '.' . $GLOBALS['table']),
1075 htmlspecialchars($_REQUEST['version'])
1078 $msg->display();
1083 * Function to get tracking set
1085 * @return string
1087 function PMA_getTrackingSet()
1089 $tracking_set = '';
1091 if ($_REQUEST['alter_table'] == true) {
1092 $tracking_set .= 'ALTER TABLE,';
1094 if ($_REQUEST['rename_table'] == true) {
1095 $tracking_set .= 'RENAME TABLE,';
1097 if ($_REQUEST['create_table'] == true) {
1098 $tracking_set .= 'CREATE TABLE,';
1100 if ($_REQUEST['drop_table'] == true) {
1101 $tracking_set .= 'DROP TABLE,';
1103 if ($_REQUEST['create_index'] == true) {
1104 $tracking_set .= 'CREATE INDEX,';
1106 if ($_REQUEST['drop_index'] == true) {
1107 $tracking_set .= 'DROP INDEX,';
1109 if ($_REQUEST['insert'] == true) {
1110 $tracking_set .= 'INSERT,';
1112 if ($_REQUEST['update'] == true) {
1113 $tracking_set .= 'UPDATE,';
1115 if ($_REQUEST['delete'] == true) {
1116 $tracking_set .= 'DELETE,';
1118 if ($_REQUEST['truncate'] == true) {
1119 $tracking_set .= 'TRUNCATE,';
1121 $tracking_set = rtrim($tracking_set, ',');
1123 return $tracking_set;
1127 * Function to create the tracking version
1129 * @return void
1131 function PMA_createTrackingVersion()
1133 $tracking_set = PMA_getTrackingSet();
1135 $versionCreated = PMA_Tracker::createVersion(
1136 $GLOBALS['db'],
1137 $GLOBALS['table'],
1138 $_REQUEST['version'],
1139 $tracking_set,
1140 PMA_Table::isView($GLOBALS['db'], $GLOBALS['table'])
1142 if ($versionCreated) {
1143 $msg = PMA_Message::success(
1144 sprintf(
1145 __('Version %1$s was created, tracking for %2$s is active.'),
1146 htmlspecialchars($_REQUEST['version']),
1147 htmlspecialchars($GLOBALS['db'] . '.' . $GLOBALS['table'])
1150 $msg->display();
1155 * Function to get the entries
1157 * @param array $data data
1158 * @param int $filter_ts_from filter time stamp from
1159 * @param int $filter_ts_to filter time stamp to
1160 * @param array $filter_users filter users
1162 * @return array
1164 function PMA_getEntries($data, $filter_ts_from, $filter_ts_to, $filter_users)
1166 $entries = array();
1167 // Filtering data definition statements
1168 if ($_REQUEST['logtype'] == 'schema'
1169 || $_REQUEST['logtype'] == 'schema_and_data'
1171 $entries = array_merge(
1172 $entries,
1173 PMA_filterTracking(
1174 $data['ddlog'], $filter_ts_from, $filter_ts_to, $filter_users
1179 // Filtering data manipulation statements
1180 if ($_REQUEST['logtype'] == 'data'
1181 || $_REQUEST['logtype'] == 'schema_and_data'
1183 $entries = array_merge(
1184 $entries,
1185 PMA_filterTracking(
1186 $data['dmlog'], $filter_ts_from, $filter_ts_to, $filter_users
1191 // Sort it
1192 foreach ($entries as $key => $row) {
1193 $ids[$key] = $row['id'];
1194 $timestamps[$key] = $row['timestamp'];
1195 $usernames[$key] = $row['username'];
1196 $statements[$key] = $row['statement'];
1199 array_multisort(
1200 $timestamps, SORT_ASC, $ids, SORT_ASC, $usernames,
1201 SORT_ASC, $statements, SORT_ASC, $entries
1204 return $entries;