migrate_picture_path.php 4.2 KB

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