restore_deleted_documents.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Script to restore some deleted documents
  5. */
  6. use Chamilo\CourseBundle\Entity\CDocument;
  7. use Chamilo\CourseBundle\Entity\CItemProperty;
  8. exit;
  9. require __DIR__.'/../../main/inc/global.inc.php';
  10. api_protect_admin_script();
  11. $em = Database::getManager();
  12. $cId = 0;
  13. $path = '%_DELETED_%';
  14. //// --->
  15. $currentUserId = api_get_user_id();
  16. $course = api_get_course_entity($cId);
  17. $courseDirectory = api_get_path(SYS_COURSE_PATH).$course->getDirectory().'/document';
  18. $documents = $em
  19. ->createQuery(
  20. 'SELECT d FROM ChamiloCourseBundle:CDocument d
  21. WHERE d.cId = :cid AND d.path LIKE :path AND d.sessionId = 0'
  22. )
  23. ->setParameters(['cid' => (int) $cId, 'path' => $path])
  24. ->getResult();
  25. header('Content-Type: text/plain');
  26. /** @var CDocument $document */
  27. foreach ($documents as $document) {
  28. $properties = $em
  29. ->createQuery(
  30. 'SELECT i FROM ChamiloCourseBundle:CItemProperty i
  31. WHERE i.course = :course AND i.session IS NULL AND i.ref = :ref'
  32. )
  33. ->setParameters(['course' => $course, 'ref' => $document->getIid()])
  34. ->getResult();
  35. /** @var CItemProperty $property */
  36. foreach ($properties as $property) {
  37. if (!in_array($property->getLasteditType(), ['DocumentDeleted', 'delete'])) {
  38. continue;
  39. }
  40. switch ($property->getLasteditType()) {
  41. case 'DocumentDeleted':
  42. echo "Changing 'deleted' log to 'added' log".PHP_EOL;
  43. $property
  44. ->setLasteditType('DocumentAdded')
  45. ->setLasteditUserId($currentUserId)
  46. ->setLasteditDate(
  47. api_get_utc_datetime(null, false, true)
  48. )
  49. ->setVisibility(1);
  50. $em->persist($property);
  51. break;
  52. case 'delete':
  53. echo "Removing delete log".PHP_EOL;
  54. $em->remove($property);
  55. break;
  56. }
  57. }
  58. $filePath = $courseDirectory.$document->getPath();
  59. $newPath = str_replace('_DELETED_'.$document->getIid(), '', $document->getPath());
  60. $newFilePath = $courseDirectory.$newPath;
  61. $document->setPath($newPath);
  62. $em->flush();
  63. $renaming = rename($filePath, $newFilePath);
  64. echo "Renaming $filePath to $newFilePath : ";
  65. echo $renaming ? 'y' : 'n';
  66. echo PHP_EOL;
  67. }