From 9e799b1d124aee9f90b479aff8ae082645d97bf4 Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Tue, 27 Jun 2017 00:27:09 -0700 Subject: [PATCH] CRM-20243 - CRM_Upgrade_Form - Delete orphaned files before upgrade --- CRM/Upgrade/Form.php | 48 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/CRM/Upgrade/Form.php b/CRM/Upgrade/Form.php index d49776861c..40c04a1491 100644 --- a/CRM/Upgrade/Form.php +++ b/CRM/Upgrade/Form.php @@ -567,6 +567,13 @@ SET version = '$version' 'reset' => TRUE, )); + $task = new CRM_Queue_Task( + array('CRM_Upgrade_Form', 'doFileCleanup'), + array($postUpgradeMessageFile), + "Cleanup old files" + ); + $queue->createItem($task); + $revisions = $upgrade->getRevisionSequence(); foreach ($revisions as $rev) { // proceed only if $currentVer < $rev @@ -603,6 +610,47 @@ SET version = '$version' return $queue; } + /** + * Find any old, orphaned files that should have been deleted. + * + * These files can get left behind, eg, if you use the Joomla + * upgrade procedure. + * + * The earlier we can do this, the better - don't want upgrade logic + * to inadvertently rely on old/relocated files. + * + * @param \CRM_Queue_TaskContext $ctx + * @param string $postUpgradeMessageFile + * @return bool + */ + public static function doFileCleanup(CRM_Queue_TaskContext $ctx, $postUpgradeMessageFile) { + $source = new CRM_Utils_Check_Component_Source(); + $files = $source->findOrphanedFiles(); + $errors = array(); + foreach ($files as $file) { + if (is_dir($file['path'])) { + @rmdir($file['path']); + } + else { + @unlink($file['path']); + } + + if (file_exists($file['path'])) { + $errors[] = sprintf("
  • %s
  • ", htmlentities($file['path'])); + } + } + + if (!empty($errors)) { + file_put_contents($postUpgradeMessageFile, + '

    ' . ts('Some old files could not be removed. Please remove them.') + . '', + FILE_APPEND + ); + } + + return TRUE; + } + /** * Perform an incremental version update. * -- 2.25.1