Translation update done using Pootle.
[phpmyadmin/ammaryasirr.git] / libraries / Tracker.class.php
blobde216ab9c5ce166d14c4be38942cbcb5337163a0
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
5 * @package phpMyAdmin
6 */
8 /**
9 * This class tracks changes on databases, tables and views.
10 * For more information please see phpMyAdmin/Documentation.html
12 * @package phpMyAdmin
14 * @todo use stristr instead of strstr
16 class PMA_Tracker
18 /**
19 * Whether tracking is ready.
21 static protected $enabled = false;
23 /**
24 * Defines the internal PMA table which contains tracking data.
26 * @access protected
27 * @var string
29 static protected $pma_table;
31 /**
32 * Defines the usage of DROP TABLE statment in SQL dumps.
34 * @access protected
35 * @var boolean
37 static protected $add_drop_table;
39 /**
40 * Defines the usage of DROP VIEW statment in SQL dumps.
42 * @access protected
43 * @var boolean
45 static protected $add_drop_view;
47 /**
48 * Defines the usage of DROP DATABASE statment in SQL dumps.
50 * @access protected
51 * @var boolean
53 static protected $add_drop_database;
55 /**
56 * Defines auto-creation of tracking versions.
58 * @var boolean
60 static protected $version_auto_create;
62 /**
63 * Defines the default set of tracked statements.
65 * @var string
67 static protected $default_tracking_set;
69 /**
70 * Initializes settings. See phpMyAdmin/Documentation.html.
72 * @static
75 static public function init()
77 self::$pma_table = PMA_backquote($GLOBALS['cfg']['Server']['pmadb']) .".".
78 PMA_backquote($GLOBALS['cfg']['Server']['tracking']);
80 self::$add_drop_table = $GLOBALS['cfg']['Server']['tracking_add_drop_table'];
82 self::$add_drop_view = $GLOBALS['cfg']['Server']['tracking_add_drop_view'];
84 self::$add_drop_database = $GLOBALS['cfg']['Server']['tracking_add_drop_database'];
86 self::$default_tracking_set = $GLOBALS['cfg']['Server']['tracking_default_statements'];
88 self::$version_auto_create = $GLOBALS['cfg']['Server']['tracking_version_auto_create'];
92 /**
93 * Actually enables tracking. This needs to be done after all
94 * underlaying code is initialized.
96 * @static
99 static public function enable()
101 self::$enabled = true;
105 * Gets the on/off value of the Tracker module, starts initialization.
107 * @static
109 * @return boolean (true=on|false=off)
111 static public function isActive()
113 if (! self::$enabled) {
114 return false;
116 /* We need to avoid attempt to track any queries from PMA_getRelationsParam */
117 self::$enabled = false;
118 $cfgRelation = PMA_getRelationsParam();
119 /* Restore original state */
120 self::$enabled = true;
121 if (! $cfgRelation['trackingwork']) {
122 return false;
124 self::init();
126 if (isset(self::$pma_table)) {
127 return true;
128 } else {
129 return false;
134 * Parses the name of a table from a SQL statement substring.
136 * @static
138 * @param string $string part of SQL statement
140 * @return string the name of table
142 static protected function getTableName($string)
144 if (strstr($string, '.')) {
145 $temp = explode('.', $string);
146 $tablename = $temp[1];
148 else {
149 $tablename = $string;
152 $str = explode("\n", $tablename);
153 $tablename = $str[0];
155 $tablename = str_replace(';', '', $tablename);
156 $tablename = str_replace('`', '', $tablename);
157 $tablename = trim($tablename);
159 return $tablename;
164 * Gets the tracking status of a table, is it active or deactive ?
166 * @static
168 * @param string $dbname name of database
169 * @param string $tablename name of table
171 * @return boolean true or false
173 static public function isTracked($dbname, $tablename)
175 if (! self::$enabled) {
176 return false;
178 /* We need to avoid attempt to track any queries from PMA_getRelationsParam */
179 self::$enabled = false;
180 $cfgRelation = PMA_getRelationsParam();
181 /* Restore original state */
182 self::$enabled = true;
183 if (! $cfgRelation['trackingwork']) {
184 return false;
187 $sql_query =
188 " SELECT tracking_active FROM " . self::$pma_table .
189 " WHERE db_name = '" . PMA_sqlAddSlashes($dbname) . "' " .
190 " AND table_name = '" . PMA_sqlAddSlashes($tablename) . "' " .
191 " ORDER BY version DESC";
193 $row = PMA_DBI_fetch_array(PMA_query_as_controluser($sql_query));
195 if (isset($row['tracking_active']) && $row['tracking_active'] == 1) {
196 return true;
197 } else {
198 return false;
203 * Returns the comment line for the log.
205 * @return string Comment, contains date and username
207 static public function getLogComment()
209 $date = date('Y-m-d H:i:s');
211 return "# log " . $date . " " . $GLOBALS['cfg']['Server']['user'] . "\n";
215 * Creates tracking version of a table / view
216 * (in other words: create a job to track future changes on the table).
218 * @static
220 * @param string $dbname name of database
221 * @param string $tablename name of table
222 * @param string $version version
223 * @param string $tracking_set set of tracking statements
224 * @param bool $is_view if table is a view
226 * @return int result of version insertion
228 static public function createVersion($dbname, $tablename, $version, $tracking_set = '', $is_view = false)
230 global $sql_backquotes;
232 if ($tracking_set == '') {
233 $tracking_set = self::$default_tracking_set;
236 require_once './libraries/export/sql.php';
238 $sql_backquotes = true;
240 $date = date('Y-m-d H:i:s');
242 // Get data definition snapshot of table
244 $columns = PMA_DBI_get_columns($dbname, $tablename, true);
245 // int indices to reduce size
246 $columns = array_values($columns);
247 // remove Privileges to reduce size
248 for ($i = 0; $i < count($columns); $i++) {
249 unset($columns[$i]['Privileges']);
252 $sql_query = '
253 SHOW INDEX FROM ' . PMA_backquote($dbname) . '.' . PMA_backquote($tablename);
255 $sql_result = PMA_DBI_query($sql_query);
257 $indexes = array();
259 while($row = PMA_DBI_fetch_assoc($sql_result)) {
260 $indexes[] = $row;
263 $snapshot = array('COLUMNS' => $columns, 'INDEXES' => $indexes);
264 $snapshot = serialize($snapshot);
266 // Get DROP TABLE / DROP VIEW and CREATE TABLE SQL statements
267 $sql_backquotes = true;
269 $create_sql = "";
271 if (self::$add_drop_table == true && $is_view == false) {
272 $create_sql .= self::getLogComment() .
273 'DROP TABLE IF EXISTS ' . PMA_backquote($tablename) . ";\n";
277 if (self::$add_drop_view == true && $is_view == true) {
278 $create_sql .= self::getLogComment() .
279 'DROP VIEW IF EXISTS ' . PMA_backquote($tablename) . ";\n";
282 $create_sql .= self::getLogComment() .
283 PMA_getTableDef($dbname, $tablename, "\n", "");
285 // Save version
287 $sql_query =
288 "/*NOTRACK*/\n" .
289 "INSERT INTO" . self::$pma_table . " (" .
290 "db_name, " .
291 "table_name, " .
292 "version, " .
293 "date_created, " .
294 "date_updated, " .
295 "schema_snapshot, " .
296 "schema_sql, " .
297 "data_sql, " .
298 "tracking " .
299 ") " .
300 "values (
301 '" . PMA_sqlAddSlashes($dbname) . "',
302 '" . PMA_sqlAddSlashes($tablename) . "',
303 '" . PMA_sqlAddSlashes($version) . "',
304 '" . PMA_sqlAddSlashes($date) . "',
305 '" . PMA_sqlAddSlashes($date) . "',
306 '" . PMA_sqlAddSlashes($snapshot) . "',
307 '" . PMA_sqlAddSlashes($create_sql) . "',
308 '" . PMA_sqlAddSlashes("\n") . "',
309 '" . PMA_sqlAddSlashes($tracking_set) . "' )";
311 $result = PMA_query_as_controluser($sql_query);
313 if ($result) {
314 // Deactivate previous version
315 self::deactivateTracking($dbname, $tablename, ($version - 1));
318 return $result;
323 * Removes all tracking data for a table
325 * @static
327 * @param string $dbname name of database
328 * @param string $tablename name of table
330 * @return int result of version insertion
332 static public function deleteTracking($dbname, $tablename)
334 $sql_query =
335 "/*NOTRACK*/\n" .
336 "DELETE FROM " . self::$pma_table . " WHERE `db_name` = '" . PMA_sqlAddSlashes($dbname) . "' AND `table_name` = '" . PMA_sqlAddSlashes($tablename) . "'";
337 $result = PMA_query_as_controluser($sql_query);
339 return $result;
343 * Creates tracking version of a database
344 * (in other words: create a job to track future changes on the database).
346 * @static
348 * @param string $dbname name of database
349 * @param string $version version
350 * @param string $query query
351 * @param string $tracking_set set of tracking statements
353 * @return int result of version insertion
355 static public function createDatabaseVersion($dbname, $version, $query, $tracking_set = 'CREATE DATABASE,ALTER DATABASE,DROP DATABASE')
357 $date = date('Y-m-d H:i:s');
359 if ($tracking_set == '') {
360 $tracking_set = self::$default_tracking_set;
363 require_once './libraries/export/sql.php';
365 $create_sql = "";
367 if (self::$add_drop_database == true) {
368 $create_sql .= self::getLogComment() .
369 'DROP DATABASE IF EXISTS ' . PMA_backquote($dbname) . ";\n";
372 $create_sql .= self::getLogComment() . $query;
374 // Save version
375 $sql_query =
376 "/*NOTRACK*/\n" .
377 "INSERT INTO" . self::$pma_table . " (" .
378 "db_name, " .
379 "table_name, " .
380 "version, " .
381 "date_created, " .
382 "date_updated, " .
383 "schema_snapshot, " .
384 "schema_sql, " .
385 "data_sql, " .
386 "tracking " .
387 ") " .
388 "values (
389 '" . PMA_sqlAddSlashes($dbname) . "',
390 '" . PMA_sqlAddSlashes('') . "',
391 '" . PMA_sqlAddSlashes($version) . "',
392 '" . PMA_sqlAddSlashes($date) . "',
393 '" . PMA_sqlAddSlashes($date) . "',
394 '" . PMA_sqlAddSlashes('') . "',
395 '" . PMA_sqlAddSlashes($create_sql) . "',
396 '" . PMA_sqlAddSlashes("\n") . "',
397 '" . PMA_sqlAddSlashes($tracking_set) . "' )";
399 $result = PMA_query_as_controluser($sql_query);
401 return $result;
407 * Changes tracking of a table.
409 * @static
411 * @param string $dbname name of database
412 * @param string $tablename name of table
413 * @param string $version version
414 * @param integer $new_state the new state of tracking
416 * @return int result of SQL query
418 static private function changeTracking($dbname, $tablename, $version, $new_state)
420 $sql_query =
421 " UPDATE " . self::$pma_table .
422 " SET `tracking_active` = '" . $new_state . "' " .
423 " WHERE `db_name` = '" . PMA_sqlAddSlashes($dbname) . "' " .
424 " AND `table_name` = '" . PMA_sqlAddSlashes($tablename) . "' " .
425 " AND `version` = '" . PMA_sqlAddSlashes($version) . "' ";
427 $result = PMA_query_as_controluser($sql_query);
429 return $result;
433 * Changes tracking data of a table.
435 * @static
437 * @param string $dbname name of database
438 * @param string $tablename name of table
439 * @param string $version version
440 * @param string $type type of data(DDL || DML)
441 * @param string|array $new_data the new tracking data
443 * @return bool result of change
445 static public function changeTrackingData($dbname, $tablename, $version, $type, $new_data)
447 if ($type == 'DDL')
448 $save_to = 'schema_sql';
449 elseif ($type == 'DML')
450 $save_to = 'data_sql';
451 else
452 return false;
454 $date = date('Y-m-d H:i:s');
456 $new_data_processed = '';
457 if (is_array($new_data)) {
458 foreach ($new_data as $data) {
459 $new_data_processed .= '# log ' . $date . ' ' . $data['username'] . PMA_sqlAddSlashes($data['statement']) . "\n";
461 } else {
462 $new_data_processed = $new_data;
465 $sql_query =
466 " UPDATE " . self::$pma_table .
467 " SET `" . $save_to . "` = '" . $new_data_processed . "' " .
468 " WHERE `db_name` = '" . PMA_sqlAddSlashes($dbname) . "' " .
469 " AND `table_name` = '" . PMA_sqlAddSlashes($tablename) . "' " .
470 " AND `version` = '" . PMA_sqlAddSlashes($version) . "' ";
472 $result = PMA_query_as_controluser($sql_query);
474 return $result;
478 * Activates tracking of a table.
480 * @static
482 * @param string $dbname name of database
483 * @param string $tablename name of table
484 * @param string $version version
486 * @return int result of SQL query
488 static public function activateTracking($dbname, $tablename, $version)
490 return self::changeTracking($dbname, $tablename, $version, 1);
495 * Deactivates tracking of a table.
497 * @static
499 * @param string $dbname name of database
500 * @param string $tablename name of table
501 * @param string $version version
503 * @return int result of SQL query
505 static public function deactivateTracking($dbname, $tablename, $version)
507 return self::changeTracking($dbname, $tablename, $version, 0);
512 * Gets the newest version of a tracking job
513 * (in other words: gets the HEAD version).
515 * @static
517 * @param string $dbname name of database
518 * @param string $tablename name of table
519 * @param string $statement tracked statement
521 * @return int (-1 if no version exists | > 0 if a version exists)
523 static public function getVersion($dbname, $tablename, $statement = null)
525 $sql_query =
526 " SELECT MAX(version) FROM " . self::$pma_table .
527 " WHERE `db_name` = '" . PMA_sqlAddSlashes($dbname) . "' " .
528 " AND `table_name` = '" . PMA_sqlAddSlashes($tablename) . "' ";
530 if ($statement != "") {
531 $sql_query .= " AND FIND_IN_SET('" . $statement . "',tracking) > 0" ;
533 $row = PMA_DBI_fetch_array(PMA_query_as_controluser($sql_query));
534 return isset($row[0])
535 ? $row[0]
536 : -1;
541 * Gets the record of a tracking job.
543 * @static
545 * @param string $dbname name of database
546 * @param string $tablename name of table
547 * @param string $version version number
549 * @return mixed record DDM log, DDL log, structure snapshot, tracked statements.
551 static public function getTrackedData($dbname, $tablename, $version)
553 if (! isset(self::$pma_table)) {
554 self::init();
556 $sql_query = " SELECT * FROM " . self::$pma_table .
557 " WHERE `db_name` = '" . PMA_sqlAddSlashes($dbname) . "' ";
558 if (! empty($tablename)) {
559 $sql_query .= " AND `table_name` = '" . PMA_sqlAddSlashes($tablename) ."' ";
561 $sql_query .= " AND `version` = '" . PMA_sqlAddSlashes($version) ."' ".
562 " ORDER BY `version` DESC LIMIT 1";
564 $mixed = PMA_DBI_fetch_assoc(PMA_query_as_controluser($sql_query));
566 // Parse log
567 $log_schema_entries = explode('# log ', $mixed['schema_sql']);
568 $log_data_entries = explode('# log ', $mixed['data_sql']);
570 $ddl_date_from = $date = date('Y-m-d H:i:s');
572 $ddlog = array();
573 $i = 0;
575 // Iterate tracked data definition statements
576 // For each log entry we want to get date, username and statement
577 foreach ($log_schema_entries as $log_entry) {
578 if (trim($log_entry) != '') {
579 $date = substr($log_entry, 0, 19);
580 $username = substr($log_entry, 20, strpos($log_entry, "\n") - 20);
581 if ($i == 0) {
582 $ddl_date_from = $date;
584 $statement = rtrim(strstr($log_entry, "\n"));
586 $ddlog[] = array( 'date' => $date,
587 'username'=> $username,
588 'statement' => $statement );
589 $i++;
593 $date_from = $ddl_date_from;
594 $date_to = $ddl_date_to = $date;
596 $dml_date_from = $date_from;
598 $dmlog = array();
599 $i = 0;
601 // Iterate tracked data manipulation statements
602 // For each log entry we want to get date, username and statement
603 foreach ($log_data_entries as $log_entry) {
604 if (trim($log_entry) != '') {
605 $date = substr($log_entry, 0, 19);
606 $username = substr($log_entry, 20, strpos($log_entry, "\n") - 20);
607 if ($i == 0) {
608 $dml_date_from = $date;
610 $statement = rtrim(strstr($log_entry, "\n"));
612 $dmlog[] = array( 'date' => $date,
613 'username' => $username,
614 'statement' => $statement );
615 $i++;
619 $dml_date_to = $date;
621 // Define begin and end of date range for both logs
622 if (strtotime($ddl_date_from) <= strtotime($dml_date_from)) {
623 $data['date_from'] = $ddl_date_from;
624 } else {
625 $data['date_from'] = $dml_date_from;
627 if (strtotime($ddl_date_to) >= strtotime($dml_date_to)) {
628 $data['date_to'] = $ddl_date_to;
629 } else {
630 $data['date_to'] = $dml_date_to;
632 $data['ddlog'] = $ddlog;
633 $data['dmlog'] = $dmlog;
634 $data['tracking'] = $mixed['tracking'];
635 $data['schema_snapshot'] = $mixed['schema_snapshot'];
637 return $data;
642 * Parses a query. Gets
643 * - statement identifier (UPDATE, ALTER TABLE, ...)
644 * - type of statement, is it part of DDL or DML ?
645 * - tablename
647 * @static
648 * @todo: using PMA SQL Parser when possible
649 * @todo: support multi-table/view drops
651 * @param string $query
653 * @return mixed Array containing identifier, type and tablename.
656 static public function parseQuery($query)
659 // Usage of PMA_SQP does not work here
661 // require_once("libraries/sqlparser.lib.php");
662 // $parsed_sql = PMA_SQP_parse($query);
663 // $sql_info = PMA_SQP_analyze($parsed_sql);
665 $query = str_replace("\n", " ", $query);
666 $query = str_replace("\r", " ", $query);
668 $query = trim($query);
669 $query = trim($query, ' -');
671 $tokens = explode(" ", $query);
672 $tokens = array_map('strtoupper', $tokens);
674 // Parse USE statement, need it for SQL dump imports
675 if (substr($query, 0, 4) == 'USE ') {
676 $prefix = explode('USE ', $query);
677 $GLOBALS['db'] = self::getTableName($prefix[1]);
681 * DDL statements
684 $result['type'] = 'DDL';
686 // Parse CREATE VIEW statement
687 if (in_array('CREATE', $tokens) == true &&
688 in_array('VIEW', $tokens) == true &&
689 in_array('AS', $tokens) == true) {
690 $result['identifier'] = 'CREATE VIEW';
692 $index = array_search('VIEW', $tokens);
694 $result['tablename'] = strtolower(self::getTableName($tokens[$index + 1]));
697 // Parse ALTER VIEW statement
698 if (in_array('ALTER', $tokens) == true &&
699 in_array('VIEW', $tokens) == true &&
700 in_array('AS', $tokens) == true &&
701 ! isset($result['identifier'])) {
702 $result['identifier'] = 'ALTER VIEW';
704 $index = array_search('VIEW', $tokens);
706 $result['tablename'] = strtolower(self::getTableName($tokens[$index + 1]));
709 // Parse DROP VIEW statement
710 if (! isset($result['identifier']) && substr($query, 0, 10) == 'DROP VIEW ') {
711 $result['identifier'] = 'DROP VIEW';
713 $prefix = explode('DROP VIEW ', $query);
714 $str = strstr($prefix[1], 'IF EXISTS');
716 if ($str == false ) {
717 $str = $prefix[1];
719 $result['tablename'] = self::getTableName($str);
722 // Parse CREATE DATABASE statement
723 if (! isset($result['identifier']) && substr($query, 0, 15) == 'CREATE DATABASE') {
724 $result['identifier'] = 'CREATE DATABASE';
725 $str = str_replace('CREATE DATABASE', '', $query);
726 $str = str_replace('IF NOT EXISTS', '', $str);
728 $prefix = explode('DEFAULT ', $str);
730 $result['tablename'] = '';
731 $GLOBALS['db'] = self::getTableName($prefix[0]);
734 // Parse ALTER DATABASE statement
735 if (! isset($result['identifier']) && substr($query, 0, 14) == 'ALTER DATABASE') {
736 $result['identifier'] = 'ALTER DATABASE';
737 $result['tablename'] = '';
740 // Parse DROP DATABASE statement
741 if (! isset($result['identifier']) && substr($query, 0, 13) == 'DROP DATABASE') {
742 $result['identifier'] = 'DROP DATABASE';
743 $str = str_replace('DROP DATABASE', '', $query);
744 $str = str_replace('IF EXISTS', '', $str);
745 $GLOBALS['db'] = self::getTableName($str);
746 $result['tablename'] = '';
749 // Parse CREATE TABLE statement
750 if (! isset($result['identifier']) && substr($query, 0, 12) == 'CREATE TABLE' ) {
751 $result['identifier'] = 'CREATE TABLE';
752 $query = str_replace('IF NOT EXISTS', '', $query);
753 $prefix = explode('CREATE TABLE ', $query);
754 $suffix = explode('(', $prefix[1]);
755 $result['tablename'] = self::getTableName($suffix[0]);
758 // Parse ALTER TABLE statement
759 if (! isset($result['identifier']) && substr($query, 0, 12) == 'ALTER TABLE ') {
760 $result['identifier'] = 'ALTER TABLE';
762 $prefix = explode('ALTER TABLE ', $query);
763 $suffix = explode(' ', $prefix[1]);
764 $result['tablename'] = self::getTableName($suffix[0]);
767 // Parse DROP TABLE statement
768 if (! isset($result['identifier']) && substr($query, 0, 11) == 'DROP TABLE ') {
769 $result['identifier'] = 'DROP TABLE';
771 $prefix = explode('DROP TABLE ', $query);
772 $str = strstr($prefix[1], 'IF EXISTS');
774 if ($str == false ) {
775 $str = $prefix[1];
777 $result['tablename'] = self::getTableName($str);
780 // Parse CREATE INDEX statement
781 if (! isset($result['identifier']) &&
782 ( substr($query, 0, 12) == 'CREATE INDEX' ||
783 substr($query, 0, 19) == 'CREATE UNIQUE INDEX' ||
784 substr($query, 0, 20) == 'CREATE SPATIAL INDEX'
787 $result['identifier'] = 'CREATE INDEX';
788 $prefix = explode('ON ', $query);
789 $suffix = explode('(', $prefix[1]);
790 $result['tablename'] = self::getTableName($suffix[0]);
793 // Parse DROP INDEX statement
794 if (! isset($result['identifier']) && substr($query, 0, 10) == 'DROP INDEX') {
795 $result['identifier'] = 'DROP INDEX';
796 $prefix = explode('ON ', $query);
797 $result['tablename'] = self::getTableName($prefix[1]);
800 // Parse RENAME TABLE statement
801 if (! isset($result['identifier']) && substr($query, 0, 13) == 'RENAME TABLE ') {
802 $result['identifier'] = 'RENAME TABLE';
803 $prefix = explode('RENAME TABLE ', $query);
804 $names = explode(' TO ', $prefix[1]);
805 $result['tablename'] = self::getTableName($names[0]);
806 $result["tablename_after_rename"] = self::getTableName($names[1]);
810 * DML statements
813 if (! isset($result['identifier'])) {
814 $result["type"] = 'DML';
816 // Parse UPDATE statement
817 if (! isset($result['identifier']) && substr($query, 0, 6) == 'UPDATE') {
818 $result['identifier'] = 'UPDATE';
819 $prefix = explode('UPDATE ', $query);
820 $suffix = explode(' ', $prefix[1]);
821 $result['tablename'] = self::getTableName($suffix[0]);
824 // Parse INSERT INTO statement
825 if (! isset($result['identifier']) && substr($query, 0, 11 ) == 'INSERT INTO') {
826 $result['identifier'] = 'INSERT';
827 $prefix = explode('INSERT INTO', $query);
828 $suffix = explode('(', $prefix[1]);
829 $result['tablename'] = self::getTableName($suffix[0]);
832 // Parse DELETE statement
833 if (! isset($result['identifier']) && substr($query, 0, 6 ) == 'DELETE') {
834 $result['identifier'] = 'DELETE';
835 $prefix = explode('FROM ', $query);
836 $suffix = explode(' ', $prefix[1]);
837 $result['tablename'] = self::getTableName($suffix[0]);
840 // Parse TRUNCATE statement
841 if (! isset($result['identifier']) && substr($query, 0, 8 ) == 'TRUNCATE') {
842 $result['identifier'] = 'TRUNCATE';
843 $prefix = explode('TRUNCATE', $query);
844 $result['tablename'] = self::getTableName($prefix[1]);
847 return $result;
852 * Analyzes a given SQL statement and saves tracking data.
854 * @static
855 * @param string $query a SQL query
857 static public function handleQuery($query)
859 // If query is marked as untouchable, leave
860 if (strstr($query, "/*NOTRACK*/")) {
861 return;
864 if (! (substr($query, -1) == ';')) {
865 $query = $query . ";\n";
867 // Get some information about query
868 $result = self::parseQuery($query);
870 // Get database name
871 $dbname = trim($GLOBALS['db'], '`');
872 // $dbname can be empty, for example when coming from Synchronize
873 // and this is a query for the remote server
874 if (empty($dbname)) {
875 return;
878 // If we found a valid statement
879 if (isset($result['identifier'])) {
880 $version = self::getVersion($dbname, $result['tablename'], $result['identifier']);
882 // If version not exists and auto-creation is enabled
883 if (self::$version_auto_create == true
884 && self::isTracked($dbname, $result['tablename']) == false
885 && $version == -1) {
886 // Create the version
888 switch ($result['identifier']) {
889 case 'CREATE TABLE':
890 self::createVersion($dbname, $result['tablename'], '1');
891 break;
892 case 'CREATE VIEW':
893 self::createVersion($dbname, $result['tablename'], '1', '', true);
894 break;
895 case 'CREATE DATABASE':
896 self::createDatabaseVersion($dbname, '1', $query);
897 break;
898 } // end switch
901 // If version exists
902 if (self::isTracked($dbname, $result['tablename']) && $version != -1) {
903 if ($result['type'] == 'DDL') {
904 $save_to = 'schema_sql';
905 } elseif ($result['type'] == 'DML') {
906 $save_to = 'data_sql';
907 } else {
908 $save_to = '';
910 $date = date('Y-m-d H:i:s');
912 // Cut off `dbname`. from query
913 $query = preg_replace('/`' . $dbname . '`\s?\./', '', $query);
915 // Add log information
916 $query = self::getLogComment() . $query ;
918 // Mark it as untouchable
919 $sql_query =
920 " /*NOTRACK*/\n" .
921 " UPDATE " . self::$pma_table .
922 " SET " . PMA_backquote($save_to) ." = CONCAT( " . PMA_backquote($save_to) . ",'\n" . PMA_sqlAddSlashes($query) . "') ," .
923 " `date_updated` = '" . $date . "' ";
925 // If table was renamed we have to change the tablename attribute in pma_tracking too
926 if ($result['identifier'] == 'RENAME TABLE') {
927 $sql_query .= ', `table_name` = \'' . PMA_sqlAddSlashes($result['tablename_after_rename']) . '\' ';
930 // Save the tracking information only for
931 // 1. the database
932 // 2. the table / view
933 // 3. the statements
934 // we want to track
935 $sql_query .=
936 " WHERE FIND_IN_SET('" . $result['identifier'] . "',tracking) > 0" .
937 " AND `db_name` = '" . PMA_sqlAddSlashes($dbname) . "' " .
938 " AND `table_name` = '" . PMA_sqlAddSlashes($result['tablename']) . "' " .
939 " AND `version` = '" . PMA_sqlAddSlashes($version) . "' ";
941 $result = PMA_query_as_controluser($sql_query);