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