Mercurial > hg > mgallery
annotate mgtool.php @ 37:99dc0843d7df
Fix sorting of albums to match image sorting.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Tue, 03 May 2016 13:44:13 +0300 |
parents | 985596db0f01 |
children | d555734526d6 |
rev | line source |
---|---|
0 | 1 #!/usr/bin/php |
2 <?php | |
3 // | |
4 // Yet Another Image Gallery | |
5 // Commandline tool for creating / updating gallery | |
32 | 6 // (C) Copyright 2015-2016 Tecnic Software productions (TNSP) |
0 | 7 // |
8 require_once "mgallery.inc.php"; | |
9 | |
10 // | |
11 // Array for specifying what will be copied and converted | |
12 // from the image file's EXIF information tag(s). | |
13 // | |
14 $galExifConversions = array( | |
15 array(MG_STR, "caption" , "ImageDescription"), | |
16 array(MG_STR, "copyright" , "Copyright"), | |
17 array(MG_STR, "model" , "Model"), | |
18 array(MG_INT, "width" , array("COMPUTED", "Width")), | |
19 array(MG_INT, "height" , array("COMPUTED", "Height")), | |
20 array(MG_DVA, "fnumber" , "FNumber"), | |
21 array(MG_DVA, "exposure" , "ExposureTime"), | |
22 array(MG_INT, "iso" , "ISOSpeedRatings"), | |
29
8cf743c7ea23
Add focal length and lens model display.
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
23 array(MG_STR, "lensmodel" , "UndefinedTag:0xA434"), |
0 | 24 array(MG_DVA, "focallength" , "FocalLength"), |
25 array(MG_STR, "datetime" , "DateTimeOriginal"), | |
26 array(MG_STR, "datetime" , "DateTimeDigitized"), | |
27 array(MG_INT, "filesize" , "FileSize"), | |
28 ); | |
29 | |
30 define("GCMD_UPDATE" , 1); | |
31 define("GCMD_RESCAN" , 2); | |
32 define("GCMD_CLEAN" , 3); | |
33 | |
34 define("GCLEAN_CACHES" , 0x01); | |
35 define("GCLEAN_THUMBNAILS", 0x02); | |
36 define("GCLEAN_ALL" , 0x0f); | |
37 | |
38 define("GUPD_MED_IMAGE" , 0x01); | |
39 define("GUPD_TN_IMAGE" , 0x02); | |
40 define("GUPD_IMAGES" , 0x0f); | |
41 define("GUPD_EXIF_INFO" , 0x10); | |
42 define("GUPD_CAPTION" , 0x20); | |
43 | |
44 | |
45 // | |
46 // Convert and scale image file function, for generating | |
47 // the intermediate size images and thumbnails. Uses the | |
48 // PHP ImageMagick bindings. | |
49 // | |
50 function mgConvertImage($inFilename, $outFilename, $outDim, $outFormat, $outQuality, $thumb) | |
51 { | |
52 // Create conversion entity | |
53 $img = new Imagick($inFilename); | |
54 if ($img === FALSE) | |
55 return mgError("ImageMagick could not digest the file '".$inFilename."'.\n"); | |
56 | |
57 if ($outDim !== FALSE) | |
58 { | |
59 // Get dimensions, setup background | |
60 $dim = $img->getImageGeometry(); | |
61 //$img->setImageBackgroundColor(imagick::COLOR_BLACK); | |
62 $img->setGravity(imagick::GRAVITY_CENTER); | |
63 | |
64 | |
65 if ($dim["width"] < $dim["height"]) | |
66 { | |
67 $stmp = $outDim[0]; | |
68 $outDim[0] = $outDim[1]; | |
69 $outDim[1] = $stmp; | |
70 } | |
71 | |
72 if ($dim["width"] != $outDim[0] || $dim["height"] != $outDim[1]) | |
73 { | |
74 $outDim[1] = ($dim["height"] * $outDim[0]) / $dim["width"]; | |
75 } | |
76 | |
77 // Act based on image size vs. desired size and $thumb mode | |
78 if ($thumb || $dim["width"] > $outDim[0] || $dim["height"] > $outDim[1]) | |
79 { | |
80 // Image is larger | |
1
a07447c02b13
Use Lanczos filter for scaling instead of quadratic. Better quality and
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
81 $img->resizeImage($outDim[0], $outDim[1], Imagick::FILTER_LANCZOS, 1); |
0 | 82 $img->setImageExtent($outDim[0], $outDim[1]); |
83 $img->normalizeImage(); | |
84 $img->unsharpMaskImage(0, 0.5, 1, 0.05); | |
85 } | |
86 if ($dim["width"] < $outDim[0] || $dim["height"] < $outDim[1]) | |
87 { | |
88 // Image is smaller than requested dimension(s)? | |
1
a07447c02b13
Use Lanczos filter for scaling instead of quadratic. Better quality and
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
89 $img->resizeImage($outDim[0], $outDim[1], Imagick::FILTER_LANCZOS, 1); |
0 | 90 $img->setImageExtent($outDim[0], $outDim[1]); |
91 $img->unsharpMaskImage(0, 0.5, 1, 0.05); | |
92 } | |
93 } | |
94 | |
95 $img->setFormat($outFormat); | |
96 $img->setCompressionQuality($outQuality); | |
97 | |
98 $img->stripImage(); | |
99 $img->writeImage($outFilename); | |
100 $img->removeImage(); | |
101 return TRUE; | |
102 } | |
103 | |
104 | |
105 // | |
106 // Converts one value (mainly from EXIF tag information) | |
107 // by doing explicing type casting and special conversions. | |
108 // | |
109 function mgConvertExifData($val, $vtype) | |
110 { | |
111 switch ($vtype) | |
112 { | |
113 case MG_STR: return (string) $val; | |
114 case MG_INT: return intval($val); | |
115 case MG_BOOL: return intval($val); | |
116 case MG_DVA: | |
117 if (sscanf($val, "%d/%d", $v1, $v2) == 2) | |
118 { | |
119 if ($v1 < $v2) | |
120 return $val; | |
121 else | |
122 return sprintf("%1.1f", $v1 / $v2); | |
123 } | |
124 else | |
125 return $val; | |
126 | |
127 default: | |
128 return $val; | |
129 } | |
130 } | |
131 | |
132 | |
133 // | |
134 // Conditionally copies one "field" from an associated array/hash to another. | |
135 // If destination is already SET, nothing will be done. If source does | |
136 // not exist (e.g. one or more of the keys do not exist in source array), | |
137 // a default value will be used, if provided. | |
138 // Source may have multi-depth keys, destination has one key. | |
139 // | |
140 function mgCopyEntryData(&$dst, $src, $vtype, $dkey, $skeys, $default = NULL) | |
141 { | |
142 // Is destination already set? | |
143 if (isset($dst[$dkey])) | |
144 return FALSE; | |
145 | |
146 // If input key is not array, change it into one | |
147 if (!is_array($skeys)) | |
148 $skeys = array($skeys); | |
149 | |
150 // Traverse input array by using consequent keys | |
151 $tmp = &$src; | |
152 foreach ($skeys as $skey) | |
153 { | |
154 if (!array_key_exists($skey, $tmp)) | |
155 { | |
156 // Key didn't exist, try for default | |
157 if ($default !== NULL) | |
158 $dst[$dkey] = $default; | |
159 return FALSE; | |
160 } | |
161 else | |
162 $tmp = &$tmp[$skey]; | |
163 } | |
164 | |
165 // Optionally convert the input value | |
166 $dst[$dkey] = mgConvertExifData($tmp, $vtype); | |
167 return TRUE; | |
168 } | |
169 | |
170 | |
171 // | |
172 // Attempt to get gallery album data from various sources. | |
173 // | |
174 function mgGetAlbumData($galBasePath, $galPath) | |
175 { | |
176 // Check path permissions | |
177 $galData = array(); | |
178 if (is_readable($galPath)) | |
179 { | |
180 // First, try to read gallery/album info file | |
181 $filename = mgGetPath($galPath, "info_file"); | |
182 if ($filename !== FALSE && file_exists($filename)) | |
183 { | |
184 mgDebug("Reading INFOFILE: ".$filename."\n"); | |
185 if (($galData = parse_ini_file($filename, FALSE)) === FALSE) | |
186 $galData = array(); | |
187 } | |
188 | |
189 // Read header file, if any, and we don't have "header" field set yet | |
190 $filename = mgGetPath($galPath, "header_file"); | |
191 if ($filename !== FALSE && file_exists($filename) && | |
192 !isset($galData["header"])) | |
193 { | |
194 mgDebug("Reading HEADERFILE: ".$filename."\n"); | |
195 $galData["header"] = file_get_contents($filename); | |
196 } | |
197 | |
198 // Check for alternate key/values for album title/caption | |
199 if (isset($galData["title"]) && !isset($galData["caption"])) | |
200 { | |
201 $galData["caption"] = $galData["title"]; | |
202 unset($galData["title"]); | |
203 } | |
204 } | |
205 else | |
206 $galData["hide"] = TRUE; | |
207 | |
208 // If caption is not set, use last path component for | |
209 // a fallback value in case we don't discover proper title | |
210 // from other sources we can't check here yet. | |
211 $path = explode("/", $galPath); | |
212 $galData["fallback_caption"] = ucfirst(str_replace("_", " ", end($path))); | |
213 | |
214 // Last, store the current gallery path | |
215 $len = strlen($galBasePath); | |
216 if ($len < strlen($galPath) && substr($galPath, 0, $len) == $galBasePath) | |
217 $galData["path"] = substr($galPath, $len); | |
218 else | |
219 $galData["path"] = ""; | |
220 | |
221 return $galData; | |
222 } | |
223 | |
224 | |
225 function mgReadCaptionsFile($galBasePath, $galPath) | |
226 { | |
227 $captions = array(); | |
228 $filename = mgGetPath($galPath, "captions_file"); | |
229 if ($filename === FALSE || ($fp = @fopen($filename, "rb")) === FALSE) | |
230 return $captions; | |
231 | |
232 mgDebug("Reading CAPTIONS: ".$filename."\n"); | |
233 | |
234 // Read and parse data | |
235 while (!feof($fp)) | |
236 { | |
237 $str = trim(fgets($fp)); | |
238 // Ignore comments and empty lines | |
239 if ($str != "#" && $str != "") | |
240 { | |
11
0b097770061d
Improve content hiding functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
241 if (preg_match("/^([#%]?)\s*(\S+?)\s+(.+)$/", $str, $m)) |
0b097770061d
Improve content hiding functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
242 $captions[$m[2]] = array("caption" => $m[3], "hide" => ($m[1] == "#"), "hide_contents" => ($m[1] == "%"), "used" => FALSE); |
0 | 243 else |
11
0b097770061d
Improve content hiding functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
244 if (preg_match("/^([#%]?)\s*(\S+?)$/", $str, $m)) |
0b097770061d
Improve content hiding functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
245 $captions[$m[2]] = array("hide" => ($m[1] == "#"), "hide_contents" => ($m[1] == "%"), "used" => FALSE); |
0 | 246 } |
247 } | |
248 | |
249 fclose($fp); | |
250 return $captions; | |
251 } | |
252 | |
253 | |
254 function mgMakeDir($path, $perm) | |
255 { | |
256 if (!file_exists($path)) | |
257 { | |
258 if (mkdir($path, $perm, TRUE) === false) | |
259 return mgError("Could not create directory '".$path."'\n"); | |
260 } | |
261 return TRUE; | |
262 } | |
263 | |
264 | |
265 function mgYesNoPrompt($msg, $default = FALSE) | |
266 { | |
267 echo $msg." [".($default ? "Y/n" : "y/N")."]? "; | |
268 $sprompt = strtolower(trim(fgets(STDIN))); | |
269 | |
270 if ($default) | |
271 return ($sprompt == "n"); | |
272 else | |
273 return ($sprompt == "y"); | |
274 } | |
275 | |
276 | |
277 function mgDelete($path, $recurse) | |
278 { | |
279 global $flagDoDelete; | |
280 if (is_dir($path)) | |
281 { | |
282 if (($dirHandle = @opendir($path)) === FALSE) | |
283 return mgError("Could not read directory '".$path."'.\n"); | |
284 | |
285 while (($dirFile = @readdir($dirHandle)) !== FALSE) | |
286 { | |
287 if ($dirFile != "." && $dirFile != "..") | |
288 mgDelete($path."/".$dirFile, $recurse); | |
289 } | |
290 closedir($dirHandle); | |
291 | |
292 echo " - ".$path." [DIR]\n"; | |
293 if ($flagDoDelete) | |
294 rmdir($path); | |
295 } | |
296 else | |
297 { | |
298 if (!$recurse) | |
299 echo " - ".$path."\n"; | |
300 if ($flagDoDelete) | |
301 unlink($path); | |
302 } | |
303 } | |
304 | |
305 | |
306 function mgCheckQuit($now = FALSE) | |
307 { | |
308 global $flagQuit; | |
309 | |
310 // Dispatch pending signals | |
311 pcntl_signal_dispatch(); | |
312 | |
313 // Check result | |
314 if ($now && $flagQuit) | |
315 mgFatal("Quitting.\n"); | |
316 | |
317 return $flagQuit; | |
318 } | |
319 | |
320 | |
321 function mgNeedUpdate($entry, $field, $cvalue) | |
322 { | |
323 if (!array_key_exists($field, $entry)) | |
324 return TRUE; | |
325 | |
326 return ($entry[$field] < $cvalue); | |
327 } | |
328 | |
329 | |
30
f2c21a8b9071
Sort images in a folder by their capture timestamp.
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
330 function mgSortFunc($a, $b) |
f2c21a8b9071
Sort images in a folder by their capture timestamp.
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
331 { |
f2c21a8b9071
Sort images in a folder by their capture timestamp.
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
332 if (isset($a["datetime"]) && isset($b["datetime"])) |
f2c21a8b9071
Sort images in a folder by their capture timestamp.
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
333 return strcmp($b["datetime"], $a["datetime"]); |
f2c21a8b9071
Sort images in a folder by their capture timestamp.
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
334 else |
f2c21a8b9071
Sort images in a folder by their capture timestamp.
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
335 return 0; |
f2c21a8b9071
Sort images in a folder by their capture timestamp.
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
336 } |
f2c21a8b9071
Sort images in a folder by their capture timestamp.
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
337 |
f2c21a8b9071
Sort images in a folder by their capture timestamp.
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
338 |
16
3491ab93630e
Add sub-gallery thumbnails to gallery cache data.
Matti Hamalainen <ccr@tnsp.org>
parents:
15
diff
changeset
|
339 function mgWriteGalleryCache($cacheFilename, &$gallery, &$entries, &$parentEntry) |
15
805f93962cf9
Factorize gallery cache file writing to a separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
340 { |
805f93962cf9
Factorize gallery cache file writing to a separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
341 // Store gallery cache for this directory |
805f93962cf9
Factorize gallery cache file writing to a separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
342 $images = array(); |
805f93962cf9
Factorize gallery cache file writing to a separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
343 $albums = array(); |
805f93962cf9
Factorize gallery cache file writing to a separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
344 foreach ($entries as $ename => &$edata) |
805f93962cf9
Factorize gallery cache file writing to a separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
345 { |
805f93962cf9
Factorize gallery cache file writing to a separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
346 if ($edata["hide"]) |
805f93962cf9
Factorize gallery cache file writing to a separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
347 continue; |
805f93962cf9
Factorize gallery cache file writing to a separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
348 |
805f93962cf9
Factorize gallery cache file writing to a separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
349 unset($edata["hide"]); |
805f93962cf9
Factorize gallery cache file writing to a separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
350 if ($edata["type"] == 0) |
805f93962cf9
Factorize gallery cache file writing to a separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
351 $images[$ename] = &$edata; |
805f93962cf9
Factorize gallery cache file writing to a separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
352 else |
805f93962cf9
Factorize gallery cache file writing to a separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
353 $albums[$ename] = &$edata; |
805f93962cf9
Factorize gallery cache file writing to a separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
354 } |
30
f2c21a8b9071
Sort images in a folder by their capture timestamp.
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
355 |
f2c21a8b9071
Sort images in a folder by their capture timestamp.
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
356 uasort($images, "mgSortFunc"); |
37
99dc0843d7df
Fix sorting of albums to match image sorting.
Matti Hamalainen <ccr@tnsp.org>
parents:
35
diff
changeset
|
357 krsort($albums); |
15
805f93962cf9
Factorize gallery cache file writing to a separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
358 |
16
3491ab93630e
Add sub-gallery thumbnails to gallery cache data.
Matti Hamalainen <ccr@tnsp.org>
parents:
15
diff
changeset
|
359 // Choose gallery album image |
3491ab93630e
Add sub-gallery thumbnails to gallery cache data.
Matti Hamalainen <ccr@tnsp.org>
parents:
15
diff
changeset
|
360 if (count($images) > 0) |
3491ab93630e
Add sub-gallery thumbnails to gallery cache data.
Matti Hamalainen <ccr@tnsp.org>
parents:
15
diff
changeset
|
361 { |
3491ab93630e
Add sub-gallery thumbnails to gallery cache data.
Matti Hamalainen <ccr@tnsp.org>
parents:
15
diff
changeset
|
362 end($images); |
3491ab93630e
Add sub-gallery thumbnails to gallery cache data.
Matti Hamalainen <ccr@tnsp.org>
parents:
15
diff
changeset
|
363 $parentEntry["image"] = key($images); |
3491ab93630e
Add sub-gallery thumbnails to gallery cache data.
Matti Hamalainen <ccr@tnsp.org>
parents:
15
diff
changeset
|
364 } |
35
985596db0f01
Implement recursive depth album covers.
Matti Hamalainen <ccr@tnsp.org>
parents:
32
diff
changeset
|
365 else |
985596db0f01
Implement recursive depth album covers.
Matti Hamalainen <ccr@tnsp.org>
parents:
32
diff
changeset
|
366 foreach ($albums as $aid => &$adata) |
985596db0f01
Implement recursive depth album covers.
Matti Hamalainen <ccr@tnsp.org>
parents:
32
diff
changeset
|
367 if (isset($adata["image"])) |
985596db0f01
Implement recursive depth album covers.
Matti Hamalainen <ccr@tnsp.org>
parents:
32
diff
changeset
|
368 { |
985596db0f01
Implement recursive depth album covers.
Matti Hamalainen <ccr@tnsp.org>
parents:
32
diff
changeset
|
369 $parentEntry["image"] = &$adata; |
985596db0f01
Implement recursive depth album covers.
Matti Hamalainen <ccr@tnsp.org>
parents:
32
diff
changeset
|
370 break; |
985596db0f01
Implement recursive depth album covers.
Matti Hamalainen <ccr@tnsp.org>
parents:
32
diff
changeset
|
371 } |
16
3491ab93630e
Add sub-gallery thumbnails to gallery cache data.
Matti Hamalainen <ccr@tnsp.org>
parents:
15
diff
changeset
|
372 |
15
805f93962cf9
Factorize gallery cache file writing to a separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
373 $str = |
805f93962cf9
Factorize gallery cache file writing to a separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
374 "<?\n". |
805f93962cf9
Factorize gallery cache file writing to a separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
375 "\$galData = ".var_export($gallery, TRUE).";\n". |
805f93962cf9
Factorize gallery cache file writing to a separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
376 "\$galAlbumsIndex = ".var_export(array_keys($albums), TRUE).";\n". |
805f93962cf9
Factorize gallery cache file writing to a separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
377 "\$galImagesIndex = ".var_export(array_keys($images), TRUE).";\n". |
805f93962cf9
Factorize gallery cache file writing to a separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
378 "\$galEntries = ".var_export($entries, TRUE).";\n". |
805f93962cf9
Factorize gallery cache file writing to a separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
379 "?>"; |
805f93962cf9
Factorize gallery cache file writing to a separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
380 |
805f93962cf9
Factorize gallery cache file writing to a separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
381 if (@file_put_contents($cacheFilename, $str, LOCK_EX) === FALSE) |
805f93962cf9
Factorize gallery cache file writing to a separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
382 return mgError("Error writing '".$cacheFilename."'\n"); |
805f93962cf9
Factorize gallery cache file writing to a separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
383 |
805f93962cf9
Factorize gallery cache file writing to a separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
384 return TRUE; |
805f93962cf9
Factorize gallery cache file writing to a separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
385 } |
805f93962cf9
Factorize gallery cache file writing to a separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
386 |
16
3491ab93630e
Add sub-gallery thumbnails to gallery cache data.
Matti Hamalainen <ccr@tnsp.org>
parents:
15
diff
changeset
|
387 |
3491ab93630e
Add sub-gallery thumbnails to gallery cache data.
Matti Hamalainen <ccr@tnsp.org>
parents:
15
diff
changeset
|
388 function mgHandleDirectory($mode, $basepath, $path, &$parentData, &$parentEntry, $writeMode, $startAt) |
0 | 389 { |
390 global $galExifConversions, $galTNPath, $galCleanFlags; | |
391 | |
392 // Get cache file path | |
393 if (($cacheFilename = mgGetPath($path, "cache_file")) === FALSE) | |
394 return mgError("Cache filename / path not set.\n"); | |
395 | |
396 mgCheckQuit(TRUE); | |
397 | |
398 // Read directory contents | |
399 $entries = array(); | |
400 if (($dirHandle = @opendir($path)) === FALSE) | |
401 return mgError("Could not read directory '".$path."'.\n"); | |
402 | |
403 while (($dirFile = @readdir($dirHandle)) !== FALSE) | |
404 { | |
405 $realFile = $path."/".$dirFile; | |
406 if (is_dir($realFile)) | |
407 { | |
408 if ($dirFile[0] != "." && $dirFile != $galTNPath) | |
409 $entries[$dirFile] = array("type" => 1, "base" => $dirFile, "ext" => "", "mtime" => filemtime($realFile)); | |
410 } | |
411 else | |
6
405a02586fc2
Fix handling of image files that have spaces in filename (regexp used \S,
Matti Hamalainen <ccr@tnsp.org>
parents:
1
diff
changeset
|
412 if (preg_match("/^([^\/]+)(".mgGetSetting("format_exts").")$/i", $dirFile, $dirMatch)) |
0 | 413 $entries[$dirFile] = array("type" => 0, "base" => $dirMatch[1], "ext" => $dirMatch[2], "mtime" => filemtime($realFile), "hide" => false); |
414 } | |
415 closedir($dirHandle); | |
416 | |
417 mgCheckQuit(); | |
418 | |
419 // Cleanup mode | |
420 if ($mode == GCMD_CLEAN) | |
421 { | |
422 $gallery = array(); | |
423 | |
424 if ($writeMode) | |
425 { | |
426 if ($galCleanFlags & GCLEAN_CACHES) | |
427 mgDelete($cacheFilename, FALSE); | |
428 | |
429 if ($galCleanFlags & GCLEAN_THUMBNAILS) | |
430 mgDelete($path."/".$galTNPath, TRUE); | |
431 } | |
432 } | |
433 else | |
434 // Update modes | |
435 if ($mode == GCMD_UPDATE || $mode == GCMD_RESCAN) | |
436 { | |
437 // Load current cache file, if it exists | |
438 $galEntries = array(); | |
439 $cacheTime = -1; | |
440 if ($mode == GCMD_UPDATE && file_exists($cacheFilename)) | |
441 { | |
442 $cacheTime = filemtime($cacheFilename); | |
443 @include $cacheFilename; | |
444 } | |
445 | |
446 // Read caption data | |
447 $captions = mgReadCaptionsFile($basepath, $path); | |
448 $gallery = mgGetAlbumData($basepath, $path); | |
449 if ($parentData !== NULL && $parentEntry !== NULL) | |
450 { | |
16
3491ab93630e
Add sub-gallery thumbnails to gallery cache data.
Matti Hamalainen <ccr@tnsp.org>
parents:
15
diff
changeset
|
451 $gallery["parent"] = &$parentData; |
0 | 452 mgCopyEntryData($gallery, $parentEntry, MG_STR, "caption", "caption"); |
453 } | |
454 | |
455 // Start actual processing | |
456 $nentries = count($entries); | |
457 $nentry = 0; | |
458 echo $path." .. "; | |
459 foreach ($entries as $ename => &$edata) | |
460 { | |
461 printf("\r%s (%1.1f%%) ..", $path, ($nentry * 100.0) / $nentries); | |
462 | |
463 $nentry++; | |
464 $efilename = $path."/".$ename; | |
465 | |
466 if (array_key_exists($ename, $galEntries)) | |
467 $galEntry = &$galEntries[$ename]; | |
468 else | |
469 $galEntry = array(); | |
470 | |
471 mgCheckQuit(FALSE); | |
472 | |
473 // Update with captions file data, if any | |
474 if (array_key_exists($ename, $captions)) | |
475 { | |
476 foreach ($captions[$ename] as $ckey => $cval) | |
477 $edata[$ckey] = $cval; | |
478 } | |
479 | |
480 // Handle entry based on type | |
481 if ($edata["type"] == 0) | |
482 { | |
483 $updFlags = 0; | |
484 $tnPath = $path."/".$galTNPath; | |
485 $medFilename = $tnPath."/".$edata["base"].mgGetSetting("med_suffix").$edata["ext"]; | |
486 $tnFilename = $tnPath."/".$ename; | |
487 $capFilename = $path."/".$edata["base"].".txt"; | |
488 | |
489 // Check what we need to update .. | |
490 if (!file_exists($medFilename) || filemtime($medFilename) < $edata["mtime"]) | |
491 $updFlags |= GUPD_MED_IMAGE; | |
492 | |
493 if (!file_exists($tnFilename) || filemtime($tnFilename) < $edata["mtime"]) | |
494 $updFlags |= GUPD_TN_IMAGE; | |
495 | |
496 if (mgNeedUpdate($galEntry, "mtime", $edata["mtime"])) | |
497 $updFlags |= GUPD_EXIF_INFO; | |
498 | |
499 if (file_exists($capFilename) && | |
500 mgNeedUpdate($galEntry, "mtime", filemtime($capFilename))) | |
501 $updFlags |= GUPD_CAPTION; | |
502 | |
503 // Check for EXIF info | |
504 if (($updFlags & GUPD_EXIF_INFO) && | |
505 ($exif = @exif_read_data($efilename)) !== FALSE) | |
506 { | |
507 echo "%"; | |
508 foreach ($galExifConversions as $conv) | |
509 mgCopyEntryData($edata, $exif, $conv[0], $conv[1], $conv[2]); | |
510 } | |
511 else | |
512 { | |
513 // Copy old data that is not yet in new | |
514 echo "*"; | |
515 foreach ($galEntry as $okey => $odata) | |
516 { | |
517 if (!array_key_exists($okey, $edata)) | |
518 $edata[$okey] = $odata; | |
519 } | |
520 } | |
521 | |
522 // Generate thumbnails, etc. | |
523 if ($updFlags & GUPD_IMAGES) | |
524 { | |
525 mgMakeDir($tnPath, 0755); | |
526 | |
527 if ($updFlags & GUPD_MED_IMAGE) | |
528 { | |
529 echo "1"; | |
530 mgConvertImage($efilename, $medFilename, | |
531 array(mgGetSetting("med_width"), mgGetSetting("med_height")), | |
532 "JPEG", mgGetSetting("med_quality"), TRUE); | |
533 } | |
534 | |
535 if ($updFlags & GUPD_TN_IMAGE) | |
536 { | |
537 echo "2"; | |
538 mgConvertImage($efilename, $tnFilename, | |
539 array(mgGetSetting("tn_width"), mgGetSetting("tn_height")), | |
540 "JPEG", mgGetSetting("tn_quality"), TRUE); | |
541 } | |
542 } | |
543 | |
544 // Check for .txt caption file | |
545 if ($updFlags & GUPD_CAPTION) | |
546 { | |
547 echo "?"; | |
548 if (($tmpData = @file_get_contents($capFilename)) !== FALSE) | |
549 $edata["caption"] = $tmpData; | |
550 } | |
551 } | |
552 else | |
553 if ($edata["type"] == 1) | |
554 { | |
10 | 555 // Set some of the album data here |
0 | 556 $tmp = mgGetAlbumData($basepath, $efilename); |
557 mgCopyEntryData($edata, $tmp, MG_STR, "caption", "caption"); | |
558 mgCopyEntryData($edata, $tmp, MG_STR, "caption", "fallback_caption"); | |
559 mgCopyEntryData($edata, $tmp, MG_STR, "caption", "title"); | |
560 mgCopyEntryData($edata, $tmp, MG_BOOL, "hide", "hide", FALSE); | |
11
0b097770061d
Improve content hiding functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
561 mgCopyEntryData($edata, $tmp, MG_BOOL, "hide_contents", "hide_contents", FALSE); |
0 | 562 } |
563 } | |
564 | |
565 echo "\r".$path." ..... DONE\n"; | |
566 | |
567 mgCheckQuit(TRUE); | |
568 } | |
569 else | |
570 mgFatal("Invalid work mode '".$mode."'.\n"); | |
571 | |
572 mgCheckQuit(TRUE); | |
573 | |
574 // Recurse to subdirectories | |
9
232086da1670
Fix generating of sub-album/dirs names without gallery.info to specify
Matti Hamalainen <ccr@tnsp.org>
parents:
6
diff
changeset
|
575 foreach ($entries as $ename => &$edata) |
0 | 576 if ($edata["type"] == 1) |
577 { | |
578 $epath = $path."/".$ename."/"; | |
579 $newWriteMode = ($writeMode === FALSE && $epath == $startAt) || $writeMode; | |
580 | |
581 if (!mgHandleDirectory($mode, $basepath, $epath, $gallery, $edata, $newWriteMode, $startAt)) | |
582 return FALSE; | |
583 } | |
584 | |
16
3491ab93630e
Add sub-gallery thumbnails to gallery cache data.
Matti Hamalainen <ccr@tnsp.org>
parents:
15
diff
changeset
|
585 // Finish update modes |
3491ab93630e
Add sub-gallery thumbnails to gallery cache data.
Matti Hamalainen <ccr@tnsp.org>
parents:
15
diff
changeset
|
586 if ($mode == GCMD_UPDATE || $mode == GCMD_RESCAN) |
3491ab93630e
Add sub-gallery thumbnails to gallery cache data.
Matti Hamalainen <ccr@tnsp.org>
parents:
15
diff
changeset
|
587 { |
3491ab93630e
Add sub-gallery thumbnails to gallery cache data.
Matti Hamalainen <ccr@tnsp.org>
parents:
15
diff
changeset
|
588 // Store gallery cache for this directory |
3491ab93630e
Add sub-gallery thumbnails to gallery cache data.
Matti Hamalainen <ccr@tnsp.org>
parents:
15
diff
changeset
|
589 if ($writeMode && !mgWriteGalleryCache($cacheFilename, $gallery, $entries, $parentEntry)) |
3491ab93630e
Add sub-gallery thumbnails to gallery cache data.
Matti Hamalainen <ccr@tnsp.org>
parents:
15
diff
changeset
|
590 return FALSE; |
3491ab93630e
Add sub-gallery thumbnails to gallery cache data.
Matti Hamalainen <ccr@tnsp.org>
parents:
15
diff
changeset
|
591 } |
3491ab93630e
Add sub-gallery thumbnails to gallery cache data.
Matti Hamalainen <ccr@tnsp.org>
parents:
15
diff
changeset
|
592 |
0 | 593 mgCheckQuit(TRUE); |
594 | |
595 return TRUE; | |
596 } | |
597 | |
598 | |
599 function mgSigHandler($signo) | |
600 { | |
601 global $flagQuit; | |
602 switch ($signo) | |
603 { | |
604 case SIGTERM: | |
605 mgFatal("Received SIGTERM.\n"); | |
606 break; | |
607 | |
608 case SIGQUIT: | |
609 case SIGINT: | |
610 $flagQuit = TRUE; | |
611 break; | |
612 | |
613 } | |
614 } | |
615 | |
616 | |
617 function mgProcessGalleries($cmd, $path) | |
618 { | |
619 global $galTNPath; | |
620 | |
621 // Fetch the settings we need | |
622 if (mgReadSettings() === FALSE) | |
623 die("MGallery not configured.\n"); | |
624 | |
625 // Check validity of some settings | |
626 $galPath = mgGetSetting("base_path"); | |
627 $galTNPath = mgCleanPath(TRUE, mgGetSetting("tn_path")); | |
628 if ($galTNPath != mgGetSetting("tn_path")) | |
629 mgError("Invalid tn_path, using '".$galTNPath."'.\n"); | |
630 | |
631 if (strpos($galTNPath, "/") !== FALSE || $galTNPath == "") | |
632 mgFatal("Invalid tn_path '".$galTNPath."'.\n"); | |
633 | |
634 $parentData = $parentEntry = NULL; | |
635 $writeMode = TRUE; | |
636 $startAt = NULL; | |
637 | |
638 // Check for path argument | |
639 if ($path !== FALSE) | |
640 { | |
641 // Check the given path, needs to be "under" the gallery path | |
642 $cmp = mgCleanPath(TRUE, mgRealPath($galPath))."/"; | |
643 $tmp = mgCleanPath(TRUE, mgRealPath($path))."/"; | |
644 if (substr($tmp, 0, strlen($cmp)) != $cmp) | |
645 mgFatal("Path '".$path."' ('".$tmp."') does not reside under '".$galPath."' ('".$cmp."')!\n"); | |
646 | |
647 // Check if we need to bootstrap | |
648 if ($cmp != $tmp) | |
649 { | |
650 $bpath = mgCleanPath(TRUE, mgRealPath($tmp."../")); | |
651 | |
652 if ($cmd != GCMD_CLEAN) | |
653 { | |
654 $cacheFile = mgGetPath($bpath, "cache_file"); | |
655 if (!file_exists($cacheFile)) | |
656 mgFatal("Can't start working from '".$path."', parent '".$cacheFile."' does not exist!\n"); | |
657 | |
658 @include($cacheFile); | |
659 if (!isset($galEntries) || !isset($galData)) | |
660 mgFatal("Cache file '".$cacheFile."' is broken or stale.\n"); | |
661 } | |
662 | |
663 $writeMode = FALSE; | |
664 $startAt = $tmp; | |
665 $path = $bpath; | |
666 | |
667 echo "Starting: '".$startAt."' inside '".$path."'.\n"; | |
668 } | |
669 else | |
670 $path = $tmp; | |
671 } | |
672 else | |
673 $path = $galPath; | |
674 | |
675 // Start working | |
676 echo "Gallery path: '".$galPath."', starting at '".$path."' ...\n"; | |
677 mgHandleDirectory($cmd, $galPath, $path, $parentData, $parentEntry, $writeMode, $startAt); | |
678 } | |
679 | |
680 | |
681 function mgShowCopyright() | |
682 { | |
683 global $mgProgVersion, $mgProgCopyright, | |
684 $mgProgInfo, $mgProgEmail; | |
685 | |
686 echo | |
687 "MGTool ".$mgProgVersion." - MGallery management tool\n". | |
688 $mgProgInfo." ".$mgProgEmail."\n". | |
689 "(C) Copyright ".$mgProgCopyright."\n"; | |
690 } | |
691 | |
692 | |
693 function mgShowHelp() | |
694 { | |
695 global $argv; | |
696 echo | |
697 "Usage: ".basename($argv[0])." <command> [arguments]\n". | |
698 "\n". | |
699 " --help - Show this help.\n". | |
700 " --version - Show version information.\n". | |
701 "\n". | |
702 " update [path]\n". | |
703 " Update directories under <path> or all gallery dirs.\n". | |
704 " Conditionally scans dirs for new or changed images and\n". | |
705 " updates cache files as needed.\n". | |
706 "\n". | |
707 " rescan [path]\n". | |
708 " Like 'update', but forces all cache files to be regenerated\n". | |
709 " and EXIF/caption/etc information to be re-scanned even\n". | |
710 " if the file timestamps do not justify re-scanning.\n". | |
711 "\n". | |
712 " clean <all|caches|thumbnails> [path]\n". | |
713 " Delete all generated files or selectively cache files or\n". | |
714 " everything inside thumbnail directories.\n". | |
715 "\n". | |
716 " config\n". | |
717 " Display configuration values.\n". | |
718 "\n"; | |
719 } | |
720 | |
721 | |
722 // | |
723 // Main code starts | |
724 // | |
725 if (php_sapi_name() != "cli" || !empty($_SERVER["REMOTE_ADDR"])) | |
726 { | |
727 header("Status: 404 Not Found"); | |
728 die(); | |
729 } | |
730 | |
731 pcntl_signal(SIGTERM, "mgSigHandler"); | |
732 pcntl_signal(SIGHUP, "mgSigHandler"); | |
733 pcntl_signal(SIGQUIT, "mgSigHandler"); | |
734 pcntl_signal(SIGINT, "mgSigHandler"); | |
735 | |
736 | |
737 $cmd = mgCArgLC(1); | |
738 switch ($cmd) | |
739 { | |
740 case "--version": | |
741 case "version": | |
742 case "ver": | |
743 mgShowCopyright(); | |
744 break; | |
745 | |
746 case FALSE: | |
747 // No arguments | |
748 mgError("Nothing to do. Showing help:\n"); | |
749 | |
750 case "--help": | |
751 case "help": | |
752 if ($cmd !== FALSE) | |
753 mgShowCopyright(); | |
754 mgShowHelp(); | |
755 break; | |
756 | |
757 case "update": case "up": case "upd": case "upda": | |
758 mgProcessGalleries(GCMD_UPDATE, mgCArg(2)); | |
759 break; | |
760 | |
761 case "rescan": case "re": case "res": | |
762 mgProcessGalleries(GCMD_RESCAN, mgCArg(2)); | |
763 break; | |
764 | |
765 case "clean": case "cl": case "cle": | |
766 $cmode = mgCArgLC(2, 2); | |
767 switch ($cmode) | |
768 { | |
769 case "al": $galCleanFlags = GCLEAN_ALL; break; | |
770 case "ca": $galCleanFlags = GCLEAN_CACHES; break; | |
771 case "th": $galCleanFlags = GCLEAN_THUMBNAILS; break; | |
772 case FALSE: | |
773 mgFatal("Cleaning requires a mode argument.\n"); | |
774 | |
775 default: | |
776 mgFatal("Invalid clean mode '".mgCArg(2)."'.\n"); | |
777 } | |
778 $flagDoDelete = FALSE; | |
779 mgProcessGalleries(GCMD_CLEAN, mgCArg(3)); | |
780 echo "--\n"; | |
781 if (mgYesNoPrompt("Really delete the above files and directories?")) | |
782 { | |
25
c698618e6f67
Actually implement deletion.
Matti Hamalainen <ccr@tnsp.org>
parents:
16
diff
changeset
|
783 echo "Deleting ...\n"; |
c698618e6f67
Actually implement deletion.
Matti Hamalainen <ccr@tnsp.org>
parents:
16
diff
changeset
|
784 $flagDoDelete = TRUE; |
c698618e6f67
Actually implement deletion.
Matti Hamalainen <ccr@tnsp.org>
parents:
16
diff
changeset
|
785 mgProcessGalleries(GCMD_CLEAN, mgCArg(3)); |
c698618e6f67
Actually implement deletion.
Matti Hamalainen <ccr@tnsp.org>
parents:
16
diff
changeset
|
786 } |
c698618e6f67
Actually implement deletion.
Matti Hamalainen <ccr@tnsp.org>
parents:
16
diff
changeset
|
787 else |
c698618e6f67
Actually implement deletion.
Matti Hamalainen <ccr@tnsp.org>
parents:
16
diff
changeset
|
788 { |
c698618e6f67
Actually implement deletion.
Matti Hamalainen <ccr@tnsp.org>
parents:
16
diff
changeset
|
789 echo "Okay, canceling operation.\n"; |
0 | 790 } |
791 break; | |
792 | |
793 case "config": | |
794 case "dump": | |
795 if (mgReadSettings() === FALSE) | |
796 die("MGallery not configured.\n"); | |
797 | |
798 foreach ($mgDefaults as $key => $dval) | |
799 { | |
800 $sval = mgGetSetting($key); | |
26 | 801 if ($cmd == "dump") |
802 { | |
803 printf("%-20s = %s\n", $key, mgGetDValStr($dval[0], $sval)); | |
804 } | |
805 else | |
806 { | |
807 printf("%-20s = %s%s\n", | |
808 $key, | |
809 mgGetDValStr($dval[0], $sval), | |
810 ($dval[1] !== NULL && $sval !== $dval[1]) ? " (default: ".mgGetDValStr($dval[0], $dval[1]).")" : ""); | |
811 } | |
0 | 812 } |
813 break; | |
814 | |
815 default: | |
816 mgError("Unknown option/command '".$cmd."'.\n"); | |
817 break; | |
818 } | |
819 | |
820 ?> |