Skip to content

Instantly share code, notes, and snippets.

@esimonetti
Last active October 30, 2019 08:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save esimonetti/e42ef2fa6ad069368365ead733545a20 to your computer and use it in GitHub Desktop.
Save esimonetti/e42ef2fa6ad069368365ead733545a20 to your computer and use it in GitHub Desktop.
Utility to extract Sugar's metadata content to be able to compare two different metadatas - USE TOOTHPASTE INSTEAD! https://github.com/esimonetti/toothpaste
<?php
// Enrico Simonetti
// enricosimonetti.com
//
// 2019-09-26 on Sugar 9.0.0
//
// Metadata extractor, to compare 2 metadatas with a diff command
// eg: diff -auNw file1.array file2.array)
function usage($error = '') {
if (!empty($error)) print(PHP_EOL . 'Error: ' . $error . PHP_EOL);
print(' php ' . __FILE__ . ' --instance /full/path --destination /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.');
}
// get command line params
$o = getopt('', array('instance:', 'destination:'));
if (!$o) usage();
// find directory
if (!empty($o['instance']) && is_dir($o['instance'])) {
print('Debug: Entering directory ' . $o['instance'] . PHP_EOL);
chdir($o['instance']);
} else {
chdir(dirname(__FILE__));
}
if (!file_exists('config.php') || !file_exists('sugar_version.php')) {
usage('The provided directory is not a Sugar system');
}
if (empty($o['destination'])) {
usage('Please provide a destination directory to save the metadata outputs');
}
$o['destination'] = rtrim($o['destination'], '/') . '/';
if (!is_dir($o['destination'])) {
usage('Please provide a valid destination directory to save the metadata outputs. ' . $o['destination'] . ' is invalid.');
}
// sugar basic setup
define('sugarEntry', true);
require_once('include/entryPoint.php');
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');
global $current_user;
$current_user = BeanFactory::getBean('Users');
$current_user->getSystemUser();
$start_time = microtime(true);
$qb = DBManagerFactory::getConnection()->createQueryBuilder();
$qb->select(['type', 'data'])
->from('metadata_cache')
->where($qb->expr()->eq('deleted', $qb->createPositionalParameter(0)))
->orderBy('type');
$res = $qb->execute();
$results = [];
while ($row = $res->fetch()) {
$row['data'] = unserialize(gzinflate(base64_decode($row['data'])));
$results[] = $row;
}
$date = gmdate('Y_m_d_H_i_s');
echo 'Extracting metadata within: ' . $o['destination'] . PHP_EOL;
file_put_contents($o['destination'] . 'metadata_' . $date . '.json', json_encode($results));
file_put_contents($o['destination'] . 'metadata_' . $date . '.array', print_r($results, true));
echo 'Extraction of metadata completed in ' . round(microtime(true) - $start_time, 2) . ' seconds.' . PHP_EOL;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment