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