cleanupJobs(); $this->cleanupScheduledJobLog(); $this->cleanupAttachments(); $this->cleanupEmails(); $this->cleanupNotes(); $this->cleanupNotifications(); $this->cleanupActionHistory(); $this->cleanupAuthToken(); $this->cleanupUpgradeBackups(); } protected function cleanupJobs() { $query = "DELETE FROM `job` WHERE DATE(modified_at) < '".$this->getCleanupJobFromDate()."' AND status <> 'Pending'"; $pdo = $this->getEntityManager()->getPDO(); $sth = $pdo->prepare($query); $sth->execute(); } protected function cleanupScheduledJobLog() { $pdo = $this->getEntityManager()->getPDO(); $sql = "SELECT id FROM scheduled_job"; $sth = $pdo->prepare($sql); $sth->execute(); while ($row = $sth->fetch(\PDO::FETCH_ASSOC)) { $id = $row['id']; $lastRowsSql = "SELECT id FROM scheduled_job_log_record WHERE scheduled_job_id = '".$id."' ORDER BY created_at DESC LIMIT 0,10"; $lastRowsSth = $pdo->prepare($lastRowsSql); $lastRowsSth->execute(); $lastRowIds = $lastRowsSth->fetchAll(\PDO::FETCH_COLUMN, 0); $delSql = "DELETE FROM `scheduled_job_log_record` WHERE scheduled_job_id = '".$id."' AND DATE(created_at) < '".$this->getCleanupJobFromDate()."' AND id NOT IN ('".implode("', '", $lastRowIds)."') "; $pdo->query($delSql); } } protected function cleanupActionHistory() { $period = '-' . $this->getConfig()->get('cleanupActionHistoryPeriod', $this->cleanupActionHistoryPeriod); $datetime = new \DateTime(); $datetime->modify($period); $query = "DELETE FROM `action_history_record` WHERE DATE(created_at) < '" . $datetime->format('Y-m-d') . "'"; $pdo = $this->getEntityManager()->getPDO(); $sth = $pdo->prepare($query); $sth->execute(); } protected function cleanupReminders() { $period = '-' . $this->getConfig()->get('cleanupRemindersPeriod', $this->cleanupRemindersPeriod); $datetime = new \DateTime(); $datetime->modify($period); $query = "DELETE FROM `reminder` WHERE DATE(remind_at) < '" . $datetime->format('Y-m-d') . "'"; $pdo = $this->getEntityManager()->getPDO(); $sth = $pdo->prepare($query); $sth->execute(); } protected function cleanupAuthToken() { $period = '-' . $this->getConfig()->get('cleanupAuthTokenPeriod', $this->cleanupAuthTokenPeriod); $datetime = new \DateTime(); $datetime->modify($period); $query = "DELETE FROM `auth_token` WHERE DATE(modified_at) < '" . $datetime->format('Y-m-d') . "' AND is_active = 0"; $pdo = $this->getEntityManager()->getPDO(); $sth = $pdo->prepare($query); $sth->execute(); } protected function getCleanupJobFromDate() { $period = '-' . $this->getConfig()->get('cleanupJobPeriod', $this->cleanupJobPeriod); $datetime = new \DateTime(); $datetime->modify($period); return $datetime->format('Y-m-d'); } protected function cleanupAttachments() { $pdo = $this->getEntityManager()->getPDO(); $period = '-' . $this->getConfig()->get('cleanupAttachmentsPeriod', $this->cleanupAttachmentsPeriod); $datetime = new \DateTime(); $datetime->modify($period); $collection = $this->getEntityManager()->getRepository('Attachment')->where(array( 'OR' => array( array( 'role' => ['Export File'] ) ), 'createdAt<' => $datetime->format('Y-m-d H:i:s') ))->limit(0, 1000)->find(); foreach ($collection as $e) { $this->getEntityManager()->removeEntity($e); } if ($this->getConfig()->get('cleanupOrphanAttachments')) { $collection = $this->getEntityManager()->getRepository('Attachment')->where(array( array( 'role' => 'Attachment' ), 'OR' => array( array( 'parentId' => null, 'parentType!=' => null, 'relatedType=' => null ), array( 'parentType' => null, 'relatedId' => null, 'relatedType!=' => null ) ), 'createdAt<' => $datetime->format('Y-m-d H:i:s'), 'createdAt>' => '2017-05-10 00:00:00' ))->limit(0, 1000)->find(); foreach ($collection as $e) { $this->getEntityManager()->removeEntity($e); } } $fromPeriod = '-' . $this->getConfig()->get('cleanupAttachmentsFromPeriod', $this->cleanupAttachmentsFromPeriod); $datetimeFrom = new \DateTime(); $datetimeFrom->modify($fromPeriod); $scopeList = array_keys($this->getMetadata()->get(['scopes'])); foreach ($scopeList as $scope) { if (!$this->getMetadata()->get(['scopes', $scope, 'entity'])) continue; if (!$this->getMetadata()->get(['scopes', $scope, 'object']) && $scope !== 'Note') continue; if (!$this->getMetadata()->get(['entityDefs', $scope, 'fields', 'modifiedAt'])) continue; $hasAttachmentField = false; if ($scope === 'Note') { $hasAttachmentField = true; } if (!$hasAttachmentField) { foreach ($this->getMetadata()->get(['entityDefs', $scope, 'fields']) as $field => $defs) { if (empty($defs['type'])) continue; if (in_array($defs['type'], ['file', 'image', 'attachmentMultiple'])) { $hasAttachmentField = true; break; } } } if (!$hasAttachmentField) continue; $deletedEntityList = $this->getEntityManager()->getRepository($scope)->where([ 'deleted' => 1, 'modifiedAt<' => $datetime->format('Y-m-d H:i:s'), 'modifiedAt>' => $datetimeFrom->format('Y-m-d H:i:s'), ])->find(['withDeleted' => true]); foreach ($deletedEntityList as $deletedEntity) { $attachmentToRemoveList = $this->getEntityManager()->getRepository('Attachment')->where(array( 'OR' => array( array( 'relatedType' => $scope, 'relatedId' => $deletedEntity->id ), array( 'parentType' => $scope, 'parentId' => $deletedEntity->id ) ) ))->find(); foreach ($attachmentToRemoveList as $attachmentToRemove) { $this->getEntityManager()->removeEntity($attachmentToRemove); } } } $sql = "DELETE FROM attachment WHERE deleted = 1 AND created_at < ".$pdo->quote($datetime->format('Y-m-d H:i:s')); $sth = $pdo->query($sql); } protected function cleanupEmails() { $pdo = $this->getEntityManager()->getPDO(); $dateBefore = date('Y-m-d H:i:s', time() - 3600 * 24 * 20); $sql = "SELECT * FROM email WHERE deleted = 1 AND created_at < ".$pdo->quote($dateBefore); $sth = $pdo->prepare($sql); $sth->execute(); while ($row = $sth->fetch(\PDO::FETCH_ASSOC)) { $id = $row['id']; $attachments = $this->getEntityManager()->getRepository('Attachment')->where(array( 'parentId' => $id, 'parentType' => 'Email' ))->find(); foreach ($attachments as $attachment) { $this->getEntityManager()->removeEntity($attachment); } $sqlDel = "DELETE FROM email WHERE deleted = 1 AND id = ".$pdo->quote($id); $pdo->query($sqlDel); $sqlDel = "DELETE FROM email_user WHERE email_id = ".$pdo->quote($id); $pdo->query($sqlDel); } } protected function cleanupNotes() { $pdo = $this->getEntityManager()->getPDO(); $period = '-' . $this->getConfig()->get('cleanupRemovedNotesPeriod', $this->cleanupRemovedNotesPeriod); $datetime = new \DateTime(); $datetime->modify($period); $sql = "SELECT * FROM `note` WHERE deleted = 1 AND DATE(created_at) < ".$pdo->quote($datetime->format('Y-m-d')); $sth = $pdo->prepare($sql); $sth->execute(); while ($row = $sth->fetch(\PDO::FETCH_ASSOC)) { $id = $row['id']; $attachments = $this->getEntityManager()->getRepository('Attachment')->where(array( 'parentId' => $id, 'parentType' => 'Note' ))->find(); foreach ($attachments as $attachment) { $this->getEntityManager()->removeEntity($attachment); } $sqlDel = "DELETE FROM `note` WHERE deleted = 1 AND id = ".$pdo->quote($id); $pdo->query($sqlDel); } } protected function cleanupNotifications() { $pdo = $this->getEntityManager()->getPDO(); $period = '-' . $this->getConfig()->get('cleanupNotificationsPeriod', $this->cleanupNotificationsPeriod); $datetime = new \DateTime(); $datetime->modify($period); $sql = "SELECT * FROM `notification` WHERE DATE(created_at) < ".$pdo->quote($datetime->format('Y-m-d')); $sth = $pdo->prepare($sql); $sth->execute(); while ($row = $sth->fetch(\PDO::FETCH_ASSOC)) { $id = $row['id']; $this->getEntityManager()->getRepository('Notification')->deleteFromDb($id); } } protected function cleanupUpgradeBackups() { $path = 'data/.backup/upgrades'; $datetime = new \DateTime('-' . $this->cleanupBackupPeriod); if (file_exists($path)) { $fileManager = $this->getContainer()->get('fileManager'); $fileList = $fileManager->getFileList($path, false, '', false); foreach ($fileList as $dirName) { $dirPath = $path . '/' . $dirName; $info = new \SplFileInfo($dirPath); if ($datetime->getTimestamp() > $info->getMTime()) { $fileManager->removeInDir($dirPath, true); } } } } }