migrate_picture_path.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #!/usr/bin/env php
  2. <?php
  3. /*
  4. * Copyright (C) 2007-2016 Laurent Destailleur <eldy@users.sourceforge.net>
  5. * Copyright (C) 2015 Jean Heimburger <http://tiaris.eu>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  19. */
  20. /**
  21. * \file scripts/product/migrate_picture_path.php
  22. * \ingroup scripts
  23. * \brief Migrate pictures from old system prior to 3.7 to new path for 3.7+
  24. */
  25. $sapi_type = php_sapi_name();
  26. $script_file = basename(__FILE__);
  27. $path = __DIR__.'/';
  28. // Test if batch mode
  29. if (substr($sapi_type, 0, 3) == 'cgi') {
  30. echo "Error: You are using PHP for CGI. To execute ".$script_file." from command line, you must use PHP for CLI mode.\n";
  31. exit(-1);
  32. }
  33. @set_time_limit(0); // No timeout for this script
  34. define('EVEN_IF_ONLY_LOGIN_ALLOWED', 1); // Set this define to 0 if you want to lock your script when dolibarr setup is "locked to admin user only".
  35. // Include and load Dolibarr environment variables
  36. require_once $path."../../htdocs/master.inc.php";
  37. require_once DOL_DOCUMENT_ROOT."/product/class/product.class.php";
  38. require_once DOL_DOCUMENT_ROOT."/core/lib/files.lib.php";
  39. // After this $db, $mysoc, $langs, $conf and $hookmanager are defined (Opened $db handler to database will be closed at end of file).
  40. // $user is created but empty.
  41. // $langs->setDefaultLang('en_US'); // To change default language of $langs
  42. $langs->load("main"); // To load language file for default language
  43. // Global variables
  44. $version = DOL_VERSION;
  45. $error = 0;
  46. $forcecommit = 0;
  47. print "***** ".$script_file." (".$version.") pid=".dol_getmypid()." *****\n";
  48. dol_syslog($script_file." launched with arg ".join(',', $argv));
  49. if (!isset($argv[1]) || $argv[1] != 'product') {
  50. print "Usage: $script_file product\n";
  51. exit(-1);
  52. }
  53. print '--- start'."\n";
  54. // Case to migrate products path
  55. if ($argv[1] == 'product') {
  56. $product = new Product($db);
  57. $sql = "SELECT rowid as pid from ".MAIN_DB_PREFIX."product"; // Get list of all products
  58. $resql = $db->query($sql);
  59. if ($resql) {
  60. while ($obj = $db->fetch_object($resql)) {
  61. $product->fetch($obj->pid);
  62. print " migrating product id=".$product->id." ref=".$product->ref."\n";
  63. migrate_product_photospath($product);
  64. }
  65. } else {
  66. print "\n sql error ".$sql;
  67. exit();
  68. }
  69. }
  70. $db->close(); // Close $db database opened handler
  71. exit($error);
  72. /**
  73. * Migrate file from old path to new one for product $product
  74. *
  75. * @param Product $product Object product
  76. * @return void
  77. */
  78. function migrate_product_photospath($product)
  79. {
  80. global $conf;
  81. $dir = $conf->product->multidir_output[$product->entity];
  82. $conf->global->PRODUCT_USE_OLD_PATH_FOR_PHOTO = 1;
  83. $origin = $dir.'/'.get_exdir($product->id, 2, 0, 0, $product, 'product').$product->id."/photos";
  84. $destin = $dir.'/'.dol_sanitizeFileName($product->ref);
  85. $error = 0;
  86. $origin_osencoded = dol_osencode($origin);
  87. $destin_osencoded = dol_osencode($destin);
  88. dol_mkdir($destin);
  89. if (dol_is_dir($origin)) {
  90. $handle = opendir($origin_osencoded);
  91. if (is_resource($handle)) {
  92. while (($file = readdir($handle)) !== false) {
  93. if ($file != '.' && $file != '..' && is_dir($origin_osencoded.'/'.$file)) {
  94. $thumbs = opendir($origin_osencoded.'/'.$file);
  95. if (is_resource($thumbs)) {
  96. dol_mkdir($destin.'/'.$file);
  97. while (($thumb = readdir($thumbs)) !== false) {
  98. dol_move($origin.'/'.$file.'/'.$thumb, $destin.'/'.$file.'/'.$thumb);
  99. }
  100. // dol_delete_dir($origin.'/'.$file);
  101. }
  102. } else {
  103. if (dol_is_file($origin.'/'.$file)) {
  104. dol_move($origin.'/'.$file, $destin.'/'.$file);
  105. }
  106. }
  107. }
  108. }
  109. }
  110. }