update_020500.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. ###
  3. # @name Update to version 2.5
  4. # @copyright 2014 by Tobias Reich
  5. ###
  6. # Add `plugins`
  7. $query = Database::prepare($database, "SELECT `key` FROM `?` WHERE `key` = 'plugins' LIMIT 1", array(LYCHEE_TABLE_SETTINGS));
  8. $result = $database->query($query);
  9. if ($result->num_rows===0) {
  10. $query = Database::prepare($database, "INSERT INTO `?` (`key`, `value`) VALUES ('plugins', '')", array(LYCHEE_TABLE_SETTINGS));
  11. $result = $database->query($query);
  12. if (!$result) {
  13. Log::error($database, 'update_020500', __LINE__, 'Could not update database (' . $database->error . ')');
  14. return false;
  15. }
  16. }
  17. # Add `takestamp`
  18. $query = Database::prepare($database, "SELECT `takestamp` FROM `?` LIMIT 1;", array(LYCHEE_TABLE_PHOTOS));
  19. if (!$database->query($query)) {
  20. $query = Database::prepare($database, "ALTER TABLE `?` ADD `takestamp` INT(11) DEFAULT NULL", array(LYCHEE_TABLE_PHOTOS));
  21. $result = $database->query($query);
  22. if (!$result) {
  23. Log::error($database, 'update_020500', __LINE__, 'Could not update database (' . $database->error . ')');
  24. return false;
  25. }
  26. }
  27. # Convert to `takestamp`
  28. $query = Database::prepare($database, "SELECT `takedate`, `taketime` FROM `?` LIMIT 1;", array(LYCHEE_TABLE_PHOTOS));
  29. if ($database->query($query)) {
  30. $query = Database::prepare($database, "SELECT `id`, `takedate`, `taketime` FROM `?` WHERE `takedate` <> '' AND `taketime` <> ''", array(LYCHEE_TABLE_PHOTOS));
  31. $result = $database->query($query);
  32. if (!$result) {
  33. Log::error($database, 'update_020500', __LINE__, 'Could not update database (' . $database->error . ')');
  34. return false;
  35. }
  36. while ($photo = $result->fetch_object()) {
  37. $takestamp = strtotime($photo->takedate . $photo->taketime);
  38. $query = Database::prepare($database, "UPDATE `?` SET `takestamp` = '?' WHERE `id` = '?'", array(LYCHEE_TABLE_PHOTOS, $takestamp, $photo->id));
  39. $database->query($query);
  40. }
  41. $query = Database::prepare($database, "ALTER TABLE `?` DROP COLUMN `takedate`;", array(LYCHEE_TABLE_PHOTOS));
  42. $result = $database->query($query);
  43. $query = Database::prepare($database, "ALTER TABLE `?` DROP COLUMN `taketime`", array(LYCHEE_TABLE_PHOTOS));
  44. $result = $database->query($query);
  45. }
  46. # Remove `import_name`
  47. $query = Database::prepare($database, "SELECT `import_name` FROM `?` LIMIT 1", array(LYCHEE_TABLE_PHOTOS));
  48. if ($database->query($query)) {
  49. $query = Database::prepare($database, "ALTER TABLE `?` DROP COLUMN `import_name`", array(LYCHEE_TABLE_PHOTOS));
  50. $result = $database->query($query);
  51. }
  52. # Remove `sysdate` and `systime`
  53. $query = Database::prepare($database, "SELECT `sysdate`, `systime` FROM `?` LIMIT 1", array(LYCHEE_TABLE_PHOTOS));
  54. if ($database->query($query)) {
  55. $query = Database::prepare($database, "ALTER TABLE `?` DROP COLUMN `sysdate`", array(LYCHEE_TABLE_PHOTOS));
  56. $result = $database->query($query);
  57. $query = Database::prepare($database, "ALTER TABLE `?` DROP COLUMN `systime`", array(LYCHEE_TABLE_PHOTOS));
  58. $result = $database->query($query);
  59. }
  60. # Add `sysstamp`
  61. $query = Database::prepare($database, "SELECT `sysstamp` FROM `?` LIMIT 1", array(LYCHEE_TABLE_ALBUMS));
  62. if (!$database->query($query)) {
  63. $query = Database::prepare($database, "ALTER TABLE `?` ADD `sysstamp` INT(11) DEFAULT NULL", array(LYCHEE_TABLE_ALBUMS));
  64. $result = $database->query($query);
  65. if (!$result) {
  66. Log::error($database, 'update_020500', __LINE__, 'Could not update database (' . $database->error . ')');
  67. return false;
  68. }
  69. }
  70. # Convert to `sysstamp`
  71. $query = Database::prepare($database, "SELECT `sysdate` FROM `?` LIMIT 1", array(LYCHEE_TABLE_ALBUMS));
  72. if ($database->query($query)) {
  73. $query = Database::prepare($database, "SELECT `id`, `sysdate` FROM `?`", array(LYCHEE_TABLE_ALBUMS));
  74. $result = $database->query($query);
  75. if (!$result) {
  76. Log::error($database, 'update_020500', __LINE__, 'Could not update database (' . $database->error . ')');
  77. return false;
  78. }
  79. while ($album = $result->fetch_object()) {
  80. $sysstamp = strtotime($album->sysdate);
  81. $query = Database::prepare($database, "UPDATE `?` SET `sysstamp` = '?' WHERE `id` = '?'", array(LYCHEE_TABLE_ALBUMS, $sysstamp, $album->id));
  82. $database->query($query);
  83. }
  84. $query = Database::prepare($database, "ALTER TABLE `?` DROP COLUMN `sysdate`", array(LYCHEE_TABLE_ALBUMS));
  85. $result = $database->query($query);
  86. }
  87. # Set character of database
  88. $result = $database->query("ALTER DATABASE $dbName CHARACTER SET utf8 COLLATE utf8_general_ci;");
  89. if (!$result) {
  90. Log::error($database, 'update_020500', __LINE__, 'Could not update database (' . $database->error . ')');
  91. return false;
  92. }
  93. # Set character
  94. $query = Database::prepare($database, "ALTER TABLE `?` CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci", array(LYCHEE_TABLE_ALBUMS));
  95. $result = $database->query($query);
  96. if (!$result) {
  97. Log::error($database, 'update_020500', __LINE__, 'Could not update database (' . $database->error . ')');
  98. return false;
  99. }
  100. # Set character
  101. $query = Database::prepare($database, "ALTER TABLE `?` CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci", array(LYCHEE_TABLE_PHOTOS));
  102. $result = $database->query($query);
  103. if (!$result) {
  104. Log::error($database, 'update_020500', __LINE__, 'Could not update database (' . $database->error . ')');
  105. return false;
  106. }
  107. # Set character
  108. $query = Database::prepare($database, "ALTER TABLE `?` CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci", array(LYCHEE_TABLE_SETTINGS));
  109. $result = $database->query($query);
  110. if (!$result) {
  111. Log::error($database, 'update_020500', __LINE__, 'Could not update database (' . $database->error . ')');
  112. return false;
  113. }
  114. # Set album password length to 100 (for longer hashes)
  115. $query = Database::prepare($database, "ALTER TABLE `?` CHANGE `password` `password` VARCHAR(100)", array(LYCHEE_TABLE_ALBUMS));
  116. $result = $database->query($query);
  117. if (!$result) {
  118. Log::error($database, 'update_020500', __LINE__, 'Could not update database (' . $database->error . ')');
  119. return false;
  120. }
  121. # Set make length to 50
  122. $query = Database::prepare($database, "ALTER TABLE `?` CHANGE `make` `make` VARCHAR(50)", array(LYCHEE_TABLE_PHOTOS));
  123. $result = $database->query($query);
  124. if (!$result) {
  125. Log::error($database, 'update_020500', __LINE__, 'Could not update database (' . $database->error . ')');
  126. return false;
  127. }
  128. # Reset sorting
  129. $query = Database::prepare($database, "UPDATE ? SET value = 'ORDER BY takestamp DESC' WHERE `key` = 'sorting' AND `value` LIKE '%UNIX_TIMESTAMP%'", array(LYCHEE_TABLE_SETTINGS));
  130. $result = $database->query($query);
  131. if (!$result) {
  132. Log::error($database, 'update_020500', __LINE__, 'Could not update database (' . $database->error . ')');
  133. return false;
  134. }
  135. # Set version
  136. if (Database::setVersion($database, '020500')===false) return false;
  137. ?>