Skip to content

Instantly share code, notes, and snippets.

@tracylemke
Forked from esimonetti/repair.php
Last active June 13, 2018 10:01
Show Gist options
  • Save tracylemke/ce25fae766a10426d6bb48514269998b to your computer and use it in GitHub Desktop.
Save tracylemke/ce25fae766a10426d6bb48514269998b to your computer and use it in GitHub Desktop.
Sugar CLI Repair - Instance as script argument
<?php
// Enrico Simonetti
// enricosimonetti.com
//
// 2017-11-01 on Sugar 7.9.2.0
function usage($error = '') {
if (!empty($error)) print(PHP_EOL . 'Error: ' . $error . PHP_EOL);
print(' php ' . __FILE__ . ' --instance /full/path' . PHP_EOL);
exit(1);
}
// only allow CLI
$sapi_type = php_sapi_name();
if (substr($sapi_type, 0, 3) != 'cli') {
die(__FILE__ . ' is CLI only.');
}
if (!file_exists('config.php') || !file_exists('sugar_version.php')) {
usage('The current directory is not a Sugar system');
}
// sugar basic setup
define('sugarEntry', true);
require_once('include/entryPoint.php');
$GLOBALS['current_user'] = BeanFactory::getBean('Users', '1');
if (extension_loaded('xdebug')) {
$GLOBALS['log']->fatal("Xdebug is enabled on this system. It is highly recommended to disable Xdebug on PHP CLI before running this script. " .
"Xdebug will cause unwanted slowness.");
}
// temporarily stop xdebug, xhprof and tideways if enabled
$GLOBALS['log']->fatal("Disabling Xdebug, if enabled.");
if (function_exists('xdebug_disable')) {
xdebug_disable();
}
$GLOBALS['log']->fatal("Disabling XHProf, if enabled.");
if(function_exists('xhprof_disable')) {
xhprof_disable();
xhprof_sample_disable();
}
$GLOBALS['log']->fatal("Disabling Tideways, if enabled.");
if (function_exists('tideways_disable')) {
tideways_disable();
}
if (empty($current_language)) {
$current_language = $sugar_config['default_language'];
}
$app_list_strings = return_app_list_strings_language($current_language);
$app_strings = return_application_language($current_language);
$mod_strings = return_module_language($current_language, 'Administration');
$start_time = microtime(true);
$GLOBALS['log']->fatal("Clearing cache.");
if (SugarCache::instance()->useBackend()) {
// clear cache
SugarCache::instance()->reset();
SugarCache::instance()->resetFull();
}
$GLOBALS['log']->fatal("Clearing opcache.");
SugarCache::cleanOpcodes();
// clear opcache before #79804
if (function_exists('opcache_reset')) {
opcache_reset();
}
require_once('modules/Administration/QuickRepairAndRebuild.php');
// remove team cache files
$files_to_remove = array(
'cache/modules/Teams/TeamSetMD5Cache.php',
'cache/modules/Teams/TeamSetCache.php'
);
$GLOBALS['log']->fatal("Deleting team cache files. " . print_r($files_to_remove,1));
foreach ($files_to_remove as $file) {
$file = SugarAutoloader::normalizeFilePath($file);
if (SugarAutoloader::fileExists($file)) {
SugarAutoloader::unlink($file);
}
}
$GLOBALS['log']->fatal("Rebuilding class map cache first to be aware of new files.");
SugarAutoLoader::buildCache();
$GLOBALS['log']->fatal("Repairing all modules.");
// repair
$repair = new RepairAndClear();
$repair->repairAndClearAll(array('clearAll'), array($mod_strings['LBL_ALL_MODULES']), true, false, '');
$GLOBALS['log']->fatal("Removing any cached js language strings.");
// remove some stuff
LanguageManager::removeJSLanguageFiles();
LanguageManager::clearLanguageCache();
$cache = new MetaDataCache(DBManagerFactory::getInstance());
$cache->reset();
// rebuild some stuff
$GLOBALS['log']->fatal("Setting up metadata caches for various platforms and languages.");
SugarAutoLoader::buildCache();
MetaDataManager::setupMetadata(array('base'), array($current_language));
// quick load of all beans
$GLOBALS['log']->fatal("Quick load all beans and create respective language files.");
global $beanList;
$full_module_list = array_merge($beanList, $app_list_strings['moduleList']);
foreach ($full_module_list as $module => $label) {
$bean = BeanFactory::newBean($module);
// load language too
LanguageManager::createLanguageFile($module, array('default'), true);
$mod_strings = return_module_language($current_language, $module);
}
// load app strings
$app_list_strings = return_app_list_strings_language($current_language);
$app_strings = return_application_language($current_language);
// load api
$GLOBALS['log']->fatal("Building all dictionaries for known service types.");
$sd = new ServiceDictionary();
$sd->buildAllDictionaries();
/** Enable the rebuilding of relationships if relationship cache has a problem*/
//$GLOBALS['log']->fatal("Rebuilding relationship files.");
//SugarRelationshipFactory::rebuildCache();
$GLOBALS['log']->fatal('Completed in ' . (int)(microtime(true) - $start_time) . ' seconds.');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment