update_020500.php 6.5 KB

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