Unverified Commit 813c5625 authored by kcristiano's avatar kcristiano Committed by GitHub

Merge pull request #148 from aydun/whitespace_cleanup

(NFC) formatting changes
parents 42e84c2b e6d38056
......@@ -26,1302 +26,1302 @@
+--------------------------------------------------------------------+
*/
if ( ! defined( 'CIVICRM_WPCLI_LOADED' ) ) {
define( 'CIVICRM_WPCLI_LOADED', 1 );
/**
* WP-CLI port of drush-civicrm integration
* andyw@circle, 08/03/2014
*
* Distributed under the GNU Affero General Public License, version 3
* http://www.gnu.org/licenses/agpl-3.0.html
*/
class CiviCRM_Command extends WP_CLI_Command {
private $args, $assoc_args;
/**
* WP-CLI integration with CiviCRM.
*
* wp civicrm api
* ===============
* Command for accessing CiviCRM APIs. Syntax is identical to drush cvap.
*
* wp civicrm cache-clear
* ===============
* Command for accessing clearing cache. Equivilant of running civicrm/admin/setting/updateConfigBackend&reset=1
*
* wp civicrm enable-debug
* ===============
* Command for to turn debug on.
*
* wp civicrm disable-debug
* ===============
* Command for to turn debug off.
*
* wp civicrm member-records
* ===============
* Run the CiviMember UpdateMembershipRecord cron ( civicrm member-records ).
*
* wp civicrm process-mail-queue
* ===============
* Process pending CiviMail mailing jobs.
* Example:
* wp civicrm process-mail-queue -u admin
*
* wp civicrm rest
* ===============
* Rest interface for accessing CiviCRM APIs. It can return xml or json formatted data.
*
* wp civicrm restore
* ==================
* Restore CiviCRM codebase and database back from the specified backup directory
*
* wp civicrm sql-conf
* ===================
* Show civicrm database connection details.
*
* wp civicrm sql-connect
* ======================
* A string which connects to the civicrm database.
*
* wp civicrm sql-cli
* ==================
* Quickly enter the mysql command line.
*
* wp civicrm sql-dump
* ===================
* Prints the whole CiviCRM database to STDOUT or save to a file.
*
* wp civicrm sql-query
* ====================
* Usage: wp civicrm sql-query <query> <options>...
* <query> is a SQL statement, which can alternatively be passed via STDIN. Any additional arguments are passed to the mysql command directly.";
*
* wp civicrm update-cfg
* =====================
* Update config_backend to correct config settings, especially when the CiviCRM site has been cloned / migrated.
*
* wp civicrm upgrade
* ==================
* Take backups, replace CiviCRM codebase with new specified tarfile and upgrade database by executing the CiviCRM upgrade process - civicrm/upgrade?reset=1. Use civicrm-restore to revert to previous state in case anything goes wrong.
*
* wp civicrm upgrade-db
* =====================
* Run civicrm/upgrade?reset=1 just as a web browser would.
*
* wp civicrm install
* ===============
* Command for to install CiviCRM. The install command requires that you have downloaded a tarball or zip file first.
* Options:
* --dbhost MySQL host for your WordPress/CiviCRM database. Defaults to localhost.
* --dbname MySQL database name of your WordPress/CiviCRM database.
* --dbpass MySQL password for your WordPress/CiviCRM database.
* --dbuser MySQL username for your WordPress/CiviCRM database.
* --lang Default language to use for installation.
* --langtarfile Path to your l10n tar.gz file.
* --site_url Base Url for your WordPress/CiviCRM website without http ( e.g. mysite.com )
* --ssl Using ssl for your WordPress/CiviCRM website if set to on ( e.g. --ssl=on )
* --tarfile Path to your CiviCRM tar.gz file.
*
*/
public function __invoke( $args, $assoc_args ) {
$this->args = $args;
$this->assoc_args = $assoc_args;
# define command router
$command_router = array(
'api' => 'api',
'cache-clear' => 'cacheClear',
'enable-debug' => 'enableDebug',
'disable-debug' => 'disableDebug',
'install' => 'install',
'member-records' => 'memberRecords',
'process-mail-queue' => 'processMailQueue',
'rest' => 'rest',
'restore' => 'restore',
'sql-cli' => 'sqlCLI',
'sql-conf' => 'sqlConf',
'sql-connect' => 'sqlConnect',
'sql-dump' => 'sqlDump',
'sql-query' => 'sqlQuery',
'update-cfg' => 'updateConfig',
'upgrade' => 'upgrade',
'upgrade-db' => 'upgradeDB',
);
# get command
$command = array_shift( $args );
# check for existence of Civi ( except for command 'install' )
if ( ! function_exists( 'civicrm_initialize' ) and 'install' != $command ) {
return WP_CLI::error( 'Unable to find CiviCRM install.' );
}
# check existence of router entry / handler method
if ( ! isset( $command_router[ $command ] ) or ! method_exists( $this, $command_router[ $command ] ) ) {
return WP_CLI::error( "Unrecognized command - '$command'" );
}
# run command
return $this->{$command_router[ $command ]}();
}
/**
* Implementation of command 'api'
*/
private function api() {
$defaults = array( 'version' => 3 );
array_shift( $this->args );
list( $entity, $action ) = explode( '.', $this->args[0] );
array_shift( $this->args );
# parse $params
$format = $this->getOption( 'in', 'args' );
switch ( $format ) {
# input params supplied via args ..
case 'args':
$params = $defaults;
foreach ( $this->args as $arg ) {
preg_match( '/^([^=]+)=(.*)$/', $arg, $matches );
$params[ $matches[1] ] = $matches[2];
}
break;
# input params supplied via json ..
case 'json':
$json = stream_get_contents( STDIN );
$params = ( empty( $json ) ? $defaults : array_merge( $defaults, json_decode( $json, true ) ) );
break;
default:
WP_CLI::error( 'Unknown format: ' . $format );
break;
}
civicrm_initialize();
// CRM-18062: Set CiviCRM timezone if any
$wp_base_timezone = date_default_timezone_get();
$wp_user_timezone = $this->getOption( 'timezone', get_option( 'timezone_string' ) );
if ( $wp_user_timezone ) {
date_default_timezone_set( $wp_user_timezone );
CRM_Core_Config::singleton()->userSystem->setMySQLTimeZone();
}
$result = civicrm_api( $entity, $action, $params );
// restore WP's timezone
if ( $wp_base_timezone ) {
date_default_timezone_set( $wp_base_timezone );
}
switch ( $this->getOption( 'out', 'pretty' ) ) {
# pretty-print output ( default )
case 'pretty':
WP_CLI::line( print_r( $result, true ) );
break;
# display output as json
case 'json':
WP_CLI::line( json_encode( $result ) );
break;
default:
return WP_CLI::error( 'Unknown format: ' . $format );
}
}
/**
* Implementation of command 'cache-clear'
*/
private function cacheClear() {
civicrm_initialize();
require_once 'CRM/Core/Config.php';
$config = CRM_Core_Config::singleton();
# clear db caching
$config->clearDBCache();
# also cleanup the templates_c directory
$config->cleanup( 1, false );
# also cleanup the session object
$session = CRM_Core_Session::singleton();
$session->reset( 1 );
}
/**
* Implementation of command 'enable-debug'
*/
private function enableDebug() {
civicrm_initialize();
Civi::settings()->add( array(
'debug_enabled' => 1,
'backtrace' => 1,
) );
WP_CLI::success( 'Debug setting enabled.' );
}
/**
* Implementation of command 'disable-debug'
*/
private function disableDebug() {
civicrm_initialize();
Civi::settings()->add( array(
'debug_enabled' => 0,
'backtrace' => 0,
) );
WP_CLI::success( 'Debug setting disabled.' );
}
/**
* Implementation of command 'install'
*/
private function install() {
# validate
if ( ! $dbuser = $this->getOption( 'dbuser', false ) ) {
return WP_CLI::error( 'CiviCRM database username not specified.' );
}
if ( ! $dbpass = $this->getOption( 'dbpass', false ) ) {
return WP_CLI::error( 'CiviCRM database password not specified.' );
}
if ( ! $dbhost = $this->getOption( 'dbhost', false ) ) {
return WP_CLI::error( 'CiviCRM database host not specified.' );
}
if ( ! $dbname = $this->getOption( 'dbname', false ) ) {
return WP_CLI::error( 'CiviCRM database name not specified.' );
}
if ( $lang = $this->getOption( 'lang', false ) and ! $langtarfile = $this->getOption( 'langtarfile', false ) ) {
return WP_CLI::error( 'CiviCRM language tarfile not specified.' );
}
# begin install
if ( $plugin_path = $this->getOption( 'destination', false ) ) {
$plugin_path = ABSPATH . $plugin_path;
} else {
$plugin_path = WP_PLUGIN_DIR . '/civicrm';
}
global $crmPath;
$crmPath = "$plugin_path/civicrm";
$crm_files_present = is_dir( $crmPath );
# extract the archive
if ( $this->getOption( 'tarfile', false ) ) {
# should probably never get to here as Wordpress Civi comes in a zip file, but
# just in case that ever changes ..
if ( $crm_files_present ) {
return WP_CLI::error( 'Existing CiviCRM found. No action taken.' );
}
if ( ! $this->untar( dirname( $plugin_path ) ) ) {
return WP_CLI::error( 'Error extracting tarfile' );
}
} elseif ( $this->getOption( 'zipfile', false ) ) {
if ( $crm_files_present ) {
return WP_CLI::error( 'Existing CiviCRM found. No action taken.' );
}
if ( ! $this->unzip( dirname( $plugin_path ) ) ) {
return WP_CLI::error( 'Error extracting zipfile' );
}
} elseif ( $crm_files_present ) {
// Site is already extracted (which is how we're running this
// script); we just need to run the installer.
} else {
return WP_CLI::error( 'No zipfile specified, use --zipfile=path/to/zipfile or extract file ahead of time' );
}
# include civicrm installer helper file
$civicrm_installer_helper = "$crmPath/install/civicrm.php";
if ( ! file_exists( $civicrm_installer_helper ) ) {
return WP_CLI::error( 'Archive could not be unpacked OR CiviCRM installer helper file is missing.' );
}
if ( $crm_files_present ) {
// We were using a directory that was already there.
WP_CLI::success( 'Using installer files found on the site.' );
} else {
// We must've just unpacked the archive because it wasn't there
// before.
WP_CLI::success( 'Archive unpacked.' );
}
require_once $civicrm_installer_helper;
if ( '' != $lang ) {
if ( ! $this->untar( $plugin_path, 'langtarfile' ) ) {
return WP_CLI::error( 'No language tarfile specified, use --langtarfile=path/to/tarfile' );
}
}
# create files dirs
$upload_dir = wp_upload_dir();
$settings_dir = $upload_dir['basedir'] . DIRECTORY_SEPARATOR . 'civicrm' . DIRECTORY_SEPARATOR;
civicrm_setup( $upload_dir['basedir'] . DIRECTORY_SEPARATOR );
WP_CLI::launch( "chmod 0755 $settings_dir -R" );
# now we've got some files in place, require PEAR DB and check db setup
$dsn = "mysql://{$dbuser}:{$dbpass}@{$dbhost}/{$dbname}?new_link=true";
$dsn_nodb = "mysql://{$dbuser}:{$dbpass}@{$dbhost}";
if ( ! defined( 'DB_DSN_MODE' ) ) {
define( 'DB_DSN_MODE', 'auto' );
}
require_once "$crmPath/packages/DB.php";
$db = DB::connect( $dsn );
if ( DB::iserror( $db ) ) {
$db = DB::connect( $dsn_nodb );
if ( DB::iserror( $db ) ) {
return WP_CLI::error( 'Unable to connect to database. Please re-check credentials.' );
}
$db->query( "CREATE DATABASE $dbname" );
if ( DB::iserror( $db ) ) {
return WP_CLI::error( 'CiviCRM database was not found. Failed to create one.' );
}
$db->disconnect();
}
# install db
global $sqlPath;
# setup database with civicrm structure and data
WP_CLI::line( 'Loading CiviCRM database structure ..' );
civicrm_source( $dsn, $sqlPath . '/civicrm.mysql' );
WP_CLI::line( 'Loading CiviCRM database with required data ..' );
# testing the translated sql files availability
$data_file = $sqlPath . '/civicrm_data.mysql';
$acl_file = $sqlPath . '/civicrm_acl.mysql';
if ( '' != $lang ) {
if ( file_exists( $sqlPath . '/civicrm_data.' . $lang . '.mysql' )
and file_exists( $sqlPath . '/civicrm_acl.' . $lang . '.mysql' )
and '' != $lang
) {
$data_file = $sqlPath . '/civicrm_data.' . $lang . '.mysql';
$acl_file = $sqlPath . '/civicrm_acl.' . $lang . '.mysql';
} else {
WP_CLI::warning( "No sql files could be retrieved for '$lang' using default language." );
}
}
civicrm_source( $dsn, $data_file );
civicrm_source( $dsn, $acl_file );
WP_CLI::success( 'CiviCRM database loaded successfully.' );
# generate civicrm.settings.php file
global $tplPath;
if ( ! file_exists( $tplPath . 'civicrm.settings.php.template' ) ) {
return WP_CLI::error( 'Could not find CiviCRM settings template and therefore could not create settings file.' );
}
WP_CLI::line( 'Generating civicrm settings file ..' );
if ( $base_url = $this->getOption( 'site_url', false ) ) {
$ssl = $this->getOption( 'ssl', false );
$protocol = ( 'on' == $ssl ? 'https' : 'http' );
}
$base_url = ! $base_url ? get_bloginfo( 'url' ) : $protocol . '://' . $base_url;
if ( substr( $base_url, -1 ) != '/' ) {
$base_url .= '/';
}
$params = array(
'crmRoot' => $crmPath . '/',
'templateCompileDir' => "{$settings_dir}templates_c",
'frontEnd' => 0,
'cms' => 'WordPress',
'baseURL' => $base_url,
'dbUser' => $dbuser,
'dbPass' => $dbpass,
'dbHost' => $dbhost,
'dbName' => $dbname,
'CMSdbUser' => DB_USER,
'CMSdbPass' => DB_PASSWORD,
'CMSdbHost' => DB_HOST,
'CMSdbName' => DB_NAME,
'siteKey' => md5(rand() . mt_rand() . rand() . uniqid('', TRUE) . $params['baseURL']),
);
$str = file_get_contents( $tplPath . 'civicrm.settings.php.template' );
foreach ( $params as $key => $value ) {
$str = str_replace( '%%' . $key . '%%', $value, $str );
}
$str = trim( $str );
$config_file = "{$settings_dir}civicrm.settings.php";
civicrm_write_file( $config_file, $str );
WP_CLI::launch( "chmod 0644 $config_file" );
WP_CLI::success( sprintf( 'Settings file generated: %s', $config_file ) );
# activate plugin and we're done
@WP_CLI::run_command( array( 'plugin', 'activate', 'civicrm' ), array() );
WP_CLI::success( 'CiviCRM installed.' );
}
/**
* Implementation of command 'member-records'
*/
private function memberRecords() {
civicrm_initialize();
if ( substr( CRM_Utils_System::version(), 0, 3 ) >= '4.3' ) {
$job = new CRM_Core_JobManager();
$job->executeJobByAction( 'job', 'process_membership' );
WP_CLI::success( "Executed 'process_membership' job." );
} else {
$_REQUEST['name'] = $this->getOption( 'civicrm_cron_username', null );
$_REQUEST['pass'] = $this->getOption( 'civicrm_cron_password', null );
$_REQUEST['key'] = $this->getOption( 'civicrm_sitekey', null );
global $argv;
$argv = array(
0 => 'drush',
1 => '-u' . $_REQUEST['name'],
2 => '-p' . $_REQUEST['pass'],
3 => '-s' . $this->getOption( 'uri', false ),
);
# if ( !defined( 'CIVICRM_CONFDIR' ) ) {
# $plugins_dir = plugin_dir_path( __FILE__ );
# define( 'CIVICRM_CONFDIR', $plugins_dir );
# }
include 'bin/UpdateMembershipRecord.php';
}
}
/**
* Implementation of command 'process-mail-queue'
*/
private function processMailQueue() {
define( 'CIVICRM_WPCLI_LOADED', 1 );
/**
* WP-CLI port of drush-civicrm integration
* andyw@circle, 08/03/2014
*
* Distributed under the GNU Affero General Public License, version 3
* http://www.gnu.org/licenses/agpl-3.0.html
*/
class CiviCRM_Command extends WP_CLI_Command {
private $args, $assoc_args;
/**
* WP-CLI integration with CiviCRM.
*
* wp civicrm api
* ===============
* Command for accessing CiviCRM APIs. Syntax is identical to drush cvap.
*
* wp civicrm cache-clear
* ===============
* Command for accessing clearing cache. Equivilant of running civicrm/admin/setting/updateConfigBackend&reset=1
*
* wp civicrm enable-debug
* ===============
* Command for to turn debug on.
*
* wp civicrm disable-debug
* ===============
* Command for to turn debug off.
*
* wp civicrm member-records
* ===============
* Run the CiviMember UpdateMembershipRecord cron ( civicrm member-records ).
*
* wp civicrm process-mail-queue
* ===============
* Process pending CiviMail mailing jobs.
* Example:
* wp civicrm process-mail-queue -u admin
*
* wp civicrm rest
* ===============
* Rest interface for accessing CiviCRM APIs. It can return xml or json formatted data.
*
* wp civicrm restore
* ==================
* Restore CiviCRM codebase and database back from the specified backup directory
*
* wp civicrm sql-conf
* ===================
* Show civicrm database connection details.
*
* wp civicrm sql-connect
* ======================
* A string which connects to the civicrm database.
*
* wp civicrm sql-cli
* ==================
* Quickly enter the mysql command line.
*
* wp civicrm sql-dump
* ===================
* Prints the whole CiviCRM database to STDOUT or save to a file.
*
* wp civicrm sql-query
* ====================
* Usage: wp civicrm sql-query <query> <options>...
* <query> is a SQL statement, which can alternatively be passed via STDIN. Any additional arguments are passed to the mysql command directly.";
*
* wp civicrm update-cfg
* =====================
* Update config_backend to correct config settings, especially when the CiviCRM site has been cloned / migrated.
*
* wp civicrm upgrade
* ==================
* Take backups, replace CiviCRM codebase with new specified tarfile and upgrade database by executing the CiviCRM upgrade process - civicrm/upgrade?reset=1. Use civicrm-restore to revert to previous state in case anything goes wrong.
*
* wp civicrm upgrade-db
* =====================
* Run civicrm/upgrade?reset=1 just as a web browser would.
*
* wp civicrm install
* ===============
* Command for to install CiviCRM. The install command requires that you have downloaded a tarball or zip file first.
* Options:
* --dbhost MySQL host for your WordPress/CiviCRM database. Defaults to localhost.
* --dbname MySQL database name of your WordPress/CiviCRM database.
* --dbpass MySQL password for your WordPress/CiviCRM database.
* --dbuser MySQL username for your WordPress/CiviCRM database.
* --lang Default language to use for installation.
* --langtarfile Path to your l10n tar.gz file.
* --site_url Base Url for your WordPress/CiviCRM website without http ( e.g. mysite.com )
* --ssl Using ssl for your WordPress/CiviCRM website if set to on ( e.g. --ssl=on )
* --tarfile Path to your CiviCRM tar.gz file.
*
*/
public function __invoke( $args, $assoc_args ) {
$this->args = $args;
$this->assoc_args = $assoc_args;
# define command router
$command_router = array(
'api' => 'api',
'cache-clear' => 'cacheClear',
'enable-debug' => 'enableDebug',
'disable-debug' => 'disableDebug',
'install' => 'install',
'member-records' => 'memberRecords',
'process-mail-queue' => 'processMailQueue',
'rest' => 'rest',
'restore' => 'restore',
'sql-cli' => 'sqlCLI',
'sql-conf' => 'sqlConf',
'sql-connect' => 'sqlConnect',
'sql-dump' => 'sqlDump',
'sql-query' => 'sqlQuery',
'update-cfg' => 'updateConfig',
'upgrade' => 'upgrade',
'upgrade-db' => 'upgradeDB',
);
# get command
$command = array_shift( $args );
# check for existence of Civi ( except for command 'install' )
if ( ! function_exists( 'civicrm_initialize' ) and 'install' != $command ) {
return WP_CLI::error( 'Unable to find CiviCRM install.' );
}
# check existence of router entry / handler method
if ( ! isset( $command_router[ $command ] ) or ! method_exists( $this, $command_router[ $command ] ) ) {
return WP_CLI::error( "Unrecognized command - '$command'" );
}
# run command
return $this->{$command_router[ $command ]}();
}
/**
* Implementation of command 'api'
*/
private function api() {
$defaults = array( 'version' => 3 );
array_shift( $this->args );
list( $entity, $action ) = explode( '.', $this->args[0] );
array_shift( $this->args );
# parse $params
$format = $this->getOption( 'in', 'args' );
switch ( $format ) {
# input params supplied via args ..
case 'args':
$params = $defaults;
foreach ( $this->args as $arg ) {
preg_match( '/^([^=]+)=(.*)$/', $arg, $matches );
$params[ $matches[1] ] = $matches[2];
}
break;
# input params supplied via json ..
case 'json':
$json = stream_get_contents( STDIN );
$params = ( empty( $json ) ? $defaults : array_merge( $defaults, json_decode( $json, true ) ) );
break;
default:
WP_CLI::error( 'Unknown format: ' . $format );
break;
}
civicrm_initialize();