xml( $xml ); $notices = array(); $keys = array(); echo "=====START of PARSE LOOP===== \n"; while( $parser->read() ) { $n_type = $parser->nodeType; //echo "n_type=$n_type \n"; if ( $n_type == XMLReader::ELEMENT ) { $name = $parser->name; //echo "name=$name \n"; if ( $name == "notice" ) { $type = $parser->getAttribute( "type" ); //echo " type=$type \n"; $parser->moveToAttribute( "revision" ); $rev = $parser->value; //echo " rev=$rev \n"; if ( $rev == "latest" ) $rev = $sysrev; //echo " rev=$rev \n"; $key = $type . $rev; $notices[ $key ] = array(); //echo " key=$key \n"; } } else if ( $n_type == XMLReader::TEXT ) { $msg = $parser->readString(); //echo " msg=+++$msg+++ \n"; $len = strlen( $msg ); if ( $len > 0 ) { // Only add if not an empty message $msg = preg_replace( "/\@revision/", $rev, $msg ); $msg = preg_replace( "/'/", "\\'", $msg ); $notices[ $key ][ 'type' ] = $type; $notices[ $key ][ 'rev' ] = $rev; $notices[ $key ][ 'msg' ] = $msg; $notices[ $key ][ 'act' ] = 'add'; $notices[ $key ][ 'id' ] = '0'; $keys[] = $key; } } } $parser->close(); echo "=====END of PARSE LOOP===== \n"; // Get data from notice DB. Update the action field to // reflect which action is required on each entry: // "add" - is only present in file (add to DB); // "del" - is only present in DB (delete from DB); // "upd" - present in both, but messages differ (update in DB); // "none" - present in both and messages identical (no DB update). $noteLink = mysqli_connect( $dbhost, $dbuser, $dbpassw, $notice_db ); if ( ! $noteLink ) { echo "Could not connect to DB $notice_db - " . mysqli_error($noteLink) . "\n"; exit(); } $query = "SELECT id, type, revision, message FROM notice"; $result = mysqli_query( $noteLink, $query ) or die( "Query failed : $query
" . mysqli_error($noteLink) ); echo "=====START of DB QUERY LOOP===== \n"; $num_rows = mysqli_num_rows( $result ); echo " numrows = $num_rows \n"; while ( list( $id, $type, $rev, $msg ) = mysqli_fetch_array( $result ) ) { $key = $type . $rev; if ( in_array( $key, $keys ) ) { // Entry is in both file and DB $msgf = $notices[ $key ][ 'msg' ]; $notices[ $key ][ 'id' ] = $id; $notices[ $key ][ 'type' ] = $type; $notices[ $key ][ 'rev' ] = $rev; if ( strcmp( $msg, $msgf ) == 0 ) { // Messages match, so no update is needed $notices[ $key ][ 'act' ] = 'none'; } else { // Messages differ, so DB entry must be updated $notices[ $key ][ 'act' ] = 'upd'; $notices[ $key ][ 'msg' ] = $msgf; echo " msg = ++$msg++\n"; echo " msgf = ++$msgf++\n"; } } else { // Entry is only in DB, so a delete is needed $notices[ $key ] = array(); $notices[ $key ][ 'id' ] = $id; $notices[ $key ][ 'type' ] = $type; $notices[ $key ][ 'rev' ] = $rev; $notices[ $key ][ 'act' ] = 'del'; $notices[ $key ][ 'msg' ] = $msg; $keys[] = $key; } } echo "=====END of DB QUERY LOOP===== \n"; echo "=====START of DB Update LOOP===== \n"; foreach ( $keys as $key ) { $type = $notices[ $key ][ 'type' ]; $rev = $notices[ $key ][ 'rev' ]; $act = $notices[ $key ][ 'act' ]; $id = $notices[ $key ][ 'id' ]; $msg = $notices[ $key ][ 'msg' ]; $len = strlen( $msg ); echo "-- key=$key --\n"; echo " type: $type \n"; echo " rev: $rev \n"; echo " act: $act \n"; echo " id: $id \n"; // echo " msg: $msg \n"; echo " msg: ( $len characters ) \n"; if ( $act == "add" ) { // Must add the entry to the database $query = "INSERT INTO notice " . "SET type='$type', revision='$rev', message='$msg'"; } else if ( $act == "del" ) { // Must delete the entry from the database $query = "DELETE FROM notice WHERE id='$id'"; } else if ( $act == "upd" ) { // Must update an existing entry in the database $query = "UPDATE notice " . "SET message='$msg' " . "WHERE id='$id'"; } echo " query: [ $query ] \n"; $result = mysqli_query( $noteLink, $query ) or die( "Query failed : $query
" . mysqli_error($noteLink) ); } echo "=====END of DB Update LOOP===== \n"; ?>