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