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