/
refresh.php
199 lines (143 loc) · 5.32 KB
/
refresh.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
<?php
/*
* Refresh memcache cache for changed photosets
* If given an argument, force refresh on all photosets and photos within them
*
*
*/
require_once 'config.flickr.php';
require_once 'hdFlickr.php';
// UTF-8
mb_internal_encoding("UTF-8");
$force_refresh = FALSE;
if($argc > 1 && (!strcasecmp($argv[1], "-h") || !strcasecmp($argv[1], "--help"))) {
echo "This utility script refreshes the internal memcached-based cache.\n";
echo "To refresh changed (i.e, diffrent number of images) photosets:\n";
echo " \$ php refresh.php\n";
echo "To refresh all available photosets:\n";
echo " \$ php refresh.php all\n";
echo "\n";
}
else if($argc > 1) {
echo "* Forcing refresh of cached data for all photosets and their photos\n";
$force_refresh = TRUE;
}
// Connect to memcache, if available
$mc = NULL;
if(class_exists("Memcache")) {
$mc = new Memcache;
if(!@$mc->connect("localhost", 11211))
$mc = NULL;
}
// Initialize API access
$flickr = new hdFlickr($api_key, $api_secret, $flickr_username, $mc);
if($flickr->initialize() === FALSE) {
die($generic_error_msg);
}
// Print current statistics
$stats = array();
if(($details = $flickr->getStats()) !== FALSE) {
foreach($details as $k => $v)
$stats[] = "$k=$v";
if($details["cache_hits"] > 0 && $details["cache_misses"] > 0)
$stats[] = sprintf("hit-ratio=%.2f", 100.0 * $details["cache_hits"] / ($details["cache_hits"] + $details["cache_misses"]));
}
else
$stats[] = "N/A";
echo "* Currents stats: ". implode(", ", $stats) ."\n";
// Fetch a (possibly) cached list
$flickr->useCache(TRUE);
$old = $flickr->getPhotosetsXML();
// ..and a known fresh list
$flickr->useCache(FALSE);
$fresh = $flickr->getPhotosetsXML();
echo "* Refreshed list of photosets (photosets.getList)\n";
echo " - Number of sets in cached copy: ". count($old->photoset) ."\n";
echo " - Number of sets in fresh copy: ". count($fresh->photoset) ."\n";
// Compare the fresh list of photosets with the old one
// and build a list of photosets that need to be refreshed
$photosets_to_refresh = array();
foreach($fresh->photoset as $fp) {
if($force_refresh) {
$photosets_to_refresh[] = (string)$fp["id"];
continue;
}
$found = FALSE;
foreach($old->photoset as $op) {
if(strcmp((string)$fp["id"], (string)$op["id"]))
continue;
$found = TRUE;
break;
}
if(!$found) {
echo "* Photoset with id '". (string)$fp["id"] ."' was previously not present in cache (NEW)!\n";
$photosets_to_refresh[] = (string)$fp["id"];
continue;
}
$diffs = array();
if((string)$fp["photos"] != (string)$op["photos"])
$diffs[] = "Number of photos differs, old=". (string)$op["photos"] .", new=". (string)$fp["photos"];
if((string)$fp["primary"] != (string)$op["primary"])
$diffs[] = "Primary ID differs, old=". (string)$op["primary"] .", new=". (string)$fp["primary"];
if(strcmp($fp->title, $op->title))
$diffs[] = "Title differs, new='". $fp->title ."'";
if(strcmp($fp->description, $op->description))
$diffs[] = "Description differs, new='". ereg_replace("\r\n|\r|\n", " ", $fp->desciption) ."'";
if(count($diffs) == 0)
continue;
echo "* Photoset with id '". (string)$fp["id"] ."' differs from the cached copy\n";
foreach($diffs as $str)
echo " - $str\n";
$photosets_to_refresh[] = (string)$fp["id"];
}
echo "* Refreshing ". count($photosets_to_refresh) ." photosets";
if(count($photosets_to_refresh))
echo " with id '". implode("', '", $photosets_to_refresh) ."'\n";
else
echo "\n";
$t0 = time();
foreach($photosets_to_refresh as $set_id) {
echo " $set_id: Refreshing meta data.. ";
if(($photoset = $flickr->getPhotosetXML($set_id)) === FALSE)
echo "FAILED!\n";
else
echo "OK!\n";
echo " $set_id: Refreshing list of photos.. ";
if(($photos = $flickr->getPhotosXML($set_id)) === FALSE)
echo "FAILED!\n";
else
echo "OK, set has ". count($photos->photo) ." photos\n";
// Simluate browsing photos (image.php) to refresh all required data
echo " $set_id: Refreshing photo details (phots.getInfo) and context (photos.getContext)\n";
$t1 = time();
$i = 1;
$num_photos = count($photos->photo);
foreach($photos->photo as $p) {
$photo_id = (string)$p["id"];
echo sprintf(" $set_id: (photo %02d/%02d) Refreshing details (photos.getInfo) for $photo_id.. ", $i, $num_photos);
if(($context = $flickr->getPhotoInfo((string)$p["id"], (string)$p["secret"])) === FALSE)
echo "FAILED\n";
else
echo "OK\n";
echo sprintf(" $set_id: (photo %02d/%02d) Refreshing photoset context for $photo_id.. ", $i, $num_photos);
if(($context = $flickr->getPhotosetContext($set_id, $p["id"])) === FALSE)
echo "FAILED\n";
else
echo "OK\n";
$i++;
}
echo sprintf(" $set_id: Done refreshing set, %d photos refreshed in %d seconds (%.2f/sec)\n", $num_photos, time() - $t1, $num_photos / (time() - $t1));
}
echo "* Done refreshing cache, ". (time() - $t0) ." seconds elapsed\n";
$stats = array();
if(($details = $flickr->getStats()) !== FALSE) {
foreach($details as $k => $v)
$stats[] = "$k=$v";
if($details["cache_hits"] > 0 && $details["cache_misses"] > 0) {
$stats[] = sprintf("hit-ratio=%.2f", 100.0 * $details["cache_hits"] / ($details["cache_hits"] + $details["cache_misses"]));
}
}
else
$stats[] = "N/A";
echo "* New stats: ". implode(", ", $stats) ."\n";
?>