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