/
album_patch.php
530 lines (494 loc) · 26 KB
/
album_patch.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
<?php
require_once 'dbconnect/dbconnect.php';
require_once 'utilities/utilities.php';
require_once 'RETS.php';
header('Content-Type: text/html; charset=utf-8');
if(mysql_select_db("home365_ios",$useradmin)){
}else{
echo "Error selecting database, exited.";
exit();
}
$rets = new RETS();
$rets->url='http://brc.retsca.interealty.com/Login.asmx/Login';
$rets->user='RETSALLISONJ';
$rets->password='RE@LE$7AT3';
$rets->useragent='RETSAllisonJiang/1.0';
$rets->useragent_password='8tHIMi7aL#e4Utd';
$current_GMT_time=get_GMT(time());
$current_GMT_time_string=date("Y-m-d H:i:s",$current_GMT_time);
$current_YVR_time=getTimeByCity($current_GMT_time,'YVR');
$current_YVR_time_string=date("Y-m-d H:i:s",$current_YVR_time);
$before1hr_time_string = date("Y-m-d H:i:s",$current_YVR_time-3600);
$file="album.txt";
$current=file_get_contents($file);
$current.="$current_YVR_time_string\n";
file_put_contents($file,$current);
$deleteSQL = "DELETE FROM sysid_raw_recent_image_update WHERE sysid IN (SELECT sysid FROM listings WHERE date_updated_local>='".$before1hr_time_string."')";
$result = mysql_query_or_die($deleteSQL, $useradmin);
$selectSQL = "SELECT SQL_CALC_FOUND_ROWS DISTINCT sysid_raw_recent_image_update.sysid, sysid_raw_recent_image_update.num_of_photos FROM sysid_raw_recent_image_update".
" LEFT JOIN listings ON sysid_raw_recent_image_update.sysid=listings.sysid".
" WHERE !ISNULL(listings.sysid)".
" ORDER BY RAND() LIMIT 0, 5";
$get_sysid = mysql_query_or_die($selectSQL, $useradmin);
$row=mysql_fetch_row(mysql_query("SELECT FOUND_ROWS()",$useradmin));
if($row[0]>0){
$response=$rets->Login();
echo "<br/>There are ".$row[0]." records in table sysid_raw_recent_image_update<br/>";
//$response=$rets->Login();
//var_dump($response);
$i=0;$j=0;$k=0;$m=0;$empty=0;$no_photo_id=0;
while($row_get_sysid=mysql_fetch_assoc($get_sysid)){
$temp_sysid=$row_get_sysid['sysid'];
$num_of_photos=$row_get_sysid['num_of_photos'];
if(empty($num_of_photos)){
$empty++;
$deleteSQL=sprintf("DELETE FROM sysid_raw_recent_image_update WHERE sysid=%s",
GetSQLValueString($temp_sysid,"int"));
$result=mysql_query_or_die($deleteSQL, $useradmin);
echo '<br/><span style="color:#ff0000;">'.$temp_sysid.' is removed from the database because remote data does\'t have num of photos data.</span><br/>';
}else{
$photo_id_array=get_photo_id($temp_sysid);
if(count($photo_id_array)>0){
if(count($photo_id_array)<$num_of_photos){
//var_dump($photo_id_array);
//echo "<br/>";
$i++;
echo $temp_sysid.":".$num_of_photos."<br/>";
$album_id=check_and_insert($temp_sysid);
if(!empty($album_id)){
$response=$rets->GetPhoto('Property',$temp_sysid.':*', 'images');
$photo_array=$response['photo_array'];
//var_dump($photo_array);
if(is_array($photo_array)&&count($photo_array)>=$num_of_photos){
echo"<br/>Number of photos retrieved matches the database record.<br/>";
for($i=0;$i<count($photo_array);$i++){
$selectSQL="SELECT * FROM photo_profile WHERE photo_path='".$photo_array[$i]."'";
//echo $selectSQL."<br/>";
if($photo_item=mysql_fetch_assoc(mysql_query_or_die($selectSQL, $useradmin))){
echo "<br/>photo_path found in database, nothing to be inserted.<br/>";
}else{
$selectSQL = "SELECT photo_profile.photo_id FROM listings LEFT JOIN album_profile ON listings.album_id = album_profile.album_id".
" LEFT JOIN photo_profile ON album_profile.photo_id = photo_profile.photo_id".
" WHERE photo_path='http://80a5e3a6041af90156b0-bf53eb49f254872ee431fd1273bc084d.r45.cf2.rackcdn.com/_.jpg'".
" AND listings.album_id=$album_id";
$get_photo = mysql_query_or_die($selectSQL, $useradmin);
if($row_get_photo=mysql_fetch_assoc($get_photo)){
echo "<br/>Broken image found. Updating record.<br/>";
$photo_id=$row_get_photo['photo_id'];
$photo_path=$photo_array[$i];
$update_SQL=sprintf("UPDATE photo_profile SET photo_path=%s WHERE photo_id=%s",
GetSQLValueString($photo_path,"text"),
GetSQLValueString($photo_id,"int"));
$result=mysql_query_or_die($update_SQL, $useradmin);
}else{
echo "<br/>photo_path not found in database, inserting new record<br/>";
$insertPhotoSQL = sprintf("INSERT INTO photo_profile(photo_path)VALUES(%s)",
GetSQLValueString($photo_array[$i],"text"));
$result=mysql_query_or_die($insertPhotoSQL,$useradmin);
$photo_id=mysql_insert_id($useradmin);
$insertAlbumSQL = sprintf("INSERT INTO album_profile(album_id, photo_id, cover)VALUES(%s,%s,%s)",
GetSQLValueString($album_id,"int"),
GetSQLValueString($photo_id,"int"),
GetSQLValueString($i==0?'Y':'N',"text"));
$result=mysql_query_or_die($insertAlbumSQL,$useradmin);
}
}
unset($selectSQL);
}
$updateSQL=sprintf("UPDATE listings SET num_of_photos=%s WHERE sysid=%s",
GetSQLValueString(count($photo_array),"int"),
GetSQLValueString($temp_sysid,"int"));
$result=mysql_query_or_die($updateSQL, $useradmin);
$deleteSQL=sprintf("DELETE FROM sysid_raw_recent_image_update WHERE sysid=%s",
GetSQLValueString($temp_sysid,"int"));
$result=mysql_query_or_die($deleteSQL, $useradmin);
echo "<br/>$temp_sysid deleted from sysid_raw_recent_image_update.<br/>";
}else{
echo "<br/>Empty photo array or number of photos retrieved doesn't match the database record<br/>";
$deleteSQL=sprintf("DELETE FROM sysid_raw_recent_image_update WHERE sysid=%s",
GetSQLValueString($temp_sysid,"int"));
$result=mysql_query_or_die($deleteSQL, $useradmin);
echo '<br/><span style="color:#ff0000;">'.$temp_sysid.' is removed from the database because number of photos retrieved doesn\'t match the database record</span><br/>';
}
}
}elseif(count($photo_id_array)>$num_of_photos){
$j++;
$deleteSQL=sprintf("DELETE FROM sysid_raw_recent_image_update WHERE sysid=%s",
GetSQLValueString($temp_sysid,"int"));
$result=mysql_query_or_die($deleteSQL, $useradmin);
echo '<br/><span style="color:#ff0000;">'.$temp_sysid.' is removed from the database because it contains more photos than the remote data</span><br/>';
}else{
if(count($photo_id_array)==$num_of_photos){
$m++;
$deleteSQL=sprintf("DELETE FROM sysid_raw_recent_image_update WHERE sysid=%s",
GetSQLValueString($temp_sysid,"int"));
$result=mysql_query_or_die($deleteSQL, $useradmin);
echo '<br/><span style="color:#ff0000;">'.$temp_sysid.' is removed from the database because it contains same amount of photos as the remote data</span><br/>';
}
}
}else{
$no_photo_id++;
$album_id=check_and_insert($temp_sysid);
if(!empty($album_id)){
$response=$rets->GetPhoto('Property',$temp_sysid.':*', 'images');
$photo_array=$response['photo_array'];
for($i=0;$i<count($photo_array);$i++){
$insertPhotoSQL = sprintf("INSERT INTO photo_profile(photo_path)VALUES(%s)",
GetSQLValueString($photo_array[$i],"text"));
$result=mysql_query_or_die($insertPhotoSQL,$useradmin);
$photo_id=mysql_insert_id($useradmin);
$insertAlbumSQL = sprintf("INSERT INTO album_profile(album_id, photo_id, cover)VALUES(%s,%s,%s)",
GetSQLValueString($album_id,"int"),
GetSQLValueString($photo_id,"int"),
GetSQLValueString($i==0?'Y':'N',"text"));
$result=mysql_query_or_die($insertAlbumSQL,$useradmin);
}
$updateSQL=sprintf("UPDATE listings SET num_of_photos=%s WHERE sysid=%s",
GetSQLValueString(count($photo_array),"int"),
GetSQLValueString($temp_sysid,"int"));
$result=mysql_query_or_die($updateSQL, $useradmin);
$deleteSQL=sprintf("DELETE FROM sysid_raw_recent_image_update WHERE sysid=%s",
GetSQLValueString($temp_sysid,"int"));
$result=mysql_query_or_die($deleteSQL, $useradmin);
echo "<br/>$temp_sysid deleted from sysid_raw_recent_image_update.<br/>";
}
}
}
//$num_of_photos_db=get_num_of_photos($row_get_sysid['sysid']);
}
echo "<br/>There are ".$i." records in database table that have less photos than remote data<br/>";
echo "<br/>There are ".$j." records in database table that have more photos than remote data<br/>";
echo "<br/>There are ".$empty." records remote data don't have num of photos<br/>";
echo "<br/>There are ".$m." records in database table that have same photos as remote data<br/>";
echo "<br/>There are ".$no_photo_id." records in database table that don't have local photo<br/>";
}
$selectSQL = "SELECT SQL_CALC_FOUND_ROWS DISTINCT sysid FROM listings
WHERE (ISNULL(listings.album_id) OR num_of_photos<=1)
AND album_patched='N'
AND status='A'
AND property_type!='Land Only'
AND area_desc!='VOT'
AND area_desc!='FOT'
ORDER BY RAND() LIMIT 0, 10";
$get_sysid=mysql_query_or_die($selectSQL, $useradmin);
$row=mysql_fetch_row(mysql_query("SELECT FOUND_ROWS()",$useradmin));
//login and receive server response.
if(is_array($row)&&$row[0]>0){
echo '<br/>'.$row[0].'records needs to be album_patched.<br/>';
$response=$rets->Login();
patch_album($get_sysid);
/*while($row_get_sysid=mysql_fetch_assoc($get_sysid)){
echo '<br/><br/>checking:<span style="color:#00ff00;">'.$row_get_sysid['sysid'].'</span><br/>';
$album_id=check_and_insert($row_get_sysid['sysid']);
if(!empty($album_id)){
$photo_id_array=get_photo_id($row_get_sysid['sysid']);
var_dump($photo_id_array);
$response=$rets->GetPhoto('Property',$row_get_sysid['sysid'].':*', 'images');
$photo_array=$response['photo_array'];
//var_dump($photo_array);
echo "<br/>服务器图片数量:".count($photo_array).' & 本地图片数量:'.count($photo_id_array)."<br/>";
if(count($photo_array)>count($photo_id_array)){
echo "<br/>We got more photos from the remote server<br/>";
for($i=0;$i<count($photo_array);$i++){
$selectSQL="SELECT * FROM photo_profile WHERE photo_path='".$photo_array[$i]."'";
if($photo_item=mysql_fetch_assoc(mysql_query_or_die($selectSQL, $useradmin))){
echo "<br/>photo_path found in database, nothing to be inserted.<br/>";
}else{
$selectSQL = "SELECT photo_profile.photo_id FROM listings LEFT JOIN album_profile ON listings.album_id = album_profile.album_id".
" LEFT JOIN photo_profile ON album_profile.photo_id = photo_profile.photo_id".
" WHERE photo_path='http://80a5e3a6041af90156b0-bf53eb49f254872ee431fd1273bc084d.r45.cf2.rackcdn.com/_.jpg'".
" AND listings.album_id=$album_id";
//echo $selectSQL;
$get_photo = mysql_query_or_die($selectSQL, $useradmin);
if($row_get_photo=mysql_fetch_assoc($get_photo)){
echo "<br/>Broken image found. Updating record.<br/>";
$photo_id=$row_get_photo['photo_id'];
$photo_path=$photo_array[$i];
$update_SQL=sprintf("UPDATE photo_profile SET photo_path=%s WHERE photo_id=%s",
GetSQLValueString($photo_path,"text"),
GetSQLValueString($photo_id,"int"));
$result=mysql_query_or_die($update_SQL, $useradmin);
}else{
echo "<br/>photo_path not found in database, inserting new record<br/>";
$insertPhotoSQL = sprintf("INSERT INTO photo_profile(photo_path)VALUES(%s)",
GetSQLValueString($photo_array[$i],"text"));
$result=mysql_query_or_die($insertPhotoSQL,$useradmin);
$photo_id=mysql_insert_id($useradmin);
$insertAlbumSQL = sprintf("INSERT INTO album_profile(album_id, photo_id, cover)VALUES(%s,%s,%s)",
GetSQLValueString($album_id,"int"),
GetSQLValueString($photo_id,"int"),
GetSQLValueString($i==0?'Y':'N',"text"));
$result=mysql_query_or_die($insertAlbumSQL,$useradmin);
}
}
unset($selectSQL);
}
$updateSQL=sprintf("UPDATE listings SET num_of_photos=%s, album_patched=%s WHERE sysid=%s",
GetSQLValueString(count($photo_array),"int"),
GetSQLValueString('Y',"text"),
GetSQLValueString($row_get_sysid['sysid'],"int"));
$result=mysql_query_or_die($updateSQL, $useradmin);
}else{
if(count($photo_id_array)==count($photo_array)){
if(count($photo_array)==1){
$updateSQL=sprintf("UPDATE listings SET album_patched=%s WHERE sysid=%s",
GetSQLValueString('Y',"text"),
GetSQLValueString($row_get_sysid['sysid'],"int"));
$result=mysql_query_or_die($updateSQL, $useradmin);
echo "<br/>retrieved 1 photo, and it is already in local database<br/>";
}else{
echo "<br/><span style=\"color:green;\">retrieved more than 1 photo, and it is already in local database, updating num_of_photos in listings table</span><br/>";
$updateSQL = sprintf("UPDATE listings SET num_of_photos=%s, album_patched=%s WHERE sysid=%s",
GetSQLValueString(count($photo_array),"int"),
GetSQLValueString('Y',"text"),
GetSQLValueString($row_get_sysid['sysid'],"int"));
$result=mysql_query_or_die($updateSQL, $useradmin);
}
}else{
echo "<br/><span style=\"color:green;\">取得的图片[".count($photo_array)."]没有本地图片[".count($photo_id_array)."]多</span><br/>";
$updateSQL = sprintf("UPDATE listings SET num_of_photos=%s, album_patched=%s WHERE sysid=%s",
GetSQLValueString(count($photo_id_array),"int"),
GetSQLValueString(count($photo_array)==0?'Y':'Y',"text"),
GetSQLValueString($row_get_sysid['sysid'],"int"));
$result=mysql_query_or_die($updateSQL, $useradmin);
}
}
}
}*/
}else{
echo "Empty Set<br/>";
}
$selectSQL = "SELECT SQL_CALC_FOUND_ROWS DISTINCT sysid FROM listings LEFT JOIN album_profile ON listings.album_id=album_profile.album_id LEFT JOIN photo_profile ON album_profile.photo_id=photo_profile.photo_id WHERE photo_path='http://80a5e3a6041af90156b0-bf53eb49f254872ee431fd1273bc084d.r45.cf2.rackcdn.com/_.jpg' AND status='A' AND property_type!='Land Only' AND area_desc!='VOT' AND area_desc!='FOT' AND city!='No City Value' ORDER BY RAND() LIMIT 0, 5";
$get_sysid=mysql_query_or_die($selectSQL, $useradmin);
$row=mysql_fetch_row(mysql_query("SELECT FOUND_ROWS()",$useradmin));
//login and receive server response.
if(is_array($row)&&$row[0]>0){
echo '<br/>'.$row[0].'records needs to be album_patched.<br/>';
$response=$rets->Login();
patch_album($get_sysid);
/*while($row_get_sysid=mysql_fetch_assoc($get_sysid)){
echo '<br/><br/>checking:<span style="color:#00ff00;">'.$row_get_sysid['sysid'].'</span><br/>';
$album_id=check_and_insert($row_get_sysid['sysid']);
if(!empty($album_id)){
$photo_id_array=get_photo_id($row_get_sysid['sysid']);
var_dump($photo_id_array);
$response=$rets->GetPhoto('Property',$row_get_sysid['sysid'].':*', 'images');
$photo_array=$response['photo_array'];
//var_dump($photo_array);
echo "<br/>服务器图片数量:".count($photo_array).' & 本地图片数量:'.count($photo_id_array)."<br/>";
if(count($photo_array)>count($photo_id_array)){
echo "<br/>We got more photos from the remote server<br/>";
for($i=0;$i<count($photo_array);$i++){
$selectSQL="SELECT * FROM photo_profile WHERE photo_path='".$photo_array[$i]."'";
if($photo_item=mysql_fetch_assoc(mysql_query_or_die($selectSQL, $useradmin))){
echo "<br/>photo_path found in database, nothing to be inserted.<br/>";
}else{
$selectSQL = "SELECT photo_profile.photo_id FROM listings LEFT JOIN album_profile ON listings.album_id = album_profile.album_id".
" LEFT JOIN photo_profile ON album_profile.photo_id = photo_profile.photo_id".
" WHERE photo_path='http://80a5e3a6041af90156b0-bf53eb49f254872ee431fd1273bc084d.r45.cf2.rackcdn.com/_.jpg'".
" AND listings.album_id=$album_id";
//echo $selectSQL;
$get_photo = mysql_query_or_die($selectSQL, $useradmin);
if($row_get_photo=mysql_fetch_assoc($get_photo)){
echo "<br/>Broken image found. Updating record.<br/>";
$photo_id=$row_get_photo['photo_id'];
$photo_path=$photo_array[$i];
$update_SQL=sprintf("UPDATE photo_profile SET photo_path=%s WHERE photo_id=%s",
GetSQLValueString($photo_path,"text"),
GetSQLValueString($photo_id,"int"));
$result=mysql_query_or_die($update_SQL, $useradmin);
}else{
echo "<br/>photo_path not found in database, inserting new record<br/>";
$insertPhotoSQL = sprintf("INSERT INTO photo_profile(photo_path)VALUES(%s)",
GetSQLValueString($photo_array[$i],"text"));
$result=mysql_query_or_die($insertPhotoSQL,$useradmin);
$photo_id=mysql_insert_id($useradmin);
$insertAlbumSQL = sprintf("INSERT INTO album_profile(album_id, photo_id, cover)VALUES(%s,%s,%s)",
GetSQLValueString($album_id,"int"),
GetSQLValueString($photo_id,"int"),
GetSQLValueString($i==0?'Y':'N',"text"));
$result=mysql_query_or_die($insertAlbumSQL,$useradmin);
}
}
unset($selectSQL);
}
$updateSQL=sprintf("UPDATE listings SET num_of_photos=%s, album_patched=%s WHERE sysid=%s",
GetSQLValueString(count($photo_array),"int"),
GetSQLValueString('Y',"text"),
GetSQLValueString($row_get_sysid['sysid'],"int"));
$result=mysql_query_or_die($updateSQL, $useradmin);
}else{
if(count($photo_id_array)==count($photo_array)){
if(count($photo_array)==1){
$updateSQL=sprintf("UPDATE listings SET album_patched=%s WHERE sysid=%s",
GetSQLValueString('Y',"text"),
GetSQLValueString($row_get_sysid['sysid'],"int"));
$result=mysql_query_or_die($updateSQL, $useradmin);
echo "<br/>retrieved 1 photo, and it is already in local database<br/>";
}else{
echo "<br/><span style=\"color:green;\">retrieved more than 1 photo, and it is already in local database, updating num_of_photos in listings table</span><br/>";
$updateSQL = sprintf("UPDATE listings SET num_of_photos=%s, album_patched=%s WHERE sysid=%s",
GetSQLValueString(count($photo_array),"int"),
GetSQLValueString('Y',"text"),
GetSQLValueString($row_get_sysid['sysid'],"int"));
$result=mysql_query_or_die($updateSQL, $useradmin);
}
}else{
echo "<br/><span style=\"color:green;\">取得的图片[".count($photo_array)."]没有本地图片[".count($photo_id_array)."]多</span><br/>";
$updateSQL = sprintf("UPDATE listings SET num_of_photos=%s, album_patched=%s WHERE sysid=%s",
GetSQLValueString(count($photo_id_array),"int"),
GetSQLValueString(count($photo_array)==0?'Y':'Y',"text"),
GetSQLValueString($row_get_sysid['sysid'],"int"));
$result=mysql_query_or_die($updateSQL, $useradmin);
}
}
}
}*/
}else{
echo "Empty Set<br/>";
}
function patch_album($get_sysid){
global $rets, $useradmin;
while($row_get_sysid=mysql_fetch_assoc($get_sysid)){
echo '<br/><br/>checking:<span style="color:#00ff00;">'.$row_get_sysid['sysid'].'</span><br/>';
$album_id=check_and_insert($row_get_sysid['sysid']);
if(!empty($album_id)){
$photo_id_array = get_photo_id($row_get_sysid['sysid']);
echo "<br/>";
var_dump($photo_id_array);
echo "<br/>";
$response=$rets->GetPhoto('Property',$row_get_sysid['sysid'].':*', 'images');
$photo_array=$response['photo_array'];
echo "<br/>服务器图片数量:".count($photo_array).' & 本地图片数量:'.count($photo_id_array)."<br/>";
if(count($photo_array)>count($photo_id_array)){
echo "<br/>We got more photos from the remote server<br/>";
for($i=0;$i<count($photo_array);$i++){
$selectSQL="SELECT * FROM photo_profile WHERE photo_path='".$photo_array[$i]."'";
if($photo_item=mysql_fetch_assoc(mysql_query_or_die($selectSQL, $useradmin))){
echo "<br/>photo_path found in database, nothing to be inserted.<br/>";
}else{
$selectSQL = "SELECT photo_profile.photo_id FROM listings LEFT JOIN album_profile ON listings.album_id = album_profile.album_id".
" LEFT JOIN photo_profile ON album_profile.photo_id = photo_profile.photo_id".
" WHERE photo_path='http://80a5e3a6041af90156b0-bf53eb49f254872ee431fd1273bc084d.r45.cf2.rackcdn.com/_.jpg'".
" AND listings.album_id=$album_id";
//echo $selectSQL;
$get_photo = mysql_query_or_die($selectSQL, $useradmin);
if($row_get_photo=mysql_fetch_assoc($get_photo)){
echo "<br/>Broken image found. Updating record.<br/>";
$photo_id=$row_get_photo['photo_id'];
$photo_path=$photo_array[$i];
$update_SQL=sprintf("UPDATE photo_profile SET photo_path=%s WHERE photo_id=%s",
GetSQLValueString($photo_path,"text"),
GetSQLValueString($photo_id,"int"));
$result=mysql_query_or_die($update_SQL, $useradmin);
}else{
echo "<br/>photo_path not found in database, inserting new record<br/>";
$insertPhotoSQL = sprintf("INSERT INTO photo_profile(photo_path)VALUES(%s)",
GetSQLValueString($photo_array[$i],"text"));
$result=mysql_query_or_die($insertPhotoSQL,$useradmin);
$photo_id=mysql_insert_id($useradmin);
$insertAlbumSQL = sprintf("INSERT INTO album_profile(album_id, photo_id, cover)VALUES(%s,%s,%s)",
GetSQLValueString($album_id,"int"),
GetSQLValueString($photo_id,"int"),
GetSQLValueString($i==0?'Y':'N',"text"));
$result=mysql_query_or_die($insertAlbumSQL,$useradmin);
}
}
unset($selectSQL);
}
$updateSQL=sprintf("UPDATE listings SET num_of_photos=%s, album_patched=%s WHERE sysid=%s",
GetSQLValueString(count($photo_array),"int"),
GetSQLValueString('Y',"text"),
GetSQLValueString($row_get_sysid['sysid'],"int"));
$result=mysql_query_or_die($updateSQL, $useradmin);
}elseif(count($photo_id_array)==count($photo_array)){
for($i=0;$i<count($photo_array);$i++){
//first we have to check if local database image is broken
$selectSQL = "SELECT photo_profile.photo_id FROM listings LEFT JOIN album_profile ON listings.album_id = album_profile.album_id".
" LEFT JOIN photo_profile ON album_profile.photo_id = photo_profile.photo_id".
" WHERE photo_path='http://80a5e3a6041af90156b0-bf53eb49f254872ee431fd1273bc084d.r45.cf2.rackcdn.com/_.jpg'".
" AND listings.album_id=$album_id";
//echo $selectSQL;
$get_photo = mysql_query_or_die($selectSQL, $useradmin);
if($row_get_photo=mysql_fetch_assoc($get_photo)){
echo "<br/><span style=\"color:red\">There is a broken image in album $album_id. Updating record.</span><br/>";
$photo_id=$row_get_photo['photo_id'];
$photo_path=$photo_array[$i];
$update_SQL=sprintf("UPDATE photo_profile SET photo_path=%s WHERE photo_id=%s",
GetSQLValueString($photo_path,"text"),
GetSQLValueString($photo_id,"int"));
$result=mysql_query_or_die($update_SQL, $useradmin);
}
}
$updateSQL=sprintf("UPDATE listings SET album_patched=%s WHERE sysid=%s",
GetSQLValueString('Y',"text"),
GetSQLValueString($row_get_sysid['sysid'],"int"));
$result=mysql_query_or_die($updateSQL, $useradmin);
}else{
echo "<br/><span style=\"color:green;\">取得的图片[".count($photo_array)."]没有本地图片[".count($photo_id_array)."]多</span><br/>";
$selectSQL = "SELECT photo_profile.photo_id FROM listings LEFT JOIN album_profile ON listings.album_id = album_profile.album_id".
" LEFT JOIN photo_profile ON album_profile.photo_id = photo_profile.photo_id".
" WHERE photo_path='http://80a5e3a6041af90156b0-bf53eb49f254872ee431fd1273bc084d.r45.cf2.rackcdn.com/_.jpg'".
" AND listings.album_id=$album_id";
//echo $selectSQL;
$get_photo = mysql_query_or_die($selectSQL, $useradmin);
if($row_get_photo=mysql_fetch_assoc($get_photo)){
echo "<br/>Broken image found. Updating record.<br/>";
$photo_id=$row_get_photo['photo_id'];
$deleteSQL = "DELETE FROM album_profile WHERE photo_id=$photo_id";
$result = mysql_query_or_die($deleteSQL, $useradmin);
$photo_id_array=get_photo_id($row_get_sysid['sysid']);
$updateSQL = sprintf("UPDATE listings SET num_of_photos=%s, album_patched=%s WHERE sysid=%s",
GetSQLValueString(count($photo_id_array),"int"),
GetSQLValueString(count($photo_array)==0?'Y':'Y',"text"),
GetSQLValueString($row_get_sysid['sysid'],"int"));
$result=mysql_query_or_die($updateSQL, $useradmin);
}
}
}
}
}
function check_and_insert($sysid){
global $useradmin;
$selectSQL = "SELECT * FROM listing_album WHERE sysid=$sysid";
$get_album = mysql_query_or_die($selectSQL, $useradmin);
if($row_get_album=mysql_fetch_assoc($get_album)){
echo '<br/><span style="color:#ff0000">album found in database</span><br/>';
echo '<br/><span style="color:#ff0000">returning'.$row_get_album['album_id'].' as album_id</span><br/>';
return $row_get_album['album_id'];
}else{
$insertSQL=sprintf("INSERT INTO listing_album (sysid) VALUES(%s)",
GetSQLValueString($sysid,"int"));
$result=mysql_query_or_die($insertSQL,$useradmin);
$album_id=mysql_insert_id($useradmin);
$updateSQL=sprintf("UPDATE listings SET album_id=%s WHERE sysid=%s",
GetSQLValueString($album_id,"int"),
GetSQLValueString($sysid,"int"));
$result=mysql_query_or_die($updateSQL,$useradmin);
return $album_id;
}
}
function get_num_of_photos($sysid){
global $useradmin;
$selectSQL = "SELECT num_of_photos FROM listings WHERE sysid=".$sysid;
$get_num_of_photos = mysql_query_or_die($selectSQL, $useradmin);
if($row_get_num_of_photos=mysql_fetch_assoc($get_num_of_photos)){
return $row_get_num_of_photos['num_of_photos'];
}else{
return false;
}
}
function get_photo_id($sysid){
global $useradmin;
$selectSQL = "SELECT album_profile.photo_id FROM listing_album".
" LEFT JOIN album_profile ON listing_album.album_id=album_profile.album_id".
" LEFT JOIN photo_profile ON album_profile.photo_id=photo_profile.photo_id".
" WHERE !ISNULL(photo_profile.photo_id) AND !ISNULL(photo_profile.photo_path) AND listing_album.sysid=".$sysid." GROUP BY photo_path";
$get_photo_id = mysql_query_or_die($selectSQL, $useradmin);
$photo_id_array=array();
while($row_get_photo_id=mysql_fetch_assoc($get_photo_id)){
array_push($photo_id_array, $row_get_photo_id['photo_id']);
}
return $photo_id_array;
}
?>