ecmdatabase.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. require '../../main.inc.php';
  34. require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  35. $action = GETPOST('action', 'aZ09');
  36. $element = GETPOST('element', 'alpha');
  37. /*
  38. * View
  39. */
  40. top_httphead();
  41. //print '<!-- Ajax page called with url '.dol_escape_htmltag($_SERVER["PHP_SELF"]).'?'.dol_escape_htmltag($_SERVER["QUERY_STRING"]).' -->'."\n";
  42. // Load original field value
  43. if (isset($action) && !empty($action)) {
  44. $error = 0;
  45. if ($action == 'build' && !empty($element)) {
  46. require_once DOL_DOCUMENT_ROOT.'/ecm/class/ecmdirectory.class.php';
  47. $ecmdirstatic = new EcmDirectory($db);
  48. $ecmdirtmp = new EcmDirectory($db);
  49. // This part of code is same than into file index.php for action refreshmanual TODO Remove duplicate
  50. clearstatcache();
  51. $diroutputslash = str_replace('\\', '/', $conf->$element->dir_output);
  52. $diroutputslash .= '/';
  53. // Scan directory tree on disk
  54. $disktree = dol_dir_list($conf->$element->dir_output, 'directories', 1, '', array('^temp$'), '', '', 0);
  55. // Scan directory tree in database
  56. $sqltree = $ecmdirstatic->get_full_arbo(0);
  57. $adirwascreated = 0;
  58. // Now we compare both trees to complete missing trees into database
  59. //var_dump($disktree);
  60. //var_dump($sqltree);
  61. foreach ($disktree as $dirdesc) { // Loop on tree onto disk
  62. set_time_limit(0); // To force restarts the timeout counter from zero
  63. $dirisindatabase = 0;
  64. foreach ($sqltree as $dirsqldesc) {
  65. if ($conf->$element->dir_output.'/'.$dirsqldesc['fullrelativename'] == $dirdesc['fullname']) {
  66. $dirisindatabase = 1;
  67. break;
  68. }
  69. }
  70. if (!$dirisindatabase) {
  71. $txt = "Directory found on disk ".$dirdesc['fullname'].", not found into table ecm_directories, so we add it";
  72. dol_syslog($txt);
  73. // We must first find the fk_parent of directory to create $dirdesc['fullname']
  74. $fk_parent = -1;
  75. $relativepathmissing = str_replace($diroutputslash, '', $dirdesc['fullname']);
  76. $relativepathtosearchparent = $relativepathmissing;
  77. //dol_syslog("Try to find parent id for directory ".$relativepathtosearchparent);
  78. if (preg_match('/\//', $relativepathtosearchparent)) {
  79. //while (preg_match('/\//',$relativepathtosearchparent))
  80. $relativepathtosearchparent = preg_replace('/\/[^\/]*$/', '', $relativepathtosearchparent);
  81. $txt = "Is relative parent path ".$relativepathtosearchparent." for ".$relativepathmissing." found in sql tree ?";
  82. dol_syslog($txt);
  83. //print $txt." -> ";
  84. $parentdirisindatabase = 0;
  85. foreach ($sqltree as $dirsqldesc) {
  86. if ($dirsqldesc['fullrelativename'] == $relativepathtosearchparent) {
  87. $parentdirisindatabase = $dirsqldesc['id'];
  88. break;
  89. }
  90. }
  91. if ($parentdirisindatabase > 0) {
  92. dol_syslog("Yes with id ".$parentdirisindatabase);
  93. //print "Yes with id ".$parentdirisindatabase."<br>\n";
  94. $fk_parent = $parentdirisindatabase;
  95. //break; // We found parent, we can stop the while loop
  96. } else {
  97. dol_syslog("No");
  98. //print "No<br>\n";
  99. }
  100. } else {
  101. dol_syslog("Parent is root");
  102. $fk_parent = 0; // Parent is root
  103. }
  104. if ($fk_parent >= 0) {
  105. $ecmdirtmp->ref = 'NOTUSEDYET';
  106. $ecmdirtmp->label = dol_basename($dirdesc['fullname']);
  107. $ecmdirtmp->description = '';
  108. $ecmdirtmp->fk_parent = $fk_parent;
  109. $txt = "We create directory ".$ecmdirtmp->label." with parent ".$fk_parent;
  110. dol_syslog($txt);
  111. //print $txt."<br>\n";
  112. $id = $ecmdirtmp->create($user);
  113. if ($id > 0) {
  114. $newdirsql = array('id'=>$id,
  115. 'id_mere'=>$ecmdirtmp->fk_parent,
  116. 'label'=>$ecmdirtmp->label,
  117. 'description'=>$ecmdirtmp->description,
  118. 'fullrelativename'=>$relativepathmissing);
  119. $sqltree[] = $newdirsql; // We complete fulltree for following loops
  120. //var_dump($sqltree);
  121. $adirwascreated = 1;
  122. } else {
  123. dol_syslog("Failed to create directory ".$ecmdirtmp->label, LOG_ERR);
  124. }
  125. } else {
  126. $txt = "Parent of ".$dirdesc['fullname']." not found";
  127. dol_syslog($txt);
  128. //print $txt."<br>\n";
  129. }
  130. }
  131. }
  132. // Loop now on each sql tree to check if dir exists
  133. foreach ($sqltree as $dirdesc) { // Loop on each sqltree to check dir is on disk
  134. $dirtotest = $conf->$element->dir_output.'/'.$dirdesc['fullrelativename'];
  135. if (!dol_is_dir($dirtotest)) {
  136. dol_syslog($dirtotest." not found onto disk. We delete from database dir with id=".$dirdesc['id']);
  137. $ecmdirtmp->id = $dirdesc['id'];
  138. $ecmdirtmp->delete($user, 'databaseonly');
  139. //exit;
  140. }
  141. }
  142. dol_syslog("Nb of directories added into database = ".$adirwascreated);
  143. $sql = "UPDATE ".MAIN_DB_PREFIX."ecm_directories set cachenbofdoc = -1 WHERE cachenbofdoc < 0"; // If pb into cache counting, we set to value -1 = "unknown"
  144. $db->query($sql);
  145. }
  146. }