source: trunk/update_notice.php@ 36

Last change on this file since 36 was 35, checked in by gegorbet, 7 years ago

mods mostly for use of mysqli

  • Property svn:keywords set to LastChangedDate Author
File size: 5.3 KB
RevLine 
[7]1<?php
2
[18]3$us3bin = exec( "ls -d ~us3/lims/bin" );
4$us3etc = exec( "ls -d ~us3/lims/etc" );
[7]5include_once "$us3bin/listen-config.php";
6
[30]7// Get the US3 system release of latest file on download site
[34]8$s_cmd1 = "ssh us3@ultrascan.uthscsa.edu 'ls -t /srv/www/htdocs/ultrascan3/software/us3*";
[33]9$s_cmd1 = "$s_cmd1 | sed -n 1p | cut -d\- -f3" . "'";
[30]10$s_cmd2 = exec( $s_cmd1 );
11$sysrev = $s_cmd2;
[7]12
13// Global variables
14$notice_db = "us3_notice";
15$dbhost = "localhost";
[34]16$dbuser = "us3_notice";
17$dbpassw = "us3_notice";
[7]18
19// Produce some output temporarily, so cron will send me message
20$now = time();
21echo "Time started: " . date( 'Y-m-d H:i:s', $now ) . "\n";
22
23// Read and parse the local notice file
24
25$n_fname = "$us3etc/us3-notice.xml";
26$fh = fopen( $n_fname, "r" );
27$xml = fread( $fh, filesize( $n_fname ) );
28
29$parser = new XMLReader();
30$parser->xml( $xml );
31
32$notices = array();
33$keys = array();
34
35echo "=====START of PARSE LOOP===== \n";
36while( $parser->read() )
37{
38 $n_type = $parser->nodeType;
39//echo "n_type=$n_type \n";
40
41 if ( $n_type == XMLReader::ELEMENT )
42 {
43 $name = $parser->name;
44//echo "name=$name \n";
45
46 if ( $name == "notice" )
47 {
48 $type = $parser->getAttribute( "type" );
49//echo " type=$type \n";
50 $parser->moveToAttribute( "revision" );
51 $rev = $parser->value;
52//echo " rev=$rev \n";
53 if ( $rev == "latest" )
54 $rev = $sysrev;
55//echo " rev=$rev \n";
56
57 $key = $type . $rev;
58 $notices[ $key ] = array();
59//echo " key=$key \n";
60 }
61 }
62
63 else if ( $n_type == XMLReader::TEXT )
64 {
65 $msg = $parser->readString();
66//echo " msg=+++$msg+++ \n";
67 $len = strlen( $msg );
68
69 if ( $len > 0 )
70 { // Only add if not an empty message
71 $msg = preg_replace( "/\@revision/", $rev, $msg );
72 $msg = preg_replace( "/'/", "\\'", $msg );
73 $notices[ $key ][ 'type' ] = $type;
74 $notices[ $key ][ 'rev' ] = $rev;
75 $notices[ $key ][ 'msg' ] = $msg;
76 $notices[ $key ][ 'act' ] = 'add';
77 $notices[ $key ][ 'id' ] = '0';
78
79 $keys[] = $key;
80 }
81 }
82}
83
84$parser->close();
85echo "=====END of PARSE LOOP===== \n";
86
87// Get data from notice DB. Update the action field to
88// reflect which action is required on each entry:
89// "add" - is only present in file (add to DB);
90// "del" - is only present in DB (delete from DB);
91// "upd" - present in both, but messages differ (update in DB);
92// "none" - present in both and messages identical (no DB update).
93
[35]94$noteLink = mysqli_connect( $dbhost, $dbuser, $dbpassw, $notice_db );
[7]95
[35]96if ( ! $noteLink )
[7]97{
[35]98 echo "Could not connect to DB $notice_db - " . mysqli_error($noteLink) . "\n";
[7]99 exit();
100}
101
102$query = "SELECT id, type, revision, message FROM notice";
103
[35]104$result = mysqli_query( $noteLink, $query )
105 or die( "Query failed : $query<br />" . mysqli_error($noteLink) );
[7]106
107echo "=====START of DB QUERY LOOP===== \n";
[35]108$num_rows = mysqli_num_rows( $result );
[7]109
110echo " numrows = $num_rows \n";
[35]111while ( list( $id, $type, $rev, $msg ) = mysqli_fetch_array( $result ) )
[7]112{
113 $key = $type . $rev;
114
115 if ( in_array( $key, $keys ) )
116 { // Entry is in both file and DB
[18]117 $msgf = $notices[ $key ][ 'msg' ];
[7]118 $notices[ $key ][ 'id' ] = $id;
119 $notices[ $key ][ 'type' ] = $type;
120 $notices[ $key ][ 'rev' ] = $rev;
121
122 if ( strcmp( $msg, $msgf ) == 0 )
123 { // Messages match, so no update is needed
124 $notices[ $key ][ 'act' ] = 'none';
125 }
126 else
127 { // Messages differ, so DB entry must be updated
128 $notices[ $key ][ 'act' ] = 'upd';
129 $notices[ $key ][ 'msg' ] = $msgf;
130echo " msg = ++$msg++\n";
131echo " msgf = ++$msgf++\n";
132 }
133 }
134
135 else
136 { // Entry is only in DB, so a delete is needed
137 $notices[ $key ] = array();
138 $notices[ $key ][ 'id' ] = $id;
139 $notices[ $key ][ 'type' ] = $type;
140 $notices[ $key ][ 'rev' ] = $rev;
141 $notices[ $key ][ 'act' ] = 'del';
142 $notices[ $key ][ 'msg' ] = $msg;
143 $keys[] = $key;
144 }
145}
146echo "=====END of DB QUERY LOOP===== \n";
147
148echo "=====START of DB Update LOOP===== \n";
149foreach ( $keys as $key )
150{
151 $type = $notices[ $key ][ 'type' ];
152 $rev = $notices[ $key ][ 'rev' ];
153 $act = $notices[ $key ][ 'act' ];
154 $id = $notices[ $key ][ 'id' ];
155 $msg = $notices[ $key ][ 'msg' ];
156 $len = strlen( $msg );
157 echo "-- key=$key --\n";
158 echo " type: $type \n";
159 echo " rev: $rev \n";
160 echo " act: $act \n";
161 echo " id: $id \n";
162// echo " msg: $msg \n";
163 echo " msg: ( $len characters ) \n";
164
165 if ( $act == "add" )
166 { // Must add the entry to the database
167 $query = "INSERT INTO notice " .
168 "SET type='$type', revision='$rev', message='$msg'";
169 }
170 else if ( $act == "del" )
171 { // Must delete the entry from the database
172 $query = "DELETE FROM notice WHERE id='$id'";
173 }
174 else if ( $act == "upd" )
175 { // Must update an existing entry in the database
176 $query = "UPDATE notice " .
177 "SET message='$msg' " .
178 "WHERE id='$id'";
179 }
180 echo " query: [ $query ] \n";
181
[35]182 $result = mysqli_query( $noteLink, $query )
183 or die( "Query failed : $query<br />" . mysqli_error($noteLink) );
[7]184
185}
186echo "=====END of DB Update LOOP===== \n";
187
188?>
Note: See TracBrowser for help on using the repository browser.