Skip to content

Commit

Permalink
[ BUGS 6307 ] Error Triggering "wp search-replace" (#2471)
Browse files Browse the repository at this point in the history
* Fixing issue with wordpress

* Update dependencies

* Add test for Wordpress WITH urls
  • Loading branch information
stovak authored Jun 1, 2023
1 parent 59df7ef commit 8dd561a
Show file tree
Hide file tree
Showing 5 changed files with 201 additions and 89 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ reports/*
Brewfile.lock.json
/.phive
/.php-cs-fixer.cache
/terminus-data
2 changes: 1 addition & 1 deletion bin/terminus
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ if (!getenv('TERMINUS_ALLOW_UNSUPPORTED_NEWER_PHP') && version_compare(PHP_VERSI

// This variable is automatically managed via updateDependenciesversion() in /RoboFile.php,
// which is run after every call to composer update.
$terminusPluginsDependenciesVersion = '10c370e142';
$terminusPluginsDependenciesVersion = '27a446d993';

// Cannot use $_SERVER superglobal since that's empty during phpunit testing
// getenv('HOME') isn't set on Windows and generates a Notice.
Expand Down
73 changes: 36 additions & 37 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

69 changes: 43 additions & 26 deletions src/Commands/Env/CloneContentCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

/**
* Class CloneContentCommand
*
* @package Pantheon\Terminus\Commands\Env
*/
class CloneContentCommand extends TerminusCommand implements SiteAwareInterface
Expand All @@ -23,6 +24,7 @@ class CloneContentCommand extends TerminusCommand implements SiteAwareInterface
* @var Environment
*/
private $source_env;

/**
* @var Environment
*/
Expand All @@ -43,9 +45,11 @@ class CloneContentCommand extends TerminusCommand implements SiteAwareInterface
*
* @command env:clone-content
*
* @param string $site_env Origin site & environment in the format `site-name.env`
* @param string $site_env Origin site & environment in the format
* `site-name.env`
* @param string $target_env Target environment
* @param array $options
*
* @option bool $cc Whether or not to clear caches
* @option bool $db-only Only clone database
* @option bool $files-only Only clone files
Expand All @@ -55,12 +59,21 @@ class CloneContentCommand extends TerminusCommand implements SiteAwareInterface
*
* @throws \Pantheon\Terminus\Exceptions\TerminusException
*
* @usage <site>.<env> <target_env> Clones database and files from <site>'s <env> environment to <target_env> environment.
* @usage <site>.<env> <target_env> --cc Clones from <site>'s <env> environment to <target_env> environment and clears the cache.
* @usage <site>.<env> <target_env> --db-only Clones only the database from <site>'s <env> environment to <target_env> environment.
* @usage <site>.<env> <target_env> --files-only Clones only files from <site>'s <env> environment to <target_env> environment.
* @usage <site>.<env> <target_env> --updatedb Clones from <site>'s <env> environment to <target_env> environment and updates the Drupal database (if applicable).
* @usage <site>.<env> <target_env> --from-url=www.example.com --to-url=mulitidevenv.example.com (WordPress only) Clones from <site>'s <env> environment to <target_env> environment and replaces www.example.com with mulitidevenv.example.com in the database.
* @usage <site>.<env> <target_env> Clones database and files from <site>'s
* <env> environment to <target_env> environment.
* @usage <site>.<env> <target_env> --cc Clones from <site>'s <env>
* environment to <target_env> environment and clears the cache.
* @usage <site>.<env> <target_env> --db-only Clones only the database from
* <site>'s <env> environment to <target_env> environment.
* @usage <site>.<env> <target_env> --files-only Clones only files from
* <site>'s <env> environment to <target_env> environment.
* @usage <site>.<env> <target_env> --updatedb Clones from <site>'s <env>
* environment to <target_env> environment and updates the Drupal
* database (if applicable).
* @usage <site>.<env> <target_env> --from-url=www.example.com
* --to-url=mulitidevenv.example.com (WordPress only) Clones from
* <site>'s <env> environment to <target_env> environment and replaces
* www.example.com with mulitidevenv.example.com in the database.
*/
public function cloneContent(
$site_env,
Expand All @@ -75,7 +88,9 @@ public function cloneContent(
]
) {
if (!empty($options['db-only']) && !empty($options['files-only'])) {
throw new TerminusException('You cannot specify both --db-only and --files-only');
throw new TerminusException(
'You cannot specify both --db-only and --files-only'
);
}

$this->requireSiteIsNotFrozen($site_env);
Expand All @@ -84,7 +99,9 @@ public function cloneContent(
$this->target_env = $site->getEnvironments()->get($target_env);

if ($this->source_env->id === $target_env) {
$this->log()->notice('The clone has been skipped because the source and target environments are the same.');
$this->log()->notice(
'The clone has been skipped because the source and target environments are the same.'
);
return;
}

Expand All @@ -94,9 +111,9 @@ public function cloneContent(
!$this->confirm(
'Are you sure you want to clone content from {from} to {to} on {site}?',
[
'from' => $this->source_env->getName(),
'site' => $site->getName(),
'to' => $this->target_env->getName(),
'from' => $this->source_env->getName(),
'site' => $site->getName(),
'to' => $this->target_env->getName(),
]
)
) {
Expand All @@ -108,25 +125,19 @@ public function cloneContent(
}

if (empty($options['files-only'])) {
// If the site is a WordPress site, we need to pass the search-replace
// option to the API along with the from_url and to_url from each
// environment.
if ($site->getFramework()->isWordpressFramework()) {
$options['search-replace'] = [
'from_url' => $options['from-url'] ?? $this->source_env->domain(),
'to_url' => $options['to-url'] ?? $this->target_env->domain(),
];
}
$this->cloneDatabase($options);
}
}

/**
* Checks to see whether the indicated environment is initialized and stops the process if it isn't
* Checks to see whether the indicated environment is initialized and stops
* the process if it isn't
*
* @param Environment $env
* @param string $direction "into" or "from" are recommended.
* @throws TerminusException Thrown if the passed-in environment is not initialized
*
* @throws TerminusException Thrown if the passed-in environment is not
* initialized
*/
private function checkForInitialization(Environment $env, $direction = '')
{
Expand All @@ -152,8 +163,11 @@ private function cloneDatabase(array $options)
'clear_cache' => $options['cc'],
'updatedb' => $options['updatedb'],
];
if (!empty($options['search-replace'])) {
$params['search_replace'] = $options['search-replace'];
if ($options['from-url'] != '') {
$params['from_url'] = $options['from-url'];
}
if ($options['to-url'] != '') {
$params['to_url'] = $options['to-url'];
}
$this->emitNotice('database');
$this->runClone(
Expand All @@ -179,7 +193,10 @@ private function emitNotice($element)
{
$this->log()->notice(
"Cloning {$element} from {source} environment to {target} environment",
['source' => $this->source_env->getName(), 'target' => $this->target_env->getName(),]
[
'source' => $this->source_env->getName(),
'target' => $this->target_env->getName(),
]
);
}

Expand Down
Loading

0 comments on commit 8dd561a

Please sign in to comment.