Skip to content

Instantly share code, notes, and snippets.

@mmenozzi
Last active June 11, 2020 08:49
Show Gist options
  • Save mmenozzi/e804da411647441dc4d6196ce5cd7b83 to your computer and use it in GitHub Desktop.
Save mmenozzi/e804da411647441dc4d6196ce5cd7b83 to your computer and use it in GitHub Desktop.
Magento 1.x Upgrade Check Assistant Script
<?php
declare(strict_types=1);
/**
* Magento 1.x Upgrade Check Assistant Script
*
* To use this generate a rewrites JSON file with
*
* n98-magerun.phar dev:module:rewrite:list --format json > rewrites.json
*
* Then put all changed files from the upgrade in a file called `diff.txt`.
* Then run MAGE_DIR=magento/root/path THEME_DIR=magento/root/path/app/design/frontend/yourtheme/default php upgrade_check.php
*
*/
$mageDir = rtrim(getenv('MAGE_DIR') ?: '.', '/');
$magePath = $mageDir . '/app/Mage.php';
if (!file_exists($magePath)) {
echo "Invalid MAGE_DIR, Mage.php cannot be found. Please, define a valid MAGE_DIR env var.";
exit(1);
}
$themeDir = rtrim(getenv('THEME_DIR'), '/');
if (empty($themeDir) || !is_dir($themeDir)) {
echo "Please, define a valid THEME_DIR env var.";
exit(1);
}
include __DIR__ . '/vendor/autoload.php';
// -------------------------------------------------
// Prevent script to be called via HTTP
// -------------------------------------------------
if (isset($_SERVER['REQUEST_METHOD'])) {
die('This script cannot be run from Browser. This is the shell script.');
}
// -------------------------------------------------
// Override useful ini values
// -------------------------------------------------
set_time_limit(0); // avoid any time limit
ini_set('memory_limit', '-1'); // avoid any memory limit
// -------------------------------------------------
// Include Magento Core
// -------------------------------------------------
require_once $magePath;
// -------------------------------------------------
// Load and Start Magento Application runtime
// -------------------------------------------------
// Trick reference:
// http://www.magentocommerce.com/boards/viewthread/38379/#t189142
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
umask(0);
$rewritesFilename = 'rewrites.json';
if (!file_exists($rewritesFilename)) {
echo 'Rewrites file ' . $rewritesFilename . ' does not exist!' . PHP_EOL;
exit(1);
}
$diffFilename = 'diff.txt';
if (!file_exists($diffFilename)) {
echo 'Diff file ' . $diffFilename . ' does not exist!' . PHP_EOL;
exit(1);
}
$rewrites = json_decode(file_get_contents($rewritesFilename), true);
$diff = file_get_contents($diffFilename);
foreach ($rewrites as $rewrite) {
$group = rtrim($rewrite['Type'], 's');
$alias = $rewrite['Class'];
$rewriteTo = $rewrite['Rewrite'];
if (!in_array($group, ['model', 'block', 'helper'])) {
echo sprintf(
'* Unsupported rewrite type "%s" for alias "%s" rewritten to "%s", please manually check',
$group,
$alias,
$rewriteTo
) . PHP_EOL;
continue;
}
$className = getGroupedClassName($group, $alias);
try {
$reflector = new \ReflectionClass($className);
} catch (\ReflectionException $e) {
echo sprintf(
'* Cannot find original class "%s" for the alias "%s" rewritten to "%s", please manually check',
$className,
$alias,
$rewriteTo
) . PHP_EOL;
continue;
}
$fileName = ltrim(str_replace(BP, '', $reflector->getFileName()), '/');
if (strpos($diff, $fileName) !== false) {
echo sprintf(
'* File "%s" found in diff and has been rewritten to "%s", please manually check',
$fileName,
$rewriteTo
) . PHP_EOL;
}
}
$templates = rglob($themeDir . '/template/*.phtml');
foreach ($templates as $template) {
$template = str_replace($themeDir . '/template/', '', $template);
if (strpos($diff, $template) !== false) {
echo sprintf(
'* Template "%s" found in diff and has been rewritten in theme, please manually check',
$template
) . PHP_EOL;
}
}
exit(0);
function rglob($pattern, $flags = 0) {
$files = glob($pattern, $flags);
foreach (glob(dirname($pattern).'/*', GLOB_ONLYDIR|GLOB_NOSORT) as $dir) {
$files = array_merge($files, rglob($dir.'/'.basename($pattern), $flags));
}
return $files;
}
function getGroupedClassName($groupType, $classId)
{
$classArr = explode('/', trim($classId));
$group = $classArr[0];
$class = !empty($classArr[1]) ? $classArr[1] : null;
$config = Mage::app()->getConfig()->getNode()->global->{$groupType.'s'}->{$group};
// First - check maybe the entity class was rewritten
$className = null;
// Second - if entity is not rewritten then use class prefix to form class name
if (!empty($config)) {
$className = $config->getClassName();
}
if (empty($className)) {
$className = 'mage_'.$group.'_'.$groupType;
}
if (!empty($class)) {
$className .= '_'.$class;
}
$className = uc_words($className);
return $className;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment