@@ -88,74 +88,80 @@ function processDatalog() {
8888 $ records = $ app ->dbmaster ->queryAllRecords ($ sql );
8989 foreach ($ records as $ d ) {
9090
91- $ data = unserialize (stripslashes ($ d ["data " ]));
91+ if (!$ data = unserialize (stripslashes ($ d ["data " ]))) {
92+ $ data = unserialize ($ d ["data " ]);
93+ }
9294 $ replication_error = false ;
9395
9496 $ this ->current_datalog_id = $ d ["datalog_id " ];
9597
96- if ($ d ["action " ] == 'i ' ) {
97- $ idx = explode (": " ,$ d ["dbidx " ]);
98- $ tmp_sql1 = '' ;
99- $ tmp_sql2 = '' ;
100- foreach ($ data ['new ' ] as $ fieldname => $ val ) {
101- $ tmp_sql1 .= "` $ fieldname`, " ;
102- $ tmp_sql2 .= "' $ val', " ;
103- }
104- $ tmp_sql1 = substr ($ tmp_sql1 ,0 ,-1 );
105- $ tmp_sql2 = substr ($ tmp_sql2 ,0 ,-1 );
106- //$tmp_sql1 .= "$idx[0]";
107- //$tmp_sql2 .= "$idx[1]";
108- $ sql = "REPLACE INTO $ d [dbtable] ( $ tmp_sql1) VALUES ( $ tmp_sql2) " ;
109- $ app ->db ->query ($ sql );
110- if ($ app ->db ->errorNumber > 0 ) {
111- $ replication_error = true ;
112- $ app ->log ("Replication failed. Error: ( " . $ d [dbtable] . ") " . $ app ->db ->errorMessage . " # SQL: " . $ sql ,LOGLEVEL_ERROR );
113- }
114- $ app ->log ("Replicated from master: " .$ sql ,LOGLEVEL_DEBUG );
115- }
116-
117- if ($ d ["action " ] == 'u ' ) {
118- $ sql = "UPDATE $ d [dbtable] SET " ;
119- foreach ($ data ['new ' ] as $ fieldname => $ val ) {
120- $ sql .= "` $ fieldname` = ' $ val', " ;
98+ if (count ($ data ['new ' ]) > 0 ) {
99+ if ($ d ["action " ] == 'i ' || $ d ["action " ] == 'u ' ) {
100+ $ idx = explode (": " ,$ d ["dbidx " ]);
101+ $ tmp_sql1 = '' ;
102+ $ tmp_sql2 = '' ;
103+ foreach ($ data ['new ' ] as $ fieldname => $ val ) {
104+ $ tmp_sql1 .= "` $ fieldname`, " ;
105+ $ tmp_sql2 .= "' $ val', " ;
106+ }
107+ $ tmp_sql1 = substr ($ tmp_sql1 ,0 ,-1 );
108+ $ tmp_sql2 = substr ($ tmp_sql2 ,0 ,-1 );
109+ //$tmp_sql1 .= "$idx[0]";
110+ //$tmp_sql2 .= "$idx[1]";
111+ $ sql = "REPLACE INTO $ d [dbtable] ( $ tmp_sql1) VALUES ( $ tmp_sql2) " ;
112+ $ app ->db ->query ($ sql );
113+ if ($ app ->db ->errorNumber > 0 ) {
114+ $ replication_error = true ;
115+ $ app ->log ("Replication failed. Error: ( " . $ d [dbtable] . ") " . $ app ->db ->errorMessage . " # SQL: " . $ sql ,LOGLEVEL_ERROR );
116+ }
117+ $ app ->log ("Replicated from master: " .$ sql ,LOGLEVEL_DEBUG );
121118 }
122- $ sql = substr ($ sql ,0 ,-1 );
123- $ idx = explode (": " ,$ d ["dbidx " ]);
124- $ sql .= " WHERE $ idx [0 ] = $ idx [1 ]" ;
125- $ app ->db ->query ($ sql );
126- if ($ app ->db ->errorNumber > 0 ) {
127- $ replication_error = true ;
128- $ app ->log ("Replication failed. Error: ( " . $ d [dbtable] . ") " . $ app ->db ->errorMessage . " # SQL: " . $ sql ,LOGLEVEL_ERROR );
119+ /*
120+ if($d["action"] == 'u') {
121+ $sql = "UPDATE $d[dbtable] SET ";
122+ foreach($data['new'] as $fieldname => $val) {
123+ $sql .= "`$fieldname` = '$val',";
124+ }
125+ $sql = substr($sql,0,-1);
126+ $idx = explode(":",$d["dbidx"]);
127+ $sql .= " WHERE $idx[0] = $idx[1]";
128+ $app->db->query($sql);
129+ if($app->db->errorNumber > 0) {
130+ $replication_error = true;
131+ $app->log("Replication failed. Error: (" . $d[dbtable] . ") " . $app->db->errorMessage . " # SQL: " . $sql,LOGLEVEL_ERROR);
132+ }
133+ $app->log("Replicated from master: ".$sql,LOGLEVEL_DEBUG);
129134 }
130- $ app -> log ( " Replicated from master: " . $ sql , LOGLEVEL_DEBUG );
131- }
132-
133- if ( $ d [ " action " ] == ' d ' ) {
134- $ idx = explode ( " : " , $ d [ " dbidx " ]) ;
135- $ sql = " DELETE FROM $ d [dbtable] " ;
136- $ sql .= " WHERE $ idx [ 0 ] = $ idx [ 1 ]" ;
137- $ app -> db -> query ( $ sql ) ;
138- if ( $ app ->db ->errorNumber > 0 ) {
139- $ replication_error = true ;
140- $ app ->log ("Replication failed. Error: ( " . $ d [dbtable] . " ) " . $ app -> db -> errorMessage . " # SQL: " . $ sql ,LOGLEVEL_ERROR );
135+ */
136+ if ( $ d [ " action " ] == ' d ' ) {
137+ $ idx = explode ( " : " , $ d [ " dbidx " ]);
138+ $ sql = " DELETE FROM $ d [dbtable] " ;
139+ $ sql .= " WHERE $ idx [ 0 ] = $ idx [ 1 ]" ;
140+ $ app -> db -> query ( $ sql ) ;
141+ if ( $ app -> db -> errorNumber > 0 ) {
142+ $ replication_error = true ;
143+ $ app -> log ( " Replication failed. Error: ( " . $ d [dbtable] . " ) " . $ app ->db ->errorMessage . " # SQL: " . $ sql , LOGLEVEL_ERROR );
144+ }
145+ $ app ->log ("Replicated from master: " . $ sql ,LOGLEVEL_DEBUG );
141146 }
142- $ app ->log ("Replicated from master: " .$ sql ,LOGLEVEL_DEBUG );
143- }
144147
145148
146- if ($ replication_error == false ) {
147- $ this ->raiseTableHook ($ d ["dbtable " ],$ d ["action " ],$ data );
148- //$app->dbmaster->query("DELETE FROM sys_datalog WHERE datalog_id = ".$d["datalog_id"]);
149- //$app->log("Deleting sys_datalog ID ".$d["datalog_id"],LOGLEVEL_DEBUG);
150- $ app ->dbmaster ->query ("UPDATE server SET updated = " .$ d ["datalog_id " ]." WHERE server_id = " .$ conf ["server_id " ]);
151- $ app ->log ("Processed datalog_id " .$ d ["datalog_id " ],LOGLEVEL_DEBUG );
149+ if ($ replication_error == false ) {
150+ $ this ->raiseTableHook ($ d ["dbtable " ],$ d ["action " ],$ data );
151+ //$app->dbmaster->query("DELETE FROM sys_datalog WHERE datalog_id = ".$d["datalog_id"]);
152+ //$app->log("Deleting sys_datalog ID ".$d["datalog_id"],LOGLEVEL_DEBUG);
153+ $ app ->dbmaster ->query ("UPDATE server SET updated = " .$ d ["datalog_id " ]." WHERE server_id = " .$ conf ["server_id " ]);
154+ $ app ->log ("Processed datalog_id " .$ d ["datalog_id " ],LOGLEVEL_DEBUG );
155+ } else {
156+ $ app ->log ("Error in Replication, changes were not processed. " ,LOGLEVEL_ERROR );
157+ /*
158+ * If there is any error in processing the datalog we can't continue, because
159+ * we do not know if the newer actions require this (old) one.
160+ */
161+ return ;
162+ }
152163 } else {
153- $ app ->log ("Error in Replication, changes were not processed. " ,LOGLEVEL_ERROR );
154- /*
155- * If there is any error in processing the datalog we can't continue, because
156- * we do not know if the newer actions require this (old) one.
157- */
158- return ;
164+ $ app ->log ("Datalog does not conatin any changes for this record " .$ d ["datalog_id " ],LOGLEVEL_DEBUG );
159165 }
160166 }
161167
@@ -164,7 +170,9 @@ function processDatalog() {
164170 $ sql = "SELECT * FROM sys_datalog WHERE datalog_id > " .$ conf ['last_datalog_id ' ]." AND (server_id = " .$ conf ["server_id " ]." OR server_id = 0) ORDER BY datalog_id " ;
165171 $ records = $ app ->db ->queryAllRecords ($ sql );
166172 foreach ($ records as $ d ) {
167- $ data = unserialize (stripslashes ($ d ["data " ]));
173+ if (!$ data = unserialize (stripslashes ($ d ["data " ]))) {
174+ $ data = unserialize ($ d ["data " ]);
175+ }
168176 $ this ->current_datalog_id = $ d ["datalog_id " ];
169177 $ this ->raiseTableHook ($ d ["dbtable " ],$ d ["action " ],$ data );
170178 //$app->db->query("DELETE FROM sys_datalog WHERE datalog_id = ".$rec["datalog_id"]);
0 commit comments