Skip to content

Instantly share code, notes, and snippets.

@borantula
Last active April 25, 2016 07:38
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 borantula/e41c4b6ba36f78b1110d400a16754691 to your computer and use it in GitHub Desktop.
Save borantula/e41c4b6ba36f78b1110d400a16754691 to your computer and use it in GitHub Desktop.
Processwire 3 Namespace Migrator
<?php
/**
* This script attempts to add namespace Processwire to every .php and .module file in the given directory.
* It worked fine for my project but I cannot guarantee for you, so PLEASE take a backup or two before you
* attempt to try it.
*
* To use it put this in your root folder and navigate with your browser to the file like
* example.com/processwire-3-namespace-migrator.php
* Also you should disable the compiler by setting $config->templateCompile=false; in your /site/config.php.
*
* By default it assumes ./site/templates folder but
* I tried it with a module which is giving errors due to namespaces and worked fine.
*
* If you are doing this in a production environment, THAT YOU NORMALLY DEFINITELY SHOULD NOT,
* delete the file after you're done.
*/
function fixIfStartingWithPhp($path, $firstLine)
{
$arr = file($path);
$l1 = '<?php namespace Processwire;?>' . "\n";
array_unshift($arr, $l1);
file_put_contents($path, implode($arr));
return $line = fgets(fopen($path, 'r'));
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<div class="row">
<div class="col-xs-12">
<style>
.filename {
padding: 5px 10px;
margin: 2px 0;
}
</style>
<?php
// you can change the directory for your needs.
$Directory = new RecursiveDirectoryIterator('./site/templates');
$Iterator = new RecursiveIteratorIterator($Directory);
$regexes = [];
$regexes[] = new RegexIterator($Iterator, '/^.+\.php$/i', RecursiveRegexIterator::GET_MATCH);
$regexes[] = new RegexIterator($Iterator, '/^.+\.modules$/i', RecursiveRegexIterator::GET_MATCH);
$countSuccess = 0;
$countFail = 0;
foreach ($regexes as $regex) {
foreach ($regex as $key => $path) {
$path = $path[0];
$contents = htmlentities(file_get_contents($path));
$hasNamespace = strpos(($contents), 'namespace Processwire');
if ($hasNamespace) {
$countSuccess++;
} else {
$countFail++;
$line = fgets(fopen($path, 'r'));
$lineFixed = fixIfStartingWithPhp($path, $line);
}
?>
<?php if ($hasNamespace): ?>
<div class="bg-success filename">
<h3><?= $key + 1; ?> - <?= $path; ?></h3>
</div>
<?php else: ?>
<div class="bg-danger filename">
<h3><?= $key + 1; ?> - <?= $path; ?></h3>
<pre><?= htmlspecialchars($line); ?></pre>
<pre><?= htmlspecialchars($lineFixed); ?></pre>
</div>
<?php endif; ?>
<?php
}
}
?>
<div class="bg-primary">
<p>Success: <?= $countSuccess; ?></p>
<p>Fail: <?= $countFail; ?></p>
</div>
</div>
</div>
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment