This documentation refers to CiviCRM 3.2, current stable version. Please introduce all the documentation changes here.

Drush CiviCRM proof of concept


This page refers to CiviCRM 3.2.


Documentation Search


CiviCRM 3.2 Documentation

Support and Participation

Developer Resources


CiviCRM book!
Make sure to check out Understanding CiviCRM as well! You can also support this project by ordering a hard copy.

Put the following stuff in civicrm/drupal/drush/civicrm.drush.inc to see a proof of concept, Try "drush crm clear" and "drush crm group get".

You might need to run this as the user running your webserver, e.g. sudo su www-data -c "drush crm clear"

See also http://issues.civicrm.org/jira/browse/CRM-5818 and http://civicrm.org/blogs/deepak/civicrm-and-drush-integration

civicrm/drupal/drush/civicrm.drush.inc
<?php
/*
 * Copyright (C) 2010 Kasper Souren
 * Licensed to CiviCRM under the GNU General Public License v2 or later
 *
 */


define('MODULE_BUILDER_ENV', 'drush');

/**
 * Implementation of hook_drush_command().
 *
 * In this hook, you specify which commands your
 * drush module makes available, what it does and
 * description.
 *
 * Notice how this structure closely resembles how
 * you define menu hooks.
 *
 * @See drush_parse_command() for a list of recognized keys.
 *
 * @return
 *   An associative array describing your command(s).
 */
function civicrm_drush_command() {
  $items = array();

  // the key in the $items array is the name of the command.
  $items['crm api'] = array(
    'callback' => 'crm_api_call',
    'description' => "Call CiviCRM API.",
    'arguments' => array(
      'api function' => 'API function.',
      'arguments' => 'Arguments.',
    ),
   'options' => array(
      '--csv' => "output as csv.",
    )
  );

  $items['crm bin'] = array(
    'callback' => 'crm_bin_call',
    'description' => "CiviCRM bin/ scripts.",
    'arguments' => array(
      'api function' => 'API function.',
      'arguments' => 'Arguments.',
    ),
  );

  $items['crm clear'] = array(
    'callback' => 'crm_clear_call',
    'description' => "Clear CiviCRM cache.",
  );

  return $items;
}

/**
 * Implementation of hook_drush_help().
 *
 * This function is called whenever a drush user calls
 * 'drush help <name-of-your-command>'
 *
 * @param
 *   A string with the help section (prepend with 'drush:')
 *
 * @return
 *   A string with the help text for your command.
 */
function civicrm_drush_help($section) {
  switch ($section) {
    case 'drush:mb':
      return dt("CiviCRM proof of concept.");
  }
}

/**
 * Module builder drush command callback.
 *
 * Form:
 * $drush mb machine_name hookA hookB hookC
 * where 'hookA' is the short name, ie 'menu' not hook_menu'.
 */
function crm_api_call() {
  $args = func_get_args();
  civicrm_initialize();
  //  Uncomment return self::error( 'Unknown function invocation' ) in REST.php
  require('CRM/Utils/REST.php');
  array_unshift( $args, null );
  $result = CRM_Utils_Rest::process( $args );
  $csv = drush_get_option('csv');
  if ($csv) {
    foreach ($result as $row) {
      fputcsv (STDOUT, $row);
    }
  } else {
    print_r( $result );
  }
}

function crm_bin_call() {
  $args = func_get_args();
  # e.g. UpdateMembershipRecord.php, CiviMailProcessor.php, civimail.cronjob.php                                                            
  $cmd = $args[0];
  print $cmd;
  # TODO: fix Failed opening required '../civicrm.config.php'                                                                               
  require ('bin/' . $args[0]);
}


function crm_clear_call() {
  civicrm_initialize();
  if ( strlen(CIVICRM_TEMPLATE_COMPILEDIR) > 5 ) {
    $exec = 'rm -r ' . CIVICRM_TEMPLATE_COMPILEDIR;
    system ($exec);
    echo "CiviCRM template dir cleared\n";
  } else {
    echo "Your template dir is probably not configured as it should be: " . CIVICRM_TEMPLATE_COMPILEDIR . "\n";
  }

  require('CRM/Admin/Form/Setting.php');
  CRM_Admin_Form_Setting::rebuildMenu();
  echo "Menu has been rebuilt (and Config.IDS.ini has been deleted).\n";
  drush_core_cache_clear();
}

# todo:
#  http://wiki.civicrm.org/confluence/display/CRMDOC/Command-line+Script+Configuration


Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.

Creative Commons License
Except where otherwise noted, content on this site is licensed under a Creative Commons Attribution-Share Alike 3.0 United States Licence.