ecmdatabase.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. /* Copyright (C) 2012 Regis Houssin <regis.houssin@inodbox.com>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. */
  17. /**
  18. * \file htdocs/ecm/ajax/ecmdatabase.php
  19. * \brief File to build/refresh the ecm database for directories
  20. */
  21. if (!defined('NOTOKENRENEWAL')) {
  22. define('NOTOKENRENEWAL', '1'); // Disables token renewal
  23. }
  24. if (!defined('NOREQUIREMENU')) {
  25. define('NOREQUIREMENU', '1');
  26. }
  27. if (!defined('NOREQUIREAJAX')) {
  28. define('NOREQUIREAJAX', '1');
  29. }
  30. if (!defined('NOREQUIRESOC')) {
  31. define('NOREQUIRESOC', '1');
  32. }
  33. // Load Dolibarr environment
  34. require '../../main.inc.php';
  35. require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  36. $action = GETPOST('action', 'aZ09');
  37. $element = GETPOST('element', 'alpha');
  38. /*
  39. * View
  40. */
  41. top_httphead();
  42. //print '<!-- Ajax page called with url '.dol_escape_htmltag($_SERVER["PHP_SELF"]).'?'.dol_escape_htmltag($_SERVER["QUERY_STRING"]).' -->'."\n";
  43. // Load original field value
  44. if (isset($action) && !empty($action)) {
  45. $error = 0;
  46. if ($action == 'build' && !empty($element)) {
  47. require_once DOL_DOCUMENT_ROOT.'/ecm/class/ecmdirectory.class.php';
  48. $ecmdirstatic = new EcmDirectory($db);
  49. $ecmdirtmp = new EcmDirectory($db);
  50. // This part of code is same than into file index.php for action refreshmanual TODO Remove duplicate
  51. clearstatcache();
  52. $diroutputslash = str_replace('\\', '/', $conf->$element->dir_output);
  53. $diroutputslash .= '/';
  54. // Scan directory tree on disk
  55. $disktree = dol_dir_list($conf->$element->dir_output, 'directories', 1, '', array('^temp$'), '', '', 0);
  56. // Scan directory tree in database
  57. $sqltree = $ecmdirstatic->get_full_arbo(0);
  58. $adirwascreated = 0;
  59. // Now we compare both trees to complete missing trees into database
  60. //var_dump($disktree);
  61. //var_dump($sqltree);
  62. foreach ($disktree as $dirdesc) { // Loop on tree onto disk
  63. set_time_limit(0); // To force restarts the timeout counter from zero
  64. $dirisindatabase = 0;
  65. foreach ($sqltree as $dirsqldesc) {
  66. if ($conf->$element->dir_output.'/'.$dirsqldesc['fullrelativename'] == $dirdesc['fullname']) {
  67. $dirisindatabase = 1;
  68. break;
  69. }
  70. }
  71. if (!$dirisindatabase) {
  72. $txt = "Directory found on disk ".$dirdesc['fullname'].", not found into table ecm_directories, so we add it";
  73. dol_syslog($txt);
  74. // We must first find the fk_parent of directory to create $dirdesc['fullname']
  75. $fk_parent = -1;
  76. $relativepathmissing = str_replace($diroutputslash, '', $dirdesc['fullname']);
  77. $relativepathtosearchparent = $relativepathmissing;
  78. //dol_syslog("Try to find parent id for directory ".$relativepathtosearchparent);
  79. if (preg_match('/\//', $relativepathtosearchparent)) {
  80. //while (preg_match('/\//',$relativepathtosearchparent))
  81. $relativepathtosearchparent = preg_replace('/\/[^\/]*$/', '', $relativepathtosearchparent);
  82. $txt = "Is relative parent path ".$relativepathtosearchparent." for ".$relativepathmissing." found in sql tree ?";
  83. dol_syslog($txt);
  84. //print $txt." -> ";
  85. $parentdirisindatabase = 0;
  86. foreach ($sqltree as $dirsqldesc) {
  87. if ($dirsqldesc['fullrelativename'] == $relativepathtosearchparent) {
  88. $parentdirisindatabase = $dirsqldesc['id'];
  89. break;
  90. }
  91. }
  92. if ($parentdirisindatabase > 0) {
  93. dol_syslog("Yes with id ".$parentdirisindatabase);
  94. //print "Yes with id ".$parentdirisindatabase."<br>\n";
  95. $fk_parent = $parentdirisindatabase;
  96. //break; // We found parent, we can stop the while loop
  97. } else {
  98. dol_syslog("No");
  99. //print "No<br>\n";
  100. }
  101. } else {
  102. dol_syslog("Parent is root");
  103. $fk_parent = 0; // Parent is root
  104. }
  105. if ($fk_parent >= 0) {
  106. $ecmdirtmp->ref = 'NOTUSEDYET';
  107. $ecmdirtmp->label = dol_basename($dirdesc['fullname']);
  108. $ecmdirtmp->description = '';
  109. $ecmdirtmp->fk_parent = $fk_parent;
  110. $txt = "We create directory ".$ecmdirtmp->label." with parent ".$fk_parent;
  111. dol_syslog($txt);
  112. //print $txt."<br>\n";
  113. $id = $ecmdirtmp->create($user);
  114. if ($id > 0) {
  115. $newdirsql = array('id'=>$id,
  116. 'id_mere'=>$ecmdirtmp->fk_parent,
  117. 'label'=>$ecmdirtmp->label,
  118. 'description'=>$ecmdirtmp->description,
  119. 'fullrelativename'=>$relativepathmissing);
  120. $sqltree[] = $newdirsql; // We complete fulltree for following loops
  121. //var_dump($sqltree);
  122. $adirwascreated = 1;
  123. } else {
  124. dol_syslog("Failed to create directory ".$ecmdirtmp->label, LOG_ERR);
  125. }
  126. } else {
  127. $txt = "Parent of ".$dirdesc['fullname']." not found";
  128. dol_syslog($txt);
  129. //print $txt."<br>\n";
  130. }
  131. }
  132. }
  133. // Loop now on each sql tree to check if dir exists
  134. foreach ($sqltree as $dirdesc) { // Loop on each sqltree to check dir is on disk
  135. $dirtotest = $conf->$element->dir_output.'/'.$dirdesc['fullrelativename'];
  136. if (!dol_is_dir($dirtotest)) {
  137. dol_syslog($dirtotest." not found onto disk. We delete from database dir with id=".$dirdesc['id']);
  138. $ecmdirtmp->id = $dirdesc['id'];
  139. $ecmdirtmp->delete($user, 'databaseonly');
  140. //exit;
  141. }
  142. }
  143. dol_syslog("Nb of directories added into database = ".$adirwascreated);
  144. $sql = "UPDATE ".MAIN_DB_PREFIX."ecm_directories set cachenbofdoc = -1 WHERE cachenbofdoc < 0"; // If pb into cache counting, we set to value -1 = "unknown"
  145. $db->query($sql);
  146. }
  147. }