Mercurial > hg > mgallery
annotate mgtool.php @ 60:ef9f17280372
Use modern PHP array definition in some places.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Mon, 12 Sep 2016 23:57:53 +0300 |
parents | 29bb365e3ee2 |
children | ee82f7c8d44c |
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"; | |
314 if ($flagDoDelete) | |
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
|
315 { |
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
|
316 $countDoDelete++; |
0 | 317 rmdir($path); |
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
|
318 } |
0 | 319 } |
320 else | |
54
624c50e1b52d
Fix 'mgtool clean' to not attempt deleting files that do not exist.
Matti Hamalainen <ccr@tnsp.org>
parents:
53
diff
changeset
|
321 if (file_exists($path)) |
0 | 322 { |
323 if (!$recurse) | |
324 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
|
325 |
0 | 326 if ($flagDoDelete) |
56
7384504d7b52
Do not prompt for deletion in cleanup if there are no files/directories to delete.
Matti Hamalainen <ccr@tnsp.org>
parents:
55
diff
changeset
|
327 { |
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
|
328 $countDoDelete++; |
0 | 329 unlink($path); |
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
|
330 } |
0 | 331 } |
332 } | |
333 | |
334 | |
335 function mgCheckQuit($now = FALSE) | |
336 { | |
337 global $flagQuit; | |
338 | |
339 // Dispatch pending signals | |
340 pcntl_signal_dispatch(); | |
341 | |
342 // Check result | |
343 if ($now && $flagQuit) | |
344 mgFatal("Quitting.\n"); | |
345 | |
346 return $flagQuit; | |
347 } | |
348 | |
349 | |
350 function mgNeedUpdate($entry, $field, $cvalue) | |
351 { | |
352 if (!array_key_exists($field, $entry)) | |
353 return TRUE; | |
354 | |
355 return ($entry[$field] < $cvalue); | |
356 } | |
357 | |
358 | |
30
f2c21a8b9071
Sort images in a folder by their capture timestamp.
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
359 function mgSortFunc($a, $b) |
f2c21a8b9071
Sort images in a folder by their capture timestamp.
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
360 { |
f2c21a8b9071
Sort images in a folder by their capture timestamp.
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
361 if (isset($a["datetime"]) && isset($b["datetime"])) |
f2c21a8b9071
Sort images in a folder by their capture timestamp.
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
362 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
|
363 else |
f2c21a8b9071
Sort images in a folder by their capture timestamp.
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
364 return 0; |
f2c21a8b9071
Sort images in a folder by their capture timestamp.
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
365 } |
f2c21a8b9071
Sort images in a folder by their capture timestamp.
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
366 |
f2c21a8b9071
Sort images in a folder by their capture timestamp.
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
367 |
16
3491ab93630e
Add sub-gallery thumbnails to gallery cache data.
Matti Hamalainen <ccr@tnsp.org>
parents:
15
diff
changeset
|
368 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
|
369 { |
805f93962cf9
Factorize gallery cache file writing to a separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
370 // 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
|
371 $images = []; |
ef9f17280372
Use modern PHP array definition in some places.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
372 $albums = []; |
ef9f17280372
Use modern PHP array definition in some places.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
373 |
15
805f93962cf9
Factorize gallery cache file writing to a separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
374 foreach ($entries as $ename => &$edata) |
805f93962cf9
Factorize gallery cache file writing to a separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
375 { |
805f93962cf9
Factorize gallery cache file writing to a separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
376 if ($edata["hide"]) |
805f93962cf9
Factorize gallery cache file writing to a separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
377 continue; |
805f93962cf9
Factorize gallery cache file writing to a separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
378 |
805f93962cf9
Factorize gallery cache file writing to a separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
379 unset($edata["hide"]); |
805f93962cf9
Factorize gallery cache file writing to a separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
380 if ($edata["type"] == 0) |
805f93962cf9
Factorize gallery cache file writing to a separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
381 $images[$ename] = &$edata; |
805f93962cf9
Factorize gallery cache file writing to a separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
382 else |
805f93962cf9
Factorize gallery cache file writing to a separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
383 $albums[$ename] = &$edata; |
805f93962cf9
Factorize gallery cache file writing to a separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
384 } |
30
f2c21a8b9071
Sort images in a folder by their capture timestamp.
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
385 |
f2c21a8b9071
Sort images in a folder by their capture timestamp.
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
386 uasort($images, "mgSortFunc"); |
37
99dc0843d7df
Fix sorting of albums to match image sorting.
Matti Hamalainen <ccr@tnsp.org>
parents:
35
diff
changeset
|
387 krsort($albums); |
15
805f93962cf9
Factorize gallery cache file writing to a separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
388 |
16
3491ab93630e
Add sub-gallery thumbnails to gallery cache data.
Matti Hamalainen <ccr@tnsp.org>
parents:
15
diff
changeset
|
389 // Choose gallery album image |
3491ab93630e
Add sub-gallery thumbnails to gallery cache data.
Matti Hamalainen <ccr@tnsp.org>
parents:
15
diff
changeset
|
390 if (count($images) > 0) |
3491ab93630e
Add sub-gallery thumbnails to gallery cache data.
Matti Hamalainen <ccr@tnsp.org>
parents:
15
diff
changeset
|
391 { |
3491ab93630e
Add sub-gallery thumbnails to gallery cache data.
Matti Hamalainen <ccr@tnsp.org>
parents:
15
diff
changeset
|
392 end($images); |
3491ab93630e
Add sub-gallery thumbnails to gallery cache data.
Matti Hamalainen <ccr@tnsp.org>
parents:
15
diff
changeset
|
393 $parentEntry["image"] = key($images); |
3491ab93630e
Add sub-gallery thumbnails to gallery cache data.
Matti Hamalainen <ccr@tnsp.org>
parents:
15
diff
changeset
|
394 } |
35
985596db0f01
Implement recursive depth album covers.
Matti Hamalainen <ccr@tnsp.org>
parents:
32
diff
changeset
|
395 else |
985596db0f01
Implement recursive depth album covers.
Matti Hamalainen <ccr@tnsp.org>
parents:
32
diff
changeset
|
396 foreach ($albums as $aid => &$adata) |
985596db0f01
Implement recursive depth album covers.
Matti Hamalainen <ccr@tnsp.org>
parents:
32
diff
changeset
|
397 if (isset($adata["image"])) |
985596db0f01
Implement recursive depth album covers.
Matti Hamalainen <ccr@tnsp.org>
parents:
32
diff
changeset
|
398 { |
985596db0f01
Implement recursive depth album covers.
Matti Hamalainen <ccr@tnsp.org>
parents:
32
diff
changeset
|
399 $parentEntry["image"] = &$adata; |
985596db0f01
Implement recursive depth album covers.
Matti Hamalainen <ccr@tnsp.org>
parents:
32
diff
changeset
|
400 break; |
985596db0f01
Implement recursive depth album covers.
Matti Hamalainen <ccr@tnsp.org>
parents:
32
diff
changeset
|
401 } |
16
3491ab93630e
Add sub-gallery thumbnails to gallery cache data.
Matti Hamalainen <ccr@tnsp.org>
parents:
15
diff
changeset
|
402 |
15
805f93962cf9
Factorize gallery cache file writing to a separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
403 $str = |
805f93962cf9
Factorize gallery cache file writing to a separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
404 "<?\n". |
805f93962cf9
Factorize gallery cache file writing to a separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
405 "\$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
|
406 "\$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
|
407 "\$galImagesIndex = ".var_export(array_keys($images), TRUE).";\n". |
805f93962cf9
Factorize gallery cache file writing to a separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
408 "\$galEntries = ".var_export($entries, TRUE).";\n". |
805f93962cf9
Factorize gallery cache file writing to a separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
409 "?>"; |
805f93962cf9
Factorize gallery cache file writing to a separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
410 |
805f93962cf9
Factorize gallery cache file writing to a separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
411 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
|
412 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
|
413 |
805f93962cf9
Factorize gallery cache file writing to a separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
414 return TRUE; |
805f93962cf9
Factorize gallery cache file writing to a separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
415 } |
805f93962cf9
Factorize gallery cache file writing to a separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
416 |
16
3491ab93630e
Add sub-gallery thumbnails to gallery cache data.
Matti Hamalainen <ccr@tnsp.org>
parents:
15
diff
changeset
|
417 |
3491ab93630e
Add sub-gallery thumbnails to gallery cache data.
Matti Hamalainen <ccr@tnsp.org>
parents:
15
diff
changeset
|
418 function mgHandleDirectory($mode, $basepath, $path, &$parentData, &$parentEntry, $writeMode, $startAt) |
0 | 419 { |
420 global $galExifConversions, $galTNPath, $galCleanFlags; | |
421 | |
422 // Get cache file path | |
423 if (($cacheFilename = mgGetPath($path, "cache_file")) === FALSE) | |
424 return mgError("Cache filename / path not set.\n"); | |
425 | |
426 mgCheckQuit(TRUE); | |
427 | |
428 // Read directory contents | |
60
ef9f17280372
Use modern PHP array definition in some places.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
429 $entries = []; |
0 | 430 if (($dirHandle = @opendir($path)) === FALSE) |
431 return mgError("Could not read directory '".$path."'.\n"); | |
432 | |
433 while (($dirFile = @readdir($dirHandle)) !== FALSE) | |
434 { | |
435 $realFile = $path."/".$dirFile; | |
436 if (is_dir($realFile)) | |
437 { | |
438 if ($dirFile[0] != "." && $dirFile != $galTNPath) | |
439 $entries[$dirFile] = array("type" => 1, "base" => $dirFile, "ext" => "", "mtime" => filemtime($realFile)); | |
440 } | |
441 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
|
442 if (preg_match("/^([^\/]+)(".mgGetSetting("format_exts").")$/i", $dirFile, $dirMatch)) |
0 | 443 $entries[$dirFile] = array("type" => 0, "base" => $dirMatch[1], "ext" => $dirMatch[2], "mtime" => filemtime($realFile), "hide" => false); |
444 } | |
445 closedir($dirHandle); | |
446 | |
447 mgCheckQuit(); | |
448 | |
449 // Cleanup mode | |
450 if ($mode == GCMD_CLEAN) | |
451 { | |
60
ef9f17280372
Use modern PHP array definition in some places.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
452 $gallery = []; |
0 | 453 |
454 if ($writeMode) | |
455 { | |
456 if ($galCleanFlags & GCLEAN_CACHES) | |
457 mgDelete($cacheFilename, FALSE); | |
458 | |
459 if ($galCleanFlags & GCLEAN_THUMBNAILS) | |
460 mgDelete($path."/".$galTNPath, TRUE); | |
461 } | |
462 } | |
463 else | |
464 // Update modes | |
465 if ($mode == GCMD_UPDATE || $mode == GCMD_RESCAN) | |
466 { | |
467 // 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
|
468 $galEntries = []; |
0 | 469 $cacheTime = -1; |
470 if ($mode == GCMD_UPDATE && file_exists($cacheFilename)) | |
471 { | |
472 $cacheTime = filemtime($cacheFilename); | |
473 @include $cacheFilename; | |
474 } | |
475 | |
476 // Read caption data | |
477 $captions = mgReadCaptionsFile($basepath, $path); | |
478 $gallery = mgGetAlbumData($basepath, $path); | |
479 if ($parentData !== NULL && $parentEntry !== NULL) | |
480 { | |
16
3491ab93630e
Add sub-gallery thumbnails to gallery cache data.
Matti Hamalainen <ccr@tnsp.org>
parents:
15
diff
changeset
|
481 $gallery["parent"] = &$parentData; |
0 | 482 mgCopyEntryData($gallery, $parentEntry, MG_STR, "caption", "caption"); |
483 } | |
484 | |
485 // Start actual processing | |
486 $nentries = count($entries); | |
487 $nentry = 0; | |
488 echo $path." .. "; | |
489 foreach ($entries as $ename => &$edata) | |
490 { | |
491 printf("\r%s (%1.1f%%) ..", $path, ($nentry * 100.0) / $nentries); | |
492 | |
493 $nentry++; | |
494 $efilename = $path."/".$ename; | |
495 | |
496 if (array_key_exists($ename, $galEntries)) | |
497 $galEntry = &$galEntries[$ename]; | |
498 else | |
499 $galEntry = array(); | |
500 | |
57
29bb365e3ee2
Be more responsive when processing images, check for signals after each
Matti Hamalainen <ccr@tnsp.org>
parents:
56
diff
changeset
|
501 mgCheckQuit(TRUE); |
0 | 502 |
503 // Update with captions file data, if any | |
504 if (array_key_exists($ename, $captions)) | |
505 { | |
506 foreach ($captions[$ename] as $ckey => $cval) | |
507 $edata[$ckey] = $cval; | |
508 } | |
509 | |
510 // Handle entry based on type | |
511 if ($edata["type"] == 0) | |
512 { | |
513 $updFlags = 0; | |
514 $tnPath = $path."/".$galTNPath; | |
515 $medFilename = $tnPath."/".$edata["base"].mgGetSetting("med_suffix").$edata["ext"]; | |
516 $tnFilename = $tnPath."/".$ename; | |
517 $capFilename = $path."/".$edata["base"].".txt"; | |
518 | |
519 // Check what we need to update .. | |
520 if (!file_exists($medFilename) || filemtime($medFilename) < $edata["mtime"]) | |
521 $updFlags |= GUPD_MED_IMAGE; | |
522 | |
523 if (!file_exists($tnFilename) || filemtime($tnFilename) < $edata["mtime"]) | |
524 $updFlags |= GUPD_TN_IMAGE; | |
525 | |
526 if (mgNeedUpdate($galEntry, "mtime", $edata["mtime"])) | |
527 $updFlags |= GUPD_EXIF_INFO; | |
528 | |
529 if (file_exists($capFilename) && | |
530 mgNeedUpdate($galEntry, "mtime", filemtime($capFilename))) | |
531 $updFlags |= GUPD_CAPTION; | |
532 | |
533 // Check for EXIF info | |
534 if (($updFlags & GUPD_EXIF_INFO) && | |
535 ($exif = @exif_read_data($efilename)) !== FALSE) | |
536 { | |
537 echo "%"; | |
538 foreach ($galExifConversions as $conv) | |
539 mgCopyEntryData($edata, $exif, $conv[0], $conv[1], $conv[2]); | |
540 } | |
541 else | |
542 { | |
543 // Copy old data that is not yet in new | |
544 echo "*"; | |
545 foreach ($galEntry as $okey => $odata) | |
546 { | |
547 if (!array_key_exists($okey, $edata)) | |
548 $edata[$okey] = $odata; | |
549 } | |
550 } | |
551 | |
552 // Generate thumbnails, etc. | |
553 if ($updFlags & GUPD_IMAGES) | |
554 { | |
555 mgMakeDir($tnPath, 0755); | |
556 | |
557 if ($updFlags & GUPD_MED_IMAGE) | |
558 { | |
559 echo "1"; | |
560 mgConvertImage($efilename, $medFilename, | |
561 array(mgGetSetting("med_width"), mgGetSetting("med_height")), | |
562 "JPEG", mgGetSetting("med_quality"), TRUE); | |
563 } | |
564 | |
565 if ($updFlags & GUPD_TN_IMAGE) | |
566 { | |
567 echo "2"; | |
568 mgConvertImage($efilename, $tnFilename, | |
569 array(mgGetSetting("tn_width"), mgGetSetting("tn_height")), | |
570 "JPEG", mgGetSetting("tn_quality"), TRUE); | |
571 } | |
572 } | |
573 | |
574 // Check for .txt caption file | |
575 if ($updFlags & GUPD_CAPTION) | |
576 { | |
577 echo "?"; | |
578 if (($tmpData = @file_get_contents($capFilename)) !== FALSE) | |
579 $edata["caption"] = $tmpData; | |
580 } | |
581 } | |
582 else | |
583 if ($edata["type"] == 1) | |
584 { | |
10 | 585 // Set some of the album data here |
0 | 586 $tmp = mgGetAlbumData($basepath, $efilename); |
587 mgCopyEntryData($edata, $tmp, MG_STR, "caption", "caption"); | |
588 mgCopyEntryData($edata, $tmp, MG_STR, "caption", "fallback_caption"); | |
589 mgCopyEntryData($edata, $tmp, MG_STR, "caption", "title"); | |
590 mgCopyEntryData($edata, $tmp, MG_BOOL, "hide", "hide", FALSE); | |
11
0b097770061d
Improve content hiding functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
591 mgCopyEntryData($edata, $tmp, MG_BOOL, "hide_contents", "hide_contents", FALSE); |
0 | 592 } |
593 } | |
594 | |
595 echo "\r".$path." ..... DONE\n"; | |
596 | |
597 mgCheckQuit(TRUE); | |
598 } | |
599 else | |
600 mgFatal("Invalid work mode '".$mode."'.\n"); | |
601 | |
602 mgCheckQuit(TRUE); | |
603 | |
604 // 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
|
605 foreach ($entries as $ename => &$edata) |
0 | 606 if ($edata["type"] == 1) |
607 { | |
608 $epath = $path."/".$ename."/"; | |
609 $newWriteMode = ($writeMode === FALSE && $epath == $startAt) || $writeMode; | |
610 | |
611 if (!mgHandleDirectory($mode, $basepath, $epath, $gallery, $edata, $newWriteMode, $startAt)) | |
612 return FALSE; | |
613 } | |
614 | |
16
3491ab93630e
Add sub-gallery thumbnails to gallery cache data.
Matti Hamalainen <ccr@tnsp.org>
parents:
15
diff
changeset
|
615 // Finish update modes |
3491ab93630e
Add sub-gallery thumbnails to gallery cache data.
Matti Hamalainen <ccr@tnsp.org>
parents:
15
diff
changeset
|
616 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
|
617 { |
3491ab93630e
Add sub-gallery thumbnails to gallery cache data.
Matti Hamalainen <ccr@tnsp.org>
parents:
15
diff
changeset
|
618 // Store gallery cache for this directory |
3491ab93630e
Add sub-gallery thumbnails to gallery cache data.
Matti Hamalainen <ccr@tnsp.org>
parents:
15
diff
changeset
|
619 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
|
620 return FALSE; |
3491ab93630e
Add sub-gallery thumbnails to gallery cache data.
Matti Hamalainen <ccr@tnsp.org>
parents:
15
diff
changeset
|
621 } |
3491ab93630e
Add sub-gallery thumbnails to gallery cache data.
Matti Hamalainen <ccr@tnsp.org>
parents:
15
diff
changeset
|
622 |
0 | 623 mgCheckQuit(TRUE); |
624 | |
625 return TRUE; | |
626 } | |
627 | |
628 | |
629 function mgSigHandler($signo) | |
630 { | |
631 global $flagQuit; | |
632 switch ($signo) | |
633 { | |
634 case SIGTERM: | |
635 mgFatal("Received SIGTERM.\n"); | |
636 break; | |
637 | |
638 case SIGQUIT: | |
639 case SIGINT: | |
640 $flagQuit = TRUE; | |
641 break; | |
642 | |
643 } | |
644 } | |
645 | |
646 | |
647 function mgProcessGalleries($cmd, $path) | |
648 { | |
649 global $galTNPath; | |
650 | |
651 // Fetch the settings we need | |
652 if (mgReadSettings() === FALSE) | |
653 die("MGallery not configured.\n"); | |
654 | |
655 // Check validity of some settings | |
656 $galPath = mgGetSetting("base_path"); | |
657 $galTNPath = mgCleanPath(TRUE, mgGetSetting("tn_path")); | |
658 if ($galTNPath != mgGetSetting("tn_path")) | |
659 mgError("Invalid tn_path, using '".$galTNPath."'.\n"); | |
660 | |
661 if (strpos($galTNPath, "/") !== FALSE || $galTNPath == "") | |
662 mgFatal("Invalid tn_path '".$galTNPath."'.\n"); | |
663 | |
664 $parentData = $parentEntry = NULL; | |
665 $writeMode = TRUE; | |
666 $startAt = NULL; | |
667 | |
668 // Check for path argument | |
669 if ($path !== FALSE) | |
670 { | |
671 // Check the given path, needs to be "under" the gallery path | |
672 $cmp = mgCleanPath(TRUE, mgRealPath($galPath))."/"; | |
673 $tmp = mgCleanPath(TRUE, mgRealPath($path))."/"; | |
674 if (substr($tmp, 0, strlen($cmp)) != $cmp) | |
675 mgFatal("Path '".$path."' ('".$tmp."') does not reside under '".$galPath."' ('".$cmp."')!\n"); | |
676 | |
677 // Check if we need to bootstrap | |
678 if ($cmp != $tmp) | |
679 { | |
680 $bpath = mgCleanPath(TRUE, mgRealPath($tmp."../")); | |
681 | |
682 if ($cmd != GCMD_CLEAN) | |
683 { | |
684 $cacheFile = mgGetPath($bpath, "cache_file"); | |
685 if (!file_exists($cacheFile)) | |
686 mgFatal("Can't start working from '".$path."', parent '".$cacheFile."' does not exist!\n"); | |
687 | |
688 @include($cacheFile); | |
689 if (!isset($galEntries) || !isset($galData)) | |
690 mgFatal("Cache file '".$cacheFile."' is broken or stale.\n"); | |
691 } | |
692 | |
693 $writeMode = FALSE; | |
694 $startAt = $tmp; | |
695 $path = $bpath; | |
696 | |
697 echo "Starting: '".$startAt."' inside '".$path."'.\n"; | |
698 } | |
699 else | |
700 $path = $tmp; | |
701 } | |
702 else | |
703 $path = $galPath; | |
704 | |
705 // Start working | |
706 echo "Gallery path: '".$galPath."', starting at '".$path."' ...\n"; | |
707 mgHandleDirectory($cmd, $galPath, $path, $parentData, $parentEntry, $writeMode, $startAt); | |
708 } | |
709 | |
710 | |
711 function mgShowCopyright() | |
712 { | |
713 global $mgProgVersion, $mgProgCopyright, | |
714 $mgProgInfo, $mgProgEmail; | |
715 | |
716 echo | |
717 "MGTool ".$mgProgVersion." - MGallery management tool\n". | |
718 $mgProgInfo." ".$mgProgEmail."\n". | |
719 "(C) Copyright ".$mgProgCopyright."\n"; | |
720 } | |
721 | |
722 | |
723 function mgShowHelp() | |
724 { | |
725 global $argv; | |
726 echo | |
727 "Usage: ".basename($argv[0])." <command> [arguments]\n". | |
728 "\n". | |
729 " --help - Show this help.\n". | |
730 " --version - Show version information.\n". | |
731 "\n". | |
732 " update [path]\n". | |
733 " Update directories under <path> or all gallery dirs.\n". | |
734 " Conditionally scans dirs for new or changed images and\n". | |
735 " updates cache files as needed.\n". | |
736 "\n". | |
737 " rescan [path]\n". | |
738 " Like 'update', but forces all cache files to be regenerated\n". | |
739 " and EXIF/caption/etc information to be re-scanned even\n". | |
740 " if the file timestamps do not justify re-scanning.\n". | |
741 "\n". | |
742 " clean <all|caches|thumbnails> [path]\n". | |
743 " Delete all generated files or selectively cache files or\n". | |
744 " everything inside thumbnail directories.\n". | |
745 "\n". | |
746 " config\n". | |
747 " Display configuration values.\n". | |
748 "\n"; | |
749 } | |
750 | |
751 | |
752 // | |
753 // Main code starts | |
754 // | |
755 if (php_sapi_name() != "cli" || !empty($_SERVER["REMOTE_ADDR"])) | |
756 { | |
757 header("Status: 404 Not Found"); | |
758 die(); | |
759 } | |
760 | |
761 pcntl_signal(SIGTERM, "mgSigHandler"); | |
762 pcntl_signal(SIGHUP, "mgSigHandler"); | |
763 pcntl_signal(SIGQUIT, "mgSigHandler"); | |
764 pcntl_signal(SIGINT, "mgSigHandler"); | |
765 | |
766 | |
767 $cmd = mgCArgLC(1); | |
768 switch ($cmd) | |
769 { | |
770 case "--version": | |
771 case "version": | |
772 case "ver": | |
773 mgShowCopyright(); | |
774 break; | |
775 | |
776 case FALSE: | |
777 // No arguments | |
778 mgError("Nothing to do. Showing help:\n"); | |
779 | |
780 case "--help": | |
781 case "help": | |
782 if ($cmd !== FALSE) | |
783 mgShowCopyright(); | |
784 mgShowHelp(); | |
785 break; | |
786 | |
787 case "update": case "up": case "upd": case "upda": | |
788 mgProcessGalleries(GCMD_UPDATE, mgCArg(2)); | |
789 break; | |
790 | |
791 case "rescan": case "re": case "res": | |
792 mgProcessGalleries(GCMD_RESCAN, mgCArg(2)); | |
793 break; | |
794 | |
795 case "clean": case "cl": case "cle": | |
796 $cmode = mgCArgLC(2, 2); | |
797 switch ($cmode) | |
798 { | |
799 case "al": $galCleanFlags = GCLEAN_ALL; break; | |
800 case "ca": $galCleanFlags = GCLEAN_CACHES; break; | |
801 case "th": $galCleanFlags = GCLEAN_THUMBNAILS; break; | |
802 case FALSE: | |
803 mgFatal("Cleaning requires a mode argument.\n"); | |
804 | |
805 default: | |
806 mgFatal("Invalid clean mode '".mgCArg(2)."'.\n"); | |
807 } | |
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
|
808 $countDoDelete = 0; |
0 | 809 $flagDoDelete = FALSE; |
810 mgProcessGalleries(GCMD_CLEAN, mgCArg(3)); | |
811 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
|
812 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
|
813 { |
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
|
814 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
|
815 } |
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 else |
0 | 817 if (mgYesNoPrompt("Really delete the above files and directories?")) |
818 { | |
25
c698618e6f67
Actually implement deletion.
Matti Hamalainen <ccr@tnsp.org>
parents:
16
diff
changeset
|
819 echo "Deleting ...\n"; |
c698618e6f67
Actually implement deletion.
Matti Hamalainen <ccr@tnsp.org>
parents:
16
diff
changeset
|
820 $flagDoDelete = TRUE; |
c698618e6f67
Actually implement deletion.
Matti Hamalainen <ccr@tnsp.org>
parents:
16
diff
changeset
|
821 mgProcessGalleries(GCMD_CLEAN, mgCArg(3)); |
c698618e6f67
Actually implement deletion.
Matti Hamalainen <ccr@tnsp.org>
parents:
16
diff
changeset
|
822 } |
c698618e6f67
Actually implement deletion.
Matti Hamalainen <ccr@tnsp.org>
parents:
16
diff
changeset
|
823 else |
c698618e6f67
Actually implement deletion.
Matti Hamalainen <ccr@tnsp.org>
parents:
16
diff
changeset
|
824 { |
c698618e6f67
Actually implement deletion.
Matti Hamalainen <ccr@tnsp.org>
parents:
16
diff
changeset
|
825 echo "Okay, canceling operation.\n"; |
0 | 826 } |
827 break; | |
828 | |
829 case "config": | |
830 case "dump": | |
831 if (mgReadSettings() === FALSE) | |
832 die("MGallery not configured.\n"); | |
833 | |
834 foreach ($mgDefaults as $key => $dval) | |
835 { | |
836 $sval = mgGetSetting($key); | |
26 | 837 if ($cmd == "dump") |
838 { | |
839 printf("%-20s = %s\n", $key, mgGetDValStr($dval[0], $sval)); | |
840 } | |
841 else | |
842 { | |
843 printf("%-20s = %s%s\n", | |
844 $key, | |
845 mgGetDValStr($dval[0], $sval), | |
846 ($dval[1] !== NULL && $sval !== $dval[1]) ? " (default: ".mgGetDValStr($dval[0], $dval[1]).")" : ""); | |
847 } | |
0 | 848 } |
849 break; | |
850 | |
851 default: | |
852 mgError("Unknown option/command '".$cmd."'.\n"); | |
853 break; | |
854 } | |
855 | |
856 ?> |