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