Mercurial > hg > mgallery
comparison mgtool.php @ 343:d6422e856ecd
Remove the backend/SQL stuff for now.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Sun, 27 Aug 2023 04:16:05 +0300 |
parents | 4dd202fffdf7 |
children | 0f14a20f48ca |
comparison
equal
deleted
inserted
replaced
342:4dd202fffdf7 | 343:d6422e856ecd |
---|---|
55 [ MG_STR, "title" , "title" , TRUE ], | 55 [ MG_STR, "title" , "title" , TRUE ], |
56 ]; | 56 ]; |
57 | 57 |
58 | 58 |
59 // | 59 // |
60 // SQL table schemas | |
61 // | |
62 $galSQLTables = | |
63 [ | |
64 "images" => [ | |
65 ["id" , "INTEGER", "PRIMARY KEY", "AUTOINCREMENT"], | |
66 ["path_id" , "INTEGER"], | |
67 ["filename" , "VARCHAR(64)"], | |
68 ], | |
69 | |
70 "paths" => [ | |
71 ["id" , "INTEGER", "PRIMARY KEY", "AUTOINCREMENT"], | |
72 ["path" , "VARCHAR(128)"], | |
73 ], | |
74 | |
75 "metadata" => [ | |
76 ["id" , "INTEGER", "PRIMARY KEY", "AUTOINCREMENT"], | |
77 ["field" , "VARCHAR(64)"], | |
78 ["data" , "VARCHAR(64)", "UNIQUE"], | |
79 ["status" , "INTEGER"], | |
80 ], | |
81 | |
82 "images_meta" => [ | |
83 ["image_id" , "INTEGER"], | |
84 ["meta_id" , "INTEGER"], | |
85 ], | |
86 ]; | |
87 | |
88 | |
89 // | |
90 // Convert and scale image file function, for generating | 60 // Convert and scale image file function, for generating |
91 // the intermediate size images and thumbnails. Uses the | 61 // the intermediate size images and thumbnails. Uses the |
92 // PHP ImageMagick or GraphicsMagick bindings. | 62 // PHP ImageMagick or GraphicsMagick bindings. |
93 // | 63 // |
94 function mgConvertImage($inFilename, $outFilename, $outDim, $outFormat, $outQuality, $thumb) | 64 function mgConvertImage($inFilename, $outFilename, $outDim, $outFormat, $outQuality, $thumb) |
829 } | 799 } |
830 | 800 |
831 | 801 |
832 function mgWriteGalleryCache($cacheFilename, &$gallery, &$entries, &$parentEntry) | 802 function mgWriteGalleryCache($cacheFilename, &$gallery, &$entries, &$parentEntry) |
833 { | 803 { |
834 global $galBackend, $db, $galExifConversions, $flagSQL; | 804 global $galBackend, $db, $galExifConversions; |
835 | 805 |
836 // Store gallery cache for this directory | 806 // Store gallery cache for this directory |
837 $images = []; | 807 $images = []; |
838 $albums = []; | 808 $albums = []; |
839 $output = []; | 809 $output = []; |
898 $parentEntry["image"] = &$adata; | 868 $parentEntry["image"] = &$adata; |
899 break; | 869 break; |
900 } | 870 } |
901 } | 871 } |
902 } | 872 } |
903 } | |
904 | |
905 if ($galBackend == "sql" && $flagSQL) | |
906 { | |
907 mgDBBeginTransaction(); | |
908 | |
909 if (($path_id = mgFetchSQLColumn(mgPrepareSQL("SELECT id FROM paths WHERE path=%s", $gallery["path"]))) === FALSE && | |
910 ($path_id = mgExecSQLInsert(mgPrepareSQL("INSERT INTO paths (path) VALUES (%s)", $gallery["path"]))) === FALSE) | |
911 mgFatal("SQL path select failed.\n"); | |
912 | |
913 foreach ($output as $entry => &$edata) | |
914 if ($edata["type"] == 0) | |
915 { | |
916 $ekeys = ["path_id", "filename"]; | |
917 $evals = [$path_id, mgDBGetSQLParam($db, "s", $entry)]; | |
918 $esets = []; | |
919 | |
920 foreach ($galExifConversions as $econv) | |
921 if ($econv[GEC_IS_UNIQUE]) | |
922 { | |
923 $ekey = $econv[GEC_NAME]; | |
924 | |
925 // Skip unset and special case(s) | |
926 if (!isset($esets[$ekey]) && isset($edata[$ekey])) | |
927 { | |
928 $esets[$ekey] = TRUE; | |
929 | |
930 switch ($econv[GEC_TYPE]) | |
931 { | |
932 case MG_DATE: $etype = "D"; break; | |
933 case MG_INT: $etype = "d"; break; | |
934 default: $etype = "s"; break; | |
935 } | |
936 | |
937 $ekeys[] = $ekey; | |
938 $evals[] = mgDBGetSQLParam($db, $etype, $edata[$ekey]); | |
939 } | |
940 } | |
941 | |
942 $sql = "INSERT INTO images (".implode(",", $ekeys).") VALUES (".implode(",", $evals).")"; | |
943 if (($image_id = mgExecSQLInsert($sql)) === FALSE) | |
944 mgFatal("Failed.\n"); | |
945 | |
946 // Special handling for non-unique fields | |
947 foreach ($galExifConversions as $econv) | |
948 { | |
949 $ekey = $econv[GEC_NAME]; | |
950 if (!$econv[GEC_IS_UNIQUE] && | |
951 array_key_exists($econv[GEC_NAME], $edata)) | |
952 { | |
953 if (is_array($edata[$ekey])) | |
954 { | |
955 foreach ($edata[$ekey] as $kw) | |
956 { | |
957 $sql = mgPrepareSQL("INSERT INTO metadata (field,data,status) VALUES (%s,%s,0) ON CONFLICT(data) DO UPDATE SET status=1", $ekey, $kw); | |
958 if (($id = mgExecSQLInsert($sql)) === FALSE) | |
959 mgFatal("Failure!\n"); | |
960 | |
961 $sql = mgPrepareSQL("INSERT INTO images_meta (image_id,meta_id) VALUES (%d,%d)", $image_id, $id); | |
962 if (($id = mgExecSQL($sql)) === FALSE) | |
963 mgFatal("Failure!\n"); | |
964 } | |
965 } | |
966 else | |
967 { | |
968 $sql = mgPrepareSQL("INSERT INTO metadata (field,data,status) VALUES (%s,%s,0) ON CONFLICT(data) DO UPDATE SET status=1", $ekey, $edata[$ekey]); | |
969 if (($id = mgExecSQLInsert($sql)) === FALSE) | |
970 mgFatal("Failure!\n"); | |
971 | |
972 $sql = mgPrepareSQL("INSERT INTO images_meta (image_id,meta_id) VALUES (%d,%d)", $image_id, $id); | |
973 if (($id = mgExecSQL($sql)) === FALSE) | |
974 mgFatal("Failure!\n"); | |
975 } | |
976 | |
977 } | |
978 } | |
979 } | |
980 | |
981 mgDBCommitTransaction(); | |
982 } | 873 } |
983 | 874 |
984 $str = | 875 $str = |
985 "<?php\n". | 876 "<?php\n". |
986 "\$galData = ".var_export($gallery, TRUE).";\n". | 877 "\$galData = ".var_export($gallery, TRUE).";\n". |
1293 } | 1184 } |
1294 | 1185 |
1295 | 1186 |
1296 function mgProcessGalleries($cmd, $path) | 1187 function mgProcessGalleries($cmd, $path) |
1297 { | 1188 { |
1298 global $galTNPath, $galMedPath, $galSQLTables, $flagSQL; | 1189 global $galTNPath, $galMedPath; |
1299 | 1190 |
1300 // Check validity of some settings | 1191 // Check validity of some settings |
1301 $galPath = mgGetSetting("base_path"); | 1192 $galPath = mgGetSetting("base_path"); |
1302 $galTNPath = mgCleanPath(TRUE, mgGetSetting("tn_path")); | 1193 $galTNPath = mgCleanPath(TRUE, mgGetSetting("tn_path")); |
1303 $galMedPath = mgCleanPath(TRUE, mgGetSetting("med_path")); | 1194 $galMedPath = mgCleanPath(TRUE, mgGetSetting("med_path")); |
1346 else | 1237 else |
1347 $path = $galPath; | 1238 $path = $galPath; |
1348 | 1239 |
1349 // Start working | 1240 // Start working |
1350 echo "Gallery path: '".$galPath."', starting at '".$path."' ...\n"; | 1241 echo "Gallery path: '".$galPath."', starting at '".$path."' ...\n"; |
1351 | |
1352 if (($cmd == GCMD_CLEAN || $cmd == GCMD_RESCAN) && $flagSQL) | |
1353 { | |
1354 foreach ($galSQLTables as $tname => $tdata) | |
1355 { | |
1356 if (mgExecSQL("DELETE FROM ".$tname) === FALSE) | |
1357 mgFatal("Failed to purge table '".$tname."'.\n"); | |
1358 } | |
1359 } | |
1360 | 1242 |
1361 mgHandleDirectory($cmd, $galPath, $path, $parentData, $parentEntry, $writeMode, $startAt); | 1243 mgHandleDirectory($cmd, $galPath, $path, $parentData, $parentEntry, $writeMode, $startAt); |
1362 } | 1244 } |
1363 | 1245 |
1364 | 1246 |
1532 | 1414 |
1533 // Configure the timezone | 1415 // Configure the timezone |
1534 if (($pageTimeZone = mgGetSetting("timezone")) !== NULL) | 1416 if (($pageTimeZone = mgGetSetting("timezone")) !== NULL) |
1535 date_default_timezone_set($pageTimeZone); | 1417 date_default_timezone_set($pageTimeZone); |
1536 | 1418 |
1537 // Check backend | |
1538 if (($galBackend = mgGetSetting("backend")) === null) | |
1539 die("MGallery backend mode not set.\n"); | |
1540 | |
1541 switch ($galBackend = strtolower($galBackend)) | |
1542 { | |
1543 case "sql": | |
1544 // Connect to database | |
1545 if (mgConnectSQLDB() === FALSE) | |
1546 die("Could not open database connection.\n"); | |
1547 | |
1548 // Create SQL schema elements for metadata fields | |
1549 $tmpSQL = []; | |
1550 foreach ($galExifConversions as $econv) | |
1551 if ($econv[GEC_IS_UNIQUE]) | |
1552 { | |
1553 switch ($econv[GEC_TYPE]) | |
1554 { | |
1555 case MG_STR: $etype = "VARCHAR(256)"; break; | |
1556 case MG_INT: $etype = "INTEGER"; break; | |
1557 case MG_DVA: $etype = "VARCHAR(32)"; break; | |
1558 case MG_DATE: $etype = "DATETIME"; break; | |
1559 } | |
1560 | |
1561 $tmpSQL[$econv[GEC_NAME]] = $etype; | |
1562 } | |
1563 | |
1564 // Merge to base "images" table schema | |
1565 foreach ($tmpSQL as $ekey => $etype) | |
1566 $galSQLTables["images"][] = [$ekey, $etype]; | |
1567 | |
1568 // Check tables | |
1569 foreach ($galSQLTables as $name => $schemaData) | |
1570 { | |
1571 $schema = mgDBGetTableSchema($db, $schemaData); | |
1572 if (!mgDBCreateOneTable($db, $name, $schema)) | |
1573 die("Failed to create SQL table '".$name."'.\n"); | |
1574 } | |
1575 break; | |
1576 | |
1577 case "php": | |
1578 break; | |
1579 | |
1580 default: | |
1581 die("Invalid MGallery backend mode '".$galBackend."'.\n"); | |
1582 } | |
1583 | |
1584 | 1419 |
1585 // Check for commandline arguments | 1420 // Check for commandline arguments |
1586 $cmd = mgCArgLC(1); | 1421 $cmd = mgCArgLC(1); |
1587 switch ($cmd) | 1422 switch ($cmd) |
1588 { | 1423 { |
1627 mgShowHelp(); | 1462 mgShowHelp(); |
1628 break; | 1463 break; |
1629 | 1464 |
1630 case "update": case "up": case "upd": case "upda": | 1465 case "update": case "up": case "upd": case "upda": |
1631 $farg = mgCArg(2); | 1466 $farg = mgCArg(2); |
1632 if ($farg == "--sql") | |
1633 { | |
1634 $farg = FALSE; | |
1635 $flagSQL = TRUE; | |
1636 } | |
1637 else | |
1638 $flagSQL = mgCArg(3) == "--sql"; | |
1639 | |
1640 mgProcessGalleries(GCMD_UPDATE, $farg); | 1467 mgProcessGalleries(GCMD_UPDATE, $farg); |
1641 break; | 1468 break; |
1642 | 1469 |
1643 case "rescan": case "re": case "res": | 1470 case "rescan": case "re": case "res": |
1644 mgProcessGalleries(GCMD_RESCAN, mgCArg(2)); | 1471 mgProcessGalleries(GCMD_RESCAN, mgCArg(2)); |