Last active
December 1, 2020 09:11
-
-
Save jeremypetrequin/813f16d30c505b4387288e24ed99b371 to your computer and use it in GitHub Desktop.
Task NPM//PHP to generate SQL queries to migrate a WP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
if(empty($_SERVER['npm_config_to'])) { | |
die('Missing --to parameter'.PHP_EOL); | |
} | |
$to = $_SERVER['npm_config_to']; | |
$_SERVER['SERVER_NAME'] = $_SERVER['npm_package_url']; | |
define('WP_USE_THEMES', false); | |
require_once(dirname(__FILE__).'/../../app/wp-load.php'); | |
$from = !empty($_SERVER['npm_config_from']) ? $_SERVER['npm_config_from'] : get_site_url(); | |
//si on ne fourni pas d'url from | |
//et que l'url to ne contient pas de http ou de https | |
//on enleve le http/https de l'url to | |
//dans la logique soit t'a un schema dans les deux, soit dans aucun, si ca te va pas tu forces le from et to et ca ne passera pas là dedans | |
if(empty($_SERVER['npm_config_from']) && (strpos($to, 'http://') === false && strpos($to, 'https://') === false)) { | |
$from = str_replace(['https://', 'http://'], '', $from); | |
} | |
if(IS_PROD) { | |
die('Access denied in production env'); | |
} | |
$params = [ | |
'prefix' => DB_PREFIX, | |
'url_to' => $to, | |
'url_from' => $from | |
]; | |
$tmpl = "/* {url_from} to {url_to} */ | |
UPDATE {prefix}options | |
SET option_value = replace(option_value, '{url_from}', | |
'{url_to}') | |
WHERE option_name = 'home' OR option_name = 'siteurl'; | |
UPDATE {prefix}posts | |
SET guid = replace(guid, '{url_from}', | |
'{url_to}'); | |
UPDATE {prefix}posts | |
SET post_content = replace(post_content, '{url_from}', | |
'{url_to}'); | |
UPDATE {prefix}postmeta | |
SET meta_value = replace(meta_value, '{url_from}', | |
'{url_to}'); | |
UPDATE {prefix}yoast_indexable | |
SET permalink = replace(permalink, '{url_from}', | |
'{url_to}'); | |
UPDATE {prefix}yoast_seo_links | |
SET url = replace(url, '{url_from}', | |
'{url_to}'); | |
"; | |
$output = str_replace(array_map(function($key) { | |
return '{'.$key .'}'; | |
}, array_keys($params)), array_values($params), $tmpl); | |
file_put_contents(ABSPATH.'../_ressources/migration.sql', $output, FILE_APPEND | LOCK_EX); | |
echo "File _ressources/migration.sql written".PHP_EOL; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "", | |
"version": "3.0.0", | |
"description": "FCINQ Wordpress", | |
"url": "http://local.wp.com/", | |
"author": { | |
"name": "FCINQ Team", | |
"url": "http://fcinq.com/" | |
}, | |
"scripts": { | |
"migratedb": "php dev/php/migrate-db.php" | |
}, | |
"devDependencies": {}, | |
"dependencies": {} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Folder structure:
/package.json
/dev/php/migrate-db.php
/_ressources/
Call:
npm run migratedb --from=http://local.env.com --to=https://prod.env.com
You will have the SQL in /_ressources/migration.sql
Don't use the two lasts queries if you don't use Yoast SEO plugin.