Newer
Older
$legacy_settings = explode("\n", $legacy_settings);
$settings = file_get_contents($settings_file);
$settings = str_replace("\r", '', $settings);
$settings = explode("\n", $settings);
if ($civicrm_root_code = reset(preg_grep('/^\s*\$civicrm_root\s*=.*$/', $legacy_settings))) {
// phpcs:disable
eval($civicrm_root_code);
// phpcs:enable
}
elseif ($civicrm_root_code = reset(preg_grep('/^\s*\$civicrm_root\s*=.*$/', $settings))) {
// phpcs:disable
eval($civicrm_root_code);
// phpcs:enable
}
else {
return WP_CLI::error('Unable to read $civicrm_root from civicrm.settings.php');
if ($civicrm_dsn_code = reset(preg_grep('/^\s*define.*CIVICRM_DSN.*$/', $settings))) {
$civicrm_dsn_code = str_replace('CIVICRM_DSN', 'CIVICRM_OLD_DSN', $civicrm_dsn_code);
// phpcs:disable
eval($civicrm_dsn_code);
// phpcs:enable
}
else {
return WP_CLI::error('Unable to read CIVICRM_DSN from civicrm.settings.php');
return WP_CLI::error('Unable to set CIVICRM_OLD_DSN.');
array_pop($basepath);
$project_path = implode('/', $basepath) . '/';
array_pop($basepath);
$plugin_path = implode('/', $basepath) . '/';
$backup_dir = $this->getOption('backup-dir', $wp_root . '../backup');
$backup_dir = rtrim($backup_dir, '/');
WP_CLI::line();
WP_CLI::line('The upgrade process involves:');
WP_CLI::line(sprintf('1. Backing up current CiviCRM code as => %s', "$backup_dir/plugins/$date/$backup_file"));
WP_CLI::line(sprintf('2. Backing up database as => %s', "$backup_dir/plugins/$date/$backup_file.sql"));
WP_CLI::line(sprintf('3. Unpacking tarfile to => %s', $plugin_path));
WP_CLI::line('4. Executing "civicrm/upgrade?reset=1" just as a browser would.');
WP_CLI::line();
return WP_CLI::error(sprintf('Failed to create directory: %s', $backup_dir));
}
$backup_target = $backup_dir . '/' . $backup_file;
return WP_CLI::error(sprintf('Failed to backup CiviCRM project directory %s to %s', $project_path, $backup_target));
['civicrm', 'sql-dump'],
['result-file' => $backup_target . '.sql']
// Should probably never get to here, because WordPress CiviCRM comes in a zip file.
if (!$this->untar($plugin_path)) {
return WP_CLI::error('Error extracting tarfile');
}
elseif ($this->getOption('zipfile', FALSE)) {
if (!$this->unzip($plugin_path)) {
return WP_CLI::error('Error extracting zipfile');
}
else {
return WP_CLI::error('No zipfile specified, use --zipfile=path/to/zipfile');
WP_CLI::line('Copying civicrm.settings.php to ' . $project_path . '..');
define('CIVICRM_SETTINGS_PATH', $project_path . 'civicrm.settings.php');
if (!copy($backup_dir . '/civicrm/civicrm.settings.php', CIVICRM_SETTINGS_PATH)) {
* Implementation of command 'upgrade-db'.
*
* @since 4.5
*/
private function upgradeDB() {
civicrm_initialize();
if (!defined('CIVICRM_UPGRADE_ACTIVE')) {
define('CIVICRM_UPGRADE_ACTIVE', 1);
// CRM_Upgrade_Headless introduced in 4.2 - at the same time as class auto-loading.
try {
$upgrade_headless = new CRM_Upgrade_Headless();
$result = $upgrade_headless->run();
WP_CLI::line(sprintf('Upgrade outputs: "%s"', $result['message']));
}
catch (Exception $e) {
WP_CLI::error($e->getMessage());
require_once 'CRM/Core/Smarty.php';
$template = CRM_Core_Smarty::singleton();
if (is_callable([$upgrade, 'setPrint'])) {
$upgrade->setPrint(TRUE);
WP_CLI::line(sprintf('Upgrade outputs: "%s"', $result));
}
}
/**
* DSN parser - this has been stolen from PEAR DB since we don't always have a
* bootstrapped environment we can access this from, eg: when doing an upgrade.
*
* @since 4.5
*
* @param string|array $dsn
* @return array $parsed The arry containing db connection details.
private static function parseDSN($dsn) {
$parsed = [
'phptype' => FALSE,
'dbsyntax' => FALSE,
'username' => FALSE,
'password' => FALSE,
'protocol' => FALSE,
'hostspec' => FALSE,
'port' => FALSE,
'socket' => FALSE,
'database' => FALSE,
];
if (is_array($dsn)) {
$dsn = array_merge($parsed, $dsn);
if (!$dsn['dbsyntax']) {
$dsn['dbsyntax'] = $dsn['phptype'];
}
return $dsn;
}
if (($pos = strpos($dsn, '://')) !== FALSE) {
$str = substr($dsn, 0, $pos);
$dsn = substr($dsn, $pos + 3);
}
else {
// $str => phptype(dbsyntax)
if (preg_match('|^(.+?)\((.*?)\)$|', $str, $arr)) {
$parsed['dbsyntax'] = !$arr[2] ? $arr[1] : $arr[2];
}
else {
$parsed['phptype'] = $str;
$parsed['dbsyntax'] = $str;
}
// $dsn => username:password@protocol+hostspec/database
if (($at = strrpos($dsn, '@')) !== FALSE) {
$str = substr($dsn, 0, $at);
$dsn = substr($dsn, $at + 1);
if (($pos = strpos($str, ':')) !== FALSE) {
$parsed['username'] = rawurldecode(substr($str, 0, $pos));
$parsed['password'] = rawurldecode(substr($str, $pos + 1));
}
else {
$parsed['username'] = rawurldecode($str);
if (preg_match('|^([^(]+)\((.*?)\)/?(.*?)$|', $dsn, $match)) {
// $dsn => proto(proto_opts)/database
}
else {
// $dsn => protocol+hostspec/database (old format)
if (strpos($dsn, '+') !== FALSE) {
list($proto, $dsn) = explode('+', $dsn, 2);
if (strpos($dsn, '/') !== FALSE) {
list($proto_opts, $dsn) = explode('/', $dsn, 2);
}
else {
$parsed['protocol'] = (!empty($proto)) ? $proto : 'tcp';
$proto_opts = rawurldecode($proto_opts);
if (strpos($proto_opts, ':') !== FALSE) {
list($proto_opts, $parsed['port']) = explode(':', $proto_opts);
if ($dsn) {
if (($pos = strpos($dsn, '?')) === FALSE) {
$parsed['database'] = rawurldecode($dsn);
}
else {
$parsed['database'] = rawurldecode(substr($dsn, 0, $pos));
$dsn = substr($dsn, $pos + 1);
if (strpos($dsn, '&') !== FALSE) {
$opts = explode('&', $dsn);
else {
// database?param1=value1
$opts = [$dsn];
}
foreach ($opts as $opt) {
list($key, $value) = explode('=', $opt);
if (!isset($parsed[$key])) {
}
}
}
}
return $parsed;
* Helper function to replicate functionality of 'drush_get_option'.
*
* @since 4.5
*
* @param string $name
* @param string $default
* @return mixed The value if found or default if not.
private function getOption($name, $default) {
return isset($this->assoc_args[$name]) ? $this->assoc_args[$name] : $default;
* Get the user the web server runs as - used to preserve file permissions on
* templates_c, civicrm/upload etc when running as root. This is not a very
* good check, but is good enough for what we want to do, which is to preserve
* file permissions.
*
* @since 4.5
*
* @return string The user which owns templates_c. Empty string if not found.
$plugins_dir_root = WP_PLUGIN_DIR;
$upload_dir = wp_upload_dir();
$tpl_path = $upload_dir['basedir'] . DIRECTORY_SEPARATOR . 'civicrm' . DIRECTORY_SEPARATOR . 'templates_c';
$legacy_tpl_path = $plugins_dir_root . '/files/civicrm/templates_c';
if (is_dir($legacy_tpl_path)) {
$owner = posix_getpwuid(fileowner($legacy_tpl_path));
if (isset($owner['name'])) {
}
elseif (is_dir($tpl_path)) {
$owner = posix_getpwuid(fileowner($tpl_path));
if (isset($owner['name'])) {
* Get the group the webserver runs as - as above, but for group.
*
* @since 4.5
*
* @return string The group the webserver runs as. Empty string if not found.
$plugins_dir_root = WP_PLUGIN_DIR;
$upload_dir = wp_upload_dir();
$tpl_path = $upload_dir['basedir'] . DIRECTORY_SEPARATOR . 'civicrm' . DIRECTORY_SEPARATOR . 'templates_c';
$legacy_tpl_path = $plugins_dir_root . '/files/civicrm/templates_c';
if (is_dir($legacy_tpl_path)) {
$group = posix_getgrgid(filegroup($legacy_tpl_path));
if (isset($group['name'])) {
}
elseif (is_dir($tpl_path)) {
$group = posix_getgrgid(filegroup($tpl_path));
if (isset($group['name'])) {
* Extracts a tar.gz archive.
*
* @since 4.5
*
* @param string $destination_path The path to extract to.
* @param string $option The command line option to get input filename from, defaults to 'tarfile'.
* @return bool True if successful, false otherwise.
private function untar($destination_path, $option = 'tarfile') {
WP_CLI::launch("gzip -d $tarfile");
$tarfile = substr($tarfile, 0, strlen($tarfile) - 3);
WP_CLI::launch("tar -xf $tarfile -C \"$destination_path\"");
return TRUE;
}
else {
return FALSE;
* Extracts a zip archive.
*
* @since 4.5
*
* @param string $destination_path The path to extract to.
* @param string $option The command line option to get zip filename from, defaults to 'zipfile'.
* @return bool True if successful, false otherwise.
private function unzip($destination_path, $option = 'zipfile') {
WP_CLI::launch("unzip -q $zipfile -d $destination_path");
return TRUE;
}
else {
return FALSE;
WP_CLI::add_command('civicrm', 'CiviCRM_Command');
WP_CLI::add_command('cv', 'CiviCRM_Command');
global $civicrm_paths;
$wp_cli_config = WP_CLI::get_config();
// If --path is set, save for later use by CiviCRM.
if (!empty($wp_cli_config['path'])) {
$civicrm_paths['cms.root']['path'] = $wp_cli_config['path'];
}
// If --url is set, save for later use by CiviCRM.
if (!empty($wp_cli_config['url'])) {
$civicrm_paths['cms.root']['url'] = $wp_cli_config['url'];
}