Mercurial > hg > mgallery
annotate mgtool.php @ 166:d8387b48b08d
Improve yes/no prompt check.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Thu, 01 Mar 2018 14:01:49 +0200 |
parents | da70f1c05ebe |
children | c0439644da6a |
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> |
161 | 7 // (C) Copyright 2015-2018 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 | |
165 | 250 // |
251 // Parse a simple image captions file, with format of one entry per line. | |
252 // Lines starting with "# " are comments (note the whitespace), empty lines ignored. | |
253 // First continuous non-whitespace text is file/dirname, followed | |
254 // by caption text, which is separate with whitespace from the filename. | |
255 // Filenames starting with # will be made hidden entries. | |
256 // Filenames starging with % will hide contents. | |
257 // | |
0 | 258 function mgReadCaptionsFile($galBasePath, $galPath) |
259 { | |
60
ef9f17280372
Use modern PHP array definition in some places.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
260 $captions = []; |
0 | 261 $filename = mgGetPath($galPath, "captions_file"); |
262 if ($filename === FALSE || ($fp = @fopen($filename, "rb")) === FALSE) | |
263 return $captions; | |
264 | |
265 mgDebug("Reading CAPTIONS: ".$filename."\n"); | |
266 | |
267 // Read and parse data | |
268 while (!feof($fp)) | |
269 { | |
270 $str = trim(fgets($fp)); | |
271 // Ignore comments and empty lines | |
272 if ($str != "#" && $str != "") | |
273 { | |
11
0b097770061d
Improve content hiding functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
274 if (preg_match("/^([#%]?)\s*(\S+?)\s+(.+)$/", $str, $m)) |
45 | 275 $captions[$m[2]] = ["caption" => $m[3], "hide" => ($m[1] == "#"), "hide_contents" => ($m[1] == "%"), "used" => FALSE]; |
0 | 276 else |
11
0b097770061d
Improve content hiding functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
277 if (preg_match("/^([#%]?)\s*(\S+?)$/", $str, $m)) |
45 | 278 $captions[$m[2]] = ["hide" => ($m[1] == "#"), "hide_contents" => ($m[1] == "%"), "used" => FALSE]; |
0 | 279 } |
280 } | |
281 | |
282 fclose($fp); | |
283 return $captions; | |
284 } | |
285 | |
286 | |
165 | 287 // |
288 // Create directory with specified permissions | |
289 // | |
0 | 290 function mgMakeDir($path, $perm) |
291 { | |
292 if (!file_exists($path)) | |
293 { | |
294 if (mkdir($path, $perm, TRUE) === false) | |
295 return mgError("Could not create directory '".$path."'\n"); | |
296 } | |
297 return TRUE; | |
298 } | |
299 | |
300 | |
165 | 301 // |
302 // Print a simple yes/no prompt with given message | |
303 // and default value. | |
304 // | |
0 | 305 function mgYesNoPrompt($msg, $default = FALSE) |
306 { | |
307 echo $msg." [".($default ? "Y/n" : "y/N")."]? "; | |
308 $sprompt = strtolower(trim(fgets(STDIN))); | |
309 | |
166
d8387b48b08d
Improve yes/no prompt check.
Matti Hamalainen <ccr@tnsp.org>
parents:
165
diff
changeset
|
310 if ($sprompt == "") |
d8387b48b08d
Improve yes/no prompt check.
Matti Hamalainen <ccr@tnsp.org>
parents:
165
diff
changeset
|
311 return $default; |
0 | 312 else |
165 | 313 return $sprompt[0] == 'y'; |
0 | 314 } |
315 | |
316 | |
165 | 317 // |
318 // Delete given directory, with optional recursiveness. | |
319 // | |
0 | 320 function mgDelete($path, $recurse) |
321 { | |
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
|
322 global $flagDoDelete, $countDoDelete; |
0 | 323 if (is_dir($path)) |
324 { | |
325 if (($dirHandle = @opendir($path)) === FALSE) | |
326 return mgError("Could not read directory '".$path."'.\n"); | |
327 | |
328 while (($dirFile = @readdir($dirHandle)) !== FALSE) | |
329 { | |
330 if ($dirFile != "." && $dirFile != "..") | |
331 mgDelete($path."/".$dirFile, $recurse); | |
332 } | |
333 closedir($dirHandle); | |
334 | |
335 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
|
336 $countDoDelete++; |
0 | 337 if ($flagDoDelete) |
338 rmdir($path); | |
339 } | |
340 else | |
54
624c50e1b52d
Fix 'mgtool clean' to not attempt deleting files that do not exist.
Matti Hamalainen <ccr@tnsp.org>
parents:
53
diff
changeset
|
341 if (file_exists($path)) |
0 | 342 { |
343 if (!$recurse) | |
344 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
|
345 |
62
3f60a5f5b4b3
Actually fix checking for files to be deleted to work.
Matti Hamalainen <ccr@tnsp.org>
parents:
61
diff
changeset
|
346 $countDoDelete++; |
0 | 347 if ($flagDoDelete) |
348 unlink($path); | |
349 } | |
350 } | |
351 | |
352 | |
165 | 353 // |
354 // Check if we have received the quit signal | |
355 // and if yes, quit cleanly now. | |
356 // | |
0 | 357 function mgCheckQuit($now = FALSE) |
358 { | |
359 global $flagQuit; | |
360 | |
361 // Dispatch pending signals | |
362 pcntl_signal_dispatch(); | |
363 | |
364 // Check result | |
365 if ($now && $flagQuit) | |
366 mgFatal("Quitting.\n"); | |
367 | |
368 return $flagQuit; | |
369 } | |
370 | |
371 | |
372 function mgNeedUpdate($entry, $field, $cvalue) | |
373 { | |
374 if (!array_key_exists($field, $entry)) | |
375 return TRUE; | |
80 | 376 |
0 | 377 return ($entry[$field] < $cvalue); |
378 } | |
379 | |
380 | |
30
f2c21a8b9071
Sort images in a folder by their capture timestamp.
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
381 function mgSortFunc($a, $b) |
f2c21a8b9071
Sort images in a folder by their capture timestamp.
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
382 { |
f2c21a8b9071
Sort images in a folder by their capture timestamp.
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
383 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
|
384 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
|
385 else |
108
417cdd9f8864
Use image file base name for sorting if timestamps are not available.
Matti Hamalainen <ccr@tnsp.org>
parents:
80
diff
changeset
|
386 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
|
387 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
|
388 else |
30
f2c21a8b9071
Sort images in a folder by their capture timestamp.
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
389 return 0; |
f2c21a8b9071
Sort images in a folder by their capture timestamp.
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
390 } |
f2c21a8b9071
Sort images in a folder by their capture timestamp.
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
391 |
f2c21a8b9071
Sort images in a folder by their capture timestamp.
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
392 |
16
3491ab93630e
Add sub-gallery thumbnails to gallery cache data.
Matti Hamalainen <ccr@tnsp.org>
parents:
15
diff
changeset
|
393 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
|
394 { |
805f93962cf9
Factorize gallery cache file writing to a separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
395 // 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
|
396 $images = []; |
ef9f17280372
Use modern PHP array definition in some places.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
397 $albums = []; |
61
ee82f7c8d44c
Do not include hidden entries in the entry list.
Matti Hamalainen <ccr@tnsp.org>
parents:
60
diff
changeset
|
398 $output = []; |
60
ef9f17280372
Use modern PHP array definition in some places.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
399 |
64
9a01e14b27ef
Implement hide_contents setting for galleries, which hides contents of an album.
Matti Hamalainen <ccr@tnsp.org>
parents:
63
diff
changeset
|
400 // 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
|
401 if (!$parentEntry["hide_contents"]) |
15
805f93962cf9
Factorize gallery cache file writing to a separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
402 { |
64
9a01e14b27ef
Implement hide_contents setting for galleries, which hides contents of an album.
Matti Hamalainen <ccr@tnsp.org>
parents:
63
diff
changeset
|
403 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
|
404 { |
9a01e14b27ef
Implement hide_contents setting for galleries, which hides contents of an album.
Matti Hamalainen <ccr@tnsp.org>
parents:
63
diff
changeset
|
405 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
|
406 continue; |
15
805f93962cf9
Factorize gallery cache file writing to a separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
407 |
64
9a01e14b27ef
Implement hide_contents setting for galleries, which hides contents of an album.
Matti Hamalainen <ccr@tnsp.org>
parents:
63
diff
changeset
|
408 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
|
409 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
|
410 $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
|
411 else |
9a01e14b27ef
Implement hide_contents setting for galleries, which hides contents of an album.
Matti Hamalainen <ccr@tnsp.org>
parents:
63
diff
changeset
|
412 $albums[$ename] = &$edata; |
61
ee82f7c8d44c
Do not include hidden entries in the entry list.
Matti Hamalainen <ccr@tnsp.org>
parents:
60
diff
changeset
|
413 |
64
9a01e14b27ef
Implement hide_contents setting for galleries, which hides contents of an album.
Matti Hamalainen <ccr@tnsp.org>
parents:
63
diff
changeset
|
414 $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
|
415 } |
30
f2c21a8b9071
Sort images in a folder by their capture timestamp.
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
416 |
64
9a01e14b27ef
Implement hide_contents setting for galleries, which hides contents of an album.
Matti Hamalainen <ccr@tnsp.org>
parents:
63
diff
changeset
|
417 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
|
418 krsort($albums); |
15
805f93962cf9
Factorize gallery cache file writing to a separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
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 // 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
|
421 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
|
422 { |
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
|
423 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
|
424 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
|
425 { |
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
|
426 $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
|
427 } |
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
|
428 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
|
429 { |
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
|
430 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
|
431 $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
|
432 } |
64
9a01e14b27ef
Implement hide_contents setting for galleries, which hides contents of an album.
Matti Hamalainen <ccr@tnsp.org>
parents:
63
diff
changeset
|
433 } |
9a01e14b27ef
Implement hide_contents setting for galleries, which hides contents of an album.
Matti Hamalainen <ccr@tnsp.org>
parents:
63
diff
changeset
|
434 else |
9a01e14b27ef
Implement hide_contents setting for galleries, which hides contents of an album.
Matti Hamalainen <ccr@tnsp.org>
parents:
63
diff
changeset
|
435 { |
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
|
436 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
|
437 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
|
438 { |
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
|
439 $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
|
440 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
|
441 } |
64
9a01e14b27ef
Implement hide_contents setting for galleries, which hides contents of an album.
Matti Hamalainen <ccr@tnsp.org>
parents:
63
diff
changeset
|
442 } |
35
985596db0f01
Implement recursive depth album covers.
Matti Hamalainen <ccr@tnsp.org>
parents:
32
diff
changeset
|
443 } |
16
3491ab93630e
Add sub-gallery thumbnails to gallery cache data.
Matti Hamalainen <ccr@tnsp.org>
parents:
15
diff
changeset
|
444 |
15
805f93962cf9
Factorize gallery cache file writing to a separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
445 $str = |
805f93962cf9
Factorize gallery cache file writing to a separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
446 "<?\n". |
805f93962cf9
Factorize gallery cache file writing to a separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
447 "\$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
|
448 "\$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
|
449 "\$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
|
450 "\$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
|
451 "?>"; |
805f93962cf9
Factorize gallery cache file writing to a separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
452 |
805f93962cf9
Factorize gallery cache file writing to a separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
453 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
|
454 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
|
455 |
805f93962cf9
Factorize gallery cache file writing to a separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
456 return TRUE; |
805f93962cf9
Factorize gallery cache file writing to a separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
457 } |
805f93962cf9
Factorize gallery cache file writing to a separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
458 |
16
3491ab93630e
Add sub-gallery thumbnails to gallery cache data.
Matti Hamalainen <ccr@tnsp.org>
parents:
15
diff
changeset
|
459 |
3491ab93630e
Add sub-gallery thumbnails to gallery cache data.
Matti Hamalainen <ccr@tnsp.org>
parents:
15
diff
changeset
|
460 function mgHandleDirectory($mode, $basepath, $path, &$parentData, &$parentEntry, $writeMode, $startAt) |
0 | 461 { |
462 global $galExifConversions, $galTNPath, $galCleanFlags; | |
463 | |
464 // Get cache file path | |
465 if (($cacheFilename = mgGetPath($path, "cache_file")) === FALSE) | |
466 return mgError("Cache filename / path not set.\n"); | |
467 | |
468 mgCheckQuit(TRUE); | |
469 | |
470 // Read directory contents | |
60
ef9f17280372
Use modern PHP array definition in some places.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
471 $entries = []; |
0 | 472 if (($dirHandle = @opendir($path)) === FALSE) |
473 return mgError("Could not read directory '".$path."'.\n"); | |
474 | |
475 while (($dirFile = @readdir($dirHandle)) !== FALSE) | |
476 { | |
477 $realFile = $path."/".$dirFile; | |
478 if (is_dir($realFile)) | |
479 { | |
480 if ($dirFile[0] != "." && $dirFile != $galTNPath) | |
63
1c32ece9acde
Some residual array() => [] conversions.
Matti Hamalainen <ccr@tnsp.org>
parents:
62
diff
changeset
|
481 $entries[$dirFile] = ["type" => 1, "base" => $dirFile, "ext" => "", "mtime" => filemtime($realFile)]; |
0 | 482 } |
483 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
|
484 if (preg_match("/^([^\/]+)(".mgGetSetting("format_exts").")$/i", $dirFile, $dirMatch)) |
63
1c32ece9acde
Some residual array() => [] conversions.
Matti Hamalainen <ccr@tnsp.org>
parents:
62
diff
changeset
|
485 $entries[$dirFile] = ["type" => 0, "base" => $dirMatch[1], "ext" => $dirMatch[2], "mtime" => filemtime($realFile), "hide" => false]; |
0 | 486 } |
487 closedir($dirHandle); | |
488 | |
489 mgCheckQuit(); | |
490 | |
491 // Cleanup mode | |
492 if ($mode == GCMD_CLEAN) | |
493 { | |
60
ef9f17280372
Use modern PHP array definition in some places.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
494 $gallery = []; |
0 | 495 |
496 if ($writeMode) | |
497 { | |
498 if ($galCleanFlags & GCLEAN_CACHES) | |
499 mgDelete($cacheFilename, FALSE); | |
500 | |
501 if ($galCleanFlags & GCLEAN_THUMBNAILS) | |
502 mgDelete($path."/".$galTNPath, TRUE); | |
503 } | |
504 } | |
505 else | |
506 // Update modes | |
507 if ($mode == GCMD_UPDATE || $mode == GCMD_RESCAN) | |
508 { | |
509 // 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
|
510 $galEntries = []; |
0 | 511 $cacheTime = -1; |
512 if ($mode == GCMD_UPDATE && file_exists($cacheFilename)) | |
513 { | |
514 $cacheTime = filemtime($cacheFilename); | |
515 @include $cacheFilename; | |
516 } | |
517 | |
518 // Read caption data | |
519 $captions = mgReadCaptionsFile($basepath, $path); | |
520 $gallery = mgGetAlbumData($basepath, $path); | |
521 if ($parentData !== NULL && $parentEntry !== NULL) | |
522 { | |
16
3491ab93630e
Add sub-gallery thumbnails to gallery cache data.
Matti Hamalainen <ccr@tnsp.org>
parents:
15
diff
changeset
|
523 $gallery["parent"] = &$parentData; |
0 | 524 mgCopyEntryData($gallery, $parentEntry, MG_STR, "caption", "caption"); |
525 } | |
526 | |
527 // Start actual processing | |
528 $nentries = count($entries); | |
529 $nentry = 0; | |
530 echo $path." .. "; | |
531 foreach ($entries as $ename => &$edata) | |
532 { | |
533 printf("\r%s (%1.1f%%) ..", $path, ($nentry * 100.0) / $nentries); | |
534 | |
535 $nentry++; | |
536 $efilename = $path."/".$ename; | |
537 | |
538 if (array_key_exists($ename, $galEntries)) | |
539 $galEntry = &$galEntries[$ename]; | |
540 else | |
63
1c32ece9acde
Some residual array() => [] conversions.
Matti Hamalainen <ccr@tnsp.org>
parents:
62
diff
changeset
|
541 $galEntry = []; |
0 | 542 |
57
29bb365e3ee2
Be more responsive when processing images, check for signals after each
Matti Hamalainen <ccr@tnsp.org>
parents:
56
diff
changeset
|
543 mgCheckQuit(TRUE); |
0 | 544 |
545 // Update with captions file data, if any | |
546 if (array_key_exists($ename, $captions)) | |
547 { | |
548 foreach ($captions[$ename] as $ckey => $cval) | |
549 $edata[$ckey] = $cval; | |
550 } | |
551 | |
552 // Handle entry based on type | |
553 if ($edata["type"] == 0) | |
554 { | |
555 $updFlags = 0; | |
556 $tnPath = $path."/".$galTNPath; | |
557 $medFilename = $tnPath."/".$edata["base"].mgGetSetting("med_suffix").$edata["ext"]; | |
558 $tnFilename = $tnPath."/".$ename; | |
559 $capFilename = $path."/".$edata["base"].".txt"; | |
560 | |
561 // Check what we need to update .. | |
562 if (!file_exists($medFilename) || filemtime($medFilename) < $edata["mtime"]) | |
563 $updFlags |= GUPD_MED_IMAGE; | |
564 | |
565 if (!file_exists($tnFilename) || filemtime($tnFilename) < $edata["mtime"]) | |
566 $updFlags |= GUPD_TN_IMAGE; | |
567 | |
568 if (mgNeedUpdate($galEntry, "mtime", $edata["mtime"])) | |
569 $updFlags |= GUPD_EXIF_INFO; | |
570 | |
571 if (file_exists($capFilename) && | |
572 mgNeedUpdate($galEntry, "mtime", filemtime($capFilename))) | |
573 $updFlags |= GUPD_CAPTION; | |
574 | |
575 // Check for EXIF info | |
576 if (($updFlags & GUPD_EXIF_INFO) && | |
577 ($exif = @exif_read_data($efilename)) !== FALSE) | |
578 { | |
579 echo "%"; | |
580 foreach ($galExifConversions as $conv) | |
581 mgCopyEntryData($edata, $exif, $conv[0], $conv[1], $conv[2]); | |
582 } | |
583 else | |
584 { | |
585 // Copy old data that is not yet in new | |
586 echo "*"; | |
587 foreach ($galEntry as $okey => $odata) | |
588 { | |
589 if (!array_key_exists($okey, $edata)) | |
590 $edata[$okey] = $odata; | |
591 } | |
592 } | |
593 | |
594 // Generate thumbnails, etc. | |
595 if ($updFlags & GUPD_IMAGES) | |
596 { | |
597 mgMakeDir($tnPath, 0755); | |
598 | |
599 if ($updFlags & GUPD_MED_IMAGE) | |
600 { | |
601 echo "1"; | |
602 mgConvertImage($efilename, $medFilename, | |
63
1c32ece9acde
Some residual array() => [] conversions.
Matti Hamalainen <ccr@tnsp.org>
parents:
62
diff
changeset
|
603 [mgGetSetting("med_width"), mgGetSetting("med_height")], |
0 | 604 "JPEG", mgGetSetting("med_quality"), TRUE); |
605 } | |
606 | |
607 if ($updFlags & GUPD_TN_IMAGE) | |
608 { | |
609 echo "2"; | |
610 mgConvertImage($efilename, $tnFilename, | |
63
1c32ece9acde
Some residual array() => [] conversions.
Matti Hamalainen <ccr@tnsp.org>
parents:
62
diff
changeset
|
611 [mgGetSetting("tn_width"), mgGetSetting("tn_height")], |
0 | 612 "JPEG", mgGetSetting("tn_quality"), TRUE); |
613 } | |
614 } | |
615 | |
616 // Check for .txt caption file | |
617 if ($updFlags & GUPD_CAPTION) | |
618 { | |
619 echo "?"; | |
620 if (($tmpData = @file_get_contents($capFilename)) !== FALSE) | |
621 $edata["caption"] = $tmpData; | |
622 } | |
623 } | |
624 else | |
625 if ($edata["type"] == 1) | |
626 { | |
10 | 627 // Set some of the album data here |
0 | 628 $tmp = mgGetAlbumData($basepath, $efilename); |
629 mgCopyEntryData($edata, $tmp, MG_STR, "caption", "caption"); | |
630 mgCopyEntryData($edata, $tmp, MG_STR, "caption", "fallback_caption"); | |
631 mgCopyEntryData($edata, $tmp, MG_STR, "caption", "title"); | |
632 mgCopyEntryData($edata, $tmp, MG_BOOL, "hide", "hide", FALSE); | |
11
0b097770061d
Improve content hiding functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
633 mgCopyEntryData($edata, $tmp, MG_BOOL, "hide_contents", "hide_contents", FALSE); |
0 | 634 } |
635 } | |
636 | |
637 echo "\r".$path." ..... DONE\n"; | |
638 | |
639 mgCheckQuit(TRUE); | |
640 } | |
641 else | |
642 mgFatal("Invalid work mode '".$mode."'.\n"); | |
643 | |
644 mgCheckQuit(TRUE); | |
645 | |
646 // 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
|
647 foreach ($entries as $ename => &$edata) |
0 | 648 if ($edata["type"] == 1) |
649 { | |
650 $epath = $path."/".$ename."/"; | |
651 $newWriteMode = ($writeMode === FALSE && $epath == $startAt) || $writeMode; | |
652 | |
653 if (!mgHandleDirectory($mode, $basepath, $epath, $gallery, $edata, $newWriteMode, $startAt)) | |
654 return FALSE; | |
655 } | |
656 | |
16
3491ab93630e
Add sub-gallery thumbnails to gallery cache data.
Matti Hamalainen <ccr@tnsp.org>
parents:
15
diff
changeset
|
657 // Finish update modes |
3491ab93630e
Add sub-gallery thumbnails to gallery cache data.
Matti Hamalainen <ccr@tnsp.org>
parents:
15
diff
changeset
|
658 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
|
659 { |
3491ab93630e
Add sub-gallery thumbnails to gallery cache data.
Matti Hamalainen <ccr@tnsp.org>
parents:
15
diff
changeset
|
660 // Store gallery cache for this directory |
3491ab93630e
Add sub-gallery thumbnails to gallery cache data.
Matti Hamalainen <ccr@tnsp.org>
parents:
15
diff
changeset
|
661 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
|
662 return FALSE; |
3491ab93630e
Add sub-gallery thumbnails to gallery cache data.
Matti Hamalainen <ccr@tnsp.org>
parents:
15
diff
changeset
|
663 } |
3491ab93630e
Add sub-gallery thumbnails to gallery cache data.
Matti Hamalainen <ccr@tnsp.org>
parents:
15
diff
changeset
|
664 |
0 | 665 mgCheckQuit(TRUE); |
666 | |
667 return TRUE; | |
668 } | |
669 | |
670 | |
671 function mgSigHandler($signo) | |
672 { | |
673 global $flagQuit; | |
674 switch ($signo) | |
675 { | |
676 case SIGTERM: | |
677 mgFatal("Received SIGTERM.\n"); | |
678 break; | |
679 | |
680 case SIGQUIT: | |
681 case SIGINT: | |
682 $flagQuit = TRUE; | |
683 break; | |
684 | |
685 } | |
686 } | |
687 | |
688 | |
689 function mgProcessGalleries($cmd, $path) | |
690 { | |
691 global $galTNPath; | |
692 | |
693 // Check validity of some settings | |
694 $galPath = mgGetSetting("base_path"); | |
695 $galTNPath = mgCleanPath(TRUE, mgGetSetting("tn_path")); | |
160
665b126cd578
Possibly better check for tn_path validity.
Matti Hamalainen <ccr@tnsp.org>
parents:
159
diff
changeset
|
696 if ($galTNPath."/" != mgGetSetting("tn_path")) |
665b126cd578
Possibly better check for tn_path validity.
Matti Hamalainen <ccr@tnsp.org>
parents:
159
diff
changeset
|
697 mgError("Invalid tn_path '".mgGetSetting("tn_path")."', using '".$galTNPath."'.\n"); |
0 | 698 |
699 if (strpos($galTNPath, "/") !== FALSE || $galTNPath == "") | |
700 mgFatal("Invalid tn_path '".$galTNPath."'.\n"); | |
701 | |
702 $parentData = $parentEntry = NULL; | |
703 $writeMode = TRUE; | |
704 $startAt = NULL; | |
705 | |
706 // Check for path argument | |
707 if ($path !== FALSE) | |
708 { | |
709 // Check the given path, needs to be "under" the gallery path | |
710 $cmp = mgCleanPath(TRUE, mgRealPath($galPath))."/"; | |
711 $tmp = mgCleanPath(TRUE, mgRealPath($path))."/"; | |
712 if (substr($tmp, 0, strlen($cmp)) != $cmp) | |
713 mgFatal("Path '".$path."' ('".$tmp."') does not reside under '".$galPath."' ('".$cmp."')!\n"); | |
714 | |
715 // Check if we need to bootstrap | |
716 if ($cmp != $tmp) | |
717 { | |
718 $bpath = mgCleanPath(TRUE, mgRealPath($tmp."../")); | |
719 | |
720 if ($cmd != GCMD_CLEAN) | |
721 { | |
722 $cacheFile = mgGetPath($bpath, "cache_file"); | |
723 if (!file_exists($cacheFile)) | |
724 mgFatal("Can't start working from '".$path."', parent '".$cacheFile."' does not exist!\n"); | |
725 | |
726 @include($cacheFile); | |
727 if (!isset($galEntries) || !isset($galData)) | |
728 mgFatal("Cache file '".$cacheFile."' is broken or stale.\n"); | |
729 } | |
730 | |
731 $writeMode = FALSE; | |
732 $startAt = $tmp; | |
733 $path = $bpath; | |
734 | |
735 echo "Starting: '".$startAt."' inside '".$path."'.\n"; | |
736 } | |
737 else | |
738 $path = $tmp; | |
739 } | |
740 else | |
741 $path = $galPath; | |
742 | |
743 // Start working | |
744 echo "Gallery path: '".$galPath."', starting at '".$path."' ...\n"; | |
745 mgHandleDirectory($cmd, $galPath, $path, $parentData, $parentEntry, $writeMode, $startAt); | |
746 } | |
747 | |
748 | |
749 function mgShowCopyright() | |
750 { | |
751 global $mgProgVersion, $mgProgCopyright, | |
752 $mgProgInfo, $mgProgEmail; | |
753 | |
754 echo | |
755 "MGTool ".$mgProgVersion." - MGallery management tool\n". | |
756 $mgProgInfo." ".$mgProgEmail."\n". | |
757 "(C) Copyright ".$mgProgCopyright."\n"; | |
758 } | |
759 | |
760 | |
761 function mgShowHelp() | |
762 { | |
763 global $argv; | |
764 echo | |
765 "Usage: ".basename($argv[0])." <command> [arguments]\n". | |
766 "\n". | |
767 " --help - Show this help.\n". | |
768 " --version - Show version information.\n". | |
769 "\n". | |
770 " update [path]\n". | |
771 " Update directories under <path> or all gallery dirs.\n". | |
772 " Conditionally scans dirs for new or changed images and\n". | |
773 " updates cache files as needed.\n". | |
774 "\n". | |
775 " rescan [path]\n". | |
776 " Like 'update', but forces all cache files to be regenerated\n". | |
777 " and EXIF/caption/etc information to be re-scanned even\n". | |
778 " if the file timestamps do not justify re-scanning.\n". | |
779 "\n". | |
780 " clean <all|caches|thumbnails> [path]\n". | |
781 " Delete all generated files or selectively cache files or\n". | |
782 " everything inside thumbnail directories.\n". | |
783 "\n". | |
159
b010c763a1fc
Document mgtool 'dump' command.
Matti Hamalainen <ccr@tnsp.org>
parents:
130
diff
changeset
|
784 " config|dump\n". |
b010c763a1fc
Document mgtool 'dump' command.
Matti Hamalainen <ccr@tnsp.org>
parents:
130
diff
changeset
|
785 " Display configuration values (with extra information) or\n". |
b010c763a1fc
Document mgtool 'dump' command.
Matti Hamalainen <ccr@tnsp.org>
parents:
130
diff
changeset
|
786 " \"dump\" current configuration as is.\n". |
0 | 787 "\n"; |
788 } | |
789 | |
790 | |
791 // | |
792 // Main code starts | |
793 // | |
794 if (php_sapi_name() != "cli" || !empty($_SERVER["REMOTE_ADDR"])) | |
795 { | |
796 header("Status: 404 Not Found"); | |
797 die(); | |
798 } | |
799 | |
800 pcntl_signal(SIGTERM, "mgSigHandler"); | |
801 pcntl_signal(SIGHUP, "mgSigHandler"); | |
802 pcntl_signal(SIGQUIT, "mgSigHandler"); | |
803 pcntl_signal(SIGINT, "mgSigHandler"); | |
804 | |
130
f825d644cf7a
Implement new config setting "timezone" for specifying the timezone used for date/timestamp interpretation.
Matti Hamalainen <ccr@tnsp.org>
parents:
124
diff
changeset
|
805 if (mgReadSettings() === FALSE) |
f825d644cf7a
Implement new config setting "timezone" for specifying the timezone used for date/timestamp interpretation.
Matti Hamalainen <ccr@tnsp.org>
parents:
124
diff
changeset
|
806 die("MGallery is not configured, failed to find a configuration file.\n"); |
f825d644cf7a
Implement new config setting "timezone" for specifying the timezone used for date/timestamp interpretation.
Matti Hamalainen <ccr@tnsp.org>
parents:
124
diff
changeset
|
807 |
f825d644cf7a
Implement new config setting "timezone" for specifying the timezone used for date/timestamp interpretation.
Matti Hamalainen <ccr@tnsp.org>
parents:
124
diff
changeset
|
808 if (($pageTimeZone = mgGetSetting("timezone")) !== NULL) |
f825d644cf7a
Implement new config setting "timezone" for specifying the timezone used for date/timestamp interpretation.
Matti Hamalainen <ccr@tnsp.org>
parents:
124
diff
changeset
|
809 date_default_timezone_set($pageTimeZone); |
0 | 810 |
811 $cmd = mgCArgLC(1); | |
812 switch ($cmd) | |
813 { | |
814 case "--version": | |
815 case "version": | |
816 case "ver": | |
817 mgShowCopyright(); | |
818 break; | |
819 | |
820 case FALSE: | |
821 // No arguments | |
822 mgError("Nothing to do. Showing help:\n"); | |
823 | |
824 case "--help": | |
825 case "help": | |
826 if ($cmd !== FALSE) | |
827 mgShowCopyright(); | |
828 mgShowHelp(); | |
829 break; | |
830 | |
831 case "update": case "up": case "upd": case "upda": | |
832 mgProcessGalleries(GCMD_UPDATE, mgCArg(2)); | |
833 break; | |
834 | |
835 case "rescan": case "re": case "res": | |
836 mgProcessGalleries(GCMD_RESCAN, mgCArg(2)); | |
837 break; | |
838 | |
839 case "clean": case "cl": case "cle": | |
840 $cmode = mgCArgLC(2, 2); | |
841 switch ($cmode) | |
842 { | |
843 case "al": $galCleanFlags = GCLEAN_ALL; break; | |
844 case "ca": $galCleanFlags = GCLEAN_CACHES; break; | |
845 case "th": $galCleanFlags = GCLEAN_THUMBNAILS; break; | |
846 case FALSE: | |
847 mgFatal("Cleaning requires a mode argument.\n"); | |
848 | |
849 default: | |
850 mgFatal("Invalid clean mode '".mgCArg(2)."'.\n"); | |
851 } | |
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
|
852 $countDoDelete = 0; |
0 | 853 $flagDoDelete = FALSE; |
854 mgProcessGalleries(GCMD_CLEAN, mgCArg(3)); | |
855 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
|
856 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
|
857 { |
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
|
858 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
|
859 } |
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
|
860 else |
0 | 861 if (mgYesNoPrompt("Really delete the above files and directories?")) |
862 { | |
25
c698618e6f67
Actually implement deletion.
Matti Hamalainen <ccr@tnsp.org>
parents:
16
diff
changeset
|
863 echo "Deleting ...\n"; |
c698618e6f67
Actually implement deletion.
Matti Hamalainen <ccr@tnsp.org>
parents:
16
diff
changeset
|
864 $flagDoDelete = TRUE; |
c698618e6f67
Actually implement deletion.
Matti Hamalainen <ccr@tnsp.org>
parents:
16
diff
changeset
|
865 mgProcessGalleries(GCMD_CLEAN, mgCArg(3)); |
c698618e6f67
Actually implement deletion.
Matti Hamalainen <ccr@tnsp.org>
parents:
16
diff
changeset
|
866 } |
c698618e6f67
Actually implement deletion.
Matti Hamalainen <ccr@tnsp.org>
parents:
16
diff
changeset
|
867 else |
c698618e6f67
Actually implement deletion.
Matti Hamalainen <ccr@tnsp.org>
parents:
16
diff
changeset
|
868 { |
c698618e6f67
Actually implement deletion.
Matti Hamalainen <ccr@tnsp.org>
parents:
16
diff
changeset
|
869 echo "Okay, canceling operation.\n"; |
0 | 870 } |
871 break; | |
872 | |
873 case "config": | |
874 case "dump": | |
875 | |
876 foreach ($mgDefaults as $key => $dval) | |
877 { | |
878 $sval = mgGetSetting($key); | |
26 | 879 if ($cmd == "dump") |
880 { | |
114
c803235bd0f9
Implement flag config option type.
Matti Hamalainen <ccr@tnsp.org>
parents:
111
diff
changeset
|
881 printf("%-20s = %s\n", $key, mgGetDValStr($dval, $sval)); |
26 | 882 } |
883 else | |
884 { | |
885 printf("%-20s = %s%s\n", | |
886 $key, | |
114
c803235bd0f9
Implement flag config option type.
Matti Hamalainen <ccr@tnsp.org>
parents:
111
diff
changeset
|
887 mgGetDValStr($dval, $sval), |
c803235bd0f9
Implement flag config option type.
Matti Hamalainen <ccr@tnsp.org>
parents:
111
diff
changeset
|
888 ($dval[1] !== NULL && $sval !== $dval[1]) ? " (default: ".mgGetDValStr($dval, $dval[1]).")" : ""); |
26 | 889 } |
0 | 890 } |
891 break; | |
892 | |
893 default: | |
894 mgError("Unknown option/command '".$cmd."'.\n"); | |
895 break; | |
896 } | |
897 | |
898 ?> |