cleanup fixes
[civicrm-core.git] / bin / cli.class.php
index 73d23978172e29786f14ae88e8f6c78bcf48343a..d1db53c541d28b48bddc9c2690c3de4ba558a623 100644 (file)
@@ -1,7 +1,7 @@
 <?php
 /*
  +--------------------------------------------------------------------+
- | CiviCRM version 4.4                                                |
+ | CiviCRM version 4.6                                                |
  +--------------------------------------------------------------------+
  | Copyright Tech To The People http:tttp.eu (c) 2008                 |
  +--------------------------------------------------------------------+
  * In addition, there are several additional classes that inherit
  * civicrm_cli to do more precise functions.
  *
- **/
+ */
 
 /**
  * base class for doing all command line operations via civicrm
  * used by cli.php
- **/
-
+ */
 class civicrm_cli {
   // required values that must be passed
   // via the command line
@@ -51,6 +50,7 @@ class civicrm_cli {
   // optional arguments
   var $_site = 'localhost';
   var $_user = NULL;
+  var $_password = NULL;
 
   // all other arguments populate the parameters
   // array that is passed to civicrm_api
@@ -58,6 +58,9 @@ class civicrm_cli {
 
   var $_errors = array();
 
+  /**
+   * @return bool
+   */
   public function initialize() {
     if (!$this->_accessing_from_cli()) {
       return FALSE;
@@ -83,6 +86,9 @@ class civicrm_cli {
     }
   }
 
+  /**
+   * @return bool
+   */
   public function callApi() {
     require_once 'api/api.php';
 
@@ -109,6 +115,9 @@ class civicrm_cli {
     return TRUE;
   }
 
+  /**
+   * @return bool
+   */
   private function _parseOptions() {
     $args = $_SERVER['argv'];
     // remove the first argument, which is the name
@@ -128,7 +137,7 @@ class civicrm_cli {
       // find the value of this arg
       if (preg_match('/=/', $arg)) {
         $parts = explode('=', $arg);
-        $arg   = $parts[0];
+        $arg = $parts[0];
         $value = $parts[1];
       }
       else {
@@ -165,7 +174,7 @@ class civicrm_cli {
         $this->_joblog = TRUE;
       }
       else {
-        while(list($short, $long) = each ($this->_additional_arguments)) {
+        while (list($short, $long) = each($this->_additional_arguments)) {
           if ($arg == '-' . $short || $arg == '--' . $long) {
             $property = '_' . $long;
             $this->$property = $value;
@@ -180,13 +189,16 @@ class civicrm_cli {
     return TRUE;
   }
 
+  /**
+   * @return bool
+   */
   private function _bootstrap() {
     // so the configuration works with php-cli
     $_SERVER['PHP_SELF'] = "/index.php";
     $_SERVER['HTTP_HOST'] = $this->_site;
     $_SERVER['REMOTE_ADDR'] = "127.0.0.1";
     $_SERVER['SERVER_SOFTWARE'] = NULL;
-    $_SERVER['REQUEST_METHOD']  = 'GET';
+    $_SERVER['REQUEST_METHOD'] = 'GET';
 
     // SCRIPT_FILENAME needed by CRM_Utils_System::cmsRootPath
     $_SERVER['SCRIPT_FILENAME'] = __FILE__;
@@ -198,20 +210,22 @@ class civicrm_cli {
 
     $civicrm_root = dirname(__DIR__);
     chdir($civicrm_root);
-    require_once ('civicrm.config.php');
+    require_once 'civicrm.config.php';
     // autoload
-    require_once $civicrm_root . '/CRM/Core/ClassLoader.php';
+    if (!class_exists('CRM_Core_ClassLoader')) {
+      require_once $civicrm_root . '/CRM/Core/ClassLoader.php';
+    }
     CRM_Core_ClassLoader::singleton()->register();
 
     $this->_config = CRM_Core_Config::singleton();
-    
+
     // HTTP_HOST will be 'localhost' unless overwritten with the -s argument.
     // Now we have a Config object, we can set it from the Base URL.
     if ($_SERVER['HTTP_HOST'] == 'localhost') {
-        $_SERVER['HTTP_HOST'] = preg_replace(
-                '!^https?://([^/]+)/$!i', 
-                '$1',
-                $this->_config->userFrameworkBaseURL);
+      $_SERVER['HTTP_HOST'] = preg_replace(
+        '!^https?://([^/]+)/$!i',
+        '$1',
+        $this->_config->userFrameworkBaseURL);
     }
 
     $class = 'CRM_Utils_System_' . $this->_config->userFramework;
@@ -230,7 +244,7 @@ class civicrm_cli {
     }
 
     if (!empty($this->_user)) {
-      if(!CRM_Utils_System::authenticateScript(TRUE, $this->_user, $this->_password, TRUE, FALSE, FALSE)) {
+      if (!CRM_Utils_System::authenticateScript(TRUE, $this->_user, $this->_password, TRUE, FALSE, FALSE)) {
         $this->_log(ts("Failed to login as %1. Wrong username or password.", array('1' => $this->_user)));
         return FALSE;
       }
@@ -243,6 +257,9 @@ class civicrm_cli {
     return TRUE;
   }
 
+  /**
+   * @return bool
+   */
   private function _validateOptions() {
     $required = $this->_required_arguments;
     while (list(, $var) = each($required)) {
@@ -257,12 +274,20 @@ class civicrm_cli {
     return TRUE;
   }
 
+  /**
+   * @param $value
+   *
+   * @return string
+   */
   private function _sanitize($value) {
     // restrict user input - we should not be needing anything
     // other than normal alpha numeric plus - and _.
     return trim(preg_replace('#^[^a-zA-Z0-9\-_=/]$#', '', $value));
   }
 
+  /**
+   * @return string
+   */
   private function _getUsage() {
     $out = "Usage: cli.php -e entity -a action [-u user] [-s site] [--output] [PARAMS]\n";
     $out .= "  entity is the name of the entity, e.g. Contact, Event, etc.\n";
@@ -274,6 +299,9 @@ class civicrm_cli {
     return ts($out);
   }
 
+  /**
+   * @param $error
+   */
   private function _log($error) {
     // fixme, this should call some CRM_Core_Error:: function
     // that properly logs
@@ -284,34 +312,35 @@ class civicrm_cli {
 /**
  * class used by csv/export.php to export records from
  * the database in a csv file format.
- **/
-
+ */
 class civicrm_cli_csv_exporter extends civicrm_cli {
   var $separator = ',';
 
-  function __construct() {
+  /**
+   */
+  public function __construct() {
     $this->_required_arguments = array('entity');
     parent::initialize();
   }
 
-  function run() {
+  public function run() {
     $out = fopen("php://output", 'w');
     fputcsv($out, $this->columns, $this->separator, '"');
 
     $this->row = 1;
     $result = civicrm_api($this->_entity, 'Get', $this->_params);
-    $first = true;
+    $first = TRUE;
     foreach ($result['values'] as $row) {
-      if($first) {
+      if ($first) {
         $columns = array_keys($row);
         fputcsv($out, $columns, $this->separator, '"');
-        $first = false;
+        $first = FALSE;
       }
       //handle values returned as arrays (i.e. custom fields that allow multiple selections) by inserting a control character
       foreach ($row as &$field) {
-        if(is_array($field)) {
+        if (is_array($field)) {
           //convert to string
-          $field = implode($field,CRM_Core_DAO::VALUE_SEPARATOR) . CRM_Core_DAO::VALUE_SEPARATOR;
+          $field = implode($field, CRM_Core_DAO::VALUE_SEPARATOR) . CRM_Core_DAO::VALUE_SEPARATOR;
         }
       }
       fputcsv($out, $row, $this->separator, '"');
@@ -326,19 +355,20 @@ class civicrm_cli_csv_exporter extends civicrm_cli {
  * and civicrm_cli_csv_deleter to add or delete
  * records based on those found in a csv file
  * passed to the script.
- **/
-
+ */
 class civicrm_cli_csv_file extends civicrm_cli {
   var $header;
   var $separator = ',';
 
-  function __construct() {
-    $this->_required_arguments = array('entity','file');
+  /**
+   */
+  public function __construct() {
+    $this->_required_arguments = array('entity', 'file');
     $this->_additional_arguments = array('f' => 'file');
     parent::initialize();
   }
 
-  function run() {
+  public function run() {
     $this->row = 1;
     $handle = fopen($this->_file, "r");
 
@@ -362,22 +392,29 @@ class civicrm_cli_csv_file extends civicrm_cli {
     $this->header = $header;
     while (($data = fgetcsv($handle, 0, $this->separator)) !== FALSE) {
       // skip blank lines
-      if(count($data) == 1 && is_null($data[0])) continue;
+      if (count($data) == 1 && is_null($data[0])) {
+        continue;
+      }
       $this->row++;
       $params = $this->convertLine($data);
       $this->processLine($params);
     }
     fclose($handle);
-    return;
+    return NULL;
   }
 
   /* return a params as expected */
-  function convertLine($data) {
+  /**
+   * @param $data
+   *
+   * @return array
+   */
+  public function convertLine($data) {
     $params = array();
     foreach ($this->header as $i => $field) {
-      //split any multiselect data, denoted with CRM_Core_DAO::VALUE_SEPARATOR 
+      //split any multiselect data, denoted with CRM_Core_DAO::VALUE_SEPARATOR
       if (strpos($data[$i], CRM_Core_DAO::VALUE_SEPARATOR) !== FALSE) {
-        $data[$i] = explode(CRM_Core_DAO::VALUE_SEPARATOR,$data[$i]);
+        $data[$i] = explode(CRM_Core_DAO::VALUE_SEPARATOR, $data[$i]);
         $data[$i] = array_combine($data[$i], $data[$i]);
       }
       $params[$field] = $data[$i];
@@ -391,10 +428,12 @@ class civicrm_cli_csv_file extends civicrm_cli {
  * class for processing records to add
  * used by csv/import.php
  *
- **/
-
+ */
 class civicrm_cli_csv_importer extends civicrm_cli_csv_file {
-  function processline($params) {
+  /**
+   * @param array $params
+   */
+  public function processline($params) {
     $result = civicrm_api($this->_entity, 'Create', $params);
     if ($result['is_error']) {
       echo "\nERROR line " . $this->row . ": " . $result['error_message'] . "\n";
@@ -409,14 +448,17 @@ class civicrm_cli_csv_importer extends civicrm_cli_csv_file {
  * class for processing records to delete
  * used by csv/delete.php
  *
- **/
-
+ */
 class civicrm_cli_csv_deleter extends civicrm_cli_csv_file {
-  function processline($params) {
+  /**
+   * @param array $params
+   */
+  public function processline($params) {
     $result = civicrm_api($this->_entity, 'Delete', $params);
     if ($result['is_error']) {
       echo "\nERROR line " . $this->row . ": " . $result['error_message'] . "\n";
-    } else {
+    }
+    else {
       echo "\nline " . $this->row . ": deleted\n";
     }
   }