Merge pull request #5485 from eileenmcnaughton/4.6
[civicrm-core.git] / CRM / Core / Config.php
index a7f7947646eaff57041a0cad72615eea5edba36f..9237db624a24b3a65d911b0499f083b50791ccbc 100644 (file)
@@ -23,7 +23,7 @@
  | GNU Affero General Public License or the licensing of CiviCRM,     |
  | see the CiviCRM license FAQ at http://civicrm.org/licensing        |
  +--------------------------------------------------------------------+
-*/
+ */
 
 /**
  * Config handles all the run time configuration changes that the system needs to deal with.
@@ -91,7 +91,7 @@ class CRM_Core_Config extends CRM_Core_Config_Variables {
   public $userPermissionClass;
 
   /**
-   * The root directory where Smarty should store compiled files
+   * The root directory where Smarty should store compiled files.
    *
    * @var string
    */
@@ -187,11 +187,12 @@ class CRM_Core_Config extends CRM_Core_Config_Variables {
   /**
    * Singleton function used to manage this object.
    *
-   * @param $loadFromDB boolean  whether to load from the database
-   * @param $force      boolean  whether to force a reconstruction
+   * @param bool $loadFromDB
+   *   whether to load from the database.
+   * @param bool $force
+   *   whether to force a reconstruction.
    *
    * @return CRM_Core_Config
-   * @static
    */
   public static function &singleton($loadFromDB = TRUE, $force = FALSE) {
     if (self::$_singleton === NULL || $force) {
@@ -210,7 +211,7 @@ class CRM_Core_Config extends CRM_Core_Config_Variables {
       self::$_singleton = $cache->get('CRM_Core_Config' . CRM_Core_Config::domainID());
       // if not in cache, fire off config construction
       if (!self::$_singleton) {
-        self::$_singleton = new CRM_Core_Config;
+        self::$_singleton = new CRM_Core_Config();
         self::$_singleton->_initialize($loadFromDB);
 
         //initialize variables. for gencode we cannot load from the
@@ -357,8 +358,8 @@ class CRM_Core_Config extends CRM_Core_Config_Variables {
 
       // also make sure we create the config directory within this directory
       // the below statement will create both the templates directory and the config and log directory
-      $this->configAndLogDir =
-        CRM_Utils_File::baseFilePath($this->templateCompileDir) .
+      $this->configAndLogDir
+        CRM_Utils_File::baseFilePath($this->templateCompileDir) .
         'ConfigAndLog' . DIRECTORY_SEPARATOR;
       CRM_Utils_File::createDir($this->configAndLogDir);
       CRM_Utils_File::restrictAccess($this->configAndLogDir);
@@ -401,7 +402,7 @@ class CRM_Core_Config extends CRM_Core_Config_Variables {
   }
 
   /**
-   * Initialize the DataObject framework
+   * Initialize the DataObject framework.
    *
    * @return void
    */
@@ -417,7 +418,7 @@ class CRM_Core_Config extends CRM_Core_Config_Variables {
   }
 
   /**
-   * Returns the singleton logger for the application
+   * Returns the singleton logger for the application.
    *
    * @param
    *
@@ -432,7 +433,7 @@ class CRM_Core_Config extends CRM_Core_Config_Variables {
   }
 
   /**
-   * Initialize the config variables
+   * Initialize the config variables.
    *
    * @return void
    */
@@ -482,7 +483,7 @@ class CRM_Core_Config extends CRM_Core_Config_Variables {
           $url = CRM_Utils_System::url('civicrm/admin/setting/path', 'reset=1');
           CRM_Core_Session::setStatus(ts('%1 has an incorrect directory path. Please go to the <a href="%2">path setting page</a> and correct it.', array(
             1 => $key,
-            2 => $url
+            2 => $url,
           )), ts('Check Settings'), 'alert');
         }
       }
@@ -525,16 +526,17 @@ class CRM_Core_Config extends CRM_Core_Config_Variables {
       $this->geocodeMethod = 'CRM_Utils_Geocode_' . $this->mapProvider;
     }
 
-    require_once (str_replace('_', DIRECTORY_SEPARATOR, $this->userFrameworkClass) . '.php');
+    require_once str_replace('_', DIRECTORY_SEPARATOR, $this->userFrameworkClass) . '.php';
     $class = $this->userFrameworkClass;
     // redundant with _setUserFrameworkConfig
     $this->userSystem = new $class();
   }
 
   /**
-   * Retrieve a mailer to send any mail from the application
+   * Retrieve a mailer to send any mail from the application.
    *
-   * @param boolean $persist open a persistent smtp connection, should speed up mailings
+   * @param bool $persist
+   *   Open a persistent smtp connection, should speed up mailings.
    * @return object
    */
   public static function &getMailer($persist = FALSE) {
@@ -610,11 +612,13 @@ class CRM_Core_Config extends CRM_Core_Config_Variables {
   }
 
   /**
-   * Create a new instance of a PEAR Mail driver
+   * Create a new instance of a PEAR Mail driver.
    *
-   * @param string $driver 'CRM_Mailing_BAO_Spool' or a name suitable for Mail::factory()
+   * @param string $driver
+   *   'CRM_Mailing_BAO_Spool' or a name suitable for Mail::factory().
    * @param array $params
-   * @return Mail (More specifically, a class which implements the "send()" function)
+   * @return object
+   *   More specifically, a class which implements the "send()" function
    */
   public static function _createMailer($driver, $params) {
     if ($driver == 'CRM_Mailing_BAO_Spool') {
@@ -628,7 +632,7 @@ class CRM_Core_Config extends CRM_Core_Config_Variables {
   }
 
   /**
-   * Deletes the web server writable directories
+   * Deletes the web server writable directories.
    *
    * @param int $value
    *   1: clean templates_c, 2: clean upload, 3: clean both
@@ -650,7 +654,12 @@ class CRM_Core_Config extends CRM_Core_Config_Variables {
 
     // Whether we delete/create or simply preserve directories, we should
     // certainly make sure the restrictions are enforced.
-    foreach (array($this->templateCompileDir, $this->uploadDir, $this->configAndLogDir, $this->customFileUploadDir) as $dir) {
+    foreach (array(
+               $this->templateCompileDir,
+               $this->uploadDir,
+               $this->configAndLogDir,
+               $this->customFileUploadDir,
+             ) as $dir) {
       if ($dir && is_dir($dir)) {
         CRM_Utils_File::restrictAccess($dir);
       }
@@ -658,13 +667,12 @@ class CRM_Core_Config extends CRM_Core_Config_Variables {
   }
 
   /**
-   * Verify that the needed parameters are not null in the config
+   * Verify that the needed parameters are not null in the config.
    *
-   * @param CRM_Core_Config (reference ) the system config object
-   * @param array           (reference ) the parameters that need a value
+   * @param CRM_Core_Config $config (reference) the system config object
+   * @param array $required (reference) the parameters that need a value
    *
-   * @return boolean
-   * @static
+   * @return bool
    */
   public static function check(&$config, &$required) {
     foreach ($required as $name) {
@@ -676,7 +684,7 @@ class CRM_Core_Config extends CRM_Core_Config_Variables {
   }
 
   /**
-   * Reset the serialized array and recompute
+   * Reset the serialized array and recompute.
    * use with care
    */
   public function reset() {
@@ -684,7 +692,9 @@ class CRM_Core_Config extends CRM_Core_Config_Variables {
     CRM_Core_DAO::executeQuery($query);
   }
 
-  // This method should initialize auth sources
+  /**
+   * This method should initialize auth sources.
+   */
   public function initAuthSrc() {
     $session = CRM_Core_Session::singleton();
     if ($session->get('userID') && !$session->get('authSrc')) {
@@ -696,7 +706,7 @@ class CRM_Core_Config extends CRM_Core_Config_Variables {
   }
 
   /**
-   * One function to get domain ID
+   * One function to get domain ID.
    */
   public static function domainID($domainID = NULL, $reset = FALSE) {
     static $domain;
@@ -737,7 +747,8 @@ class CRM_Core_Config extends CRM_Core_Config_Variables {
       $this->userPermissionClass->upgradePermissions(
         CRM_Core_Permission::basicPermissions()
       );
-    } else {
+    }
+    else {
       // Cannot store permissions -- warn if any modules require them
       $modules_with_perms = array();
       foreach ($module_files as $module_file) {
@@ -757,7 +768,7 @@ class CRM_Core_Config extends CRM_Core_Config_Variables {
   }
 
   /**
-   * Flush information about loaded modules
+   * Flush information about loaded modules.
    */
   public function clearModuleList() {
     CRM_Extension_System::singleton()->getCache()->flush();
@@ -767,7 +778,7 @@ class CRM_Core_Config extends CRM_Core_Config_Variables {
   }
 
   /**
-   * Clear db cache
+   * Clear db cache.
    */
   public static function clearDBCache() {
     $queries = array(
@@ -791,25 +802,37 @@ class CRM_Core_Config extends CRM_Core_Config_Variables {
   }
 
   /**
-   * Clear leftover temporary tables
+   * Clear leftover temporary tables.
+   *
+   * This is called on upgrade, during tests and site move, from the cron and via clear caches in the UI.
+   *
+   * Currently the UI clear caches does not pass a time interval - which may need review as it does risk
+   * ripping the tables out from underneath a current action. This was considered but
+   * out-of-scope for CRM-16167
+   *
+   * @param string|bool $timeInterval
+   *   Optional time interval for mysql date function.g '2 day'. This can be used to prevent
+   *   tables created recently from being deleted.
    */
-  public static function clearTempTables() {
-    // CRM-5645
-    $dao = CRM_Core_DAO::executeQuery("SELECT DATABASE();");
+  public static function clearTempTables($timeInterval = FALSE) {
+
+    $dao = new CRM_Core_DAO();
     $query = "
-SELECT TABLE_NAME as tableName
-FROM   INFORMATION_SCHEMA.TABLES
-WHERE  TABLE_SCHEMA = %1
-AND
-  ( TABLE_NAME LIKE 'civicrm_import_job_%'
-  OR       TABLE_NAME LIKE 'civicrm_export_temp%'
-  OR       TABLE_NAME LIKE 'civicrm_task_action_temp%'
-  OR       TABLE_NAME LIKE 'civicrm_report_temp%'
-  )
-";
-
-    $params = array(1 => array($dao->database(), 'String'));
-    $tableDAO = CRM_Core_DAO::executeQuery($query, $params);
+      SELECT TABLE_NAME as tableName
+      FROM   INFORMATION_SCHEMA.TABLES
+      WHERE  TABLE_SCHEMA = %1
+      AND (
+        TABLE_NAME LIKE 'civicrm_import_job_%'
+        OR TABLE_NAME LIKE 'civicrm_export_temp%'
+        OR TABLE_NAME LIKE 'civicrm_task_action_temp%'
+        OR TABLE_NAME LIKE 'civicrm_report_temp%'
+        )
+    ";
+    if ($timeInterval) {
+      $query .= " AND CREATE_TIME < DATE_SUB(NOW(), INTERVAL {$timeInterval})";
+    }
+
+    $tableDAO = CRM_Core_DAO::executeQuery($query, array(1 => array($dao->database(), 'String')));
     $tables = array();
     while ($tableDAO->fetch()) {
       $tables[] = $tableDAO->tableName;
@@ -822,7 +845,7 @@ AND
   }
 
   /**
-   * Check if running in upgrade mode
+   * Check if running in upgrade mode.
    */
   public static function isUpgradeMode($path = NULL) {
     if (defined('CIVICRM_UPGRADE_ACTIVE')) {
@@ -848,7 +871,7 @@ AND
   }
 
   /**
-   * Wrapper function to allow unit tests to switch user framework on the fly
+   * Wrapper function to allow unit tests to switch user framework on the fly.
    */
   public function setUserFramework($userFramework = NULL) {
     $this->userFramework = $userFramework;
@@ -865,4 +888,18 @@ AND
   public static function isEnabledBackOfficeCreditCardPayments() {
     return CRM_Financial_BAO_PaymentProcessor::hasPaymentProcessorSupporting(array('BackOffice'));
   }
+
+  /**
+   * Resets the singleton, so that the next call to CRM_Core_Config::singleton()
+   * reloads completely.
+   *
+   * While normally we could call the singleton function with $force = TRUE,
+   * this function addresses a very specific use-case in the CiviCRM installer,
+   * where we cannot yet force a reload, but we want to make sure that the next
+   * call to this object gets a fresh start (ex: to initialize the DAO).
+   */
+  public function free() {
+    self::$_singleton = NULL;
+  }
+
 }