| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | Copyright CiviCRM LLC. All rights reserved. | |
| 5 | | | |
| 6 | | This work is published under the GNU AGPLv3 license with some | |
| 7 | | permitted exceptions and without any warranty. For full license | |
| 8 | | and copyright information, see https://civicrm.org/licensing | |
| 9 | +--------------------------------------------------------------------+ |
| 10 | */ |
| 11 | |
| 12 | /** |
| 13 | * Perform an upgrade without using the web-frontend |
| 14 | */ |
| 15 | class CRM_Upgrade_Headless { |
| 16 | |
| 17 | /** |
| 18 | * Perform an upgrade without using the web-frontend |
| 19 | * |
| 20 | * @param bool $enablePrint |
| 21 | * |
| 22 | * @throws Exception |
| 23 | * @return array |
| 24 | * - with keys: |
| 25 | * - message: string, HTML-ish blob |
| 26 | */ |
| 27 | public function run($enablePrint = TRUE) { |
| 28 | set_time_limit(0); |
| 29 | |
| 30 | $upgrade = new CRM_Upgrade_Form(); |
| 31 | list($currentVer, $latestVer) = $upgrade->getUpgradeVersions(); |
| 32 | |
| 33 | if ($error = $upgrade->checkUpgradeableVersion($currentVer, $latestVer)) { |
| 34 | throw new Exception($error); |
| 35 | } |
| 36 | |
| 37 | // Disable our SQL triggers |
| 38 | CRM_Core_DAO::dropTriggers(); |
| 39 | |
| 40 | // CRM-11156 |
| 41 | $preUpgradeMessage = NULL; |
| 42 | $upgrade->setPreUpgradeMessage($preUpgradeMessage, $currentVer, $latestVer); |
| 43 | |
| 44 | $postUpgradeMessageFile = CRM_Utils_File::tempnam('civicrm-post-upgrade'); |
| 45 | $queueRunner = new CRM_Queue_Runner([ |
| 46 | 'title' => ts('CiviCRM Upgrade Tasks'), |
| 47 | 'queue' => CRM_Upgrade_Form::buildQueue($currentVer, $latestVer, $postUpgradeMessageFile), |
| 48 | ]); |
| 49 | $queueResult = $queueRunner->runAll(); |
| 50 | if ($queueResult !== TRUE) { |
| 51 | $errorMessage = CRM_Core_Error::formatTextException($queueResult['exception']); |
| 52 | CRM_Core_Error::debug_log_message($errorMessage); |
| 53 | if ($enablePrint) { |
| 54 | print ($errorMessage); |
| 55 | } |
| 56 | // FIXME test |
| 57 | throw $queueResult['exception']; |
| 58 | } |
| 59 | |
| 60 | CRM_Upgrade_Form::doFinish(); |
| 61 | |
| 62 | $message = file_get_contents($postUpgradeMessageFile); |
| 63 | return [ |
| 64 | 'latestVer' => $latestVer, |
| 65 | 'message' => $message, |
| 66 | 'text' => CRM_Utils_String::htmlToText($message), |
| 67 | ]; |
| 68 | } |
| 69 | |
| 70 | } |