Mercurial > hg > fapweb
annotate faptool.php @ 983:1e347288712d
Add "upd" option to previews generation.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Sat, 29 Nov 2014 15:31:26 +0200 |
parents | d28f36ced178 |
children | 03b245ef3a47 |
rev | line source |
---|---|
827 | 1 #!/usr/bin/php |
2 <? | |
943 | 3 // |
4 // FAPWeb - Simple Web-based Demoparty Management System | |
5 // Special entry/preview/etc backend management commandline utility | |
6 // (C) Copyright 2012-2014 Tecnic Software productions (TNSP) | |
7 // | |
827 | 8 require_once "mconfig.inc.php"; |
9 require_once "msite.inc.php"; | |
10 | |
11 | |
12 | |
882 | 13 function wtConvertImage($inFilename, $inFileType, $outFilename, $setDim, $setFormat, $setQuality, $thumb) |
14 { | |
15 global $setPreviewPath; | |
912 | 16 $isTemp = FALSE; |
882 | 17 |
884 | 18 if ($inFileType == "gfx") |
19 { | |
20 // Oh great .. we need gfxconv here because Imagick handles ILBM like shit | |
951 | 21 $tmpFilename = tempnam($setPreviewPath, "tmp"); |
912 | 22 $isTemp = TRUE; |
23 if (wtExec( | |
884 | 24 "/usr/local/bin/gfxconv", |
970
9c93a3c4f95c
Clean out the debug code, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
969
diff
changeset
|
25 escapeshellarg($inFilename)." -f png -o ".escapeshellarg($tmpFilename), |
9c93a3c4f95c
Clean out the debug code, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
969
diff
changeset
|
26 0) === false) |
912 | 27 return FALSE; |
28 | |
951 | 29 if (!file_exists($tmpFilename)) |
912 | 30 { |
31 echo "ERROR: gfxconv did not output a temporary conversion inbetween.\n"; | |
32 return FALSE; | |
33 } | |
884 | 34 } |
35 else | |
951 | 36 $tmpFilename = $inFilename; |
884 | 37 |
882 | 38 // convert -resize 640x480 -background black -gravity center -extent 640x480 |
39 // -unsharp "0x0.75+0.75+0.008" lol.lbm -quality 95 test.jpg | |
884 | 40 |
41 // Create conversion entity | |
951 | 42 $img = new Imagick($tmpFilename); |
884 | 43 if ($img === false) |
912 | 44 { |
951 | 45 echo "ERROR: Oh noes! ImageMagick could not digest the file '".$tmpFilename."' (".$inFilename.")\n"; |
912 | 46 return FALSE; |
47 } | |
884 | 48 |
963 | 49 if ($setDim !== FALSE) |
884 | 50 { |
963 | 51 if (($outDim = stGetSetting($setDim)) === FALSE || |
52 ($outFormat = stGetSetting($setFormat)) === FALSE || | |
53 ($outQuality = stGetSetting($setQuality)) === FALSE) | |
54 { | |
55 die("Missing one of res/format/quality settings for '".$outFilename."'\n"); | |
56 } | |
57 | |
58 // Get dimensions, setup background | |
59 $dim = $img->getImageGeometry(); | |
969 | 60 // $img->setBackgroundColor(imagick::COLOR_BLACK); |
963 | 61 $img->setGravity(imagick::GRAVITY_CENTER); |
62 | |
63 // Act based on image size vs. desired size and $thumb mode | |
64 if ($thumb || $dim["width"] > $outDim[0] || $dim["height"] > $outDim[1]) | |
65 { | |
66 // Image is larger | |
67 $img->resizeImage($outDim[0], $outDim[1], Imagick::FILTER_QUADRATIC, 1); | |
969 | 68 $img->setImageExtent($outDim[0], $outDim[1]); |
963 | 69 $img->normalizeImage(); |
70 $img->unsharpMaskImage(0, 0.5, 1, 0.05); | |
71 } | |
72 else | |
73 if ($dim["width"] < $outDim[0] || $dim["height"] < $outDim[1]) | |
74 { | |
75 // Image is smaller than requested dimension(s)? | |
76 $img->resizeImage($outDim[0], $outDim[1], Imagick::FILTER_POINT, 1); | |
969 | 77 $img->setImageExtent($outDim[0], $outDim[1]); |
963 | 78 } |
884 | 79 } |
80 | |
81 $img->setFormat($outFormat); | |
82 $img->setCompressionQuality($outQuality); | |
83 | |
84 $img->stripImage(); | |
85 $img->writeImage($outFilename); | |
86 $img->removeImage(); | |
882 | 87 } |
88 | |
89 | |
898 | 90 function wtRenderSample($inFilename, $outFilename) |
882 | 91 { |
898 | 92 $sfreq = intval(stGetSetting("sampleFreq")); |
93 $sduration = intval(stGetSetting("sampleDuration")); | |
94 $schannels = intval(stGetSetting("sampleChannels")); | |
882 | 95 |
902 | 96 return wtExec( |
898 | 97 "/usr/local/bin/openmpt123" |
98 , | |
954 | 99 "--force --quiet ". |
884 | 100 "--samplerate ".$sfreq." ". |
101 "--channels ".$schannels." ". | |
102 "--playtime ".$sduration." ". | |
954 | 103 escapeshellarg($inFilename)." -o ".escapeshellarg($outFilename), |
963 | 104 0); |
882 | 105 } |
106 | |
107 | |
948
1334b93939a3
Change how output options are handled in wtConvertSample().
Matti Hamalainen <ccr@tnsp.org>
parents:
947
diff
changeset
|
108 function wtConvertSample($inFilename, $outFilename, $outOpts) |
882 | 109 { |
898 | 110 $sfreq = intval(stGetSetting("sampleFreq")); |
111 $sduration = intval(stGetSetting("sampleDuration")); | |
112 $schannels = intval(stGetSetting("sampleChannels")); | |
882 | 113 |
948
1334b93939a3
Change how output options are handled in wtConvertSample().
Matti Hamalainen <ccr@tnsp.org>
parents:
947
diff
changeset
|
114 foreach ($outOpts as $okey => $oval) |
898 | 115 $optStr .= $okey." ".$oval." "; |
939 | 116 |
902 | 117 return wtExec( |
898 | 118 "/usr/local/bin/avconv", |
119 "-y -t ".intval($sduration)." ". | |
120 "-i ".escapeshellarg($inFilename). | |
121 " ".$optStr." -ac ".$schannels. | |
122 " -map_metadata -1 ". | |
123 escapeshellarg($outFilename)); | |
882 | 124 } |
125 | |
126 | |
983
1e347288712d
Add "upd" option to previews generation.
Matti Hamalainen <ccr@tnsp.org>
parents:
971
diff
changeset
|
127 function wtEntryToSource($compo, $inFilename, $edata, $entryFilename, &$outData, &$outFilename, $force) |
882 | 128 { |
963 | 129 // Does compo preview type match this file class? |
130 if ($edata["class"] != $compo["preview_type"]) | |
131 return FALSE; | |
954 | 132 |
969 | 133 switch ($edata["class"]) |
134 { | |
135 case EFILE_AUDIO: $fext = "wav"; break; | |
136 case EFILE_IMAGE: $fext = "png"; break; | |
137 default: | |
138 return FALSE; | |
139 } | |
140 | |
141 $outFilename = stReplaceFileExt($entryFilename, "_preview.".$fext); | |
983
1e347288712d
Add "upd" option to previews generation.
Matti Hamalainen <ccr@tnsp.org>
parents:
971
diff
changeset
|
142 if (!$force && file_exists($outFilename) && filemtime($outFilename) >= filemtime($entryFilename)) |
969 | 143 return TRUE; |
963 | 144 |
969 | 145 printf("GENERATING: %s -> %s\n", $inFilename, $outFilename); |
146 | |
954 | 147 switch ($edata["class"]) |
939 | 148 { |
954 | 149 case EFILE_AUDIO: |
150 if ($edata["mime"] == "audio/x-mod") | |
963 | 151 $res = wtRenderSample($inFilename, $outFilename); |
954 | 152 else |
969 | 153 $res = wtConvertSample($inFilename, $outFilename, |
154 array("-f" => "wav")); | |
954 | 155 break; |
963 | 156 |
954 | 157 case EFILE_IMAGE: |
963 | 158 $res = wtConvertImage( |
159 $inFilename, | |
160 $edata["mime"], | |
161 $outFilename, | |
162 FALSE, "PNG", 9, FALSE); | |
163 break; | |
939 | 164 } |
963 | 165 |
166 return $res; | |
882 | 167 } |
168 | |
169 | |
902 | 170 function wtUnpackArchiveTo($atype, $filename, $path) |
882 | 171 { |
172 // Check file type before doing anything | |
953 | 173 echo "INFO: Preparing to unpack archive file '".$filename."' ...\n"; |
902 | 174 switch ($atype) |
882 | 175 { |
176 case "LHA": | |
177 $exe = "/usr/bin/lha"; | |
967 | 178 $args = "x ".escapeshellarg($filename); |
882 | 179 break; |
180 | |
181 case "ZIP": | |
182 $exe = "/usr/bin/unzip"; | |
183 $args = "-d ".escapeshellarg($path)." ".escapeshellarg($filename); | |
184 break; | |
185 | |
898 | 186 case "RAR": |
187 $exe = "/usr/bin/rar"; | |
968 | 188 $args = "x -w".escapeshellarg($path)." ".escapeshellarg($filename); |
189 break; | |
190 | |
191 case "7ZIP": | |
192 $exe = "/usr/bin/7z"; | |
193 $args = "x -o".escapeshellarg($path)." ".escapeshellarg($filename); | |
898 | 194 break; |
195 | |
882 | 196 default: |
902 | 197 echo "Unsupported archive file type: ".$atype."\n"; |
898 | 198 return FALSE; |
882 | 199 } |
200 | |
201 // Create temporary directory | |
202 wtPurgeDir($path); | |
935 | 203 wtMakeDir($path, 0700); |
882 | 204 |
205 if (!is_dir($path) || chdir($path) === false) | |
902 | 206 { |
953 | 207 echo "ERROR: Failed to chdir to '".$path."', can't unpack archive.\n"; |
902 | 208 return FALSE; |
209 } | |
882 | 210 |
211 // Unpack archive | |
902 | 212 return wtExec($exe, $args); |
213 } | |
214 | |
215 | |
933 | 216 function wtNiceName($compo, $entry, $efile = FALSE) |
217 { | |
218 return sprintf( | |
219 "%d: %s by %s%s", | |
220 $entry["id"], | |
221 $entry["name"], | |
222 $entry["author"], | |
223 ($efile !== false) ? " [".$efile["filename"]." / TYPE: '".$efile["filetype"]."']" : ""); | |
224 } | |
225 | |
226 | |
882 | 227 function wtHandleEntryPreview($compo, $entry, $mode) |
228 { | |
941 | 229 global $fileTypeData, $setEntryPath; |
933 | 230 |
231 // Get current preview file(s) data | |
898 | 232 if (!stGetPreviewFileData($compo, $entry, $pdata)) |
233 return FALSE; | |
234 | |
933 | 235 // Get entry file data |
963 | 236 $efile = stFetchSQL("SELECT * FROM files WHERE deleted=0 AND id=".$entry["file_id"]); |
898 | 237 |
238 // Check preview file(s) status | |
949
2179600710d1
Add "lis" as alias to "sta".
Matti Hamalainen <ccr@tnsp.org>
parents:
948
diff
changeset
|
239 if ($mode == "sta" || $mode == "lis") |
898 | 240 { |
241 printf(" %03d | %s%s%s | %-40s | %-5s | %s\n", | |
242 $entry["id"], | |
933 | 243 ($efile !== false) ? "E" : ".", |
244 isset($pdata["file"]) ? "P" : ".", | |
245 $pdata["valid"] ? "V" : ".", | |
246 substr($entry["name"]." by ".$entry["author"], 0, 40), | |
898 | 247 isset($pdata["file"]) ? $pdata["file"]["filetype"] : "", |
248 isset($pdata["file"]) ? $pdata["file"]["filename"] : "" | |
249 ); | |
954 | 250 return TRUE; |
898 | 251 } |
252 else | |
983
1e347288712d
Add "upd" option to previews generation.
Matti Hamalainen <ccr@tnsp.org>
parents:
971
diff
changeset
|
253 if ($mode != "gen" && $mode != "upd") |
953 | 254 die("ERROR: Unsupported previews mode ".$mode."\n"); |
933 | 255 |
983
1e347288712d
Add "upd" option to previews generation.
Matti Hamalainen <ccr@tnsp.org>
parents:
971
diff
changeset
|
256 $force = ($mode == "upd"); |
1e347288712d
Add "upd" option to previews generation.
Matti Hamalainen <ccr@tnsp.org>
parents:
971
diff
changeset
|
257 |
933 | 258 // Check validity of previews |
983
1e347288712d
Add "upd" option to previews generation.
Matti Hamalainen <ccr@tnsp.org>
parents:
971
diff
changeset
|
259 if ($pdata["valid"] && !$force) |
933 | 260 return TRUE; |
261 | |
262 // Okay, no valid previews .. lets see what we can do .. | |
263 // First, check if we have a source preview file | |
264 if (!isset($pdata["file"])) | |
898 | 265 { |
935 | 266 // No source preview, check if we have entry file either? |
267 if ($efile === false) | |
268 { | |
269 echo | |
270 "INFO: No entry file for ".wtNiceName($compo, $entry, $efile). | |
271 ", can't attempt to generate preview.\n"; | |
272 return FALSE; | |
273 } | |
954 | 274 |
275 // Actual entry filename + path | |
276 $filename = stMakePath(FALSE, TRUE, array($setEntryPath, $compo["cpath"], $efile["filename"])); | |
277 if (!file_exists($filename)) | |
278 { | |
279 echo "ERROR: Entry file '".$filename."' for ".wtNiceName($compo, $entry, FALSE)." does not exist!\n"; | |
280 return FALSE; | |
281 } | |
282 | |
935 | 283 // Preview source file does not exist, let's see .. |
963 | 284 $edata = stProbeFileInfo($filename, TRUE); |
285 if ($edata === false) | |
882 | 286 { |
935 | 287 echo |
288 "ERROR: Invalid/unsupported file type for entry ".wtNiceName($compo, $entry, $efile)."\n"; | |
289 return FALSE; | |
290 } | |
291 | |
942 | 292 $found = FALSE; |
935 | 293 if ($edata["class"] == EFILE_ARCHIVE) |
294 { | |
295 // Entry is an archive file .. | |
963 | 296 $path = stMakePath(FALSE, FALSE, array($setEntryPath, "UNPACKS", $efile["filename"])); |
941 | 297 |
298 if (wtUnpackArchiveTo($efile["filetype"], $filename, $path) === false) | |
299 return FALSE; | |
935 | 300 |
941 | 301 // Scan through files ... |
302 $dir = opendir($path); | |
303 while (($dentry = readdir($dir)) !== false) | |
304 { | |
305 $fname = $path."/".$dentry; | |
954 | 306 // check file type against entry's preview_type |
307 // if match, render / convert / cp to as source and exit loop. | |
308 if (is_file($fname) && | |
309 ($mdata = stProbeFileInfo($fname, TRUE)) !== false && | |
983
1e347288712d
Add "upd" option to previews generation.
Matti Hamalainen <ccr@tnsp.org>
parents:
971
diff
changeset
|
310 ($found = wtEntryToSource($compo, $fname, $mdata, $filename, $pdata, $outFilename, $force)) === true) |
954 | 311 break; |
941 | 312 } |
954 | 313 |
314 // Cleanup | |
941 | 315 closedir($dir); |
316 wtPurgeDir($path); | |
954 | 317 } |
318 else | |
319 if ($edata["class"] == $compo["preview_type"]) | |
320 { | |
321 // Single file | |
983
1e347288712d
Add "upd" option to previews generation.
Matti Hamalainen <ccr@tnsp.org>
parents:
971
diff
changeset
|
322 $found = wtEntryToSource($compo, $filename, $edata, $filename, $pdata, $outFilename, $force); |
954 | 323 } |
969 | 324 |
950 | 325 if (!$found) |
882 | 326 { |
935 | 327 echo "WARNING: Could not generate preview from entry ".wtNiceName($compo, $entry, $efile)."\n"; |
328 return FALSE; | |
898 | 329 } |
969 | 330 |
331 $inFilename = $outFilename; | |
898 | 332 } |
969 | 333 else |
334 $inFilename = stMakePath(FALSE, TRUE, array($setEntryPath, $compo["cpath"], $pdata["file"]["filename"])); | |
898 | 335 |
983
1e347288712d
Add "upd" option to previews generation.
Matti Hamalainen <ccr@tnsp.org>
parents:
971
diff
changeset
|
336 // Check validity vs. |
1e347288712d
Add "upd" option to previews generation.
Matti Hamalainen <ccr@tnsp.org>
parents:
971
diff
changeset
|
337 if (!$force) |
1e347288712d
Add "upd" option to previews generation.
Matti Hamalainen <ccr@tnsp.org>
parents:
971
diff
changeset
|
338 { |
1e347288712d
Add "upd" option to previews generation.
Matti Hamalainen <ccr@tnsp.org>
parents:
971
diff
changeset
|
339 $valid = TRUE; |
1e347288712d
Add "upd" option to previews generation.
Matti Hamalainen <ccr@tnsp.org>
parents:
971
diff
changeset
|
340 $inMTime = filemtime($inFilename); |
1e347288712d
Add "upd" option to previews generation.
Matti Hamalainen <ccr@tnsp.org>
parents:
971
diff
changeset
|
341 foreach ($pdata["files"] as $stype => $sdata) |
1e347288712d
Add "upd" option to previews generation.
Matti Hamalainen <ccr@tnsp.org>
parents:
971
diff
changeset
|
342 { |
1e347288712d
Add "upd" option to previews generation.
Matti Hamalainen <ccr@tnsp.org>
parents:
971
diff
changeset
|
343 if (!file_exists($sdata["file"]) || filemtime($sdata["file"]) < $inMTime) |
1e347288712d
Add "upd" option to previews generation.
Matti Hamalainen <ccr@tnsp.org>
parents:
971
diff
changeset
|
344 { |
1e347288712d
Add "upd" option to previews generation.
Matti Hamalainen <ccr@tnsp.org>
parents:
971
diff
changeset
|
345 $valid = FALSE; |
1e347288712d
Add "upd" option to previews generation.
Matti Hamalainen <ccr@tnsp.org>
parents:
971
diff
changeset
|
346 break; |
1e347288712d
Add "upd" option to previews generation.
Matti Hamalainen <ccr@tnsp.org>
parents:
971
diff
changeset
|
347 } |
1e347288712d
Add "upd" option to previews generation.
Matti Hamalainen <ccr@tnsp.org>
parents:
971
diff
changeset
|
348 } |
1e347288712d
Add "upd" option to previews generation.
Matti Hamalainen <ccr@tnsp.org>
parents:
971
diff
changeset
|
349 if ($valid) |
1e347288712d
Add "upd" option to previews generation.
Matti Hamalainen <ccr@tnsp.org>
parents:
971
diff
changeset
|
350 return TRUE; |
1e347288712d
Add "upd" option to previews generation.
Matti Hamalainen <ccr@tnsp.org>
parents:
971
diff
changeset
|
351 } |
1e347288712d
Add "upd" option to previews generation.
Matti Hamalainen <ccr@tnsp.org>
parents:
971
diff
changeset
|
352 |
935 | 353 // Either we now have a sourcefile or generated file |
969 | 354 switch ($compo["preview_type"]) |
941 | 355 { |
963 | 356 case EFILE_AUDIO: |
950 | 357 foreach (stGetSetting("sampleTypes") as $stype => $sopts) |
942 | 358 { |
950 | 359 wtConvertSample( |
360 $inFilename, | |
969 | 361 $pdata["files"][$stype]["file"], |
950 | 362 $sopts |
363 ); | |
942 | 364 } |
365 break; | |
969 | 366 |
963 | 367 case EFILE_IMAGE: |
969 | 368 wtConvertImage( |
369 $inFilename, | |
370 "png", | |
371 $pdata["files"]["image"]["file"], | |
942 | 372 "previewImageSize", "previewImageType", |
373 "previewImageQuality", FALSE); | |
882 | 374 |
969 | 375 wtConvertImage( |
376 $inFilename, | |
377 "png", | |
378 $pdata["files"]["thumb"]["file"], | |
942 | 379 "previewThumbSize", "previewThumbType", |
380 "previewThumbQuality", TRUE); | |
381 break; | |
941 | 382 } |
963 | 383 |
933 | 384 return TRUE; |
882 | 385 } |
386 | |
387 | |
884 | 388 function wtHandleEntry($compo, $entry, $mode) |
389 { | |
390 } | |
391 | |
392 | |
963 | 393 function wtUnpackEntry($compo, $entry, $pathPrefix, $useOrig = TRUE) |
394 { | |
395 global $setEntryPath; | |
396 | |
397 if (($efile = stFetchSQL("SELECT * FROM files WHERE deleted=0 AND id=".$entry["file_id"])) === false) | |
398 { | |
399 echo "INFO: No entry file for ".wtNiceName($compo, $entry, $efile)."\n"; | |
400 return FALSE; | |
401 } | |
402 | |
403 $filename = stMakePath(FALSE, TRUE, array($setEntryPath, $compo["cpath"], $efile["filename"])); | |
404 if (!file_exists($filename)) | |
405 { | |
406 echo "ERROR: Entry file '".$filename."' for ".wtNiceName($compo, $entry, FALSE)." does not exist!\n"; | |
407 return FALSE; | |
408 } | |
409 | |
410 $edata = stProbeFileInfo($filename, TRUE); | |
411 if ($edata === false) | |
412 { | |
413 echo | |
414 "ERROR: Invalid/unsupported file type for entry ".wtNiceName($compo, $entry, $efile)."\n"; | |
415 return FALSE; | |
416 } | |
417 | |
418 if (wtMakeDir(stMakePath(FALSE, FALSE, array($pathPrefix, $compo["cpath"])), 0755) === false) | |
419 return FALSE; | |
420 | |
964 | 421 $dstFileBase = $entry["show_id"]."--".($useOrig ? $efile["origname"] : $efile["filename"]); |
963 | 422 |
423 if ($edata["class"] == EFILE_ARCHIVE) | |
424 { | |
425 // Entry is an archive file .. | |
426 $dstPath = stMakePath(FALSE, FALSE, array($pathPrefix, $compo["cpath"], stReplaceFileExt($dstFileBase, ""))); | |
964 | 427 if (wtUnpackArchiveTo($edata["id"], $filename, $dstPath) === false) |
963 | 428 return FALSE; |
429 } | |
430 else | |
431 { | |
432 $dstFilename = stMakePath(FALSE, FALSE, array($pathPrefix, $compo["cpath"], $dstFileBase)); | |
433 if (copy($filename, $dstFilename) === false) | |
434 { | |
435 echo "Failed to copy '".$filename."' to '".$dstFilename."'\n"; | |
436 return FALSE; | |
437 } | |
438 } | |
439 | |
440 return TRUE; | |
441 } | |
442 | |
443 | |
827 | 444 // |
941 | 445 // Misc helper functions |
827 | 446 // |
941 | 447 function wtExec($exe, $args, $expect = 0) |
448 { | |
953 | 449 echo "EXEC: ".$exe." ".$args."\n"; |
941 | 450 exec(escapeshellcmd($exe)." ".$args, $output, $code); |
451 if ($code !== $expect) | |
452 { | |
453 echo | |
953 | 454 "ERROR: Executing ".$exe.": ".$code."\n".$args."\n". |
941 | 455 "------------------------------------------------\n". |
456 implode("\n", $output). | |
457 "------------------------------------------------\n"; | |
458 return FALSE; | |
459 } | |
460 return TRUE; | |
461 } | |
462 | |
463 | |
464 function wtExecOrDie($exe, $args) | |
465 { | |
466 if (wtExec($exe, $args) === false) | |
467 die(); | |
468 } | |
469 | |
470 | |
471 function wtPurgeDir($path) | |
472 { | |
942 | 473 if ($path != "" && $path !== false && file_exists($path) && is_dir($path)) |
941 | 474 { |
969 | 475 // echo "PURGING: ".$path."\n"; |
942 | 476 foreach (scandir($path) as $file) |
477 if ($file !== "." && $file !== "..") | |
478 { | |
479 $sub = $path."/".$file; | |
480 if (is_dir($sub)) | |
481 wtPurgeDir($sub); | |
482 else | |
483 unlink($sub); | |
484 } | |
485 rmdir($path); | |
941 | 486 } |
487 } | |
488 | |
489 | |
935 | 490 function wtMakeDir($path, $perm) |
827 | 491 { |
492 if (!file_exists($path)) | |
493 { | |
953 | 494 echo "INFO: Creating ".$path."\n"; |
827 | 495 if (mkdir($path, $perm, TRUE) === false) |
496 die("Could not create directory '".$path."'\n"); | |
497 } | |
498 } | |
499 | |
500 | |
935 | 501 function wtInitializeDirs() |
827 | 502 { |
902 | 503 global |
504 $setEntryPath, $setPreviewPath, $setThumbDir, | |
827 | 505 $setEntryPathPerms, $setPrevPathPerms; |
506 | |
507 echo "Checking for missing directories ...\n"; | |
935 | 508 wtMakeDir($setEntryPath, $setEntryPathPerms); |
509 wtMakeDir($setPreviewPath, $setPrevPathPerms); | |
827 | 510 |
511 foreach (stExecSQL("SELECT * FROM compos WHERE cpath <> '' AND cpath IS NOT NULL") as $compo) | |
512 { | |
935 | 513 wtMakeDir(stMakePath(FALSE, FALSE, array($setEntryPath, $compo["cpath"])), $setEntryPathPerms); |
514 wtMakeDir(stMakePath(FALSE, FALSE, array($setPreviewPath, $compo["cpath"])), $setPrevPathPerms); | |
515 wtMakeDir(stMakePath(FALSE, FALSE, array($setPreviewPath, $compo["cpath"], $setThumbDir)), $setPrevPathPerms); | |
827 | 516 } |
517 } | |
518 | |
519 | |
968 | 520 function wtPrintCompoHeader($compo) |
521 { | |
522 global $setTermWidth; | |
523 printf("==%'=-".($setTermWidth-2)."s\n", | |
524 sprintf("[ #%03d - %s ]", $compo["id"], substr($compo["name"], 0, 40)) | |
525 ); | |
526 } | |
527 | |
528 | |
529 function wtPrintDivider($chr = "-") | |
530 { | |
531 global $setTermWidth; | |
532 echo str_repeat($chr, $setTermWidth)."\n"; | |
533 } | |
534 | |
535 | |
935 | 536 function wtShowHelp() |
827 | 537 { |
935 | 538 global $argv; |
827 | 539 echo |
540 "faptool - Do stuff with FAPWeb database\n". | |
541 "(C) Copyright 2014 ccr/TNSP\n". | |
542 "\n". | |
917 | 543 "Usage: ".$argv[0]." <command> [args]\n". |
544 "Where command is one of following:\n". | |
827 | 545 "\n". |
546 " init\n". | |
547 " Create directories for entries and previews, if needed.\n". | |
548 "\n". | |
928 | 549 /* |
917 | 550 " clean [delete|crap]\n". |
551 " Clean files marked to be deleted and 'stale' files. This includes\n". | |
552 " any old or stale preview files and entry files.\n". | |
553 " Without 'delete' parameter just checks and shows what would deleted.\n". | |
554 " 'crap' parameter will purge EVERYTHING that is NOT in the database,\n". | |
555 " e.g. any other files, like temp files, etc. DANGEROUS IF YOU DO NOT\n". | |
556 " KNOW WHAT YOU ARE DOING!!!!!\n". | |
901 | 557 "\n". |
928 | 558 */ |
938 | 559 " probe <filename> [filename2 ..]\n". |
925
5d53b9d6c319
Add 'probe' function to faptool.
Matti Hamalainen <ccr@tnsp.org>
parents:
922
diff
changeset
|
560 " Probe specified file for file information.\n". |
5d53b9d6c319
Add 'probe' function to faptool.
Matti Hamalainen <ccr@tnsp.org>
parents:
922
diff
changeset
|
561 "\n". |
881 | 562 " previews <cmd> [args]\n". |
563 " Where <cmd> is one of:\n". | |
933 | 564 " status [cid] - List files and show what is missing, etc. (All or <cid>)\n". |
970
9c93a3c4f95c
Clean out the debug code, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
969
diff
changeset
|
565 " generate - Generate preview files that are missing OR out of date.\n". |
928 | 566 " (older then preview source file OR entry file where appropriate)\n". |
567 // " add <entry_id> <filename> - Add file as preview for entry_id.\n". | |
901 | 568 "\n". |
569 " entry <cmd> [args]\n". | |
933 | 570 " status [cid] - List entries for all compos or <cid>.\n". |
966 | 571 " unpack <path> - Create subdirs and copy all entries UNPACKED there.\n". |
928 | 572 /* |
573 " add <filename> '<name>' '<author>' - Add file as entry file\n". | |
574 " (works same as uploading from admin interface)\n". | |
575 " package <lha|zip|rar|7z>\n". | |
576 " Repackages/packages entries (unpacks first if archive),\n". | |
577 " and adds generated infofile.txt to packages.\n". | |
578 */ | |
827 | 579 "\n"; |
935 | 580 } |
581 | |
582 // | |
583 // Main program starts | |
584 // | |
944
535466595a5a
Move CLI exec check to program start.
Matti Hamalainen <ccr@tnsp.org>
parents:
943
diff
changeset
|
585 stCheckCLIExec(); |
935 | 586 if ($argc < 2) |
587 { | |
588 wtShowHelp(); | |
827 | 589 exit; |
590 } | |
591 | |
935 | 592 |
827 | 593 // Try to connect to database |
594 $spec = stGetSetting("sqlDB"); | |
595 if (($db = stConnectSQLDBSpec($spec)) === false) | |
596 die("Could not connect to SQL database '".$spec."'.\n"); | |
597 | |
598 echo "Using database spec '".$spec."'.\n"; | |
599 | |
600 // Fetch non-"hardcoded" settings from SQL database | |
601 stReloadSettings(); | |
602 | |
603 // Set some globals for our benefit | |
922 | 604 $setTermWidth = 75; |
827 | 605 $setEntryPath = stGetSetting("entryPath"); |
606 $setPreviewPath = stGetSetting("previewPath"); | |
607 $setPreviewURL = stGetSetting("previewURL"); | |
608 $setThumbDir = stGetSetting("thumbnailSubDir"); | |
832 | 609 $setEntryPathPerms = stGetSetting("entryPathPerms"); |
610 $setPrevPathPerms = stGetSetting("previewPathPerms"); | |
827 | 611 |
612 if ($setEntryPath === FALSE || $setPreviewPath === FALSE || | |
832 | 613 $setPreviewURL === FALSE || $setThumbDir === FALSE || |
614 $setEntryPathPerms === FALSE || $setPrevPathPerms === FALSE) | |
827 | 615 { |
922 | 616 die("Some required settings are not defined in mconfig.inc.php!\n"); |
827 | 617 } |
618 | |
619 | |
620 // Act according to specified command | |
884 | 621 switch (substr(stCArgLC(1), 0, 3)) |
827 | 622 { |
935 | 623 case "-?": case "--h": |
624 wtShowHelp(); | |
625 exit; | |
626 break; | |
627 | |
884 | 628 case "ini": |
827 | 629 // |
630 // Initialize the data directories etc | |
631 // | |
953 | 632 echo "INFO: Checking for missing directories ...\n"; |
935 | 633 wtInitializeDirs(); |
827 | 634 break; |
635 | |
925
5d53b9d6c319
Add 'probe' function to faptool.
Matti Hamalainen <ccr@tnsp.org>
parents:
922
diff
changeset
|
636 case "pro": |
945 | 637 // |
638 // Probe specified files | |
639 // | |
640 if ($argc < 2) | |
641 die("No filename specified.\n"); | |
642 | |
643 if (($finfo = finfo_open()) === false) | |
954 | 644 die("Internal error. Failed to initialize finfo()."); |
925
5d53b9d6c319
Add 'probe' function to faptool.
Matti Hamalainen <ccr@tnsp.org>
parents:
922
diff
changeset
|
645 |
945 | 646 for ($i = 2; $i < $argc; $i++) |
647 { | |
648 // Probe one file | |
649 $filename = $argv[$i]; | |
650 $sdata = @finfo_file($finfo, $filename, FILEINFO_NONE); | |
651 $smime = @finfo_file($finfo, $filename, FILEINFO_MIME_TYPE); | |
652 | |
653 echo | |
654 "\n". | |
655 "File : ".$filename."\n". | |
656 "Probed : ".$sdata."\n". | |
657 "Mime : ".$smime."\n"; | |
658 | |
954 | 659 if (($info = stProbeFileInfo($filename, TRUE)) !== false) |
925
5d53b9d6c319
Add 'probe' function to faptool.
Matti Hamalainen <ccr@tnsp.org>
parents:
922
diff
changeset
|
660 { |
945 | 661 $tmp = array(); |
662 foreach ($info as $ikey => $ival) | |
663 $tmp[] = $ikey."='".$ival."'"; | |
664 | |
665 echo "Data : ".implode(", ", $tmp)."\n"; | |
925
5d53b9d6c319
Add 'probe' function to faptool.
Matti Hamalainen <ccr@tnsp.org>
parents:
922
diff
changeset
|
666 } |
946 | 667 } |
925
5d53b9d6c319
Add 'probe' function to faptool.
Matti Hamalainen <ccr@tnsp.org>
parents:
922
diff
changeset
|
668 |
946 | 669 finfo_close($finfo); |
925
5d53b9d6c319
Add 'probe' function to faptool.
Matti Hamalainen <ccr@tnsp.org>
parents:
922
diff
changeset
|
670 break; |
5d53b9d6c319
Add 'probe' function to faptool.
Matti Hamalainen <ccr@tnsp.org>
parents:
922
diff
changeset
|
671 |
901 | 672 case "cle": |
947 | 673 // |
674 // File cleanup / deletion | |
675 // | |
901 | 676 $doDelete = (stCArgLC(2) == "delete"); |
677 foreach (stExecSQL("SELECT * FROM compos WHERE cpath <> '' AND cpath IS NOT NULL") as $compo) | |
678 if (stFetchSQLColumn("SELECT COUNT(*) FROM entries WHERE compo_id=".$compo["id"]) > 0) | |
679 { | |
968 | 680 wtPrintCompoHeader($compo); |
681 wtPrintDivider(); | |
901 | 682 |
683 foreach (stExecSQL("SELECT * FROM entries WHERE compo_id=".$compo["id"]) as $entry) | |
684 { | |
685 foreach (stExecSQL("SELECT * FROM files WHERE entry_id=".$entry["id"]) as $efile) | |
686 { | |
935 | 687 if ($efile["deleted"] != 0) |
901 | 688 { |
689 echo "X"; | |
690 } | |
691 } | |
692 } | |
693 echo "\n"; | |
968 | 694 wtPrintDivider(); |
901 | 695 } |
696 break; | |
697 | |
698 case "ent": | |
699 // | |
700 // Entry files handling | |
701 // | |
702 $mode = substr(stCArgLC(2), 0, 3); | |
703 switch ($mode) | |
704 { | |
705 case "sta": | |
949
2179600710d1
Add "lis" as alias to "sta".
Matti Hamalainen <ccr@tnsp.org>
parents:
948
diff
changeset
|
706 case "lis": |
933 | 707 $sql = (stCArg(3) != "") ? " AND id=".intval(stCArg(3)) : ""; |
708 foreach (stExecSQL("SELECT * FROM compos WHERE cpath <> '' AND cpath IS NOT NULL".$sql) as $compo) | |
901 | 709 if (stFetchSQLColumn("SELECT COUNT(*) FROM entries WHERE compo_id=".$compo["id"]) > 0) |
710 { | |
968 | 711 wtPrintCompoHeader($compo); |
712 wtPrintDivider(); | |
901 | 713 |
714 foreach (stExecSQL("SELECT * FROM entries WHERE compo_id=".$compo["id"]) as $entry) | |
715 wtHandleEntry($compo, $entry, $mode); | |
716 | |
968 | 717 wtPrintDivider(); |
901 | 718 } |
719 break; | |
720 | |
963 | 721 case "unp": |
722 if (($setPrefix = stCArg(3)) == "") | |
723 die("Unpack needs a destination path.\n"); | |
724 | |
725 foreach (stExecSQL("SELECT * FROM compos WHERE cpath <> '' AND cpath IS NOT NULL") as $compo) | |
726 foreach (stExecSQL("SELECT * FROM entries WHERE compo_id=".$compo["id"]) as $entry) | |
727 { | |
728 wtUnpackEntry($compo, $entry, $setPrefix); | |
729 } | |
730 break; | |
731 | |
901 | 732 case "pac": |
929 | 733 echo "Not implemented. :D\n"; |
901 | 734 break; |
735 | |
736 case "add": | |
929 | 737 echo "Not implemented. :D\n"; |
901 | 738 break; |
739 | |
740 default: | |
741 if ($mode == "") | |
742 die("ERROR! Entry command requires a sub-command argument.\n"); | |
743 else | |
744 die("ERROR! Invalid entry sub-command '".stCArg(2)."'.\n"); | |
745 break; | |
746 } | |
747 break; | |
748 | |
884 | 749 case "pre": |
881 | 750 // |
751 // Preview files handling | |
752 // | |
753 $mode = substr(stCArgLC(2), 0, 3); | |
754 switch ($mode) | |
755 { | |
970
9c93a3c4f95c
Clean out the debug code, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
969
diff
changeset
|
756 case "gen": |
983
1e347288712d
Add "upd" option to previews generation.
Matti Hamalainen <ccr@tnsp.org>
parents:
971
diff
changeset
|
757 case "upd": |
881 | 758 case "sta": |
949
2179600710d1
Add "lis" as alias to "sta".
Matti Hamalainen <ccr@tnsp.org>
parents:
948
diff
changeset
|
759 case "lis": |
933 | 760 $sql = (stCArg(3) != "") ? " AND id=".intval(stCArg(3)) : ""; |
761 foreach (stExecSQL("SELECT * FROM compos WHERE cpath <> '' AND cpath IS NOT NULL".$sql) as $compo) | |
898 | 762 if (stFetchSQLColumn("SELECT COUNT(*) FROM entries WHERE compo_id=".$compo["id"]) > 0) |
881 | 763 { |
968 | 764 wtPrintCompoHeader($compo); |
881 | 765 printf( |
898 | 766 "PrevType : %s\n", |
767 $previewTypeList[$compo["preview_type"]][0]); | |
941 | 768 |
968 | 769 if ($mode == "sta" || $mode == "lis") |
941 | 770 { |
771 printf(" %-3s | %-3s | %-40s |\n", | |
968 | 772 "#ID", "EPV", "Entry name/author"); |
941 | 773 } |
774 | |
968 | 775 wtPrintDivider("-"); |
881 | 776 |
777 foreach (stExecSQL("SELECT * FROM entries WHERE compo_id=".$compo["id"]) as $entry) | |
778 wtHandleEntryPreview($compo, $entry, $mode); | |
898 | 779 |
968 | 780 wtPrintDivider("="); |
781 } | |
782 if ($mode == "sta" || $mode == "lis") | |
783 { | |
784 echo "Flags: E = Entry has file, P = Has preview file, V = Generated previews are up to date.\n"; | |
881 | 785 } |
786 break; | |
787 | |
901 | 788 case "add": |
929 | 789 echo "Not implemented. :D\n"; |
901 | 790 break; |
791 | |
881 | 792 default: |
793 if ($mode == "") | |
794 die("ERROR! Previews command requires a sub-command argument.\n"); | |
795 else | |
796 die("ERROR! Invalid previews sub-command '".stCArg(2)."'.\n"); | |
797 break; | |
798 } | |
799 break; | |
800 | |
827 | 801 default: |
802 echo "ERROR! Invalid operation mode '".stCArg(1)."'.\n"; | |
803 break; | |
804 } | |
805 | |
941 | 806 /* Dummy |
807 { | |
808 | |
809 } | |
810 */ | |
827 | 811 ?> |